ProductController.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. /**
  3. * User: admin
  4. * Date Time: 2021/1/15 20:36
  5. */
  6. namespace ghs\controllers;
  7. use bizGhs\admin\classes\AdminClass;
  8. use bizGhs\item\classes\ItemClass;
  9. use bizGhs\item\classes\ItemClassClass;
  10. use bizGhs\product\classes\ProductClass;
  11. use bizGhs\product\services\ProductService;
  12. use bizGhs\item\services\ItemService;
  13. use common\components\noticeUtil;
  14. use common\components\util;
  15. use Yii;
  16. class ProductController extends BaseController
  17. {
  18. public $guestAccess = ['index'];
  19. //开单时获取所有的分类花材信息
  20. //库存预警
  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 ($this->lookShopId) {
  36. $shopId = $this->lookShopId;
  37. }
  38. //库存预警
  39. if ($type == 'waring') {
  40. //shopId 改为可以前端传,默认是当前shopId linqh 2021.1.26
  41. $shopId = Yii::$app->request->get('shopId', 0);
  42. if (!$shopId) {
  43. $shopId = $this->shopId;
  44. }
  45. }
  46. $where['shopId'] = $shopId;
  47. $itemInfoData = ProductClass::getAllByCondition($where, null, "*");
  48. $itemInfoData = ProductClass::groupProductInfo($itemInfoData);
  49. //常用的itemIds
  50. $oftenItemIds = ProductClass::getOftenItemIds($shopId);
  51. //获取供应商下所有花材ID (itemIds)
  52. $itemIdsInfo = ItemService::getGhsItemIds($this->sjId);
  53. //dd($itemInfoData);
  54. //组装数据: [{classId:'','className:'',child:[{itemInfoData}]}]
  55. $data = ProductService::assembleData($classIds, $itemIdsInfo, $itemInfoData, $oftenItemIds, $type);
  56. util::success($data);
  57. }
  58. //当前门店所有花材
  59. public function actionList()
  60. {
  61. $classId = Yii::$app->request->get('classId', 0);
  62. $pyName = Yii::$app->request->get('py', '');
  63. // $itemInfo = ItemClass::getGhsItemAll($this->sjId);
  64. // $itemIds = array_column($itemInfo, 'itemId');
  65. $where['sjId'] = $this->sjId;
  66. if ($pyName) {
  67. // $pyNameItemIds = ItemClass::getItemIdsByPyName($pyName);
  68. // $itemIds = array_intersect($itemIds, $pyNameItemIds);
  69. $pyName = strtolower($pyName);
  70. $where['py'] = $pyName;
  71. }
  72. if ($classId) {
  73. //获取itemIds
  74. $itemIds = ItemClass::getItemIdsByClassId($this->sjId, $classId);
  75. $where['itemId'] = ['in', $itemIds];
  76. }
  77. $shopId = Yii::$app->request->get('shopId', 0);
  78. if (!$shopId) {
  79. $shopId = $this->shopId;
  80. }
  81. $where['shopId'] = $shopId;
  82. $productIds = Yii::$app->request->get('ids','');
  83. if(!empty($productIds)){
  84. $productIdArr = explode(',',$productIds);
  85. if(!empty($productIdArr)){
  86. $where['id'] = ['in', $productIdArr];
  87. }
  88. }
  89. $type = Yii::$app->request->get('type', '');
  90. $showPage = Yii::$app->request->get('showPage', 0);
  91. if ($showPage) {
  92. $respond = ProductClass::getList("*", $where, "inTurn desc");
  93. $respond['list'] = ProductClass::groupProductInfo($respond['list']);
  94. $respond['list'] = ProductClass::groupClassName($this->sjId, $respond['list']);
  95. util::success($respond);
  96. }
  97. $data = ProductClass::getAllByCondition($where, "inTurn desc", "*");
  98. $data = ProductClass::groupProductInfo($data);
  99. $data = ProductClass::groupClassName($this->sjId, $data);
  100. if ($type == 'waring') {
  101. //库存预警
  102. $newData = [];
  103. foreach ($data as $v) {
  104. if ($v['stock'] <= $v['stockWarning']) {
  105. $newData[] = $v;
  106. }
  107. }
  108. util::success(['list' => $newData]);
  109. }
  110. util::success(['list' => $data]);
  111. }
  112. //改价
  113. public function actionChangePrice()
  114. {
  115. $productId = Yii::$app->request->post('productId', 0);
  116. $price = Yii::$app->request->post('price', '');
  117. if (is_numeric($price) && $price > 0) {
  118. //改价
  119. //判断是否有效
  120. $productData = ProductClass::getProductData($productId, $this->shopId);
  121. if (!$productData) {
  122. util::fail("花材不存在");
  123. }
  124. ProductClass::changePrice($productId, $this->shopId, $price, ProductClass::PRICE_LABEL_CHANGE);
  125. } else {
  126. //自动调价
  127. $productData = ProductClass::getById($productId);
  128. if (!$productData) {
  129. util::fail("花材不存在");
  130. }
  131. $itemId = $productData['itemId'];
  132. //获取该花材的每扎成本价和每扎加价
  133. //自动调价=每扎成本价+每扎加价
  134. $itemData = ItemClass::getItemData($itemId, $this->sjId);
  135. // if (!$itemData) {
  136. // util::fail("出错拉");
  137. // }
  138. // $price = bcadd($itemData['cost'], $itemData['addPrice'], 2);
  139. $price = bcadd($productData['cost'], $productData['addPrice'], 2);
  140. ProductClass::changePrice($productId, $this->shopId, $price, ProductClass::PRICE_LABEL_AUTO);
  141. }
  142. util::success(['price' => $price]);
  143. }
  144. //恢复自动调价
  145. public function actionAutoPrice()
  146. {
  147. $productId = Yii::$app->request->post('productId', 0);
  148. $productData = ProductClass::getById($productId);
  149. if (!$productData) {
  150. util::fail("花材不存在");
  151. }
  152. //$itemId = $productData['itemId'];
  153. //获取该花材的每扎成本价和每扎加价
  154. //自动调价=每扎成本价+每扎加价
  155. // $itemData = ItemClass::getItemData($itemId, $this->sjId);
  156. // if (!$itemData) {
  157. // util::fail("出错拉");
  158. // }
  159. //2021.04.06改为用product中的成本价+加价
  160. $price = bcadd($productData['cost'], $productData['addPrice'], 2);
  161. ProductClass::changePrice($productId, $this->shopId, $price, ProductClass::PRICE_LABEL_AUTO);
  162. util::complete("操作成功");
  163. }
  164. //修改加价
  165. public function actionChangeAddPrice()
  166. {
  167. $productId = Yii::$app->request->post('productId', 0);
  168. $addPrice = Yii::$app->request->post('addPrice', '');
  169. if ($addPrice < 0) {
  170. util::fail("加价不能小于0");
  171. }
  172. $productData = ProductClass::getProductData($productId, $this->shopId);
  173. if (!$productData) {
  174. util::fail("花材不存在");
  175. }
  176. ProductClass::changeAddPrice($productId, $addPrice);
  177. util::complete();
  178. }
  179. //批量修改product 排序,成本价,加价
  180. public function actionBatchUpdate()
  181. {
  182. $data = Yii::$app->request->post('data', ''); //data = [{id:1,inTurn:10,addPrice:2,cost:10}]
  183. $type = Yii::$app->request->post('type', '');
  184. if (empty($data) || empty($type)) {
  185. util::fail("参数错误");
  186. }
  187. if (in_array($type, ['inTurn', 'addPrice', 'cost', 'weight', 'ratio']))
  188. $data = json_decode($data, true);
  189. foreach ($data as $v) {
  190. $id = $v['id'];
  191. $val = $v[$type] ?? '';
  192. $where = [];
  193. $where['id'] = $id;
  194. $where['shopId'] = $this->shopId;
  195. $where['sjId'] = $this->sjId;
  196. if (empty($val)) {
  197. util::fail("参数错误1");
  198. }
  199. $data = [
  200. $type => $val
  201. ];
  202. ProductClass::updateByCondition($where, $data);
  203. ProductClass::autoPriceById($id);
  204. }
  205. util::complete();
  206. }
  207. public function actionUpdate()
  208. {
  209. $post = Yii::$app->request->post();
  210. ProductClass::updateProduct($post);
  211. util::complete();
  212. }
  213. //详情 ruidian 2021.5.3
  214. public function actionDetail()
  215. {
  216. $id = Yii::$app->request->get('id', 0);
  217. $respond = ProductClass::getItemInfo($id);
  218. util::success($respond);
  219. }
  220. }