ProductController.php 58 KB

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