ProductController.php 42 KB

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