ProductController.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /**
  3. * User: admin
  4. * Date Time: 2021/3/2 14:34
  5. */
  6. namespace hd\controllers;
  7. use biz\ghs\classes\GhsClass;
  8. use biz\shop\classes\ShopClass;
  9. use bizGhs\admin\classes\AdminClass;
  10. use bizGhs\custom\classes\CustomClass;
  11. use bizGhs\item\classes\ItemClassClass;
  12. use bizGhs\product\classes\ProductClass;
  13. use bizGhs\product\services\ProductService;
  14. use bizGhs\item\services\ItemService;
  15. use common\components\dict;
  16. use common\components\util;
  17. use Yii;
  18. class ProductController extends BaseController
  19. {
  20. public $guestAccess = ['ghs-item'];
  21. public function actionIndex()
  22. {
  23. $py = Yii::$app->request->get('py', '');
  24. $type = Yii::$app->request->get('type', '');
  25. //获取所有当前供应商的分类 (全部、常用)
  26. //根据分类组装分类下的花材信息
  27. $classIds = ItemClassClass::getGhsItemClassAll($this->sjId);
  28. $where['sjId'] = $this->sjId;
  29. if ($py) {
  30. $pyName = strtolower($py);
  31. $where['py'] = $pyName;
  32. }
  33. $shopId = $this->shopId;
  34. //库存预警
  35. if ($type == 'waring') {
  36. //shopId 改为可以前端传,默认是当前shopId linqh 2021.1.26
  37. $shopId = Yii::$app->request->get('shopId', 0);
  38. if (!$shopId) {
  39. $shopId = $this->shopId;
  40. }
  41. }
  42. $where['shopId'] = $shopId;
  43. $where['delStatus'] = 0;
  44. $itemInfoData = ProductClass::getAllByCondition($where, ['actualSold' => SORT_DESC, 'inTurn' => SORT_DESC], "*");
  45. $itemInfoData = ProductClass::groupProductInfo($itemInfoData);
  46. //常用的itemIds
  47. $oftenItemIds = ProductClass::getOftenItemIds($shopId);
  48. //获取供应商下所有花材ID (itemIds)
  49. $itemIdsInfo = ProductClass::getGhsItemIds($this->sjId);
  50. //dd($itemInfoData);
  51. //组装数据: [{classId:'','className:'',child:[{itemInfoData}]}]
  52. $data = ProductService::assembleData($classIds, $itemIdsInfo, $itemInfoData, $oftenItemIds, $type);
  53. util::success($data);
  54. }
  55. //当前门店所有花材分类
  56. public function actionList()
  57. {
  58. $classId = Yii::$app->request->get('classId', 0);
  59. $pyName = Yii::$app->request->get('py', '');
  60. // $itemInfo = ItemClass::getGhsItemAll($this->sjId);
  61. // $itemIds = array_column($itemInfo, 'itemId');
  62. $where['sjId'] = $this->sjId;
  63. if ($pyName) {
  64. // $pyNameItemIds = ItemClass::getItemIdsByPyName($pyName);
  65. // $itemIds = array_intersect($itemIds, $pyNameItemIds);
  66. $pyName = strtolower($pyName);
  67. $where['py'] = $pyName;
  68. }
  69. $shopId = Yii::$app->request->get('shopId', 0);
  70. if (!$shopId) {
  71. $shopId = $this->shopId;
  72. }
  73. $where['shopId'] = $shopId;
  74. if (!empty($classId)) {
  75. $where['classId'] = $classId;
  76. }
  77. $type = Yii::$app->request->get('type', '');
  78. $showPage = Yii::$app->request->get('showPage', 0);
  79. //需要显示分页
  80. if ($showPage) {
  81. $respond = ProductClass::getList("*", $where, "inTurn desc");
  82. $respond['list'] = ProductClass::groupClassName($this->sjId, $respond['list']);
  83. $respond['list'] = ProductClass::groupProductInfo($respond['list']);
  84. util::success($respond);
  85. }
  86. $data = ProductClass::getAllByCondition($where, "inTurn desc", "*");
  87. $data = ProductClass::groupProductInfo($data);
  88. if ($type == 'waring') {
  89. //库存预警
  90. $newData = [];
  91. foreach ($data as $v) {
  92. if ($v['stock'] <= $v['stockWarning']) {
  93. $newData[] = $v;
  94. }
  95. }
  96. util::success(['list' => $newData]);
  97. }
  98. util::success(['list' => $data]);
  99. }
  100. //从零售端采购入库,查看供应商的某个门店的product
  101. public function actionGhsItem()
  102. {
  103. $get = Yii::$app->request->get();
  104. $id = $get['id'] ?? 0;
  105. if (!empty($id)) {
  106. $ghs = GhsClass::getGhsInfo($id);
  107. if (empty($ghs)) {
  108. util::fail('没有找到供应商');
  109. }
  110. $sjId = $ghs['sjId'] ?? 0;
  111. $shopId = $ghs['shopId'] ?? 0;
  112. $customId = $ghs['customId'] ?? 0;
  113. if (!empty($customId)) {
  114. $custom = CustomClass::getById($customId, true);
  115. if (isset($custom->black) && $custom->black == CustomClass::IS_BLACK) {
  116. util::fail('您已被列入黑名单');
  117. }
  118. }
  119. } else {
  120. $shopId = $get['shopId'] ?? 0;
  121. if (empty($shopId)) {
  122. util::fail('没有供应商信息');
  123. }
  124. $shop = ShopClass::getShopInfo($shopId);
  125. if (empty($shop)) {
  126. util::fail('没有供应商门店');
  127. }
  128. $sjId = $shop['sjId'] ?? 0;
  129. }
  130. $connection = Yii::$app->db;//事务处理
  131. $transaction = $connection->beginTransaction();
  132. try {
  133. //获取所有当前供应商的分类 (全部、常用)
  134. //根据分类组装分类下的花材信息
  135. $classIds = ItemClassClass::getGhsItemClassAll($sjId);
  136. $where['sjId'] = $sjId;
  137. $where['shopId'] = $shopId;
  138. $where['delStatus'] = 0;
  139. $where['status'] = 1;
  140. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], "*");
  141. $itemInfoData = ProductClass::groupProductInfo($itemInfoData);
  142. //常用的itemIds
  143. //$oftenItemIds = ProductClass::getOftenItemIds($shopId);
  144. $oftenItemIds = [];
  145. //获取供应商下所有上架的花材
  146. $itemIdsInfo = ProductClass::getAllList('classId,itemId', ['sjId' => $sjId, 'status' => 1]);
  147. //dd($itemInfoData);
  148. //组装数据: [{classId:'','className:'',child:[{itemInfoData}]}]
  149. $data = ProductService::assembleData($classIds, $itemIdsInfo, $itemInfoData, $oftenItemIds, '', true);
  150. util::success($data);
  151. } catch (\Exception $e) {
  152. $transaction->rollBack();
  153. util::fail();
  154. }
  155. }
  156. public function actionDetail()
  157. {
  158. $id = Yii::$app->request->get('id', 0);
  159. $respond = ProductClass::getItemInfo($id);
  160. util::success($respond);
  161. }
  162. public function actionUpdate()
  163. {
  164. $post = Yii::$app->request->post();
  165. ProductClass::updateProduct($post);
  166. util::complete();
  167. }
  168. //批量修改product 排序,成本价,加价
  169. public function actionBatchUpdate()
  170. {
  171. $data = Yii::$app->request->post('data', ''); //data = [{id:1,inTurn:10,addPrice:2,cost:10}]
  172. $type = Yii::$app->request->post('type', '');
  173. if (empty($data) || empty($type)) {
  174. util::fail("参数错误");
  175. }
  176. if (in_array($type, ['inTurn', 'addPrice', 'cost', 'weight', 'ratio']))
  177. $data = json_decode($data, true);
  178. foreach ($data as $v) {
  179. $id = $v['id'];
  180. $val = $v[$type] ?? '';
  181. $where = [];
  182. $where['id'] = $id;
  183. $where['shopId'] = $this->shopId;
  184. $where['sjId'] = $this->sjId;
  185. if (empty($val)) {
  186. util::fail("参数错误1");
  187. }
  188. $data = [
  189. $type => $val
  190. ];
  191. ProductClass::updateByCondition($where, $data);
  192. ProductClass::autoPriceById($id);
  193. }
  194. util::complete();
  195. }
  196. }