ProductController.php 46 KB

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