ProductController.php 20 KB

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