ProductController.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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\purchase\classes\PurchaseClass;
  12. use bizHd\purchase\classes\PurchaseItemClass;
  13. use common\components\printUtil;
  14. use common\components\stringUtil;
  15. use common\components\util;
  16. use Yii;
  17. use yii\helpers\ArrayHelper;
  18. class ProductController extends BaseController
  19. {
  20. public $guestAccess = ['ghs-item', 'ghs-product-detail'];
  21. //复制新建花材 ssh 20250523
  22. public function actionClone()
  23. {
  24. $post = Yii::$app->request->post();
  25. $appVersion = $post['appVersion'] ?? 0;
  26. if ($appVersion != 1) {
  27. util::fail('请先升级应用');
  28. }
  29. $discountPrice = $post['discountPrice'] ?? 0;
  30. if ($discountPrice > 0) {
  31. if (isset($post['hjDiscountPrice']) == false || empty($post['hjDiscountPrice'])) {
  32. $addPrice = $post['addPrice'] ?? 0;
  33. $hjAddPrice = $post['hjAddPrice'] ?? 0;
  34. $skMore = $post['skMore'] ?? 0;
  35. $diff = bcsub($addPrice, $hjAddPrice, 2);
  36. if ($diff > 0 && $discountPrice > $diff) {
  37. $hjDiscountPrice = bcsub($discountPrice, $diff, 2);
  38. } else {
  39. $hjDiscountPrice = $discountPrice;
  40. }
  41. $diff = bcsub($skMore, $addPrice, 2);
  42. if ($diff > 0) {
  43. $skDiscountPrice = bcadd($diff, $discountPrice, 2);
  44. } else {
  45. $skDiscountPrice = $discountPrice;
  46. }
  47. $post['skDiscountPrice'] = $skDiscountPrice;
  48. $post['hjDiscountPrice'] = $hjDiscountPrice;
  49. }
  50. }
  51. $shop = $this->shop;
  52. $join = $shop->join ?? 0;
  53. $default = $shop->default ?? 0;
  54. if ($join == 0 && $default == 0) {
  55. util::fail('请在首店操作');
  56. }
  57. $shopAdmin = $this->shopAdmin;
  58. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  59. util::fail('管理员才能添加花材');
  60. }
  61. $post['sjId'] = $this->sjId;
  62. $post['adminId'] = $this->adminId;
  63. $post['mainId'] = $this->mainId;
  64. $post['staffName'] = $shopAdmin->name ?? '';
  65. $price = $post['price'] ?? 0;
  66. if (empty($price) || $price <= 0) {
  67. util::fail('请填写批发价');
  68. }
  69. $skPrice = $post['skPrice'] ?? 0;
  70. if (empty($skPrice) || $skPrice <= 0) {
  71. util::fail('请填写零售价');
  72. }
  73. $hjPrice = $post['hjPrice'] ?? 0;
  74. if (empty($hjPrice) || $hjPrice <= 0) {
  75. util::fail('请填写会员价');
  76. }
  77. $addPrice = $post['addPrice'] ?? 0;
  78. if ($addPrice > $price) {
  79. util::fail('批发价的加价不能大于售价');
  80. }
  81. //复制标识
  82. $post['copy'] = 1;
  83. //去除无用的字段
  84. unset($post['viewNum']);
  85. unset($post['actualSold']);
  86. unset($post['itemId']);
  87. unset($post['id']);
  88. unset($post['delStatus']);
  89. ProductClass::addProduct($post, $this->shop);
  90. util::complete('复制成功');
  91. }
  92. //花材展示状态控制 ssh 20250519
  93. public function actionDelStatusUpdate()
  94. {
  95. $post = Yii::$app->request->post();
  96. $shop = $this->shop;
  97. $join = $shop->join ?? 0;
  98. $default = $shop->default ?? 0;
  99. if ($join == 0 && $default == 0) {
  100. util::fail('请在首店操作');
  101. }
  102. $id = $post['id'] ?? 0;
  103. $delStatus = $post['delStatus'] ?? 0;
  104. $product = ProductClass::getById($id, true);
  105. if ($product->mainId != $this->mainId) {
  106. util::fail('不是你的花材哦!!');
  107. }
  108. $product->delStatus = $delStatus;
  109. if ($delStatus == 1) {
  110. $product->stock = 0;
  111. $product->status = 2;
  112. }
  113. $product->save();
  114. $ptItemId = $product->itemId ?? 0;
  115. if (isset($shop->default) && $shop->default == 1 && isset($shop->dataSync) && $shop->dataSync == 1) {
  116. //总店修改同步到所有门店
  117. $sjId = $shop->sjId ?? 0;
  118. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  119. foreach ($chainShopList as $chainShop) {
  120. $chainShopId = $chainShop->id ?? 0;
  121. if (isset($chainShop->join) && $chainShop->join == 1) {
  122. continue;
  123. }
  124. if ($chainShopId == $shop->id) {
  125. continue;
  126. }
  127. $chainMainId = $chainShop->mainId ?? 0;
  128. $chainProduct = ProductClass::getByCondition(['mainId' => $chainMainId, 'itemId' => $ptItemId], true);
  129. if (!empty($chainProduct)) {
  130. $chainProduct->delStatus = $delStatus;
  131. if ($delStatus == 1) {
  132. $chainProduct->stock = 0;
  133. $chainProduct->status = 2;
  134. }
  135. $chainProduct->save();
  136. }
  137. }
  138. }
  139. util::complete();
  140. }
  141. //打印花材的标签 ssh 20220602
  142. public function actionPrint()
  143. {
  144. $get = Yii::$app->request->get();
  145. $id = $get['id'] ?? 0;
  146. $num = $get['num'] ?? 1;
  147. $info = ProductClass::getById($id, true);
  148. if (empty($info)) {
  149. util::fail('没有找到花材');
  150. }
  151. $name = $info->name ?? '';
  152. $ptItemId = $info->itemId ?? 0;
  153. $shopId = $this->shopId;
  154. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  155. $printLabelSn = $ext->printLabelSn ?? '';
  156. if (empty($printLabelSn)) {
  157. util::fail('请设置标签打印机');
  158. }
  159. $p = new printUtil($printLabelSn);
  160. $unit = $info->ratio . $info->smallUnit . '/' . $info->bigUnit;
  161. if (isset($info->ratioType) && $info->ratioType == 1) {
  162. $unit = '若干' . $info->smallUnit . '/' . $info->bigUnit;
  163. }
  164. $p->times = $num;
  165. $content = '<TEXT x="360" y="30" font="12" w="2" h="2" r="90">' . $name . '</TEXT>';
  166. $content .= '<BC128 x="260" y="30" h="70" s="1" r="90" n="4" w="11">' . $ptItemId . '</BC128>';
  167. $content .= '<TEXT x="60" y="30" font="12" w="2" h="2" r="90">' . $unit . '</TEXT>';
  168. $p->printLabelMsg($content);
  169. util::complete();
  170. }
  171. //新增花材 20250523 ssh
  172. public function actionAdd()
  173. {
  174. $post = Yii::$app->request->post();
  175. //临时兼容,2023.7.1后可以删除
  176. $appVersion = $post['appVersion'] ?? 0;
  177. if ($appVersion != 1) {
  178. util::fail('请先升级应用');
  179. }
  180. $discountPrice = $post['discountPrice'] ?? 0;
  181. if ($discountPrice > 0) {
  182. if (isset($post['hjDiscountPrice']) == false || empty($post['hjDiscountPrice'])) {
  183. $addPrice = $post['addPrice'] ?? 0;
  184. $hjAddPrice = $post['hjAddPrice'] ?? 0;
  185. $skMore = $post['skMore'] ?? 0;
  186. $diff = bcsub($addPrice, $hjAddPrice, 2);
  187. if ($diff > 0 && $discountPrice > $diff) {
  188. $hjDiscountPrice = bcsub($discountPrice, $diff, 2);
  189. } else {
  190. $hjDiscountPrice = $discountPrice;
  191. }
  192. $diff = bcsub($skMore, $addPrice, 2);
  193. if ($diff > 0) {
  194. $skDiscountPrice = bcadd($diff, $discountPrice, 2);
  195. } else {
  196. $skDiscountPrice = $discountPrice;
  197. }
  198. $post['skDiscountPrice'] = $skDiscountPrice;
  199. $post['hjDiscountPrice'] = $hjDiscountPrice;
  200. }
  201. }
  202. $shop = $this->shop;
  203. $join = $shop->join ?? 0;
  204. $default = $shop->default ?? 0;
  205. if ($join == 0 && $default == 0) {
  206. util::fail('当前直营分店,请切回总店添加花材');
  207. }
  208. $shopAdmin = $this->shopAdmin;
  209. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  210. util::fail('管理员才能添加花材');
  211. }
  212. $post['sjId'] = $this->sjId;
  213. $post['adminId'] = $this->adminId;
  214. $post['mainId'] = $this->mainId;
  215. $price = $post['price'] ?? 0;
  216. $addPrice = $post['addPrice'] ?? 0;
  217. if ($addPrice > $price) {
  218. util::fail('加价不能大于售价');
  219. }
  220. $weight = isset($post['weight']) && is_numeric($post['weight']) ? $post['weight'] : 0;
  221. if ($weight <= 0) {
  222. util::fail('请填写重量,最小0.1');
  223. }
  224. $post['staffName'] = $shopAdmin->name ?? '';
  225. ProductClass::addProduct($post, $this->shop);
  226. util::complete('添加成功');
  227. }
  228. //获取拼音缩写 ssh 20210707
  229. public function actionPy()
  230. {
  231. $name = Yii::$app->request->get('name');
  232. $py = stringUtil::py($name);
  233. util::success(['py' => $py]);
  234. }
  235. //所有花材
  236. public function actionAll()
  237. {
  238. $where = [];
  239. $where['mainId'] = $this->mainId;
  240. $classId = Yii::$app->request->get('classId', 0);
  241. if (!empty($classId)) {
  242. $where['classId'] = $classId;
  243. }
  244. $status = Yii::$app->request->get('status', 0);
  245. if (!empty($status)) {
  246. $where['status'] = $status;
  247. }
  248. $delStatus = Yii::$app->request->get('delStatus', 0);
  249. if ($delStatus != 2) {
  250. $where['delStatus'] = $delStatus;
  251. }
  252. //零售价
  253. $level = 0;
  254. $data = ProductClass::getAllByCondition($where, ['actualSold' => SORT_DESC, 'inTurn' => SORT_DESC], "*");
  255. $data = ProductClass::groupProductInfo($data, $level);
  256. util::success(['list' => $data]);
  257. }
  258. public function actionIndex()
  259. {
  260. $py = Yii::$app->request->get('py', '');
  261. //默认显示上架商品
  262. $status = Yii::$app->request->get('status', 1);
  263. //默认显示未删除商品
  264. $delStatus = Yii::$app->request->get('delStatus', 0);
  265. //获取所有当前供货商的分类 (全部、常用)
  266. //根据分类组装分类下的花材信息
  267. //中央ID
  268. $classIds = ItemClassClass::getGhsItemClassAll($this->mainId);
  269. $where['mainId'] = $this->mainId;
  270. if ($py) {
  271. $pyName = strtolower($py);
  272. $where['py'] = $pyName;
  273. }
  274. if (!empty($status)) {
  275. $where['status'] = $status;
  276. }
  277. $where['delStatus'] = $delStatus;
  278. $level = 0;
  279. $field = 'id,cover,name,cost,itemId,classId,property,skPrice,skMore,addPrice,variety,price,discountPrice,skDiscountPrice,stock,stockWarning,presell,ratioType,smallRatio,smallUnit,bigUnit,ratio,actualSold';
  280. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  281. $itemInfoData = ProductClass::groupProductInfo($itemInfoData, $level);
  282. $respond = ProductService::assembleData($classIds, $itemInfoData, true);
  283. $classItem = $respond['classItem'] ?? [];
  284. util::success($classItem);
  285. }
  286. //当前门店所有花材分类
  287. public function actionList()
  288. {
  289. $classId = Yii::$app->request->get('classId', 0);
  290. $pyName = Yii::$app->request->get('py', '');
  291. if ($pyName) {
  292. $pyName = strtolower($pyName);
  293. $where['py'] = $pyName;
  294. }
  295. $where['mainId'] = $this->mainId;
  296. if (!empty($classId)) {
  297. $where['classId'] = $classId;
  298. }
  299. $type = Yii::$app->request->get('type', '');
  300. $showPage = Yii::$app->request->get('showPage', 0);
  301. //需要显示分页
  302. if ($showPage) {
  303. $respond = ProductClass::getList("*", $where, "inTurn desc");
  304. $respond['list'] = ProductClass::groupClassName($this->mainId, $respond['list']);
  305. $respond['list'] = ProductClass::groupProductInfo($respond['list']);
  306. util::success($respond);
  307. }
  308. $data = ProductClass::getAllByCondition($where, "inTurn desc", "*");
  309. $data = ProductClass::groupProductInfo($data);
  310. if ($type == 'waring') {
  311. //库存预警
  312. $newData = [];
  313. foreach ($data as $v) {
  314. if ($v['stock'] <= $v['stockWarning']) {
  315. $newData[] = $v;
  316. }
  317. }
  318. util::success(['list' => $newData]);
  319. }
  320. util::success(['list' => $data]);
  321. }
  322. //批量改价
  323. public function actionBatchChangePrice()
  324. {
  325. $shop = $this->shop ?? [];
  326. $shopId = $shop->id ?? 0;
  327. $default = $shop->default ?? 0;
  328. $join = $shop->join ?? 0;
  329. $shopAdmin = $this->shopAdmin;
  330. $adminId = $this->adminId ?? 0;
  331. //normal常规改价,cg采购改价
  332. $changeType = Yii::$app->request->post('changeType', 'normal');
  333. $doType = Yii::$app->request->post('doType', 0);
  334. if ($changeType == 'cg' && $doType == 0) {
  335. if (getenv('YII_ENV') == 'production') {
  336. //小蔡鲜花的几个分店的零售店不能修改价格
  337. if (in_array($shopId, [29153, 29155, 29157, 29162, 29164])) {
  338. util::fail('本店不能改价哦');
  339. }
  340. } else {
  341. if (in_array($shopId, [36545])) {
  342. util::fail('本店无法改价哈');
  343. }
  344. }
  345. }
  346. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  347. util::fail('超管才能修改');
  348. }
  349. if ($join == 0 && $default == 0) {
  350. //util::fail('当前直营分店,只能回总店修改价格');
  351. }
  352. $targetId = Yii::$app->request->post('targetId', 0);
  353. $changeParams = ['staffId' => $shopAdmin->id, 'staffName' => $shopAdmin->name, 'changeType' => $changeType, 'targetId' => $targetId];
  354. $post = Yii::$app->request->post();
  355. $data = $post['data'] ?? '';
  356. if (empty($data)) {
  357. util::fail('没有修改');
  358. }
  359. $arr = json_decode($data, true);
  360. if (empty($arr)) {
  361. util::fail('没有修改!');
  362. }
  363. $connection = Yii::$app->db;
  364. $transaction = $connection->beginTransaction();
  365. try {
  366. if ($doType == 0 || $doType == 1) {
  367. $cg = PurchaseClass::getLockById($targetId);
  368. if (!empty($cg)) {
  369. if (isset($cg->mainId) == false || $cg->mainId != $this->mainId) {
  370. util::fail('不是您的采购单');
  371. }
  372. if ($cg->status == PurchaseClass::STATUS_UN_SEND && $cg->book == 1) {
  373. util::fail('预订单只有发货后,才能确认收货');
  374. }
  375. //没有发货则先发货
  376. if ($cg->status == PurchaseClass::STATUS_UN_SEND) {
  377. $cg = PurchaseClass::orderSend($cg, [], true);
  378. }
  379. //确认收货并入库
  380. if ($cg->status == PurchaseClass::STATUS_SENDING) {
  381. // 构建“冲销”函数的参数 -- 变动前的花材存库数据
  382. $purchaseItems = PurchaseItemClass::getAllByCondition(['orderSn' => $cg->orderSn], null, "productId");
  383. $productIds = ArrayHelper::getColumn($purchaseItems, 'productId'); //$productIds 是本次入库涉及的花材ID数组
  384. $oldStocks = [];
  385. foreach ($productIds as $productId) {
  386. $product = ProductClass::getById($productId, true);
  387. $oldStocks[$productId] = $product;
  388. }
  389. PurchaseClass::takeToPutIn($cg);
  390. // “冲销”本次入库的数量
  391. PurchaseClass::rollbackProductStock($oldStocks, $shop, $adminId, $this->mainId, $this->shopAdmin->name);
  392. if ($doType == 1) {
  393. $transaction->commit();
  394. util::complete('入库成功');
  395. }
  396. } else {
  397. util::fail('订单不是待入库状态');
  398. }
  399. } else {
  400. if ($doType == 1) {
  401. util::fail('入库失败,没有找到采购单');
  402. }
  403. }
  404. }
  405. $ids = array_column($arr, 'id');
  406. $ids = array_unique(array_filter($ids));
  407. if (empty($ids)) {
  408. util::fail('请选择花材');
  409. }
  410. $sjId = $shop->sjId ?? 0;
  411. $chainShopList = [];
  412. if ($default == 1) {
  413. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId, 'join' => 0], null, '*', null, true);
  414. }
  415. $productList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id', true);
  416. foreach ($arr as $product) {
  417. if (isset($product['price']) == false) {
  418. util::fail('没有价格');
  419. }
  420. if (isset($product['id']) == false) {
  421. util::fail('没有花材');
  422. }
  423. $price = $product['price'];
  424. $productId = $product['id'];
  425. $productInfo = $productList[$productId] ?? [];
  426. if (empty($productInfo)) {
  427. util::fail('没有找到花材');
  428. }
  429. $productName = $productInfo->name ?? '';
  430. if (isset($product['price']) == false || is_numeric($product['price']) == false || $product['price'] < 0) {
  431. util::fail('请填写' . $productName . '的批发价');
  432. }
  433. if (isset($product['skPrice']) == false || is_numeric($product['skPrice']) == false || $product['skPrice'] < 0) {
  434. util::fail('请填写' . $productName . '的零售价');
  435. }
  436. $skPrice = $product['skPrice'];
  437. if (isset($product['hjPrice']) == false || is_numeric($product['hjPrice']) == false || $product['hjPrice'] < 0) {
  438. util::fail('请填写' . $productName . '的会员价');
  439. }
  440. $hjPrice = $product['hjPrice'] ?? 0;
  441. if (isset($productInfo->mainId) == false || $productInfo->mainId != $this->mainId) {
  442. util::fail('无法访问');
  443. }
  444. if ($hjPrice > $price) {
  445. util::fail($productName . ' 批发价要大于会员价');
  446. }
  447. if ($price > $skPrice) {
  448. util::fail($productName . ' 零售价要大于批发价');
  449. }
  450. $ptItemId = $productInfo->itemId;
  451. ProductClass::changeSinglePrice($shop, $ptItemId, $price, $skPrice, $hjPrice, $changeParams);
  452. //在首店修改需要同步到所有直营店
  453. if ($default == 1 && isset($shop->dataSync) && $shop->dataSync == 1) {
  454. foreach ($chainShopList as $chain) {
  455. if ($chain->id == $shop->id) {
  456. continue;
  457. }
  458. $chainMainId = $chain->mainId ?? 0;
  459. $staff = ShopAdminClass::getByCondition(['mainId' => $chainMainId, 'adminId' => $adminId], true);
  460. $changeParams2 = ['staffId' => $staff->id, 'staffName' => $staff->name, 'changeType' => $changeType, 'targetId' => $targetId];
  461. ProductClass::changeSinglePrice($chain, $ptItemId, $price, $skPrice, $hjPrice, $changeParams2);
  462. }
  463. }
  464. }
  465. $mainId = $shop->mainId ?? 0;
  466. if (!empty($mainId)) {
  467. //提醒零售收银台界面要刷新,多处要修改,关键词 modify_price_remind_cashier
  468. $staffList = ShopAdminClass::getAllByCondition(['mainId' => $mainId], null, '*', null, true);
  469. if (!empty($staffList)) {
  470. foreach ($staffList as $staff) {
  471. $staffId = $staff->id ?? 0;
  472. Yii::$app->redis->executeCommand('SET', ['cashier_' . $mainId . '_' . $staffId . '_may_refresh', 'yes']);
  473. Yii::$app->redis->executeCommand('SET', ['ghs_cashier_' . $mainId . '_' . $staffId . '_may_refresh', 'yes']);
  474. }
  475. }
  476. }
  477. $transaction->commit();
  478. util::complete('操作成功');
  479. } catch (\Exception $e) {
  480. $transaction->rollBack();
  481. Yii::info("失败原因:" . $e->getMessage());
  482. util::fail('修改失败');
  483. }
  484. }
  485. //从零售端采购入库,查看供货商的某个门店的product
  486. public function actionGhsItem()
  487. {
  488. $get = Yii::$app->request->get();
  489. $id = $get['id'] ?? 0;
  490. $ghs = GhsClass::getGhsInfo($id);
  491. if (empty($ghs)) {
  492. util::fail('没有找到供货商');
  493. }
  494. $shopId = $ghs['shopId'] ?? 0;
  495. $black = $ghs['black'] ?? 2;
  496. if ($black == 2) {
  497. util::fail('暂无花材');
  498. }
  499. $connection = Yii::$app->db;
  500. $transaction = $connection->beginTransaction();
  501. try {
  502. //获取所有当前供货商的分类 (全部、常用)
  503. //根据分类组装分类下的花材信息
  504. $shop = ShopClass::getById($shopId, true);
  505. $mainId = $shop->mainId ?? 0;
  506. $classIds = ItemClassClass::getGhsItemClassAll($mainId);
  507. $where['mainId'] = $mainId;
  508. $where['delStatus'] = 0;
  509. //是否显示全部花材,包括下架的
  510. $showAll = $get['showAll'] ?? 0;
  511. if ($showAll == 0) {
  512. $where['status'] = 1;
  513. }
  514. $where['frontHide'] = 0;
  515. $level = $ghs['giveLevel'] ?? 1;
  516. $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,limitBuy';
  517. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  518. $itemInfoData = ProductClass::hdCgGroup($itemInfoData, $level);
  519. $myProduct = ProductClass::getAllByCondition(['mainId' => $this->mainId, 'delStatus' => 0], null, 'id,stock,onStock,stockWarning,itemId', 'itemId');
  520. if (!empty($itemInfoData)) {
  521. $myShop = $this->shop;
  522. $myDisplay = isset($myShop->pfShopId) && $myShop->pfShopId > 0 ? 1 : 0;
  523. foreach ($itemInfoData as $key => $val) {
  524. $ptItemId = $val['itemId'] ?? 0;
  525. $myStock = $myProduct[$ptItemId]['stock'] ?? 0;
  526. $myStock = floatval($myStock);
  527. $myOnStock = $myProduct[$ptItemId]['onStock'] ?? 0;
  528. $myStockWarning = $myProduct[$ptItemId]['stockWarning'] ?? 5;
  529. $myLack = bcadd($myStock, $myOnStock, 2) > $myStockWarning ? 0 : 1;
  530. $itemInfoData[$key]['myStock'] = $myStock;
  531. $itemInfoData[$key]['myOnStock'] = $myOnStock;
  532. $itemInfoData[$key]['myStockWarning'] = $myStockWarning;
  533. $itemInfoData[$key]['myLack'] = $myLack;
  534. $itemInfoData[$key]['myDisplay'] = $myDisplay;
  535. }
  536. }
  537. $respond = ProductService::assembleData($classIds, $itemInfoData, true);
  538. $classItem = $respond['classItem'] ?? [];
  539. util::success($classItem);
  540. } catch (\Exception $e) {
  541. $transaction->rollBack();
  542. Yii::info("报错内容:" . $e->getMessage());
  543. util::fail();
  544. }
  545. }
  546. public function actionGhsItemV2()
  547. {
  548. $get = Yii::$app->request->get();
  549. $id = $get['id'] ?? 0;
  550. $ghs = GhsClass::getGhsInfo($id);
  551. if (empty($ghs)) {
  552. util::fail('没有找到供货商');
  553. }
  554. $shopId = $ghs['shopId'] ?? 0;
  555. $black = $ghs['black'] ?? 2;
  556. if ($black == 2) {
  557. util::fail('暂无花材');
  558. }
  559. $connection = Yii::$app->db;
  560. $transaction = $connection->beginTransaction();
  561. try {
  562. //获取所有当前供货商的分类 (全部、常用)
  563. //根据分类组装分类下的花材信息
  564. $shop = ShopClass::getById($shopId, true);
  565. $mainId = $shop->mainId ?? 0;
  566. $open = $shop->open ?? 1;
  567. if ($open == 0) {
  568. //九江云朵、丽子鲜花休店不让看花材
  569. if (in_array($mainId, [26374, 1496])) {
  570. util::fail('已休店');
  571. }
  572. }
  573. $classIds = ItemClassClass::getGhsItemClassAll($mainId);
  574. $where['mainId'] = $mainId;
  575. $where['delStatus'] = 0;
  576. //是否显示全部花材,包括下架的
  577. $showAll = $get['showAll'] ?? 0;
  578. if ($showAll == 0) {
  579. $where['status'] = 1;
  580. }
  581. $where['frontHide'] = 0;
  582. $level = $ghs['giveLevel'] ?? 1;
  583. $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,limitBuy';
  584. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  585. $itemInfoData = ProductClass::hdCgGroup($itemInfoData, $level);
  586. $myProduct = ProductClass::getAllByCondition(['mainId' => $this->mainId, 'delStatus' => 0], null, 'id,stock,onStock,stockWarning,itemId', 'itemId');
  587. if (!empty($itemInfoData)) {
  588. $myShop = $this->shop;
  589. $myDisplay = 0;
  590. if (isset($myShop->pfShopId) && $myShop->pfShopId > 0) {
  591. $myDisplay = 1;
  592. }
  593. foreach ($itemInfoData as $key => $val) {
  594. $ptItemId = $val['itemId'] ?? 0;
  595. $myStock = $myProduct[$ptItemId]['stock'] ?? 0;
  596. $myStock = floatval($myStock);
  597. $myOnStock = $myProduct[$ptItemId]['onStock'] ?? 0;
  598. $myStockWarning = $myProduct[$ptItemId]['stockWarning'] ?? 5;
  599. $myLack = bcadd($myStock, $myOnStock, 2) > $myStockWarning ? 0 : 1;
  600. $itemInfoData[$key]['myStock'] = $myStock;
  601. $itemInfoData[$key]['myOnStock'] = $myOnStock;
  602. $itemInfoData[$key]['myStockWarning'] = $myStockWarning;
  603. $itemInfoData[$key]['myLack'] = $myLack;
  604. $itemInfoData[$key]['myDisplay'] = $myDisplay;
  605. }
  606. }
  607. $respond = ProductService::assembleData($classIds, $itemInfoData, true);
  608. if ($showAll == 1) {
  609. if (isset($respond['classItem']) && !empty($respond['classItem'])) {
  610. foreach ($respond['classItem'] as $bigKey => $bigVal) {
  611. if (isset($bigVal['child']) && !empty($bigVal['child'])) {
  612. foreach ($bigVal['child'] as $key => $val) {
  613. unset($bigVal['child'][$key]['cover']);
  614. unset($bigVal['child'][$key]['actualSold']);
  615. unset($bigVal['child'][$key]['defaultGradePrice']);
  616. unset($bigVal['child'][$key]['discountPrice']);
  617. unset($bigVal['child'][$key]['hjDiscountPrice']);
  618. unset($bigVal['child'][$key]['hjPrice']);
  619. unset($bigVal['child'][$key]['presellDateShow']);
  620. unset($bigVal['child'][$key]['prePrice']);
  621. unset($bigVal['child'][$key]['print']);
  622. unset($bigVal['child'][$key]['reachNum']);
  623. unset($bigVal['child'][$key]['reachNumDiscount']);
  624. unset($bigVal['child'][$key]['reachPrice']);
  625. unset($bigVal['child'][$key]['skDiscountPrice']);
  626. unset($bigVal['child'][$key]['skPrice']);
  627. unset($bigVal['child'][$key]['bigUnit']);
  628. unset($bigVal['child'][$key]['weight']);
  629. unset($bigVal['child'][$key]['stockWarning']);
  630. //unset($bigVal['child'][$key]['smallPrice']);
  631. //unset($bigVal['child'][$key]['smallRatio']);
  632. }
  633. $respond['classItem'][$bigKey]['child'] = $bigVal['child'];
  634. }
  635. }
  636. }
  637. }
  638. util::success($respond);
  639. } catch (\Exception $e) {
  640. $transaction->rollBack();
  641. Yii::info("报错内容:" . $e->getMessage());
  642. util::fail();
  643. }
  644. }
  645. public function actionDetail()
  646. {
  647. $id = Yii::$app->request->get('id', 0);
  648. $respond = ProductClass::getItemInfo($id);
  649. $ptItemId = $respond['itemId'] ?? 0;
  650. $ptItemInfo = PtItemClass::getById($ptItemId);
  651. $respond['ptItemInfo'] = $ptItemInfo;
  652. util::success($respond);
  653. }
  654. public function actionUpdate()
  655. {
  656. $shopAdmin = $this->shopAdmin;
  657. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  658. util::fail('超管才能操作');
  659. }
  660. $shop = $this->shop;
  661. $join = $shop->join ?? 0;
  662. $default = $shop->default ?? 0;
  663. if ($join == 0 && $default == 0) {
  664. //因为盘点不能将花材盘成0,所以这边放出入口允许创始人在分店修改分店的花材库存
  665. if ($shopAdmin->super != 1) {
  666. util::fail('请先开通超管权限');
  667. }
  668. }
  669. $deadline = $shop->deadline;
  670. $deadTime = strtotime($deadline);
  671. if (time() > $deadTime) {
  672. util::fail('系统已到期');
  673. }
  674. $post = Yii::$app->request->post();
  675. $discountPrice = $post['discountPrice'] ?? 0;
  676. if ($discountPrice > 0) {
  677. if (isset($post['hjDiscountPrice']) == false || empty($post['hjDiscountPrice'])) {
  678. $addPrice = $post['addPrice'] ?? 0;
  679. $hjAddPrice = $post['hjAddPrice'] ?? 0;
  680. $skMore = $post['skMore'] ?? 0;
  681. $diff = bcsub($addPrice, $hjAddPrice, 2);
  682. if ($diff > 0 && $discountPrice > $diff) {
  683. $hjDiscountPrice = bcsub($discountPrice, $diff, 2);
  684. } else {
  685. $hjDiscountPrice = $discountPrice;
  686. }
  687. $diff = bcsub($skMore, $addPrice, 2);
  688. if ($diff > 0) {
  689. $skDiscountPrice = bcadd($diff, $discountPrice, 2);
  690. } else {
  691. $skDiscountPrice = $discountPrice;
  692. }
  693. $post['skDiscountPrice'] = $skDiscountPrice;
  694. $post['hjDiscountPrice'] = $hjDiscountPrice;
  695. }
  696. $reachNum = $post['reachNum'] ?? 0;
  697. $reachNumDiscount = $post['reachNumDiscount'] ?? 0;
  698. if ($reachNum > 0 && $reachNumDiscount > 0) {
  699. util::fail('特价和满减不能同时进行');
  700. }
  701. }
  702. $post['adminId'] = $this->adminId;
  703. $post['staffId'] = $this->shopAdmin->id;
  704. $post['staffName'] = $this->shopAdmin->name;
  705. $connection = Yii::$app->db;
  706. $transaction = $connection->beginTransaction();
  707. try {
  708. ProductClass::updateProduct($post, $this->shop);
  709. $transaction->commit();
  710. util::complete();
  711. } catch (\Exception $e) {
  712. Yii::info("操作失败:" . $e->getMessage());
  713. $transaction->rollBack();
  714. util::fail('操作失败');
  715. }
  716. }
  717. //批量修改product 排序,成本价,加价
  718. public function actionBatchUpdate()
  719. {
  720. //data = [{id:1,inTurn:10,addPrice:2,cost:10}]
  721. $data = Yii::$app->request->post('data', '');
  722. $type = Yii::$app->request->post('type', '');
  723. if (empty($data) || empty($type)) {
  724. util::fail("参数错误");
  725. }
  726. if (in_array($type, ['inTurn', 'addPrice', 'cost', 'weight', 'ratio']))
  727. $data = json_decode($data, true);
  728. foreach ($data as $v) {
  729. $id = $v['id'];
  730. $val = $v[$type] ?? '';
  731. $where = [];
  732. $where['id'] = $id;
  733. $where['shopId'] = $this->shopId;
  734. $where['sjId'] = $this->sjId;
  735. if (empty($val)) {
  736. util::fail("参数错误1");
  737. }
  738. $data = [
  739. $type => $val
  740. ];
  741. ProductClass::updateByCondition($where, $data);
  742. ProductClass::autoPriceById($id);
  743. }
  744. util::complete();
  745. }
  746. //供货商产品的详情 ssh 20230111
  747. public function actionGhsProductDetail()
  748. {
  749. $get = Yii::$app->request->get();
  750. $id = $get['id'] ?? 0;
  751. $ghsId = $get['ghsId'] ?? 0;
  752. $product = ProductClass::getById($id);
  753. if (empty($product)) {
  754. util::fail('没有花材信息');
  755. }
  756. $ghsInfo = GhsClass::getById($ghsId, true);
  757. $level = isset($ghsInfo->giveLevel) ? $ghsInfo->giveLevel : 1;
  758. $list = ProductClass::groupProductInfo([$product], $level);
  759. $product = current($list);
  760. util::success($product);
  761. }
  762. //批量修改加价 ssh 20211211
  763. public function actionBatchChangeAddPrice()
  764. {
  765. $shopAdmin = $this->shopAdmin;
  766. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  767. util::fail('超管才能操作');
  768. }
  769. $shop = $this->shop;
  770. $pfShopId = $shop->pfShopId ?? 0;
  771. if (!empty($pfShopId)) {
  772. util::fail('请在批发端操作');
  773. }
  774. $addData = Yii::$app->request->post('addData', '');
  775. if (empty($addData)) {
  776. util::fail('没有花材');
  777. }
  778. $itemData = json_decode($addData, true);
  779. if (is_array($itemData) == false) {
  780. util::fail('没有花材...');
  781. }
  782. $ids = array_column($itemData, 'id');
  783. $infoList = ProductClass::getByIds($ids);
  784. if (empty($infoList)) {
  785. util::fail('没有花材');
  786. }
  787. foreach ($infoList as $info) {
  788. if (isset($info['mainId']) == false || $info['mainId'] != $this->mainId) {
  789. util::fail('非常访问,不是你的花材不能修改');
  790. }
  791. }
  792. foreach ($itemData as $key => $data) {
  793. $id = $data['id'] ?? 0;
  794. ProductClass::updateById($id, $data);
  795. }
  796. util::complete('修改成功');
  797. }
  798. }