ProductController.php 18 KB

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