ProductController.php 60 KB

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