ProductController.php 6.9 KB

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