ProductController.php 11 KB

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