ProductController.php 59 KB

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