ProductController.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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\util;
  14. use Yii;
  15. class ProductController extends BaseController
  16. {
  17. public $guestAccessAction = ['index'];
  18. //开单时获取所有的分类花材信息
  19. //库存预警
  20. public function actionIndex()
  21. {
  22. $py = Yii::$app->request->get('py', '');
  23. $type = Yii::$app->request->get('type', '');
  24. //访问店铺时用户增加最近访问的店铺 ssh 2021.3.1
  25. AdminClass::addRecentShop($this->lookShopId, $this->admin);
  26. //获取所有当前供应商的分类 (全部、常用)
  27. //根据分类组装分类下的花材信息
  28. $classIds = ItemClassClass::getGhsItemClassAll($this->sjId);
  29. //获取供应商下所有花材ID (itemIds)
  30. $itemIdsInfo = ItemService::getGhsItemIds($this->sjId);
  31. $itemIds = array_column($itemIdsInfo, 'itemId');
  32. if ($py) {
  33. $pyNameItemIds = ItemClass::getItemIdsByPyName($py);
  34. $itemIds = array_intersect($itemIds, $pyNameItemIds);
  35. }
  36. $shopId = $this->shopId;
  37. //兼容客户根据门店下单
  38. if ($this->lookShopId) {
  39. $shopId = $this->lookShopId;
  40. }
  41. //库存预警
  42. if ($type == 'waring') {
  43. //shopId 改为可以前端传,默认是当前shopId linqh 2021.1.26
  44. $shopId = Yii::$app->request->get('shopId', 0);
  45. if (!$shopId) {
  46. $shopId = $this->shopId;
  47. }
  48. $waringItemIds = ProductClass::getItemIdsWarning($this->sjId, $shopId);
  49. $itemIds = array_intersect($itemIds, $waringItemIds);
  50. }
  51. $itemInfoData = ProductClass::getProductInfoByItemIds($itemIds, $shopId);
  52. //常用的itemIds
  53. $oftenItemIds = ProductClass::getOftenItemIds($shopId);
  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. if ($pyName) {
  66. $pyNameItemIds = ItemClass::getItemIdsByPyName($pyName);
  67. $itemIds = array_intersect($itemIds, $pyNameItemIds);
  68. }
  69. $shopId = Yii::$app->request->get('shopId', 0);
  70. if (!$shopId) {
  71. $shopId = $this->shopId;
  72. }
  73. $type = Yii::$app->request->get('type', '');
  74. if ($type == 'waring') {
  75. //库存预警
  76. $waringItemIds = ProductClass::getItemIdsWarning($this->sjId, $shopId);
  77. $itemIds = array_intersect($itemIds, $waringItemIds);
  78. }
  79. $data = ProductClass::getProductInfoByItemIds($itemIds, $shopId);
  80. util::success(['list' => $data]);
  81. }
  82. //改价
  83. public function actionChangePrice()
  84. {
  85. $productId = Yii::$app->request->post('productId', 0);
  86. $price = Yii::$app->request->post('price', '');
  87. if (is_numeric($price) && $price > 0) {
  88. //改价
  89. //判断是否有效
  90. $productData = ProductClass::getProductData($productId, $this->shopId);
  91. if (!$productData) {
  92. util::fail("花材不存在");
  93. }
  94. ProductClass::changePrice($productId, $this->shopId, $price, ProductClass::PRICE_LABEL_CHANGE);
  95. } else {
  96. //自动调价
  97. $productData = ProductClass::getById($productId);
  98. if (!$productData) {
  99. util::fail("花材不存在");
  100. }
  101. $itemId = $productData['itemId'];
  102. //获取该花材的每扎成本价和每扎加价
  103. //自动调价=每扎成本价+每扎加价
  104. $itemData = ItemClass::getItemData($itemId, $this->sjId);
  105. if (!$itemData) {
  106. util::fail("出错拉");
  107. }
  108. $price = bcadd($itemData['cost'], $itemData['addPrice'], 2);
  109. ProductClass::changePrice($productId, $this->shopId, $price, ProductClass::PRICE_LABEL_AUTO);
  110. }
  111. util::success(['price'=>$price]);
  112. }
  113. //恢复自动调价
  114. public function actionAutoPrice()
  115. {
  116. $productId = Yii::$app->request->post('productId', 0);
  117. $productData = ProductClass::getById($productId);
  118. if (!$productData) {
  119. util::fail("花材不存在");
  120. }
  121. //$itemId = $productData['itemId'];
  122. //获取该花材的每扎成本价和每扎加价
  123. //自动调价=每扎成本价+每扎加价
  124. // $itemData = ItemClass::getItemData($itemId, $this->sjId);
  125. // if (!$itemData) {
  126. // util::fail("出错拉");
  127. // }
  128. //2021.04.06改为用product中的成本价+加价
  129. $price = bcadd($productData['cost'], $productData['addPrice'], 2);
  130. ProductClass::changePrice($productId, $this->shopId, $price, ProductClass::PRICE_LABEL_AUTO);
  131. util::complete("操作成功");
  132. }
  133. //修改加价
  134. public function actionChangeAddPrice()
  135. {
  136. $productId = Yii::$app->request->post('productId', 0);
  137. $addPrice = Yii::$app->request->post('addPrice', '');
  138. if($addPrice < 0 ){
  139. util::fail("加价不能小于0");
  140. }
  141. $productData = ProductClass::getProductData($productId, $this->shopId);
  142. if (!$productData) {
  143. util::fail("花材不存在");
  144. }
  145. ProductClass::changeAddPrice($productId,$addPrice);
  146. util::complete();
  147. }
  148. }