ProductController.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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 biz\ghs\classes\GhsClass;
  9. use bizGhs\custom\classes\CustomClass;
  10. use bizGhs\item\classes\ItemClassClass;
  11. use bizGhs\merchant\classes\ShopClass;
  12. use bizGhs\product\classes\ProductClass;
  13. use bizGhs\product\models\Product;
  14. use bizGhs\product\services\ProductService;
  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. $status = Yii::$app->request->get('status', 1);
  29. //获取所有当前供应商的分类 (全部、常用)
  30. //根据分类组装分类下的花材信息
  31. $classIds = ItemClassClass::getGhsItemClassAll($this->sjId);
  32. $where['sjId'] = $this->sjId;
  33. if ($py) {
  34. $pyName = strtolower($py);
  35. $where['py'] = $pyName;
  36. }
  37. $shopId = $this->shopId;
  38. //兼容客户根据门店下单
  39. if ($this->lookShopId) {
  40. $shopId = $this->lookShopId;
  41. }
  42. //库存预警
  43. if ($type == 'waring') {
  44. //shopId 改为可以前端传,默认是当前shopId linqh 2021.1.26
  45. $shopId = Yii::$app->request->get('shopId', 0);
  46. if (!$shopId) {
  47. $shopId = $this->shopId;
  48. }
  49. }
  50. $where['shopId'] = $shopId;
  51. if (!empty($status)) {
  52. $where['status'] = $status;
  53. }
  54. $where['delStatus'] = 0;
  55. //客户等级
  56. $customId = Yii::$app->request->get('customId', 0);
  57. $level = 1;
  58. if (!empty($customId)) {
  59. $custom = CustomClass::getById($customId, true);
  60. $level = $custom->level ?? 1;
  61. }
  62. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], "*");
  63. $itemInfoData = ProductClass::groupProductInfo($itemInfoData, $level);
  64. $data = ProductService::assembleData($classIds, $itemInfoData, true);
  65. util::success($data);
  66. }
  67. //当前门店所有花材
  68. public function actionList()
  69. {
  70. $where = [];
  71. $where['sjId'] = $this->sjId;
  72. $classId = Yii::$app->request->get('classId', 0);
  73. if (!empty($classId)) {
  74. $where['classId'] = $classId;
  75. }
  76. $name = Yii::$app->request->get('name', '');
  77. if (empty($name)) {
  78. $py = Yii::$app->request->get('py', '');
  79. $name = !empty($py) ? $py : '';
  80. }
  81. if (!empty($name)) {
  82. if (stringUtil::isLetter($name)) {
  83. $where['py'] = ['like', $name];
  84. } else {
  85. $where['name'] = ['like', $name];
  86. }
  87. }
  88. $status = Yii::$app->request->get('status', 0);
  89. if (!empty($status)) {
  90. $where['status'] = $status;
  91. }
  92. $delStatus = Yii::$app->request->get('delStatus', 0);
  93. if ($delStatus != 2) {
  94. $where['delStatus'] = $delStatus;
  95. }
  96. $shopId = Yii::$app->request->get('shopId', 0);
  97. if (!$shopId) {
  98. $shopId = $this->shopId;
  99. }
  100. $where['shopId'] = $shopId;
  101. $productIds = Yii::$app->request->get('ids', '');
  102. if (!empty($productIds)) {
  103. $productIdArr = explode(',', $productIds);
  104. if (!empty($productIdArr)) {
  105. $where['id'] = ['in', $productIdArr];
  106. }
  107. }
  108. //输出标准会员价,即花店价,默认显示的是花店价
  109. $level = 1;
  110. $type = Yii::$app->request->get('type', '');
  111. $showPage = Yii::$app->request->get('showPage', 0);
  112. if ($showPage) {
  113. if ($type == 'waring') {
  114. $pro = Product::find()->where("shopId={$shopId}")->andWhere('`stock`<`stockWarning`')->select('id')->asArray()->all();
  115. $lackIds = array_column($pro, 'id');
  116. if (empty($lackIds)) {
  117. util::success([]);
  118. }
  119. $lackIds = array_filter(array_unique($lackIds));
  120. $where['id'] = ['in', $lackIds];
  121. $respond = ProductClass::getList("*", $where, "actualSold desc");
  122. } else {
  123. $respond = ProductClass::getList("*", $where, "inTurn desc");
  124. }
  125. $respond['list'] = ProductClass::groupProductInfo($respond['list'], $level);
  126. $respond['list'] = ProductClass::groupClassName($this->sjId, $respond['list']);
  127. util::success($respond);
  128. }
  129. if ($type == 'waring') {
  130. //库存预警 按库存从小到大排序
  131. $data = ProductClass::getAllByCondition($where, ['stock' => SORT_ASC, 'inTurn' => SORT_DESC], "*");
  132. } else {
  133. if (!empty($productIds)) {
  134. //优先按 $productIds 排序 "FIELD(`id`,4,5,6)"=>true
  135. $data = ProductClass::getAllByCondition($where, ["FIELD(`id`,$productIds)" => true, 'inTurn' => SORT_DESC], "*");
  136. } else {
  137. $data = ProductClass::getAllByCondition($where, ['actualSold' => SORT_DESC, 'inTurn' => SORT_DESC], "*");
  138. }
  139. }
  140. $data = ProductClass::groupProductInfo($data, $level);
  141. $data = ProductClass::groupClassName($this->sjId, $data);
  142. if ($type == 'waring') {
  143. //库存预警
  144. $newData = [];
  145. foreach ($data as $v) {
  146. if ($v['stock'] <= $v['stockWarning']) {
  147. $newData[] = $v;
  148. }
  149. }
  150. util::success(['list' => $newData]);
  151. }
  152. util::success(['list' => $data]);
  153. }
  154. //批量改价
  155. public function actionBatchChangePrice()
  156. {
  157. $shopAdmin = $this->shopAdmin;
  158. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  159. util::fail('超管才能修改');
  160. }
  161. //花材价格只能在首店修改,并同步到分店
  162. $shop = $this->shop;
  163. $default = $shop->default ?? 0;
  164. if ($default != 1) {
  165. util::fail('请在首店修改');
  166. }
  167. $data = Yii::$app->request->post('data', '');
  168. if (empty($data)) {
  169. util::fail('没有修改');
  170. }
  171. $arr = json_decode($data, true);
  172. if (empty($arr)) {
  173. util::fail('没有修改!');
  174. }
  175. $connection = Yii::$app->db;
  176. $transaction = $connection->beginTransaction();
  177. try {
  178. $ids = array_column($arr, 'id');
  179. $ids = array_unique(array_filter($ids));
  180. if (empty($ids)) {
  181. util::fail('请选择花材');
  182. }
  183. $productList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id', true);
  184. foreach ($arr as $product) {
  185. if (isset($product['price']) == false) {
  186. util::fail('没有价格');
  187. }
  188. if (isset($product['id']) == false) {
  189. util::fail('没有花材');
  190. }
  191. $price = $product['price'];
  192. $productId = $product['id'];
  193. $product = $productList[$productId] ?? [];
  194. if (empty($product)) {
  195. util::fail('没有找到花材');
  196. }
  197. if (isset($product->shopId) == false || $product->shopId != $this->shopId) {
  198. util::fail('没有权限访问');
  199. }
  200. if (empty($price)) {
  201. continue;
  202. }
  203. ProductClass::changePrice($product, $price, ProductClass::PRICE_LABEL_CHANGE);
  204. }
  205. $transaction->commit();
  206. util::complete('修改成功');
  207. } catch (\Exception $e) {
  208. $transaction->rollBack();
  209. Yii::info("修改失败原因:" . $e->getMessage());
  210. util::fail('修改失败');
  211. }
  212. }
  213. //改价
  214. public function actionChangePrice()
  215. {
  216. $shopAdmin = $this->shopAdmin;
  217. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  218. util::fail('超管才能修改');
  219. }
  220. //花材价格只能在首店修改,并同步到分店
  221. $shop = $this->shop;
  222. $default = $shop->default ?? 0;
  223. if ($default != 1) {
  224. util::fail('请在首店修改');
  225. }
  226. $productId = Yii::$app->request->post('productId', 0);
  227. $productInfo = ProductClass::getById($productId, true);
  228. if (empty($productInfo)) {
  229. util::fail("花材不存在");
  230. }
  231. if ($productInfo->shopId != $this->shopId) {
  232. util::fail('没有权限');
  233. }
  234. $price = Yii::$app->request->post('price', '');
  235. if (is_numeric($price) && $price > 0) {
  236. ProductClass::changePrice($productInfo, $price, ProductClass::PRICE_LABEL_CHANGE);
  237. } else {
  238. $price = bcadd($productInfo->cost, $productInfo->addPrice, 2);
  239. ProductClass::changePrice($productInfo, $price, ProductClass::PRICE_LABEL_AUTO);
  240. }
  241. util::success(['price' => $price]);
  242. }
  243. //恢复自动调价
  244. public function actionAutoPrice()
  245. {
  246. $productId = Yii::$app->request->post('productId', 0);
  247. $productData = ProductClass::getById($productId);
  248. if (!$productData) {
  249. util::fail("花材不存在");
  250. }
  251. //2021.04.06改为用product中的成本价+加价
  252. $price = bcadd($productData['cost'], $productData['addPrice'], 2);
  253. ProductClass::changePrice($productId, $this->shopId, $price, ProductClass::PRICE_LABEL_AUTO);
  254. util::complete("操作成功");
  255. }
  256. //修改加价
  257. public function actionChangeAddPrice()
  258. {
  259. $productId = Yii::$app->request->post('productId', 0);
  260. $addPrice = Yii::$app->request->post('addPrice', '');
  261. if ($addPrice < 0) {
  262. util::fail("加价不能小于0");
  263. }
  264. $productData = ProductClass::getProductData($productId, $this->shopId);
  265. if (!$productData) {
  266. util::fail("花材不存在");
  267. }
  268. ProductClass::changeAddPrice($productId, $addPrice);
  269. util::complete();
  270. }
  271. //批量修改加价 shish 20211211
  272. public function actionBatchChangeAddPrice()
  273. {
  274. $addData = Yii::$app->request->post('addData', '');
  275. print_r($addData);
  276. }
  277. //批量修改product 排序,成本价,加价
  278. public function actionBatchUpdate()
  279. {
  280. $shopAdmin = $this->shopAdmin;
  281. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  282. util::fail('超管才能修改');
  283. }
  284. //花材价格只能在首店修改,并同步到分店
  285. $shop = $this->shop;
  286. $default = $shop->default ?? 0;
  287. if ($default != 1) {
  288. util::fail('请在首店修改');
  289. }
  290. $data = Yii::$app->request->post('data', '');
  291. $type = Yii::$app->request->post('type', '');
  292. if (empty($data) || empty($type)) {
  293. util::fail("参数错误");
  294. }
  295. if (in_array($type, ['inTurn', 'addPrice', 'skAddPrice', 'hjAddPrice', 'zsAddPrice', 'cost', 'weight', 'ratio', 'ratio', 'price', 'stockWarning', 'shelfLife']) == false) {
  296. util::fail('非法数据修改');
  297. }
  298. $data = json_decode($data, true);
  299. foreach ($data as $v) {
  300. if ($type == 'status') {
  301. $val = $v[$type] ?? '';
  302. if (!in_array($val, [1, 2])) {
  303. util::fail("参数错误");
  304. }
  305. }
  306. }
  307. foreach ($data as $v) {
  308. $id = $v['id'];
  309. $val = $v[$type] ?? '';
  310. //shish 20210703
  311. if ($type == 'ratio') {
  312. if (preg_match("/^[1-9][0-9]*$/", $val) == false || $val <= 0) {
  313. util::fail('单位比请填写整数,并且要大于0');
  314. }
  315. }
  316. //修改售价 shish 20210703
  317. if ($type == 'price') {
  318. $product = ProductClass::getLockById($id);
  319. $currentName = $product->name ?? '';
  320. if (empty($product)) {
  321. continue;
  322. }
  323. if ($product->shopId != $this->shopId) {
  324. util::fail("不是您的产品");
  325. }
  326. if (empty($val)) {
  327. util::fail("没有填写价格:" . $currentName);
  328. }
  329. if (is_numeric($val) == false) {
  330. util::fail("价格不是数值:" . $currentName);
  331. }
  332. $currentItemId = $product->itemId ?? 0;
  333. if (empty($currentItemId)) {
  334. continue;
  335. }
  336. if (empty($val)) {
  337. continue;
  338. }
  339. //成本价需要修改的花材id
  340. $costChangeIds = Yii::$app->request->post('costChangeIds', []);
  341. if (!empty($costChangeIds)) {
  342. $arrIds = json_decode($costChangeIds, true);
  343. $costIds = is_array($arrIds) ? $arrIds : [];
  344. if (!empty($costIds) && in_array($id, $costIds)) {
  345. $currentAddPrice = bcsub($val, $product->cost, 2);
  346. $skDiff = bcsub($product->skAddPrice, $product->addPrice, 2);
  347. //大众会员价比标准会员价高,所以是相加
  348. $newSkAddPrice = bcadd($currentAddPrice, $skDiff, 2);
  349. $hjDiff = bcsub($product->addPrice, $product->hjAddPrice, 2);
  350. $newHjAddPrice = bcsub($currentAddPrice, $hjDiff, 2);
  351. $zsDiff = bcsub($product->addPrice, $product->zsAddPrice, 2);
  352. $newZsAddPrice = bcsub($currentAddPrice, $zsDiff, 2);
  353. $params = [];
  354. $params['skAddPrice'] = $newSkAddPrice;
  355. $params['addPrice'] = $currentAddPrice;
  356. $params['hjAddPrice'] = $newHjAddPrice;
  357. $params['zsAddPrice'] = $newZsAddPrice;
  358. $sjId = $product->sjId ?? 0;
  359. $shopAll = ShopClass::getAllShop($sjId);
  360. if ($shopAll) {
  361. foreach ($shopAll as $shop) {
  362. $currentId = $shop['id'] ?? 0;
  363. $itemId = $product->itemId ?? 0;
  364. ProductClass::updateByCondition(['shopId' => $currentId, 'itemId' => $itemId], $params);
  365. }
  366. }
  367. }
  368. }
  369. ProductClass::changePrice($product, $val, ProductClass::PRICE_LABEL_CHANGE);
  370. continue;
  371. }
  372. // $where = [];
  373. // $where['id'] = $id;
  374. // $where['shopId'] = $this->shopId;
  375. // $where['sjId'] = $this->sjId;
  376. // if (empty($val)) {
  377. // util::fail("参数错误!");
  378. // }
  379. // $data = [$type => $val];
  380. // ProductClass::updateByCondition($where, $data);
  381. $data = [$type => $val];
  382. $currentProduct = ProductClass::getById($id, true);
  383. if ($currentProduct->shopId != $this->shopId) {
  384. util::fail('非常操作');
  385. }
  386. $currentProduct->$type = $val;
  387. if ($type == 'addPrice') {
  388. $skAddPrice = $currentProduct->skAddPrice;
  389. if ($skAddPrice <= 0) {
  390. $currentProduct->skAddPrice = $val - 1 > 0 ? $val - 1 : 1;
  391. $data['skAddPrice'] = $currentProduct->skAddPrice;
  392. }
  393. $hjAddPrice = $currentProduct->hjAddPrice;
  394. if ($hjAddPrice <= 0) {
  395. $currentProduct->hjAddPrice = $val - 3 > 0 ? $val - 3 : 1;
  396. $data['hjAddPrice'] = $currentProduct->hjAddPrice;
  397. }
  398. $zsAddPrice = $currentProduct->zsAddPrice;
  399. if ($zsAddPrice <= 0) {
  400. $currentProduct->zsAddPrice = $val - 4 > 0 ? $val - 4 : 1;
  401. $data['zsAddPrice'] = $currentProduct->zsAddPrice;
  402. }
  403. }
  404. $currentProduct->save();
  405. //2021.7.6 linqh 如果是首店,则更新全部分店
  406. ProductClass::updateSyncProduct($id, $this->shopId, $data);
  407. }
  408. util::complete();
  409. }
  410. public function actionUpdate()
  411. {
  412. $post = Yii::$app->request->post();
  413. $shopAdmin = $this->shopAdmin;
  414. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  415. util::fail('超管才能修改');
  416. }
  417. $shop = $this->shop;
  418. $default = $shop->default ?? 0;
  419. if ($default != 1) {
  420. //除了上下架其它只能在首店修改
  421. if (isset($post['status']) == false) {
  422. util::fail('请在首店修改');
  423. }
  424. }
  425. $post['adminId'] = $this->adminId;
  426. ProductClass::updateProduct($post);
  427. util::complete();
  428. }
  429. //上下架状态控制 shish 20210713
  430. public function actionStatusUpdate()
  431. {
  432. $post = Yii::$app->request->post();
  433. $shopAdmin = $this->shopAdmin;
  434. $roleId = $shopAdmin['roleId'] ?? 0;
  435. $role = AdminRoleClass::getById($roleId);
  436. $roleName = $role['roleName'] ?? '';
  437. if ($roleName == '员工') {
  438. util::fail('没有权限操作哦');
  439. }
  440. $id = $post['id'] ?? 0;
  441. $status = $post['status'] ?? 1;
  442. $product = ProductClass::getById($id, true);
  443. if ($product->shopId != $this->shopId) {
  444. util::fail('没有权限操作');
  445. }
  446. $product->status = $status;
  447. if ($status == 2) {
  448. $product->discountPrice = 0;
  449. }
  450. $product->save();
  451. util::complete();
  452. }
  453. //花材展示状态控制 shish 20210804
  454. public function actionDelStatusUpdate()
  455. {
  456. $post = Yii::$app->request->post();
  457. $shopAdmin = $this->shopAdmin;
  458. $roleId = $shopAdmin['roleId'] ?? 0;
  459. $role = AdminRoleClass::getById($roleId);
  460. $roleName = $role['roleName'] ?? '';
  461. if ($roleName == '员工') {
  462. util::fail('没有权限操作');
  463. }
  464. $id = $post['id'] ?? 0;
  465. $delStatus = $post['delStatus'] ?? 0;
  466. $product = ProductClass::getById($id, true);
  467. if ($product->shopId != $this->shopId) {
  468. util::fail('没有权限操作');
  469. }
  470. $product->delStatus = $delStatus;
  471. $product->save();
  472. util::complete();
  473. }
  474. //详情 ruidian 2021.5.3
  475. public function actionDetail()
  476. {
  477. $id = Yii::$app->request->get('id', 0);
  478. $respond = ProductClass::getItemInfo($id);
  479. util::success($respond);
  480. }
  481. //新增花材 2021.6.21
  482. public function actionAdd()
  483. {
  484. $post = Yii::$app->request->post();
  485. $shopAdmin = $this->shopAdmin;
  486. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  487. util::fail('超管才能添加花材');
  488. }
  489. $shop = $this->shop;
  490. $default = $shop->default ?? 0;
  491. if ($default != 1) {
  492. util::fail('请在首店添加');
  493. }
  494. $post['sjId'] = $this->sjId;
  495. $post['adminId'] = $this->adminId;
  496. $post['shopId'] = $this->shopId;
  497. $price = $post['price'] ?? 0;
  498. $addPrice = $post['addPrice'] ?? 0;
  499. if ($addPrice > $price) {
  500. util::fail('加价不能大于售价');
  501. }
  502. $cost = bcsub($price, $addPrice, 2);
  503. $post['cost'] = $cost;
  504. ProductClass::addProduct($post);
  505. util::complete('添加成功');
  506. }
  507. //获取拼音缩写 shish 20210707
  508. public function actionPy()
  509. {
  510. $name = Yii::$app->request->get('name');
  511. $py = stringUtil::py($name);
  512. util::success(['py' => $py]);
  513. }
  514. //复制花材
  515. public function actionCopy()
  516. {
  517. $shopAdmin = $this->shopAdmin;
  518. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  519. util::fail('超管才能添加花材');
  520. }
  521. $shop = $this->shop;
  522. $default = $shop->default ?? 0;
  523. if ($default != 1) {
  524. util::fail('请在首店添加');
  525. }
  526. $id = Yii::$app->request->get('id');
  527. $productData = ProductClass::getById($id);
  528. if (!$productData) {
  529. util::fail("花材不存在");
  530. }
  531. if ($productData['shopId'] != $this->shopId) {
  532. util::fail("无效花材");
  533. }
  534. if (isset($productData['variety']) && $productData['variety'] != 0) {
  535. util::fail('特殊花材不能复制');
  536. }
  537. //去除无用的字段
  538. unset($productData['stock']); //库存去掉
  539. unset($productData['viewNum']);
  540. unset($productData['actualSold']);
  541. unset($productData['itemId']);
  542. unset($productData['id']);
  543. unset($productData['delStatus']);
  544. $productData['copy'] = 1; //复制标识
  545. ProductClass::addProduct($productData);
  546. util::complete();
  547. }
  548. //下载价格表 shish 20210922
  549. public function actionGeneratePriceTable()
  550. {
  551. $level = Yii::$app->request->get('level', 0);
  552. $respond = ProductService::generatePriceTable($this->sjId, $this->shopId, $level);
  553. $file = $respond['file'] ?? '';
  554. $shortFile = $respond['shortFile'] ?? '';
  555. $fileUrl = Yii::$app->params['ghsHost'] . $file;
  556. util::success(['file' => $fileUrl, 'shortFile' => $shortFile]);
  557. }
  558. }