ProductController.php 60 KB

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