ProductController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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\stringUtil;
  16. use common\components\util;
  17. use Yii;
  18. class ProductController extends BaseController
  19. {
  20. public $guestAccess = ['index'];
  21. //开单时获取所有的分类花材信息
  22. //库存预警
  23. public function actionIndex()
  24. {
  25. $py = Yii::$app->request->get('py', '');
  26. $type = Yii::$app->request->get('type', '');
  27. //获取所有当前供应商的分类 (全部、常用)
  28. //根据分类组装分类下的花材信息
  29. $classIds = ItemClassClass::getGhsItemClassAll($this->sjId);
  30. $where['sjId'] = $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. $where['delStatus'] = 0;
  50. $itemInfoData = ProductClass::getAllByCondition($where, ['actualSold' => SORT_DESC, 'inTurn' => SORT_DESC], "*");
  51. $itemInfoData = ProductClass::groupProductInfo($itemInfoData);
  52. //常用的itemIds
  53. $oftenItemIds = ProductClass::getOftenItemIds($shopId);
  54. //获取供应商下所有花材ID (itemIds)
  55. $itemIdsInfo = ItemService::getGhsItemIds($this->sjId);
  56. //dd($itemInfoData);
  57. //组装数据: [{classId:'','className:'',child:[{itemInfoData}]}]
  58. $data = ProductService::assembleData($classIds, $itemIdsInfo, $itemInfoData, $oftenItemIds, $type);
  59. util::success($data);
  60. }
  61. //当前门店所有花材
  62. public function actionList()
  63. {
  64. $classId = Yii::$app->request->get('classId', 0);
  65. // $itemInfo = ItemClass::getGhsItemAll($this->sjId);
  66. // $itemIds = array_column($itemInfo, 'itemId');
  67. $where['sjId'] = $this->sjId;
  68. $name = Yii::$app->request->get('name', '');
  69. if (!empty($name)) {
  70. if (stringUtil::isLetter($name)) {
  71. $where['py'] = ['like', $name];
  72. } else {
  73. $where['name'] = ['like', $name];
  74. }
  75. }
  76. if ($classId) {
  77. //获取itemIds
  78. //$itemIds = ItemClass::getItemIdsByClassId($this->sjId, $classId);
  79. //$where['itemId'] = ['in', $itemIds];
  80. $where['classId'] = $classId;
  81. }
  82. $shopId = Yii::$app->request->get('shopId', 0);
  83. if (!$shopId) {
  84. $shopId = $this->shopId;
  85. }
  86. $where['shopId'] = $shopId;
  87. $productIds = Yii::$app->request->get('ids', '');
  88. if (!empty($productIds)) {
  89. $productIdArr = explode(',', $productIds);
  90. if (!empty($productIdArr)) {
  91. $where['id'] = ['in', $productIdArr];
  92. }
  93. }
  94. $type = Yii::$app->request->get('type', '');
  95. $showPage = Yii::$app->request->get('showPage', 0);
  96. if ($showPage) {
  97. $respond = ProductClass::getList("*", $where, "inTurn desc");
  98. $respond['list'] = ProductClass::groupProductInfo($respond['list']);
  99. $respond['list'] = ProductClass::groupClassName($this->sjId, $respond['list']);
  100. util::success($respond);
  101. }
  102. $data = ProductClass::getAllByCondition($where, ['actualSold' => SORT_DESC, 'inTurn' => SORT_DESC], "*");
  103. $data = ProductClass::groupProductInfo($data);
  104. $data = ProductClass::groupClassName($this->sjId, $data);
  105. if ($type == 'waring') {
  106. //库存预警
  107. $newData = [];
  108. foreach ($data as $v) {
  109. if ($v['stock'] <= $v['stockWarning']) {
  110. $newData[] = $v;
  111. }
  112. }
  113. util::success(['list' => $newData]);
  114. }
  115. util::success(['list' => $data]);
  116. }
  117. //改价
  118. public function actionChangePrice()
  119. {
  120. $productId = Yii::$app->request->post('productId', 0);
  121. $price = Yii::$app->request->post('price', '');
  122. if (is_numeric($price) && $price > 0) {
  123. //改价
  124. //判断是否有效
  125. $productData = ProductClass::getProductData($productId, $this->shopId);
  126. if (!$productData) {
  127. util::fail("花材不存在");
  128. }
  129. ProductClass::changePrice($productId, $this->shopId, $price, ProductClass::PRICE_LABEL_CHANGE);
  130. } else {
  131. //自动调价
  132. $productData = ProductClass::getById($productId);
  133. if (!$productData) {
  134. util::fail("花材不存在");
  135. }
  136. $itemId = $productData['itemId'];
  137. //获取该花材的每扎成本价和每扎加价
  138. //自动调价=每扎成本价+每扎加价
  139. $itemData = ItemClass::getItemData($itemId, $this->sjId);
  140. // if (!$itemData) {
  141. // util::fail("出错拉");
  142. // }
  143. // $price = bcadd($itemData['cost'], $itemData['addPrice'], 2);
  144. $price = bcadd($productData['cost'], $productData['addPrice'], 2);
  145. ProductClass::changePrice($productId, $this->shopId, $price, ProductClass::PRICE_LABEL_AUTO);
  146. }
  147. util::success(['price' => $price]);
  148. }
  149. //恢复自动调价
  150. public function actionAutoPrice()
  151. {
  152. $productId = Yii::$app->request->post('productId', 0);
  153. $productData = ProductClass::getById($productId);
  154. if (!$productData) {
  155. util::fail("花材不存在");
  156. }
  157. //$itemId = $productData['itemId'];
  158. //获取该花材的每扎成本价和每扎加价
  159. //自动调价=每扎成本价+每扎加价
  160. // $itemData = ItemClass::getItemData($itemId, $this->sjId);
  161. // if (!$itemData) {
  162. // util::fail("出错拉");
  163. // }
  164. //2021.04.06改为用product中的成本价+加价
  165. $price = bcadd($productData['cost'], $productData['addPrice'], 2);
  166. ProductClass::changePrice($productId, $this->shopId, $price, ProductClass::PRICE_LABEL_AUTO);
  167. util::complete("操作成功");
  168. }
  169. //修改加价
  170. public function actionChangeAddPrice()
  171. {
  172. $productId = Yii::$app->request->post('productId', 0);
  173. $addPrice = Yii::$app->request->post('addPrice', '');
  174. if ($addPrice < 0) {
  175. util::fail("加价不能小于0");
  176. }
  177. $productData = ProductClass::getProductData($productId, $this->shopId);
  178. if (!$productData) {
  179. util::fail("花材不存在");
  180. }
  181. ProductClass::changeAddPrice($productId, $addPrice);
  182. util::complete();
  183. }
  184. //批量修改product 排序,成本价,加价
  185. public function actionBatchUpdate()
  186. {
  187. $shopAdmin = $this->shopAdmin;
  188. $roleId = $shopAdmin['roleId'] ?? 0;
  189. $role = AdminRoleClass::getById($roleId);
  190. $roleName = $role['roleName'] ?? '';
  191. if ($roleName == '员工') {
  192. util::fail('您没有权限操作哦');
  193. }
  194. $data = Yii::$app->request->post('data', ''); //data = [{id:1,inTurn:10,addPrice:2,cost:10}]
  195. $type = Yii::$app->request->post('type', '');
  196. if (empty($data) || empty($type)) {
  197. util::fail("参数错误");
  198. }
  199. if (in_array($type, ['inTurn', 'addPrice', 'cost', 'weight', 'ratio', 'delStatus', 'ratio'])) {
  200. $data = json_decode($data, true);
  201. }
  202. foreach ($data as $v) {
  203. if ($type == 'delStatus') {
  204. $val = $v[$type] ?? '';
  205. if (!in_array($val, [0, 1])) {
  206. util::fail("参数错误");
  207. }
  208. }
  209. }
  210. foreach ($data as $v) {
  211. $id = $v['id'];
  212. $val = $v[$type] ?? '';
  213. if ($type == 'ratio') {
  214. if (preg_match("/^[1-9][0-9]*$/", $val) == false || $val <= 0) {
  215. util::fail('单位比请填写整数,并且要大于0');
  216. }
  217. }
  218. $where = [];
  219. $where['id'] = $id;
  220. $where['shopId'] = $this->shopId;
  221. $where['sjId'] = $this->sjId;
  222. if (empty($val)) {
  223. util::fail("参数错误!");
  224. }
  225. $data = [
  226. $type => $val
  227. ];
  228. ProductClass::updateByCondition($where, $data);
  229. ProductClass::autoPriceById($id);
  230. }
  231. util::complete();
  232. }
  233. public function actionUpdate()
  234. {
  235. $post = Yii::$app->request->post();
  236. $shopAdmin = $this->shopAdmin;
  237. $roleId = $shopAdmin['roleId'] ?? 0;
  238. $role = AdminRoleClass::getById($roleId);
  239. $roleName = $role['roleName'] ?? '';
  240. if ($roleName == '员工') {
  241. util::fail('您没有权限操作哦');
  242. }
  243. ProductClass::updateProduct($post);
  244. util::complete();
  245. }
  246. //详情 ruidian 2021.5.3
  247. public function actionDetail()
  248. {
  249. $id = Yii::$app->request->get('id', 0);
  250. $respond = ProductClass::getItemInfo($id);
  251. util::success($respond);
  252. }
  253. //新增花材 2021.6.21
  254. public function actionAdd()
  255. {
  256. $post = Yii::$app->request->post();
  257. $shopAdmin = $this->shopAdmin;
  258. $roleId = $shopAdmin['roleId'] ?? 0;
  259. $role = AdminRoleClass::getById($roleId);
  260. $roleName = $role['roleName'] ?? '';
  261. if ($roleName == '员工') {
  262. util::fail('您没有权限操作哦');
  263. }
  264. $post['sjId'] = $this->sjId;
  265. ProductClass::addProduct($post);
  266. util::complete('添加成功');
  267. }
  268. }