ProductController.php 8.0 KB

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