ProductController.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. /**
  3. * User: admin
  4. * Date Time: 2021/1/15 20:36
  5. */
  6. namespace ghs\controllers;
  7. use biz\admin\classes\AdminRoleClass;
  8. use bizGhs\admin\classes\AdminClass;
  9. use bizGhs\item\classes\ItemClass;
  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\noticeUtil;
  15. use common\components\util;
  16. use Yii;
  17. class ProductController extends BaseController
  18. {
  19. public $guestAccess = ['index'];
  20. //开单时获取所有的分类花材信息
  21. //库存预警
  22. public function actionIndex()
  23. {
  24. $py = Yii::$app->request->get('py', '');
  25. $type = Yii::$app->request->get('type', '');
  26. //获取所有当前供应商的分类 (全部、常用)
  27. //根据分类组装分类下的花材信息
  28. $classIds = ItemClassClass::getGhsItemClassAll($this->sjId);
  29. $where['sjId'] = $this->sjId;
  30. if ($py) {
  31. $pyName = strtolower($py);
  32. $where['py'] = $pyName;
  33. }
  34. $shopId = $this->shopId;
  35. //兼容客户根据门店下单
  36. if ($this->lookShopId) {
  37. $shopId = $this->lookShopId;
  38. }
  39. //库存预警
  40. if ($type == 'waring') {
  41. //shopId 改为可以前端传,默认是当前shopId linqh 2021.1.26
  42. $shopId = Yii::$app->request->get('shopId', 0);
  43. if (!$shopId) {
  44. $shopId = $this->shopId;
  45. }
  46. }
  47. $where['shopId'] = $shopId;
  48. $where['delStatus'] = 0;
  49. $itemInfoData = ProductClass::getAllByCondition($where, ['actualSold' => SORT_DESC, 'inTurn' => SORT_DESC], "*");
  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['sjId'] = $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. $where['classId'] = $classId;
  79. }
  80. $shopId = Yii::$app->request->get('shopId', 0);
  81. if (!$shopId) {
  82. $shopId = $this->shopId;
  83. }
  84. $where['shopId'] = $shopId;
  85. $productIds = Yii::$app->request->get('ids', '');
  86. if (!empty($productIds)) {
  87. $productIdArr = explode(',', $productIds);
  88. if (!empty($productIdArr)) {
  89. $where['id'] = ['in', $productIdArr];
  90. }
  91. }
  92. $type = Yii::$app->request->get('type', '');
  93. $showPage = Yii::$app->request->get('showPage', 0);
  94. if ($showPage) {
  95. $respond = ProductClass::getList("*", $where, "inTurn desc");
  96. $respond['list'] = ProductClass::groupProductInfo($respond['list']);
  97. $respond['list'] = ProductClass::groupClassName($this->sjId, $respond['list']);
  98. util::success($respond);
  99. }
  100. $data = ProductClass::getAllByCondition($where, ['actualSold' => SORT_DESC, 'inTurn' => SORT_DESC], "*");
  101. $data = ProductClass::groupProductInfo($data);
  102. $data = ProductClass::groupClassName($this->sjId, $data);
  103. if ($type == 'waring') {
  104. //库存预警
  105. $newData = [];
  106. foreach ($data as $v) {
  107. if ($v['stock'] <= $v['stockWarning']) {
  108. $newData[] = $v;
  109. }
  110. }
  111. util::success(['list' => $newData]);
  112. }
  113. util::success(['list' => $data]);
  114. }
  115. //改价
  116. public function actionChangePrice()
  117. {
  118. $productId = Yii::$app->request->post('productId', 0);
  119. $price = Yii::$app->request->post('price', '');
  120. if (is_numeric($price) && $price > 0) {
  121. //改价
  122. //判断是否有效
  123. $productData = ProductClass::getProductData($productId, $this->shopId);
  124. if (!$productData) {
  125. util::fail("花材不存在");
  126. }
  127. ProductClass::changePrice($productId, $this->shopId, $price, ProductClass::PRICE_LABEL_CHANGE);
  128. } else {
  129. //自动调价
  130. $productData = ProductClass::getById($productId);
  131. if (!$productData) {
  132. util::fail("花材不存在");
  133. }
  134. $itemId = $productData['itemId'];
  135. //获取该花材的每扎成本价和每扎加价
  136. //自动调价=每扎成本价+每扎加价
  137. $itemData = ItemClass::getItemData($itemId, $this->sjId);
  138. // if (!$itemData) {
  139. // util::fail("出错拉");
  140. // }
  141. // $price = bcadd($itemData['cost'], $itemData['addPrice'], 2);
  142. $price = bcadd($productData['cost'], $productData['addPrice'], 2);
  143. ProductClass::changePrice($productId, $this->shopId, $price, ProductClass::PRICE_LABEL_AUTO);
  144. }
  145. util::success(['price' => $price]);
  146. }
  147. //恢复自动调价
  148. public function actionAutoPrice()
  149. {
  150. $productId = Yii::$app->request->post('productId', 0);
  151. $productData = ProductClass::getById($productId);
  152. if (!$productData) {
  153. util::fail("花材不存在");
  154. }
  155. //$itemId = $productData['itemId'];
  156. //获取该花材的每扎成本价和每扎加价
  157. //自动调价=每扎成本价+每扎加价
  158. // $itemData = ItemClass::getItemData($itemId, $this->sjId);
  159. // if (!$itemData) {
  160. // util::fail("出错拉");
  161. // }
  162. //2021.04.06改为用product中的成本价+加价
  163. $price = bcadd($productData['cost'], $productData['addPrice'], 2);
  164. ProductClass::changePrice($productId, $this->shopId, $price, ProductClass::PRICE_LABEL_AUTO);
  165. util::complete("操作成功");
  166. }
  167. //修改加价
  168. public function actionChangeAddPrice()
  169. {
  170. $productId = Yii::$app->request->post('productId', 0);
  171. $addPrice = Yii::$app->request->post('addPrice', '');
  172. if ($addPrice < 0) {
  173. util::fail("加价不能小于0");
  174. }
  175. $productData = ProductClass::getProductData($productId, $this->shopId);
  176. if (!$productData) {
  177. util::fail("花材不存在");
  178. }
  179. ProductClass::changeAddPrice($productId, $addPrice);
  180. util::complete();
  181. }
  182. //批量修改product 排序,成本价,加价
  183. public function actionBatchUpdate()
  184. {
  185. $data = Yii::$app->request->post('data', ''); //data = [{id:1,inTurn:10,addPrice:2,cost:10}]
  186. $type = Yii::$app->request->post('type', '');
  187. if (empty($data) || empty($type)) {
  188. util::fail("参数错误");
  189. }
  190. if (in_array($type, ['inTurn', 'addPrice', 'cost', 'weight', 'ratio', 'delStatus'])) {
  191. $data = json_decode($data, true);
  192. }
  193. foreach ($data as $v) {
  194. if ($type == 'delStatus') {
  195. $val = $v[$type] ?? '';
  196. if (!in_array($val, [0, 1])) {
  197. util::fail("参数错误");
  198. }
  199. }
  200. }
  201. foreach ($data as $v) {
  202. $id = $v['id'];
  203. $val = $v[$type] ?? '';
  204. $where = [];
  205. $where['id'] = $id;
  206. $where['shopId'] = $this->shopId;
  207. $where['sjId'] = $this->sjId;
  208. if (empty($val)) {
  209. util::fail("参数错误1");
  210. }
  211. $data = [
  212. $type => $val
  213. ];
  214. ProductClass::updateByCondition($where, $data);
  215. ProductClass::autoPriceById($id);
  216. }
  217. util::complete();
  218. }
  219. public function actionUpdate()
  220. {
  221. $post = Yii::$app->request->post();
  222. $shopAdmin = $this->shopAdmin;
  223. $roleId = $shopAdmin['roleId'] ?? 0;
  224. $role = AdminRoleClass::getById($roleId);
  225. $roleName = $role['roleName'] ?? '';
  226. if ($roleName == '员工') {
  227. util::fail('您没有权限操作哦');
  228. }
  229. ProductClass::updateProduct($post);
  230. util::complete();
  231. }
  232. //详情 ruidian 2021.5.3
  233. public function actionDetail()
  234. {
  235. $id = Yii::$app->request->get('id', 0);
  236. $respond = ProductClass::getItemInfo($id);
  237. util::success($respond);
  238. }
  239. //新增花材 2021.6.21
  240. public function actionAdd()
  241. {
  242. $post = Yii::$app->request->post();
  243. $shopAdmin = $this->shopAdmin;
  244. $roleId = $shopAdmin['roleId'] ?? 0;
  245. $role = AdminRoleClass::getById($roleId);
  246. $roleName = $role['roleName'] ?? '';
  247. if ($roleName == '员工') {
  248. util::fail('您没有权限操作哦');
  249. }
  250. $post['sjId'] = $this->sjId;
  251. ProductClass::addProduct($post);
  252. util::complete('添加成功');
  253. }
  254. }