ProductController.php 10 KB

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