ProductController.php 41 KB

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