ProductController.php 37 KB

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