ProductController.php 58 KB

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