ProductController.php 39 KB

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