ProductController.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. $where['delStatus'] = 0;
  48. $itemInfoData = ProductClass::getAllByCondition($where, ['actualSold'=>SORT_DESC,'inTurn'=>SORT_DESC], "*");
  49. $itemInfoData = ProductClass::groupProductInfo($itemInfoData);
  50. //常用的itemIds
  51. $oftenItemIds = ProductClass::getOftenItemIds($shopId);
  52. //获取供应商下所有花材ID (itemIds)
  53. $itemIdsInfo = ItemService::getGhsItemIds($this->sjId);
  54. //dd($itemInfoData);
  55. //组装数据: [{classId:'','className:'',child:[{itemInfoData}]}]
  56. $data = ProductService::assembleData($classIds, $itemIdsInfo, $itemInfoData, $oftenItemIds, $type);
  57. util::success($data);
  58. }
  59. //当前门店所有花材
  60. public function actionList()
  61. {
  62. $classId = Yii::$app->request->get('classId', 0);
  63. $pyName = Yii::$app->request->get('py', '');
  64. // $itemInfo = ItemClass::getGhsItemAll($this->sjId);
  65. // $itemIds = array_column($itemInfo, 'itemId');
  66. $where['sjId'] = $this->sjId;
  67. if ($pyName) {
  68. // $pyNameItemIds = ItemClass::getItemIdsByPyName($pyName);
  69. // $itemIds = array_intersect($itemIds, $pyNameItemIds);
  70. $pyName = strtolower($pyName);
  71. $where['py'] = $pyName;
  72. }
  73. if ($classId) {
  74. //获取itemIds
  75. $itemIds = ItemClass::getItemIdsByClassId($this->sjId, $classId);
  76. $where['itemId'] = ['in', $itemIds];
  77. }
  78. $shopId = Yii::$app->request->get('shopId', 0);
  79. if (!$shopId) {
  80. $shopId = $this->shopId;
  81. }
  82. $where['shopId'] = $shopId;
  83. $productIds = Yii::$app->request->get('ids','');
  84. if(!empty($productIds)){
  85. $productIdArr = explode(',',$productIds);
  86. if(!empty($productIdArr)){
  87. $where['id'] = ['in', $productIdArr];
  88. }
  89. }
  90. $type = Yii::$app->request->get('type', '');
  91. $showPage = Yii::$app->request->get('showPage', 0);
  92. if ($showPage) {
  93. $respond = ProductClass::getList("*", $where, "inTurn desc");
  94. $respond['list'] = ProductClass::groupProductInfo($respond['list']);
  95. $respond['list'] = ProductClass::groupClassName($this->sjId, $respond['list']);
  96. util::success($respond);
  97. }
  98. $data = ProductClass::getAllByCondition($where, ['actualSold'=>SORT_DESC,'inTurn'=>SORT_DESC], "*");
  99. $data = ProductClass::groupProductInfo($data);
  100. $data = ProductClass::groupClassName($this->sjId, $data);
  101. if ($type == 'waring') {
  102. //库存预警
  103. $newData = [];
  104. foreach ($data as $v) {
  105. if ($v['stock'] <= $v['stockWarning']) {
  106. $newData[] = $v;
  107. }
  108. }
  109. util::success(['list' => $newData]);
  110. }
  111. util::success(['list' => $data]);
  112. }
  113. //改价
  114. public function actionChangePrice()
  115. {
  116. $productId = Yii::$app->request->post('productId', 0);
  117. $price = Yii::$app->request->post('price', '');
  118. if (is_numeric($price) && $price > 0) {
  119. //改价
  120. //判断是否有效
  121. $productData = ProductClass::getProductData($productId, $this->shopId);
  122. if (!$productData) {
  123. util::fail("花材不存在");
  124. }
  125. ProductClass::changePrice($productId, $this->shopId, $price, ProductClass::PRICE_LABEL_CHANGE);
  126. } else {
  127. //自动调价
  128. $productData = ProductClass::getById($productId);
  129. if (!$productData) {
  130. util::fail("花材不存在");
  131. }
  132. $itemId = $productData['itemId'];
  133. //获取该花材的每扎成本价和每扎加价
  134. //自动调价=每扎成本价+每扎加价
  135. $itemData = ItemClass::getItemData($itemId, $this->sjId);
  136. // if (!$itemData) {
  137. // util::fail("出错拉");
  138. // }
  139. // $price = bcadd($itemData['cost'], $itemData['addPrice'], 2);
  140. $price = bcadd($productData['cost'], $productData['addPrice'], 2);
  141. ProductClass::changePrice($productId, $this->shopId, $price, ProductClass::PRICE_LABEL_AUTO);
  142. }
  143. util::success(['price' => $price]);
  144. }
  145. //恢复自动调价
  146. public function actionAutoPrice()
  147. {
  148. $productId = Yii::$app->request->post('productId', 0);
  149. $productData = ProductClass::getById($productId);
  150. if (!$productData) {
  151. util::fail("花材不存在");
  152. }
  153. //$itemId = $productData['itemId'];
  154. //获取该花材的每扎成本价和每扎加价
  155. //自动调价=每扎成本价+每扎加价
  156. // $itemData = ItemClass::getItemData($itemId, $this->sjId);
  157. // if (!$itemData) {
  158. // util::fail("出错拉");
  159. // }
  160. //2021.04.06改为用product中的成本价+加价
  161. $price = bcadd($productData['cost'], $productData['addPrice'], 2);
  162. ProductClass::changePrice($productId, $this->shopId, $price, ProductClass::PRICE_LABEL_AUTO);
  163. util::complete("操作成功");
  164. }
  165. //修改加价
  166. public function actionChangeAddPrice()
  167. {
  168. $productId = Yii::$app->request->post('productId', 0);
  169. $addPrice = Yii::$app->request->post('addPrice', '');
  170. if ($addPrice < 0) {
  171. util::fail("加价不能小于0");
  172. }
  173. $productData = ProductClass::getProductData($productId, $this->shopId);
  174. if (!$productData) {
  175. util::fail("花材不存在");
  176. }
  177. ProductClass::changeAddPrice($productId, $addPrice);
  178. util::complete();
  179. }
  180. //批量修改product 排序,成本价,加价
  181. public function actionBatchUpdate()
  182. {
  183. $data = Yii::$app->request->post('data', ''); //data = [{id:1,inTurn:10,addPrice:2,cost:10}]
  184. $type = Yii::$app->request->post('type', '');
  185. if (empty($data) || empty($type)) {
  186. util::fail("参数错误");
  187. }
  188. if (in_array($type, ['inTurn', 'addPrice', 'cost', 'weight', 'ratio','delStatus'])){
  189. $data = json_decode($data, true);
  190. }
  191. foreach ($data as $v) {
  192. if($type=='delStatus'){
  193. $val = $v[$type] ?? '';
  194. if(!in_array($val,[0,1])){
  195. util::fail("参数错误");
  196. }
  197. }
  198. }
  199. foreach ($data as $v) {
  200. $id = $v['id'];
  201. $val = $v[$type] ?? '';
  202. $where = [];
  203. $where['id'] = $id;
  204. $where['shopId'] = $this->shopId;
  205. $where['sjId'] = $this->sjId;
  206. if (empty($val)) {
  207. util::fail("参数错误1");
  208. }
  209. $data = [
  210. $type => $val
  211. ];
  212. ProductClass::updateByCondition($where, $data);
  213. ProductClass::autoPriceById($id);
  214. }
  215. util::complete();
  216. }
  217. public function actionUpdate()
  218. {
  219. $post = Yii::$app->request->post();
  220. ProductClass::updateProduct($post);
  221. util::complete();
  222. }
  223. //详情 ruidian 2021.5.3
  224. public function actionDetail()
  225. {
  226. $id = Yii::$app->request->get('id', 0);
  227. $respond = ProductClass::getItemInfo($id);
  228. util::success($respond);
  229. }
  230. //新增花材 2021.6.21
  231. public function actionAdd()
  232. {
  233. $post = Yii::$app->request->post();
  234. ProductClass::addProduct($post);
  235. util::complete('添加成功');
  236. }
  237. }