ProductController.php 19 KB

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