ProductController.php 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  1. <?php
  2. namespace hd\controllers;
  3. use biz\ghs\classes\GhsClass;
  4. use biz\item\classes\PtItemClass;
  5. use biz\shop\classes\ShopClass;
  6. use biz\shop\classes\ShopExtClass;
  7. use bizGhs\item\classes\ItemClassClass;
  8. use bizGhs\product\classes\ProductClass;
  9. use bizGhs\product\services\ProductService;
  10. use bizHd\admin\classes\ShopAdminClass;
  11. use bizHd\item\classes\UnitChangeClass;
  12. use bizHd\purchase\classes\PurchaseClass;
  13. use bizHd\purchase\classes\PurchaseItemClass;
  14. use bizHd\pictext\classes\PicTextGhsItemClass;
  15. use common\components\printUtil;
  16. use common\components\stringUtil;
  17. use common\components\util;
  18. use Yii;
  19. use yii\helpers\ArrayHelper;
  20. class ProductController extends BaseController
  21. {
  22. public $guestAccess = ['ghs-item', 'ghs-product-detail'];
  23. //复制新建花材 ssh 20250523
  24. public function actionClone()
  25. {
  26. $post = Yii::$app->request->post();
  27. $appVersion = $post['appVersion'] ?? 0;
  28. if ($appVersion != 1) {
  29. util::fail('请先升级应用');
  30. }
  31. $discountPrice = $post['discountPrice'] ?? 0;
  32. if ($discountPrice > 0) {
  33. if (isset($post['hjDiscountPrice']) == false || empty($post['hjDiscountPrice'])) {
  34. $addPrice = $post['addPrice'] ?? 0;
  35. $hjAddPrice = $post['hjAddPrice'] ?? 0;
  36. $skMore = $post['skMore'] ?? 0;
  37. $diff = bcsub($addPrice, $hjAddPrice, 2);
  38. if ($diff > 0 && $discountPrice > $diff) {
  39. $hjDiscountPrice = bcsub($discountPrice, $diff, 2);
  40. } else {
  41. $hjDiscountPrice = $discountPrice;
  42. }
  43. $diff = bcsub($skMore, $addPrice, 2);
  44. if ($diff > 0) {
  45. $skDiscountPrice = bcadd($diff, $discountPrice, 2);
  46. } else {
  47. $skDiscountPrice = $discountPrice;
  48. }
  49. $post['skDiscountPrice'] = $skDiscountPrice;
  50. $post['hjDiscountPrice'] = $hjDiscountPrice;
  51. }
  52. }
  53. $shop = $this->shop;
  54. $join = $shop->join ?? 0;
  55. $default = $shop->default ?? 0;
  56. if ($join == 0 && $default == 0) {
  57. util::fail('请在首店操作');
  58. }
  59. $shopAdmin = $this->shopAdmin;
  60. if (!isset($shopAdmin->super) || $shopAdmin->super != 1) {
  61. util::fail('管理员才能添加花材');
  62. }
  63. $post['sjId'] = $this->sjId;
  64. $post['adminId'] = $this->adminId;
  65. $post['mainId'] = $this->mainId;
  66. $post['staffName'] = $shopAdmin->name ?? '';
  67. $price = $post['price'] ?? 0;
  68. if (empty($price) || $price <= 0) {
  69. util::fail('请填写批发价');
  70. }
  71. $skPrice = $post['skPrice'] ?? 0;
  72. if (empty($skPrice) || $skPrice <= 0) {
  73. util::fail('请填写零售价');
  74. }
  75. $hjPrice = $post['hjPrice'] ?? 0;
  76. if (empty($hjPrice) || $hjPrice <= 0) {
  77. util::fail('请填写会员价');
  78. }
  79. $addPrice = $post['addPrice'] ?? 0;
  80. if ($addPrice > $price) {
  81. util::fail('批发价的加价不能大于售价');
  82. }
  83. // 图片列表数组转换为字符串
  84. if (is_array($post['images'])) {
  85. $post['images'] = implode(',', $post['images']);
  86. }
  87. //复制标识
  88. $post['copy'] = 1;
  89. //去除无用的字段
  90. unset($post['viewNum']);
  91. unset($post['actualSold']);
  92. unset($post['itemId']);
  93. unset($post['id']);
  94. unset($post['delStatus']);
  95. ProductClass::addProduct($post, $this->shop);
  96. util::complete('复制成功');
  97. }
  98. //花材展示状态控制 ssh 20250519
  99. public function actionDelStatusUpdate()
  100. {
  101. $post = Yii::$app->request->post();
  102. $shop = $this->shop;
  103. $join = $shop->join ?? 0;
  104. $default = $shop->default ?? 0;
  105. if ($join == 0 && $default == 0) {
  106. util::fail('请在首店操作');
  107. }
  108. $id = $post['id'] ?? 0;
  109. $delStatus = $post['delStatus'] ?? 0;
  110. $product = ProductClass::getById($id, true);
  111. if ($product->mainId != $this->mainId) {
  112. util::fail('不是你的花材哦!!');
  113. }
  114. //扫码收款、直接收款和特价商品,不可删除,多处要同步修改 system_item
  115. if (getenv('YII_ENV') == 'production') {
  116. $systemItem = [78076, 80960, 81004];
  117. $adminList = [4, 1960];
  118. } else {
  119. $systemItem = [11157, 11156, 11158];
  120. $adminList = [919, 932];
  121. }
  122. if (in_array($product->itemId, $systemItem)) {
  123. $adminId = $this->adminId;
  124. if (!in_array($adminId, $adminList)) {
  125. util::fail('系统花材,不可删除');
  126. }
  127. }
  128. $product->delStatus = $delStatus;
  129. if ($delStatus == 1) {
  130. $product->stock = 0;
  131. $product->status = 2;
  132. }
  133. $product->save();
  134. $ptItemId = $product->itemId ?? 0;
  135. if (isset($shop->default) && $shop->default == 1 && isset($shop->dataSync) && $shop->dataSync == 1) {
  136. //总店修改同步到所有门店
  137. $sjId = $shop->sjId ?? 0;
  138. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  139. foreach ($chainShopList as $chainShop) {
  140. $chainShopId = $chainShop->id ?? 0;
  141. if (isset($chainShop->join) && $chainShop->join == 1) {
  142. continue;
  143. }
  144. if ($chainShopId == $shop->id) {
  145. continue;
  146. }
  147. $chainMainId = $chainShop->mainId ?? 0;
  148. $chainProduct = ProductClass::getByCondition(['mainId' => $chainMainId, 'itemId' => $ptItemId], true);
  149. if (!empty($chainProduct)) {
  150. $chainProduct->delStatus = $delStatus;
  151. if ($delStatus == 1) {
  152. $chainProduct->stock = 0;
  153. $chainProduct->status = 2;
  154. }
  155. $chainProduct->save();
  156. }
  157. }
  158. }
  159. util::complete();
  160. }
  161. //打印花材的标签 ssh 20220602
  162. public function actionPrint()
  163. {
  164. $get = Yii::$app->request->get();
  165. $id = $get['id'] ?? 0;
  166. $num = $get['num'] ?? 1;
  167. $num = floatval($num);
  168. $num = (int)$num;
  169. $type = $get['type'] ?? 0;
  170. $priceOption = $get['priceOption'] ?? 0;
  171. $info = ProductClass::getById($id, true);
  172. if (empty($info)) {
  173. util::fail('没有找到花材');
  174. }
  175. $name = $info->name ?? '';
  176. $ptItemId = $info->itemId ?? 0;
  177. $mainId = $info->mainId ?? 0;
  178. $shopId = $this->shopId;
  179. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  180. if (empty($ext)) {
  181. util::fail('没有找到门店信息');
  182. }
  183. $printLabelSn = $ext->printLabelSn ?? '';
  184. if (empty($printLabelSn)) {
  185. util::fail('请设置标签打印机');
  186. }
  187. $p = new printUtil($printLabelSn);
  188. $unit = $info->ratio . $info->smallUnit . '/' . $info->bigUnit;
  189. if (isset($info->ratioType) && $info->ratioType == 1) {
  190. $unit = '若干' . $info->smallUnit . '/' . $info->bigUnit;
  191. }
  192. $p->times = $num;
  193. if ($priceOption == 2) {
  194. // 每7个汉字截取一次(支持汉字、字母、数字混合),多处需要同步修改,关键词 label_print_substr
  195. $content = '';
  196. $len = mb_strlen($name, 'UTF-8');
  197. $baseNum = 30;
  198. $u = 0;
  199. for ($i = 0; $i < $len; $i += 6) {
  200. $currentName = mb_substr($name, $i, 6, 'UTF-8');
  201. $number = $baseNum + 90 * $u;
  202. $u++;
  203. $content .= '<TEXT x="10" y="' . $number . '" font="12" w="3" h="3" r="0">' . $currentName . '</TEXT>';
  204. }
  205. } else {
  206. if (stringUtil::getWordNum($name) > 8) {
  207. $content = '<TEXT x="30" y="30" font="12" w="1" h="1" r="0">' . $name . '</TEXT>';
  208. } else {
  209. $content = '<TEXT x="30" y="30" font="12" w="2" h="2" r="0">' . $name . '</TEXT>';
  210. }
  211. }
  212. if ($type == 0) {
  213. if ($priceOption != 2) {
  214. //打标签
  215. $content .= '<BC128 x="30" y="115" h="75" s="1" r="0" n="3" w="10">' . $ptItemId . '</BC128>';
  216. if (in_array($mainId, [52, 1459])) {
  217. $content .= '<TEXT x="30" y="250" font="12" w="2" h="2" r="0">惠雅鲜花</TEXT>';
  218. } elseif ($mainId == 16948) {
  219. $content .= '<TEXT x="30" y="250" font="12" w="2" h="2" r="0">淘花里鲜花</TEXT>';
  220. } elseif ($mainId == 72057) {
  221. $content .= '<TEXT x="30" y="250" font="12" w="2" h="2" r="0">淘花里小榄店</TEXT>';
  222. } else {
  223. $content .= '<TEXT x="30" y="250" font="12" w="1" h="1" r="0">' . $unit . '</TEXT>';
  224. }
  225. }
  226. } else {
  227. if (getenv('YII_ENV') == 'production') {
  228. $doublePrice = [52, 1459];
  229. } else {
  230. $doublePrice = [];
  231. }
  232. if (in_array($this->mainId, $doublePrice)) {
  233. //打价格,显示正价和会员价
  234. $price = $info->skPrice ?? 0;
  235. $price = floatval($price);
  236. $hyPrice = $info->price ?? 0;
  237. $hyPrice = floatval($hyPrice);
  238. $content .= '<TEXT x="35" y="90" font="12" w="2" h="2" r="0">----------</TEXT>';
  239. $content .= '<TEXT x="35" y="150" font="12" w="2" h="2" r="0">正价 ' . $price . '元</TEXT>';
  240. $content .= '<TEXT x="35" y="230" font="12" w="2" h="2" r="0">会员价 ' . $hyPrice . '元</TEXT>';
  241. } else {
  242. //打价格,显示零售价,并且不带元,杭州斗南鲜花不要带元
  243. $price = $info->skPrice ?? 0;
  244. $price = floatval($price);
  245. $content .= '<TEXT x="35" y="150" font="12" w="4" h="4" r="0">' . $price . '</TEXT>';
  246. }
  247. }
  248. $p->printLabelMsg($content);
  249. util::complete();
  250. }
  251. //新增花材 20250523 ssh
  252. public function actionAdd()
  253. {
  254. $post = Yii::$app->request->post();
  255. $discountPrice = $post['discountPrice'] ?? 0;
  256. if ($discountPrice > 0) {
  257. if (empty($post['hjDiscountPrice'])) {
  258. $addPrice = $post['addPrice'] ?? 0;
  259. $hjAddPrice = $post['hjAddPrice'] ?? 0;
  260. $skMore = $post['skMore'] ?? 0;
  261. $diff = bcsub($addPrice, $hjAddPrice, 2);
  262. if ($diff > 0 && $discountPrice > $diff) {
  263. $hjDiscountPrice = bcsub($discountPrice, $diff, 2);
  264. } else {
  265. $hjDiscountPrice = $discountPrice;
  266. }
  267. $diff = bcsub($skMore, $addPrice, 2);
  268. if ($diff > 0) {
  269. $skDiscountPrice = bcadd($diff, $discountPrice, 2);
  270. } else {
  271. $skDiscountPrice = $discountPrice;
  272. }
  273. $post['skDiscountPrice'] = $skDiscountPrice;
  274. $post['hjDiscountPrice'] = $hjDiscountPrice;
  275. }
  276. }
  277. $shop = $this->shop;
  278. $join = $shop->join ?? 0;
  279. $default = $shop->default ?? 0;
  280. if ($join == 0 && $default == 0) {
  281. util::fail('当前直营分店,请切回总店添加花材');
  282. }
  283. $shopAdmin = $this->shopAdmin;
  284. if (!isset($shopAdmin->super) || $shopAdmin->super != 1) {
  285. util::fail('管理员才能添加花材');
  286. }
  287. $post['sjId'] = $this->sjId;
  288. $post['adminId'] = $this->adminId;
  289. $post['mainId'] = $this->mainId;
  290. $price = $post['price'] ?? 0;
  291. $addPrice = $post['addPrice'] ?? 0;
  292. if ($addPrice > $price) {
  293. util::fail('加价不能大于售价');
  294. }
  295. $weight = isset($post['weight']) && is_numeric($post['weight']) ? $post['weight'] : 0;
  296. if ($weight <= 0) {
  297. util::fail('请填写重量,最小0.1');
  298. }
  299. $post['staffName'] = $shopAdmin->name ?? '';
  300. // 图片列表数组转换为字符串
  301. if (is_array($post['images'])) {
  302. $post['images'] = implode(',', $post['images']);
  303. }
  304. ProductClass::addProduct($post, $this->shop);
  305. util::complete('添加成功');
  306. }
  307. //获取拼音缩写 ssh 20210707
  308. public function actionPy()
  309. {
  310. $name = Yii::$app->request->get('name');
  311. $py = stringUtil::py($name);
  312. util::success(['py' => $py]);
  313. }
  314. //所有花材
  315. public function actionAll()
  316. {
  317. $where = [];
  318. $where['mainId'] = $this->mainId;
  319. $classId = Yii::$app->request->get('classId', 0);
  320. if (!empty($classId)) {
  321. $where['classId'] = $classId;
  322. }
  323. $status = Yii::$app->request->get('status', 0);
  324. if (!empty($status)) {
  325. $where['status'] = $status;
  326. }
  327. $delStatus = Yii::$app->request->get('delStatus', 0);
  328. if ($delStatus != 2) {
  329. $where['delStatus'] = $delStatus;
  330. }
  331. //零售价
  332. $level = 0;
  333. $data = ProductClass::getAllByCondition($where, ['actualSold' => SORT_DESC, 'inTurn' => SORT_DESC], "*");
  334. $data = ProductClass::groupProductInfo($data, $level);
  335. util::success(['list' => $data]);
  336. }
  337. public function actionIndex()
  338. {
  339. $py = Yii::$app->request->get('py', '');
  340. //默认显示上架商品
  341. $status = Yii::$app->request->get('status', 1);
  342. //默认显示未删除商品
  343. $delStatus = Yii::$app->request->get('delStatus', 0);
  344. //获取所有当前供货商的分类 (全部、常用)
  345. //根据分类组装分类下的花材信息
  346. //中央ID
  347. $classIds = ItemClassClass::getGhsItemClassAll($this->mainId);
  348. $where['mainId'] = $this->mainId;
  349. if ($py) {
  350. $pyName = strtolower($py);
  351. $where['py'] = $pyName;
  352. }
  353. if (!empty($status)) {
  354. $where['status'] = $status;
  355. }
  356. $where['delStatus'] = $delStatus;
  357. $level = 0;
  358. $field = 'id,cover,name,cost,itemId,classId,property,skPrice,skMore,addPrice,variety,price,discountPrice,skDiscountPrice,stock,stockWarning,presell,ratioType,smallRatio,smallUnit,bigUnit,ratio,actualSold';
  359. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  360. $itemInfoData = ProductClass::groupProductInfo($itemInfoData, $level);
  361. $respond = ProductService::assembleData($classIds, $itemInfoData, true);
  362. $classItem = $respond['classItem'] ?? [];
  363. util::success($classItem);
  364. }
  365. //当前门店所有花材分类
  366. public function actionList()
  367. {
  368. $classId = Yii::$app->request->get('classId', 0);
  369. $pyName = Yii::$app->request->get('py', '');
  370. if ($pyName) {
  371. $pyName = strtolower($pyName);
  372. $where['py'] = $pyName;
  373. }
  374. $where['mainId'] = $this->mainId;
  375. if (!empty($classId)) {
  376. $where['classId'] = $classId;
  377. }
  378. $type = Yii::$app->request->get('type', '');
  379. $showPage = Yii::$app->request->get('showPage', 0);
  380. //需要显示分页
  381. if ($showPage) {
  382. $respond = ProductClass::getList("*", $where, "inTurn desc");
  383. $respond['list'] = ProductClass::groupClassName($this->mainId, $respond['list']);
  384. $respond['list'] = ProductClass::groupProductInfo($respond['list']);
  385. util::success($respond);
  386. }
  387. $data = ProductClass::getAllByCondition($where, "inTurn desc", "*");
  388. $data = ProductClass::groupProductInfo($data);
  389. if ($type == 'waring') {
  390. //库存预警
  391. $newData = [];
  392. foreach ($data as $v) {
  393. if ($v['stock'] <= $v['stockWarning']) {
  394. $newData[] = $v;
  395. }
  396. }
  397. util::success(['list' => $newData]);
  398. }
  399. util::success(['list' => $data]);
  400. }
  401. //批量改价
  402. public function actionBatchChangePrice()
  403. {
  404. $shop = $this->shop ?? [];
  405. $shopId = $shop->id ?? 0;
  406. $default = $shop->default ?? 0;
  407. $join = $shop->join ?? 0;
  408. $shopAdmin = $this->shopAdmin;
  409. $adminId = $this->adminId ?? 0;
  410. //normal常规改价,cg采购改价
  411. $changeType = Yii::$app->request->post('changeType', 'normal');
  412. $doType = Yii::$app->request->post('doType', 0);
  413. if ($changeType == 'cg' && $doType == 0) {
  414. if (getenv('YII_ENV') == 'production') {
  415. //小蔡鲜花的几个分店的零售店不能修改价格
  416. if (in_array($shopId, [29153, 29155, 29157, 29162, 29164])) {
  417. util::fail('本店不能改价哦');
  418. }
  419. } else {
  420. if (in_array($shopId, [36545])) {
  421. util::fail('本店无法改价哈');
  422. }
  423. }
  424. }
  425. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  426. util::fail('超管才能修改');
  427. }
  428. if ($join == 0 && $default == 0) {
  429. //util::fail('当前直营分店,只能回总店修改价格');
  430. }
  431. $targetId = Yii::$app->request->post('targetId', 0);
  432. $changeParams = ['staffId' => $shopAdmin->id, 'staffName' => $shopAdmin->name, 'changeType' => $changeType, 'targetId' => $targetId];
  433. $post = Yii::$app->request->post();
  434. $data = $post['data'] ?? '';
  435. if (empty($data)) {
  436. util::fail('没有修改');
  437. }
  438. $arr = json_decode($data, true);
  439. if (empty($arr)) {
  440. util::fail('没有修改!');
  441. }
  442. $connection = Yii::$app->db;
  443. $transaction = $connection->beginTransaction();
  444. try {
  445. if ($doType == 0 || $doType == 1) {
  446. $cg = PurchaseClass::getLockById($targetId);
  447. if (!empty($cg)) {
  448. if (isset($cg->mainId) == false || $cg->mainId != $this->mainId) {
  449. util::fail('不是您的采购单');
  450. }
  451. if ($cg->status == PurchaseClass::STATUS_UN_SEND && $cg->book == 1) {
  452. util::fail('预订单只有发货后,才能确认收货');
  453. }
  454. //没有发货则先发货
  455. if ($cg->status == PurchaseClass::STATUS_UN_SEND) {
  456. $cg = PurchaseClass::orderSend($cg, [], true);
  457. }
  458. //确认收货并入库
  459. if ($cg->status == PurchaseClass::STATUS_SENDING) {
  460. // 构建“冲销”函数的参数 -- 变动前的花材存库数据
  461. $purchaseItems = PurchaseItemClass::getAllByCondition(['orderSn' => $cg->orderSn], null, "productId");
  462. $productIds = ArrayHelper::getColumn($purchaseItems, 'productId'); //$productIds 是本次入库涉及的花材ID数组
  463. $oldStocks = [];
  464. foreach ($productIds as $productId) {
  465. $product = ProductClass::getById($productId, true);
  466. $oldStocks[$productId] = $product;
  467. }
  468. PurchaseClass::takeToPutIn($cg);
  469. // “冲销”本次入库的数量
  470. PurchaseClass::rollbackProductStock($oldStocks, $shop, $adminId, $this->mainId, $this->shopAdmin->name);
  471. if ($doType == 1) {
  472. $transaction->commit();
  473. util::complete('入库成功');
  474. }
  475. } else {
  476. util::fail('订单不是待入库状态');
  477. }
  478. } else {
  479. if ($doType == 1) {
  480. util::fail('入库失败,没有找到采购单');
  481. }
  482. }
  483. }
  484. $ids = array_column($arr, 'id');
  485. $ids = array_unique(array_filter($ids));
  486. if (empty($ids)) {
  487. util::fail('请选择花材');
  488. }
  489. $sjId = $shop->sjId ?? 0;
  490. $chainShopList = [];
  491. if ($default == 1) {
  492. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId, 'join' => 0], null, '*', null, true);
  493. }
  494. $productList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id', true);
  495. foreach ($arr as $product) {
  496. if (isset($product['price']) == false) {
  497. util::fail('没有价格');
  498. }
  499. if (isset($product['id']) == false) {
  500. util::fail('没有花材');
  501. }
  502. $price = $product['price'];
  503. $productId = $product['id'];
  504. $productInfo = $productList[$productId] ?? [];
  505. if (empty($productInfo)) {
  506. util::fail('没有找到花材');
  507. }
  508. $productName = $productInfo->name ?? '';
  509. if (isset($product['price']) == false || is_numeric($product['price']) == false || $product['price'] < 0) {
  510. util::fail('请填写' . $productName . '的批发价');
  511. }
  512. if (isset($product['skPrice']) == false || is_numeric($product['skPrice']) == false || $product['skPrice'] < 0) {
  513. util::fail('请填写' . $productName . '的零售价');
  514. }
  515. $skPrice = $product['skPrice'];
  516. if (isset($product['hjPrice']) == false || is_numeric($product['hjPrice']) == false || $product['hjPrice'] < 0) {
  517. util::fail('请填写' . $productName . '的会员价');
  518. }
  519. $hjPrice = $product['hjPrice'] ?? 0;
  520. if (isset($productInfo->mainId) == false || $productInfo->mainId != $this->mainId) {
  521. util::fail('无法访问');
  522. }
  523. if ($hjPrice > $price) {
  524. util::fail($productName . ' 批发价要大于会员价');
  525. }
  526. if ($price > $skPrice) {
  527. util::fail($productName . ' 零售价要大于批发价');
  528. }
  529. $ptItemId = $productInfo->itemId;
  530. ProductClass::changeSinglePrice($shop, $ptItemId, $price, $skPrice, $hjPrice, $changeParams);
  531. //在首店修改需要同步到所有直营店
  532. if ($default == 1 && isset($shop->dataSync) && $shop->dataSync == 1) {
  533. foreach ($chainShopList as $chain) {
  534. if ($chain->id == $shop->id) {
  535. continue;
  536. }
  537. $chainMainId = $chain->mainId ?? 0;
  538. $staff = ShopAdminClass::getByCondition(['mainId' => $chainMainId, 'adminId' => $adminId], true);
  539. $changeParams2 = ['staffId' => $staff->id, 'staffName' => $staff->name, 'changeType' => $changeType, 'targetId' => $targetId];
  540. ProductClass::changeSinglePrice($chain, $ptItemId, $price, $skPrice, $hjPrice, $changeParams2);
  541. }
  542. }
  543. }
  544. $mainId = $shop->mainId ?? 0;
  545. if (!empty($mainId)) {
  546. //提醒零售收银台界面要刷新,多处要修改,关键词 modify_price_remind_cashier
  547. $staffList = ShopAdminClass::getAllByCondition(['mainId' => $mainId], null, '*', null, true);
  548. if (!empty($staffList)) {
  549. foreach ($staffList as $staff) {
  550. $staffId = $staff->id ?? 0;
  551. Yii::$app->redis->executeCommand('SET', ['cashier_' . $mainId . '_' . $staffId . '_may_refresh', 'yes']);
  552. Yii::$app->redis->executeCommand('SET', ['ghs_cashier_' . $mainId . '_' . $staffId . '_may_refresh', 'yes']);
  553. }
  554. }
  555. }
  556. $transaction->commit();
  557. util::complete('操作成功');
  558. } catch (\Exception $e) {
  559. $transaction->rollBack();
  560. Yii::info("失败原因:" . $e->getMessage());
  561. util::fail('修改失败');
  562. }
  563. }
  564. //从零售端采购入库,查看供货商的某个门店的product
  565. public function actionGhsItem()
  566. {
  567. $get = Yii::$app->request->get();
  568. $id = $get['id'] ?? 0;
  569. $ghs = GhsClass::getGhsInfo($id);
  570. if (empty($ghs)) {
  571. util::fail('没有找到供货商');
  572. }
  573. $shopId = $ghs['shopId'] ?? 0;
  574. $black = $ghs['black'] ?? 2;
  575. if ($black == 2) {
  576. util::fail('暂无花材');
  577. }
  578. $connection = Yii::$app->db;
  579. $transaction = $connection->beginTransaction();
  580. try {
  581. //获取所有当前供货商的分类 (全部、常用)
  582. //根据分类组装分类下的花材信息
  583. $shop = ShopClass::getById($shopId, true);
  584. $mainId = $shop->mainId ?? 0;
  585. $classIds = ItemClassClass::getGhsItemClassAll($mainId);
  586. $where['mainId'] = $mainId;
  587. $where['delStatus'] = 0;
  588. //是否显示全部花材,包括下架的
  589. $showAll = $get['showAll'] ?? 0;
  590. if ($showAll == 0) {
  591. $where['status'] = 1;
  592. }
  593. $where['frontHide'] = 0;
  594. $level = $ghs['giveLevel'] ?? 1;
  595. $field = 'id,py,cover,name,ratio,classId,itemId,weight,smallUnit,bigUnit,smallUnit,smallRatio,ratioType,variety,price,skPrice,skDiscountPrice,hjPrice,hjDiscountPrice,actualSold,stock,stockWarning,discountPrice,presell,presellDate,reachNum,reachNumDiscount,limitBuy';
  596. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  597. $itemInfoData = ProductClass::hdCgGroup($itemInfoData, $level, $ghs);
  598. $myProduct = ProductClass::getAllByCondition(['mainId' => $this->mainId, 'delStatus' => 0], null, 'id,stock,onStock,stockWarning,itemId', 'itemId');
  599. if (!empty($itemInfoData)) {
  600. $myShop = $this->shop;
  601. $myDisplay = isset($myShop->pfShopId) && $myShop->pfShopId > 0 ? 1 : 0;
  602. foreach ($itemInfoData as $key => $val) {
  603. $ptItemId = $val['itemId'] ?? 0;
  604. $myStock = $myProduct[$ptItemId]['stock'] ?? 0;
  605. $myStock = floatval($myStock);
  606. $myOnStock = $myProduct[$ptItemId]['onStock'] ?? 0;
  607. $myStockWarning = $myProduct[$ptItemId]['stockWarning'] ?? 5;
  608. $myLack = bcadd($myStock, $myOnStock, 2) > $myStockWarning ? 0 : 1;
  609. $itemInfoData[$key]['myStock'] = $myStock;
  610. $itemInfoData[$key]['myOnStock'] = $myOnStock;
  611. $itemInfoData[$key]['myStockWarning'] = $myStockWarning;
  612. $itemInfoData[$key]['myLack'] = $myLack;
  613. $itemInfoData[$key]['myDisplay'] = $myDisplay;
  614. }
  615. }
  616. $respond = ProductService::assembleData($classIds, $itemInfoData, true);
  617. $classItem = $respond['classItem'] ?? [];
  618. util::success($classItem);
  619. } catch (\Exception $e) {
  620. $transaction->rollBack();
  621. Yii::info("报错内容:" . $e->getMessage());
  622. util::fail();
  623. }
  624. }
  625. public function actionGhsItemV2()
  626. {
  627. $get = Yii::$app->request->get();
  628. $id = $get['id'] ?? 0;
  629. $ghs = GhsClass::getGhsInfo($id);
  630. if (empty($ghs)) {
  631. util::fail('没有找到供货商');
  632. }
  633. $shopId = $ghs['shopId'] ?? 0;
  634. $black = $ghs['black'] ?? 2;
  635. if ($black == 2) {
  636. util::fail('暂无花材');
  637. }
  638. $connection = Yii::$app->db;
  639. $transaction = $connection->beginTransaction();
  640. try {
  641. //获取所有当前供货商的分类 (全部、常用)
  642. //根据分类组装分类下的花材信息
  643. $shop = ShopClass::getById($shopId, true);
  644. $mainId = $shop->mainId ?? 0;
  645. $open = $shop->open ?? 1;
  646. $openStartTime = $shop->openStartTime;
  647. $openEndTime = $shop->openEndTime;
  648. if (!empty($openStartTime) && !empty($openEndTime)) {
  649. $date = date('Y-m-d');
  650. $openStartDate = $date . ' ' . $openStartTime;
  651. $openEndDate = $date . ' ' . $openEndTime;
  652. $startTime = strtotime($openStartDate);
  653. $endTime = strtotime($openEndDate);
  654. $currentTime = time();
  655. if ($currentTime < $startTime || $currentTime > $endTime) {
  656. $open = 0;
  657. }
  658. }
  659. if ($open == 0) {
  660. if (getenv('YII_ENV') == 'production') {
  661. //九江云朵、丽子鲜花、沈阳市云花鲜花,休店不让看花材
  662. if (in_array($mainId, [26374, 1496, 67094])) {
  663. util::fail('已休店');
  664. }
  665. } else {
  666. if (in_array($mainId, [644, 1])) {
  667. util::fail('已休店');
  668. }
  669. }
  670. }
  671. $classIds = ItemClassClass::getGhsItemClassAll($mainId);
  672. $where['mainId'] = $mainId;
  673. $where['delStatus'] = 0;
  674. //是否显示全部花材,包括下架的
  675. $showAll = $get['showAll'] ?? 0;
  676. if ($showAll == 0) {
  677. $where['status'] = 1;
  678. }
  679. $where['frontHide'] = 0;
  680. $level = $ghs['giveLevel'] ?? 1;
  681. $field = 'id,py,cover,name,ratio,classId,itemId,weight,smallUnit,bigUnit,smallUnit,smallRatio,ratioType,variety,price,skPrice,skDiscountPrice,hjPrice,hjDiscountPrice,avCost,actualSold,stock,stockWarning,discountPrice,presell,presellDate,reachNum,reachNumDiscount,limitBuy';
  682. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  683. $itemInfoData = ProductClass::hdCgGroup($itemInfoData, $level, $ghs);
  684. $myProduct = ProductClass::getAllByCondition(['mainId' => $this->mainId, 'delStatus' => 0], null, 'id,stock,onStock,stockWarning,itemId', 'itemId');
  685. if (!empty($itemInfoData)) {
  686. $myShop = $this->shop;
  687. $myDisplay = 0;
  688. if (isset($myShop->pfShopId) && $myShop->pfShopId > 0) {
  689. $myDisplay = 1;
  690. }
  691. foreach ($itemInfoData as $key => $val) {
  692. $ptItemId = $val['itemId'] ?? 0;
  693. $myStock = $myProduct[$ptItemId]['stock'] ?? 0;
  694. $myStock = floatval($myStock);
  695. $myOnStock = $myProduct[$ptItemId]['onStock'] ?? 0;
  696. $myStockWarning = $myProduct[$ptItemId]['stockWarning'] ?? 5;
  697. $myLack = bcadd($myStock, $myOnStock, 2) > $myStockWarning ? 0 : 1;
  698. $itemInfoData[$key]['myStock'] = $myStock;
  699. $itemInfoData[$key]['myOnStock'] = $myOnStock;
  700. $itemInfoData[$key]['myStockWarning'] = $myStockWarning;
  701. $itemInfoData[$key]['myLack'] = $myLack;
  702. $itemInfoData[$key]['myDisplay'] = $myDisplay;
  703. }
  704. }
  705. $respond = ProductService::assembleData($classIds, $itemInfoData, true);
  706. if ($showAll == 1) {
  707. if (isset($respond['classItem']) && !empty($respond['classItem'])) {
  708. foreach ($respond['classItem'] as $bigKey => $bigVal) {
  709. if (isset($bigVal['child']) && !empty($bigVal['child'])) {
  710. foreach ($bigVal['child'] as $key => $val) {
  711. unset($bigVal['child'][$key]['cover']);
  712. unset($bigVal['child'][$key]['actualSold']);
  713. unset($bigVal['child'][$key]['defaultGradePrice']);
  714. unset($bigVal['child'][$key]['discountPrice']);
  715. unset($bigVal['child'][$key]['hjDiscountPrice']);
  716. unset($bigVal['child'][$key]['hjPrice']);
  717. unset($bigVal['child'][$key]['presellDateShow']);
  718. unset($bigVal['child'][$key]['prePrice']);
  719. unset($bigVal['child'][$key]['print']);
  720. unset($bigVal['child'][$key]['reachNum']);
  721. unset($bigVal['child'][$key]['reachNumDiscount']);
  722. unset($bigVal['child'][$key]['reachPrice']);
  723. unset($bigVal['child'][$key]['skDiscountPrice']);
  724. unset($bigVal['child'][$key]['skPrice']);
  725. unset($bigVal['child'][$key]['bigUnit']);
  726. unset($bigVal['child'][$key]['weight']);
  727. unset($bigVal['child'][$key]['stockWarning']);
  728. //unset($bigVal['child'][$key]['smallPrice']);
  729. //unset($bigVal['child'][$key]['smallRatio']);
  730. }
  731. $respond['classItem'][$bigKey]['child'] = $bigVal['child'];
  732. }
  733. }
  734. }
  735. }
  736. util::success($respond);
  737. } catch (\Exception $e) {
  738. $transaction->rollBack();
  739. Yii::info("报错内容:" . $e->getMessage());
  740. util::fail();
  741. }
  742. }
  743. public function actionDetail()
  744. {
  745. $id = Yii::$app->request->get('id', 0);
  746. $respond = ProductClass::getItemInfo($id);
  747. $ptItemId = $respond['itemId'] ?? 0;
  748. $ptItemInfo = PtItemClass::getById($ptItemId);
  749. $respond['ptItemInfo'] = $ptItemInfo;
  750. // 封面图与多花材图片的兼容处理
  751. if ($respond['images'] == '' && $respond['shortCover'] != '') {
  752. $respond['images'] = $respond['shortCover'];
  753. }
  754. //查出拆散的子花材
  755. $part = UnitChangeClass::getByCondition(['big' => $id], true);
  756. $partAcceptItem = 0;
  757. $partAcceptItemName = '';
  758. if (!empty($part)) {
  759. $partAcceptItem = $part->small;
  760. $small = ProductClass::getByCondition(['id' => $partAcceptItem], true, false, 'id,name');
  761. $partAcceptItemName = $small->name ?? '';;
  762. }
  763. $respond['partAcceptItem'] = $partAcceptItem;
  764. $respond['partAcceptItemName'] = $partAcceptItemName;
  765. util::success($respond);
  766. }
  767. public function actionUpdate()
  768. {
  769. $shopAdmin = $this->shopAdmin;
  770. if (!isset($shopAdmin->super) || $shopAdmin->super != 1) {
  771. util::fail('超管才能操作');
  772. }
  773. $shop = $this->shop;
  774. $join = $shop->join ?? 0;
  775. $default = $shop->default ?? 0;
  776. if ($join == 0 && $default == 0) {
  777. //因为盘点不能将花材盘成0,所以这边放出入口允许创始人在分店修改分店的花材库存
  778. if ($shopAdmin->super != 1) {
  779. util::fail('请先开通超管权限');
  780. }
  781. }
  782. $deadline = $shop->deadline;
  783. $deadTime = strtotime($deadline);
  784. if (time() > $deadTime) {
  785. util::fail('系统已到期,编号563');
  786. }
  787. $post = Yii::$app->request->post();
  788. $discountPrice = $post['discountPrice'] ?? 0;
  789. if ($discountPrice > 0) {
  790. if (empty($post['hjDiscountPrice'])) {
  791. $addPrice = $post['addPrice'] ?? 0;
  792. $hjAddPrice = $post['hjAddPrice'] ?? 0;
  793. $skMore = $post['skMore'] ?? 0;
  794. $diff = bcsub($addPrice, $hjAddPrice, 2);
  795. if ($diff > 0 && $discountPrice > $diff) {
  796. $hjDiscountPrice = bcsub($discountPrice, $diff, 2);
  797. } else {
  798. $hjDiscountPrice = $discountPrice;
  799. }
  800. $diff = bcsub($skMore, $addPrice, 2);
  801. if ($diff > 0) {
  802. $skDiscountPrice = bcadd($diff, $discountPrice, 2);
  803. } else {
  804. $skDiscountPrice = $discountPrice;
  805. }
  806. $post['skDiscountPrice'] = $skDiscountPrice;
  807. $post['hjDiscountPrice'] = $hjDiscountPrice;
  808. }
  809. $reachNum = $post['reachNum'] ?? 0;
  810. $reachNumDiscount = $post['reachNumDiscount'] ?? 0;
  811. if ($reachNum > 0 && $reachNumDiscount > 0) {
  812. util::fail('特价和满减不能同时进行');
  813. }
  814. }
  815. $post['adminId'] = $this->adminId;
  816. $post['staffId'] = $this->shopAdmin->id;
  817. $post['staffName'] = $this->shopAdmin->name;
  818. $connection = Yii::$app->db;
  819. $transaction = $connection->beginTransaction();
  820. try {
  821. ProductClass::updateProduct($post, $this->shop);
  822. $transaction->commit();
  823. util::complete();
  824. } catch (\Exception $e) {
  825. Yii::info("操作失败:" . $e->getMessage());
  826. $transaction->rollBack();
  827. util::fail('操作失败');
  828. }
  829. }
  830. //批量修改product 排序,成本价,加价
  831. public function actionBatchUpdate()
  832. {
  833. //data = [{id:1,inTurn:10,addPrice:2,cost:10}]
  834. $data = Yii::$app->request->post('data', '');
  835. $type = Yii::$app->request->post('type', '');
  836. if (empty($data) || empty($type)) {
  837. util::fail("参数错误");
  838. }
  839. if (in_array($type, ['inTurn', 'addPrice', 'cost', 'weight', 'ratio']))
  840. $data = json_decode($data, true);
  841. foreach ($data as $v) {
  842. $id = $v['id'];
  843. $val = $v[$type] ?? '';
  844. $where = [];
  845. $where['id'] = $id;
  846. $where['shopId'] = $this->shopId;
  847. $where['sjId'] = $this->sjId;
  848. if (empty($val)) {
  849. util::fail("参数错误1");
  850. }
  851. $data = [
  852. $type => $val
  853. ];
  854. ProductClass::updateByCondition($where, $data);
  855. ProductClass::autoPriceById($id);
  856. }
  857. util::complete();
  858. }
  859. //供货商产品的详情 ssh 20230111
  860. public function actionGhsProductDetail()
  861. {
  862. $get = Yii::$app->request->get();
  863. $id = $get['id'] ?? 0;
  864. $ghsId = $get['ghsId'] ?? 0;
  865. $product = ProductClass::getById($id);
  866. if (empty($product)) {
  867. util::fail('没有花材信息');
  868. }
  869. $ghsInfo = GhsClass::getById($ghsId, true, 'giveLevel');
  870. $level = isset($ghsInfo->giveLevel) ? $ghsInfo->giveLevel : 1;
  871. $list = ProductClass::groupProductInfo([$product], $level);
  872. $product = current($list);
  873. $mainId = $product['mainId'] ?? 0;
  874. $pixTextGoods = PicTextGhsItemClass::getByCondition(['mainId' => $mainId, 'ghsItemId' => $id], true);
  875. $product['picTextGoods'] = $pixTextGoods;
  876. // 封面图与多花材图片的兼容处理
  877. if ($product['images'] == '' && $product['shortCover'] != '') {
  878. $product['images'] = $product['shortCover'];
  879. }
  880. util::success($product);
  881. }
  882. //批量修改加价 ssh 20211211
  883. public function actionBatchChangeAddPrice()
  884. {
  885. $shopAdmin = $this->shopAdmin;
  886. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  887. util::fail('超管才能操作');
  888. }
  889. $shop = $this->shop;
  890. $pfShopId = $shop->pfShopId ?? 0;
  891. if (!empty($pfShopId)) {
  892. util::fail('请在批发端操作');
  893. }
  894. $addData = Yii::$app->request->post('addData', '');
  895. if (empty($addData)) {
  896. util::fail('没有花材');
  897. }
  898. $itemData = json_decode($addData, true);
  899. if (is_array($itemData) == false) {
  900. util::fail('没有花材...');
  901. }
  902. $ids = array_column($itemData, 'id');
  903. $infoList = ProductClass::getByIds($ids);
  904. if (empty($infoList)) {
  905. util::fail('没有花材');
  906. }
  907. foreach ($infoList as $info) {
  908. if (isset($info['mainId']) == false || $info['mainId'] != $this->mainId) {
  909. util::fail('非常访问,不是你的花材不能修改');
  910. }
  911. }
  912. foreach ($itemData as $key => $data) {
  913. $id = $data['id'] ?? 0;
  914. ProductClass::updateById($id, $data);
  915. }
  916. util::complete('修改成功');
  917. }
  918. //修改拆散接收的花材 ssh 20251124
  919. public function actionModifyPartAcceptItem()
  920. {
  921. $get = Yii::$app->request->get();
  922. $smallId = $get['small'] ?? 0;
  923. $bigId = $get['big'] ?? 0;
  924. $small = ProductClass::getById($smallId, true);
  925. if (empty($small)) {
  926. util::fail('子花材没找到');
  927. }
  928. $smallName = $small->name ?? '';
  929. if ($small->mainId != $this->mainId) {
  930. util::fail($smallName . '不是你的花材');
  931. }
  932. if ($small->ratio != 1) {
  933. util::fail($smallName . '不是1支装的');
  934. }
  935. $big = ProductClass::getById($bigId, true);
  936. if (empty($big)) {
  937. util::fail('没有找到花材');
  938. }
  939. if ($big->mainId != $this->mainId) {
  940. util::fail('不是你的花材呢');
  941. }
  942. $change = UnitChangeClass::getByCondition(['big' => $bigId], true);
  943. if (empty($change)) {
  944. UnitChangeClass::add(['big' => $bigId, 'small' => $smallId], true);
  945. } else {
  946. $change->small = $smallId;
  947. $change->save();
  948. }
  949. util::complete('修改成功');
  950. }
  951. public function actionCancelDiscount()
  952. {
  953. $get = Yii::$app->request->get();
  954. $id = $get['id'] ?? 0;
  955. $info = ProductClass::getById($id, true);
  956. ProductClass::check($info, $this->mainId);
  957. $info->discountPrice = 0;
  958. $info->skDiscountPrice = 0;
  959. $info->hjDiscountPrice = 0;
  960. $info->save();
  961. util::complete('取消成功');
  962. }
  963. }