ItemController.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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. $where = ['mainId' => $this->mainId, 'delStatus' => 0];
  197. if (!empty($name)) {
  198. if (stringUtil::isLetter($name)) {
  199. $where['py'] = ['like', $name];
  200. } else {
  201. $where['name'] = ['like', $name];
  202. }
  203. }
  204. $respond = ItemClass::getSimpleList($where);
  205. util::success($respond);
  206. }
  207. public function actionChangeFrontHide()
  208. {
  209. $get = Yii::$app->request->get();
  210. $id = $get['id'] ?? 0;
  211. $status = $get['status'] ?? '';
  212. if (is_numeric($status) == false) {
  213. util::fail('请选择方式');
  214. }
  215. $info = ItemClass::getById($id, true);
  216. if (empty($info)) {
  217. util::fail('没有找到花材');
  218. }
  219. if ($info->mainId != $this->mainId) {
  220. util::fail('无法操作');
  221. }
  222. $info->frontHide = $status;
  223. $info->save();
  224. util::complete();
  225. }
  226. //检测是否要刷新
  227. public function actionCheckRefresh()
  228. {
  229. $mainId = $this->mainId;
  230. $staffId = $this->shopAdminId;
  231. $respond = Yii::$app->redis->executeCommand('GET', ['ghs_cashier_' . $mainId . '_' . $staffId . '_may_refresh']);
  232. if ($respond == 'yes') {
  233. util::success(['remind' => 1]);
  234. }
  235. util::complete();
  236. }
  237. //批量恢复花材 ssh 20230206
  238. public function actionBatchRecover()
  239. {
  240. $post = Yii::$app->request->post();
  241. $string = $post['ids'] ?? '';
  242. $ids = json_decode($string, true);
  243. $ids = array_unique(array_filter($ids));
  244. if (empty($ids)) {
  245. util::fail('请选择花材');
  246. }
  247. $infoList = ItemClass::getAllByCondition(['id' => ['in', $ids]], null, 'id,name,mainId,status,delStatus,removeStatus', null, true);
  248. if (empty($infoList)) {
  249. util::fail('请选择花材哦');
  250. }
  251. foreach ($infoList as $info) {
  252. if ($info->mainId != $this->mainId) {
  253. util::fail('请选择自己的花材');
  254. }
  255. $info->removeStatus = 0;
  256. $info->save();
  257. }
  258. util::complete('操作成功');
  259. }
  260. //批量删除花材 ssh 202302026
  261. public function actionBatchRemove()
  262. {
  263. $post = Yii::$app->request->post();
  264. $string = $post['ids'] ?? '';
  265. $ids = json_decode($string, true);
  266. $ids = array_unique(array_filter($ids));
  267. if (empty($ids)) {
  268. util::fail('请选择花材');
  269. }
  270. $infoList = ItemClass::getAllByCondition(['id' => ['in', $ids]], null, 'id,name,mainId,status,delStatus,removeStatus', null, true);
  271. if (empty($infoList)) {
  272. util::fail('请选择花材哦');
  273. }
  274. foreach ($infoList as $info) {
  275. if ($info->mainId != $this->mainId) {
  276. util::fail('请选择自己的花材');
  277. }
  278. $name = $info->name ?? '';
  279. if ($info->delStatus != 1) {
  280. util::fail($name . ' 停用了才能删除');
  281. }
  282. $info->removeStatus = 1;
  283. $info->save();
  284. }
  285. util::complete('操作成功');
  286. }
  287. //批量启用花材 ssh 20230206
  288. public function actionBatchEnable()
  289. {
  290. $post = Yii::$app->request->post();
  291. $string = $post['ids'] ?? '';
  292. $ids = json_decode($string, true);
  293. $ids = array_unique(array_filter($ids));
  294. if (empty($ids)) {
  295. util::fail('请选择花材');
  296. }
  297. $infoList = ItemClass::getAllByCondition(['id' => ['in', $ids]], null, 'id,name,mainId,status,delStatus,removeStatus', null, true);
  298. if (empty($infoList)) {
  299. util::fail('请选择花材哦');
  300. }
  301. foreach ($infoList as $info) {
  302. if ($info->mainId != $this->mainId) {
  303. util::fail('请选择自己的花材');
  304. }
  305. $info->delStatus = 0;
  306. $info->save();
  307. }
  308. util::complete('操作成功');
  309. }
  310. //批量停用花材 20230206
  311. public function actionBatchStop()
  312. {
  313. $post = Yii::$app->request->post();
  314. $string = $post['ids'] ?? '';
  315. $ids = json_decode($string, true);
  316. $ids = array_unique(array_filter($ids));
  317. if (empty($ids)) {
  318. util::fail('请选择花材');
  319. }
  320. $infoList = ItemClass::getAllByCondition(['id' => ['in', $ids]], null, 'id,name,mainId,status,delStatus,removeStatus', null, true);
  321. if (empty($infoList)) {
  322. util::fail('请选择花材哦');
  323. }
  324. foreach ($infoList as $info) {
  325. if ($info->mainId != $this->mainId) {
  326. util::fail('请选择自己的花材');
  327. }
  328. $name = $info->name ?? '';
  329. if ($info->status != 2) {
  330. util::fail($name . ' 下载了才能停用');
  331. }
  332. $info->delStatus = 1;
  333. $info->save();
  334. }
  335. util::complete('操作成功');
  336. }
  337. //获取给散客下单的花材海报 ssh 20220111
  338. public function actionGetSkPoster()
  339. {
  340. $get = Yii::$app->request->get();
  341. $id = $get['id'] ?? 0;
  342. $item = ItemClass::getById($id, true);
  343. if (empty($item) || $item->mainId != $this->mainId) {
  344. util::fail('获取失败');
  345. }
  346. $cover = $item->cover ?? '';
  347. if (empty($cover)) {
  348. util::fail('获取失败');
  349. }
  350. $merchant = WxOpenClass::getMallWxInfo();
  351. $page = 'pages/item/detail';
  352. $ptStyle = dict::getDict('ptStyle', 'mall');
  353. $shop = $this->shop;
  354. $lsShopId = $shop->lsShopId ?? 0;
  355. //scene不能超过32个字条
  356. $scene = "id=" . $id . '&a=' . $lsShopId;
  357. $miniCode = miniUtil::generateUnlimitedMiniCode($merchant, $page, $scene, $ptStyle);
  358. $miniCode = $miniCode . '?x-oss-process=image/resize,m_fill,h_200,w_200';
  359. $miniCodeBase64 = stringUtil::ossBase64($miniCode);
  360. Yii::$app->params['ptStyle'] = dict::getDict('ptStyle', 'hd');
  361. $prefix = imgUtil::getPrefix();
  362. $price = $item->skPrice ?? 0;
  363. if ($item->discountPrice > 0) {
  364. $skMore = $item->skMore ?? 0;
  365. $price = bcadd($item->discountPrice, $skMore, 2);
  366. }
  367. $newPrice = '¥' . floatval($price);
  368. $priceBase64 = stringUtil::ossBase64($newPrice);
  369. $name = $item->name ?? '';
  370. $nameBase64 = stringUtil::ossBase64($name);
  371. $staff = $this->shopAdmin;
  372. $staffName = $staff->name ?? '';
  373. $newStaffName = $staffName . ' 为您推荐';
  374. $staffNameBase64 = stringUtil::ossBase64($newStaffName);
  375. //花材主图
  376. $itemCover = $cover . '?x-oss-process=image/resize,m_fill,h_750,w_750';
  377. $itemCoverBase64 = stringUtil::ossBase64($itemCover);
  378. $logoBase64 = '';
  379. if (in_array($this->mainId, [52, 1459, 13495])) {
  380. $logo = 'poster/hyxh_ncd_logo.png?x-oss-process=image/resize,m_fill,h_90,w_90';
  381. if ($this->mainId == 1459) {
  382. $logo = 'poster/hyxh_gcd_logo.png?x-oss-process=image/resize,m_fill,h_90,w_90';
  383. }
  384. if ($this->mainId == 13495) {
  385. $logo = 'poster/hyxh_hzd_logo.png?x-oss-process=image/resize,m_fill,h_90,w_90';
  386. }
  387. $logoBase64 = stringUtil::ossBase64($logo);
  388. }
  389. $url = $prefix . 'poster/1234.jpg?x-oss-process=image';
  390. //花材主图
  391. $url .= '/watermark,image_' . $itemCoverBase64 . ',g_nw,x_0,y_0';
  392. //小程序码
  393. $url .= '/watermark,image_' . $miniCodeBase64 . ',g_se';
  394. if (!empty($logoBase64)) {
  395. //logo
  396. $url .= '/watermark,image_' . $logoBase64 . ',g_se,x_65,y_65';
  397. }
  398. //为你推荐
  399. $url .= '/watermark,text_' . $staffNameBase64 . ',g_sw,x_18,y_235,color_8B8989,type_d3F5LW1pY3JvaGVp,size_30';
  400. //标题
  401. $url .= '/watermark,text_' . $nameBase64 . ',g_sw,x_18,y_145';
  402. //价格
  403. $url .= '/watermark,text_' . $priceBase64 . ',g_sw,x_18,y_50,color_EE2C2C,type_d3F5LW1pY3JvaGVp,size_50';
  404. $respond = ['imgUrl' => $url];
  405. util::success($respond);
  406. }
  407. //获取给花店下单的花材海报 ssh 20220111
  408. public function actionGetHdPoster()
  409. {
  410. $get = Yii::$app->request->get();
  411. $id = $get['id'] ?? 0;
  412. $item = ItemClass::getById($id, true);
  413. if (empty($item) || $item->mainId != $this->mainId) {
  414. util::fail('获取失败');
  415. }
  416. $cover = $item->cover ?? '';
  417. if (empty($cover)) {
  418. util::fail('获取失败');
  419. }
  420. $merchant = WxOpenClass::getWxInfo();
  421. $page = 'admin/ghsProduct/detail';
  422. $ptStyle = dict::getDict('ptStyle', 'hd');
  423. //小程序码,scene不能超过32个字条
  424. $scene = "id=" . $id . '&sId=' . $this->shopId;
  425. $miniCode = miniUtil::generateUnlimitedMiniCode($merchant, $page, $scene, $ptStyle);
  426. $miniCode = $miniCode . '?x-oss-process=image/resize,m_fill,h_200,w_200';
  427. $miniCodeBase64 = stringUtil::ossBase64($miniCode);
  428. Yii::$app->params['ptStyle'] = dict::getDict('ptStyle', 'ghs');
  429. $prefix = imgUtil::getPrefix();
  430. $price = $item->price ?? 0;
  431. if ($item->discountPrice > 0) {
  432. $price = $item->discountPrice;
  433. }
  434. $newPrice = '¥' . floatval($price);
  435. $priceBase64 = stringUtil::ossBase64($newPrice);
  436. $name = $item->name ?? '';
  437. $nameBase64 = stringUtil::ossBase64($name);
  438. $staff = $this->shopAdmin;
  439. $staffName = $staff->name ?? '';
  440. $newStaffName = $staffName . ' 为您推荐';
  441. $staffNameBase64 = stringUtil::ossBase64($newStaffName);
  442. //花材主图
  443. $itemCover = $cover . '?x-oss-process=image/resize,m_fill,h_750,w_750';
  444. $itemCoverBase64 = stringUtil::ossBase64($itemCover);
  445. $logoBase64 = '';
  446. if (in_array($this->mainId, [52, 1459, 13495])) {
  447. $logo = 'poster/hyxh_ncd_logo.png?x-oss-process=image/resize,m_fill,h_90,w_90';
  448. if ($this->mainId == 1459) {
  449. $logo = 'poster/hyxh_gcd_logo.png?x-oss-process=image/resize,m_fill,h_90,w_90';
  450. }
  451. if ($this->mainId == 13495) {
  452. $logo = 'poster/hyxh_hzd_logo.png?x-oss-process=image/resize,m_fill,h_90,w_90';
  453. }
  454. $logoBase64 = stringUtil::ossBase64($logo);
  455. }
  456. $url = $prefix . 'poster/1234.jpg?x-oss-process=image';
  457. //花材主图
  458. $url .= '/watermark,image_' . $itemCoverBase64 . ',g_nw,x_0,y_0';
  459. //小程序码
  460. $url .= '/watermark,image_' . $miniCodeBase64 . ',g_se';
  461. if (!empty($logoBase64)) {
  462. //logo
  463. $url .= '/watermark,image_' . $logoBase64 . ',g_se,x_65,y_65';
  464. }
  465. //为你推荐
  466. $url .= '/watermark,text_' . $staffNameBase64 . ',g_sw,x_18,y_235,color_8B8989,type_d3F5LW1pY3JvaGVp,size_30';
  467. //标题
  468. $url .= '/watermark,text_' . $nameBase64 . ',g_sw,x_18,y_145';
  469. //价格
  470. $url .= '/watermark,text_' . $priceBase64 . ',g_sw,x_18,y_50,color_EE2C2C,type_d3F5LW1pY3JvaGVp,size_50';
  471. $respond = ['imgUrl' => $url];
  472. util::success($respond);
  473. }
  474. //是否有C级价格大于B级的 ssh 20221204
  475. public function actionGetErrorPrice()
  476. {
  477. $shop = $this->shop;
  478. $list = ItemClass::errorPrice($shop);
  479. util::success(['list' => $list]);
  480. }
  481. //列出所有花材,包括特价花材列表 ssh 20220315
  482. public function actionShowList()
  483. {
  484. $get = Yii::$app->request->get();
  485. $customId = $get['customId'] ?? 0;
  486. $custom = CustomClass::getById($customId, true);
  487. $where['mainId'] = $this->mainId;
  488. $where['delStatus'] = 0;
  489. $where['status'] = 1;
  490. $list = ItemClass::getShowList($where, $custom);
  491. $mainId = $this->mainId;
  492. $get = Yii::$app->request->get();
  493. $isCashier = $get['isCashier'] ?? 0;
  494. if ($isCashier == 1) {
  495. //去掉收银台提示价格更新
  496. $staffId = $this->shopAdminId;
  497. Yii::$app->redis->executeCommand('DEL', ['ghs_cashier_' . $mainId . '_' . $staffId . '_may_refresh']);
  498. }
  499. util::success(['list' => $list]);
  500. }
  501. public function actionAdd()
  502. {
  503. util::fail('开发中');
  504. }
  505. //批量添加花材
  506. public function actionBatchAdd()
  507. {
  508. $shopAdmin = $this->shopAdmin;
  509. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  510. util::fail('超管才能操作');
  511. }
  512. $post = Yii::$app->request->post();
  513. //[{"itemId":"1175","classId":"3548","price":"5","skPrice":"8","cost":"1","name":"卡娜百合"}]
  514. $data = $post['data'];
  515. if (empty($data)) {
  516. util::fail("请选花材");
  517. }
  518. $data = json_decode($data, true);
  519. if (is_array($data) == false || empty($data)) {
  520. util::fail("请选花材");
  521. }
  522. $shop = $this->shop;
  523. $shopId = $this->shopId;
  524. $mainId = $this->mainId;
  525. $sjId = $this->sjId;
  526. $adminId = $this->adminId;
  527. $ids = array_column($data, 'itemId');
  528. $ids = array_unique(array_filter($ids));
  529. $has = ProductClass::getByCondition(['mainId' => $mainId, 'itemId' => ['in', $ids]]);
  530. if (!empty($has)) {
  531. $hasName = $has->name ?? '';
  532. util::fail($hasName . " 已经添加过了");
  533. }
  534. $ptItemInfo = PtItemClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id');
  535. $connection = Yii::$app->db;
  536. $transaction = $connection->beginTransaction();
  537. try {
  538. foreach ($data as $v) {
  539. $ptItemId = $v['itemId'] ?? 0;
  540. $ratio = $ptItemInfo[$ptItemId]['ratio'] ?? 20;
  541. $shelfLife = $ptItemInfo[$ptItemId]['shelfLife'] ?? 7;
  542. $ratioType = $ptItemInfo[$ptItemId]['ratioType'] ?? 0;
  543. $variety = $ptItemInfo[$ptItemId]['variety'] ?? 0;
  544. $stockWarning = $ptItemInfo[$ptItemId]['stockWarning'] ?? 5;
  545. $weight = $ptItemInfo[$ptItemId]['weight'] ?? 1;
  546. $bigUnit = $ptItemInfo[$ptItemId]['bigUnit'] ?? '扎';
  547. $smallUnit = $ptItemInfo[$ptItemId]['smallUnit'] ?? '支';
  548. $bigUnitId = $ptItemInfo[$ptItemId]['bigUnitId'] ?? 1;
  549. $smallUnitId = $ptItemInfo[$ptItemId]['smallUnitId'] ?? 2;
  550. $cover = $ptItemInfo[$ptItemId]['cover'] ?? 'default-img.png';
  551. $v['ratio'] = $ratio;
  552. $v['cover'] = $cover;
  553. $v['shelfLife'] = $shelfLife;
  554. $v['ratioType'] = $ratioType;
  555. $v['variety'] = $variety;
  556. $v['stockWarning'] = $stockWarning;
  557. $v['weight'] = $weight;
  558. $v['bigUnit'] = $bigUnit;
  559. $v['smallUnit'] = $smallUnit;
  560. $v['bigUnitId'] = $bigUnitId;
  561. $v['smallUnitId'] = $smallUnitId;
  562. $v['adminId'] = $adminId;
  563. $v['sjId'] = $sjId;
  564. $v['shopId'] = $shopId;
  565. $v['mainId'] = $mainId;
  566. $v['staffName'] = $shopAdmin->name ?? '';
  567. $name = $v['name'] ?? '';
  568. $price = is_numeric($v['price']) ? $v['price'] : 0;
  569. $skPrice = is_numeric($v['skPrice']) ? $v['skPrice'] : 0;
  570. $cost = is_numeric($v['cost']) ? $v['cost'] : 0;
  571. if ($price <= 0) {
  572. util::fail("请填写{$name}的批发价");
  573. }
  574. if ($skPrice <= 0) {
  575. util::fail("请填写{$name}的零售价");
  576. }
  577. if ($cost <= 0) {
  578. util::fail("请填写{$name}的成本价");
  579. }
  580. if ($price > $skPrice) {
  581. util::fail("{$name}的批发价比零售价还高");
  582. }
  583. if ($cost > $price) {
  584. util::fail("{$name}的成本价比批发价还高");
  585. }
  586. $skMore = bcsub($skPrice, $price, 2);
  587. $addPrice = bcsub($price, $cost, 2);
  588. $v['skMore'] = $skMore;
  589. $v['addPrice'] = $addPrice;
  590. $v['hjAddPrice'] = $addPrice;
  591. $v['hjPrice'] = $price;
  592. ProductClass::addProduct($v, $shop);
  593. }
  594. $transaction->commit();
  595. util::complete('添加成功');
  596. } catch (Exception $e) {
  597. $transaction->rollBack();
  598. util::fail();
  599. }
  600. }
  601. public function actionDelete()
  602. {
  603. util::fail('不能删除哦!');
  604. }
  605. //获取平台里的花材信息
  606. public function actionPlatformItem()
  607. {
  608. $py = Yii::$app->request->get('py', '');
  609. $where = [];
  610. if ($py) {
  611. $where = ['py' => $py];
  612. }
  613. $list = PtItemClass::getItemList($where);
  614. util::success($list);
  615. }
  616. //列出需要进行颜色管理的花材 ssh 20220820
  617. public function actionColorList()
  618. {
  619. $mainId = $this->mainId ?? 0;
  620. $list = ItemClass::getAllByCondition(['mainId' => $mainId, 'variety' => 1], null, '*');
  621. util::success(['list' => $list]);
  622. }
  623. //多颜色管理启用与停用 ssh 20221229
  624. public function actionChangeVariety()
  625. {
  626. $get = Yii::$app->request->get();
  627. $id = $get['id'] ?? 0;
  628. $variety = $get['variety'] ?? 0;
  629. $info = ItemClass::getById($id, true);
  630. if (empty($info) || $info->mainId != $this->mainId) {
  631. util::fail('操作失败');
  632. }
  633. $info->variety = $variety;
  634. $info->save();
  635. util::complete('操作成功');
  636. }
  637. //拆散花材 ssh 20221229
  638. public function actionBreakItem()
  639. {
  640. util::fail('开发中');
  641. util::complete();
  642. }
  643. }