ProductController.php 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896
  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. public function actionBatchChangePrice()
  724. {
  725. $shop = $this->shop;
  726. $join = $shop->join ?? 0;
  727. $default = $shop->default ?? 0;
  728. if ($join == 0 && $default == 0) {
  729. //除了小蔡鲜花的蔡总、郑总、苏南3007,其他人直营分店不能改价格,多处要同步修改,关键词 direct_price_change
  730. if (!in_array($this->adminId, [35725, 23659, 29264])) {
  731. util::fail('直营店不能改价');
  732. }
  733. }
  734. $shopAdmin = $this->shopAdmin;
  735. if (!isset($shopAdmin->super) || $shopAdmin->super != 1) {
  736. util::fail('超管才能修改');
  737. }
  738. $shop = $this->shop;
  739. $default = $shop->default ?? 0;
  740. $adminId = $this->adminId ?? 0;
  741. //normal常规改价,cg采购改价
  742. $changeType = Yii::$app->request->post('changeType', 'normal');
  743. $targetId = Yii::$app->request->post('targetId', 0);
  744. $changeParams = ['staffId' => $shopAdmin->id, 'staffName' => $shopAdmin->name, 'changeType' => $changeType, 'targetId' => $targetId];
  745. $post = Yii::$app->request->post();
  746. $data = $post['data'] ?? '';
  747. if (empty($data)) {
  748. util::fail('没有修改');
  749. }
  750. $arr = json_decode($data, true);
  751. if (empty($arr)) {
  752. util::fail('没有修改哦');
  753. }
  754. $appVersion = $post['appVersion'] ?? 0;
  755. if ($appVersion != 1) {
  756. util::fail('请先升级应用');
  757. }
  758. //解决重复请求问题
  759. $cacheKey = 'batch_change_price_' . $targetId;
  760. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  761. if (!empty($has)) {
  762. util::fail('请5秒后操作');
  763. }
  764. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 5, 'has']);
  765. $connection = Yii::$app->db;
  766. $transaction = $connection->beginTransaction();
  767. try {
  768. $needNotice = false;
  769. $cg = PurchaseOrderClass::getLockById($targetId);
  770. if (!empty($cg)) {
  771. if (isset($cg->mainId) == false || $cg->mainId != $this->mainId) {
  772. util::fail('不是您的采购单');
  773. }
  774. //批量改价同时入库
  775. if ($cg->status == PurchaseOrderClass::PURCHASE_ORDER_STATUS_SENDING) {
  776. $staff = $this->shopAdmin;
  777. $data = [];
  778. $data['staffId'] = $staff->id ?? 0;
  779. $data['staffName'] = $staff->name ?? '';
  780. $data['adminId'] = $this->adminId ?? 0;
  781. $data['shop'] = $this->shop;
  782. PurchaseOrderClass::putIn($cg, $data);
  783. $needNotice = true;
  784. }
  785. }
  786. $ids = array_column($arr, 'id');
  787. $ids = array_unique(array_filter($ids));
  788. if (empty($ids)) {
  789. util::fail('请选择花材');
  790. }
  791. $sjId = $shop->sjId ?? 0;
  792. $chainShopList = [];
  793. if ($default == 1) {
  794. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId, 'join' => 0], null, '*', null, true);
  795. }
  796. $productList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id', true);
  797. foreach ($arr as $product) {
  798. $productId = $product['id'];
  799. $productInfo = $productList[$productId] ?? [];
  800. if (empty($productInfo)) {
  801. util::fail('没有找到花材');
  802. }
  803. $productName = $productInfo->name ?? '';
  804. if (!isset($product['id'])) {
  805. util::fail('没有花材');
  806. }
  807. if (!isset($product['price']) || !is_numeric($product['price']) || $product['price'] < 0) {
  808. util::fail('请填写' . $productName . '的批发价');
  809. }
  810. $price = $product['price'];
  811. if (!isset($product['skPrice']) || !is_numeric($product['skPrice']) || $product['skPrice'] < 0) {
  812. util::fail('请填写' . $productName . '的零售价');
  813. }
  814. $skPrice = $product['skPrice'];
  815. if (!isset($product['hjPrice']) || !is_numeric($product['hjPrice']) || $product['hjPrice'] < 0) {
  816. util::fail('请填写' . $productName . '的会员价');
  817. }
  818. $hjPrice = $product['hjPrice'] ?? 0;
  819. if (!isset($productInfo->mainId) || $productInfo->mainId != $this->mainId) {
  820. util::fail('无法访问..');
  821. }
  822. if ($hjPrice > $price) {
  823. util::fail($productName . ' 批发价要大于会员价');
  824. }
  825. if ($price > $skPrice) {
  826. util::fail($productName . ' 零售价要大于批发价');
  827. }
  828. $ptItemId = $productInfo->itemId;
  829. ProductClass::changeSinglePrice($shop, $ptItemId, $price, $skPrice, $hjPrice, $changeParams);
  830. //在首店修改需要同步到所有直营店
  831. if ($default == 1 && isset($shop->dataSync) && $shop->dataSync == 1) {
  832. foreach ($chainShopList as $chain) {
  833. if ($chain->id == $shop->id) {
  834. continue;
  835. }
  836. $chainMainId = $chain->mainId ?? 0;
  837. $staff = ShopAdminClass::getByCondition(['mainId' => $chainMainId, 'adminId' => $adminId], true);
  838. $staffId = $staff->id ?? 0;
  839. $staffName = $staff->name ?? '';
  840. $changeParams2 = ['staffId' => $staffId, 'staffName' => $staffName, 'changeType' => $changeType, 'targetId' => $targetId];
  841. ProductClass::changeSinglePrice($chain, $ptItemId, $price, $skPrice, $hjPrice, $changeParams2);
  842. }
  843. }
  844. }
  845. $mainId = $shop->mainId ?? 0;
  846. if (!empty($mainId)) {
  847. //提醒零售收银台界面要刷新,多处要修改,关键词 modify_price_remind_cashier
  848. $staffList = ShopAdminClass::getAllByCondition(['mainId' => $mainId], null, '*', null, true);
  849. if (!empty($staffList)) {
  850. foreach ($staffList as $staff) {
  851. $staffId = $staff->id ?? 0;
  852. Yii::$app->redis->executeCommand('SET', ['cashier_' . $mainId . '_' . $staffId . '_may_refresh', 'yes']);
  853. Yii::$app->redis->executeCommand('SET', ['ghs_cashier_' . $mainId . '_' . $staffId . '_may_refresh', 'yes']);
  854. }
  855. }
  856. }
  857. $transaction->commit();
  858. //入库成功通知相关人员,关键词 put_in_notice
  859. if ($needNotice) {
  860. // $cg = PurchaseOrderClass::getLockById($targetId);
  861. // WxMessageClass::ghsCgEntryInform($shop, $cg);
  862. }
  863. util::complete('操作成功');
  864. } catch (\Exception $e) {
  865. $transaction->rollBack();
  866. $msg = $e->getMessage();
  867. Yii::info("修改失败原因:" . $msg);
  868. noticeUtil::push("修改价格出现失败情况,原因:" . $msg, '15280215347');
  869. util::fail('修改失败');
  870. }
  871. }
  872. //多个花材同时改成一个价 ssh 20231003
  873. public function actionManyItemChangeOnePrice()
  874. {
  875. $shop = $this->shop;
  876. $join = $shop->join ?? 0;
  877. $default = $shop->default ?? 0;
  878. if ($join == 0 && $default == 0) {
  879. util::fail('当前直营分店,只能回总店修改价格');
  880. }
  881. $shopAdmin = $this->shopAdmin;
  882. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  883. util::fail('超管才能修改');
  884. }
  885. $post = Yii::$app->request->post();
  886. $idsList = $post['ids'] ?? '';
  887. $ids = json_decode($idsList, true);
  888. if (empty($ids)) {
  889. util::fail('请选择花材');
  890. }
  891. $list = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', null, true);
  892. if (empty($list)) {
  893. util::fail('请选择花材哦');
  894. }
  895. $mainId = $this->mainId;
  896. $adminId = $this->adminId ?? 0;
  897. $shop = $this->shop;
  898. $default = $shop->default ?? 0;
  899. $shopAdmin = $this->shopAdmin;
  900. $price = $post['price'] ?? 0;
  901. if (is_numeric($price) == false) {
  902. util::fail('批发价填写错误');
  903. }
  904. $skPrice = $post['skPrice'] ?? 0;
  905. if (is_numeric($skPrice) == false) {
  906. util::fail('散客价填写错误');
  907. }
  908. $hjPrice = $post['hjPrice'] ?? 0;
  909. if (is_numeric($hjPrice) == false) {
  910. util::fail('会员价填写错误');
  911. }
  912. if ($hjPrice > $price) {
  913. util::fail('会员价不能大于批发价');
  914. }
  915. if ($price > $skPrice) {
  916. util::fail('批发价不能大于零售价');
  917. }
  918. $sjId = $shop->sjId ?? 0;
  919. $chainShopList = [];
  920. if ($default == 1) {
  921. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId, 'join' => 0], null, '*', null, true);
  922. }
  923. //normal常规改价,cg采购改价
  924. $changeType = Yii::$app->request->post('changeType', 'normal');
  925. $targetId = 0;
  926. $changeParams = ['staffId' => $shopAdmin->id, 'staffName' => $shopAdmin->name, 'changeType' => $changeType, 'targetId' => $targetId];
  927. $connection = Yii::$app->db;
  928. $transaction = $connection->beginTransaction();
  929. try {
  930. foreach ($list as $product) {
  931. if ($product->mainId != $mainId) {
  932. util::fail('修改失败,不是你的花材');
  933. }
  934. $ptItemId = $product->itemId;
  935. ProductClass::changeSinglePrice($shop, $ptItemId, $price, $skPrice, $hjPrice, $changeParams);
  936. //在首店修改需要同步到所有直营店
  937. if ($default == 1 && isset($shop->dataSync) && $shop->dataSync == 1) {
  938. if (!empty($chainShopList)) {
  939. foreach ($chainShopList as $chain) {
  940. if ($chain->id == $shop->id) {
  941. continue;
  942. }
  943. $chainMainId = $chain->mainId ?? 0;
  944. $staff = ShopAdminClass::getByCondition(['mainId' => $chainMainId, 'adminId' => $adminId], true);
  945. $changeParams2 = ['staffId' => $staff->id, 'staffName' => $staff->name, 'changeType' => $changeType, 'targetId' => $targetId];
  946. ProductClass::changeSinglePrice($chain, $ptItemId, $price, $skPrice, $hjPrice, $changeParams2);
  947. }
  948. }
  949. }
  950. }
  951. if (!empty($mainId)) {
  952. //提醒零售收银台界面要刷新,多处要修改,关键词 modify_price_remind_cashier
  953. $staffList = ShopAdminClass::getAllByCondition(['mainId' => $mainId], null, '*', null, true);
  954. if (!empty($staffList)) {
  955. foreach ($staffList as $staff) {
  956. $staffId = $staff->id ?? 0;
  957. Yii::$app->redis->executeCommand('SET', ['cashier_' . $mainId . '_' . $staffId . '_may_refresh', 'yes']);
  958. Yii::$app->redis->executeCommand('SET', ['ghs_cashier_' . $mainId . '_' . $staffId . '_may_refresh', 'yes']);
  959. }
  960. }
  961. }
  962. $transaction->commit();
  963. util::complete('修改成功');
  964. } catch (\Exception $e) {
  965. $transaction->rollBack();
  966. Yii::info("修改失败原因:" . $e->getMessage());
  967. util::fail('修改失败');
  968. }
  969. }
  970. //改价
  971. public function actionChangePrice()
  972. {
  973. $shopAdmin = $this->shopAdmin;
  974. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  975. util::fail('超管才能修改');
  976. }
  977. $productId = Yii::$app->request->post('productId', 0);
  978. $productInfo = ProductClass::getById($productId, true);
  979. if (empty($productInfo)) {
  980. util::fail("花材不存在");
  981. }
  982. if ($productInfo->mainId != $this->mainId) {
  983. util::fail('没有权限');
  984. }
  985. $price = Yii::$app->request->post('price', '');
  986. if (is_numeric($price) && $price > 0) {
  987. ProductClass::changeSinglePrice($productInfo, $price, ProductClass::PRICE_LABEL_CHANGE);
  988. } else {
  989. $price = bcadd($productInfo->cost, $productInfo->addPrice, 2);
  990. ProductClass::changeSinglePrice($productInfo, $price, ProductClass::PRICE_LABEL_AUTO);
  991. }
  992. util::success(['price' => $price]);
  993. }
  994. //恢复自动调价
  995. public function actionAutoPrice()
  996. {
  997. $productId = Yii::$app->request->post('productId', 0);
  998. $productData = ProductClass::getById($productId);
  999. if (!$productData) {
  1000. util::fail("花材不存在");
  1001. }
  1002. //2021.04.06改为用product中的成本价+加价
  1003. $price = bcadd($productData['cost'], $productData['addPrice'], 2);
  1004. ProductClass::changeSinglePrice($productId, $this->shopId, $price, ProductClass::PRICE_LABEL_AUTO);
  1005. util::complete("操作成功");
  1006. }
  1007. //修改加价
  1008. public function actionChangeAddPrice()
  1009. {
  1010. $productId = Yii::$app->request->post('productId', 0);
  1011. $addPrice = Yii::$app->request->post('addPrice', '');
  1012. if ($addPrice < 0) {
  1013. util::fail("加价不能小于0");
  1014. }
  1015. $productData = ProductClass::getProductData($productId, $this->mainId);
  1016. if (!$productData) {
  1017. util::fail("花材不存在");
  1018. }
  1019. ProductClass::changeAddPrice($productId, $addPrice);
  1020. util::complete();
  1021. }
  1022. //批量修改更多,包括排序、重量和保质期 ssh 20211211
  1023. public function actionBatchChangeMore()
  1024. {
  1025. $shop = $this->shop;
  1026. $default = $shop->default ?? 0;
  1027. $join = $shop->join ?? 0;
  1028. if ($default == 0 && $join == 0) {
  1029. util::fail('当前是直营店,请返回总店修改');
  1030. }
  1031. $shopAdmin = $this->shopAdmin;
  1032. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  1033. util::fail('超管才能操作');
  1034. }
  1035. $addData = Yii::$app->request->post('addData', '');
  1036. if (empty($addData)) {
  1037. util::fail('没有花材');
  1038. }
  1039. $itemData = json_decode($addData, true);
  1040. if (is_array($itemData) == false) {
  1041. util::fail('没有花材...');
  1042. }
  1043. $ids = array_column($itemData, 'id');
  1044. $infoList = ProductClass::getByIds($ids, null, 'id');
  1045. if (empty($infoList)) {
  1046. util::fail('没有花材');
  1047. }
  1048. foreach ($infoList as $info) {
  1049. if (isset($info['mainId']) == false || $info['mainId'] != $this->mainId) {
  1050. util::fail('不是你的花材不能修改');
  1051. }
  1052. }
  1053. //总店修改同步到所有门店
  1054. $sjId = $shop->sjId ?? 0;
  1055. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  1056. foreach ($itemData as $key => $data) {
  1057. $id = $data['id'] ?? 0;
  1058. unset($data['id']);
  1059. ProductClass::updateById($id, $data);
  1060. $ptItemId = $infoList[$id]['itemId'] ?? 0;
  1061. if (empty($ptItemId)) {
  1062. continue;
  1063. }
  1064. if (isset($shop->default) && $shop->default == 1 && isset($shop->dataSync) && $shop->dataSync == 1) {
  1065. foreach ($chainShopList as $chainShop) {
  1066. $currentMainId = $chainShop->mainId ?? 0;
  1067. if ($currentMainId == $shop->mainId) {
  1068. //前面已经修改过了,不需要重复修改
  1069. continue;
  1070. }
  1071. ProductClass::updateByCondition(['mainId' => $currentMainId, 'itemId' => $ptItemId], $data);
  1072. }
  1073. }
  1074. }
  1075. util::complete('修改成功');
  1076. }
  1077. //批量修改加价 ssh 20211211
  1078. public function actionBatchChangeAddPrice()
  1079. {
  1080. $shop = $this->shop;
  1081. $join = $shop->join ?? 0;
  1082. $default = $shop->default ?? 0;
  1083. if ($join == 0 && $default == 0) {
  1084. util::fail('当前直营分店,请切回总店操作');
  1085. }
  1086. $shopAdmin = $this->shopAdmin;
  1087. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  1088. util::fail('超管才能操作');
  1089. }
  1090. $post = Yii::$app->request->post();
  1091. $addData = $post['addData'] ?? '';
  1092. if (empty($addData)) {
  1093. util::fail('没有花材');
  1094. }
  1095. $itemData = json_decode($addData, true);
  1096. if (is_array($itemData) == false) {
  1097. util::fail('没有花材...');
  1098. }
  1099. $ids = array_column($itemData, 'id');
  1100. $infoList = ProductClass::getByIds($ids, null, 'id');
  1101. if (empty($infoList)) {
  1102. util::fail('没有花材');
  1103. }
  1104. foreach ($infoList as $info) {
  1105. if (isset($info['mainId']) == false || $info['mainId'] != $this->mainId) {
  1106. util::fail('不能修改别人的花材');
  1107. }
  1108. }
  1109. $appVersion = $post['appVersion'] ?? 0;
  1110. if ($appVersion != 1) {
  1111. util::fail('请先升级应用');
  1112. }
  1113. //总店修改同步到所有门店
  1114. $sjId = $shop->sjId ?? 0;
  1115. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  1116. foreach ($itemData as $key => $data) {
  1117. $id = $data['id'] ?? 0;
  1118. unset($data['id']);
  1119. ProductClass::updateById($id, $data);
  1120. $ptItemId = $infoList[$id]['itemId'] ?? 0;
  1121. if (empty($ptItemId)) {
  1122. continue;
  1123. }
  1124. if (isset($shop->default) && $shop->default == 1 && isset($shop->dataSync) && $shop->dataSync == 1) {
  1125. foreach ($chainShopList as $chainShop) {
  1126. if (isset($chainShop->join) && $chainShop->join == 1) {
  1127. continue;
  1128. }
  1129. $currentMainId = $chainShop->mainId ?? 0;
  1130. if ($currentMainId == $shop->mainId) {
  1131. //前面已经修改过了,不需要重复修改
  1132. continue;
  1133. }
  1134. ProductClass::updateByCondition(['mainId' => $currentMainId, 'itemId' => $ptItemId], $data);
  1135. }
  1136. }
  1137. }
  1138. util::complete('修改成功');
  1139. }
  1140. public function actionUpdate()
  1141. {
  1142. $shopAdmin = $this->shopAdmin;
  1143. if (!isset($shopAdmin->super) || $shopAdmin->super != 1) {
  1144. util::fail('超管才能操作');
  1145. }
  1146. $shop = $this->shop;
  1147. $join = $shop->join ?? 0;
  1148. $default = $shop->default ?? 0;
  1149. if ($join == 0 && $default == 0) {
  1150. //因为盘点不能将花材盘成0,所以这边放出入口允许创始人在分店修改分店的花材库存
  1151. if ($shopAdmin->super != 1) {
  1152. util::fail('请先开通超管权限');
  1153. }
  1154. }
  1155. $deadline = $shop->deadline;
  1156. $deadTime = strtotime($deadline);
  1157. if (time() > $deadTime) {
  1158. util::fail('系统已到期,编号1689');
  1159. }
  1160. $post = Yii::$app->request->post();
  1161. $discountPrice = $post['discountPrice'] ?? 0;
  1162. if ($discountPrice > 0) {
  1163. if (empty($post['hjDiscountPrice'])) {
  1164. $addPrice = $post['addPrice'] ?? 0;
  1165. $hjAddPrice = $post['hjAddPrice'] ?? 0;
  1166. $skMore = $post['skMore'] ?? 0;
  1167. $diff = bcsub($addPrice, $hjAddPrice, 2);
  1168. if ($diff > 0 && $discountPrice > $diff) {
  1169. $hjDiscountPrice = bcsub($discountPrice, $diff, 2);
  1170. } else {
  1171. $hjDiscountPrice = $discountPrice;
  1172. }
  1173. $diff = bcsub($skMore, $addPrice, 2);
  1174. if ($diff > 0) {
  1175. $skDiscountPrice = bcadd($diff, $discountPrice, 2);
  1176. } else {
  1177. $skDiscountPrice = $discountPrice;
  1178. }
  1179. $post['skDiscountPrice'] = $skDiscountPrice;
  1180. $post['hjDiscountPrice'] = $hjDiscountPrice;
  1181. }
  1182. $reachNum = $post['reachNum'] ?? 0;
  1183. $reachNumDiscount = $post['reachNumDiscount'] ?? 0;
  1184. if ($reachNum > 0 && $reachNumDiscount > 0) {
  1185. util::fail('特价和满减不能同时进行');
  1186. }
  1187. }
  1188. $post['adminId'] = $this->adminId;
  1189. $post['staffId'] = $this->shopAdmin->id;
  1190. $post['staffName'] = $this->shopAdmin->name;
  1191. // 图片列表数组转换为字符串
  1192. if (is_array($post['images'])) {
  1193. $post['images'] = implode(',', $post['images']);
  1194. }
  1195. //如果开启限购则必须升级应用
  1196. $limitBuy = $post['limitBuy'] ?? 0;
  1197. if ($limitBuy > 0) {
  1198. $appVersion = $post['appVersion'] ?? 0;
  1199. if ($appVersion < 2) {
  1200. util::fail('请先升级,才能使用限购');
  1201. }
  1202. }
  1203. $connection = Yii::$app->db;
  1204. $transaction = $connection->beginTransaction();
  1205. try {
  1206. ProductClass::updateProduct($post, $this->shop, 'ghs');
  1207. $transaction->commit();
  1208. util::complete();
  1209. } catch (\Exception $e) {
  1210. Yii::info("操作失败:" . $e->getMessage());
  1211. $transaction->rollBack();
  1212. util::fail('操作失败');
  1213. }
  1214. }
  1215. //上下架状态控制 ssh 20210713
  1216. public function actionStatusUpdate()
  1217. {
  1218. $post = Yii::$app->request->post();
  1219. $shopAdmin = $this->shopAdmin;
  1220. $roleId = $shopAdmin['roleId'] ?? 0;
  1221. $role = AdminRoleClass::getById($roleId);
  1222. $roleName = $role['roleName'] ?? '';
  1223. if ($roleName == '员工') {
  1224. util::fail('无法操作哦');
  1225. }
  1226. $id = $post['id'] ?? 0;
  1227. $status = $post['status'] ?? 1;
  1228. $product = ProductClass::getById($id, true);
  1229. if ($product->mainId != $this->mainId) {
  1230. util::fail('无法操作');
  1231. }
  1232. $product->status = $status;
  1233. if ($status == 2) {
  1234. $product->discountPrice = 0;
  1235. }
  1236. $product->save();
  1237. util::complete();
  1238. }
  1239. //花材展示状态控制 ssh 20231113
  1240. public function actionDelStatusUpdate()
  1241. {
  1242. $post = Yii::$app->request->post();
  1243. $shop = $this->shop;
  1244. $join = $shop->join ?? 0;
  1245. $default = $shop->default ?? 0;
  1246. if ($join == 0 && $default == 0) {
  1247. util::fail('请在首店操作');
  1248. }
  1249. $id = $post['id'] ?? 0;
  1250. $delStatus = $post['delStatus'] ?? 0;
  1251. $product = ProductClass::getById($id, true);
  1252. if ($product->mainId != $this->mainId) {
  1253. util::fail('不是你的花材哦!!');
  1254. }
  1255. //扫码收款、直接收款和特价商品,不可删除,多处要同步修改 system_item
  1256. if (getenv('YII_ENV') == 'production') {
  1257. $systemItem = [78076, 80960, 81004];
  1258. } else {
  1259. $systemItem = [11157, 11156, 11158];
  1260. }
  1261. if (in_array($product->itemId, $systemItem)) {
  1262. util::fail('系统使用花材,不可删除');
  1263. }
  1264. $product->delStatus = $delStatus;
  1265. if ($delStatus == 1) {
  1266. $product->stock = 0;
  1267. $product->status = 2;
  1268. }
  1269. $product->save();
  1270. $ptItemId = $product->itemId ?? 0;
  1271. if (isset($shop->default) && $shop->default == 1 && isset($shop->dataSync) && $shop->dataSync == 1) {
  1272. //总店修改同步到所有门店
  1273. $sjId = $shop->sjId ?? 0;
  1274. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  1275. foreach ($chainShopList as $chainShop) {
  1276. $chainShopId = $chainShop->id ?? 0;
  1277. if (isset($chainShop->join) && $chainShop->join == 1) {
  1278. continue;
  1279. }
  1280. if ($chainShopId == $shop->id) {
  1281. continue;
  1282. }
  1283. $chainMainId = $chainShop->mainId ?? 0;
  1284. $chainProduct = ProductClass::getByCondition(['mainId' => $chainMainId, 'itemId' => $ptItemId], true);
  1285. if (!empty($chainProduct)) {
  1286. $chainProduct->delStatus = $delStatus;
  1287. if ($delStatus == 1) {
  1288. $chainProduct->stock = 0;
  1289. $chainProduct->status = 2;
  1290. }
  1291. $chainProduct->save();
  1292. }
  1293. }
  1294. }
  1295. util::complete();
  1296. }
  1297. //详情 shish 20240115
  1298. public function actionDetail()
  1299. {
  1300. $id = Yii::$app->request->get('id', 0);
  1301. $source = Yii::$app->request->get('source', '');
  1302. $respond = ProductClass::getItemInfo($id);
  1303. if (empty($respond) || !is_array($respond)) {
  1304. util::fail('没有找到花材');
  1305. }
  1306. $limitBuyClearInfo = ProductClass::getLimitBuyClearInfo($id);
  1307. if (!is_array($limitBuyClearInfo)) {
  1308. $limitBuyClearInfo = [];
  1309. }
  1310. // foreach ($limitBuyClearInfo as $key => $value) {
  1311. // $respond[$key] = $value;
  1312. // }
  1313. $respond['limitBuyClearInfo'] = $limitBuyClearInfo;
  1314. $ptItemId = $respond['itemId'] ?? 0;
  1315. // 封面图与多花材图片的兼容处理
  1316. if ($respond['images'] == '' && $respond['shortCover'] != '') {
  1317. $respond['images'] = $respond['shortCover'];
  1318. }
  1319. $ptItemInfo = PtItemClass::getById($ptItemId);
  1320. $respond['ptItemInfo'] = $ptItemInfo;
  1321. //通知提醒抹除
  1322. if ($source == 'notify' && $respond['mainId'] == $this->mainId) {
  1323. $staff = $this->shopAdmin;
  1324. $staffId = $this->shopAdminId;
  1325. $notify = NotifyClass::getByCondition(['staffId' => $staffId, 'type' => 1, 'targetId' => $id], true);
  1326. if (!empty($notify)) {
  1327. if ($notify->read == 0) {
  1328. $notify->read = 1;
  1329. $notify->save();
  1330. $notifyNum = $staff->notifyNum;
  1331. if ($notifyNum > 0) {
  1332. $notifyNum--;
  1333. $staff->notifyNum = $notifyNum;
  1334. $staff->save();
  1335. }
  1336. }
  1337. }
  1338. }
  1339. //重复花材
  1340. $repeatList = ProductClass::getAllByCondition(['mainId' => $this->mainId, 'itemId' => $ptItemId, 'delStatus' => 0], null, '*', null, true);
  1341. $repeatId = 0;
  1342. if (!empty($repeatList)) {
  1343. foreach ($repeatList as $repeat) {
  1344. if ($repeat->id != $id) {
  1345. $repeatId = $repeat->id;
  1346. }
  1347. }
  1348. }
  1349. $respond['repeatId'] = $repeatId;
  1350. $cpItemList = CpItemClass::getAllByCondition(['productId' => $id], null, '*', 'cpId');
  1351. $cpInfo = [];
  1352. if (!empty($cpItemList)) {
  1353. $cpIds = array_column($cpItemList, 'cpId');
  1354. $cpList = CpClass::getByIds($cpIds);
  1355. if (!empty($cpList)) {
  1356. foreach ($cpList as $key => $val) {
  1357. $id = $val['id'] ?? 0;
  1358. $name = $val['name'] ?? '';
  1359. $cpInfo[] = ['cpId' => $id, 'cpName' => $name];
  1360. }
  1361. }
  1362. }
  1363. //判断这个花材能否被修改单位比和重量,同步参考ShopController actionMyShop方法,关键词has_right_shop_list
  1364. //判断当前登录的人,在自己有财务权限的main里面,是否包括了这个花材所属的平台id,在这个平台id下所有花材的main
  1365. $adminId = $this->adminId;
  1366. $hasRightRespond = ShopAdminClass::changeWeightRight($adminId, $ptItemId);
  1367. $respond['hasRightChange'] = $hasRightRespond['hasRightChange'];
  1368. $respond['hasRightChangeReason'] = $hasRightRespond['reason'];
  1369. //查出拆散的子花材
  1370. $part = UnitChangeClass::getByCondition(['big' => $id], true);
  1371. $partAcceptItem = 0;
  1372. $partAcceptItemName = '';
  1373. if (!empty($part)) {
  1374. $partAcceptItem = $part->small;
  1375. $small = ProductClass::getByCondition(['id' => $partAcceptItem], true, false, 'id,name');
  1376. $partAcceptItemName = $small->name ?? '';;
  1377. }
  1378. $respond['partAcceptItem'] = $partAcceptItem;
  1379. $respond['partAcceptItemName'] = $partAcceptItemName;
  1380. $respond['cpInfo'] = $cpInfo;
  1381. util::success($respond);
  1382. }
  1383. //新增花材 2021.6.21
  1384. public function actionAdd()
  1385. {
  1386. $post = Yii::$app->request->post();
  1387. $discountPrice = $post['discountPrice'] ?? 0;
  1388. if ($discountPrice > 0) {
  1389. if (isset($post['hjDiscountPrice']) == false || empty($post['hjDiscountPrice'])) {
  1390. $addPrice = $post['addPrice'] ?? 0;
  1391. $hjAddPrice = $post['hjAddPrice'] ?? 0;
  1392. $skMore = $post['skMore'] ?? 0;
  1393. $diff = bcsub($addPrice, $hjAddPrice, 2);
  1394. if ($diff > 0 && $discountPrice > $diff) {
  1395. $hjDiscountPrice = bcsub($discountPrice, $diff, 2);
  1396. } else {
  1397. $hjDiscountPrice = $discountPrice;
  1398. }
  1399. $diff = bcsub($skMore, $addPrice, 2);
  1400. if ($diff > 0) {
  1401. $skDiscountPrice = bcadd($diff, $discountPrice, 2);
  1402. } else {
  1403. $skDiscountPrice = $discountPrice;
  1404. }
  1405. $post['skDiscountPrice'] = $skDiscountPrice;
  1406. $post['hjDiscountPrice'] = $hjDiscountPrice;
  1407. }
  1408. }
  1409. $shop = $this->shop;
  1410. $join = $shop->join ?? 0;
  1411. $default = $shop->default ?? 0;
  1412. if ($join == 0 && $default == 0) {
  1413. util::fail('当前直营分店,请切回总店添加花材');
  1414. }
  1415. $shopAdmin = $this->shopAdmin;
  1416. if (!isset($shopAdmin->super) || $shopAdmin->super != 1) {
  1417. util::fail('管理员才能添加花材');
  1418. }
  1419. $post['sjId'] = $this->sjId;
  1420. $post['adminId'] = $this->adminId;
  1421. $post['mainId'] = $this->mainId;
  1422. $price = $post['price'] ?? 0;
  1423. $addPrice = $post['addPrice'] ?? 0;
  1424. if ($addPrice > $price) {
  1425. //客户添加花材时,经常会出现加价大于售价的怦,这个时候就直接把加价全部设置为0
  1426. $post['addPrice'] = 0;
  1427. $post['hjAddPrice'] = 0;
  1428. $post['skMore'] = 0;
  1429. //util::fail('加价不能大于售价');
  1430. }
  1431. $weight = isset($post['weight']) && is_numeric($post['weight']) ? $post['weight'] : 0;
  1432. if ($weight <= 0) {
  1433. util::fail('请填写重量,最小0.1');
  1434. }
  1435. $post['staffName'] = $shopAdmin->name ?? '';
  1436. // 图片列表数组转换为字符串
  1437. if (is_array($post['images'])) {
  1438. $post['images'] = implode(',', $post['images']);
  1439. }
  1440. ProductClass::addProduct($post, $this->shop);
  1441. util::complete('添加成功');
  1442. }
  1443. //获取拼音缩写 ssh 20210707
  1444. public function actionPy()
  1445. {
  1446. $name = Yii::$app->request->get('name');
  1447. $py = stringUtil::py($name);
  1448. util::success(['py' => $py]);
  1449. }
  1450. //(客户端-批发商)复制花材
  1451. public function actionClone()
  1452. {
  1453. $post = Yii::$app->request->post();
  1454. $discountPrice = $post['discountPrice'] ?? 0;
  1455. if ($discountPrice > 0) {
  1456. if (isset($post['hjDiscountPrice']) == false || empty($post['hjDiscountPrice'])) {
  1457. $addPrice = $post['addPrice'] ?? 0;
  1458. $hjAddPrice = $post['hjAddPrice'] ?? 0;
  1459. $skMore = $post['skMore'] ?? 0;
  1460. $diff = bcsub($addPrice, $hjAddPrice, 2);
  1461. if ($diff > 0 && $discountPrice > $diff) {
  1462. $hjDiscountPrice = bcsub($discountPrice, $diff, 2);
  1463. } else {
  1464. $hjDiscountPrice = $discountPrice;
  1465. }
  1466. $diff = bcsub($skMore, $addPrice, 2);
  1467. if ($diff > 0) {
  1468. $skDiscountPrice = bcadd($diff, $discountPrice, 2);
  1469. } else {
  1470. $skDiscountPrice = $discountPrice;
  1471. }
  1472. $post['skDiscountPrice'] = $skDiscountPrice;
  1473. $post['hjDiscountPrice'] = $hjDiscountPrice;
  1474. }
  1475. }
  1476. $shop = $this->shop;
  1477. $join = $shop->join ?? 0;
  1478. $default = $shop->default ?? 0;
  1479. if ($join == 0 && $default == 0) {
  1480. util::fail('请在首店操作');
  1481. }
  1482. $shopAdmin = $this->shopAdmin;
  1483. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  1484. util::fail('管理员才能添加花材');
  1485. }
  1486. $post['sjId'] = $this->sjId;
  1487. $post['adminId'] = $this->adminId;
  1488. $post['mainId'] = $this->mainId;
  1489. $post['staffName'] = $shopAdmin->name ?? '';
  1490. $price = $post['price'] ?? 0;
  1491. if (empty($price) || $price <= 0) {
  1492. util::fail('请填写批发价');
  1493. }
  1494. $skPrice = $post['skPrice'] ?? 0;
  1495. if (empty($skPrice) || $skPrice <= 0) {
  1496. util::fail('请填写零售价');
  1497. }
  1498. $hjPrice = $post['hjPrice'] ?? 0;
  1499. if (empty($hjPrice) || $hjPrice <= 0) {
  1500. util::fail('请填写会员价');
  1501. }
  1502. $addPrice = $post['addPrice'] ?? 0;
  1503. if ($addPrice > $price) {
  1504. util::fail('批发价的加价不能大于售价');
  1505. }
  1506. // 图片列表数组转换为字符串
  1507. if (is_array($post['images'])) {
  1508. $post['images'] = implode(',', $post['images']);
  1509. }
  1510. //复制标识
  1511. $post['copy'] = 1;
  1512. //去除无用的字段
  1513. unset($post['viewNum']);
  1514. unset($post['actualSold']);
  1515. unset($post['itemId']);
  1516. unset($post['id']);
  1517. unset($post['delStatus']);
  1518. unset($post['variety']);
  1519. ProductClass::addProduct($post, $this->shop);
  1520. util::complete('复制成功');
  1521. }
  1522. //下载价格表 ssh 20210922
  1523. public function actionGeneratePriceTable()
  1524. {
  1525. $level = Yii::$app->request->get('level', 0);
  1526. $respond = ProductService::generatePriceTable($this->sjId, $level, $this->mainId);
  1527. $file = $respond['file'] ?? '';
  1528. $shortFile = $respond['shortFile'] ?? '';
  1529. $fileUrl = Yii::$app->params['ghsHost'] . $file;
  1530. util::success(['file' => $fileUrl, 'shortFile' => $shortFile]);
  1531. }
  1532. //下载有库存花材,不含虚拟库存 ssh 20241-28
  1533. public function actionGenerateStockTable()
  1534. {
  1535. $level = Yii::$app->request->get('level', 0);
  1536. $respond = ProductService::generateStockTable($this->sjId, $level, $this->mainId);
  1537. $file = $respond['file'] ?? '';
  1538. $shortFile = $respond['shortFile'] ?? '';
  1539. $fileUrl = Yii::$app->params['ghsHost'] . $file;
  1540. util::success(['file' => $fileUrl, 'shortFile' => $shortFile]);
  1541. }
  1542. public function actionGenerateItemTable()
  1543. {
  1544. $level = Yii::$app->request->get('level', 0);
  1545. $respond = ProductService::generateItemTable($this->sjId, $level, $this->mainId);
  1546. $file = $respond['file'] ?? '';
  1547. $shortFile = $respond['shortFile'] ?? '';
  1548. $fileUrl = Yii::$app->params['ghsHost'] . $file;
  1549. util::success(['file' => $fileUrl, 'shortFile' => $shortFile]);
  1550. }
  1551. //下载条形码 lqh 20211227
  1552. public function actionGenerateBarcodeTable()
  1553. {
  1554. $respond = ProductService::generateBarcodeTable($this->sjId, $this->shopId, $this->mainId);
  1555. $file = $respond['file'] ?? '';
  1556. $shortFile = $respond['shortFile'] ?? '';
  1557. $fileUrl = Yii::$app->params['ghsHost'] . $file;
  1558. util::success(['file' => $fileUrl, 'shortFile' => $shortFile]);
  1559. }
  1560. //批量修改花材的分类 ssh 20230405
  1561. public function actionBatchModifyClass()
  1562. {
  1563. $post = Yii::$app->request->post();
  1564. $shop = $this->shop;
  1565. $sjName = $shop->merchantName ?? '';
  1566. $shopName = $shop->shopName ?? '';
  1567. $sjShopName = $shop->shopName == '首店' ? $sjName : $sjName . ' ' . $shopName;
  1568. //noticeUtil::push($sjShopName . ' 批量移动花材分类', '15280215347');
  1569. $join = $shop->join ?? 0;
  1570. $default = $shop->default ?? 0;
  1571. if ($join == 0 && $default == 0) {
  1572. util::fail('当前直营分店,请切回总店操作');
  1573. }
  1574. $classId = $post['classId'] ?? 0;
  1575. $class = ItemClassClass::getById($classId, true);
  1576. if (empty($class) || $class->mainId != $this->mainId) {
  1577. util::fail('请选择您自己的分类');
  1578. }
  1579. $connection = Yii::$app->db;
  1580. $transaction = $connection->beginTransaction();
  1581. try {
  1582. $className = $class->name ?? '';
  1583. $idsList = $post['ids'] ?? '';
  1584. $ids = json_decode($idsList, true);
  1585. if (empty($ids)) {
  1586. util::fail('请选择花材');
  1587. }
  1588. $list = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', null, true);
  1589. if (empty($list)) {
  1590. util::fail('请选择花材');
  1591. }
  1592. $ptItemList = [];
  1593. foreach ($list as $item) {
  1594. $name = $item->name ?? '';
  1595. if ($item->mainId != $this->mainId) {
  1596. util::fail($name . '不是您的花材');
  1597. }
  1598. $item->classId = $classId;
  1599. $item->save();
  1600. $ptItemId = $item->itemId ?? 0;
  1601. $ptItemList[] = $ptItemId;
  1602. //如果有修改分类,则在多个订单表中同步修改,如需修改,多个地方要同步修改,请搜索关键词item_class_modify_sync
  1603. $itemClassKey = 'item_class_modify_sync';
  1604. Yii::$app->redis->executeCommand('LPUSH', [$itemClassKey, $item->id]);
  1605. }
  1606. $shop = $this->shop;
  1607. if (isset($shop->default) && $shop->default == 1 && isset($shop->dataSync) && $shop->dataSync == 1) {
  1608. //总店修改同步到所有门店
  1609. $sjId = $shop->sjId ?? 0;
  1610. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  1611. foreach ($chainShopList as $chainShop) {
  1612. $chainShopId = $chainShop->id ?? 0;
  1613. if ($chainShopId == $shop->id) {
  1614. continue;
  1615. }
  1616. $chainManId = $chainShop->mainId ?? 0;
  1617. $chainAllClassList = ItemClassClass::getAllByCondition(['mainId' => $chainManId], null, '*', null, true);
  1618. if (!empty($chainAllClassList)) {
  1619. foreach ($chainAllClassList as $chainClass) {
  1620. $chainName = $chainClass->name ?? '';
  1621. $chainClassId = $chainClass->id ?? 0;
  1622. if ($chainName == $className) {
  1623. $chainProductList = Productclass::getAllByCondition(['mainId' => $chainManId, 'itemId' => ['in', $ptItemList]], null, '*', null, true);
  1624. if (!empty($chainProductList)) {
  1625. foreach ($chainProductList as $currentProduct) {
  1626. $currentProduct->classId = $chainClassId;
  1627. $currentProduct->save();
  1628. //如果有修改分类,则在多个订单表中同步修改,如需修改,多个地方要同步修改,请搜索关键词item_class_modify_sync
  1629. $itemClassKey = 'item_class_modify_sync';
  1630. Yii::$app->redis->executeCommand('LPUSH', [$itemClassKey, $currentProduct->id]);
  1631. }
  1632. }
  1633. }
  1634. }
  1635. }
  1636. }
  1637. }
  1638. $transaction->commit();
  1639. util::complete('修改成功');
  1640. } catch (\Exception $e) {
  1641. $transaction->rollBack();
  1642. Yii::info("修改失败原因:" . $e->getMessage());
  1643. util::fail('修改失败');
  1644. }
  1645. }
  1646. //批量修改采购人 ssh 20240609
  1647. public function actionBatchModifyCgStaff()
  1648. {
  1649. $post = Yii::$app->request->post();
  1650. $staffId = $post['staffId'] ?? 0;
  1651. $staff = ShopAdminClass::getById($staffId, true);
  1652. if (empty($staff)) {
  1653. util::fail('没有找到这个员工');
  1654. }
  1655. if ($staff->mainId != $this->mainId) {
  1656. util::fail('不是你的员工');
  1657. }
  1658. $staffName = $staff->name ?? '';
  1659. $connection = Yii::$app->db;
  1660. $transaction = $connection->beginTransaction();
  1661. try {
  1662. $idsList = $post['ids'] ?? '';
  1663. $ids = json_decode($idsList, true);
  1664. if (empty($ids)) {
  1665. util::fail('请选择花材');
  1666. }
  1667. $list = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', null, true);
  1668. if (empty($list)) {
  1669. util::fail('请选择花材');
  1670. }
  1671. $shop = $this->shop;
  1672. $bookSn = $shop->bookSn ?? 0;
  1673. foreach ($list as $item) {
  1674. $name = $item->name ?? '';
  1675. if ($item->mainId != $this->mainId) {
  1676. util::fail($name . '不是您的花材');
  1677. }
  1678. $item->cgStaffId = $staffId;
  1679. $item->cgStaffName = $staffName;
  1680. $item->save();
  1681. $itemId = $item->id ?? 0;
  1682. if (!empty($bookSn)) {
  1683. //预订管理的花材需要变化
  1684. BookItemClass::updateByCondition(['bookSn' => $bookSn, 'itemId' => $itemId], ['cgStaffId' => $staffId, 'cgStaffName' => $staffName]);
  1685. }
  1686. }
  1687. $transaction->commit();
  1688. util::complete('修改成功');
  1689. } catch (\Exception $e) {
  1690. $transaction->rollBack();
  1691. Yii::info("修改失败原因:" . $e->getMessage());
  1692. util::fail('修改失败');
  1693. }
  1694. }
  1695. //修改属于成本还是收入选项 ssh 20240514
  1696. public function actionChangeBelongCost()
  1697. {
  1698. $get = Yii::$app->request->get();
  1699. $id = $get['id'] ?? 0;
  1700. $belongCost = $get['belongCost'] ?? 0;
  1701. $info = ProductClass::getById($id, true);
  1702. if (empty($info)) {
  1703. util::fail('没有找到花材');
  1704. }
  1705. if ($info->mainId != $this->mainId) {
  1706. util::fail('不是你的花材哦');
  1707. }
  1708. $info->belongCost = $belongCost;
  1709. $info->save();
  1710. //队列方式去处理
  1711. $redisKey = 'change_belong_cost_item_list';
  1712. $value = $id . '_' . $belongCost;
  1713. Yii::$app->redis->executeCommand('LPUSH', [$redisKey, $value]);
  1714. util::complete('修改成功');
  1715. }
  1716. //修改拆散接收的花材 ssh 20251124
  1717. public function actionModifyPartAcceptItem()
  1718. {
  1719. $get = Yii::$app->request->get();
  1720. $smallId = $get['small'] ?? 0;
  1721. $bigId = $get['big'] ?? 0;
  1722. $small = ProductClass::getById($smallId, true);
  1723. if (empty($small)) {
  1724. util::fail('子花材没找到');
  1725. }
  1726. $smallName = $small->name ?? '';
  1727. if ($small->mainId != $this->mainId) {
  1728. util::fail($smallName . '不是你的花材');
  1729. }
  1730. if ($small->ratio != 1) {
  1731. util::fail($smallName . '不是1支装的');
  1732. }
  1733. $big = ProductClass::getById($bigId, true);
  1734. if (empty($big)) {
  1735. util::fail('没有找到花材');
  1736. }
  1737. if ($big->mainId != $this->mainId) {
  1738. util::fail('不是你的花材呢');
  1739. }
  1740. $change = UnitChangeClass::getByCondition(['big' => $bigId], true);
  1741. if (empty($change)) {
  1742. UnitChangeClass::add(['big' => $bigId, 'small' => $smallId], true);
  1743. } else {
  1744. $change->small = $smallId;
  1745. $change->save();
  1746. }
  1747. util::complete('修改成功');
  1748. }
  1749. }