ItemController.php 25 KB

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