ItemController.php 26 KB

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