ProductController.php 11 KB

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