ProductController.php 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509
  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 biz\wx\classes\WxMessageClass;
  10. use bizGhs\cp\classes\CpClass;
  11. use bizGhs\cp\classes\CpItemClass;
  12. use bizGhs\custom\classes\CustomClass;
  13. use bizGhs\item\classes\ItemClass;
  14. use bizGhs\item\classes\ItemClassClass;
  15. use bizGhs\merchant\classes\ShopClass;
  16. use bizGhs\notify\classes\NotifyClass;
  17. use bizGhs\order\classes\PurchaseOrderClass;
  18. use bizGhs\product\classes\ProductClass;
  19. use bizGhs\product\models\Product;
  20. use bizGhs\product\services\ProductService;
  21. use bizGhs\shop\classes\ShopAdminClass;
  22. use bizGhs\stock\classes\StockInDayClass;
  23. use common\components\imgUtil;
  24. use common\components\noticeUtil;
  25. use common\components\printUtil;
  26. use common\components\stringUtil;
  27. use common\components\util;
  28. use bizGhs\stock\classes\OnStockRecordClass;
  29. use Yii;
  30. class ProductController extends BaseController
  31. {
  32. public $guestAccess = ['index'];
  33. //修改路上库存 ssh 20231219
  34. public function actionModifyOnStock()
  35. {
  36. $get = Yii::$app->request->get();
  37. $id = $get['id'] ?? 0;
  38. $onStock = $get['onStock'] ?? 0;
  39. $info = ProductClass::getLockById($id);
  40. ProductClass::check($info, $this->mainId);
  41. $oldOnStock = $info->onStock ?? 0;
  42. $newOnStock = $onStock;
  43. $num = bcsub($newOnStock, $oldOnStock);
  44. $num = abs($num);
  45. $staff = $this->shopAdmin;
  46. $staffName = $staff->name ?? '';
  47. $onRecord = [];
  48. $onRecord['sjId'] = $this->sjId;
  49. $onRecord['shopId'] = $this->shopId;
  50. $onRecord['mainId'] = $this->mainId;
  51. $onRecord['orderSn'] = '';
  52. $onRecord['productId'] = $id;
  53. $onRecord['itemId'] = $info->itemId ?? 0;
  54. $onRecord['relateName'] = $staffName;
  55. $onRecord['itemNum'] = $num;
  56. $onRecord['oldStock'] = $oldOnStock;
  57. $onRecord['newStock'] = $newOnStock;
  58. $onRecord['ptStyle'] = 2;
  59. OnStockRecordClass::modify($onRecord);
  60. $info->onStock = $newOnStock;
  61. $info->save();
  62. util::complete('修改成功');
  63. }
  64. //停止预售 ssh 20230224
  65. public function actionCancelPreSell()
  66. {
  67. $get = Yii::$app->request->get();
  68. $id = $get['id'] ?? 0;
  69. $info = ProductClass::getById($id, true);
  70. ProductClass::check($info, $this->mainId);
  71. if ($info->stock > 0) {
  72. util::fail('没有库存才能停止预售');
  73. }
  74. $info->presell = 0;
  75. $info->presellDate = '';
  76. $info->save();
  77. util::complete();
  78. }
  79. //修改预售 ssh 20221214
  80. public function actionChangePresell()
  81. {
  82. $post = Yii::$app->request->post();
  83. $id = $post['id'] ?? 0;
  84. $info = ProductClass::getById($id, true);
  85. ProductClass::check($info, $this->mainId);
  86. $str = $post['date'] ?? '';
  87. $presell = $post['presell'] ?? 0;
  88. $newStr = '';
  89. if ($presell == 1) {
  90. if (empty($str)) {
  91. util::fail('请填写预售日期');
  92. }
  93. $arr = json_decode($str, true);
  94. if (empty($arr)) {
  95. util::fail('请填写预售日期');
  96. }
  97. $new = [];
  98. foreach ($arr as $date) {
  99. $new[] = strtotime($date);
  100. }
  101. $new = array_unique(array_filter($new));
  102. asort($new);
  103. $newStr = implode(',', $new);
  104. }
  105. $info->presell = $presell;
  106. $info->presellDate = $newStr;
  107. $info->discountPrice = 0;
  108. $info->save();
  109. util::complete();
  110. }
  111. //取消满减 ssh 20231216
  112. public function actionCancelFullOff()
  113. {
  114. $get = Yii::$app->request->get();
  115. $id = $get['id'] ?? 0;
  116. $info = ProductClass::getById($id, true);
  117. ProductClass::check($info, $this->mainId);
  118. $info->reachNum = 0;
  119. $info->reachNumDiscount = 0;
  120. $info->save();
  121. util::complete('取消成功');
  122. }
  123. //取消特价 ssh 20220901
  124. public function actionCancelDiscount()
  125. {
  126. $get = Yii::$app->request->get();
  127. $id = $get['id'] ?? 0;
  128. $info = ProductClass::getById($id, true);
  129. ProductClass::check($info, $this->mainId);
  130. $info->discountPrice = 0;
  131. $info->skDiscountPrice = 0;
  132. $info->hjDiscountPrice = 0;
  133. $info->save();
  134. util::complete('取消成功');
  135. }
  136. //打印花材的标签 ssh 20220602
  137. public function actionPrint()
  138. {
  139. $get = Yii::$app->request->get();
  140. $id = $get['id'] ?? 0;
  141. $num = $get['num'] ?? 1;
  142. $num = floatval($num);
  143. $num = (int)$num;
  144. $type = $get['type'] ?? 0;
  145. $info = ProductClass::getById($id, true);
  146. if (empty($info)) {
  147. util::fail('没有找到花材');
  148. }
  149. $name = $info->name ?? '';
  150. $ptItemId = $info->itemId ?? 0;
  151. $mainId = $info->mainId ?? 0;
  152. $ext = $this->shopExt;
  153. $printLabelSn = $ext->printLabelSn ?? '';
  154. if (empty($printLabelSn)) {
  155. util::fail('请设置标签打印机');
  156. }
  157. $p = new printUtil($printLabelSn);
  158. $unit = $info->ratio . $info->smallUnit . '/' . $info->bigUnit;
  159. if (isset($info->ratioType) && $info->ratioType == 1) {
  160. $unit = '若干' . $info->smallUnit . '/' . $info->bigUnit;
  161. }
  162. $p->times = $num;
  163. if (stringUtil::getWordNum($name) > 7) {
  164. $content = '<TEXT x="30" y="30" font="12" w="1" h="1" r="0">' . $name . '</TEXT>';
  165. } else {
  166. $content = '<TEXT x="30" y="30" font="12" w="2" h="2" r="0">' . $name . '</TEXT>';
  167. }
  168. //杭州斗南批发店
  169. if (isset($get['type']) == false && $this->shopId == 4608) {
  170. $content .= '<QR x="35" y="100" e="L" w="5">' . Yii::$app->params['mallHost'] . "/item/show-info?id=" . $id . '</QR>';
  171. $content .= '<TEXT x="40" y="270" font="12" w="1" h="1" r="0">扫码看价格</TEXT>';
  172. } else {
  173. if ($type == 0) {
  174. //打标签
  175. $content .= '<BC128 x="30" y="115" h="75" s="1" r="0" n="3" w="10">' . $ptItemId . '</BC128>';
  176. if (in_array($mainId, [52, 1459])) {
  177. $content .= '<TEXT x="30" y="250" font="12" w="2" h="2" r="0">惠雅鲜花</TEXT>';
  178. } else {
  179. $content .= '<TEXT x="30" y="250" font="12" w="1" h="1" r="0">' . $unit . '</TEXT>';
  180. }
  181. } else {
  182. //打价码
  183. $content .= '<QR x="35" y="100" e="L" w="5">' . Yii::$app->params['mallHost'] . "/item/show-info?id=" . $id . '</QR>';
  184. $content .= '<TEXT x="40" y="270" font="12" w="1" h="1" r="0">扫码看价格</TEXT>';
  185. }
  186. }
  187. $p->printLabelMsg($content);
  188. util::complete();
  189. }
  190. //取出今天有入库的花材 ssh 20221024
  191. public function actionStockIn()
  192. {
  193. $py = Yii::$app->request->get('py', '');
  194. $version = Yii::$app->request->get('version', 0);
  195. if ($version == 0) {
  196. util::fail('请先升级版本到1.0.70以上');
  197. }
  198. $classInfo = ItemClassClass::getGhsItemClassAll($this->mainId);
  199. $mainId = $this->mainId ?? 0;
  200. $where['mainId'] = $mainId;
  201. if ($py) {
  202. $pyName = strtolower($py);
  203. $where['py'] = $pyName;
  204. }
  205. $arr = StockInDayClass::getAllByCondition(['mainId' => $mainId], null, '*');
  206. if (empty($arr)) {
  207. util::fail('没有花材需要盘点');
  208. }
  209. $ids = array_column($arr, 'itemId');
  210. $ids = array_unique($ids);
  211. $where['id'] = ['in', $ids];
  212. $level = 1;
  213. $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';
  214. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  215. $itemInfoData = ProductClass::groupProductInfo($itemInfoData, $level);
  216. $data = ProductService::assembleData($classInfo, $itemInfoData, true);
  217. util::success($data);
  218. }
  219. //按分类列出所有花材
  220. public function actionShow()
  221. {
  222. $py = Yii::$app->request->get('py', '');
  223. $name = Yii::$app->request->get('name', '');
  224. $requestType = Yii::$app->request->get('requestType', 'common');
  225. //默认显示上架商品
  226. $status = Yii::$app->request->get('status', 1);
  227. //默认显示未删除商品
  228. $delStatus = Yii::$app->request->get('delStatus', 0);
  229. $classId = Yii::$app->request->get('classId', 0);
  230. //获取所有当前供货商的分类 (全部、常用)
  231. //根据分类组装分类下的花材信息
  232. //中央ID
  233. $classInfo = ItemClassClass::getGhsItemClassAll($this->mainId);
  234. $where['mainId'] = $this->mainId;
  235. if ($py) {
  236. $pyName = strtolower($py);
  237. $where['py'] = $pyName;
  238. }
  239. //改价和全部有库存花材列表
  240. if ($requestType == 'changePrice' || $requestType == 'hasStockList') {
  241. $where['stock>'] = 0;
  242. }
  243. if (!empty($status)) {
  244. $where['status'] = $status;
  245. if ($requestType == 'itemList') {
  246. unset($where['status']);
  247. }
  248. }
  249. if ($requestType == 'book') {
  250. unset($where['status']);
  251. }
  252. $where['delStatus'] = $delStatus;
  253. if (!empty($classId)) {
  254. $where['classId'] = $classId;
  255. }
  256. if (!empty($name)) {
  257. if (stringUtil::isLetter($name)) {
  258. $name = strtolower($name);
  259. $where['py'] = ['like', $name];
  260. } else {
  261. $where['name'] = ['like', $name];
  262. }
  263. }
  264. //客户等级
  265. $customId = Yii::$app->request->get('customId', 0);
  266. $level = 1;
  267. if (!empty($customId)) {
  268. $custom = CustomClass::getById($customId, true);
  269. $level = $custom->level ?? 1;
  270. }
  271. $itemInfoData = [];
  272. if ($requestType == 'hasStockList') {
  273. //有库存的花材列表
  274. $field = 'id,py,cover,name,classId,itemId,price,skPrice,hjPrice,stock,cost';
  275. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  276. if (!empty($itemInfoData)) {
  277. foreach ($itemInfoData as $key => $val) {
  278. //以下参数主要是网页批量改价要用到
  279. $shortCover = $val['cover'] ?? '';
  280. $itemInfoData[$key]['cover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_100,w_100";
  281. $itemInfoData[$key]['shortCover'] = $shortCover;
  282. }
  283. }
  284. } elseif ($requestType == 'changePrice') {
  285. $field = 'id,py,cover,name,cost,itemId,classId,addPrice,skPrice,hjPrice,discountPrice,hjDiscountPrice,skDiscountPrice,skMore,hjAddPrice,addPrice,variety,price,hjPrice,stock,onStock,presell,frontHide,reachNum,reachNumDiscount';
  286. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  287. $itemInfoData = ProductClass::changePriceGroup($itemInfoData, $level);
  288. } elseif ($requestType == 'changeStock') {
  289. //修改库存列表
  290. $field = 'id,py,cover,name,cost,itemId,status,classId,addPrice,skPrice,discountPrice,skMore,variety,price,stock,frontHide';
  291. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  292. $itemInfoData = ProductClass::changeStockGroup($itemInfoData, $level);
  293. } elseif ($requestType == 'kd') {
  294. //开单的花材列表
  295. $field = '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';
  296. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  297. $itemInfoData = ProductClass::kdItemGroup($itemInfoData, $level);
  298. } elseif ($requestType == 'book') {
  299. //预订花材列表
  300. $field = 'id,py,cover,name,cost,itemId,classId,skPrice,variety,price,hjPrice,hjDiscountPrice,skDiscountPrice,discountPrice,stock,weight,stockWarning,presell,ratioType,smallUnit,smallRatio,bigUnit,ratio,frontHide';
  301. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  302. $itemInfoData = ProductClass::bookItemGroup($itemInfoData, $level);
  303. } elseif ($requestType == 'itemList') {
  304. //花材列表
  305. $field = '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';
  306. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  307. $itemInfoData = ProductClass::itemListGroup($itemInfoData, $level);
  308. } elseif ($requestType == 'outList') {
  309. //下架花材列表
  310. $where['status'] = 2;
  311. $where['delStatus'] = 0;
  312. $where['removeStatus'] = 0;
  313. $field = 'id,py,cover,name,cost,itemId,classId,addPrice,skPrice,bigUnit,smallUnit,ratio,ratioType,discountPrice,skMore,variety,price,stock,presell,status';
  314. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  315. $itemInfoData = ProductClass::simpleGroup($itemInfoData, $level);
  316. } elseif ($requestType == 'stopList') {
  317. //停用花材列表
  318. unset($where['status']);
  319. $where['delStatus'] = 1;
  320. $where['removeStatus'] = 0;
  321. $field = 'id,py,cover,name,cost,itemId,classId,addPrice,skPrice,bigUnit,smallUnit,ratio,ratioType,discountPrice,skMore,variety,price,stock,presell,status,delStatus';
  322. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  323. $itemInfoData = ProductClass::simpleGroup($itemInfoData, $level);
  324. } elseif ($requestType == 'removeList') {
  325. //删除花材列表
  326. unset($where['status']);
  327. $where['delStatus'] = 1;
  328. $where['removeStatus'] = 1;
  329. $field = 'id,py,cover,name,cost,itemId,classId,addPrice,skPrice,bigUnit,smallUnit,ratio,ratioType,discountPrice,skMore,variety,price,stock,presell,status';
  330. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  331. $itemInfoData = ProductClass::simpleGroup($itemInfoData, $level);
  332. } elseif ($requestType == 'check') {
  333. $field = 'id,py,cover,name,itemId,classId,variety,price,discountPrice,stock,stockWarning,presell,ratioType,smallUnit,bigUnit,ratio,frontHide';
  334. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  335. $itemInfoData = ProductClass::checkItemGroup($itemInfoData, $level);
  336. } elseif ($requestType == 'break') {
  337. $field = 'id,py,cover,name,itemId,classId,variety,price,discountPrice,stock,stockWarning,presell,ratioType,smallUnit,bigUnit,ratio,frontHide';
  338. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  339. $itemInfoData = ProductClass::breakItemGroup($itemInfoData, $level);
  340. } elseif ($requestType == 'part') {
  341. //拆散花材
  342. $field = 'id,py,cover,name,itemId,classId,variety,price,discountPrice,stock,stockWarning,presell,ratioType,smallUnit,bigUnit,ratio';
  343. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  344. $itemInfoData = ProductClass::partItemGroup($itemInfoData, $level);
  345. } elseif ($requestType == 'stockOut') {
  346. $field = 'id,py,cover,name,itemId,classId,variety,price,discountPrice,stock,stockWarning,presell,ratioType,smallUnit,bigUnit,ratio,smallRatio,frontHide';
  347. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  348. $itemInfoData = ProductClass::stockOutGroup($itemInfoData, $level);
  349. } elseif ($requestType == 'cg') {
  350. $field = 'id,py,cover,name,itemId,classId,variety,price,discountPrice,stock,presell,ratioType,smallUnit,bigUnit,ratio,weight,frontHide';
  351. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  352. $itemInfoData = ProductClass::cgItemGroup($itemInfoData, $level);
  353. } elseif ($requestType == 'warning') {
  354. $field = 'id,py,cover,name,cost,itemId,classId,skPrice,variety,price,discountPrice,stock,onStock,actualSold,presell,bigUnit,stockWarning,status,frontHide';
  355. $itemInfoData = Product::find()->where("mainId={$this->mainId}")
  356. ->andWhere('`stock`+`onStock`<`stockWarning`')
  357. ->andWhere("delStatus=0")
  358. ->select($field)->asArray()->all();
  359. $itemInfoData = ProductClass::warningGroup($itemInfoData, $level);
  360. } else {
  361. util::fail('没有数据');
  362. }
  363. $data = ProductService::assembleItemData($classInfo, $itemInfoData);
  364. util::success($data);
  365. }
  366. //取出今天有入库的花材 ssh 20221224
  367. public function actionGetStockIn()
  368. {
  369. $py = Yii::$app->request->get('py', '');
  370. $version = Yii::$app->request->get('version', 0);
  371. if ($version == 0) {
  372. util::fail('请先升级版本到1.0.70以上');
  373. }
  374. $classInfo = ItemClassClass::getGhsItemClassAll($this->mainId);
  375. $mainId = $this->mainId ?? 0;
  376. $where['mainId'] = $mainId;
  377. if ($py) {
  378. $pyName = strtolower($py);
  379. $where['py'] = $pyName;
  380. }
  381. $arr = StockInDayClass::getAllByCondition(['mainId' => $mainId], null, '*');
  382. if (empty($arr)) {
  383. util::fail('没有花材需要盘点');
  384. }
  385. $ids = array_column($arr, 'itemId');
  386. $ids = array_unique($ids);
  387. $where['id'] = ['in', $ids];
  388. $level = 1;
  389. $field = 'id,py,cover,name,itemId,classId,variety,price,discountPrice,stock,stockWarning,presell,ratioType,smallUnit,bigUnit,ratio';
  390. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  391. $itemInfoData = ProductClass::checkItemGroup($itemInfoData, $level);
  392. $data = ProductService::assembleItemData($classInfo, $itemInfoData);
  393. util::success($data);
  394. }
  395. //按分类列出所有花材
  396. public function actionIndex()
  397. {
  398. $py = Yii::$app->request->get('py', '');
  399. $requestType = Yii::$app->request->get('requestType', 'common');
  400. //默认显示上架商品
  401. $status = Yii::$app->request->get('status', 1);
  402. //默认显示未删除商品
  403. $delStatus = Yii::$app->request->get('delStatus', 0);
  404. $warning = Yii::$app->request->get('warning', 0);
  405. //获取所有当前供货商的分类 (全部、常用)
  406. //根据分类组装分类下的花材信息
  407. //中央ID
  408. $classInfo = ItemClassClass::getGhsItemClassAll($this->mainId);
  409. $where['mainId'] = $this->mainId;
  410. if ($py) {
  411. $pyName = strtolower($py);
  412. $where['py'] = $pyName;
  413. }
  414. if ($requestType == 'hasStock') {
  415. //改价只显示库存大于0的
  416. $where['stock>'] = 0;
  417. }
  418. if (!empty($status)) {
  419. $where['status'] = $status;
  420. }
  421. $where['delStatus'] = $delStatus;
  422. //客户等级
  423. $customId = Yii::$app->request->get('customId', 0);
  424. $level = 1;
  425. if (!empty($customId)) {
  426. $custom = CustomClass::getById($customId, true);
  427. $level = $custom->level ?? 1;
  428. }
  429. if ($warning == 1) {
  430. //库存预警
  431. $field = 'id,py,cover,name,ratio,cost,addPrice,classId,skMore,skPrice,itemId,smallUnit,smallRatio,presellDate,bigUnit,smallUnit,ratioType,variety,price,stock,stockWarning,discountPrice,presell';
  432. $itemInfoData = Product::find()->where("mainId={$this->mainId}")
  433. ->andWhere('`stock`<`stockWarning`')
  434. ->andWhere("delStatus=0")
  435. ->select($field)->asArray()->all();
  436. } else {
  437. $field = 'id,py,cover,name,ratio,cost,addPrice,classId,skMore,skPrice,itemId,smallUnit,smallRatio,presellDate,bigUnit,smallUnit,ratioType,variety,price,stock,stockWarning,discountPrice,presell';
  438. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  439. }
  440. $itemInfoData = ProductClass::groupProductInfo($itemInfoData, $level);
  441. $data = ProductService::assembleData($classInfo, $itemInfoData, true);
  442. util::success($data);
  443. }
  444. //所有花材分页和不分页
  445. public function actionList()
  446. {
  447. $where = [];
  448. $classId = Yii::$app->request->get('classId', 0);
  449. if (!empty($classId)) {
  450. $where['classId'] = $classId;
  451. }
  452. $name = Yii::$app->request->get('name', '');
  453. if (empty($name)) {
  454. $py = Yii::$app->request->get('py', '');
  455. $name = !empty($py) ? $py : '';
  456. }
  457. if (!empty($name)) {
  458. if (stringUtil::isLetter($name)) {
  459. $where['py'] = ['like', $name];
  460. } else {
  461. $where['name'] = ['like', $name];
  462. }
  463. }
  464. $status = Yii::$app->request->get('status', 0);
  465. if (!empty($status)) {
  466. $where['status'] = $status;
  467. }
  468. $delStatus = Yii::$app->request->get('delStatus', 0);
  469. if ($delStatus != 2) {
  470. $where['delStatus'] = $delStatus;
  471. }
  472. $where['mainId'] = $this->mainId;
  473. $productIds = Yii::$app->request->get('ids', '');
  474. if (!empty($productIds)) {
  475. $productIdArr = explode(',', $productIds);
  476. if (!empty($productIdArr)) {
  477. $where['id'] = ['in', $productIdArr];
  478. }
  479. }
  480. //输出标准会员价,即批发价,默认显示的是批发价
  481. $level = 1;
  482. $type = Yii::$app->request->get('type', '');
  483. $showPage = Yii::$app->request->get('showPage', 0);
  484. if ($showPage) {
  485. if ($type == 'waring') {
  486. $pro = Product::find()->where("mainId={$this->mainId}")->andWhere('`stock`<`stockWarning`')->select('id')->asArray()->all();
  487. $lackIds = array_column($pro, 'id');
  488. if (empty($lackIds)) {
  489. util::success([]);
  490. }
  491. $lackIds = array_filter(array_unique($lackIds));
  492. $where['id'] = ['in', $lackIds];
  493. $respond = ProductClass::getList("*", $where, "actualSold desc");
  494. } else {
  495. $respond = ProductClass::getList("*", $where, "inTurn desc");
  496. }
  497. $respond['list'] = ProductClass::groupProductInfo($respond['list'], $level);
  498. $respond['list'] = ProductClass::groupClassName($this->sjId, $respond['list']);
  499. util::success($respond);
  500. } else {
  501. if ($type == 'waring') {
  502. //库存预警 按库存从小到大排序
  503. $data = ProductClass::getAllByCondition($where, ['stock' => SORT_ASC, 'inTurn' => SORT_DESC], "*");
  504. } else {
  505. if (!empty($productIds)) {
  506. //优先按 $productIds 排序 "FIELD(`id`,4,5,6)"=>true
  507. $data = ProductClass::getAllByCondition($where, ["FIELD(`id`,$productIds)" => true, 'inTurn' => SORT_DESC], "*");
  508. } else {
  509. $data = ProductClass::getAllByCondition($where, ['actualSold' => SORT_DESC, 'inTurn' => SORT_DESC], "*");
  510. }
  511. }
  512. $data = ProductClass::groupProductInfo($data, $level);
  513. $data = ProductClass::groupClassName($this->sjId, $data);
  514. if ($type == 'waring') {
  515. //库存预警
  516. $newData = [];
  517. foreach ($data as $v) {
  518. if ($v['stock'] <= $v['stockWarning']) {
  519. $newData[] = $v;
  520. }
  521. }
  522. util::success(['list' => $newData]);
  523. }
  524. util::success(['list' => $data]);
  525. }
  526. }
  527. //所有花材
  528. public function actionAll()
  529. {
  530. $where = [];
  531. $where['mainId'] = $this->mainId;
  532. $classId = Yii::$app->request->get('classId', 0);
  533. if (!empty($classId)) {
  534. $where['classId'] = $classId;
  535. }
  536. $status = Yii::$app->request->get('status', 0);
  537. if (!empty($status)) {
  538. $where['status'] = $status;
  539. }
  540. $delStatus = Yii::$app->request->get('delStatus', 0);
  541. if ($delStatus != 2) {
  542. $where['delStatus'] = $delStatus;
  543. }
  544. //输出标准会员价,即批发价,默认显示的是批发价
  545. $level = 1;
  546. $data = ProductClass::getAllByCondition($where, ['actualSold' => SORT_DESC, 'inTurn' => SORT_DESC], "*");
  547. $data = ProductClass::groupProductInfo($data, $level);
  548. util::success(['list' => $data]);
  549. }
  550. //批量改价
  551. public function actionBatchChangePrice()
  552. {
  553. $shop = $this->shop;
  554. $join = $shop->join ?? 0;
  555. $default = $shop->default ?? 0;
  556. if ($join == 0 && $default == 0) {
  557. util::fail('当前直营分店,只能回总店修改价格');
  558. }
  559. $shopAdmin = $this->shopAdmin;
  560. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  561. util::fail('超管才能修改');
  562. }
  563. $shop = $this->shop;
  564. $default = $shop->default ?? 0;
  565. $adminId = $this->adminId ?? 0;
  566. //normal常规改价,cg采购改价
  567. $changeType = Yii::$app->request->post('changeType', 'normal');
  568. $targetId = Yii::$app->request->post('targetId', 0);
  569. $changeParams = ['staffId' => $shopAdmin->id, 'staffName' => $shopAdmin->name, 'changeType' => $changeType, 'targetId' => $targetId];
  570. $post = Yii::$app->request->post();
  571. $data = $post['data'] ?? '';
  572. if (empty($data)) {
  573. util::fail('没有修改');
  574. }
  575. $arr = json_decode($data, true);
  576. if (empty($arr)) {
  577. util::fail('没有修改哦');
  578. }
  579. $appVersion = $post['appVersion'] ?? 0;
  580. if ($appVersion != 1) {
  581. util::fail('请先升级应用');
  582. }
  583. //解决重复请求问题
  584. $cacheKey = 'batch_change_price_' . $targetId;
  585. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  586. if (!empty($has)) {
  587. util::fail('请稍等5秒操作');
  588. }
  589. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 5, 'has']);
  590. $connection = Yii::$app->db;
  591. $transaction = $connection->beginTransaction();
  592. try {
  593. $needNotice = false;
  594. $cg = PurchaseOrderClass::getLockById($targetId);
  595. if (!empty($cg)) {
  596. if (isset($cg->mainId) == false || $cg->mainId != $this->mainId) {
  597. util::fail('不是您的采购单');
  598. }
  599. //批量改价同时入库
  600. if ($cg->status == PurchaseOrderClass::PURCHASE_ORDER_STATUS_SENDING) {
  601. $staff = $this->shopAdmin;
  602. $data = [];
  603. $data['staffId'] = $staff->id ?? 0;
  604. $data['staffName'] = $staff->name ?? '';
  605. $data['adminId'] = $this->adminId ?? 0;
  606. PurchaseOrderClass::putIn($cg, $data);
  607. $needNotice = true;
  608. }
  609. }
  610. $ids = array_column($arr, 'id');
  611. $ids = array_unique(array_filter($ids));
  612. if (empty($ids)) {
  613. util::fail('请选择花材');
  614. }
  615. $sjId = $shop->sjId ?? 0;
  616. $chainShopList = [];
  617. if ($default == 1) {
  618. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId, 'join' => 0], null, '*', null, true);
  619. }
  620. $productList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id', true);
  621. foreach ($arr as $product) {
  622. $productId = $product['id'];
  623. $productInfo = $productList[$productId] ?? [];
  624. if (empty($productInfo)) {
  625. util::fail('没有找到花材');
  626. }
  627. $productName = $productInfo->name ?? '';
  628. if (isset($product['id']) == false) {
  629. util::fail('没有花材');
  630. }
  631. if (isset($product['price']) == false || is_numeric($product['price']) == false || $product['price'] < 0) {
  632. util::fail('请填写' . $productName . '的批发价');
  633. }
  634. $price = $product['price'];
  635. if (isset($product['skPrice']) == false || is_numeric($product['skPrice']) == false || $product['skPrice'] < 0) {
  636. util::fail('请填写' . $productName . '的零售价');
  637. }
  638. $skPrice = $product['skPrice'];
  639. if (isset($product['hjPrice']) == false || is_numeric($product['hjPrice']) == false || $product['hjPrice'] < 0) {
  640. util::fail('请填写' . $productName . '的会员价');
  641. }
  642. $hjPrice = $product['hjPrice'] ?? 0;
  643. if (isset($productInfo->mainId) == false || $productInfo->mainId != $this->mainId) {
  644. util::fail('无法访问');
  645. }
  646. if ($hjPrice > $price) {
  647. util::fail($productName . ' 批发价要大于会员价');
  648. }
  649. if ($price > $skPrice) {
  650. util::fail($productName . ' 零售价要大于批发价');
  651. }
  652. $ptItemId = $productInfo->itemId;
  653. ProductClass::changeSinglePrice($shop, $ptItemId, $price, $skPrice, $hjPrice, $changeParams);
  654. //在首店修改需要同步到所有直营店
  655. if ($default == 1 && isset($shop->dataSync) && $shop->dataSync == 1) {
  656. foreach ($chainShopList as $chain) {
  657. if ($chain->id == $shop->id) {
  658. continue;
  659. }
  660. $chainMainId = $chain->mainId ?? 0;
  661. $staff = ShopAdminClass::getByCondition(['mainId' => $chainMainId, 'adminId' => $adminId], true);
  662. $staffId = $staff->id ?? 0;
  663. $staffName = $staff->name ?? '';
  664. $changeParams2 = ['staffId' => $staffId, 'staffName' => $staffName, 'changeType' => $changeType, 'targetId' => $targetId];
  665. ProductClass::changeSinglePrice($chain, $ptItemId, $price, $skPrice, $hjPrice, $changeParams2);
  666. }
  667. }
  668. }
  669. $mainId = $shop->mainId ?? 0;
  670. if (!empty($mainId)) {
  671. //提醒零售收银台界面要刷新
  672. $staffList = ShopAdminClass::getAllByCondition(['mainId' => $mainId], null, '*', null, true);
  673. if (!empty($staffList)) {
  674. foreach ($staffList as $staff) {
  675. $staffId = $staff->id ?? 0;
  676. Yii::$app->redis->executeCommand('SET', ['cashier_' . $mainId . '_' . $staffId . '_may_refresh', 'yes']);
  677. Yii::$app->redis->executeCommand('SET', ['ghs_cashier_' . $mainId . '_' . $staffId . '_may_refresh', 'yes']);
  678. }
  679. }
  680. }
  681. $transaction->commit();
  682. //入库成功通知相关人员,关键词 put_in_notice
  683. if ($needNotice) {
  684. $cg = PurchaseOrderClass::getLockById($targetId);
  685. WxMessageClass::ghsCgEntryInform($shop, $cg);
  686. }
  687. util::complete('操作成功');
  688. } catch (\Exception $e) {
  689. $transaction->rollBack();
  690. $msg = $e->getMessage();
  691. Yii::info("修改失败原因:" . $msg);
  692. noticeUtil::push("修改价格出现失败情况,原因:" . $msg, '15280215347');
  693. util::fail('修改失败');
  694. }
  695. }
  696. //多个花材同时改成一个价 ssh 20231003
  697. public function actionManyItemChangeOnePrice()
  698. {
  699. $shop = $this->shop;
  700. $join = $shop->join ?? 0;
  701. $default = $shop->default ?? 0;
  702. if ($join == 0 && $default == 0) {
  703. util::fail('当前直营分店,只能回总店修改价格');
  704. }
  705. $shopAdmin = $this->shopAdmin;
  706. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  707. util::fail('超管才能修改');
  708. }
  709. $post = Yii::$app->request->post();
  710. $idsList = $post['ids'] ?? '';
  711. $ids = json_decode($idsList, true);
  712. if (empty($ids)) {
  713. util::fail('请选择花材');
  714. }
  715. $list = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', null, true);
  716. if (empty($list)) {
  717. util::fail('请选择花材哦');
  718. }
  719. $mainId = $this->mainId;
  720. $adminId = $this->adminId ?? 0;
  721. $shop = $this->shop;
  722. $default = $shop->default ?? 0;
  723. $shopAdmin = $this->shopAdmin;
  724. $price = $post['price'] ?? 0;
  725. if (is_numeric($price) == false) {
  726. util::fail('批发价填写错误');
  727. }
  728. $skPrice = $post['skPrice'] ?? 0;
  729. if (is_numeric($skPrice) == false) {
  730. util::fail('散客价填写错误');
  731. }
  732. $hjPrice = $post['hjPrice'] ?? 0;
  733. if (is_numeric($hjPrice) == false) {
  734. util::fail('会员价填写错误');
  735. }
  736. if ($hjPrice > $price) {
  737. util::fail('会员价不能大于批发价');
  738. }
  739. if ($price > $skPrice) {
  740. util::fail('批发价不能大于零售价');
  741. }
  742. $sjId = $shop->sjId ?? 0;
  743. $chainShopList = [];
  744. if ($default == 1) {
  745. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId, 'join' => 0], null, '*', null, true);
  746. }
  747. //normal常规改价,cg采购改价
  748. $changeType = Yii::$app->request->post('changeType', 'normal');
  749. $targetId = 0;
  750. $changeParams = ['staffId' => $shopAdmin->id, 'staffName' => $shopAdmin->name, 'changeType' => $changeType, 'targetId' => $targetId];
  751. $connection = Yii::$app->db;
  752. $transaction = $connection->beginTransaction();
  753. try {
  754. foreach ($list as $product) {
  755. if ($product->mainId != $mainId) {
  756. util::fail('修改失败,不是你的花材');
  757. }
  758. $ptItemId = $product->itemId;
  759. ProductClass::changeSinglePrice($shop, $ptItemId, $price, $skPrice, $hjPrice, $changeParams);
  760. //在首店修改需要同步到所有直营店
  761. if ($default == 1 && isset($shop->dataSync) && $shop->dataSync == 1) {
  762. if (!empty($chainShopList)) {
  763. foreach ($chainShopList as $chain) {
  764. if ($chain->id == $shop->id) {
  765. continue;
  766. }
  767. $chainMainId = $chain->mainId ?? 0;
  768. $staff = ShopAdminClass::getByCondition(['mainId' => $chainMainId, 'adminId' => $adminId], true);
  769. $changeParams2 = ['staffId' => $staff->id, 'staffName' => $staff->name, 'changeType' => $changeType, 'targetId' => $targetId];
  770. ProductClass::changeSinglePrice($chain, $ptItemId, $price, $skPrice, $hjPrice, $changeParams2);
  771. }
  772. }
  773. }
  774. }
  775. if (!empty($mainId)) {
  776. //提醒零售收银台界面要刷新
  777. $staffList = ShopAdminClass::getAllByCondition(['mainId' => $mainId], null, '*', null, true);
  778. if (!empty($staffList)) {
  779. foreach ($staffList as $staff) {
  780. $staffId = $staff->id ?? 0;
  781. Yii::$app->redis->executeCommand('SET', ['cashier_' . $mainId . '_' . $staffId . '_may_refresh', 'yes']);
  782. Yii::$app->redis->executeCommand('SET', ['ghs_cashier_' . $mainId . '_' . $staffId . '_may_refresh', 'yes']);
  783. }
  784. }
  785. }
  786. $transaction->commit();
  787. util::complete('修改成功');
  788. } catch (\Exception $e) {
  789. $transaction->rollBack();
  790. Yii::info("修改失败原因:" . $e->getMessage());
  791. util::fail('修改失败');
  792. }
  793. }
  794. //改价
  795. public function actionChangePrice()
  796. {
  797. $shopAdmin = $this->shopAdmin;
  798. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  799. util::fail('超管才能修改');
  800. }
  801. $productId = Yii::$app->request->post('productId', 0);
  802. $productInfo = ProductClass::getById($productId, true);
  803. if (empty($productInfo)) {
  804. util::fail("花材不存在");
  805. }
  806. if ($productInfo->mainId != $this->mainId) {
  807. util::fail('没有权限');
  808. }
  809. $price = Yii::$app->request->post('price', '');
  810. if (is_numeric($price) && $price > 0) {
  811. ProductClass::changeSinglePrice($productInfo, $price, ProductClass::PRICE_LABEL_CHANGE);
  812. } else {
  813. $price = bcadd($productInfo->cost, $productInfo->addPrice, 2);
  814. ProductClass::changeSinglePrice($productInfo, $price, ProductClass::PRICE_LABEL_AUTO);
  815. }
  816. util::success(['price' => $price]);
  817. }
  818. //恢复自动调价
  819. public function actionAutoPrice()
  820. {
  821. $productId = Yii::$app->request->post('productId', 0);
  822. $productData = ProductClass::getById($productId);
  823. if (!$productData) {
  824. util::fail("花材不存在");
  825. }
  826. //2021.04.06改为用product中的成本价+加价
  827. $price = bcadd($productData['cost'], $productData['addPrice'], 2);
  828. ProductClass::changeSinglePrice($productId, $this->shopId, $price, ProductClass::PRICE_LABEL_AUTO);
  829. util::complete("操作成功");
  830. }
  831. //修改加价
  832. public function actionChangeAddPrice()
  833. {
  834. $productId = Yii::$app->request->post('productId', 0);
  835. $addPrice = Yii::$app->request->post('addPrice', '');
  836. if ($addPrice < 0) {
  837. util::fail("加价不能小于0");
  838. }
  839. $productData = ProductClass::getProductData($productId, $this->mainId);
  840. if (!$productData) {
  841. util::fail("花材不存在");
  842. }
  843. ProductClass::changeAddPrice($productId, $addPrice);
  844. util::complete();
  845. }
  846. //批量修改更多,包括排序、重量和保质期 ssh 20211211
  847. public function actionBatchChangeMore()
  848. {
  849. $shop = $this->shop;
  850. $default = $shop->default ?? 0;
  851. if ($default == 0) {
  852. util::fail('当前直营分店,请返回总店修改');
  853. }
  854. $shopAdmin = $this->shopAdmin;
  855. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  856. util::fail('超管才能操作');
  857. }
  858. $addData = Yii::$app->request->post('addData', '');
  859. if (empty($addData)) {
  860. util::fail('没有花材');
  861. }
  862. $itemData = json_decode($addData, true);
  863. if (is_array($itemData) == false) {
  864. util::fail('没有花材...');
  865. }
  866. $ids = array_column($itemData, 'id');
  867. $infoList = ProductClass::getByIds($ids, null, 'id');
  868. if (empty($infoList)) {
  869. util::fail('没有花材');
  870. }
  871. foreach ($infoList as $info) {
  872. if (isset($info['mainId']) == false || $info['mainId'] != $this->mainId) {
  873. util::fail('不是你的花材不能修改');
  874. }
  875. }
  876. //总店修改同步到所有门店
  877. $sjId = $shop->sjId ?? 0;
  878. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  879. foreach ($itemData as $key => $data) {
  880. $id = $data['id'] ?? 0;
  881. unset($data['id']);
  882. ProductClass::updateById($id, $data);
  883. $ptItemId = $infoList[$id]['itemId'] ?? 0;
  884. if (empty($ptItemId)) {
  885. continue;
  886. }
  887. if (isset($shop->default) && $shop->default == 1 && isset($shop->dataSync) && $shop->dataSync == 1) {
  888. foreach ($chainShopList as $chainShop) {
  889. $currentMainId = $chainShop->mainId ?? 0;
  890. if ($currentMainId == $shop->mainId) {
  891. //前面已经修改过了,不需要重复修改
  892. continue;
  893. }
  894. ProductClass::updateByCondition(['mainId' => $currentMainId, 'itemId' => $ptItemId], $data);
  895. }
  896. }
  897. }
  898. util::complete('修改成功');
  899. }
  900. //批量修改加价 ssh 20211211
  901. public function actionBatchChangeAddPrice()
  902. {
  903. $shop = $this->shop;
  904. $join = $shop->join ?? 0;
  905. $default = $shop->default ?? 0;
  906. if ($join == 0 && $default == 0) {
  907. util::fail('当前直营分店,请切回总店操作');
  908. }
  909. $shopAdmin = $this->shopAdmin;
  910. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  911. util::fail('超管才能操作');
  912. }
  913. $post = Yii::$app->request->post();
  914. $addData = $post['addData'] ?? '';
  915. if (empty($addData)) {
  916. util::fail('没有花材');
  917. }
  918. $itemData = json_decode($addData, true);
  919. if (is_array($itemData) == false) {
  920. util::fail('没有花材...');
  921. }
  922. $ids = array_column($itemData, 'id');
  923. $infoList = ProductClass::getByIds($ids, null, 'id');
  924. if (empty($infoList)) {
  925. util::fail('没有花材');
  926. }
  927. foreach ($infoList as $info) {
  928. if (isset($info['mainId']) == false || $info['mainId'] != $this->mainId) {
  929. util::fail('不能修改别人的花材');
  930. }
  931. }
  932. $appVersion = $post['appVersion'] ?? 0;
  933. if ($appVersion != 1) {
  934. util::fail('请先升级应用');
  935. }
  936. //总店修改同步到所有门店
  937. $sjId = $shop->sjId ?? 0;
  938. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  939. foreach ($itemData as $key => $data) {
  940. $id = $data['id'] ?? 0;
  941. unset($data['id']);
  942. ProductClass::updateById($id, $data);
  943. $ptItemId = $infoList[$id]['itemId'] ?? 0;
  944. if (empty($ptItemId)) {
  945. continue;
  946. }
  947. if (isset($shop->default) && $shop->default == 1 && isset($shop->dataSync) && $shop->dataSync == 1) {
  948. foreach ($chainShopList as $chainShop) {
  949. if (isset($chainShop->join) && $chainShop->join == 1) {
  950. continue;
  951. }
  952. $currentMainId = $chainShop->mainId ?? 0;
  953. if ($currentMainId == $shop->mainId) {
  954. //前面已经修改过了,不需要重复修改
  955. continue;
  956. }
  957. ProductClass::updateByCondition(['mainId' => $currentMainId, 'itemId' => $ptItemId], $data);
  958. }
  959. }
  960. }
  961. util::complete('修改成功');
  962. }
  963. public function actionUpdate()
  964. {
  965. $shopAdmin = $this->shopAdmin;
  966. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  967. util::fail('超管才能操作');
  968. }
  969. $shop = $this->shop;
  970. $join = $shop->join ?? 0;
  971. $default = $shop->default ?? 0;
  972. if ($join == 0 && $default == 0) {
  973. //因为盘点不能将花材盘成0,所以这边放出入口允许创始人在分店修改分店的花材库存
  974. if ($shopAdmin->super != 1) {
  975. util::fail('请先开通超管权限');
  976. }
  977. }
  978. $post = Yii::$app->request->post();
  979. $discountPrice = $post['discountPrice'] ?? 0;
  980. if ($discountPrice > 0) {
  981. if (isset($post['hjDiscountPrice']) == false || empty($post['hjDiscountPrice'])) {
  982. $addPrice = $post['addPrice'] ?? 0;
  983. $hjAddPrice = $post['hjAddPrice'] ?? 0;
  984. $skMore = $post['skMore'] ?? 0;
  985. $diff = bcsub($addPrice, $hjAddPrice, 2);
  986. if ($diff > 0 && $discountPrice > $diff) {
  987. $hjDiscountPrice = bcsub($discountPrice, $diff, 2);
  988. } else {
  989. $hjDiscountPrice = $discountPrice;
  990. }
  991. $diff = bcsub($skMore, $addPrice, 2);
  992. if ($diff > 0) {
  993. $skDiscountPrice = bcadd($diff, $discountPrice, 2);
  994. } else {
  995. $skDiscountPrice = $discountPrice;
  996. }
  997. $post['skDiscountPrice'] = $skDiscountPrice;
  998. $post['hjDiscountPrice'] = $hjDiscountPrice;
  999. }
  1000. $reachNum = $post['reachNum'] ?? 0;
  1001. $reachNumDiscount = $post['reachNumDiscount'] ?? 0;
  1002. if ($reachNum > 0 && $reachNumDiscount > 0) {
  1003. util::fail('特价和满减不能同时进行');
  1004. }
  1005. }
  1006. $post['adminId'] = $this->adminId;
  1007. $post['staffId'] = $this->shopAdmin->id;
  1008. $post['staffName'] = $this->shopAdmin->name;
  1009. ProductClass::updateProduct($post, $this->shop);
  1010. util::complete();
  1011. }
  1012. //上下架状态控制 ssh 20210713
  1013. public function actionStatusUpdate()
  1014. {
  1015. $post = Yii::$app->request->post();
  1016. $shopAdmin = $this->shopAdmin;
  1017. $roleId = $shopAdmin['roleId'] ?? 0;
  1018. $role = AdminRoleClass::getById($roleId);
  1019. $roleName = $role['roleName'] ?? '';
  1020. if ($roleName == '员工') {
  1021. util::fail('无法操作哦');
  1022. }
  1023. $id = $post['id'] ?? 0;
  1024. $status = $post['status'] ?? 1;
  1025. $product = ProductClass::getById($id, true);
  1026. if ($product->mainId != $this->mainId) {
  1027. util::fail('无法操作');
  1028. }
  1029. $product->status = $status;
  1030. if ($status == 2) {
  1031. $product->discountPrice = 0;
  1032. }
  1033. $product->save();
  1034. util::complete();
  1035. }
  1036. //花材展示状态控制 ssh 20231113
  1037. public function actionDelStatusUpdate()
  1038. {
  1039. $post = Yii::$app->request->post();
  1040. $shop = $this->shop;
  1041. $join = $shop->join ?? 0;
  1042. $default = $shop->default ?? 0;
  1043. if ($join == 0 && $default == 0) {
  1044. util::fail('请在首店操作');
  1045. }
  1046. $id = $post['id'] ?? 0;
  1047. $delStatus = $post['delStatus'] ?? 0;
  1048. $product = ProductClass::getById($id, true);
  1049. if ($product->mainId != $this->mainId) {
  1050. util::fail('不是你的花材哦!!');
  1051. }
  1052. $product->delStatus = $delStatus;
  1053. if ($delStatus == 1) {
  1054. $product->stock = 0;
  1055. $product->status = 2;
  1056. }
  1057. $product->save();
  1058. $ptItemId = $product->itemId ?? 0;
  1059. if (isset($shop->default) && $shop->default == 1 && isset($shop->dataSync) && $shop->dataSync == 1) {
  1060. //总店修改同步到所有门店
  1061. $sjId = $shop->sjId ?? 0;
  1062. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  1063. foreach ($chainShopList as $chainShop) {
  1064. $chainShopId = $chainShop->id ?? 0;
  1065. if (isset($chainShop->join) && $chainShop->join == 1) {
  1066. continue;
  1067. }
  1068. if ($chainShopId == $shop->id) {
  1069. continue;
  1070. }
  1071. $chainMainId = $chainShop->mainId ?? 0;
  1072. $chainProduct = ProductClass::getByCondition(['mainId' => $chainMainId, 'itemId' => $ptItemId], true);
  1073. if (!empty($chainProduct)) {
  1074. $chainProduct->delStatus = $delStatus;
  1075. if ($delStatus == 1) {
  1076. $chainProduct->stock = 0;
  1077. $chainProduct->status = 2;
  1078. }
  1079. $chainProduct->save();
  1080. }
  1081. }
  1082. }
  1083. util::complete();
  1084. }
  1085. //详情 shish 20240115
  1086. public function actionDetail()
  1087. {
  1088. $id = Yii::$app->request->get('id', 0);
  1089. $source = Yii::$app->request->get('source', '');
  1090. $respond = ProductClass::getItemInfo($id);
  1091. $ptItemId = $respond['itemId'] ?? 0;
  1092. $ptItemInfo = PtItemClass::getById($ptItemId);
  1093. $respond['ptItemInfo'] = $ptItemInfo;
  1094. //通知提醒抹除
  1095. if ($source == 'notify' && $respond['mainId'] == $this->mainId) {
  1096. $staff = $this->shopAdmin;
  1097. $staffId = $this->shopAdminId;
  1098. $notify = NotifyClass::getByCondition(['staffId' => $staffId, 'type' => 1, 'targetId' => $id], true);
  1099. if (!empty($notify)) {
  1100. if ($notify->read == 0) {
  1101. $notify->read = 1;
  1102. $notify->save();
  1103. $notifyNum = $staff->notifyNum;
  1104. if ($notifyNum > 0) {
  1105. $notifyNum--;
  1106. $staff->notifyNum = $notifyNum;
  1107. $staff->save();
  1108. }
  1109. }
  1110. }
  1111. }
  1112. //重复花材
  1113. $repeatList = ProductClass::getAllByCondition(['mainId' => $this->mainId, 'itemId' => $ptItemId, 'delStatus' => 0], null, '*', null, true);
  1114. $repeatId = 0;
  1115. if (!empty($repeatList)) {
  1116. foreach ($repeatList as $repeat) {
  1117. if ($repeat->id != $id) {
  1118. $repeatId = $repeat->id;
  1119. }
  1120. }
  1121. }
  1122. $respond['repeatId'] = $repeatId;
  1123. $cpItemList = CpItemClass::getAllByCondition(['productId' => $id], null, '*', 'cpId');
  1124. $cpInfo = [];
  1125. if (!empty($cpItemList)) {
  1126. $cpIds = array_column($cpItemList, 'cpId');
  1127. $cpList = CpClass::getByIds($cpIds);
  1128. if (!empty($cpList)) {
  1129. foreach ($cpList as $key => $val) {
  1130. $id = $val['id'] ?? 0;
  1131. $name = $val['name'] ?? '';
  1132. $cpInfo[] = ['cpId' => $id, 'cpName' => $name];
  1133. }
  1134. }
  1135. }
  1136. $respond['cpInfo'] = $cpInfo;
  1137. util::success($respond);
  1138. }
  1139. //新增花材 2021.6.21
  1140. public function actionAdd()
  1141. {
  1142. $post = Yii::$app->request->post();
  1143. //临时兼容,2023.7.1后可以删除
  1144. $appVersion = $post['appVersion'] ?? 0;
  1145. if ($appVersion != 1) {
  1146. util::fail('请先升级应用');
  1147. }
  1148. $discountPrice = $post['discountPrice'] ?? 0;
  1149. if ($discountPrice > 0) {
  1150. if (isset($post['hjDiscountPrice']) == false || empty($post['hjDiscountPrice'])) {
  1151. $addPrice = $post['addPrice'] ?? 0;
  1152. $hjAddPrice = $post['hjAddPrice'] ?? 0;
  1153. $skMore = $post['skMore'] ?? 0;
  1154. $diff = bcsub($addPrice, $hjAddPrice, 2);
  1155. if ($diff > 0 && $discountPrice > $diff) {
  1156. $hjDiscountPrice = bcsub($discountPrice, $diff, 2);
  1157. } else {
  1158. $hjDiscountPrice = $discountPrice;
  1159. }
  1160. $diff = bcsub($skMore, $addPrice, 2);
  1161. if ($diff > 0) {
  1162. $skDiscountPrice = bcadd($diff, $discountPrice, 2);
  1163. } else {
  1164. $skDiscountPrice = $discountPrice;
  1165. }
  1166. $post['skDiscountPrice'] = $skDiscountPrice;
  1167. $post['hjDiscountPrice'] = $hjDiscountPrice;
  1168. }
  1169. }
  1170. $shop = $this->shop;
  1171. $join = $shop->join ?? 0;
  1172. $default = $shop->default ?? 0;
  1173. if ($join == 0 && $default == 0) {
  1174. util::fail('当前直营分店,请切回总店添加花材');
  1175. }
  1176. $shopAdmin = $this->shopAdmin;
  1177. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  1178. util::fail('管理员才能添加花材');
  1179. }
  1180. $post['sjId'] = $this->sjId;
  1181. $post['adminId'] = $this->adminId;
  1182. $post['mainId'] = $this->mainId;
  1183. $price = $post['price'] ?? 0;
  1184. $addPrice = $post['addPrice'] ?? 0;
  1185. if ($addPrice > $price) {
  1186. util::fail('加价不能大于售价');
  1187. }
  1188. $weight = isset($post['weight']) && is_numeric($post['weight']) ? $post['weight'] : 0;
  1189. if ($weight <= 0) {
  1190. util::fail('请填写重量,最小0.1');
  1191. }
  1192. $post['staffName'] = $shopAdmin->name ?? '';
  1193. ProductClass::addProduct($post, $this->shop);
  1194. util::complete('添加成功');
  1195. }
  1196. //获取拼音缩写 ssh 20210707
  1197. public function actionPy()
  1198. {
  1199. $name = Yii::$app->request->get('name');
  1200. $py = stringUtil::py($name);
  1201. util::success(['py' => $py]);
  1202. }
  1203. //(客户端-批发商)复制花材
  1204. public function actionClone()
  1205. {
  1206. $post = Yii::$app->request->post();
  1207. $appVersion = $post['appVersion'] ?? 0;
  1208. if ($appVersion != 1) {
  1209. util::fail('请先升级应用');
  1210. }
  1211. $discountPrice = $post['discountPrice'] ?? 0;
  1212. if ($discountPrice > 0) {
  1213. if (isset($post['hjDiscountPrice']) == false || empty($post['hjDiscountPrice'])) {
  1214. $addPrice = $post['addPrice'] ?? 0;
  1215. $hjAddPrice = $post['hjAddPrice'] ?? 0;
  1216. $skMore = $post['skMore'] ?? 0;
  1217. $diff = bcsub($addPrice, $hjAddPrice, 2);
  1218. if ($diff > 0 && $discountPrice > $diff) {
  1219. $hjDiscountPrice = bcsub($discountPrice, $diff, 2);
  1220. } else {
  1221. $hjDiscountPrice = $discountPrice;
  1222. }
  1223. $diff = bcsub($skMore, $addPrice, 2);
  1224. if ($diff > 0) {
  1225. $skDiscountPrice = bcadd($diff, $discountPrice, 2);
  1226. } else {
  1227. $skDiscountPrice = $discountPrice;
  1228. }
  1229. $post['skDiscountPrice'] = $skDiscountPrice;
  1230. $post['hjDiscountPrice'] = $hjDiscountPrice;
  1231. }
  1232. }
  1233. $shop = $this->shop;
  1234. $join = $shop->join ?? 0;
  1235. $default = $shop->default ?? 0;
  1236. if ($join == 0 && $default == 0) {
  1237. util::fail('请在首店操作');
  1238. }
  1239. $shopAdmin = $this->shopAdmin;
  1240. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  1241. util::fail('管理员才能添加花材');
  1242. }
  1243. $post['sjId'] = $this->sjId;
  1244. $post['adminId'] = $this->adminId;
  1245. $post['mainId'] = $this->mainId;
  1246. $post['staffName'] = $shopAdmin->name ?? '';
  1247. $price = $post['price'] ?? 0;
  1248. if (empty($price) || $price <= 0) {
  1249. util::fail('请填写批发价');
  1250. }
  1251. $skPrice = $post['skPrice'] ?? 0;
  1252. if (empty($skPrice) || $skPrice <= 0) {
  1253. util::fail('请填写零售价');
  1254. }
  1255. $hjPrice = $post['hjPrice'] ?? 0;
  1256. if (empty($hjPrice) || $hjPrice <= 0) {
  1257. util::fail('请填写会员价');
  1258. }
  1259. $addPrice = $post['addPrice'] ?? 0;
  1260. if ($addPrice > $price) {
  1261. util::fail('批发价的加价不能大于售价');
  1262. }
  1263. //复制标识
  1264. $post['copy'] = 1;
  1265. //去除无用的字段
  1266. unset($post['viewNum']);
  1267. unset($post['actualSold']);
  1268. unset($post['itemId']);
  1269. unset($post['id']);
  1270. unset($post['delStatus']);
  1271. ProductClass::addProduct($post, $this->shop);
  1272. util::complete('复制成功');
  1273. }
  1274. //下载价格表 ssh 20210922
  1275. public function actionGeneratePriceTable()
  1276. {
  1277. $level = Yii::$app->request->get('level', 0);
  1278. $respond = ProductService::generatePriceTable($this->sjId, $level, $this->mainId);
  1279. $file = $respond['file'] ?? '';
  1280. $shortFile = $respond['shortFile'] ?? '';
  1281. $fileUrl = Yii::$app->params['ghsHost'] . $file;
  1282. util::success(['file' => $fileUrl, 'shortFile' => $shortFile]);
  1283. }
  1284. public function actionGenerateItemTable()
  1285. {
  1286. $level = Yii::$app->request->get('level', 0);
  1287. $respond = ProductService::generateItemTable($this->sjId, $level, $this->mainId);
  1288. $file = $respond['file'] ?? '';
  1289. $shortFile = $respond['shortFile'] ?? '';
  1290. $fileUrl = Yii::$app->params['ghsHost'] . $file;
  1291. util::success(['file' => $fileUrl, 'shortFile' => $shortFile]);
  1292. }
  1293. //下载条形码 lqh 20211227
  1294. public function actionGenerateBarcodeTable()
  1295. {
  1296. $respond = ProductService::generateBarcodeTable($this->sjId, $this->shopId, $this->mainId);
  1297. $file = $respond['file'] ?? '';
  1298. $shortFile = $respond['shortFile'] ?? '';
  1299. $fileUrl = Yii::$app->params['ghsHost'] . $file;
  1300. util::success(['file' => $fileUrl, 'shortFile' => $shortFile]);
  1301. }
  1302. //批量修改花材的分类 ssh 20230405
  1303. public function actionBatchModifyClass()
  1304. {
  1305. $post = Yii::$app->request->post();
  1306. $shop = $this->shop;
  1307. $join = $shop->join ?? 0;
  1308. $default = $shop->default ?? 0;
  1309. if ($join == 0 && $default == 0) {
  1310. util::fail('当前直营分店,请切回总店操作');
  1311. }
  1312. $classId = $post['classId'] ?? 0;
  1313. $class = ItemClassClass::getById($classId, true);
  1314. if (empty($class) || $class->mainId != $this->mainId) {
  1315. util::fail('请选择您自己的分类');
  1316. }
  1317. $connection = Yii::$app->db;
  1318. $transaction = $connection->beginTransaction();
  1319. try {
  1320. $className = $class->name ?? '';
  1321. $idsList = $post['ids'] ?? '';
  1322. $ids = json_decode($idsList, true);
  1323. if (empty($ids)) {
  1324. util::fail('请选择花材');
  1325. }
  1326. $list = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', null, true);
  1327. if (empty($list)) {
  1328. util::fail('请选择花材');
  1329. }
  1330. $ptItemList = [];
  1331. foreach ($list as $item) {
  1332. $name = $item->name ?? '';
  1333. if ($item->mainId != $this->mainId) {
  1334. util::fail($name . '不是您的花材');
  1335. }
  1336. $item->classId = $classId;
  1337. $item->save();
  1338. $ptItemId = $item->itemId ?? 0;
  1339. $ptItemList[] = $ptItemId;
  1340. }
  1341. $shop = $this->shop;
  1342. if (isset($shop->default) && $shop->default == 1 && isset($shop->dataSync) && $shop->dataSync == 1) {
  1343. //总店修改同步到所有门店
  1344. $sjId = $shop->sjId ?? 0;
  1345. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  1346. foreach ($chainShopList as $chainShop) {
  1347. $chainShopId = $chainShop->id ?? 0;
  1348. if ($chainShopId == $shop->id) {
  1349. continue;
  1350. }
  1351. $chainManId = $chainShop->mainId ?? 0;
  1352. $chainAllClassList = ItemClassClass::getAllByCondition(['mainId' => $chainManId], null, '*', null, true);
  1353. if (!empty($chainAllClassList)) {
  1354. foreach ($chainAllClassList as $chainClass) {
  1355. $chainName = $chainClass->name ?? '';
  1356. $chainClassId = $chainClass->id ?? 0;
  1357. if ($chainName == $className) {
  1358. $chainProductList = Productclass::getAllByCondition(['mainId' => $chainManId, 'itemId' => ['in', $ptItemList]], null, '*', null, true);
  1359. if (!empty($chainProductList)) {
  1360. foreach ($chainProductList as $currentProduct) {
  1361. $currentProduct->classId = $chainClassId;
  1362. $currentProduct->save();
  1363. }
  1364. }
  1365. }
  1366. }
  1367. }
  1368. }
  1369. }
  1370. $transaction->commit();
  1371. util::complete('修改成功');
  1372. } catch (\Exception $e) {
  1373. $transaction->rollBack();
  1374. Yii::info("修改失败原因:" . $e->getMessage());
  1375. util::fail('修改失败');
  1376. }
  1377. }
  1378. //修改属于成本还是收入选项 ssh 20240514
  1379. public function actionChangeBelongCost()
  1380. {
  1381. $get = Yii::$app->request->get();
  1382. $id = $get['id'] ?? 0;
  1383. $belongCost = $get['belongCost'] ?? 0;
  1384. $info = ProductClass::getById($id, true);
  1385. if (empty($info)) {
  1386. util::fail('没有找到花材');
  1387. }
  1388. if ($info->mainId != $this->mainId) {
  1389. util::fail('不是你的花材哦');
  1390. }
  1391. $info->belongCost = $belongCost;
  1392. $info->save();
  1393. //队列方式去处理
  1394. $redisKey = 'change_belong_cost_item_list';
  1395. $value = $id . '_' . $belongCost;
  1396. Yii::$app->redis->executeCommand('LPUSH', [$redisKey, $value]);
  1397. util::complete('修改成功');
  1398. }
  1399. }