ProductController.php 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017
  1. <?php
  2. /**
  3. * User: admin
  4. * Date Time: 2021/1/15 20:36
  5. */
  6. namespace ghs\controllers;
  7. use biz\admin\classes\AdminRoleClass;
  8. use biz\item\classes\PtItemClass;
  9. use bizGhs\book\classes\BookItemClass;
  10. use bizGhs\cp\classes\CpClass;
  11. use bizGhs\cp\classes\CpItemClass;
  12. use bizGhs\custom\classes\CustomClass;
  13. use bizGhs\item\classes\ItemClassClass;
  14. use bizGhs\item\classes\UnitChangeClass;
  15. use bizGhs\merchant\classes\ShopClass;
  16. use bizGhs\notify\classes\NotifyClass;
  17. use bizGhs\order\classes\CheckOrderClass;
  18. use bizGhs\order\classes\PurchaseOrderClass;
  19. use bizGhs\product\classes\ProductClass;
  20. use bizGhs\product\models\Product;
  21. use bizGhs\product\services\ProductService;
  22. use bizGhs\shop\classes\ShopAdminClass;
  23. use bizGhs\stock\classes\StockInDayClass;
  24. use common\components\imgUtil;
  25. use common\components\noticeUtil;
  26. use common\components\printUtil;
  27. use common\components\stringUtil;
  28. use common\components\util;
  29. use bizGhs\stock\classes\OnStockRecordClass;
  30. use Yii;
  31. class ProductController extends BaseController
  32. {
  33. public $guestAccess = ['index'];
  34. //修改平均成本 ssh 2025114
  35. public function actionModifyAvCost()
  36. {
  37. $get = Yii::$app->request->get();
  38. $id = $get['id'] ?? 0;
  39. $amount = $get['avCost'] ?? 0;
  40. $product = ProductClass::getById($id, true);
  41. if (empty($product)) {
  42. util::fail('没有找到花材');
  43. }
  44. if ($product->mainId != $this->mainId) {
  45. util::fail('不是你的花材');
  46. }
  47. $product->avCost = $amount;
  48. $product->save();
  49. util::complete('修改成功');
  50. }
  51. //修改路上库存 ssh 20231219
  52. public function actionModifyOnStock()
  53. {
  54. $get = Yii::$app->request->get();
  55. $id = $get['id'] ?? 0;
  56. $onStock = $get['onStock'] ?? 0;
  57. $info = ProductClass::getLockById($id);
  58. ProductClass::check($info, $this->mainId);
  59. $oldOnStock = $info->onStock ?? 0;
  60. $newOnStock = $onStock;
  61. $num = bcsub($newOnStock, $oldOnStock);
  62. $num = abs($num);
  63. $staff = $this->shopAdmin;
  64. $staffName = $staff->name ?? '';
  65. $onRecord = [];
  66. $onRecord['sjId'] = $this->sjId;
  67. $onRecord['shopId'] = $this->shopId;
  68. $onRecord['mainId'] = $this->mainId;
  69. $onRecord['orderSn'] = '';
  70. $onRecord['productId'] = $id;
  71. $onRecord['itemId'] = $info->itemId ?? 0;
  72. $onRecord['relateName'] = $staffName;
  73. $onRecord['itemNum'] = $num;
  74. $onRecord['oldStock'] = $oldOnStock;
  75. $onRecord['newStock'] = $newOnStock;
  76. $onRecord['ptStyle'] = 2;
  77. OnStockRecordClass::modify($onRecord);
  78. $info->onStock = $newOnStock;
  79. $info->save();
  80. util::complete('修改成功');
  81. }
  82. //停止预售 ssh 20230224
  83. public function actionCancelPreSell()
  84. {
  85. $get = Yii::$app->request->get();
  86. $id = $get['id'] ?? 0;
  87. $clearStock = $get['clearStock'] ?? -1;
  88. if ($clearStock == -1) {
  89. util::fail('请选择是否清库存');
  90. }
  91. $info = ProductClass::getById($id, true);
  92. ProductClass::check($info, $this->mainId);
  93. $info->presell = 0;
  94. $info->presellDate = '';
  95. $info->save();
  96. if ($clearStock == 1) {
  97. // 清库存,参考项目里的盘点功能,盘点成0
  98. $staff = $this->shopAdmin;
  99. $modifyData = [
  100. 'remark' => '停止预售清库存',
  101. 'sjId' => $this->sjId,
  102. 'shopId' => $this->shopId,
  103. 'adminId' => $this->adminId,
  104. 'mainId' => $this->mainId,
  105. 'staffName' => $staff->name ?? '',
  106. 'itemInfo' => [['productId' => $id, 'bigNum' => 0, 'smallNum' => 0]],
  107. ];
  108. CheckOrderClass::opOrder($modifyData, false, false);
  109. }
  110. util::complete();
  111. }
  112. //修改预售 ssh 20221214
  113. public function actionChangePresell()
  114. {
  115. $post = Yii::$app->request->post();
  116. $id = $post['id'] ?? 0;
  117. $info = ProductClass::getById($id, true);
  118. ProductClass::check($info, $this->mainId);
  119. try {
  120. $str = $post['date'] ?? '';
  121. $presell = $post['presell'] ?? 0;
  122. $newStr = '';
  123. if ($presell == 1) {
  124. if (empty($str)) {
  125. util::fail('请填写预售日期');
  126. }
  127. $arr = json_decode($str, true);
  128. if (empty($arr)) {
  129. util::fail('请填写预售日期');
  130. }
  131. $new = [];
  132. foreach ($arr as $date) {
  133. $new[] = strtotime($date);
  134. }
  135. $new = array_unique(array_filter($new));
  136. sort($new);
  137. $newStr = implode(',', $new);
  138. //使用redis缓存来保存某用户设置了某花材的预售
  139. $redis = Yii::$app->redis;
  140. $redis->set('product_presell:' . $id, $new[0]);
  141. } else {
  142. $redis = Yii::$app->redis;
  143. if ($redis->exists('product_presell:' . $id)) {
  144. $redis->del('product_presell:' . $id);
  145. }
  146. }
  147. $info->presell = $presell;
  148. $info->presellDate = $newStr;
  149. //$info->discountPrice = 0;
  150. if ($presell == 1 && $info->discountPrice > 0) {
  151. util::fail('已设置特价,无法开启预售');
  152. }
  153. $info->save();
  154. } catch (\Exception $e) {
  155. // 失败情况,要把缓存删除
  156. if ($presell == 1) {
  157. $redis = Yii::$app->redis;
  158. if ($redis->exists('product_presell:' . $id)) {
  159. $redis->del('product_presell:' . $id);
  160. }
  161. }
  162. }
  163. util::complete();
  164. }
  165. //取消满减 ssh 20231216
  166. public function actionCancelFullOff()
  167. {
  168. $get = Yii::$app->request->get();
  169. $id = $get['id'] ?? 0;
  170. $info = ProductClass::getById($id, true);
  171. ProductClass::check($info, $this->mainId);
  172. $db = Yii::$app->db;
  173. $transaction = $db->beginTransaction();
  174. try {
  175. $info->reachNum = 0;
  176. $info->reachNumDiscount = 0;
  177. $info->save();
  178. // 如果当前在总店,则直营分店要同步取消
  179. $currentShop = $this->shop;
  180. if (isset($currentShop->default) && $currentShop->default == 1) {
  181. $sjId = $currentShop->sjId ?? 0;
  182. $ptItemId = $info->itemId ?? 0;
  183. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  184. if (!empty($chainShopList)) {
  185. foreach ($chainShopList as $chainShop) {
  186. $chainShopId = $chainShop->id ?? 0;
  187. if ($chainShopId == $this->shopId) {
  188. continue;
  189. }
  190. $isDefault = $chainShop->default ?? 0;
  191. $isJoin = $chainShop->join ?? 0;
  192. if ($isDefault == 0 && $isJoin == 0) {
  193. $chainMainId = $chainShop->mainId ?? 0;
  194. $chainProduct = ProductClass::getByCondition(['mainId' => $chainMainId, 'itemId' => $ptItemId], true);
  195. if (!empty($chainProduct)) {
  196. $chainProduct->reachNum = 0;
  197. $chainProduct->reachNumDiscount = 0;
  198. $chainProduct->save();
  199. }
  200. }
  201. }
  202. }
  203. }
  204. $transaction->commit();
  205. util::complete('取消成功');
  206. } catch (\Exception $e) {
  207. $transaction->rollBack();
  208. util::fail('取消失败:' . $e->getMessage());
  209. }
  210. }
  211. //取消特价 ssh 20220901
  212. public function actionCancelDiscount()
  213. {
  214. $get = Yii::$app->request->get();
  215. $id = $get['id'] ?? 0;
  216. $info = ProductClass::getById($id, true);
  217. ProductClass::check($info, $this->mainId);
  218. $db = Yii::$app->db;
  219. $transaction = $db->beginTransaction();
  220. try {
  221. $info->discountPrice = 0;
  222. $info->skDiscountPrice = 0;
  223. $info->hjDiscountPrice = 0;
  224. $info->save();
  225. // 如果当前在总店,则直营分店要同步取消特价
  226. $currentShop = $this->shop;
  227. if (isset($currentShop->default) && $currentShop->default == 1) {
  228. $sjId = $currentShop->sjId ?? 0;
  229. $ptItemId = $info->itemId ?? 0;
  230. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  231. if (!empty($chainShopList)) {
  232. foreach ($chainShopList as $chainShop) {
  233. $chainShopId = $chainShop->id ?? 0;
  234. if ($chainShopId == $this->shopId) {
  235. continue;
  236. }
  237. $isDefault = $chainShop->default ?? 0;
  238. $isJoin = $chainShop->join ?? 0;
  239. if ($isDefault == 0 && $isJoin == 0) {
  240. $chainMainId = $chainShop->mainId ?? 0;
  241. $chainProduct = ProductClass::getByCondition(['mainId' => $chainMainId, 'itemId' => $ptItemId], true);
  242. if (!empty($chainProduct)) {
  243. $chainProduct->discountPrice = 0;
  244. $chainProduct->skDiscountPrice = 0;
  245. $chainProduct->hjDiscountPrice = 0;
  246. $chainProduct->save();
  247. }
  248. }
  249. }
  250. }
  251. }
  252. $transaction->commit();
  253. util::complete('取消成功');
  254. } catch (\Exception $e) {
  255. $transaction->rollBack();
  256. util::fail('取消失败:' . $e->getMessage());
  257. }
  258. }
  259. //打印花材的标签 ssh 20220602
  260. public function actionPrint()
  261. {
  262. $get = Yii::$app->request->get();
  263. $id = $get['id'] ?? 0;
  264. $num = $get['num'] ?? 1;
  265. $num = floatval($num);
  266. $num = (int)$num;
  267. $type = $get['type'] ?? 0;
  268. $priceOption = $get['priceOption'] ?? 0;
  269. $info = ProductClass::getById($id, true);
  270. if (empty($info)) {
  271. util::fail('没有找到花材');
  272. }
  273. $priceMap = CustomClass::$levelPriceKeyMap;
  274. $addPriceMap = CustomClass::$levelAddPriceKeyMap;
  275. $price = ProductClass::getFinalPrice($info, 0, $priceMap, $addPriceMap);
  276. $price = floatval($price);
  277. $hyPrice = ProductClass::getFinalPrice($info, 1, $priceMap, $addPriceMap);
  278. $hyPrice = floatval($hyPrice);
  279. $name = $info->name ?? '';
  280. $name = preg_replace('/\s{2,}/', ' ', $name ?? '');
  281. $ptItemId = $info->itemId ?? 0;
  282. $mainId = $info->mainId ?? 0;
  283. $ext = $this->shopExt;
  284. $printLabelSn = $ext->printLabelSn ?? '';
  285. if (empty($printLabelSn)) {
  286. util::fail('请设置标签打印机');
  287. }
  288. $p = new printUtil($printLabelSn);
  289. $unit = $info->ratio . $info->smallUnit . '装';
  290. if (isset($info->ratioType) && $info->ratioType == 1) {
  291. $unit = '若干' . $info->smallUnit . '装';
  292. }
  293. $p->times = $num;
  294. if ($priceOption == 2) {
  295. // 每7个汉字截取一次(支持汉字、字母、数字混合),多处需要同步修改,关键词 label_print_substr
  296. $content = '';
  297. $len = mb_strlen($name, 'UTF-8');
  298. $baseNum = 30;
  299. $u = 0;
  300. for ($i = 0; $i < $len; $i += 6) {
  301. $currentName = mb_substr($name, $i, 6, 'UTF-8');
  302. $number = $baseNum + 90 * $u;
  303. $u++;
  304. $content .= '<TEXT x="10" y="' . $number . '" font="12" w="3" h="3" r="0">' . $currentName . '</TEXT>';
  305. }
  306. } else {
  307. if (stringUtil::getWordNum($name) > 8) {
  308. $content = '<TEXT x="30" y="30" font="12" w="1" h="2" r="0">' . $name . '</TEXT>';
  309. } else {
  310. $content = '<TEXT x="30" y="30" font="12" w="2" h="2" r="0">' . $name . '</TEXT>';
  311. }
  312. }
  313. if ($type == 0) {
  314. if ($priceOption != 2) {
  315. //打标签
  316. $content .= '<BC128 x="30" y="115" h="75" s="1" r="0" n="3" w="10">' . $ptItemId . '</BC128>';
  317. if (in_array($mainId, [52, 1459])) {
  318. $content .= '<TEXT x="30" y="250" font="12" w="2" h="2" r="0">惠雅鲜花</TEXT>';
  319. } elseif ($mainId == 16948) {
  320. $content .= '<TEXT x="30" y="250" font="12" w="2" h="2" r="0">淘花里鲜花</TEXT>';
  321. } elseif ($mainId == 72057) {
  322. $content .= '<TEXT x="30" y="250" font="12" w="2" h="2" r="0">淘花里小榄店</TEXT>';
  323. } else {
  324. if ($priceOption == 0) {
  325. $content .= '<TEXT x="30" y="250" font="12" w="2" h="2" r="0">' . $unit . '</TEXT>';
  326. } else {
  327. $content .= '<TEXT x="30" y="250" font="12" w="2" h="2" r="0">' . $unit . ' ' . $price . '元</TEXT>';
  328. }
  329. }
  330. }
  331. } else {
  332. if (getenv('YII_ENV') == 'production') {
  333. $doublePrice = [52, 1459];
  334. } else {
  335. $doublePrice = [];
  336. }
  337. if (in_array($this->mainId, $doublePrice)) {
  338. //惠雅鲜花,打价格,显示正价和会员价,多处同步修改,关键词 hy_print_label_price ssh 20260101
  339. $content .= '<TEXT x="35" y="90" font="12" w="2" h="2" r="0">----------</TEXT>';
  340. $content .= '<TEXT x="35" y="150" font="12" w="2" h="2" r="0">正价 ' . $price . '元</TEXT>';
  341. $content .= '<TEXT x="35" y="230" font="12" w="2" h="2" r="0">会员价 ' . $hyPrice . '元</TEXT>';
  342. } elseif ($this->mainId == 4556) {
  343. //打价格,显示零售价,并且不带元,杭州斗南鲜花不要带元
  344. $content .= '<TEXT x="35" y="150" font="12" w="4" h="4" r="0">' . $price . '</TEXT>';
  345. } else {
  346. $content .= '<TEXT x="35" y="150" font="12" w="4" h="4" r="0">' . $price . '元</TEXT>';
  347. }
  348. }
  349. $p->printLabelMsg($content);
  350. util::complete();
  351. }
  352. //取出今天有入库的花材 ssh 20221024
  353. public function actionStockIn()
  354. {
  355. $py = Yii::$app->request->get('py', '');
  356. $version = Yii::$app->request->get('version', 0);
  357. if ($version == 0) {
  358. util::fail('请先升级版本到1.0.70以上');
  359. }
  360. $classInfo = ItemClassClass::getGhsItemClassAll($this->mainId);
  361. $mainId = $this->mainId ?? 0;
  362. $where['mainId'] = $mainId;
  363. if ($py) {
  364. $pyName = strtolower($py);
  365. $where['py'] = $pyName;
  366. }
  367. $arr = StockInDayClass::getAllByCondition(['mainId' => $mainId], null, '*');
  368. if (empty($arr)) {
  369. util::fail('没有花材需要盘点');
  370. }
  371. $ids = array_column($arr, 'itemId');
  372. $ids = array_unique($ids);
  373. $where['id'] = ['in', $ids];
  374. $level = 1;
  375. $field = 'id,py,cover,name,ratio,cost,addPrice,classId,skMore,skPrice,itemId,smallUnit,smallRatio,presellDate,bigUnit,smallUnit,ratioType,variety,price,stock,stockWarning,discountPrice,presell,presellDate';
  376. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  377. $itemInfoData = ProductClass::groupProductInfo($itemInfoData, $level);
  378. $data = ProductService::assembleData($classInfo, $itemInfoData, true);
  379. util::success($data);
  380. }
  381. //按分类列出所有花材
  382. public function actionShow()
  383. {
  384. $py = Yii::$app->request->get('py', '');
  385. $name = Yii::$app->request->get('name', '');
  386. $requestType = Yii::$app->request->get('requestType', 'common');
  387. //默认显示上架商品
  388. $status = Yii::$app->request->get('status', 1);
  389. //默认显示未删除商品
  390. $delStatus = Yii::$app->request->get('delStatus', 0);
  391. $classId = Yii::$app->request->get('classId', 0);
  392. //获取所有当前供货商的分类 (全部、常用)
  393. //根据分类组装分类下的花材信息
  394. //中央ID
  395. $classInfo = ItemClassClass::getGhsItemClassAll($this->mainId);
  396. $where['mainId'] = $this->mainId;
  397. if ($py) {
  398. $pyName = strtolower($py);
  399. $where['py'] = $pyName;
  400. }
  401. //改价和全部有库存花材列表
  402. if ($requestType == 'changePrice' || $requestType == 'hasStockList') {
  403. $where['stock>'] = 0;
  404. }
  405. if (!empty($status)) {
  406. $where['status'] = $status;
  407. if ($requestType == 'itemList') {
  408. unset($where['status']);
  409. }
  410. }
  411. if ($requestType == 'book') {
  412. unset($where['status']);
  413. }
  414. $where['delStatus'] = $delStatus;
  415. if (!empty($classId)) {
  416. $where['classId'] = $classId;
  417. }
  418. if (!empty($name)) {
  419. if (stringUtil::isLetter($name)) {
  420. $name = strtolower($name);
  421. $where['py'] = ['like', $name];
  422. } else {
  423. $where['name'] = ['like', $name];
  424. }
  425. }
  426. if ($requestType == 'hideItem') {
  427. $where['frontHide'] = 1;
  428. }
  429. //客户等级
  430. $customId = Yii::$app->request->get('customId', 0);
  431. $level = 1;
  432. $custom = [];
  433. if (!empty($customId)) {
  434. $custom = CustomClass::getById($customId, true);
  435. $level = $custom->level ?? 1;
  436. }
  437. $itemInfoData = [];
  438. $fieldMap = [
  439. 'hasStockList' => 'id,py,cover,name,classId,itemId,price,skPrice,hjPrice,stock,onStock,cost,avCost,addPrice,hjAddPrice,skMore,limitBuy',
  440. 'hideItem' => 'id,py,cover,name,classId,itemId,price,skPrice,hjPrice,stock,onStock,cost,avCost,addPrice,hjAddPrice,skMore,limitBuy',
  441. 'changePrice' => 'id,py,cover,name,cost,itemId,classId,addPrice,skPrice,hjPrice,discountPrice,hjDiscountPrice,skDiscountPrice,skMore,hjAddPrice,variety,price,stock,onStock,presell,frontHide,reachNum,reachNumDiscount,limitBuy',
  442. 'changeStock' => 'id,py,cover,name,cost,itemId,status,classId,addPrice,skPrice,discountPrice,skMore,variety,price,stock,frontHide,limitBuy',
  443. 'kd' => 'id,py,cover,name,cost,itemId,classId,skPrice,variety,price,hjPrice,hjDiscountPrice,skDiscountPrice,discountPrice,stock,weight,stockWarning,presell,ratioType,smallUnit,smallRatio,bigUnit,ratio,frontHide,reachNum,reachNumDiscount,limitBuy,itemRemark',
  444. 'book' => 'id,py,cover,name,cost,itemId,classId,skPrice,variety,price,hjPrice,hjDiscountPrice,skDiscountPrice,discountPrice,stock,weight,stockWarning,presell,ratioType,smallUnit,smallRatio,bigUnit,ratio,frontHide,limitBuy,itemRemark',
  445. 'itemList' => 'id,py,cover,name,cost,avCost,itemId,classId,skPrice,variety,price,hjPrice,discountPrice,hjDiscountPrice,skDiscountPrice,stock,onStock,actualSold,status,ratio,ratioType,presell,frontHide,auth,reachNum,reachNumDiscount,addPrice,hjAddPrice,skMore,limitBuy,itemRemark',
  446. 'cgStaffList' => 'id,py,cover,name,cost,itemId,classId,skPrice,variety,price,hjPrice,discountPrice,hjDiscountPrice,skDiscountPrice,stock,onStock,actualSold,status,ratio,ratioType,presell,frontHide,auth,reachNum,reachNumDiscount,cgStaffId,cgStaffName,limitBuy',
  447. 'outList' => 'id,py,cover,name,cost,itemId,classId,addPrice,skPrice,bigUnit,smallUnit,ratio,ratioType,discountPrice,skMore,variety,price,stock,presell,status,limitBuy,itemRemark',
  448. 'stopList' => 'id,py,cover,name,cost,itemId,classId,addPrice,skPrice,bigUnit,smallUnit,ratio,ratioType,discountPrice,skMore,variety,price,stock,presell,status,delStatus,limitBuy',
  449. 'removeList' => 'id,py,cover,name,cost,itemId,classId,addPrice,skPrice,bigUnit,smallUnit,ratio,ratioType,discountPrice,skMore,variety,price,stock,presell,status,limitBuy',
  450. 'check' => 'id,py,cover,name,itemId,classId,variety,price,discountPrice,stock,stockWarning,presell,ratioType,smallUnit,bigUnit,ratio,frontHide,limitBuy',
  451. 'break' => 'id,py,cover,name,itemId,classId,variety,price,discountPrice,stock,stockWarning,presell,ratioType,smallUnit,bigUnit,ratio,frontHide,limitBuy',
  452. 'part' => 'id,py,cover,name,itemId,classId,variety,price,discountPrice,stock,stockWarning,presell,ratioType,smallUnit,bigUnit,ratio,limitBuy',
  453. 'stockOut' => 'id,py,cover,name,itemId,classId,variety,price,discountPrice,stock,stockWarning,presell,ratioType,smallUnit,bigUnit,ratio,smallRatio,frontHide,limitBuy',
  454. 'cg' => 'id,py,cover,name,itemId,classId,variety,price,discountPrice,stock,presell,ratioType,smallUnit,bigUnit,ratio,weight,frontHide,limitBuy',
  455. 'warning' => 'id,py,cover,name,cost,itemId,classId,skPrice,variety,price,hjPrice,discountPrice,stock,onStock,actualSold,presell,bigUnit,stockWarning,status,frontHide,limitBuy',
  456. ];
  457. if (!isset($fieldMap[$requestType])) {
  458. util::fail('没有数据');
  459. }
  460. $field = $fieldMap[$requestType];
  461. // 获取数据
  462. if ($requestType == 'warning') {
  463. $itemInfoData = Product::find()->where("mainId={$this->mainId}")
  464. ->andWhere('`stock`+`onStock`<`stockWarning`')
  465. ->andWhere("delStatus=0")
  466. ->select($field)->asArray()->all();
  467. } else {
  468. // 处理特殊的 where 条件
  469. if ($requestType == 'cgStaffList') {
  470. unset($where['status']);
  471. $where['delStatus'] = 0;
  472. } elseif ($requestType == 'outList') {
  473. $where['status'] = 2;
  474. $where['delStatus'] = 0;
  475. $where['removeStatus'] = 0;
  476. } elseif ($requestType == 'stopList') {
  477. unset($where['status']);
  478. $where['delStatus'] = 1;
  479. $where['removeStatus'] = 0;
  480. } elseif ($requestType == 'removeList') {
  481. unset($where['status']);
  482. $where['delStatus'] = 1;
  483. $where['removeStatus'] = 1;
  484. }
  485. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  486. }
  487. // 数据后处理
  488. if ($requestType == 'hasStockList') {
  489. if (!empty($itemInfoData)) {
  490. foreach ($itemInfoData as $key => $val) {
  491. $shortCover = $val['cover'] ?? '';
  492. $itemInfoData[$key]['cover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_100,w_100";
  493. $itemInfoData[$key]['shortCover'] = $shortCover;
  494. }
  495. }
  496. } elseif ($requestType == 'changePrice') {
  497. $itemInfoData = ProductClass::changePriceGroup($itemInfoData, $level);
  498. } elseif ($requestType == 'changeStock') {
  499. $itemInfoData = ProductClass::changeStockGroup($itemInfoData, $level);
  500. } elseif ($requestType == 'kd') {
  501. $itemInfoData = ProductClass::kdItemGroup($itemInfoData, $level, $custom);
  502. } elseif ($requestType == 'book') {
  503. $itemInfoData = ProductClass::bookItemGroup($itemInfoData, $level);
  504. } elseif ($requestType == 'itemList') {
  505. $itemInfoData = ProductClass::itemListGroup($itemInfoData, $level);
  506. } elseif ($requestType == 'cgStaffList') {
  507. $itemInfoData = ProductClass::cgStaffListGroup($itemInfoData, $level);
  508. } elseif (in_array($requestType, ['outList', 'stopList', 'removeList'])) {
  509. $itemInfoData = ProductClass::simpleGroup($itemInfoData, $level);
  510. } elseif ($requestType == 'check') {
  511. $itemInfoData = ProductClass::checkItemGroup($itemInfoData, $level);
  512. } elseif ($requestType == 'break') {
  513. $itemInfoData = ProductClass::breakItemGroup($itemInfoData, $level);
  514. } elseif ($requestType == 'part') {
  515. $itemInfoData = ProductClass::partItemGroup($itemInfoData, $level);
  516. } elseif ($requestType == 'stockOut') {
  517. $itemInfoData = ProductClass::stockOutGroup($itemInfoData, $level);
  518. } elseif ($requestType == 'cg') {
  519. $itemInfoData = ProductClass::cgItemGroup($itemInfoData, $level);
  520. } elseif ($requestType == 'warning') {
  521. $itemInfoData = ProductClass::warningGroup($itemInfoData, $level);
  522. }
  523. $data = ProductService::assembleItemData($classInfo, $itemInfoData);
  524. util::success($data);
  525. }
  526. //取出今天有入库的花材 ssh 20221224
  527. public function actionGetStockIn()
  528. {
  529. $py = Yii::$app->request->get('py', '');
  530. $version = Yii::$app->request->get('version', 0);
  531. if ($version == 0) {
  532. util::fail('请先升级版本到1.0.70以上');
  533. }
  534. $classInfo = ItemClassClass::getGhsItemClassAll($this->mainId);
  535. $mainId = $this->mainId ?? 0;
  536. $where['mainId'] = $mainId;
  537. if ($py) {
  538. $pyName = strtolower($py);
  539. $where['py'] = $pyName;
  540. }
  541. $arr = StockInDayClass::getAllByCondition(['mainId' => $mainId], null, '*');
  542. if (empty($arr)) {
  543. util::fail('没有花材需要盘点');
  544. }
  545. $ids = array_column($arr, 'itemId');
  546. $ids = array_unique($ids);
  547. $where['id'] = ['in', $ids];
  548. $level = 1;
  549. $field = 'id,py,cover,name,itemId,classId,variety,price,discountPrice,stock,stockWarning,presell,ratioType,smallUnit,bigUnit,ratio';
  550. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  551. $itemInfoData = ProductClass::checkItemGroup($itemInfoData, $level);
  552. $data = ProductService::assembleItemData($classInfo, $itemInfoData);
  553. util::success($data);
  554. }
  555. //按分类列出所有花材
  556. public function actionIndex()
  557. {
  558. $py = Yii::$app->request->get('py', '');
  559. $requestType = Yii::$app->request->get('requestType', 'common');
  560. //默认显示上架商品
  561. $status = Yii::$app->request->get('status', 1);
  562. //默认显示未删除商品
  563. $delStatus = Yii::$app->request->get('delStatus', 0);
  564. $warning = Yii::$app->request->get('warning', 0);
  565. //获取所有当前供货商的分类 (全部、常用)
  566. //根据分类组装分类下的花材信息
  567. //中央ID
  568. $classInfo = ItemClassClass::getGhsItemClassAll($this->mainId);
  569. $where['mainId'] = $this->mainId;
  570. if ($py) {
  571. $pyName = strtolower($py);
  572. $where['py'] = $pyName;
  573. }
  574. if ($requestType == 'hasStock') {
  575. //改价只显示库存大于0的
  576. $where['stock>'] = 0;
  577. }
  578. if (!empty($status)) {
  579. $where['status'] = $status;
  580. }
  581. $where['delStatus'] = $delStatus;
  582. //客户等级
  583. $customId = Yii::$app->request->get('customId', 0);
  584. $level = 1;
  585. if (!empty($customId)) {
  586. $custom = CustomClass::getById($customId, true);
  587. $level = $custom->level ?? 1;
  588. }
  589. if ($warning == 1) {
  590. //库存预警
  591. $field = 'id,py,cover,name,ratio,cost,addPrice,classId,skMore,skPrice,itemId,smallUnit,smallRatio,presellDate,bigUnit,smallUnit,ratioType,variety,price,stock,stockWarning,discountPrice,presell';
  592. $itemInfoData = Product::find()->where("mainId={$this->mainId}")
  593. ->andWhere('`stock`<`stockWarning`')
  594. ->andWhere("delStatus=0")
  595. ->select($field)->asArray()->all();
  596. } else {
  597. $field = 'id,py,cover,name,ratio,cost,addPrice,classId,skMore,skPrice,itemId,smallUnit,smallRatio,presellDate,bigUnit,smallUnit,ratioType,variety,price,stock,stockWarning,discountPrice,presell';
  598. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  599. }
  600. $itemInfoData = ProductClass::groupProductInfo($itemInfoData, $level);
  601. $data = ProductService::assembleData($classInfo, $itemInfoData, true);
  602. util::success($data);
  603. }
  604. //所有花材分页和不分页
  605. public function actionList()
  606. {
  607. $get = Yii::$app->request->get();
  608. $where = [];
  609. $classId = Yii::$app->request->get('classId', 0);
  610. if (!empty($classId)) {
  611. $where['classId'] = $classId;
  612. }
  613. $name = Yii::$app->request->get('name', '');
  614. if (empty($name)) {
  615. $py = Yii::$app->request->get('py', '');
  616. $name = !empty($py) ? $py : '';
  617. }
  618. if (!empty($name)) {
  619. if (stringUtil::isLetter($name)) {
  620. $where['py'] = ['like', $name];
  621. } else {
  622. $where['name'] = ['like', $name];
  623. }
  624. }
  625. $status = Yii::$app->request->get('status', 0);
  626. if (!empty($status)) {
  627. $where['status'] = $status;
  628. }
  629. $delStatus = Yii::$app->request->get('delStatus', 0);
  630. if ($delStatus != 2) {
  631. $where['delStatus'] = $delStatus;
  632. }
  633. $where['mainId'] = $this->mainId;
  634. $productIds = Yii::$app->request->get('ids', '');
  635. if (!empty($productIds)) {
  636. $productIdArr = explode(',', $productIds);
  637. if (!empty($productIdArr)) {
  638. $where['id'] = ['in', $productIdArr];
  639. }
  640. }
  641. //输出标准会员价,即批发价,默认显示的是批发价
  642. $level = 1;
  643. $type = Yii::$app->request->get('type', '');
  644. $showPage = Yii::$app->request->get('showPage', 0);
  645. if ($showPage) {
  646. if ($type == 'waring') {
  647. $pro = Product::find()->where("mainId={$this->mainId}")->andWhere('`stock`<`stockWarning`')->select('id')->asArray()->all();
  648. $lackIds = array_column($pro, 'id');
  649. if (empty($lackIds)) {
  650. util::success([]);
  651. }
  652. $lackIds = array_filter(array_unique($lackIds));
  653. $where['id'] = ['in', $lackIds];
  654. $respond = ProductClass::getList("*", $where, "actualSold desc");
  655. } else {
  656. $hasStock = $get['hasStock'] ?? -1;
  657. if ($hasStock == 0) {
  658. $where['stock'] = 0;
  659. }
  660. if ($hasStock == 1) {
  661. $where['stock>'] = 0;
  662. }
  663. $cgStaffId = $get['cgStaffId'] ?? 0;
  664. if (!empty($cgStaffId)) {
  665. $where['cgStaffId'] = $cgStaffId;
  666. }
  667. $respond = ProductClass::getList("*", $where, "inTurn desc");
  668. }
  669. $respond['list'] = ProductClass::groupProductInfo($respond['list'], $level);
  670. $respond['list'] = ProductClass::groupClassName($this->mainId, $respond['list']);
  671. util::success($respond);
  672. } else {
  673. if ($type == 'waring') {
  674. //库存预警 按库存从小到大排序
  675. $data = ProductClass::getAllByCondition($where, ['stock' => SORT_ASC, 'inTurn' => SORT_DESC], "*");
  676. } else {
  677. if (!empty($productIds)) {
  678. //优先按 $productIds 排序 "FIELD(`id`,4,5,6)"=>true
  679. $data = ProductClass::getAllByCondition($where, ["FIELD(`id`,$productIds)" => true, 'inTurn' => SORT_DESC], "*");
  680. } else {
  681. $data = ProductClass::getAllByCondition($where, ['actualSold' => SORT_DESC, 'inTurn' => SORT_DESC], "*");
  682. }
  683. }
  684. $data = ProductClass::groupProductInfo($data, $level);
  685. $data = ProductClass::groupClassName($this->mainId, $data);
  686. if ($type == 'waring') {
  687. //库存预警
  688. $newData = [];
  689. foreach ($data as $v) {
  690. if ($v['stock'] <= $v['stockWarning']) {
  691. $newData[] = $v;
  692. }
  693. }
  694. util::success(['list' => $newData]);
  695. }
  696. util::success(['list' => $data]);
  697. }
  698. }
  699. //所有花材
  700. public function actionAll()
  701. {
  702. $where = [];
  703. $where['mainId'] = $this->mainId;
  704. $classId = Yii::$app->request->get('classId', 0);
  705. if (!empty($classId)) {
  706. $where['classId'] = $classId;
  707. }
  708. $status = Yii::$app->request->get('status', 0);
  709. if (!empty($status)) {
  710. $where['status'] = $status;
  711. }
  712. $delStatus = Yii::$app->request->get('delStatus', 0);
  713. if ($delStatus != 2) {
  714. $where['delStatus'] = $delStatus;
  715. }
  716. //输出标准会员价,即批发价,默认显示的是批发价
  717. $level = 1;
  718. $data = ProductClass::getAllByCondition($where, ['actualSold' => SORT_DESC, 'inTurn' => SORT_DESC], "*");
  719. $data = ProductClass::groupProductInfo($data, $level);
  720. util::success(['list' => $data]);
  721. }
  722. /**
  723. * 职责:商家后台批量修改花材价格(批发价、零售价、会员价)
  724. * 入参:HTTP POST 数据
  725. * 返回:修改成功或失败消息的 JSON
  726. * 副作用:会批量保存多条商品价格信息,一次性批量往 xhGhsPriceChange 表插入改价日志
  727. * 关键边界:
  728. * 1. 提取循环体内部关于分店管理员的重复 SQL 查询至外层并作 Map 映射。
  729. * 2. 大批量合并商品查询,一次性拉出总店和分店下所有关联的商品数据。
  730. * 3. 收集所有改价日志后采用 bulk-insert 方式一键提交,降低高并发下的事务锁冲突和 RTT 开销。
  731. */
  732. public function actionBatchChangePrice()
  733. {
  734. $shop = $this->shop;
  735. $join = $shop->join ?? 0;
  736. $default = $shop->default ?? 0;
  737. if ($join == 0 && $default == 0) {
  738. //除了小蔡鲜花的蔡总、郑总、苏南3007,其他人直营分店不能改价格,多处要同步修改,关键词 direct_price_change
  739. if (!in_array($this->adminId, [35725, 23659, 29264])) {
  740. util::fail('直营店不能改价');
  741. }
  742. }
  743. $shopAdmin = $this->shopAdmin;
  744. if (!isset($shopAdmin->super) || $shopAdmin->super != 1) {
  745. util::fail('超管才能修改');
  746. }
  747. $shop = $this->shop;
  748. $default = $shop->default ?? 0;
  749. $adminId = $this->adminId ?? 0;
  750. //normal常规改价,cg采购改价
  751. $changeType = Yii::$app->request->post('changeType', 'normal');
  752. $targetId = Yii::$app->request->post('targetId', 0);
  753. $changeParams = ['staffId' => $shopAdmin->id, 'staffName' => $shopAdmin->name, 'changeType' => $changeType, 'targetId' => $targetId];
  754. $post = Yii::$app->request->post();
  755. $data = $post['data'] ?? '';
  756. if (empty($data)) {
  757. util::fail('没有修改');
  758. }
  759. $arr = json_decode($data, true);
  760. if (empty($arr)) {
  761. util::fail('没有修改哦');
  762. }
  763. $appVersion = $post['appVersion'] ?? 0;
  764. if ($appVersion != 1) {
  765. util::fail('请先升级应用');
  766. }
  767. //解决重复请求问题
  768. $cacheKey = 'batch_change_price_' . $targetId;
  769. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  770. if (!empty($has)) {
  771. util::fail('请5秒后操作');
  772. }
  773. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 5, 'has']);
  774. $connection = Yii::$app->db;
  775. $transaction = $connection->beginTransaction();
  776. try {
  777. $needNotice = false;
  778. $cg = PurchaseOrderClass::getLockById($targetId);
  779. if (!empty($cg)) {
  780. if (isset($cg->mainId) == false || $cg->mainId != $this->mainId) {
  781. util::fail('不是您的采购单');
  782. }
  783. //批量改价同时入库
  784. if ($cg->status == PurchaseOrderClass::PURCHASE_ORDER_STATUS_SENDING) {
  785. $staff = $this->shopAdmin;
  786. $data = [];
  787. $data['staffId'] = $staff->id ?? 0;
  788. $data['staffName'] = $staff->name ?? '';
  789. $data['adminId'] = $this->adminId ?? 0;
  790. $data['shop'] = $this->shop;
  791. PurchaseOrderClass::putIn($cg, $data);
  792. $needNotice = true;
  793. }
  794. }
  795. $ids = array_column($arr, 'id');
  796. $ids = array_unique(array_filter($ids));
  797. if (empty($ids)) {
  798. util::fail('请选择花材');
  799. }
  800. $sjId = $shop->sjId ?? 0;
  801. $chainShopList = [];
  802. if ($default == 1) {
  803. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId, 'join' => 0], null, '*', null, true);
  804. }
  805. $productList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id', true);
  806. // ==================== 【性能优化1】提前收集所有 mainId 和 itemId ====================
  807. $ptItemIds = [];
  808. foreach ($productList as $productInfo) {
  809. if (!empty($productInfo->itemId)) {
  810. $ptItemIds[] = (int)$productInfo->itemId;
  811. }
  812. }
  813. $ptItemIds = array_unique($ptItemIds);
  814. $mainIds = [(int)$this->mainId];
  815. if ($default == 1 && isset($shop->dataSync) && $shop->dataSync == 1) {
  816. foreach ($chainShopList as $chain) {
  817. if ($chain->id != $shop->id && !empty($chain->mainId)) {
  818. $mainIds[] = (int)$chain->mainId;
  819. }
  820. }
  821. }
  822. $mainIds = array_unique($mainIds);
  823. // ==================== 【性能优化2】一次性查出总店及直营分店的所有相关商品记录 ====================
  824. $productMap = []; // 存储结构:$productMap[mainId][itemId] = product_model
  825. if (!empty($ptItemIds) && !empty($mainIds)) {
  826. $allDbProducts = ProductClass::getAllByCondition([
  827. 'itemId' => ['in', $ptItemIds],
  828. 'mainId' => ['in', $mainIds],
  829. 'delStatus' => 0
  830. ], null, '*', 'id', true);
  831. foreach ($allDbProducts as $p) {
  832. $productMap[(int)$p->mainId][(int)$p->itemId] = $p;
  833. }
  834. }
  835. // ==================== 【性能优化3】外提并缓存直营分店的管理员信息,避免在循环内部重复 SELECT ====================
  836. $chainStaffMap = [];
  837. if ($default == 1 && isset($shop->dataSync) && $shop->dataSync == 1) {
  838. foreach ($chainShopList as $chain) {
  839. if ($chain->id == $shop->id) {
  840. continue;
  841. }
  842. $chainMainId = $chain->mainId ?? 0;
  843. $staff = ShopAdminClass::getByCondition(['mainId' => $chainMainId, 'adminId' => $adminId], true);
  844. $chainStaffMap[$chain->id] = $staff;
  845. }
  846. }
  847. $priceChangeRows = []; // 存储所有待批量写入的改价日志记录
  848. foreach ($arr as $product) {
  849. $productId = $product['id'];
  850. $productInfo = $productList[$productId] ?? [];
  851. if (empty($productInfo)) {
  852. util::fail('没有找到花材');
  853. }
  854. $productName = $productInfo->name ?? '';
  855. if (!isset($product['id'])) {
  856. util::fail('没有花材');
  857. }
  858. if (!isset($product['price']) || !is_numeric($product['price']) || $product['price'] < 0) {
  859. util::fail('请填写' . $productName . '的批发价');
  860. }
  861. $price = $product['price'];
  862. if (!isset($product['skPrice']) || !is_numeric($product['skPrice']) || $product['skPrice'] < 0) {
  863. util::fail('请填写' . $productName . '的零售价');
  864. }
  865. $skPrice = $product['skPrice'];
  866. if (!isset($product['hjPrice']) || !is_numeric($product['hjPrice']) || $product['hjPrice'] < 0) {
  867. util::fail('请填写' . $productName . '的会员价');
  868. }
  869. $hjPrice = $product['hjPrice'] ?? 0;
  870. if (!isset($productInfo->mainId) || $productInfo->mainId != $this->mainId) {
  871. util::fail('无法访问..');
  872. }
  873. if ($hjPrice > $price) {
  874. util::fail($productName . ' 批发价要大于会员价');
  875. }
  876. if ($price > $skPrice) {
  877. util::fail($productName . ' 零售价要大于批发价');
  878. }
  879. $ptItemId = (int)$productInfo->itemId;
  880. // ==================== 【性能优化4】内存更新商品价格,收集待批量插入的改价记录 ====================
  881. $mainShopProduct = $productMap[(int)$this->mainId][$ptItemId] ?? null;
  882. if (!empty($mainShopProduct)) {
  883. $mainShopProduct->price = $price;
  884. $mainShopProduct->skPrice = $skPrice;
  885. $mainShopProduct->hjPrice = $hjPrice;
  886. $mainShopProduct->save(false); // 避免多余的基础规则验证,提升存储性能
  887. $priceChangeRows[] = [
  888. 'ptStyle' => 2,
  889. 'sjId' => (int)$shop->sjId,
  890. 'mainId' => (int)$this->mainId,
  891. 'itemId' => (int)$mainShopProduct->id,
  892. 'ptItemId' => $ptItemId,
  893. 'price' => $price,
  894. 'skPrice' => $skPrice,
  895. 'hjPrice' => $hjPrice,
  896. 'staffId' => (int)$shopAdmin->id,
  897. 'staffName' => $shopAdmin->name ?? '',
  898. 'name' => $mainShopProduct->name ?? '',
  899. 'cover' => $mainShopProduct->cover ?? '',
  900. 'event' => ($changeType == 'cg' ? '采购后修改' : '修改'),
  901. 'type' => ($changeType == 'cg' ? 1 : 0),
  902. 'targetId' => (int)$targetId,
  903. 'addTime' => date('Y-m-d H:i:s'),
  904. ];
  905. }
  906. //在首店修改需要同步到所有直营店
  907. if ($default == 1 && isset($shop->dataSync) && $shop->dataSync == 1) {
  908. foreach ($chainShopList as $chain) {
  909. if ($chain->id == $shop->id) {
  910. continue;
  911. }
  912. $chainMainId = (int)($chain->mainId ?? 0);
  913. $staff = $chainStaffMap[$chain->id] ?? null;
  914. $staffId = $staff->id ?? 0;
  915. $staffName = $staff->name ?? '';
  916. $chainProduct = $productMap[$chainMainId][$ptItemId] ?? null;
  917. if (!empty($chainProduct)) {
  918. $chainProduct->price = $price;
  919. $chainProduct->skPrice = $skPrice;
  920. $chainProduct->hjPrice = $hjPrice;
  921. $chainProduct->save(false);
  922. $priceChangeRows[] = [
  923. 'ptStyle' => 2,
  924. 'sjId' => (int)$chain->sjId,
  925. 'mainId' => $chainMainId,
  926. 'itemId' => (int)$chainProduct->id,
  927. 'ptItemId' => $ptItemId,
  928. 'price' => $price,
  929. 'skPrice' => $skPrice,
  930. 'hjPrice' => $hjPrice,
  931. 'staffId' => (int)$staffId,
  932. 'staffName' => $staffName,
  933. 'name' => $chainProduct->name ?? '',
  934. 'cover' => $chainProduct->cover ?? '',
  935. 'event' => ($changeType == 'cg' ? '采购后修改' : '修改'),
  936. 'type' => ($changeType == 'cg' ? 1 : 0),
  937. 'targetId' => (int)$targetId,
  938. 'addTime' => date('Y-m-d H:i:s'),
  939. ];
  940. }
  941. }
  942. }
  943. }
  944. // ==================== 【性能优化5】大批量合并 INSERT 写入,避免循环体内频繁单条写入数据库 ====================
  945. if (!empty($priceChangeRows)) {
  946. $fieldNames = array_keys($priceChangeRows[0]);
  947. Yii::$app->db->createCommand()->batchInsert(
  948. 'xhGhsPriceChange',
  949. $fieldNames,
  950. $priceChangeRows
  951. )->execute();
  952. }
  953. $mainId = $shop->mainId ?? 0;
  954. if (!empty($mainId)) {
  955. //提醒零售收银台界面要刷新,多处要修改,关键词 modify_price_remind_cashier
  956. $staffList = ShopAdminClass::getAllByCondition(['mainId' => $mainId], null, '*', null, true);
  957. if (!empty($staffList)) {
  958. foreach ($staffList as $staff) {
  959. $staffId = $staff->id ?? 0;
  960. Yii::$app->redis->executeCommand('SET', ['cashier_' . $mainId . '_' . $staffId . '_may_refresh', 'yes']);
  961. Yii::$app->redis->executeCommand('SET', ['ghs_cashier_' . $mainId . '_' . $staffId . '_may_refresh', 'yes']);
  962. }
  963. }
  964. }
  965. $transaction->commit();
  966. //入库成功通知相关人员,关键词 put_in_notice
  967. if ($needNotice) {
  968. // $cg = PurchaseOrderClass::getLockById($targetId);
  969. // WxMessageClass::ghsCgEntryInform($shop, $cg);
  970. }
  971. util::complete('操作成功');
  972. } catch (\Exception $e) {
  973. $transaction->rollBack();
  974. $msg = $e->getMessage();
  975. Yii::info("修改失败原因:" . $msg);
  976. noticeUtil::push("修改价格出现失败情况,原因:" . $msg, '15280215347');
  977. util::fail('修改失败:' . $msg);
  978. }
  979. }
  980. //多个花材同时改成一个价 ssh 20231003
  981. public function actionManyItemChangeOnePrice()
  982. {
  983. $shop = $this->shop;
  984. $join = $shop->join ?? 0;
  985. $default = $shop->default ?? 0;
  986. if ($join == 0 && $default == 0) {
  987. util::fail('当前直营分店,只能回总店修改价格');
  988. }
  989. $shopAdmin = $this->shopAdmin;
  990. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  991. util::fail('超管才能修改');
  992. }
  993. $post = Yii::$app->request->post();
  994. $idsList = $post['ids'] ?? '';
  995. $ids = json_decode($idsList, true);
  996. if (empty($ids)) {
  997. util::fail('请选择花材');
  998. }
  999. $list = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', null, true);
  1000. if (empty($list)) {
  1001. util::fail('请选择花材哦');
  1002. }
  1003. $mainId = $this->mainId;
  1004. $adminId = $this->adminId ?? 0;
  1005. $shop = $this->shop;
  1006. $default = $shop->default ?? 0;
  1007. $shopAdmin = $this->shopAdmin;
  1008. $price = $post['price'] ?? 0;
  1009. if (is_numeric($price) == false) {
  1010. util::fail('批发价填写错误');
  1011. }
  1012. $skPrice = $post['skPrice'] ?? 0;
  1013. if (is_numeric($skPrice) == false) {
  1014. util::fail('散客价填写错误');
  1015. }
  1016. $hjPrice = $post['hjPrice'] ?? 0;
  1017. if (is_numeric($hjPrice) == false) {
  1018. util::fail('会员价填写错误');
  1019. }
  1020. if ($hjPrice > $price) {
  1021. util::fail('会员价不能大于批发价');
  1022. }
  1023. if ($price > $skPrice) {
  1024. util::fail('批发价不能大于零售价');
  1025. }
  1026. $sjId = $shop->sjId ?? 0;
  1027. $chainShopList = [];
  1028. if ($default == 1) {
  1029. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId, 'join' => 0], null, '*', null, true);
  1030. }
  1031. //normal常规改价,cg采购改价
  1032. $changeType = Yii::$app->request->post('changeType', 'normal');
  1033. $targetId = 0;
  1034. $changeParams = ['staffId' => $shopAdmin->id, 'staffName' => $shopAdmin->name, 'changeType' => $changeType, 'targetId' => $targetId];
  1035. $connection = Yii::$app->db;
  1036. $transaction = $connection->beginTransaction();
  1037. try {
  1038. foreach ($list as $product) {
  1039. if ($product->mainId != $mainId) {
  1040. util::fail('修改失败,不是你的花材');
  1041. }
  1042. $ptItemId = $product->itemId;
  1043. ProductClass::changeSinglePrice($shop, $ptItemId, $price, $skPrice, $hjPrice, $changeParams);
  1044. //在首店修改需要同步到所有直营店
  1045. if ($default == 1 && isset($shop->dataSync) && $shop->dataSync == 1) {
  1046. if (!empty($chainShopList)) {
  1047. foreach ($chainShopList as $chain) {
  1048. if ($chain->id == $shop->id) {
  1049. continue;
  1050. }
  1051. $chainMainId = $chain->mainId ?? 0;
  1052. $staff = ShopAdminClass::getByCondition(['mainId' => $chainMainId, 'adminId' => $adminId], true);
  1053. $changeParams2 = ['staffId' => $staff->id, 'staffName' => $staff->name, 'changeType' => $changeType, 'targetId' => $targetId];
  1054. ProductClass::changeSinglePrice($chain, $ptItemId, $price, $skPrice, $hjPrice, $changeParams2);
  1055. }
  1056. }
  1057. }
  1058. }
  1059. if (!empty($mainId)) {
  1060. //提醒零售收银台界面要刷新,多处要修改,关键词 modify_price_remind_cashier
  1061. $staffList = ShopAdminClass::getAllByCondition(['mainId' => $mainId], null, '*', null, true);
  1062. if (!empty($staffList)) {
  1063. foreach ($staffList as $staff) {
  1064. $staffId = $staff->id ?? 0;
  1065. Yii::$app->redis->executeCommand('SET', ['cashier_' . $mainId . '_' . $staffId . '_may_refresh', 'yes']);
  1066. Yii::$app->redis->executeCommand('SET', ['ghs_cashier_' . $mainId . '_' . $staffId . '_may_refresh', 'yes']);
  1067. }
  1068. }
  1069. }
  1070. $transaction->commit();
  1071. util::complete('修改成功');
  1072. } catch (\Exception $e) {
  1073. $transaction->rollBack();
  1074. Yii::info("修改失败原因:" . $e->getMessage());
  1075. util::fail('修改失败');
  1076. }
  1077. }
  1078. //改价
  1079. public function actionChangePrice()
  1080. {
  1081. $shopAdmin = $this->shopAdmin;
  1082. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  1083. util::fail('超管才能修改');
  1084. }
  1085. $productId = Yii::$app->request->post('productId', 0);
  1086. $productInfo = ProductClass::getById($productId, true);
  1087. if (empty($productInfo)) {
  1088. util::fail("花材不存在");
  1089. }
  1090. if ($productInfo->mainId != $this->mainId) {
  1091. util::fail('没有权限');
  1092. }
  1093. $price = Yii::$app->request->post('price', '');
  1094. if (is_numeric($price) && $price > 0) {
  1095. ProductClass::changeSinglePrice($productInfo, $price, ProductClass::PRICE_LABEL_CHANGE);
  1096. } else {
  1097. $price = bcadd($productInfo->cost, $productInfo->addPrice, 2);
  1098. ProductClass::changeSinglePrice($productInfo, $price, ProductClass::PRICE_LABEL_AUTO);
  1099. }
  1100. util::success(['price' => $price]);
  1101. }
  1102. //恢复自动调价
  1103. public function actionAutoPrice()
  1104. {
  1105. $productId = Yii::$app->request->post('productId', 0);
  1106. $productData = ProductClass::getById($productId);
  1107. if (!$productData) {
  1108. util::fail("花材不存在");
  1109. }
  1110. //2021.04.06改为用product中的成本价+加价
  1111. $price = bcadd($productData['cost'], $productData['addPrice'], 2);
  1112. ProductClass::changeSinglePrice($productId, $this->shopId, $price, ProductClass::PRICE_LABEL_AUTO);
  1113. util::complete("操作成功");
  1114. }
  1115. //修改加价
  1116. public function actionChangeAddPrice()
  1117. {
  1118. $productId = Yii::$app->request->post('productId', 0);
  1119. $addPrice = Yii::$app->request->post('addPrice', '');
  1120. if ($addPrice < 0) {
  1121. util::fail("加价不能小于0");
  1122. }
  1123. $productData = ProductClass::getProductData($productId, $this->mainId);
  1124. if (!$productData) {
  1125. util::fail("花材不存在");
  1126. }
  1127. ProductClass::changeAddPrice($productId, $addPrice);
  1128. util::complete();
  1129. }
  1130. //批量修改更多,包括排序、重量和保质期 ssh 20211211
  1131. public function actionBatchChangeMore()
  1132. {
  1133. $shop = $this->shop;
  1134. $default = $shop->default ?? 0;
  1135. $join = $shop->join ?? 0;
  1136. if ($default == 0 && $join == 0) {
  1137. util::fail('当前是直营店,请返回总店修改');
  1138. }
  1139. $shopAdmin = $this->shopAdmin;
  1140. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  1141. util::fail('超管才能操作');
  1142. }
  1143. $addData = Yii::$app->request->post('addData', '');
  1144. if (empty($addData)) {
  1145. util::fail('没有花材');
  1146. }
  1147. $itemData = json_decode($addData, true);
  1148. if (is_array($itemData) == false) {
  1149. util::fail('没有花材...');
  1150. }
  1151. $ids = array_column($itemData, 'id');
  1152. $infoList = ProductClass::getByIds($ids, null, 'id');
  1153. if (empty($infoList)) {
  1154. util::fail('没有花材');
  1155. }
  1156. foreach ($infoList as $info) {
  1157. if (isset($info['mainId']) == false || $info['mainId'] != $this->mainId) {
  1158. util::fail('不是你的花材不能修改');
  1159. }
  1160. }
  1161. //总店修改同步到所有门店
  1162. $sjId = $shop->sjId ?? 0;
  1163. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  1164. foreach ($itemData as $key => $data) {
  1165. $id = $data['id'] ?? 0;
  1166. unset($data['id']);
  1167. ProductClass::updateById($id, $data);
  1168. $ptItemId = $infoList[$id]['itemId'] ?? 0;
  1169. if (empty($ptItemId)) {
  1170. continue;
  1171. }
  1172. if (isset($shop->default) && $shop->default == 1 && isset($shop->dataSync) && $shop->dataSync == 1) {
  1173. foreach ($chainShopList as $chainShop) {
  1174. $currentMainId = $chainShop->mainId ?? 0;
  1175. if ($currentMainId == $shop->mainId) {
  1176. //前面已经修改过了,不需要重复修改
  1177. continue;
  1178. }
  1179. ProductClass::updateByCondition(['mainId' => $currentMainId, 'itemId' => $ptItemId], $data);
  1180. }
  1181. }
  1182. }
  1183. util::complete('修改成功');
  1184. }
  1185. //批量修改加价 ssh 20211211
  1186. public function actionBatchChangeAddPrice()
  1187. {
  1188. $shop = $this->shop;
  1189. $join = $shop->join ?? 0;
  1190. $default = $shop->default ?? 0;
  1191. if ($join == 0 && $default == 0) {
  1192. util::fail('当前直营分店,请切回总店操作');
  1193. }
  1194. $shopAdmin = $this->shopAdmin;
  1195. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  1196. util::fail('超管才能操作');
  1197. }
  1198. $post = Yii::$app->request->post();
  1199. $addData = $post['addData'] ?? '';
  1200. if (empty($addData)) {
  1201. util::fail('没有花材');
  1202. }
  1203. $itemData = json_decode($addData, true);
  1204. if (is_array($itemData) == false) {
  1205. util::fail('没有花材...');
  1206. }
  1207. $ids = array_column($itemData, 'id');
  1208. $infoList = ProductClass::getByIds($ids, null, 'id');
  1209. if (empty($infoList)) {
  1210. util::fail('没有花材');
  1211. }
  1212. foreach ($infoList as $info) {
  1213. if (isset($info['mainId']) == false || $info['mainId'] != $this->mainId) {
  1214. util::fail('不能修改别人的花材');
  1215. }
  1216. }
  1217. $appVersion = $post['appVersion'] ?? 0;
  1218. if ($appVersion != 1) {
  1219. util::fail('请先升级应用');
  1220. }
  1221. //总店修改同步到所有门店
  1222. $sjId = $shop->sjId ?? 0;
  1223. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  1224. foreach ($itemData as $key => $data) {
  1225. $id = $data['id'] ?? 0;
  1226. unset($data['id']);
  1227. ProductClass::updateById($id, $data);
  1228. $ptItemId = $infoList[$id]['itemId'] ?? 0;
  1229. if (empty($ptItemId)) {
  1230. continue;
  1231. }
  1232. if (isset($shop->default) && $shop->default == 1 && isset($shop->dataSync) && $shop->dataSync == 1) {
  1233. foreach ($chainShopList as $chainShop) {
  1234. if (isset($chainShop->join) && $chainShop->join == 1) {
  1235. continue;
  1236. }
  1237. $currentMainId = $chainShop->mainId ?? 0;
  1238. if ($currentMainId == $shop->mainId) {
  1239. //前面已经修改过了,不需要重复修改
  1240. continue;
  1241. }
  1242. ProductClass::updateByCondition(['mainId' => $currentMainId, 'itemId' => $ptItemId], $data);
  1243. }
  1244. }
  1245. }
  1246. util::complete('修改成功');
  1247. }
  1248. public function actionUpdate()
  1249. {
  1250. $shopAdmin = $this->shopAdmin;
  1251. if (!isset($shopAdmin->super) || $shopAdmin->super != 1) {
  1252. util::fail('超管才能操作');
  1253. }
  1254. $shop = $this->shop;
  1255. $join = $shop->join ?? 0;
  1256. $default = $shop->default ?? 0;
  1257. if ($join == 0 && $default == 0) {
  1258. //因为盘点不能将花材盘成0,所以这边放出入口允许创始人在分店修改分店的花材库存
  1259. if ($shopAdmin->super != 1) {
  1260. util::fail('请先开通超管权限');
  1261. }
  1262. }
  1263. $deadline = $shop->deadline;
  1264. $deadTime = strtotime($deadline);
  1265. if (time() > $deadTime) {
  1266. util::fail('系统已到期,编号1689');
  1267. }
  1268. $post = Yii::$app->request->post();
  1269. $discountPrice = $post['discountPrice'] ?? 0;
  1270. if ($discountPrice > 0) {
  1271. if (empty($post['hjDiscountPrice'])) {
  1272. $addPrice = $post['addPrice'] ?? 0;
  1273. $hjAddPrice = $post['hjAddPrice'] ?? 0;
  1274. $skMore = $post['skMore'] ?? 0;
  1275. $diff = bcsub($addPrice, $hjAddPrice, 2);
  1276. if ($diff > 0 && $discountPrice > $diff) {
  1277. $hjDiscountPrice = bcsub($discountPrice, $diff, 2);
  1278. } else {
  1279. $hjDiscountPrice = $discountPrice;
  1280. }
  1281. $diff = bcsub($skMore, $addPrice, 2);
  1282. if ($diff > 0) {
  1283. $skDiscountPrice = bcadd($diff, $discountPrice, 2);
  1284. } else {
  1285. $skDiscountPrice = $discountPrice;
  1286. }
  1287. $post['skDiscountPrice'] = $skDiscountPrice;
  1288. $post['hjDiscountPrice'] = $hjDiscountPrice;
  1289. }
  1290. $reachNum = $post['reachNum'] ?? 0;
  1291. $reachNumDiscount = $post['reachNumDiscount'] ?? 0;
  1292. if ($reachNum > 0 && $reachNumDiscount > 0) {
  1293. util::fail('特价和满减不能同时进行');
  1294. }
  1295. }
  1296. $post['adminId'] = $this->adminId;
  1297. $post['staffId'] = $this->shopAdmin->id;
  1298. $post['staffName'] = $this->shopAdmin->name;
  1299. // 图片列表数组转换为字符串
  1300. if (is_array($post['images'])) {
  1301. $post['images'] = implode(',', $post['images']);
  1302. }
  1303. //如果开启限购则必须升级应用
  1304. $limitBuy = $post['limitBuy'] ?? 0;
  1305. if ($limitBuy > 0) {
  1306. $appVersion = $post['appVersion'] ?? 0;
  1307. if ($appVersion < 2) {
  1308. util::fail('请先升级,才能使用限购');
  1309. }
  1310. }
  1311. $connection = Yii::$app->db;
  1312. $transaction = $connection->beginTransaction();
  1313. try {
  1314. ProductClass::updateProduct($post, $this->shop, 'ghs');
  1315. $transaction->commit();
  1316. util::complete();
  1317. } catch (\Exception $e) {
  1318. Yii::info("操作失败:" . $e->getMessage());
  1319. $transaction->rollBack();
  1320. util::fail('操作失败');
  1321. }
  1322. }
  1323. //上下架状态控制 ssh 20210713
  1324. public function actionStatusUpdate()
  1325. {
  1326. $post = Yii::$app->request->post();
  1327. $shopAdmin = $this->shopAdmin;
  1328. $roleId = $shopAdmin['roleId'] ?? 0;
  1329. $role = AdminRoleClass::getById($roleId);
  1330. $roleName = $role['roleName'] ?? '';
  1331. if ($roleName == '员工') {
  1332. util::fail('无法操作哦');
  1333. }
  1334. $id = $post['id'] ?? 0;
  1335. $status = $post['status'] ?? 1;
  1336. $product = ProductClass::getById($id, true);
  1337. if ($product->mainId != $this->mainId) {
  1338. util::fail('无法操作');
  1339. }
  1340. $product->status = $status;
  1341. if ($status == 2) {
  1342. $product->discountPrice = 0;
  1343. }
  1344. $product->save();
  1345. util::complete();
  1346. }
  1347. //花材展示状态控制 ssh 20231113
  1348. public function actionDelStatusUpdate()
  1349. {
  1350. $post = Yii::$app->request->post();
  1351. $shop = $this->shop;
  1352. $join = $shop->join ?? 0;
  1353. $default = $shop->default ?? 0;
  1354. if ($join == 0 && $default == 0) {
  1355. util::fail('请在首店操作');
  1356. }
  1357. $id = $post['id'] ?? 0;
  1358. $delStatus = $post['delStatus'] ?? 0;
  1359. $product = ProductClass::getById($id, true);
  1360. if ($product->mainId != $this->mainId) {
  1361. util::fail('不是你的花材哦!!');
  1362. }
  1363. //扫码收款、直接收款和特价商品,不可删除,多处要同步修改 system_item
  1364. if (getenv('YII_ENV') == 'production') {
  1365. $systemItem = [78076, 80960, 81004];
  1366. } else {
  1367. $systemItem = [11157, 11156, 11158];
  1368. }
  1369. if (in_array($product->itemId, $systemItem)) {
  1370. util::fail('系统使用花材,不可删除');
  1371. }
  1372. $product->delStatus = $delStatus;
  1373. if ($delStatus == 1) {
  1374. $product->stock = 0;
  1375. $product->status = 2;
  1376. }
  1377. $product->save();
  1378. $ptItemId = $product->itemId ?? 0;
  1379. if (isset($shop->default) && $shop->default == 1 && isset($shop->dataSync) && $shop->dataSync == 1) {
  1380. //总店修改同步到所有门店
  1381. $sjId = $shop->sjId ?? 0;
  1382. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  1383. foreach ($chainShopList as $chainShop) {
  1384. $chainShopId = $chainShop->id ?? 0;
  1385. if (isset($chainShop->join) && $chainShop->join == 1) {
  1386. continue;
  1387. }
  1388. if ($chainShopId == $shop->id) {
  1389. continue;
  1390. }
  1391. $chainMainId = $chainShop->mainId ?? 0;
  1392. $chainProduct = ProductClass::getByCondition(['mainId' => $chainMainId, 'itemId' => $ptItemId], true);
  1393. if (!empty($chainProduct)) {
  1394. $chainProduct->delStatus = $delStatus;
  1395. if ($delStatus == 1) {
  1396. $chainProduct->stock = 0;
  1397. $chainProduct->status = 2;
  1398. }
  1399. $chainProduct->save();
  1400. }
  1401. }
  1402. }
  1403. util::complete();
  1404. }
  1405. //详情 shish 20240115
  1406. public function actionDetail()
  1407. {
  1408. $id = Yii::$app->request->get('id', 0);
  1409. $source = Yii::$app->request->get('source', '');
  1410. $respond = ProductClass::getItemInfo($id);
  1411. if (empty($respond) || !is_array($respond)) {
  1412. util::fail('没有找到花材');
  1413. }
  1414. $limitBuyClearInfo = ProductClass::getLimitBuyClearInfo($id);
  1415. if (!is_array($limitBuyClearInfo)) {
  1416. $limitBuyClearInfo = [];
  1417. }
  1418. // foreach ($limitBuyClearInfo as $key => $value) {
  1419. // $respond[$key] = $value;
  1420. // }
  1421. $respond['limitBuyClearInfo'] = $limitBuyClearInfo;
  1422. $ptItemId = $respond['itemId'] ?? 0;
  1423. // 封面图与多花材图片的兼容处理
  1424. if ($respond['images'] == '' && $respond['shortCover'] != '') {
  1425. $respond['images'] = $respond['shortCover'];
  1426. }
  1427. $ptItemInfo = PtItemClass::getById($ptItemId);
  1428. $respond['ptItemInfo'] = $ptItemInfo;
  1429. //通知提醒抹除
  1430. if ($source == 'notify' && $respond['mainId'] == $this->mainId) {
  1431. $staff = $this->shopAdmin;
  1432. $staffId = $this->shopAdminId;
  1433. $notify = NotifyClass::getByCondition(['staffId' => $staffId, 'type' => 1, 'targetId' => $id], true);
  1434. if (!empty($notify)) {
  1435. if ($notify->read == 0) {
  1436. $notify->read = 1;
  1437. $notify->save();
  1438. $notifyNum = $staff->notifyNum;
  1439. if ($notifyNum > 0) {
  1440. $notifyNum--;
  1441. $staff->notifyNum = $notifyNum;
  1442. $staff->save();
  1443. }
  1444. }
  1445. }
  1446. }
  1447. //重复花材
  1448. $repeatList = ProductClass::getAllByCondition(['mainId' => $this->mainId, 'itemId' => $ptItemId, 'delStatus' => 0], null, '*', null, true);
  1449. $repeatId = 0;
  1450. if (!empty($repeatList)) {
  1451. foreach ($repeatList as $repeat) {
  1452. if ($repeat->id != $id) {
  1453. $repeatId = $repeat->id;
  1454. }
  1455. }
  1456. }
  1457. $respond['repeatId'] = $repeatId;
  1458. $cpItemList = CpItemClass::getAllByCondition(['productId' => $id], null, '*', 'cpId');
  1459. $cpInfo = [];
  1460. if (!empty($cpItemList)) {
  1461. $cpIds = array_column($cpItemList, 'cpId');
  1462. $cpList = CpClass::getByIds($cpIds);
  1463. if (!empty($cpList)) {
  1464. foreach ($cpList as $key => $val) {
  1465. $id = $val['id'] ?? 0;
  1466. $name = $val['name'] ?? '';
  1467. $cpInfo[] = ['cpId' => $id, 'cpName' => $name];
  1468. }
  1469. }
  1470. }
  1471. //判断这个花材能否被修改单位比和重量,同步参考ShopController actionMyShop方法,关键词has_right_shop_list
  1472. //判断当前登录的人,在自己有财务权限的main里面,是否包括了这个花材所属的平台id,在这个平台id下所有花材的main
  1473. $adminId = $this->adminId;
  1474. $hasRightRespond = ShopAdminClass::changeWeightRight($adminId, $ptItemId);
  1475. $respond['hasRightChange'] = $hasRightRespond['hasRightChange'];
  1476. $respond['hasRightChangeReason'] = $hasRightRespond['reason'];
  1477. //查出拆散的子花材
  1478. $part = UnitChangeClass::getByCondition(['big' => $id], true);
  1479. $partAcceptItem = 0;
  1480. $partAcceptItemName = '';
  1481. if (!empty($part)) {
  1482. $partAcceptItem = $part->small;
  1483. $small = ProductClass::getByCondition(['id' => $partAcceptItem], true, false, 'id,name');
  1484. $partAcceptItemName = $small->name ?? '';;
  1485. }
  1486. $respond['partAcceptItem'] = $partAcceptItem;
  1487. $respond['partAcceptItemName'] = $partAcceptItemName;
  1488. $respond['cpInfo'] = $cpInfo;
  1489. util::success($respond);
  1490. }
  1491. //新增花材 2021.6.21
  1492. public function actionAdd()
  1493. {
  1494. $post = Yii::$app->request->post();
  1495. $discountPrice = $post['discountPrice'] ?? 0;
  1496. if ($discountPrice > 0) {
  1497. if (isset($post['hjDiscountPrice']) == false || empty($post['hjDiscountPrice'])) {
  1498. $addPrice = $post['addPrice'] ?? 0;
  1499. $hjAddPrice = $post['hjAddPrice'] ?? 0;
  1500. $skMore = $post['skMore'] ?? 0;
  1501. $diff = bcsub($addPrice, $hjAddPrice, 2);
  1502. if ($diff > 0 && $discountPrice > $diff) {
  1503. $hjDiscountPrice = bcsub($discountPrice, $diff, 2);
  1504. } else {
  1505. $hjDiscountPrice = $discountPrice;
  1506. }
  1507. $diff = bcsub($skMore, $addPrice, 2);
  1508. if ($diff > 0) {
  1509. $skDiscountPrice = bcadd($diff, $discountPrice, 2);
  1510. } else {
  1511. $skDiscountPrice = $discountPrice;
  1512. }
  1513. $post['skDiscountPrice'] = $skDiscountPrice;
  1514. $post['hjDiscountPrice'] = $hjDiscountPrice;
  1515. }
  1516. }
  1517. $shop = $this->shop;
  1518. $join = $shop->join ?? 0;
  1519. $default = $shop->default ?? 0;
  1520. if ($join == 0 && $default == 0) {
  1521. util::fail('当前直营分店,请切回总店添加花材');
  1522. }
  1523. $shopAdmin = $this->shopAdmin;
  1524. if (!isset($shopAdmin->super) || $shopAdmin->super != 1) {
  1525. util::fail('管理员才能添加花材');
  1526. }
  1527. $post['sjId'] = $this->sjId;
  1528. $post['adminId'] = $this->adminId;
  1529. $post['mainId'] = $this->mainId;
  1530. $price = $post['price'] ?? 0;
  1531. $addPrice = $post['addPrice'] ?? 0;
  1532. if ($addPrice > $price) {
  1533. //客户添加花材时,经常会出现加价大于售价的怦,这个时候就直接把加价全部设置为0
  1534. $post['addPrice'] = 0;
  1535. $post['hjAddPrice'] = 0;
  1536. $post['skMore'] = 0;
  1537. //util::fail('加价不能大于售价');
  1538. }
  1539. $weight = isset($post['weight']) && is_numeric($post['weight']) ? $post['weight'] : 0;
  1540. if ($weight <= 0) {
  1541. util::fail('请填写重量,最小0.1');
  1542. }
  1543. $post['staffName'] = $shopAdmin->name ?? '';
  1544. // 图片列表数组转换为字符串
  1545. if (is_array($post['images'])) {
  1546. $post['images'] = implode(',', $post['images']);
  1547. }
  1548. ProductClass::addProduct($post, $this->shop);
  1549. util::complete('添加成功');
  1550. }
  1551. //获取拼音缩写 ssh 20210707
  1552. public function actionPy()
  1553. {
  1554. $name = Yii::$app->request->get('name');
  1555. $py = stringUtil::py($name);
  1556. util::success(['py' => $py]);
  1557. }
  1558. //(客户端-批发商)复制花材
  1559. public function actionClone()
  1560. {
  1561. $post = Yii::$app->request->post();
  1562. $discountPrice = $post['discountPrice'] ?? 0;
  1563. if ($discountPrice > 0) {
  1564. if (isset($post['hjDiscountPrice']) == false || empty($post['hjDiscountPrice'])) {
  1565. $addPrice = $post['addPrice'] ?? 0;
  1566. $hjAddPrice = $post['hjAddPrice'] ?? 0;
  1567. $skMore = $post['skMore'] ?? 0;
  1568. $diff = bcsub($addPrice, $hjAddPrice, 2);
  1569. if ($diff > 0 && $discountPrice > $diff) {
  1570. $hjDiscountPrice = bcsub($discountPrice, $diff, 2);
  1571. } else {
  1572. $hjDiscountPrice = $discountPrice;
  1573. }
  1574. $diff = bcsub($skMore, $addPrice, 2);
  1575. if ($diff > 0) {
  1576. $skDiscountPrice = bcadd($diff, $discountPrice, 2);
  1577. } else {
  1578. $skDiscountPrice = $discountPrice;
  1579. }
  1580. $post['skDiscountPrice'] = $skDiscountPrice;
  1581. $post['hjDiscountPrice'] = $hjDiscountPrice;
  1582. }
  1583. }
  1584. $shop = $this->shop;
  1585. $join = $shop->join ?? 0;
  1586. $default = $shop->default ?? 0;
  1587. if ($join == 0 && $default == 0) {
  1588. util::fail('请在首店操作');
  1589. }
  1590. $shopAdmin = $this->shopAdmin;
  1591. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  1592. util::fail('管理员才能添加花材');
  1593. }
  1594. $post['sjId'] = $this->sjId;
  1595. $post['adminId'] = $this->adminId;
  1596. $post['mainId'] = $this->mainId;
  1597. $post['staffName'] = $shopAdmin->name ?? '';
  1598. $price = $post['price'] ?? 0;
  1599. if (empty($price) || $price <= 0) {
  1600. util::fail('请填写批发价');
  1601. }
  1602. $skPrice = $post['skPrice'] ?? 0;
  1603. if (empty($skPrice) || $skPrice <= 0) {
  1604. util::fail('请填写零售价');
  1605. }
  1606. $hjPrice = $post['hjPrice'] ?? 0;
  1607. if (empty($hjPrice) || $hjPrice <= 0) {
  1608. util::fail('请填写会员价');
  1609. }
  1610. $addPrice = $post['addPrice'] ?? 0;
  1611. if ($addPrice > $price) {
  1612. util::fail('批发价的加价不能大于售价');
  1613. }
  1614. // 图片列表数组转换为字符串
  1615. if (is_array($post['images'])) {
  1616. $post['images'] = implode(',', $post['images']);
  1617. }
  1618. //复制标识
  1619. $post['copy'] = 1;
  1620. //去除无用的字段
  1621. unset($post['viewNum']);
  1622. unset($post['actualSold']);
  1623. unset($post['itemId']);
  1624. unset($post['id']);
  1625. unset($post['delStatus']);
  1626. unset($post['variety']);
  1627. ProductClass::addProduct($post, $this->shop);
  1628. util::complete('复制成功');
  1629. }
  1630. //下载价格表 ssh 20210922
  1631. public function actionGeneratePriceTable()
  1632. {
  1633. $level = Yii::$app->request->get('level', 0);
  1634. $respond = ProductService::generatePriceTable($this->sjId, $level, $this->mainId);
  1635. $file = $respond['file'] ?? '';
  1636. $shortFile = $respond['shortFile'] ?? '';
  1637. $fileUrl = Yii::$app->params['ghsHost'] . $file;
  1638. util::success(['file' => $fileUrl, 'shortFile' => $shortFile]);
  1639. }
  1640. //下载有库存花材,不含虚拟库存 ssh 20241-28
  1641. public function actionGenerateStockTable()
  1642. {
  1643. $level = Yii::$app->request->get('level', 0);
  1644. $respond = ProductService::generateStockTable($this->sjId, $level, $this->mainId);
  1645. $file = $respond['file'] ?? '';
  1646. $shortFile = $respond['shortFile'] ?? '';
  1647. $fileUrl = Yii::$app->params['ghsHost'] . $file;
  1648. util::success(['file' => $fileUrl, 'shortFile' => $shortFile]);
  1649. }
  1650. public function actionGenerateItemTable()
  1651. {
  1652. $level = Yii::$app->request->get('level', 0);
  1653. $respond = ProductService::generateItemTable($this->sjId, $level, $this->mainId);
  1654. $file = $respond['file'] ?? '';
  1655. $shortFile = $respond['shortFile'] ?? '';
  1656. $fileUrl = Yii::$app->params['ghsHost'] . $file;
  1657. util::success(['file' => $fileUrl, 'shortFile' => $shortFile]);
  1658. }
  1659. //下载条形码 lqh 20211227
  1660. public function actionGenerateBarcodeTable()
  1661. {
  1662. $respond = ProductService::generateBarcodeTable($this->sjId, $this->shopId, $this->mainId);
  1663. $file = $respond['file'] ?? '';
  1664. $shortFile = $respond['shortFile'] ?? '';
  1665. $fileUrl = Yii::$app->params['ghsHost'] . $file;
  1666. util::success(['file' => $fileUrl, 'shortFile' => $shortFile]);
  1667. }
  1668. //批量修改花材的分类 ssh 20230405
  1669. public function actionBatchModifyClass()
  1670. {
  1671. $post = Yii::$app->request->post();
  1672. $shop = $this->shop;
  1673. $sjName = $shop->merchantName ?? '';
  1674. $shopName = $shop->shopName ?? '';
  1675. $sjShopName = $shop->shopName == '首店' ? $sjName : $sjName . ' ' . $shopName;
  1676. //noticeUtil::push($sjShopName . ' 批量移动花材分类', '15280215347');
  1677. $join = $shop->join ?? 0;
  1678. $default = $shop->default ?? 0;
  1679. if ($join == 0 && $default == 0) {
  1680. util::fail('当前直营分店,请切回总店操作');
  1681. }
  1682. $classId = $post['classId'] ?? 0;
  1683. $class = ItemClassClass::getById($classId, true);
  1684. if (empty($class) || $class->mainId != $this->mainId) {
  1685. util::fail('请选择您自己的分类');
  1686. }
  1687. $connection = Yii::$app->db;
  1688. $transaction = $connection->beginTransaction();
  1689. try {
  1690. $className = $class->name ?? '';
  1691. $idsList = $post['ids'] ?? '';
  1692. $ids = json_decode($idsList, true);
  1693. if (empty($ids)) {
  1694. util::fail('请选择花材');
  1695. }
  1696. $list = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', null, true);
  1697. if (empty($list)) {
  1698. util::fail('请选择花材');
  1699. }
  1700. $ptItemList = [];
  1701. foreach ($list as $item) {
  1702. $name = $item->name ?? '';
  1703. if ($item->mainId != $this->mainId) {
  1704. util::fail($name . '不是您的花材');
  1705. }
  1706. $item->classId = $classId;
  1707. $item->save();
  1708. $ptItemId = $item->itemId ?? 0;
  1709. $ptItemList[] = $ptItemId;
  1710. //如果有修改分类,则在多个订单表中同步修改,如需修改,多个地方要同步修改,请搜索关键词item_class_modify_sync
  1711. $itemClassKey = 'item_class_modify_sync';
  1712. Yii::$app->redis->executeCommand('LPUSH', [$itemClassKey, $item->id]);
  1713. }
  1714. $shop = $this->shop;
  1715. if (isset($shop->default) && $shop->default == 1 && isset($shop->dataSync) && $shop->dataSync == 1) {
  1716. //总店修改同步到所有门店
  1717. $sjId = $shop->sjId ?? 0;
  1718. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  1719. foreach ($chainShopList as $chainShop) {
  1720. $chainShopId = $chainShop->id ?? 0;
  1721. if ($chainShopId == $shop->id) {
  1722. continue;
  1723. }
  1724. $chainManId = $chainShop->mainId ?? 0;
  1725. $chainAllClassList = ItemClassClass::getAllByCondition(['mainId' => $chainManId], null, '*', null, true);
  1726. if (!empty($chainAllClassList)) {
  1727. foreach ($chainAllClassList as $chainClass) {
  1728. $chainName = $chainClass->name ?? '';
  1729. $chainClassId = $chainClass->id ?? 0;
  1730. if ($chainName == $className) {
  1731. $chainProductList = Productclass::getAllByCondition(['mainId' => $chainManId, 'itemId' => ['in', $ptItemList]], null, '*', null, true);
  1732. if (!empty($chainProductList)) {
  1733. foreach ($chainProductList as $currentProduct) {
  1734. $currentProduct->classId = $chainClassId;
  1735. $currentProduct->save();
  1736. //如果有修改分类,则在多个订单表中同步修改,如需修改,多个地方要同步修改,请搜索关键词item_class_modify_sync
  1737. $itemClassKey = 'item_class_modify_sync';
  1738. Yii::$app->redis->executeCommand('LPUSH', [$itemClassKey, $currentProduct->id]);
  1739. }
  1740. }
  1741. }
  1742. }
  1743. }
  1744. }
  1745. }
  1746. $transaction->commit();
  1747. util::complete('修改成功');
  1748. } catch (\Exception $e) {
  1749. $transaction->rollBack();
  1750. Yii::info("修改失败原因:" . $e->getMessage());
  1751. util::fail('修改失败');
  1752. }
  1753. }
  1754. //批量修改采购人 ssh 20240609
  1755. public function actionBatchModifyCgStaff()
  1756. {
  1757. $post = Yii::$app->request->post();
  1758. $staffId = $post['staffId'] ?? 0;
  1759. $staff = ShopAdminClass::getById($staffId, true);
  1760. if (empty($staff)) {
  1761. util::fail('没有找到这个员工');
  1762. }
  1763. if ($staff->mainId != $this->mainId) {
  1764. util::fail('不是你的员工');
  1765. }
  1766. $staffName = $staff->name ?? '';
  1767. $connection = Yii::$app->db;
  1768. $transaction = $connection->beginTransaction();
  1769. try {
  1770. $idsList = $post['ids'] ?? '';
  1771. $ids = json_decode($idsList, true);
  1772. if (empty($ids)) {
  1773. util::fail('请选择花材');
  1774. }
  1775. $list = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', null, true);
  1776. if (empty($list)) {
  1777. util::fail('请选择花材');
  1778. }
  1779. $shop = $this->shop;
  1780. $bookSn = $shop->bookSn ?? 0;
  1781. foreach ($list as $item) {
  1782. $name = $item->name ?? '';
  1783. if ($item->mainId != $this->mainId) {
  1784. util::fail($name . '不是您的花材');
  1785. }
  1786. $item->cgStaffId = $staffId;
  1787. $item->cgStaffName = $staffName;
  1788. $item->save();
  1789. $itemId = $item->id ?? 0;
  1790. if (!empty($bookSn)) {
  1791. //预订管理的花材需要变化
  1792. BookItemClass::updateByCondition(['bookSn' => $bookSn, 'itemId' => $itemId], ['cgStaffId' => $staffId, 'cgStaffName' => $staffName]);
  1793. }
  1794. }
  1795. $transaction->commit();
  1796. util::complete('修改成功');
  1797. } catch (\Exception $e) {
  1798. $transaction->rollBack();
  1799. Yii::info("修改失败原因:" . $e->getMessage());
  1800. util::fail('修改失败');
  1801. }
  1802. }
  1803. //修改属于成本还是收入选项 ssh 20240514
  1804. public function actionChangeBelongCost()
  1805. {
  1806. $get = Yii::$app->request->get();
  1807. $id = $get['id'] ?? 0;
  1808. $belongCost = $get['belongCost'] ?? 0;
  1809. $info = ProductClass::getById($id, true);
  1810. if (empty($info)) {
  1811. util::fail('没有找到花材');
  1812. }
  1813. if ($info->mainId != $this->mainId) {
  1814. util::fail('不是你的花材哦');
  1815. }
  1816. $info->belongCost = $belongCost;
  1817. $info->save();
  1818. //队列方式去处理
  1819. $redisKey = 'change_belong_cost_item_list';
  1820. $value = $id . '_' . $belongCost;
  1821. Yii::$app->redis->executeCommand('LPUSH', [$redisKey, $value]);
  1822. util::complete('修改成功');
  1823. }
  1824. //修改拆散接收的花材 ssh 20251124
  1825. public function actionModifyPartAcceptItem()
  1826. {
  1827. $get = Yii::$app->request->get();
  1828. $smallId = $get['small'] ?? 0;
  1829. $bigId = $get['big'] ?? 0;
  1830. $small = ProductClass::getById($smallId, true);
  1831. if (empty($small)) {
  1832. util::fail('子花材没找到');
  1833. }
  1834. $smallName = $small->name ?? '';
  1835. if ($small->mainId != $this->mainId) {
  1836. util::fail($smallName . '不是你的花材');
  1837. }
  1838. if ($small->ratio != 1) {
  1839. util::fail($smallName . '不是1支装的');
  1840. }
  1841. $big = ProductClass::getById($bigId, true);
  1842. if (empty($big)) {
  1843. util::fail('没有找到花材');
  1844. }
  1845. if ($big->mainId != $this->mainId) {
  1846. util::fail('不是你的花材呢');
  1847. }
  1848. $change = UnitChangeClass::getByCondition(['big' => $bigId], true);
  1849. if (empty($change)) {
  1850. UnitChangeClass::add(['big' => $bigId, 'small' => $smallId], true);
  1851. } else {
  1852. $change->small = $smallId;
  1853. $change->save();
  1854. }
  1855. util::complete('修改成功');
  1856. }
  1857. }