ProductController.php 7.0 KB

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