ItemController.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. <?php
  2. namespace ghs\controllers;
  3. use biz\item\classes\PtItemClass;
  4. use biz\product\models\Product;
  5. use bizGhs\custom\classes\CustomClass;
  6. use bizGhs\item\classes\ItemClass;
  7. use bizGhs\shop\classes\ShopAdminClass;
  8. use common\components\dict;
  9. use common\components\imgUtil;
  10. use common\components\miniUtil;
  11. use common\components\noticeUtil;
  12. use common\components\stringUtil;
  13. use common\components\util;
  14. use bizGhs\product\classes\ProductClass;
  15. use bizHd\wx\classes\WxOpenClass;
  16. use Yii;
  17. class ItemController extends BaseController
  18. {
  19. //获取花材最新信息,库存 ssh 20240318
  20. public function actionGetNewInfo()
  21. {
  22. $post = Yii::$app->request->post();
  23. $str = $post['data'] ?? '';
  24. if (empty($str)) {
  25. util::fail('请传花材信息');
  26. }
  27. $arr = json_decode($str, true);
  28. if (empty($arr)) {
  29. util::fail('请传花材信息哈');
  30. }
  31. $ids = array_column($arr, 'productId');
  32. $list = ItemClass::getAllByCondition(['id' => ['in', $ids]], null, 'id,name,stock,mainId', 'id', true);
  33. if (!empty($list)) {
  34. foreach ($list as $item) {
  35. if ($item->mainId != $this->mainId) {
  36. util::fail('不是你的花材');
  37. }
  38. }
  39. }
  40. util::success(['list' => $list]);
  41. }
  42. //列出所有花材 ssh 20240222
  43. public function actionGetItemList()
  44. {
  45. $get = Yii::$app->request->get();
  46. $search = $get['search'] ?? '';
  47. $requestType = $get['requestType'] ?? 'kd';
  48. $classId = $get['classId'] ?? 0;
  49. $where['mainId'] = $this->mainId;
  50. if ($requestType == 'kd') {
  51. $where['delStatus'] = 0;
  52. $where['status'] = 1;
  53. } elseif ($requestType == 'itemList') {
  54. $where['delStatus'] = 0;
  55. unset($where['status']);
  56. } elseif ($requestType == 'changePrice') {
  57. $where['delStatus'] = 0;
  58. $where['stock>'] = 0;
  59. } elseif ($requestType == 'hasStockList') {
  60. $where['delStatus'] = 0;
  61. $where['stock>'] = 0;
  62. } elseif ($requestType == 'book') {
  63. $where['delStatus'] = 0;
  64. unset($where['status']);
  65. } elseif ($requestType == 'outList') {
  66. $where['delStatus'] = 0;
  67. $where['status'] = 2;
  68. $where['removeStatus'] = 0;
  69. } elseif ($requestType == 'stopList') {
  70. $where['delStatus'] = 1;
  71. unset($where['status']);
  72. $where['removeStatus'] = 0;
  73. } elseif ($requestType == 'removeList') {
  74. $where['delStatus'] = 1;
  75. unset($where['status']);
  76. $where['removeStatus'] = 1;
  77. } elseif ($requestType == 'cg') {
  78. $where['delStatus'] = 0;
  79. unset($where['status']);
  80. } elseif ($requestType == 'changeStock') {
  81. $where['delStatus'] = 0;
  82. unset($where['status']);
  83. } elseif ($requestType == 'check') {
  84. $where['delStatus'] = 0;
  85. unset($where['status']);
  86. } elseif ($requestType == 'stockOut') {
  87. $where['delStatus'] = 0;
  88. $where['status'] = 1;
  89. } elseif ($requestType == 'break') {
  90. $where['delStatus'] = 0;
  91. $where['status'] = 1;
  92. } else {
  93. util::fail('没有定义的请求方式');
  94. }
  95. if (!empty($search)) {
  96. if (stringUtil::isLetter($search)) {
  97. $search = strtolower($search);
  98. $where['py'] = ['like', $search];
  99. } else {
  100. $where['name'] = ['like', $search];
  101. }
  102. } else {
  103. if (!empty($classId)) {
  104. if ($classId == -1) {
  105. $where['discountPrice>'] = 0;
  106. } elseif ($classId == -2) {
  107. $where['reachNum>'] = 0;
  108. } elseif ($classId == -3) {
  109. $where['presell'] = 1;
  110. } else {
  111. $where['classId'] = $classId;
  112. }
  113. }
  114. }
  115. $customId = $get['customId'] ?? 0;
  116. $custom = CustomClass::getById($customId, true);
  117. if (!empty($custom)) {
  118. if ($custom->ownMainId != $this->mainId) {
  119. util::fail('不是你的客户');
  120. }
  121. }
  122. $level = $custom->level ?? 1;
  123. $field = '*';
  124. $result = ProductClass::getList($field, $where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC]);
  125. $list = $result['list'] ?? [];
  126. if ($requestType == 'hasStockList') {
  127. util::fail('无效方式');
  128. } elseif ($requestType == 'changePrice') {
  129. $list = ProductClass::changePriceGroup($list, $level);
  130. } elseif ($requestType == 'changeStock') {
  131. $list = ProductClass::changeStockGroup($list, $level);
  132. } elseif ($requestType == 'kd') {
  133. $list = ProductClass::kdItemGroup($list, $level);
  134. } elseif ($requestType == 'book') {
  135. $list = ProductClass::bookItemGroup($list, $level);
  136. } elseif ($requestType == 'itemList') {
  137. $list = ProductClass::itemListGroup($list, $level);
  138. } elseif ($requestType == 'outList') {
  139. $list = ProductClass::simpleGroup($list, $level);
  140. } elseif ($requestType == 'stopList') {
  141. $list = ProductClass::simpleGroup($list, $level);
  142. } elseif ($requestType == 'removeList') {
  143. $list = ProductClass::simpleGroup($list, $level);
  144. } elseif ($requestType == 'check') {
  145. $list = ProductClass::checkItemGroup($list, $level);
  146. } elseif ($requestType == 'break') {
  147. $list = ProductClass::breakItemGroup($list, $level);
  148. } elseif ($requestType == 'part') {
  149. $list = ProductClass::partItemGroup($list, $level);
  150. } elseif ($requestType == 'stockOut') {
  151. $list = ProductClass::stockOutGroup($list, $level);
  152. } elseif ($requestType == 'cg') {
  153. $list = ProductClass::cgItemGroup($list, $level);
  154. } elseif ($requestType == 'warning') {
  155. util::fail('无效方式');
  156. // $itemInfoData = Product::find()->where("mainId={$this->mainId}")
  157. // ->andWhere('`stock`+`onStock`<`stockWarning`')
  158. // ->andWhere("delStatus=0")
  159. // ->select($field)->asArray()->all();
  160. // $itemInfoData = ProductClass::warningGroup($itemInfoData, $level);
  161. } else {
  162. util::fail('没有数据');
  163. }
  164. $result['list'] = $list;
  165. util::success($result);
  166. }
  167. //花材申请平台认证 ssh 20231125
  168. public function actionApplyAuth()
  169. {
  170. $get = Yii::$app->request->get();
  171. $id = $get['id'] ?? 0;
  172. $info = ItemClass::getById($id, true);
  173. if (empty($info)) {
  174. util::fail('没有找到花材');
  175. }
  176. if ($info->mainId != $this->mainId) {
  177. util::fail('不是你的花材哦');
  178. }
  179. $shop = $this->shop;
  180. $sjName = $shop->merchantName ?? '';
  181. $shopName = $shop->shopName ?? '';
  182. $name = $shopName == '首店' ? $sjName : $sjName . ' ' . $shopName;
  183. $ptItemId = $info->itemId ?? 0;
  184. $ptItem = PtItemClass::getById($ptItemId, true);
  185. if (empty($ptItem)) {
  186. util::fail('没有找到平台花材');
  187. }
  188. $ptItemName = $ptItem->name ?? '';
  189. noticeUtil::push($name . " 申请将【{$ptItemName}/{$ptItemId}】进行认证", '15280215347');
  190. util::complete();
  191. }
  192. //简单花材的列表
  193. public function actionSimpleList()
  194. {
  195. $get = Yii::$app->request->get();
  196. $name = $get['name'] ?? '';
  197. $num = $get['num'] ?? 0;
  198. $where = ['mainId' => $this->mainId, 'delStatus' => 0];
  199. if (!empty($name)) {
  200. if (stringUtil::isLetter($name)) {
  201. $where['py'] = ['like', $name];
  202. } else {
  203. $where['name'] = ['like', $name];
  204. }
  205. }
  206. $respond = ItemClass::getSimpleList($where, $num);
  207. util::success($respond);
  208. }
  209. public function actionChangeFrontHide()
  210. {
  211. $get = Yii::$app->request->get();
  212. $id = $get['id'] ?? 0;
  213. $status = $get['status'] ?? '';
  214. if (is_numeric($status) == false) {
  215. util::fail('请选择方式');
  216. }
  217. $info = ItemClass::getById($id, true);
  218. if (empty($info)) {
  219. util::fail('没有找到花材');
  220. }
  221. if ($info->mainId != $this->mainId) {
  222. util::fail('无法操作');
  223. }
  224. $info->frontHide = $status;
  225. $info->save();
  226. util::complete();
  227. }
  228. //检测是否要刷新
  229. public function actionCheckRefresh()
  230. {
  231. $mainId = $this->mainId;
  232. $staffId = $this->shopAdminId;
  233. $respond = Yii::$app->redis->executeCommand('GET', ['ghs_cashier_' . $mainId . '_' . $staffId . '_may_refresh']);
  234. if ($respond == 'yes') {
  235. util::success(['remind' => 1]);
  236. }
  237. util::complete();
  238. }
  239. //批量恢复花材 ssh 20230206
  240. public function actionBatchRecover()
  241. {
  242. $post = Yii::$app->request->post();
  243. $string = $post['ids'] ?? '';
  244. $ids = json_decode($string, true);
  245. $ids = array_unique(array_filter($ids));
  246. if (empty($ids)) {
  247. util::fail('请选择花材');
  248. }
  249. $infoList = ItemClass::getAllByCondition(['id' => ['in', $ids]], null, 'id,name,mainId,status,delStatus,removeStatus', null, true);
  250. if (empty($infoList)) {
  251. util::fail('请选择花材哦');
  252. }
  253. foreach ($infoList as $info) {
  254. if ($info->mainId != $this->mainId) {
  255. util::fail('请选择自己的花材');
  256. }
  257. $info->removeStatus = 0;
  258. $info->save();
  259. }
  260. util::complete('操作成功');
  261. }
  262. //批量删除花材 ssh 202302026
  263. public function actionBatchRemove()
  264. {
  265. $post = Yii::$app->request->post();
  266. $string = $post['ids'] ?? '';
  267. $ids = json_decode($string, true);
  268. $ids = array_unique(array_filter($ids));
  269. if (empty($ids)) {
  270. util::fail('请选择花材');
  271. }
  272. $infoList = ItemClass::getAllByCondition(['id' => ['in', $ids]], null, 'id,name,mainId,status,delStatus,removeStatus', null, true);
  273. if (empty($infoList)) {
  274. util::fail('请选择花材哦');
  275. }
  276. foreach ($infoList as $info) {
  277. if ($info->mainId != $this->mainId) {
  278. util::fail('请选择自己的花材');
  279. }
  280. $name = $info->name ?? '';
  281. if ($info->delStatus != 1) {
  282. util::fail($name . ' 停用了才能删除');
  283. }
  284. $info->removeStatus = 1;
  285. $info->save();
  286. }
  287. util::complete('操作成功');
  288. }
  289. //批量启用花材 ssh 20230206
  290. public function actionBatchEnable()
  291. {
  292. $post = Yii::$app->request->post();
  293. $string = $post['ids'] ?? '';
  294. $ids = json_decode($string, true);
  295. $ids = array_unique(array_filter($ids));
  296. if (empty($ids)) {
  297. util::fail('请选择花材');
  298. }
  299. $infoList = ItemClass::getAllByCondition(['id' => ['in', $ids]], null, 'id,name,mainId,status,delStatus,removeStatus', null, true);
  300. if (empty($infoList)) {
  301. util::fail('请选择花材哦');
  302. }
  303. foreach ($infoList as $info) {
  304. if ($info->mainId != $this->mainId) {
  305. util::fail('请选择自己的花材');
  306. }
  307. $info->delStatus = 0;
  308. $info->save();
  309. }
  310. util::complete('操作成功');
  311. }
  312. //批量停用花材 20230206
  313. public function actionBatchStop()
  314. {
  315. $post = Yii::$app->request->post();
  316. $string = $post['ids'] ?? '';
  317. $ids = json_decode($string, true);
  318. $ids = array_unique(array_filter($ids));
  319. if (empty($ids)) {
  320. util::fail('请选择花材');
  321. }
  322. $infoList = ItemClass::getAllByCondition(['id' => ['in', $ids]], null, 'id,name,mainId,status,delStatus,removeStatus', null, true);
  323. if (empty($infoList)) {
  324. util::fail('请选择花材哦');
  325. }
  326. foreach ($infoList as $info) {
  327. if ($info->mainId != $this->mainId) {
  328. util::fail('请选择自己的花材');
  329. }
  330. $name = $info->name ?? '';
  331. if ($info->status != 2) {
  332. util::fail($name . ' 下载了才能停用');
  333. }
  334. $info->delStatus = 1;
  335. $info->save();
  336. }
  337. util::complete('操作成功');
  338. }
  339. //获取给散客下单的花材海报 ssh 20220111
  340. public function actionGetSkPoster()
  341. {
  342. $get = Yii::$app->request->get();
  343. $id = $get['id'] ?? 0;
  344. $item = ItemClass::getById($id, true);
  345. if (empty($item) || $item->mainId != $this->mainId) {
  346. util::fail('获取失败');
  347. }
  348. $cover = $item->cover ?? '';
  349. if (empty($cover)) {
  350. util::fail('获取失败');
  351. }
  352. $merchant = WxOpenClass::getMallWxInfo();
  353. $page = 'pages/item/detail';
  354. $ptStyle = dict::getDict('ptStyle', 'mall');
  355. $shop = $this->shop;
  356. $lsShopId = $shop->lsShopId ?? 0;
  357. //scene不能超过32个字条
  358. $scene = "id=" . $id . '&a=' . $lsShopId;
  359. $miniCode = miniUtil::generateUnlimitedMiniCode($merchant, $page, $scene, $ptStyle);
  360. $miniCode = $miniCode . '?x-oss-process=image/resize,m_fill,h_200,w_200';
  361. $miniCodeBase64 = stringUtil::ossBase64($miniCode);
  362. Yii::$app->params['ptStyle'] = dict::getDict('ptStyle', 'hd');
  363. $prefix = imgUtil::getPrefix();
  364. $price = $item->skPrice ?? 0;
  365. if ($item->discountPrice > 0) {
  366. $skMore = $item->skMore ?? 0;
  367. $price = bcadd($item->discountPrice, $skMore, 2);
  368. }
  369. $newPrice = '¥' . floatval($price);
  370. $priceBase64 = stringUtil::ossBase64($newPrice);
  371. $name = $item->name ?? '';
  372. $nameBase64 = stringUtil::ossBase64($name);
  373. $staff = $this->shopAdmin;
  374. $staffName = $staff->name ?? '';
  375. $newStaffName = $staffName . ' 为您推荐';
  376. $staffNameBase64 = stringUtil::ossBase64($newStaffName);
  377. //花材主图
  378. $itemCover = $cover . '?x-oss-process=image/resize,m_fill,h_750,w_750';
  379. $itemCoverBase64 = stringUtil::ossBase64($itemCover);
  380. $logoBase64 = '';
  381. if (in_array($this->mainId, [52, 1459, 13495])) {
  382. $logo = 'poster/hyxh_ncd_logo.png?x-oss-process=image/resize,m_fill,h_90,w_90';
  383. if ($this->mainId == 1459) {
  384. $logo = 'poster/hyxh_gcd_logo.png?x-oss-process=image/resize,m_fill,h_90,w_90';
  385. }
  386. if ($this->mainId == 13495) {
  387. $logo = 'poster/hyxh_hzd_logo.png?x-oss-process=image/resize,m_fill,h_90,w_90';
  388. }
  389. $logoBase64 = stringUtil::ossBase64($logo);
  390. }
  391. $url = $prefix . 'poster/1234.jpg?x-oss-process=image';
  392. //花材主图
  393. $url .= '/watermark,image_' . $itemCoverBase64 . ',g_nw,x_0,y_0';
  394. //小程序码
  395. $url .= '/watermark,image_' . $miniCodeBase64 . ',g_se';
  396. if (!empty($logoBase64)) {
  397. //logo
  398. $url .= '/watermark,image_' . $logoBase64 . ',g_se,x_65,y_65';
  399. }
  400. //为你推荐
  401. $url .= '/watermark,text_' . $staffNameBase64 . ',g_sw,x_18,y_235,color_8B8989,type_d3F5LW1pY3JvaGVp,size_30';
  402. //标题
  403. $url .= '/watermark,text_' . $nameBase64 . ',g_sw,x_18,y_145';
  404. //价格
  405. $url .= '/watermark,text_' . $priceBase64 . ',g_sw,x_18,y_50,color_EE2C2C,type_d3F5LW1pY3JvaGVp,size_50';
  406. $respond = ['imgUrl' => $url];
  407. util::success($respond);
  408. }
  409. //获取给花店下单的花材海报 ssh 20220111
  410. public function actionGetHdPoster()
  411. {
  412. $get = Yii::$app->request->get();
  413. $id = $get['id'] ?? 0;
  414. $item = ItemClass::getById($id, true);
  415. if (empty($item) || $item->mainId != $this->mainId) {
  416. util::fail('获取失败');
  417. }
  418. $cover = $item->cover ?? '';
  419. if (empty($cover)) {
  420. util::fail('获取失败');
  421. }
  422. $merchant = WxOpenClass::getWxInfo();
  423. $page = 'admin/ghsProduct/detail';
  424. $ptStyle = dict::getDict('ptStyle', 'hd');
  425. //小程序码,scene不能超过32个字条
  426. $scene = "id=" . $id . '&sId=' . $this->shopId;
  427. $miniCode = miniUtil::generateUnlimitedMiniCode($merchant, $page, $scene, $ptStyle);
  428. $miniCode = $miniCode . '?x-oss-process=image/resize,m_fill,h_200,w_200';
  429. $miniCodeBase64 = stringUtil::ossBase64($miniCode);
  430. Yii::$app->params['ptStyle'] = dict::getDict('ptStyle', 'ghs');
  431. $prefix = imgUtil::getPrefix();
  432. $price = $item->price ?? 0;
  433. if ($item->discountPrice > 0) {
  434. $price = $item->discountPrice;
  435. }
  436. $newPrice = '¥' . floatval($price);
  437. $priceBase64 = stringUtil::ossBase64($newPrice);
  438. $name = $item->name ?? '';
  439. $nameBase64 = stringUtil::ossBase64($name);
  440. $staff = $this->shopAdmin;
  441. $staffName = $staff->name ?? '';
  442. $newStaffName = $staffName . ' 为您推荐';
  443. $staffNameBase64 = stringUtil::ossBase64($newStaffName);
  444. //花材主图
  445. $itemCover = $cover . '?x-oss-process=image/resize,m_fill,h_750,w_750';
  446. $itemCoverBase64 = stringUtil::ossBase64($itemCover);
  447. $logoBase64 = '';
  448. if (in_array($this->mainId, [52, 1459, 13495])) {
  449. $logo = 'poster/hyxh_ncd_logo.png?x-oss-process=image/resize,m_fill,h_90,w_90';
  450. if ($this->mainId == 1459) {
  451. $logo = 'poster/hyxh_gcd_logo.png?x-oss-process=image/resize,m_fill,h_90,w_90';
  452. }
  453. if ($this->mainId == 13495) {
  454. $logo = 'poster/hyxh_hzd_logo.png?x-oss-process=image/resize,m_fill,h_90,w_90';
  455. }
  456. $logoBase64 = stringUtil::ossBase64($logo);
  457. }
  458. $url = $prefix . 'poster/1234.jpg?x-oss-process=image';
  459. //花材主图
  460. $url .= '/watermark,image_' . $itemCoverBase64 . ',g_nw,x_0,y_0';
  461. //小程序码
  462. $url .= '/watermark,image_' . $miniCodeBase64 . ',g_se';
  463. if (!empty($logoBase64)) {
  464. //logo
  465. $url .= '/watermark,image_' . $logoBase64 . ',g_se,x_65,y_65';
  466. }
  467. //为你推荐
  468. $url .= '/watermark,text_' . $staffNameBase64 . ',g_sw,x_18,y_235,color_8B8989,type_d3F5LW1pY3JvaGVp,size_30';
  469. //标题
  470. $url .= '/watermark,text_' . $nameBase64 . ',g_sw,x_18,y_145';
  471. //价格
  472. $url .= '/watermark,text_' . $priceBase64 . ',g_sw,x_18,y_50,color_EE2C2C,type_d3F5LW1pY3JvaGVp,size_50';
  473. $respond = ['imgUrl' => $url];
  474. util::success($respond);
  475. }
  476. //是否有C级价格大于B级的 ssh 20221204
  477. public function actionGetErrorPrice()
  478. {
  479. $shop = $this->shop;
  480. $list = ItemClass::errorPrice($shop);
  481. util::success(['list' => $list]);
  482. }
  483. //列出所有花材,包括特价花材列表 ssh 20220315
  484. public function actionShowList()
  485. {
  486. $get = Yii::$app->request->get();
  487. $customId = $get['customId'] ?? 0;
  488. $custom = CustomClass::getById($customId, true);
  489. $where['mainId'] = $this->mainId;
  490. $where['delStatus'] = 0;
  491. $where['status'] = 1;
  492. $list = ItemClass::getShowList($where, $custom);
  493. $mainId = $this->mainId;
  494. $get = Yii::$app->request->get();
  495. $isCashier = $get['isCashier'] ?? 0;
  496. if ($isCashier == 1) {
  497. //去掉收银台提示价格更新
  498. $staffId = $this->shopAdminId;
  499. Yii::$app->redis->executeCommand('DEL', ['ghs_cashier_' . $mainId . '_' . $staffId . '_may_refresh']);
  500. }
  501. util::success(['list' => $list]);
  502. }
  503. public function actionAdd()
  504. {
  505. util::fail('开发中');
  506. }
  507. //批量添加花材
  508. public function actionBatchAdd()
  509. {
  510. $shopAdmin = $this->shopAdmin;
  511. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  512. util::fail('超管才能操作');
  513. }
  514. $post = Yii::$app->request->post();
  515. //[{"itemId":"1175","classId":"3548","price":"5","skPrice":"8","cost":"1","name":"卡娜百合"}]
  516. $data = $post['data'];
  517. if (empty($data)) {
  518. util::fail("请选花材");
  519. }
  520. $data = json_decode($data, true);
  521. if (is_array($data) == false || empty($data)) {
  522. util::fail("请选花材");
  523. }
  524. $shop = $this->shop;
  525. $shopId = $this->shopId;
  526. $mainId = $this->mainId;
  527. $sjId = $this->sjId;
  528. $adminId = $this->adminId;
  529. $ids = array_column($data, 'itemId');
  530. $ids = array_unique(array_filter($ids));
  531. $has = ProductClass::getByCondition(['mainId' => $mainId, 'itemId' => ['in', $ids]]);
  532. if (!empty($has)) {
  533. $hasName = $has->name ?? '';
  534. util::fail($hasName . " 已经添加过了");
  535. }
  536. $ptItemInfo = PtItemClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id');
  537. $connection = Yii::$app->db;
  538. $transaction = $connection->beginTransaction();
  539. try {
  540. foreach ($data as $v) {
  541. $ptItemId = $v['itemId'] ?? 0;
  542. $ratio = $ptItemInfo[$ptItemId]['ratio'] ?? 20;
  543. $shelfLife = $ptItemInfo[$ptItemId]['shelfLife'] ?? 7;
  544. $ratioType = $ptItemInfo[$ptItemId]['ratioType'] ?? 0;
  545. $variety = $ptItemInfo[$ptItemId]['variety'] ?? 0;
  546. $stockWarning = $ptItemInfo[$ptItemId]['stockWarning'] ?? 5;
  547. $weight = $ptItemInfo[$ptItemId]['weight'] ?? 1;
  548. $bigUnit = $ptItemInfo[$ptItemId]['bigUnit'] ?? '扎';
  549. $smallUnit = $ptItemInfo[$ptItemId]['smallUnit'] ?? '支';
  550. $bigUnitId = $ptItemInfo[$ptItemId]['bigUnitId'] ?? 1;
  551. $smallUnitId = $ptItemInfo[$ptItemId]['smallUnitId'] ?? 2;
  552. $cover = $ptItemInfo[$ptItemId]['cover'] ?? 'default-img.png';
  553. $v['ratio'] = $ratio;
  554. $v['cover'] = $cover;
  555. $v['shelfLife'] = $shelfLife;
  556. $v['ratioType'] = $ratioType;
  557. $v['variety'] = $variety;
  558. $v['stockWarning'] = $stockWarning;
  559. $v['weight'] = $weight;
  560. $v['bigUnit'] = $bigUnit;
  561. $v['smallUnit'] = $smallUnit;
  562. $v['bigUnitId'] = $bigUnitId;
  563. $v['smallUnitId'] = $smallUnitId;
  564. $v['adminId'] = $adminId;
  565. $v['sjId'] = $sjId;
  566. $v['shopId'] = $shopId;
  567. $v['mainId'] = $mainId;
  568. $v['staffName'] = $shopAdmin->name ?? '';
  569. $name = $v['name'] ?? '';
  570. $price = is_numeric($v['price']) ? $v['price'] : 0;
  571. $skPrice = is_numeric($v['skPrice']) ? $v['skPrice'] : 0;
  572. $cost = is_numeric($v['cost']) ? $v['cost'] : 0;
  573. if ($price <= 0) {
  574. util::fail("请填写{$name}的批发价");
  575. }
  576. if ($skPrice <= 0) {
  577. util::fail("请填写{$name}的零售价");
  578. }
  579. if ($cost <= 0) {
  580. util::fail("请填写{$name}的成本价");
  581. }
  582. if ($price > $skPrice) {
  583. util::fail("{$name}的批发价比零售价还高");
  584. }
  585. if ($cost > $price) {
  586. util::fail("{$name}的成本价比批发价还高");
  587. }
  588. $skMore = bcsub($skPrice, $price, 2);
  589. $addPrice = bcsub($price, $cost, 2);
  590. $v['skMore'] = $skMore;
  591. $v['addPrice'] = $addPrice;
  592. $v['hjAddPrice'] = $addPrice;
  593. $v['hjPrice'] = $price;
  594. ProductClass::addProduct($v, $shop);
  595. }
  596. $transaction->commit();
  597. util::complete('添加成功');
  598. } catch (Exception $e) {
  599. $transaction->rollBack();
  600. util::fail();
  601. }
  602. }
  603. public function actionDelete()
  604. {
  605. util::fail('不能删除哦!');
  606. }
  607. //获取平台里的花材信息
  608. public function actionPlatformItem()
  609. {
  610. $py = Yii::$app->request->get('py', '');
  611. $where = [];
  612. if ($py) {
  613. $where = ['py' => $py];
  614. }
  615. $list = PtItemClass::getItemList($where);
  616. util::success($list);
  617. }
  618. //列出需要进行颜色管理的花材 ssh 20220820
  619. public function actionColorList()
  620. {
  621. $mainId = $this->mainId ?? 0;
  622. $list = ItemClass::getAllByCondition(['mainId' => $mainId, 'variety' => 1], null, '*');
  623. util::success(['list' => $list]);
  624. }
  625. //多颜色管理启用与停用 ssh 20221229
  626. public function actionChangeVariety()
  627. {
  628. $get = Yii::$app->request->get();
  629. $id = $get['id'] ?? 0;
  630. $variety = $get['variety'] ?? 0;
  631. $info = ItemClass::getById($id, true);
  632. if (empty($info) || $info->mainId != $this->mainId) {
  633. util::fail('操作失败');
  634. }
  635. $info->variety = $variety;
  636. $info->save();
  637. util::complete('操作成功');
  638. }
  639. //拆散花材 ssh 20221229
  640. public function actionBreakItem()
  641. {
  642. util::fail('开发中');
  643. util::complete();
  644. }
  645. //批量修改花材的负责人 ssh 20240718
  646. public function actionChangeCgStaff()
  647. {
  648. $get = Yii::$app->request->get();
  649. $beforeId = $get['beforeId'] ?? 0;
  650. $newId = $get['newId']??0;
  651. if(empty($beforeId) || empty($newId)){
  652. util::fail('员工信息有问题');
  653. }
  654. $new = ShopAdminClass::getById($newId,true);
  655. if(empty($new) || $new->mainId != $this->mainId){
  656. util::fail('错误的员工信息');
  657. }
  658. $before = ShopAdminClass::getById($beforeId,true);
  659. if(empty($before) || $before->mainId != $this->mainId){
  660. util::fail('错误的员工信息2');
  661. }
  662. $newName = $new->name??'';
  663. ItemClass::updateByCondition(['mainId'=>$this->mainId,'cgStaffId'=>$beforeId],['cgStaffId'=>$newId,'cgStaffName'=>$newName]);
  664. util::complete('修改成功');
  665. }
  666. }