ProductController.php 41 KB

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