ProductController.php 57 KB

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