ProductController.php 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  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\order\classes\PurchaseOrderClass;
  14. use bizGhs\product\classes\ProductClass;
  15. use bizGhs\product\models\Product;
  16. use bizGhs\product\services\ProductService;
  17. use bizGhs\shop\classes\ShopAdminClass;
  18. use bizGhs\stock\classes\StockInDayClass;
  19. use common\components\printUtil;
  20. use common\components\stringUtil;
  21. use common\components\util;
  22. use Yii;
  23. class ProductController extends BaseController
  24. {
  25. public $guestAccess = ['index'];
  26. //停止预售 ssh 20230224
  27. public function actionCancelPreSell()
  28. {
  29. $get = Yii::$app->request->get();
  30. $id = $get['id'] ?? 0;
  31. $info = ProductClass::getById($id, true);
  32. ProductClass::check($info, $this->mainId);
  33. if ($info->stock > 0) {
  34. util::fail('没有库存才能停止预售');
  35. }
  36. $info->presell = 0;
  37. $info->presellDate = '';
  38. $info->save();
  39. util::complete();
  40. }
  41. //修改预售 ssh 20221214
  42. public function actionChangePresell()
  43. {
  44. $post = Yii::$app->request->post();
  45. $id = $post['id'] ?? 0;
  46. $info = ProductClass::getById($id, true);
  47. ProductClass::check($info, $this->mainId);
  48. $str = $post['date'] ?? '';
  49. $presell = $post['presell'] ?? 0;
  50. $newStr = '';
  51. if ($presell == 1) {
  52. if (empty($str)) {
  53. util::fail('请填写预售日期');
  54. }
  55. $arr = json_decode($str, true);
  56. if (empty($arr)) {
  57. util::fail('请填写预售日期');
  58. }
  59. $new = [];
  60. foreach ($arr as $date) {
  61. $new[] = strtotime($date);
  62. }
  63. $new = array_unique(array_filter($new));
  64. asort($new);
  65. $newStr = implode(',', $new);
  66. }
  67. $info->presell = $presell;
  68. $info->presellDate = $newStr;
  69. $info->discountPrice = 0;
  70. $info->save();
  71. util::complete();
  72. }
  73. //取消特价 ssh 20220901
  74. public function actionCancelDiscount()
  75. {
  76. $get = Yii::$app->request->get();
  77. $id = $get['id'] ?? 0;
  78. $info = ProductClass::getById($id, true);
  79. ProductClass::check($info, $this->mainId);
  80. $info->discountPrice = 0;
  81. $info->save();
  82. util::complete();
  83. }
  84. //打印花材的标签 ssh 20220602
  85. public function actionPrint()
  86. {
  87. $get = Yii::$app->request->get();
  88. $id = $get['id'] ?? 0;
  89. $num = $get['num'] ?? 1;
  90. $type = $get['type'] ?? 0;
  91. $info = ProductClass::getById($id, true);
  92. if (empty($info)) {
  93. util::fail('没有找到花材');
  94. }
  95. $name = $info->name ?? '';
  96. $ptItemId = $info->itemId ?? 0;
  97. $mainId = $info->mainId ?? 0;
  98. $ext = $this->shopExt;
  99. $printLabelSn = $ext->printLabelSn ?? '';
  100. if (empty($printLabelSn)) {
  101. util::fail('请设置标签打印机');
  102. }
  103. $p = new printUtil($printLabelSn);
  104. $unit = $info->ratio . $info->smallUnit . '/' . $info->bigUnit;
  105. $p->times = $num;
  106. if (stringUtil::getWordNum($name) > 7) {
  107. $content = '<TEXT x="30" y="30" font="12" w="1" h="1" r="0">' . $name . '</TEXT>';
  108. } else {
  109. $content = '<TEXT x="30" y="30" font="12" w="2" h="2" r="0">' . $name . '</TEXT>';
  110. }
  111. //杭州斗南批发店
  112. if (isset($get['type']) == false && $this->shopId == 4608) {
  113. $content .= '<QR x="35" y="100" e="L" w="5">' . Yii::$app->params['mallHost'] . "/item/show-info?id=" . $id . '</QR>';
  114. $content .= '<TEXT x="40" y="270" font="12" w="1" h="1" r="0">扫码看价格</TEXT>';
  115. } else {
  116. if ($type == 0) {
  117. //打标签
  118. $content .= '<BC128 x="30" y="115" h="75" s="1" r="0" n="3" w="10">' . $ptItemId . '</BC128>';
  119. if (in_array($mainId, [52, 1459])) {
  120. $content .= '<TEXT x="30" y="250" font="12" w="2" h="2" r="0">惠雅鲜花</TEXT>';
  121. } else {
  122. $content .= '<TEXT x="30" y="250" font="12" w="2" h="2" r="0">' . $unit . '</TEXT>';
  123. }
  124. } else {
  125. //打价码
  126. $content .= '<QR x="35" y="100" e="L" w="5">' . Yii::$app->params['mallHost'] . "/item/show-info?id=" . $id . '</QR>';
  127. $content .= '<TEXT x="40" y="270" font="12" w="1" h="1" r="0">扫码看价格</TEXT>';
  128. }
  129. }
  130. $p->printLabelMsg($content);
  131. util::complete();
  132. }
  133. //取出今天有入库的花材 ssh 20221024
  134. public function actionStockIn()
  135. {
  136. $py = Yii::$app->request->get('py', '');
  137. $version = Yii::$app->request->get('version', 0);
  138. if ($version == 0) {
  139. util::fail('请先升级版本到1.0.70以上');
  140. }
  141. $classInfo = ItemClassClass::getGhsItemClassAll($this->mainId);
  142. $mainId = $this->mainId ?? 0;
  143. $where['mainId'] = $mainId;
  144. if ($py) {
  145. $pyName = strtolower($py);
  146. $where['py'] = $pyName;
  147. }
  148. $arr = StockInDayClass::getAllByCondition(['mainId' => $mainId], null, '*');
  149. if (empty($arr)) {
  150. util::fail('没有花材需要盘点');
  151. }
  152. $ids = array_column($arr, 'itemId');
  153. $ids = array_unique($ids);
  154. $where['id'] = ['in', $ids];
  155. $level = 1;
  156. $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';
  157. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  158. $itemInfoData = ProductClass::groupProductInfo($itemInfoData, $level);
  159. $data = ProductService::assembleData($classInfo, $itemInfoData, true);
  160. util::success($data);
  161. }
  162. //按分类列出所有花材
  163. public function actionShow()
  164. {
  165. $py = Yii::$app->request->get('py', '');
  166. $requestType = Yii::$app->request->get('requestType', 'common');
  167. //默认显示上架商品
  168. $status = Yii::$app->request->get('status', 1);
  169. //默认显示未删除商品
  170. $delStatus = Yii::$app->request->get('delStatus', 0);
  171. //获取所有当前供货商的分类 (全部、常用)
  172. //根据分类组装分类下的花材信息
  173. //中央ID
  174. $classInfo = ItemClassClass::getGhsItemClassAll($this->mainId);
  175. $where['mainId'] = $this->mainId;
  176. if ($py) {
  177. $pyName = strtolower($py);
  178. $where['py'] = $pyName;
  179. }
  180. //改价和全部有库存花材列表
  181. if ($requestType == 'changePrice' || $requestType == 'hasStockList') {
  182. $where['stock>'] = 0;
  183. }
  184. if (!empty($status)) {
  185. $where['status'] = $status;
  186. }
  187. $where['delStatus'] = $delStatus;
  188. //客户等级
  189. $customId = Yii::$app->request->get('customId', 0);
  190. $level = 1;
  191. if (!empty($customId)) {
  192. $custom = CustomClass::getById($customId, true);
  193. $level = $custom->level ?? 1;
  194. }
  195. $itemInfoData = [];
  196. if ($requestType == 'hasStockList') {
  197. //有库存的花材列表
  198. $field = 'id,py,name,classId,itemId,price,skPrice,stock';
  199. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  200. } elseif ($requestType == 'changePrice') {
  201. //修改价格列表
  202. $field = 'id,py,cover,name,cost,itemId,classId,addPrice,skPrice,discountPrice,skMore,variety,price,stock,presell';
  203. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  204. $itemInfoData = ProductClass::changePriceGroup($itemInfoData, $level);
  205. } elseif ($requestType == 'kd') {
  206. //开单的花材列表
  207. $field = 'id,py,cover,name,cost,itemId,classId,skPrice,variety,price,discountPrice,stock,stockWarning,presell,ratioType,smallUnit,smallRatio,bigUnit,ratio';
  208. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  209. $itemInfoData = ProductClass::kdItemGroup($itemInfoData, $level);
  210. } elseif ($requestType == 'itemList') {
  211. //花材列表
  212. $field = 'id,py,cover,name,cost,itemId,classId,skPrice,variety,price,discountPrice,skMore,stock,actualSold,stockWarning,status,presell';
  213. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  214. $itemInfoData = ProductClass::itemListGroup($itemInfoData, $level);
  215. } elseif ($requestType == 'outList') {
  216. //下架花材列表
  217. $where['status'] = 2;
  218. $where['delStatus'] = 0;
  219. $where['removeStatus'] = 0;
  220. $field = 'id,py,cover,name,cost,itemId,classId,addPrice,skPrice,bigUnit,smallUnit,ratio,ratioType,discountPrice,skMore,variety,price,stock,presell,status';
  221. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  222. $itemInfoData = ProductClass::simpleGroup($itemInfoData, $level);
  223. } elseif ($requestType == 'stopList') {
  224. //停用花材列表
  225. $where['delStatus'] = 1;
  226. $where['removeStatus'] = 0;
  227. $field = 'id,py,cover,name,cost,itemId,classId,addPrice,skPrice,bigUnit,smallUnit,ratio,ratioType,discountPrice,skMore,variety,price,stock,presell,status';
  228. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  229. $itemInfoData = ProductClass::simpleGroup($itemInfoData, $level);
  230. } elseif ($requestType == 'removeList') {
  231. //删除花材列表
  232. $where['delStatus'] = 1;
  233. $where['removeStatus'] = 1;
  234. $field = 'id,py,cover,name,cost,itemId,classId,addPrice,skPrice,bigUnit,smallUnit,ratio,ratioType,discountPrice,skMore,variety,price,stock,presell,status';
  235. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  236. $itemInfoData = ProductClass::simpleGroup($itemInfoData, $level);
  237. } elseif ($requestType == 'check') {
  238. //盘点花材列表
  239. $field = 'id,py,cover,name,itemId,classId,variety,price,discountPrice,stock,stockWarning,presell,ratioType,smallUnit,bigUnit,ratio';
  240. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  241. $itemInfoData = ProductClass::checkItemGroup($itemInfoData, $level);
  242. } elseif ($requestType == 'break') {
  243. //报损花材
  244. $field = 'id,py,cover,name,itemId,classId,variety,price,discountPrice,stock,stockWarning,presell,ratioType,smallUnit,bigUnit,ratio';
  245. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  246. $itemInfoData = ProductClass::breakItemGroup($itemInfoData, $level);
  247. } elseif ($requestType == 'part') {
  248. //报损花材
  249. $field = 'id,py,cover,name,itemId,classId,variety,price,discountPrice,stock,stockWarning,presell,ratioType,smallUnit,bigUnit,ratio';
  250. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  251. $itemInfoData = ProductClass::partItemGroup($itemInfoData, $level);
  252. } elseif ($requestType == 'stockOut') {
  253. //出库花材
  254. $field = 'id,py,cover,name,itemId,classId,variety,price,discountPrice,stock,stockWarning,presell,ratioType,smallUnit,bigUnit,ratio,smallRatio';
  255. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  256. $itemInfoData = ProductClass::stockOutGroup($itemInfoData, $level);
  257. } elseif ($requestType == 'cg') {
  258. //采购花材
  259. $field = 'id,py,cover,name,itemId,classId,variety,price,discountPrice,stock,presell,ratioType,smallUnit,bigUnit,ratio,weight';
  260. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  261. $itemInfoData = ProductClass::cgItemGroup($itemInfoData, $level);
  262. } elseif ($requestType == 'warning') {
  263. //库存预警
  264. $field = 'id,py,cover,name,cost,itemId,classId,skPrice,variety,price,discountPrice,stock,actualSold,presell,bigUnit,stockWarning,status';
  265. $itemInfoData = Product::find()->where("mainId={$this->mainId}")
  266. ->andWhere('`stock`<`stockWarning`')
  267. ->andWhere("delStatus=0")
  268. ->select($field)->asArray()->all();
  269. $itemInfoData = ProductClass::warningGroup($itemInfoData, $level);
  270. } else {
  271. util::fail('没有数据');
  272. }
  273. $data = ProductService::assembleItemData($classInfo, $itemInfoData);
  274. util::success($data);
  275. }
  276. //取出今天有入库的花材 ssh 20221224
  277. public function actionGetStockIn()
  278. {
  279. $py = Yii::$app->request->get('py', '');
  280. $version = Yii::$app->request->get('version', 0);
  281. if ($version == 0) {
  282. util::fail('请先升级版本到1.0.70以上');
  283. }
  284. $classInfo = ItemClassClass::getGhsItemClassAll($this->mainId);
  285. $mainId = $this->mainId ?? 0;
  286. $where['mainId'] = $mainId;
  287. if ($py) {
  288. $pyName = strtolower($py);
  289. $where['py'] = $pyName;
  290. }
  291. $arr = StockInDayClass::getAllByCondition(['mainId' => $mainId], null, '*');
  292. if (empty($arr)) {
  293. util::fail('没有花材需要盘点');
  294. }
  295. $ids = array_column($arr, 'itemId');
  296. $ids = array_unique($ids);
  297. $where['id'] = ['in', $ids];
  298. $level = 1;
  299. $field = 'id,py,cover,name,itemId,classId,variety,price,discountPrice,stock,stockWarning,presell,ratioType,smallUnit,bigUnit,ratio';
  300. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  301. $itemInfoData = ProductClass::checkItemGroup($itemInfoData, $level);
  302. $data = ProductService::assembleItemData($classInfo, $itemInfoData);
  303. util::success($data);
  304. }
  305. //按分类列出所有花材
  306. public function actionIndex()
  307. {
  308. $py = Yii::$app->request->get('py', '');
  309. $requestType = Yii::$app->request->get('requestType', 'common');
  310. //默认显示上架商品
  311. $status = Yii::$app->request->get('status', 1);
  312. //默认显示未删除商品
  313. $delStatus = Yii::$app->request->get('delStatus', 0);
  314. $warning = Yii::$app->request->get('warning', 0);
  315. //获取所有当前供货商的分类 (全部、常用)
  316. //根据分类组装分类下的花材信息
  317. //中央ID
  318. $classInfo = ItemClassClass::getGhsItemClassAll($this->mainId);
  319. $where['mainId'] = $this->mainId;
  320. if ($py) {
  321. $pyName = strtolower($py);
  322. $where['py'] = $pyName;
  323. }
  324. if ($requestType == 'hasStock') {
  325. //改价只显示库存大于0的
  326. $where['stock>'] = 0;
  327. }
  328. if (!empty($status)) {
  329. $where['status'] = $status;
  330. }
  331. $where['delStatus'] = $delStatus;
  332. //客户等级
  333. $customId = Yii::$app->request->get('customId', 0);
  334. $level = 1;
  335. if (!empty($customId)) {
  336. $custom = CustomClass::getById($customId, true);
  337. $level = $custom->level ?? 1;
  338. }
  339. if ($warning == 1) {
  340. //库存预警
  341. $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';
  342. $itemInfoData = Product::find()->where("mainId={$this->mainId}")
  343. ->andWhere('`stock`<`stockWarning`')
  344. ->andWhere("delStatus=0")
  345. ->select($field)->asArray()->all();
  346. } else {
  347. $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';
  348. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  349. }
  350. $itemInfoData = ProductClass::groupProductInfo($itemInfoData, $level);
  351. $data = ProductService::assembleData($classInfo, $itemInfoData, true);
  352. util::success($data);
  353. }
  354. //所有花材分页和不分页
  355. public function actionList()
  356. {
  357. $where = [];
  358. $classId = Yii::$app->request->get('classId', 0);
  359. if (!empty($classId)) {
  360. $where['classId'] = $classId;
  361. }
  362. $name = Yii::$app->request->get('name', '');
  363. if (empty($name)) {
  364. $py = Yii::$app->request->get('py', '');
  365. $name = !empty($py) ? $py : '';
  366. }
  367. if (!empty($name)) {
  368. if (stringUtil::isLetter($name)) {
  369. $where['py'] = ['like', $name];
  370. } else {
  371. $where['name'] = ['like', $name];
  372. }
  373. }
  374. $status = Yii::$app->request->get('status', 0);
  375. if (!empty($status)) {
  376. $where['status'] = $status;
  377. }
  378. $delStatus = Yii::$app->request->get('delStatus', 0);
  379. if ($delStatus != 2) {
  380. $where['delStatus'] = $delStatus;
  381. }
  382. $where['mainId'] = $this->mainId;
  383. $productIds = Yii::$app->request->get('ids', '');
  384. if (!empty($productIds)) {
  385. $productIdArr = explode(',', $productIds);
  386. if (!empty($productIdArr)) {
  387. $where['id'] = ['in', $productIdArr];
  388. }
  389. }
  390. //输出标准会员价,即花店价,默认显示的是花店价
  391. $level = 1;
  392. $type = Yii::$app->request->get('type', '');
  393. $showPage = Yii::$app->request->get('showPage', 0);
  394. if ($showPage) {
  395. if ($type == 'waring') {
  396. $pro = Product::find()->where("mainId={$this->mainId}")->andWhere('`stock`<`stockWarning`')->select('id')->asArray()->all();
  397. $lackIds = array_column($pro, 'id');
  398. if (empty($lackIds)) {
  399. util::success([]);
  400. }
  401. $lackIds = array_filter(array_unique($lackIds));
  402. $where['id'] = ['in', $lackIds];
  403. $respond = ProductClass::getList("*", $where, "actualSold desc");
  404. } else {
  405. $respond = ProductClass::getList("*", $where, "inTurn desc");
  406. }
  407. $respond['list'] = ProductClass::groupProductInfo($respond['list'], $level);
  408. $respond['list'] = ProductClass::groupClassName($this->sjId, $respond['list']);
  409. util::success($respond);
  410. } else {
  411. if ($type == 'waring') {
  412. //库存预警 按库存从小到大排序
  413. $data = ProductClass::getAllByCondition($where, ['stock' => SORT_ASC, 'inTurn' => SORT_DESC], "*");
  414. } else {
  415. if (!empty($productIds)) {
  416. //优先按 $productIds 排序 "FIELD(`id`,4,5,6)"=>true
  417. $data = ProductClass::getAllByCondition($where, ["FIELD(`id`,$productIds)" => true, 'inTurn' => SORT_DESC], "*");
  418. } else {
  419. $data = ProductClass::getAllByCondition($where, ['actualSold' => SORT_DESC, 'inTurn' => SORT_DESC], "*");
  420. }
  421. }
  422. $data = ProductClass::groupProductInfo($data, $level);
  423. $data = ProductClass::groupClassName($this->sjId, $data);
  424. if ($type == 'waring') {
  425. //库存预警
  426. $newData = [];
  427. foreach ($data as $v) {
  428. if ($v['stock'] <= $v['stockWarning']) {
  429. $newData[] = $v;
  430. }
  431. }
  432. util::success(['list' => $newData]);
  433. }
  434. util::success(['list' => $data]);
  435. }
  436. }
  437. //所有花材
  438. public function actionAll()
  439. {
  440. $where = [];
  441. $where['mainId'] = $this->mainId;
  442. $classId = Yii::$app->request->get('classId', 0);
  443. if (!empty($classId)) {
  444. $where['classId'] = $classId;
  445. }
  446. $status = Yii::$app->request->get('status', 0);
  447. if (!empty($status)) {
  448. $where['status'] = $status;
  449. }
  450. $delStatus = Yii::$app->request->get('delStatus', 0);
  451. if ($delStatus != 2) {
  452. $where['delStatus'] = $delStatus;
  453. }
  454. //输出标准会员价,即花店价,默认显示的是花店价
  455. $level = 1;
  456. $data = ProductClass::getAllByCondition($where, ['actualSold' => SORT_DESC, 'inTurn' => SORT_DESC], "*");
  457. $data = ProductClass::groupProductInfo($data, $level);
  458. util::success(['list' => $data]);
  459. }
  460. //批量改价
  461. public function actionBatchChangePrice()
  462. {
  463. $shop = $this->shop;
  464. $join = $shop->join ?? 0;
  465. $default = $shop->default ?? 0;
  466. if ($join == 0 && $default == 0) {
  467. util::fail('请在默认门店修改');
  468. }
  469. $shopAdmin = $this->shopAdmin;
  470. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  471. util::fail('超管才能修改');
  472. }
  473. $shop = $this->shop;
  474. $default = $shop->default ?? 0;
  475. $adminId = $this->adminId ?? 0;
  476. //normal常规改价,cg采购改价
  477. $changeType = Yii::$app->request->post('changeType', 'normal');
  478. $targetId = Yii::$app->request->post('targetId', 0);
  479. $changeParams = ['staffId' => $shopAdmin->id, 'staffName' => $shopAdmin->name, 'changeType' => $changeType, 'targetId' => $targetId];
  480. $data = Yii::$app->request->post('data', '');
  481. if (empty($data)) {
  482. util::fail('没有修改');
  483. }
  484. $arr = json_decode($data, true);
  485. if (empty($arr)) {
  486. util::fail('没有修改哦');
  487. }
  488. $connection = Yii::$app->db;
  489. $transaction = $connection->beginTransaction();
  490. try {
  491. $cg = PurchaseOrderClass::getLockById($targetId);
  492. if (!empty($cg)) {
  493. if (isset($cg->mainId) == false || $cg->mainId != $this->mainId) {
  494. util::fail('不是您的采购单');
  495. }
  496. //批量改价同时入库
  497. if ($cg->status == PurchaseOrderClass::PURCHASE_ORDER_STATUS_SENDING) {
  498. $staff = $this->shopAdmin;
  499. $data = [];
  500. $data['staffId'] = $staff->id ?? 0;
  501. $data['staffName'] = $staff->name ?? '';
  502. $data['adminId'] = $this->adminId ?? 0;
  503. PurchaseOrderClass::putIn($cg, $data);
  504. }
  505. }
  506. $ids = array_column($arr, 'id');
  507. $ids = array_unique(array_filter($ids));
  508. if (empty($ids)) {
  509. util::fail('请选择花材');
  510. }
  511. $sjId = $shop->sjId ?? 0;
  512. $chainShopList = [];
  513. if ($default == 1) {
  514. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId, 'join' => 0], null, '*', null, true);
  515. }
  516. $productList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id', true);
  517. foreach ($arr as $product) {
  518. $productId = $product['id'];
  519. $productInfo = $productList[$productId] ?? [];
  520. if (empty($productInfo)) {
  521. util::fail('没有找到花材');
  522. }
  523. $productName = $productInfo->name ?? '';
  524. if (isset($product['id']) == false) {
  525. util::fail('没有花材');
  526. }
  527. if (isset($product['price']) == false || is_numeric($product['price']) == false || $product['price'] < 0) {
  528. util::fail('请填写' . $productName . '的花店价');
  529. }
  530. $price = $product['price'];
  531. if (isset($product['skPrice']) == false || is_numeric($product['skPrice']) == false || $product['skPrice'] < 0) {
  532. util::fail('请填写' . $productName . '的散客价');
  533. }
  534. $skPrice = $product['skPrice'];
  535. if (isset($productInfo->mainId) == false || $productInfo->mainId != $this->mainId) {
  536. util::fail('没有权限访问');
  537. }
  538. if (empty($price)) {
  539. continue;
  540. }
  541. $ptItemId = $productInfo->itemId;
  542. ProductClass::changeSinglePrice($shop, $ptItemId, $price, $skPrice, $changeParams);
  543. //在首店修改需要同步到所有直营店
  544. if ($default == 1) {
  545. foreach ($chainShopList as $chain) {
  546. if ($chain->id == $shop->id) {
  547. continue;
  548. }
  549. $chainMainId = $chain->mainId ?? 0;
  550. $staff = ShopAdminClass::getByCondition(['mainId' => $chainMainId, 'adminId' => $adminId], true);
  551. $changeParams2 = ['staffId' => $staff->id, 'staffName' => $staff->name, 'changeType' => $changeType, 'targetId' => $targetId];
  552. ProductClass::changeSinglePrice($chain, $ptItemId, $price, $skPrice, $changeParams2);
  553. }
  554. }
  555. }
  556. $mainId = $shop->mainId ?? 0;
  557. if (!empty($mainId)) {
  558. //提醒零售收银台界面要刷新
  559. $staffList = ShopAdminClass::getAllByCondition(['mainId' => $mainId], null, '*', null, true);
  560. if (!empty($staffList)) {
  561. foreach ($staffList as $staff) {
  562. $staffId = $staff->id ?? 0;
  563. Yii::$app->redis->executeCommand('SET', ['cashier_' . $mainId . '_' . $staffId . '_may_refresh', 'yes']);
  564. }
  565. }
  566. }
  567. $transaction->commit();
  568. util::complete('操作成功');
  569. } catch (\Exception $e) {
  570. $transaction->rollBack();
  571. Yii::info("修改失败原因:" . $e->getMessage());
  572. util::fail('修改失败');
  573. }
  574. }
  575. //改价
  576. public function actionChangePrice()
  577. {
  578. $shopAdmin = $this->shopAdmin;
  579. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  580. util::fail('超管才能修改');
  581. }
  582. $productId = Yii::$app->request->post('productId', 0);
  583. $productInfo = ProductClass::getById($productId, true);
  584. if (empty($productInfo)) {
  585. util::fail("花材不存在");
  586. }
  587. if ($productInfo->mainId != $this->mainId) {
  588. util::fail('没有权限');
  589. }
  590. $price = Yii::$app->request->post('price', '');
  591. if (is_numeric($price) && $price > 0) {
  592. ProductClass::changeSinglePrice($productInfo, $price, ProductClass::PRICE_LABEL_CHANGE);
  593. } else {
  594. $price = bcadd($productInfo->cost, $productInfo->addPrice, 2);
  595. ProductClass::changeSinglePrice($productInfo, $price, ProductClass::PRICE_LABEL_AUTO);
  596. }
  597. util::success(['price' => $price]);
  598. }
  599. //恢复自动调价
  600. public function actionAutoPrice()
  601. {
  602. $productId = Yii::$app->request->post('productId', 0);
  603. $productData = ProductClass::getById($productId);
  604. if (!$productData) {
  605. util::fail("花材不存在");
  606. }
  607. //2021.04.06改为用product中的成本价+加价
  608. $price = bcadd($productData['cost'], $productData['addPrice'], 2);
  609. ProductClass::changeSinglePrice($productId, $this->shopId, $price, ProductClass::PRICE_LABEL_AUTO);
  610. util::complete("操作成功");
  611. }
  612. //修改加价
  613. public function actionChangeAddPrice()
  614. {
  615. $productId = Yii::$app->request->post('productId', 0);
  616. $addPrice = Yii::$app->request->post('addPrice', '');
  617. if ($addPrice < 0) {
  618. util::fail("加价不能小于0");
  619. }
  620. $productData = ProductClass::getProductData($productId, $this->mainId);
  621. if (!$productData) {
  622. util::fail("花材不存在");
  623. }
  624. ProductClass::changeAddPrice($productId, $addPrice);
  625. util::complete();
  626. }
  627. //批量修改更多,包括排序、重量和保质期 ssh 20211211
  628. public function actionBatchChangeMore()
  629. {
  630. $shop = $this->shop;
  631. $default = $shop->default ?? 0;
  632. if ($default == 0) {
  633. util::fail('请在首店修改');
  634. }
  635. $shopAdmin = $this->shopAdmin;
  636. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  637. util::fail('超管才能操作');
  638. }
  639. $addData = Yii::$app->request->post('addData', '');
  640. if (empty($addData)) {
  641. util::fail('没有花材');
  642. }
  643. $itemData = json_decode($addData, true);
  644. if (is_array($itemData) == false) {
  645. util::fail('没有花材...');
  646. }
  647. $ids = array_column($itemData, 'id');
  648. $infoList = ProductClass::getByIds($ids, null, 'id');
  649. if (empty($infoList)) {
  650. util::fail('没有花材');
  651. }
  652. foreach ($infoList as $info) {
  653. if (isset($info['mainId']) == false || $info['mainId'] != $this->mainId) {
  654. util::fail('不是你的花材不能修改');
  655. }
  656. }
  657. //首店修改同步到所有直营店
  658. $sjId = $shop->sjId ?? 0;
  659. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  660. foreach ($itemData as $key => $data) {
  661. $id = $data['id'] ?? 0;
  662. unset($data['id']);
  663. ProductClass::updateById($id, $data);
  664. $ptItemId = $infoList[$id]['itemId'] ?? 0;
  665. if (empty($ptItemId)) {
  666. continue;
  667. }
  668. if (isset($shop->default) && $shop->default == 1) {
  669. foreach ($chainShopList as $chainShop) {
  670. $currentMainId = $chainShop->mainId ?? 0;
  671. if ($currentMainId == $shop->mainId) {
  672. //前面已经修改过了,不需要重复修改
  673. continue;
  674. }
  675. ProductClass::updateByCondition(['mainId' => $currentMainId, 'itemId' => $ptItemId], $data);
  676. }
  677. }
  678. }
  679. util::complete('修改成功');
  680. }
  681. //批量修改加价 ssh 20211211
  682. public function actionBatchChangeAddPrice()
  683. {
  684. $shop = $this->shop;
  685. $join = $shop->join ?? 0;
  686. $default = $shop->default ?? 0;
  687. if ($join == 0 && $default == 0) {
  688. util::fail('请在首店操作');
  689. }
  690. $shopAdmin = $this->shopAdmin;
  691. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  692. util::fail('超管才能操作');
  693. }
  694. $addData = Yii::$app->request->post('addData', '');
  695. if (empty($addData)) {
  696. util::fail('没有花材');
  697. }
  698. $itemData = json_decode($addData, true);
  699. if (is_array($itemData) == false) {
  700. util::fail('没有花材...');
  701. }
  702. $ids = array_column($itemData, 'id');
  703. $infoList = ProductClass::getByIds($ids, null, 'id');
  704. if (empty($infoList)) {
  705. util::fail('没有花材');
  706. }
  707. foreach ($infoList as $info) {
  708. if (isset($info['mainId']) == false || $info['mainId'] != $this->mainId) {
  709. util::fail('不能修改别人的花材');
  710. }
  711. }
  712. //首店修改同步到所有直营店
  713. $sjId = $shop->sjId ?? 0;
  714. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  715. foreach ($itemData as $key => $data) {
  716. $id = $data['id'] ?? 0;
  717. unset($data['id']);
  718. ProductClass::updateById($id, $data);
  719. $ptItemId = $infoList[$id]['itemId'] ?? 0;
  720. if (empty($ptItemId)) {
  721. continue;
  722. }
  723. if (isset($shop->default) && $shop->default == 1) {
  724. foreach ($chainShopList as $chainShop) {
  725. if (isset($chainShop->join) && $chainShop->join == 1) {
  726. continue;
  727. }
  728. $currentMainId = $chainShop->mainId ?? 0;
  729. if ($currentMainId == $shop->mainId) {
  730. //前面已经修改过了,不需要重复修改
  731. continue;
  732. }
  733. ProductClass::updateByCondition(['mainId' => $currentMainId, 'itemId' => $ptItemId], $data);
  734. }
  735. }
  736. }
  737. util::complete('修改成功');
  738. }
  739. public function actionUpdate()
  740. {
  741. $shopAdmin = $this->shopAdmin;
  742. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  743. util::fail('超管才能操作');
  744. }
  745. $shop = $this->shop;
  746. $join = $shop->join ?? 0;
  747. $default = $shop->default ?? 0;
  748. if ($join == 0 && $default == 0) {
  749. //因为盘点不能将花材盘成0,所以这边放出入口允许创始人在分店修改分店的花材库存
  750. if ($shopAdmin->super != 1) {
  751. util::fail('请先开通超管权限');
  752. }
  753. }
  754. $post = Yii::$app->request->post();
  755. $post['adminId'] = $this->adminId;
  756. $post['staffId'] = $this->shopAdmin->id;
  757. $post['staffName'] = $this->shopAdmin->name;
  758. ProductClass::updateProduct($post, $this->shop);
  759. util::complete();
  760. }
  761. //上下架状态控制 ssh 20210713
  762. public function actionStatusUpdate()
  763. {
  764. $post = Yii::$app->request->post();
  765. $shopAdmin = $this->shopAdmin;
  766. $roleId = $shopAdmin['roleId'] ?? 0;
  767. $role = AdminRoleClass::getById($roleId);
  768. $roleName = $role['roleName'] ?? '';
  769. if ($roleName == '员工') {
  770. util::fail('没有权限操作哦');
  771. }
  772. $id = $post['id'] ?? 0;
  773. $status = $post['status'] ?? 1;
  774. $product = ProductClass::getById($id, true);
  775. if ($product->mainId != $this->mainId) {
  776. util::fail('没有权限操作');
  777. }
  778. $product->status = $status;
  779. if ($status == 2) {
  780. $product->discountPrice = 0;
  781. }
  782. $product->save();
  783. util::complete();
  784. }
  785. //花材展示状态控制 ssh 20210804
  786. public function actionDelStatusUpdate()
  787. {
  788. $post = Yii::$app->request->post();
  789. $shop = $this->shop;
  790. $join = $shop->join ?? 0;
  791. $default = $shop->default ?? 0;
  792. if ($join == 0 && $default == 0) {
  793. util::fail('请在首店操作');
  794. }
  795. $shopAdmin = $this->shopAdmin;
  796. $roleId = $shopAdmin['roleId'] ?? 0;
  797. $role = AdminRoleClass::getById($roleId);
  798. $roleName = $role['roleName'] ?? '';
  799. if ($roleName == '员工') {
  800. util::fail('没有权限操作');
  801. }
  802. $id = $post['id'] ?? 0;
  803. $delStatus = $post['delStatus'] ?? 0;
  804. $product = ProductClass::getById($id, true);
  805. if ($product->mainId != $this->mainId) {
  806. util::fail('没有权限操作');
  807. }
  808. $product->delStatus = $delStatus;
  809. $product->save();
  810. $ptItemId = $product->itemId ?? 0;
  811. if (isset($shop->default) && $shop->default == 1) {
  812. //首店修改同步到所有直营店
  813. $sjId = $shop->sjId ?? 0;
  814. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  815. foreach ($chainShopList as $chainShop) {
  816. $chainShopId = $chainShop->id ?? 0;
  817. if (isset($chainShop->join) && $chainShop->join == 1) {
  818. continue;
  819. }
  820. if ($chainShopId == $shop->id) {
  821. continue;
  822. }
  823. $chainMainId = $chainShop->mainId ?? 0;
  824. $chainProduct = ProductClass::getByCondition(['mainId' => $chainMainId, 'itemId' => $ptItemId], true);
  825. if (!empty($chainProduct)) {
  826. $chainProduct->delStatus = $delStatus;
  827. $chainProduct->save();
  828. }
  829. }
  830. }
  831. util::complete();
  832. }
  833. //详情 ruidian 2021.5.3
  834. public function actionDetail()
  835. {
  836. $id = Yii::$app->request->get('id', 0);
  837. $respond = ProductClass::getItemInfo($id);
  838. $ptItemId = $respond['itemId'] ?? 0;
  839. $ptItemInfo = PtItemClass::getById($ptItemId);
  840. $respond['ptItemInfo'] = $ptItemInfo;
  841. util::success($respond);
  842. }
  843. //新增花材 2021.6.21
  844. public function actionAdd()
  845. {
  846. $post = Yii::$app->request->post();
  847. $shop = $this->shop;
  848. $default = $shop->default ?? 0;
  849. if ($default == 0) {
  850. util::fail('请在默认门店添加花材,分店只能修改花材');
  851. }
  852. $shopAdmin = $this->shopAdmin;
  853. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  854. util::fail('管理员才能添加花材');
  855. }
  856. $post['sjId'] = $this->sjId;
  857. $post['adminId'] = $this->adminId;
  858. $post['mainId'] = $this->mainId;
  859. $price = $post['price'] ?? 0;
  860. $addPrice = $post['addPrice'] ?? 0;
  861. if ($addPrice > $price) {
  862. util::fail('加价不能大于售价');
  863. }
  864. $weight = isset($post['weight']) && is_numeric($post['weight']) ? $post['weight'] : 0;
  865. if ($weight <= 0) {
  866. util::fail('请填写重量,最小0.1');
  867. }
  868. $post['staffName'] = $shopAdmin->name ?? '';
  869. ProductClass::addProduct($post, $this->shop);
  870. util::complete('添加成功');
  871. }
  872. //获取拼音缩写 ssh 20210707
  873. public function actionPy()
  874. {
  875. $name = Yii::$app->request->get('name');
  876. $py = stringUtil::py($name);
  877. util::success(['py' => $py]);
  878. }
  879. //(客户端-批发商)复制花材
  880. public function actionClone()
  881. {
  882. $post = Yii::$app->request->post();
  883. $shop = $this->shop;
  884. $join = $shop->join ?? 0;
  885. $default = $shop->default ?? 0;
  886. if ($join == 0 && $default == 0) {
  887. util::fail('请在首店操作');
  888. }
  889. $shopAdmin = $this->shopAdmin;
  890. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  891. util::fail('管理员才能添加花材');
  892. }
  893. $shop = $this->shop;
  894. $default = $shop->default ?? 0;
  895. if ($default != 1) {
  896. util::fail('请在总店添加');
  897. }
  898. $post['sjId'] = $this->sjId;
  899. $post['adminId'] = $this->adminId;
  900. $post['mainId'] = $this->mainId;
  901. $post['staffName'] = $shopAdmin->name ?? '';
  902. $price = $post['price'] ?? 0;
  903. $addPrice = $post['addPrice'] ?? 0;
  904. if ($addPrice > $price) {
  905. util::fail('加价不能大于售价');
  906. }
  907. //复制标识
  908. $post['copy'] = 1;
  909. //去除无用的字段
  910. unset($post['viewNum']);
  911. unset($post['actualSold']);
  912. unset($post['itemId']);
  913. unset($post['id']);
  914. unset($post['delStatus']);
  915. ProductClass::addProduct($post, $this->shop);
  916. util::complete('复制成功');
  917. }
  918. //下载价格表 ssh 20210922
  919. public function actionGeneratePriceTable()
  920. {
  921. $level = Yii::$app->request->get('level', 0);
  922. $respond = ProductService::generatePriceTable($this->sjId, $level, $this->mainId);
  923. $file = $respond['file'] ?? '';
  924. $shortFile = $respond['shortFile'] ?? '';
  925. $fileUrl = Yii::$app->params['ghsHost'] . $file;
  926. util::success(['file' => $fileUrl, 'shortFile' => $shortFile]);
  927. }
  928. public function actionGenerateItemTable()
  929. {
  930. $level = Yii::$app->request->get('level', 0);
  931. $respond = ProductService::generateItemTable($this->sjId, $level, $this->mainId);
  932. $file = $respond['file'] ?? '';
  933. $shortFile = $respond['shortFile'] ?? '';
  934. $fileUrl = Yii::$app->params['ghsHost'] . $file;
  935. util::success(['file' => $fileUrl, 'shortFile' => $shortFile]);
  936. }
  937. //下载条形码 lqh 20211227
  938. public function actionGenerateBarcodeTable()
  939. {
  940. $respond = ProductService::generateBarcodeTable($this->sjId, $this->shopId, $this->mainId);
  941. $file = $respond['file'] ?? '';
  942. $shortFile = $respond['shortFile'] ?? '';
  943. $fileUrl = Yii::$app->params['ghsHost'] . $file;
  944. util::success(['file' => $fileUrl, 'shortFile' => $shortFile]);
  945. }
  946. //批量修改花材的分类 ssh 20230405
  947. public function actionBatchModifyClass()
  948. {
  949. $post = Yii::$app->request->post();
  950. $classId = $post['classId'] ?? 0;
  951. $class = ItemClassClass::getById($classId, true);
  952. if (empty($class) || $class->mainId != $this->mainId) {
  953. util::fail('请选择您自己的分类');
  954. }
  955. $idsList = $post['ids'] ?? '';
  956. $ids = json_decode($idsList, true);
  957. if (empty($ids)) {
  958. util::fail('请选择花材');
  959. }
  960. $list = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', null, true);
  961. if (empty($list)) {
  962. util::fail('请选择花材');
  963. }
  964. foreach ($list as $item) {
  965. $name = $item->name ?? '';
  966. if ($item->mainId != $this->mainId) {
  967. util::fail($name . '不是您的花材');
  968. }
  969. $item->classId = $classId;
  970. $item->save();
  971. }
  972. util::complete();
  973. }
  974. }