ProductController.php 58 KB

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