ProductController.php 37 KB

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