ProductController.php 36 KB

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