ProductController.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. <?php
  2. namespace hd\controllers;
  3. use biz\ghs\classes\GhsClass;
  4. use biz\item\classes\PtItemClass;
  5. use biz\shop\classes\ShopClass;
  6. use biz\shop\classes\ShopExtClass;
  7. use bizGhs\item\classes\ItemClassClass;
  8. use bizGhs\product\classes\ProductClass;
  9. use bizGhs\product\services\ProductService;
  10. use bizHd\admin\classes\ShopAdminClass;
  11. use bizHd\item\classes\ItemClass;
  12. use bizHd\purchase\classes\PurchaseClass;
  13. use common\components\printUtil;
  14. use common\components\stringUtil;
  15. use common\components\util;
  16. use Yii;
  17. class ProductController extends BaseController
  18. {
  19. public $guestAccess = ['ghs-item', 'ghs-product-detail'];
  20. //打印花材的标签 ssh 20220602
  21. public function actionPrint()
  22. {
  23. $get = Yii::$app->request->get();
  24. $id = $get['id'] ?? 0;
  25. $num = $get['num'] ?? 1;
  26. $info = ProductClass::getById($id, true);
  27. if (empty($info)) {
  28. util::fail('没有找到花材');
  29. }
  30. $name = $info->name ?? '';
  31. $ptItemId = $info->itemId ?? 0;
  32. $shopId = $this->shopId;
  33. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  34. $printLabelSn = $ext->printLabelSn ?? '';
  35. if (empty($printLabelSn)) {
  36. util::fail('请设置标签打印机');
  37. }
  38. $p = new printUtil($printLabelSn);
  39. $unit = $info->ratio . $info->smallUnit . '/' . $info->bigUnit;
  40. if (isset($info->ratioType) && $info->ratioType == 1) {
  41. $unit = '若干' . $info->smallUnit . '/' . $info->bigUnit;
  42. }
  43. $p->times = $num;
  44. $content = '<TEXT x="360" y="30" font="12" w="2" h="2" r="90">' . $name . '</TEXT>';
  45. $content .= '<BC128 x="260" y="30" h="70" s="1" r="90" n="4" w="11">' . $ptItemId . '</BC128>';
  46. $content .= '<TEXT x="60" y="30" font="12" w="2" h="2" r="90">' . $unit . '</TEXT>';
  47. $p->printLabelMsg($content);
  48. util::complete();
  49. }
  50. //新增花材 2021.6.21
  51. public function actionAdd()
  52. {
  53. $post = Yii::$app->request->post();
  54. $shop = $this->shop ?? [];
  55. $pfShopId = $shop->pfShopId ?? 0;
  56. if (!empty($pfShopId)) {
  57. util::fail('请在批发端操作');
  58. }
  59. $shopAdmin = $this->shopAdmin;
  60. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  61. util::fail('管理员才能添加花材');
  62. }
  63. $post['sjId'] = $this->sjId;
  64. $post['adminId'] = $this->adminId;
  65. $post['mainId'] = $this->mainId;
  66. $price = $post['price'] ?? 0;
  67. if ($price <= 0) {
  68. util::fail('请填写价格');
  69. }
  70. $addPrice = $post['addPrice'] ?? 0;
  71. if ($addPrice > $price) {
  72. util::fail('加价不能大于售价');
  73. }
  74. $post['skPrice'] = $price;
  75. $cost = bcsub($price, $addPrice, 2);
  76. $post['cost'] = $cost;
  77. ItemClass::addItem($post, $this->shop);
  78. util::complete('添加成功');
  79. }
  80. //获取拼音缩写 ssh 20210707
  81. public function actionPy()
  82. {
  83. $name = Yii::$app->request->get('name');
  84. $py = stringUtil::py($name);
  85. util::success(['py' => $py]);
  86. }
  87. //所有花材
  88. public function actionAll()
  89. {
  90. $where = [];
  91. $where['mainId'] = $this->mainId;
  92. $classId = Yii::$app->request->get('classId', 0);
  93. if (!empty($classId)) {
  94. $where['classId'] = $classId;
  95. }
  96. $status = Yii::$app->request->get('status', 0);
  97. if (!empty($status)) {
  98. $where['status'] = $status;
  99. }
  100. $delStatus = Yii::$app->request->get('delStatus', 0);
  101. if ($delStatus != 2) {
  102. $where['delStatus'] = $delStatus;
  103. }
  104. //零售价
  105. $level = 0;
  106. $data = ProductClass::getAllByCondition($where, ['actualSold' => SORT_DESC, 'inTurn' => SORT_DESC], "*");
  107. $data = ProductClass::groupProductInfo($data, $level);
  108. util::success(['list' => $data]);
  109. }
  110. public function actionIndex()
  111. {
  112. $py = Yii::$app->request->get('py', '');
  113. //默认显示上架商品
  114. $status = Yii::$app->request->get('status', 1);
  115. //默认显示未删除商品
  116. $delStatus = Yii::$app->request->get('delStatus', 0);
  117. //获取所有当前供货商的分类 (全部、常用)
  118. //根据分类组装分类下的花材信息
  119. //中央ID
  120. $classIds = ItemClassClass::getGhsItemClassAll($this->mainId);
  121. $where['mainId'] = $this->mainId;
  122. if ($py) {
  123. $pyName = strtolower($py);
  124. $where['py'] = $pyName;
  125. }
  126. if (!empty($status)) {
  127. $where['status'] = $status;
  128. }
  129. $where['delStatus'] = $delStatus;
  130. $level = 0;
  131. $field = 'id,cover,name,cost,itemId,classId,property,skPrice,skMore,addPrice,variety,price,discountPrice,skDiscountPrice,stock,stockWarning,presell,ratioType,smallRatio,smallUnit,bigUnit,ratio,actualSold';
  132. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  133. $itemInfoData = ProductClass::groupProductInfo($itemInfoData, $level);
  134. $data = ProductService::assembleData($classIds, $itemInfoData, true);
  135. util::success($data);
  136. }
  137. //当前门店所有花材分类
  138. public function actionList()
  139. {
  140. $classId = Yii::$app->request->get('classId', 0);
  141. $pyName = Yii::$app->request->get('py', '');
  142. if ($pyName) {
  143. $pyName = strtolower($pyName);
  144. $where['py'] = $pyName;
  145. }
  146. $where['mainId'] = $this->mainId;
  147. if (!empty($classId)) {
  148. $where['classId'] = $classId;
  149. }
  150. $type = Yii::$app->request->get('type', '');
  151. $showPage = Yii::$app->request->get('showPage', 0);
  152. //需要显示分页
  153. if ($showPage) {
  154. $respond = ProductClass::getList("*", $where, "inTurn desc");
  155. $respond['list'] = ProductClass::groupClassName($this->sjId, $respond['list']);
  156. $respond['list'] = ProductClass::groupProductInfo($respond['list']);
  157. util::success($respond);
  158. }
  159. $data = ProductClass::getAllByCondition($where, "inTurn desc", "*");
  160. $data = ProductClass::groupProductInfo($data);
  161. if ($type == 'waring') {
  162. //库存预警
  163. $newData = [];
  164. foreach ($data as $v) {
  165. if ($v['stock'] <= $v['stockWarning']) {
  166. $newData[] = $v;
  167. }
  168. }
  169. util::success(['list' => $newData]);
  170. }
  171. util::success(['list' => $data]);
  172. }
  173. //批量改价
  174. public function actionBatchChangePrice()
  175. {
  176. $shop = $this->shop ?? [];
  177. $default = $shop->default ?? 0;
  178. $join = $shop->join ?? 0;
  179. $shopAdmin = $this->shopAdmin;
  180. $adminId = $this->adminId ?? 0;
  181. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  182. util::fail('超管才能修改');
  183. }
  184. if ($join == 0 && $default == 0) {
  185. util::fail('当前直营分店,只能回总店修改价格');
  186. }
  187. //normal常规改价,cg采购改价
  188. $changeType = Yii::$app->request->post('changeType', 'normal');
  189. $doType = Yii::$app->request->post('doType', 0);
  190. $targetId = Yii::$app->request->post('targetId', 0);
  191. $changeParams = ['staffId' => $shopAdmin->id, 'staffName' => $shopAdmin->name, 'changeType' => $changeType, 'targetId' => $targetId];
  192. $post = Yii::$app->request->post();
  193. $data = $post['data'] ?? '';
  194. if (empty($data)) {
  195. util::fail('没有修改');
  196. }
  197. $arr = json_decode($data, true);
  198. if (empty($arr)) {
  199. util::fail('没有修改!');
  200. }
  201. $connection = Yii::$app->db;
  202. $transaction = $connection->beginTransaction();
  203. try {
  204. if ($doType == 0 || $doType == 1) {
  205. $cg = PurchaseClass::getLockById($targetId);
  206. if (!empty($cg)) {
  207. if (isset($cg->mainId) == false || $cg->mainId != $this->mainId) {
  208. util::fail('不是您的采购单');
  209. }
  210. if ($cg->status == PurchaseClass::STATUS_UN_SEND && $cg->book == 1) {
  211. util::fail('预订单只有发货后,才能确认收货');
  212. }
  213. //没有发货则先发货
  214. if ($cg->status == PurchaseClass::STATUS_UN_SEND) {
  215. $cg = PurchaseClass::orderSend($cg, [], true);
  216. }
  217. //确认收货并入库
  218. if ($cg->status == PurchaseClass::STATUS_SENDING) {
  219. PurchaseClass::takeToPutIn($cg);
  220. if ($doType == 1) {
  221. $transaction->commit();
  222. util::complete('入库成功');
  223. }
  224. } else {
  225. util::fail('订单不是待入库状态');
  226. }
  227. } else {
  228. if ($doType == 1) {
  229. util::fail('入库失败,没有找到采购单');
  230. }
  231. }
  232. }
  233. $ids = array_column($arr, 'id');
  234. $ids = array_unique(array_filter($ids));
  235. if (empty($ids)) {
  236. util::fail('请选择花材');
  237. }
  238. $sjId = $shop->sjId ?? 0;
  239. $chainShopList = [];
  240. if ($default == 1) {
  241. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId, 'join' => 0], null, '*', null, true);
  242. }
  243. $productList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id', true);
  244. foreach ($arr as $product) {
  245. if (isset($product['price']) == false) {
  246. util::fail('没有价格');
  247. }
  248. if (isset($product['id']) == false) {
  249. util::fail('没有花材');
  250. }
  251. $price = $product['price'];
  252. $productId = $product['id'];
  253. $productInfo = $productList[$productId] ?? [];
  254. if (empty($productInfo)) {
  255. util::fail('没有找到花材');
  256. }
  257. $productName = $productInfo->name ?? '';
  258. if (isset($product['price']) == false || is_numeric($product['price']) == false || $product['price'] < 0) {
  259. util::fail('请填写' . $productName . '的批发价');
  260. }
  261. if (isset($product['skPrice']) == false || is_numeric($product['skPrice']) == false || $product['skPrice'] < 0) {
  262. util::fail('请填写' . $productName . '的零售价');
  263. }
  264. $skPrice = $product['skPrice'];
  265. if (isset($product['hjPrice']) == false || is_numeric($product['hjPrice']) == false || $product['hjPrice'] < 0) {
  266. util::fail('请填写' . $productName . '的会员价');
  267. }
  268. $hjPrice = $product['hjPrice'] ?? 0;
  269. if (isset($productInfo->mainId) == false || $productInfo->mainId != $this->mainId) {
  270. util::fail('无法访问');
  271. }
  272. if ($hjPrice > $price) {
  273. util::fail($productName . ' 批发价要大于会员价');
  274. }
  275. if ($price > $skPrice) {
  276. util::fail($productName . ' 零售价要大于批发价');
  277. }
  278. $ptItemId = $productInfo->itemId;
  279. ProductClass::changeSinglePrice($shop, $ptItemId, $price, $skPrice, $hjPrice, $changeParams);
  280. //在首店修改需要同步到所有直营店
  281. if ($default == 1 && isset($shop->dataSync) && $shop->dataSync == 1) {
  282. foreach ($chainShopList as $chain) {
  283. if ($chain->id == $shop->id) {
  284. continue;
  285. }
  286. $chainMainId = $chain->mainId ?? 0;
  287. $staff = ShopAdminClass::getByCondition(['mainId' => $chainMainId, 'adminId' => $adminId], true);
  288. $changeParams2 = ['staffId' => $staff->id, 'staffName' => $staff->name, 'changeType' => $changeType, 'targetId' => $targetId];
  289. ProductClass::changeSinglePrice($chain, $ptItemId, $price, $skPrice, $hjPrice, $changeParams2);
  290. }
  291. }
  292. }
  293. $mainId = $shop->mainId ?? 0;
  294. if (!empty($mainId)) {
  295. //提醒零售收银台界面要刷新
  296. $staffList = ShopAdminClass::getAllByCondition(['mainId' => $mainId], null, '*', null, true);
  297. if (!empty($staffList)) {
  298. foreach ($staffList as $staff) {
  299. $staffId = $staff->id ?? 0;
  300. Yii::$app->redis->executeCommand('SET', ['cashier_' . $mainId . '_' . $staffId . '_may_refresh', 'yes']);
  301. Yii::$app->redis->executeCommand('SET', ['ghs_cashier_' . $mainId . '_' . $staffId . '_may_refresh', 'yes']);
  302. }
  303. }
  304. }
  305. $transaction->commit();
  306. util::complete('操作成功');
  307. } catch (\Exception $e) {
  308. $transaction->rollBack();
  309. Yii::info("失败原因:" . $e->getMessage());
  310. util::fail('修改失败');
  311. }
  312. }
  313. //从零售端采购入库,查看供货商的某个门店的product
  314. public function actionGhsItem()
  315. {
  316. $get = Yii::$app->request->get();
  317. $id = $get['id'] ?? 0;
  318. $ghs = GhsClass::getGhsInfo($id);
  319. if (empty($ghs)) {
  320. util::fail('没有找到供货商');
  321. }
  322. $shopId = $ghs['shopId'] ?? 0;
  323. $black = $ghs['black'] ?? 2;
  324. if ($black == 2) {
  325. util::fail('暂无花材');
  326. }
  327. $connection = Yii::$app->db;//事务处理
  328. $transaction = $connection->beginTransaction();
  329. try {
  330. //获取所有当前供货商的分类 (全部、常用)
  331. //根据分类组装分类下的花材信息
  332. $shop = ShopClass::getById($shopId, true);
  333. $mainId = $shop->mainId ?? 0;
  334. $classIds = ItemClassClass::getGhsItemClassAll($mainId);
  335. $where['mainId'] = $mainId;
  336. $where['delStatus'] = 0;
  337. //是否显示全部花材,包括下架的
  338. $showAll = $get['showAll'] ?? 0;
  339. if ($showAll == 0) {
  340. $where['status'] = 1;
  341. }
  342. $where['frontHide'] = 0;
  343. $level = $ghs['giveLevel'] ?? 1;
  344. $field = 'id,py,cover,name,ratio,classId,itemId,weight,smallUnit,bigUnit,smallUnit,smallRatio,ratioType,variety,price,skPrice,skDiscountPrice,hjPrice,hjDiscountPrice,actualSold,stock,stockWarning,discountPrice,presell,presellDate,reachNum,reachNumDiscount';
  345. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  346. $itemInfoData = ProductClass::hdCgGroup($itemInfoData, $level);
  347. $myProduct = ProductClass::getAllByCondition(['mainId' => $this->mainId, 'delStatus' => 0], null, 'id,stock,onStock,stockWarning,itemId', 'itemId');
  348. if (!empty($itemInfoData)) {
  349. $myShop = $this->shop;
  350. $myDisplay = isset($myShop->pfShopId) && $myShop->pfShopId > 0 ? 1 : 0;
  351. foreach ($itemInfoData as $key => $val) {
  352. $ptItemId = $val['itemId'] ?? 0;
  353. $myStock = $myProduct[$ptItemId]['stock'] ?? 0;
  354. $myStock = floatval($myStock);
  355. $myOnStock = $myProduct[$ptItemId]['onStock'] ?? 0;
  356. $myStockWarning = $myProduct[$ptItemId]['stockWarning'] ?? 5;
  357. $myLack = bcadd($myStock, $myOnStock, 2) > $myStockWarning ? 0 : 1;
  358. $itemInfoData[$key]['myStock'] = $myStock;
  359. $itemInfoData[$key]['myOnStock'] = $myOnStock;
  360. $itemInfoData[$key]['myStockWarning'] = $myStockWarning;
  361. $itemInfoData[$key]['myLack'] = $myLack;
  362. $itemInfoData[$key]['myDisplay'] = $myDisplay;
  363. }
  364. }
  365. $data = ProductService::assembleData($classIds, $itemInfoData, true);
  366. util::success($data);
  367. } catch (\Exception $e) {
  368. $transaction->rollBack();
  369. Yii::info("报错内容:" . $e->getMessage());
  370. util::fail();
  371. }
  372. }
  373. public function actionDetail()
  374. {
  375. $id = Yii::$app->request->get('id', 0);
  376. $respond = ProductClass::getItemInfo($id);
  377. $ptItemId = $respond['itemId'] ?? 0;
  378. $ptItemInfo = PtItemClass::getById($ptItemId);
  379. $respond['ptItemInfo'] = $ptItemInfo;
  380. util::success($respond);
  381. }
  382. public function actionUpdate()
  383. {
  384. $shop = $this->shop ?? [];
  385. $pfShopId = $shop->pfShopId ?? 0;
  386. if (!empty($pfShopId)) {
  387. util::fail('请在批发端操作');
  388. }
  389. $post = Yii::$app->request->post();
  390. $shopAdmin = $this->shopAdmin;
  391. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  392. util::fail('超管才能修改');
  393. }
  394. $post['adminId'] = $this->adminId;
  395. $post['staffName'] = $this->shopAdmin->name;
  396. ItemClass::updateProduct($post, $this->shop);
  397. util::complete();
  398. }
  399. //批量修改product 排序,成本价,加价
  400. public function actionBatchUpdate()
  401. {
  402. //data = [{id:1,inTurn:10,addPrice:2,cost:10}]
  403. $data = Yii::$app->request->post('data', '');
  404. $type = Yii::$app->request->post('type', '');
  405. if (empty($data) || empty($type)) {
  406. util::fail("参数错误");
  407. }
  408. if (in_array($type, ['inTurn', 'addPrice', 'cost', 'weight', 'ratio']))
  409. $data = json_decode($data, true);
  410. foreach ($data as $v) {
  411. $id = $v['id'];
  412. $val = $v[$type] ?? '';
  413. $where = [];
  414. $where['id'] = $id;
  415. $where['shopId'] = $this->shopId;
  416. $where['sjId'] = $this->sjId;
  417. if (empty($val)) {
  418. util::fail("参数错误1");
  419. }
  420. $data = [
  421. $type => $val
  422. ];
  423. ProductClass::updateByCondition($where, $data);
  424. ProductClass::autoPriceById($id);
  425. }
  426. util::complete();
  427. }
  428. //供货商产品的详情 ssh 20230111
  429. public function actionGhsProductDetail()
  430. {
  431. $get = Yii::$app->request->get();
  432. $id = $get['id'] ?? 0;
  433. $ghsId = $get['ghsId'] ?? 0;
  434. $product = ProductClass::getById($id);
  435. if (empty($product)) {
  436. util::fail('没有花材信息');
  437. }
  438. $ghsInfo = GhsClass::getById($ghsId, true);
  439. $level = $ghsInfo->giveLevel ? $ghsInfo->giveLevel : 1;
  440. $list = ProductClass::groupProductInfo([$product], $level);
  441. $product = current($list);
  442. util::success($product);
  443. }
  444. //批量修改加价 ssh 20211211
  445. public function actionBatchChangeAddPrice()
  446. {
  447. $shopAdmin = $this->shopAdmin;
  448. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  449. util::fail('超管才能操作');
  450. }
  451. $shop = $this->shop;
  452. $pfShopId = $shop->pfShopId ?? 0;
  453. if (!empty($pfShopId)) {
  454. util::fail('请在批发端操作');
  455. }
  456. $addData = Yii::$app->request->post('addData', '');
  457. if (empty($addData)) {
  458. util::fail('没有花材');
  459. }
  460. $itemData = json_decode($addData, true);
  461. if (is_array($itemData) == false) {
  462. util::fail('没有花材...');
  463. }
  464. $ids = array_column($itemData, 'id');
  465. $infoList = ProductClass::getByIds($ids);
  466. if (empty($infoList)) {
  467. util::fail('没有花材');
  468. }
  469. foreach ($infoList as $info) {
  470. if (isset($info['mainId']) == false || $info['mainId'] != $this->mainId) {
  471. util::fail('非常访问,不是你的花材不能修改');
  472. }
  473. }
  474. foreach ($itemData as $key => $data) {
  475. $id = $data['id'] ?? 0;
  476. ProductClass::updateById($id, $data);
  477. }
  478. util::complete('修改成功');
  479. }
  480. }