ProductController.php 18 KB

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