ProductController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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\item\classes\ItemClassClass;
  9. use bizGhs\merchant\classes\ShopClass;
  10. use bizGhs\product\classes\ProductClass;
  11. use bizGhs\product\models\Product;
  12. use bizGhs\product\services\ProductService;
  13. use common\components\stringUtil;
  14. use common\components\util;
  15. use Yii;
  16. class ProductController extends BaseController
  17. {
  18. public $guestAccess = ['index'];
  19. //开单时获取所有的分类花材信息
  20. //库存预警
  21. public function actionIndex()
  22. {
  23. $py = Yii::$app->request->get('py', '');
  24. $type = Yii::$app->request->get('type', '');
  25. //默认只显示上架的商品
  26. $status = Yii::$app->request->get('status', 1);
  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. if (!empty($status)) {
  50. $where['status'] = $status;
  51. }
  52. $where['delStatus'] = 0;
  53. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], "*");
  54. $itemInfoData = ProductClass::groupProductInfo($itemInfoData);
  55. //常用的itemIds
  56. $oftenItemIds = [];
  57. //$oftenItemIds = ProductClass::getOftenItemIds($shopId);
  58. //获取供应商下所有花材ID (itemIds)
  59. $itemIdsInfo = ProductClass::getGhsItemIds($this->sjId);
  60. //dd($itemInfoData);
  61. //组装数据: [{classId:'','className:'',child:[{itemInfoData}]}]
  62. $data = ProductService::assembleData($classIds, $itemIdsInfo, $itemInfoData, $oftenItemIds, $type);
  63. util::success($data);
  64. }
  65. //当前门店所有花材
  66. public function actionList()
  67. {
  68. $where = [];
  69. $where['sjId'] = $this->sjId;
  70. $classId = Yii::$app->request->get('classId', 0);
  71. if (!empty($classId)) {
  72. $where['classId'] = $classId;
  73. }
  74. $name = Yii::$app->request->get('name', '');
  75. if (empty($name)) {
  76. $py = Yii::$app->request->get('py', '');
  77. $name = !empty($py) ? $py : '';
  78. }
  79. if (!empty($name)) {
  80. if (stringUtil::isLetter($name)) {
  81. $where['py'] = ['like', $name];
  82. } else {
  83. $where['name'] = ['like', $name];
  84. }
  85. }
  86. $status = Yii::$app->request->get('status', 0);
  87. if (!empty($status)) {
  88. $where['status'] = $status;
  89. }
  90. $delStatus = Yii::$app->request->get('delStatus', '');
  91. if (strlen($delStatus) > 0 && is_numeric($delStatus)) {
  92. $where['delStatus'] = $delStatus;
  93. }
  94. $shopId = Yii::$app->request->get('shopId', 0);
  95. if (!$shopId) {
  96. $shopId = $this->shopId;
  97. }
  98. $where['shopId'] = $shopId;
  99. $productIds = Yii::$app->request->get('ids', '');
  100. if (!empty($productIds)) {
  101. $productIdArr = explode(',', $productIds);
  102. if (!empty($productIdArr)) {
  103. $where['id'] = ['in', $productIdArr];
  104. }
  105. }
  106. $type = Yii::$app->request->get('type', '');
  107. $showPage = Yii::$app->request->get('showPage', 0);
  108. if ($showPage) {
  109. if ($type == 'waring') {
  110. $pro = Product::find()->where("shopId={$shopId}")->andWhere('`stock`<`stockWarning`')->select('id')->asArray()->all();
  111. $lackIds = array_column($pro, 'id');
  112. if (empty($lackIds)) {
  113. util::success([]);
  114. }
  115. $lackIds = array_filter(array_unique($lackIds));
  116. $where['id'] = ['in', $lackIds];
  117. $respond = ProductClass::getList("*", $where, "stock asc");
  118. } else {
  119. $respond = ProductClass::getList("*", $where, "inTurn desc");
  120. }
  121. $respond['list'] = ProductClass::groupProductInfo($respond['list']);
  122. $respond['list'] = ProductClass::groupClassName($this->sjId, $respond['list']);
  123. util::success($respond);
  124. }
  125. if ($type == 'waring') {
  126. //库存预警 按库存从小到大排序
  127. $data = ProductClass::getAllByCondition($where, ['stock' => SORT_ASC, 'inTurn' => SORT_DESC], "*");
  128. } else {
  129. $data = ProductClass::getAllByCondition($where, ['actualSold' => SORT_DESC, 'inTurn' => SORT_DESC], "*");
  130. }
  131. $data = ProductClass::groupProductInfo($data);
  132. $data = ProductClass::groupClassName($this->sjId, $data);
  133. if ($type == 'waring') {
  134. //库存预警
  135. $newData = [];
  136. foreach ($data as $v) {
  137. if ($v['stock'] <= $v['stockWarning']) {
  138. $newData[] = $v;
  139. }
  140. }
  141. util::success(['list' => $newData]);
  142. }
  143. util::success(['list' => $data]);
  144. }
  145. //批量改价
  146. public function actionBatchChangePrice()
  147. {
  148. $shopAdmin = $this->shopAdmin;
  149. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  150. util::fail('超管才能修改');
  151. }
  152. //花材价格只能在首店修改,并同步到分店
  153. $shop = $this->shop;
  154. $default = $shop->default ?? 0;
  155. if ($default != 1) {
  156. util::fail('请在首店修改');
  157. }
  158. $data = Yii::$app->request->post('data', '');
  159. if (empty($data)) {
  160. util::fail('没有修改');
  161. }
  162. $arr = json_decode($data, true);
  163. if (empty($arr)) {
  164. util::fail('没有修改!');
  165. }
  166. $connection = Yii::$app->db;
  167. $transaction = $connection->beginTransaction();
  168. try {
  169. $ids = array_column($arr, 'id');
  170. $ids = array_unique(array_filter($ids));
  171. if (empty($ids)) {
  172. util::fail('请选择花材');
  173. }
  174. $productList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id', true);
  175. foreach ($arr as $product) {
  176. if (isset($product['price']) == false) {
  177. util::fail('没有价格');
  178. }
  179. if (isset($product['id']) == false) {
  180. util::fail('没有花材');
  181. }
  182. $price = $product['price'];
  183. $productId = $product['id'];
  184. $product = $productList[$productId] ?? [];
  185. if (empty($product)) {
  186. util::fail('没有找到花材');
  187. }
  188. if (isset($product->shopId) == false || $product->shopId != $this->shopId) {
  189. util::fail('没有权限访问');
  190. }
  191. if (empty($price)) {
  192. //恢复自动调价
  193. $price = bcadd($product->cost, $product->addPrice, 2);
  194. ProductClass::changePrice($product, $price, ProductClass::PRICE_LABEL_AUTO);
  195. } else {
  196. //改价
  197. ProductClass::changePrice($product, $price, ProductClass::PRICE_LABEL_CHANGE);
  198. }
  199. }
  200. $transaction->commit();
  201. util::complete('修改成功');
  202. } catch (\Exception $e) {
  203. $transaction->rollBack();
  204. Yii::info("修改失败原因:" . $e->getMessage());
  205. util::fail('修改失败');
  206. }
  207. }
  208. //改价
  209. public function actionChangePrice()
  210. {
  211. $shopAdmin = $this->shopAdmin;
  212. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  213. util::fail('超管才能修改');
  214. }
  215. //花材价格只能在首店修改,并同步到分店
  216. $shop = $this->shop;
  217. $default = $shop->default ?? 0;
  218. if ($default != 1) {
  219. util::fail('请在首店修改');
  220. }
  221. $productId = Yii::$app->request->post('productId', 0);
  222. $productInfo = ProductClass::getById($productId, true);
  223. if (empty($productInfo)) {
  224. util::fail("花材不存在");
  225. }
  226. if ($productInfo->shopId != $this->shopId) {
  227. util::fail('没有权限');
  228. }
  229. $price = Yii::$app->request->post('price', '');
  230. if (is_numeric($price) && $price > 0) {
  231. ProductClass::changePrice($productInfo, $price, ProductClass::PRICE_LABEL_CHANGE);
  232. } else {
  233. $price = bcadd($productInfo->cost, $productInfo->addPrice, 2);
  234. ProductClass::changePrice($productInfo, $price, ProductClass::PRICE_LABEL_AUTO);
  235. }
  236. util::success(['price' => $price]);
  237. }
  238. //恢复自动调价
  239. public function actionAutoPrice()
  240. {
  241. $productId = Yii::$app->request->post('productId', 0);
  242. $productData = ProductClass::getById($productId);
  243. if (!$productData) {
  244. util::fail("花材不存在");
  245. }
  246. //2021.04.06改为用product中的成本价+加价
  247. $price = bcadd($productData['cost'], $productData['addPrice'], 2);
  248. ProductClass::changePrice($productId, $this->shopId, $price, ProductClass::PRICE_LABEL_AUTO);
  249. util::complete("操作成功");
  250. }
  251. //修改加价
  252. public function actionChangeAddPrice()
  253. {
  254. $productId = Yii::$app->request->post('productId', 0);
  255. $addPrice = Yii::$app->request->post('addPrice', '');
  256. if ($addPrice < 0) {
  257. util::fail("加价不能小于0");
  258. }
  259. $productData = ProductClass::getProductData($productId, $this->shopId);
  260. if (!$productData) {
  261. util::fail("花材不存在");
  262. }
  263. ProductClass::changeAddPrice($productId, $addPrice);
  264. util::complete();
  265. }
  266. //批量修改product 排序,成本价,加价
  267. public function actionBatchUpdate()
  268. {
  269. $shopAdmin = $this->shopAdmin;
  270. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  271. util::fail('超管才能修改');
  272. }
  273. //花材价格只能在首店修改,并同步到分店
  274. $shop = $this->shop;
  275. $default = $shop->default ?? 0;
  276. if ($default != 1) {
  277. util::fail('请在首店修改');
  278. }
  279. $data = Yii::$app->request->post('data', ''); //data = [{id:1,inTurn:10,addPrice:2,cost:10}]
  280. $type = Yii::$app->request->post('type', '');
  281. if (empty($data) || empty($type)) {
  282. util::fail("参数错误");
  283. }
  284. if (in_array($type, ['inTurn', 'addPrice', 'cost', 'weight', 'ratio', 'ratio', 'price', 'stockWarning', 'shelfLife'])) {
  285. $data = json_decode($data, true);
  286. } else {
  287. util::fail('非法操作');
  288. }
  289. foreach ($data as $v) {
  290. if ($type == 'status') {
  291. $val = $v[$type] ?? '';
  292. if (!in_array($val, [1, 2])) {
  293. util::fail("参数错误");
  294. }
  295. }
  296. }
  297. foreach ($data as $v) {
  298. $id = $v['id'];
  299. $val = $v[$type] ?? '';
  300. //shish 20210703
  301. if ($type == 'ratio') {
  302. if (preg_match("/^[1-9][0-9]*$/", $val) == false || $val <= 0) {
  303. util::fail('单位比请填写整数,并且要大于0');
  304. }
  305. }
  306. //修改售价 shish 20210703
  307. if ($type == 'price') {
  308. $product = ProductClass::getLockById($id);
  309. if (empty($product)) {
  310. continue;
  311. }
  312. $currentItemId = $product->itemId ?? 0;
  313. if (empty($currentItemId)) {
  314. continue;
  315. }
  316. //留空恢复自动调价
  317. //所有门店同步调整
  318. if (empty($val)) {
  319. $currentPrice = bcadd($product->cost, $product->addPrice, 2);
  320. $upData = ['priceLabel' => ProductClass::PRICE_LABEL_AUTO, 'price' => $currentPrice];
  321. } else {
  322. $upData = ['priceLabel' => ProductClass::PRICE_LABEL_CHANGE, 'price' => $val];
  323. }
  324. $shopAll = ShopClass::getAllShop($this->sjId);
  325. if (!empty($shopAll)) {
  326. foreach ($shopAll as $currentShop) {
  327. $currentShopId = $currentShop['id'] ?? 0;
  328. if (empty($currentShopId)) {
  329. continue;
  330. }
  331. ProductClass::updateByCondition(['shopId' => $currentShopId, 'itemId' => $currentItemId], $upData);
  332. }
  333. }
  334. continue;
  335. }
  336. $where = [];
  337. $where['id'] = $id;
  338. $where['shopId'] = $this->shopId;
  339. $where['sjId'] = $this->sjId;
  340. if (empty($val)) {
  341. util::fail("参数错误!");
  342. }
  343. $data = [$type => $val];
  344. ProductClass::updateByCondition($where, $data);
  345. //2021.7.6 linqh 如果是首店,则更新全部分店
  346. ProductClass::updateSyncProduct($id, $this->shopId, $data);
  347. }
  348. util::complete();
  349. }
  350. public function actionUpdate()
  351. {
  352. $post = Yii::$app->request->post();
  353. $shopAdmin = $this->shopAdmin;
  354. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  355. util::fail('超管才能修改');
  356. }
  357. $shop = $this->shop;
  358. $default = $shop->default ?? 0;
  359. if ($default != 1) {
  360. //除了上下架其它只能在首店修改
  361. if (isset($post['status']) == false) {
  362. util::fail('请在首店修改');
  363. }
  364. }
  365. $post['adminId'] = $this->adminId;
  366. ProductClass::updateProduct($post);
  367. util::complete();
  368. }
  369. //上下架状态控制 shish 20210713
  370. public function actionStatusUpdate()
  371. {
  372. $post = Yii::$app->request->post();
  373. $shopAdmin = $this->shopAdmin;
  374. $roleId = $shopAdmin['roleId'] ?? 0;
  375. $role = AdminRoleClass::getById($roleId);
  376. $roleName = $role['roleName'] ?? '';
  377. if ($roleName == '员工') {
  378. util::fail('没有权限操作哦');
  379. }
  380. $id = $post['id'] ?? 0;
  381. $status = $post['status'] ?? 1;
  382. $product = ProductClass::getById($id, true);
  383. if ($product->shopId != $this->shopId) {
  384. util::fail('没有权限操作');
  385. }
  386. $product->status = $status;
  387. $product->save();
  388. util::complete();
  389. }
  390. //花材展示状态控制 shish 20210804
  391. public function actionDelStatusUpdate()
  392. {
  393. $post = Yii::$app->request->post();
  394. $shopAdmin = $this->shopAdmin;
  395. $roleId = $shopAdmin['roleId'] ?? 0;
  396. $role = AdminRoleClass::getById($roleId);
  397. $roleName = $role['roleName'] ?? '';
  398. if ($roleName == '员工') {
  399. util::fail('没有权限操作');
  400. }
  401. $id = $post['id'] ?? 0;
  402. $delStatus = $post['delStatus'] ?? 0;
  403. $product = ProductClass::getById($id, true);
  404. if ($product->shopId != $this->shopId) {
  405. util::fail('没有权限操作');
  406. }
  407. $product->delStatus = $delStatus;
  408. $product->save();
  409. util::complete();
  410. }
  411. //详情 ruidian 2021.5.3
  412. public function actionDetail()
  413. {
  414. $id = Yii::$app->request->get('id', 0);
  415. $respond = ProductClass::getItemInfo($id);
  416. util::success($respond);
  417. }
  418. //新增花材 2021.6.21
  419. public function actionAdd()
  420. {
  421. $post = Yii::$app->request->post();
  422. $shopAdmin = $this->shopAdmin;
  423. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  424. util::fail('超管才能添加花材');
  425. }
  426. $shop = $this->shop;
  427. $default = $shop->default ?? 0;
  428. if ($default != 1) {
  429. util::fail('请在首店添加');
  430. }
  431. $post['sjId'] = $this->sjId;
  432. $post['adminId'] = $this->adminId;
  433. $post['shopId'] = $this->shopId;
  434. ProductClass::addProduct($post);
  435. util::complete('添加成功');
  436. }
  437. //获取拼音缩写 shish 20210707
  438. public function actionPy()
  439. {
  440. $name = Yii::$app->request->get('name');
  441. $py = stringUtil::py($name);
  442. util::success(['py' => $py]);
  443. }
  444. //复制花材
  445. public function actionCopy()
  446. {
  447. $shopAdmin = $this->shopAdmin;
  448. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  449. util::fail('超管才能添加花材');
  450. }
  451. $shop = $this->shop;
  452. $default = $shop->default ?? 0;
  453. if ($default != 1) {
  454. util::fail('请在首店添加');
  455. }
  456. $id = Yii::$app->request->get('id');
  457. $productData = ProductClass::getById($id);
  458. if (!$productData) {
  459. util::fail("花材不存在");
  460. }
  461. if ($productData['shopId'] != $this->shopId) {
  462. util::fail("无效花材");
  463. }
  464. //去除无用的字段
  465. unset($productData['stock']); //库存去掉
  466. unset($productData['viewNum']);
  467. unset($productData['actualSold']);
  468. unset($productData['itemId']);
  469. unset($productData['id']);
  470. unset($productData['delStatus']);
  471. $productData['copy'] = 1; //复制标识
  472. ProductClass::addProduct($productData);
  473. util::complete();
  474. }
  475. }