ProductController.php 59 KB

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