ProductController.php 60 KB

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