ProductController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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. //没有发货则先发货
  211. if ($cg->status == PurchaseClass::STATUS_UN_SEND) {
  212. $cg = PurchaseClass::orderSend($cg, [], true);
  213. }
  214. //确认收货并入库
  215. if ($cg->status == PurchaseClass::STATUS_SENDING) {
  216. PurchaseClass::takeToPutIn($cg);
  217. if ($doType == 1) {
  218. $transaction->commit();
  219. util::complete('入库成功');
  220. }
  221. } else {
  222. util::fail('订单不是待入库状态');
  223. }
  224. } else {
  225. if ($doType == 1) {
  226. util::fail('入库失败,没有找到采购单');
  227. }
  228. }
  229. }
  230. $ids = array_column($arr, 'id');
  231. $ids = array_unique(array_filter($ids));
  232. if (empty($ids)) {
  233. util::fail('请选择花材');
  234. }
  235. $sjId = $shop->sjId ?? 0;
  236. $chainShopList = [];
  237. if ($default == 1) {
  238. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId, 'join' => 0], null, '*', null, true);
  239. }
  240. $productList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id', true);
  241. foreach ($arr as $product) {
  242. if (isset($product['price']) == false) {
  243. util::fail('没有价格');
  244. }
  245. if (isset($product['id']) == false) {
  246. util::fail('没有花材');
  247. }
  248. $price = $product['price'];
  249. $productId = $product['id'];
  250. $productInfo = $productList[$productId] ?? [];
  251. if (empty($productInfo)) {
  252. util::fail('没有找到花材');
  253. }
  254. $productName = $productInfo->name ?? '';
  255. if (isset($product['price']) == false || is_numeric($product['price']) == false || $product['price'] < 0) {
  256. util::fail('请填写' . $productName . '的批发价');
  257. }
  258. if (isset($product['skPrice']) == false || is_numeric($product['skPrice']) == false || $product['skPrice'] < 0) {
  259. util::fail('请填写' . $productName . '的零售价');
  260. }
  261. $skPrice = $product['skPrice'];
  262. if (isset($product['hjPrice']) == false || is_numeric($product['hjPrice']) == false || $product['hjPrice'] < 0) {
  263. util::fail('请填写' . $productName . '的会员价');
  264. }
  265. $hjPrice = $product['hjPrice'] ?? 0;
  266. if (isset($productInfo->mainId) == false || $productInfo->mainId != $this->mainId) {
  267. util::fail('无法访问');
  268. }
  269. if ($hjPrice > $price) {
  270. util::fail($productName . ' 批发价要大于会员价');
  271. }
  272. if ($price > $skPrice) {
  273. util::fail($productName . ' 零售价要大于批发价');
  274. }
  275. $ptItemId = $productInfo->itemId;
  276. ProductClass::changeSinglePrice($shop, $ptItemId, $price, $skPrice, $hjPrice, $changeParams);
  277. //在首店修改需要同步到所有直营店
  278. if ($default == 1 && isset($shop->dataSync) && $shop->dataSync == 1) {
  279. foreach ($chainShopList as $chain) {
  280. if ($chain->id == $shop->id) {
  281. continue;
  282. }
  283. $chainMainId = $chain->mainId ?? 0;
  284. $staff = ShopAdminClass::getByCondition(['mainId' => $chainMainId, 'adminId' => $adminId], true);
  285. $changeParams2 = ['staffId' => $staff->id, 'staffName' => $staff->name, 'changeType' => $changeType, 'targetId' => $targetId];
  286. ProductClass::changeSinglePrice($chain, $ptItemId, $price, $skPrice, $hjPrice, $changeParams2);
  287. }
  288. }
  289. }
  290. $mainId = $shop->mainId ?? 0;
  291. if (!empty($mainId)) {
  292. //提醒零售收银台界面要刷新
  293. $staffList = ShopAdminClass::getAllByCondition(['mainId' => $mainId], null, '*', null, true);
  294. if (!empty($staffList)) {
  295. foreach ($staffList as $staff) {
  296. $staffId = $staff->id ?? 0;
  297. Yii::$app->redis->executeCommand('SET', ['cashier_' . $mainId . '_' . $staffId . '_may_refresh', 'yes']);
  298. Yii::$app->redis->executeCommand('SET', ['ghs_cashier_' . $mainId . '_' . $staffId . '_may_refresh', 'yes']);
  299. }
  300. }
  301. }
  302. $transaction->commit();
  303. util::complete('操作成功');
  304. } catch (\Exception $e) {
  305. $transaction->rollBack();
  306. Yii::info("失败原因:" . $e->getMessage());
  307. util::fail('修改失败');
  308. }
  309. }
  310. //从零售端采购入库,查看供货商的某个门店的product
  311. public function actionGhsItem()
  312. {
  313. $get = Yii::$app->request->get();
  314. $id = $get['id'] ?? 0;
  315. $ghs = GhsClass::getGhsInfo($id);
  316. if (empty($ghs)) {
  317. util::fail('没有找到供货商');
  318. }
  319. $shopId = $ghs['shopId'] ?? 0;
  320. $black = $ghs['black'] ?? 2;
  321. if ($black == 2) {
  322. util::fail('暂无花材');
  323. }
  324. $connection = Yii::$app->db;//事务处理
  325. $transaction = $connection->beginTransaction();
  326. try {
  327. //获取所有当前供货商的分类 (全部、常用)
  328. //根据分类组装分类下的花材信息
  329. $shop = ShopClass::getById($shopId, true);
  330. $mainId = $shop->mainId ?? 0;
  331. $classIds = ItemClassClass::getGhsItemClassAll($mainId);
  332. $where['mainId'] = $mainId;
  333. $where['delStatus'] = 0;
  334. //是否显示全部花材,包括下架的
  335. $showAll = $get['showAll'] ?? 0;
  336. if ($showAll == 0) {
  337. $where['status'] = 1;
  338. }
  339. $where['frontHide'] = 0;
  340. $level = $ghs['giveLevel'] ?? 1;
  341. $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';
  342. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  343. $itemInfoData = ProductClass::hdCgGroup($itemInfoData, $level);
  344. $data = ProductService::assembleData($classIds, $itemInfoData, true);
  345. util::success($data);
  346. } catch (\Exception $e) {
  347. $transaction->rollBack();
  348. Yii::info("报错内容:" . $e->getMessage());
  349. util::fail();
  350. }
  351. }
  352. public function actionDetail()
  353. {
  354. $id = Yii::$app->request->get('id', 0);
  355. $respond = ProductClass::getItemInfo($id);
  356. $ptItemId = $respond['itemId'] ?? 0;
  357. $ptItemInfo = PtItemClass::getById($ptItemId);
  358. $respond['ptItemInfo'] = $ptItemInfo;
  359. util::success($respond);
  360. }
  361. public function actionUpdate()
  362. {
  363. $shop = $this->shop ?? [];
  364. $pfShopId = $shop->pfShopId ?? 0;
  365. if (!empty($pfShopId)) {
  366. util::fail('请在批发端操作');
  367. }
  368. $post = Yii::$app->request->post();
  369. $shopAdmin = $this->shopAdmin;
  370. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  371. util::fail('超管才能修改');
  372. }
  373. $post['adminId'] = $this->adminId;
  374. $post['staffName'] = $this->shopAdmin->name;
  375. ItemClass::updateProduct($post, $this->shop);
  376. util::complete();
  377. }
  378. //批量修改product 排序,成本价,加价
  379. public function actionBatchUpdate()
  380. {
  381. //data = [{id:1,inTurn:10,addPrice:2,cost:10}]
  382. $data = Yii::$app->request->post('data', '');
  383. $type = Yii::$app->request->post('type', '');
  384. if (empty($data) || empty($type)) {
  385. util::fail("参数错误");
  386. }
  387. if (in_array($type, ['inTurn', 'addPrice', 'cost', 'weight', 'ratio']))
  388. $data = json_decode($data, true);
  389. foreach ($data as $v) {
  390. $id = $v['id'];
  391. $val = $v[$type] ?? '';
  392. $where = [];
  393. $where['id'] = $id;
  394. $where['shopId'] = $this->shopId;
  395. $where['sjId'] = $this->sjId;
  396. if (empty($val)) {
  397. util::fail("参数错误1");
  398. }
  399. $data = [
  400. $type => $val
  401. ];
  402. ProductClass::updateByCondition($where, $data);
  403. ProductClass::autoPriceById($id);
  404. }
  405. util::complete();
  406. }
  407. //供货商产品的详情 ssh 20230111
  408. public function actionGhsProductDetail()
  409. {
  410. $get = Yii::$app->request->get();
  411. $id = $get['id'] ?? 0;
  412. $ghsId = $get['ghsId'] ?? 0;
  413. $product = ProductClass::getById($id);
  414. if (empty($product)) {
  415. util::fail('没有花材信息');
  416. }
  417. $ghsInfo = GhsClass::getById($ghsId, true);
  418. $level = $ghsInfo->giveLevel ? $ghsInfo->giveLevel : 1;
  419. $list = ProductClass::groupProductInfo([$product], $level);
  420. $product = current($list);
  421. util::success($product);
  422. }
  423. //批量修改加价 ssh 20211211
  424. public function actionBatchChangeAddPrice()
  425. {
  426. $shopAdmin = $this->shopAdmin;
  427. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  428. util::fail('超管才能操作');
  429. }
  430. $shop = $this->shop;
  431. $pfShopId = $shop->pfShopId ?? 0;
  432. if (!empty($pfShopId)) {
  433. util::fail('请在批发端操作');
  434. }
  435. $addData = Yii::$app->request->post('addData', '');
  436. if (empty($addData)) {
  437. util::fail('没有花材');
  438. }
  439. $itemData = json_decode($addData, true);
  440. if (is_array($itemData) == false) {
  441. util::fail('没有花材...');
  442. }
  443. $ids = array_column($itemData, 'id');
  444. $infoList = ProductClass::getByIds($ids);
  445. if (empty($infoList)) {
  446. util::fail('没有花材');
  447. }
  448. foreach ($infoList as $info) {
  449. if (isset($info['mainId']) == false || $info['mainId'] != $this->mainId) {
  450. util::fail('非常访问,不是你的花材不能修改');
  451. }
  452. }
  453. foreach ($itemData as $key => $data) {
  454. $id = $data['id'] ?? 0;
  455. ProductClass::updateById($id, $data);
  456. }
  457. util::complete('修改成功');
  458. }
  459. }