ProductController.php 19 KB

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