ProductController.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. <?php
  2. /**
  3. * User: admin
  4. * Date Time: 2021/1/15 20:36
  5. */
  6. namespace ghs\controllers;
  7. use biz\admin\classes\AdminRoleClass;
  8. use biz\ghs\classes\GhsClass;
  9. use bizGhs\custom\classes\CustomClass;
  10. use bizGhs\item\classes\ItemClassClass;
  11. use bizGhs\merchant\classes\ShopClass;
  12. use bizGhs\product\classes\ProductClass;
  13. use bizGhs\product\models\Product;
  14. use bizGhs\product\services\ProductService;
  15. use common\components\stringUtil;
  16. use common\components\util;
  17. use Yii;
  18. class ProductController extends BaseController
  19. {
  20. public $guestAccess = ['index'];
  21. //按分类列出所有花材
  22. public function actionIndex()
  23. {
  24. $py = Yii::$app->request->get('py', '');
  25. $type = Yii::$app->request->get('type', '');
  26. //默认只显示上架的商品
  27. $status = Yii::$app->request->get('status', 1);
  28. //获取所有当前供应商的分类 (全部、常用)
  29. //根据分类组装分类下的花材信息
  30. $classIds = ItemClassClass::getGhsItemClassAll($this->sjId);
  31. $where['sjId'] = $this->sjId;
  32. if ($py) {
  33. $pyName = strtolower($py);
  34. $where['py'] = $pyName;
  35. }
  36. $shopId = $this->shopId;
  37. //兼容客户根据门店下单
  38. if ($this->lookShopId) {
  39. $shopId = $this->lookShopId;
  40. }
  41. //库存预警
  42. if ($type == 'waring') {
  43. //shopId 改为可以前端传,默认是当前shopId linqh 2021.1.26
  44. $shopId = Yii::$app->request->get('shopId', 0);
  45. if (!$shopId) {
  46. $shopId = $this->shopId;
  47. }
  48. }
  49. $where['shopId'] = $shopId;
  50. if (!empty($status)) {
  51. $where['status'] = $status;
  52. }
  53. $where['delStatus'] = 0;
  54. //客户等级
  55. $customId = Yii::$app->request->get('customId', 0);
  56. $level = 1;
  57. if (!empty($customId)) {
  58. $custom = CustomClass::getById($customId, true);
  59. $level = $custom->level ?? 1;
  60. }
  61. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], "*");
  62. $itemInfoData = ProductClass::groupProductInfo($itemInfoData, $level);
  63. $data = ProductService::assembleData($classIds, $itemInfoData, true);
  64. util::success($data);
  65. }
  66. //所有花材分页和不分页
  67. public function actionList()
  68. {
  69. $where = [];
  70. $where['sjId'] = $this->sjId;
  71. $classId = Yii::$app->request->get('classId', 0);
  72. if (!empty($classId)) {
  73. $where['classId'] = $classId;
  74. }
  75. $name = Yii::$app->request->get('name', '');
  76. if (empty($name)) {
  77. $py = Yii::$app->request->get('py', '');
  78. $name = !empty($py) ? $py : '';
  79. }
  80. if (!empty($name)) {
  81. if (stringUtil::isLetter($name)) {
  82. $where['py'] = ['like', $name];
  83. } else {
  84. $where['name'] = ['like', $name];
  85. }
  86. }
  87. $status = Yii::$app->request->get('status', 0);
  88. if (!empty($status)) {
  89. $where['status'] = $status;
  90. }
  91. $delStatus = Yii::$app->request->get('delStatus', 0);
  92. if ($delStatus != 2) {
  93. $where['delStatus'] = $delStatus;
  94. }
  95. $shopId = Yii::$app->request->get('shopId', 0);
  96. if (!$shopId) {
  97. $shopId = $this->shopId;
  98. }
  99. $where['shopId'] = $shopId;
  100. $productIds = Yii::$app->request->get('ids', '');
  101. if (!empty($productIds)) {
  102. $productIdArr = explode(',', $productIds);
  103. if (!empty($productIdArr)) {
  104. $where['id'] = ['in', $productIdArr];
  105. }
  106. }
  107. //输出标准会员价,即花店价,默认显示的是花店价
  108. $level = 1;
  109. $type = Yii::$app->request->get('type', '');
  110. $showPage = Yii::$app->request->get('showPage', 0);
  111. if ($showPage) {
  112. if ($type == 'waring') {
  113. $pro = Product::find()->where("shopId={$shopId}")->andWhere('`stock`<`stockWarning`')->select('id')->asArray()->all();
  114. $lackIds = array_column($pro, 'id');
  115. if (empty($lackIds)) {
  116. util::success([]);
  117. }
  118. $lackIds = array_filter(array_unique($lackIds));
  119. $where['id'] = ['in', $lackIds];
  120. $respond = ProductClass::getList("*", $where, "actualSold desc");
  121. } else {
  122. $respond = ProductClass::getList("*", $where, "inTurn desc");
  123. }
  124. $respond['list'] = ProductClass::groupProductInfo($respond['list'], $level);
  125. $respond['list'] = ProductClass::groupClassName($this->sjId, $respond['list']);
  126. util::success($respond);
  127. } else {
  128. if ($type == 'waring') {
  129. //库存预警 按库存从小到大排序
  130. $data = ProductClass::getAllByCondition($where, ['stock' => SORT_ASC, 'inTurn' => SORT_DESC], "*");
  131. } else {
  132. if (!empty($productIds)) {
  133. //优先按 $productIds 排序 "FIELD(`id`,4,5,6)"=>true
  134. $data = ProductClass::getAllByCondition($where, ["FIELD(`id`,$productIds)" => true, 'inTurn' => SORT_DESC], "*");
  135. } else {
  136. $data = ProductClass::getAllByCondition($where, ['actualSold' => SORT_DESC, 'inTurn' => SORT_DESC], "*");
  137. }
  138. }
  139. $data = ProductClass::groupProductInfo($data, $level);
  140. $data = ProductClass::groupClassName($this->sjId, $data);
  141. if ($type == 'waring') {
  142. //库存预警
  143. $newData = [];
  144. foreach ($data as $v) {
  145. if ($v['stock'] <= $v['stockWarning']) {
  146. $newData[] = $v;
  147. }
  148. }
  149. util::success(['list' => $newData]);
  150. }
  151. util::success(['list' => $data]);
  152. }
  153. }
  154. //所有花材
  155. public function actionAll()
  156. {
  157. $where = [];
  158. $where['sjId'] = $this->sjId;
  159. $where['shopId'] = $this->shopId;
  160. $status = Yii::$app->request->get('status', 0);
  161. if (!empty($status)) {
  162. $where['status'] = $status;
  163. }
  164. $delStatus = Yii::$app->request->get('delStatus', 0);
  165. if ($delStatus != 2) {
  166. $where['delStatus'] = $delStatus;
  167. }
  168. //输出标准会员价,即花店价,默认显示的是花店价
  169. $level = 1;
  170. $data = ProductClass::getAllByCondition($where, ['actualSold' => SORT_DESC, 'inTurn' => SORT_DESC], "*");
  171. $data = ProductClass::groupProductInfo($data, $level);
  172. util::success(['list' => $data]);
  173. }
  174. //批量改价
  175. public function actionBatchChangePrice()
  176. {
  177. $shopAdmin = $this->shopAdmin;
  178. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  179. util::fail('超管才能修改');
  180. }
  181. //花材价格只能在首店修改,并同步到分店
  182. $shop = $this->shop;
  183. $default = $shop->default ?? 0;
  184. if ($default != 1) {
  185. util::fail('请在首店修改');
  186. }
  187. $data = Yii::$app->request->post('data', '');
  188. if (empty($data)) {
  189. util::fail('没有修改');
  190. }
  191. $arr = json_decode($data, true);
  192. if (empty($arr)) {
  193. util::fail('没有修改!');
  194. }
  195. $connection = Yii::$app->db;
  196. $transaction = $connection->beginTransaction();
  197. try {
  198. $ids = array_column($arr, 'id');
  199. $ids = array_unique(array_filter($ids));
  200. if (empty($ids)) {
  201. util::fail('请选择花材');
  202. }
  203. $productList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id', true);
  204. foreach ($arr as $product) {
  205. if (isset($product['price']) == false) {
  206. util::fail('没有价格');
  207. }
  208. if (isset($product['id']) == false) {
  209. util::fail('没有花材');
  210. }
  211. $price = $product['price'];
  212. $productId = $product['id'];
  213. $product = $productList[$productId] ?? [];
  214. if (empty($product)) {
  215. util::fail('没有找到花材');
  216. }
  217. if (isset($product->shopId) == false || $product->shopId != $this->shopId) {
  218. util::fail('没有权限访问');
  219. }
  220. if (empty($price)) {
  221. continue;
  222. }
  223. ProductClass::changePrice($product, $price, ProductClass::PRICE_LABEL_CHANGE);
  224. }
  225. $transaction->commit();
  226. util::complete('修改成功');
  227. } catch (\Exception $e) {
  228. $transaction->rollBack();
  229. Yii::info("修改失败原因:" . $e->getMessage());
  230. util::fail('修改失败');
  231. }
  232. }
  233. //改价
  234. public function actionChangePrice()
  235. {
  236. $shopAdmin = $this->shopAdmin;
  237. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  238. util::fail('超管才能修改');
  239. }
  240. //花材价格只能在首店修改,并同步到分店
  241. $shop = $this->shop;
  242. $default = $shop->default ?? 0;
  243. if ($default != 1) {
  244. util::fail('请在首店修改');
  245. }
  246. $productId = Yii::$app->request->post('productId', 0);
  247. $productInfo = ProductClass::getById($productId, true);
  248. if (empty($productInfo)) {
  249. util::fail("花材不存在");
  250. }
  251. if ($productInfo->shopId != $this->shopId) {
  252. util::fail('没有权限');
  253. }
  254. $price = Yii::$app->request->post('price', '');
  255. if (is_numeric($price) && $price > 0) {
  256. ProductClass::changePrice($productInfo, $price, ProductClass::PRICE_LABEL_CHANGE);
  257. } else {
  258. $price = bcadd($productInfo->cost, $productInfo->addPrice, 2);
  259. ProductClass::changePrice($productInfo, $price, ProductClass::PRICE_LABEL_AUTO);
  260. }
  261. util::success(['price' => $price]);
  262. }
  263. //恢复自动调价
  264. public function actionAutoPrice()
  265. {
  266. $productId = Yii::$app->request->post('productId', 0);
  267. $productData = ProductClass::getById($productId);
  268. if (!$productData) {
  269. util::fail("花材不存在");
  270. }
  271. //2021.04.06改为用product中的成本价+加价
  272. $price = bcadd($productData['cost'], $productData['addPrice'], 2);
  273. ProductClass::changePrice($productId, $this->shopId, $price, ProductClass::PRICE_LABEL_AUTO);
  274. util::complete("操作成功");
  275. }
  276. //修改加价
  277. public function actionChangeAddPrice()
  278. {
  279. $productId = Yii::$app->request->post('productId', 0);
  280. $addPrice = Yii::$app->request->post('addPrice', '');
  281. if ($addPrice < 0) {
  282. util::fail("加价不能小于0");
  283. }
  284. $productData = ProductClass::getProductData($productId, $this->shopId);
  285. if (!$productData) {
  286. util::fail("花材不存在");
  287. }
  288. ProductClass::changeAddPrice($productId, $addPrice);
  289. util::complete();
  290. }
  291. //批量修改更多,包括排序、重量和保质期 shish 20211211
  292. public function actionBatchChangeMore()
  293. {
  294. $shopAdmin = $this->shopAdmin;
  295. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  296. util::fail('超管才能操作');
  297. }
  298. $addData = Yii::$app->request->post('addData', '');
  299. if (empty($addData)) {
  300. util::fail('没有花材');
  301. }
  302. $itemData = json_decode($addData, true);
  303. if (is_array($itemData) == false) {
  304. util::fail('没有花材...');
  305. }
  306. $ids = array_column($itemData, 'id');
  307. $infoList = ProductClass::getByIds($ids);
  308. if (empty($infoList)) {
  309. util::fail('没有花材');
  310. }
  311. foreach ($infoList as $info) {
  312. if (isset($info['shopId']) == false || $info['shopId'] != $this->shopId) {
  313. util::fail('非常访问,不是你的花材不能修改');
  314. }
  315. }
  316. foreach ($itemData as $key => $data) {
  317. $id = $data['id'] ?? 0;
  318. ProductClass::updateById($id, $data);
  319. }
  320. util::complete('修改成功');
  321. }
  322. //批量修改加价 shish 20211211
  323. public function actionBatchChangeAddPrice()
  324. {
  325. $shopAdmin = $this->shopAdmin;
  326. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  327. util::fail('超管才能操作');
  328. }
  329. $addData = Yii::$app->request->post('addData', '');
  330. if (empty($addData)) {
  331. util::fail('没有花材');
  332. }
  333. $itemData = json_decode($addData, true);
  334. if (is_array($itemData) == false) {
  335. util::fail('没有花材...');
  336. }
  337. $ids = array_column($itemData, 'id');
  338. $infoList = ProductClass::getByIds($ids);
  339. if (empty($infoList)) {
  340. util::fail('没有花材');
  341. }
  342. foreach ($infoList as $info) {
  343. if (isset($info['shopId']) == false || $info['shopId'] != $this->shopId) {
  344. util::fail('非常访问,不是你的花材不能修改');
  345. }
  346. }
  347. foreach ($itemData as $key => $data) {
  348. $id = $data['id'] ?? 0;
  349. ProductClass::updateById($id, $data);
  350. }
  351. util::complete('修改成功');
  352. }
  353. //批量修改product 排序,成本价,加价
  354. public function actionBatchUpdate()
  355. {
  356. $shopAdmin = $this->shopAdmin;
  357. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  358. util::fail('超管才能修改');
  359. }
  360. //花材价格只能在首店修改,并同步到分店
  361. $shop = $this->shop;
  362. $default = $shop->default ?? 0;
  363. if ($default != 1) {
  364. util::fail('请在首店修改');
  365. }
  366. $data = Yii::$app->request->post('data', '');
  367. $type = Yii::$app->request->post('type', '');
  368. if (empty($data) || empty($type)) {
  369. util::fail("参数错误");
  370. }
  371. if (in_array($type, ['inTurn', 'addPrice', 'skAddPrice', 'hjAddPrice', 'zsAddPrice', 'cost', 'weight', 'ratio', 'ratio', 'price', 'stockWarning', 'shelfLife']) == false) {
  372. util::fail('非法数据修改');
  373. }
  374. $data = json_decode($data, true);
  375. foreach ($data as $v) {
  376. if ($type == 'status') {
  377. $val = $v[$type] ?? '';
  378. if (!in_array($val, [1, 2])) {
  379. util::fail("参数错误");
  380. }
  381. }
  382. }
  383. foreach ($data as $v) {
  384. $id = $v['id'];
  385. $val = $v[$type] ?? '';
  386. //shish 20210703
  387. if ($type == 'ratio') {
  388. if (preg_match("/^[1-9][0-9]*$/", $val) == false || $val <= 0) {
  389. util::fail('单位比请填写整数,并且要大于0');
  390. }
  391. }
  392. //修改售价 shish 20210703
  393. if ($type == 'price') {
  394. $product = ProductClass::getLockById($id);
  395. $currentName = $product->name ?? '';
  396. if (empty($product)) {
  397. continue;
  398. }
  399. if ($product->shopId != $this->shopId) {
  400. util::fail("不是您的产品");
  401. }
  402. if (empty($val)) {
  403. util::fail("没有填写价格:" . $currentName);
  404. }
  405. if (is_numeric($val) == false) {
  406. util::fail("价格不是数值:" . $currentName);
  407. }
  408. $currentItemId = $product->itemId ?? 0;
  409. if (empty($currentItemId)) {
  410. continue;
  411. }
  412. if (empty($val)) {
  413. continue;
  414. }
  415. //成本价需要修改的花材id
  416. $costChangeIds = Yii::$app->request->post('costChangeIds', []);
  417. if (!empty($costChangeIds)) {
  418. $arrIds = json_decode($costChangeIds, true);
  419. $costIds = is_array($arrIds) ? $arrIds : [];
  420. if (!empty($costIds) && in_array($id, $costIds)) {
  421. $currentAddPrice = bcsub($val, $product->cost, 2);
  422. $skDiff = bcsub($product->skAddPrice, $product->addPrice, 2);
  423. //大众会员价比标准会员价高,所以是相加
  424. $newSkAddPrice = bcadd($currentAddPrice, $skDiff, 2);
  425. $hjDiff = bcsub($product->addPrice, $product->hjAddPrice, 2);
  426. $newHjAddPrice = bcsub($currentAddPrice, $hjDiff, 2);
  427. $zsDiff = bcsub($product->addPrice, $product->zsAddPrice, 2);
  428. $newZsAddPrice = bcsub($currentAddPrice, $zsDiff, 2);
  429. $params = [];
  430. $params['skAddPrice'] = $newSkAddPrice;
  431. $params['addPrice'] = $currentAddPrice;
  432. $params['hjAddPrice'] = $newHjAddPrice;
  433. $params['zsAddPrice'] = $newZsAddPrice;
  434. $sjId = $product->sjId ?? 0;
  435. $shopAll = ShopClass::getAllShop($sjId);
  436. if ($shopAll) {
  437. foreach ($shopAll as $shop) {
  438. $currentId = $shop['id'] ?? 0;
  439. $itemId = $product->itemId ?? 0;
  440. ProductClass::updateByCondition(['shopId' => $currentId, 'itemId' => $itemId], $params);
  441. }
  442. }
  443. }
  444. }
  445. ProductClass::changePrice($product, $val, ProductClass::PRICE_LABEL_CHANGE);
  446. continue;
  447. }
  448. // $where = [];
  449. // $where['id'] = $id;
  450. // $where['shopId'] = $this->shopId;
  451. // $where['sjId'] = $this->sjId;
  452. // if (empty($val)) {
  453. // util::fail("参数错误!");
  454. // }
  455. // $data = [$type => $val];
  456. // ProductClass::updateByCondition($where, $data);
  457. $data = [$type => $val];
  458. $currentProduct = ProductClass::getById($id, true);
  459. if ($currentProduct->shopId != $this->shopId) {
  460. util::fail('非常操作');
  461. }
  462. $currentProduct->$type = $val;
  463. if ($type == 'addPrice') {
  464. $skAddPrice = $currentProduct->skAddPrice;
  465. if ($skAddPrice <= 0) {
  466. $currentProduct->skAddPrice = $val - 1 > 0 ? $val - 1 : 1;
  467. $data['skAddPrice'] = $currentProduct->skAddPrice;
  468. }
  469. $hjAddPrice = $currentProduct->hjAddPrice;
  470. if ($hjAddPrice <= 0) {
  471. $currentProduct->hjAddPrice = $val - 3 > 0 ? $val - 3 : 1;
  472. $data['hjAddPrice'] = $currentProduct->hjAddPrice;
  473. }
  474. $zsAddPrice = $currentProduct->zsAddPrice;
  475. if ($zsAddPrice <= 0) {
  476. $currentProduct->zsAddPrice = $val - 4 > 0 ? $val - 4 : 1;
  477. $data['zsAddPrice'] = $currentProduct->zsAddPrice;
  478. }
  479. }
  480. $currentProduct->save();
  481. //2021.7.6 linqh 如果是首店,则更新全部分店
  482. ProductClass::updateSyncProduct($id, $this->shopId, $data);
  483. }
  484. util::complete();
  485. }
  486. public function actionUpdate()
  487. {
  488. $post = Yii::$app->request->post();
  489. $shopAdmin = $this->shopAdmin;
  490. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  491. util::fail('超管才能修改');
  492. }
  493. $shop = $this->shop;
  494. $default = $shop->default ?? 0;
  495. if ($default != 1) {
  496. //除了上下架其它只能在首店修改
  497. if (isset($post['status']) == false) {
  498. util::fail('请在首店修改');
  499. }
  500. }
  501. $post['adminId'] = $this->adminId;
  502. ProductClass::updateProduct($post);
  503. util::complete();
  504. }
  505. //上下架状态控制 shish 20210713
  506. public function actionStatusUpdate()
  507. {
  508. $post = Yii::$app->request->post();
  509. $shopAdmin = $this->shopAdmin;
  510. $roleId = $shopAdmin['roleId'] ?? 0;
  511. $role = AdminRoleClass::getById($roleId);
  512. $roleName = $role['roleName'] ?? '';
  513. if ($roleName == '员工') {
  514. util::fail('没有权限操作哦');
  515. }
  516. $id = $post['id'] ?? 0;
  517. $status = $post['status'] ?? 1;
  518. $product = ProductClass::getById($id, true);
  519. if ($product->shopId != $this->shopId) {
  520. util::fail('没有权限操作');
  521. }
  522. $product->status = $status;
  523. if ($status == 2) {
  524. $product->discountPrice = 0;
  525. }
  526. $product->save();
  527. util::complete();
  528. }
  529. //花材展示状态控制 shish 20210804
  530. public function actionDelStatusUpdate()
  531. {
  532. $post = Yii::$app->request->post();
  533. $shopAdmin = $this->shopAdmin;
  534. $roleId = $shopAdmin['roleId'] ?? 0;
  535. $role = AdminRoleClass::getById($roleId);
  536. $roleName = $role['roleName'] ?? '';
  537. if ($roleName == '员工') {
  538. util::fail('没有权限操作');
  539. }
  540. $id = $post['id'] ?? 0;
  541. $delStatus = $post['delStatus'] ?? 0;
  542. $product = ProductClass::getById($id, true);
  543. if ($product->shopId != $this->shopId) {
  544. util::fail('没有权限操作');
  545. }
  546. $product->delStatus = $delStatus;
  547. $product->save();
  548. util::complete();
  549. }
  550. //详情 ruidian 2021.5.3
  551. public function actionDetail()
  552. {
  553. $id = Yii::$app->request->get('id', 0);
  554. $respond = ProductClass::getItemInfo($id);
  555. util::success($respond);
  556. }
  557. //新增花材 2021.6.21
  558. public function actionAdd()
  559. {
  560. $post = Yii::$app->request->post();
  561. $shopAdmin = $this->shopAdmin;
  562. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  563. util::fail('超管才能添加花材');
  564. }
  565. $shop = $this->shop;
  566. $default = $shop->default ?? 0;
  567. if ($default != 1) {
  568. util::fail('请在首店添加');
  569. }
  570. $post['sjId'] = $this->sjId;
  571. $post['adminId'] = $this->adminId;
  572. $post['shopId'] = $this->shopId;
  573. $price = $post['price'] ?? 0;
  574. $addPrice = $post['addPrice'] ?? 0;
  575. if ($addPrice > $price) {
  576. util::fail('加价不能大于售价');
  577. }
  578. $cost = bcsub($price, $addPrice, 2);
  579. $post['cost'] = $cost;
  580. ProductClass::addProduct($post);
  581. util::complete('添加成功');
  582. }
  583. //获取拼音缩写 shish 20210707
  584. public function actionPy()
  585. {
  586. $name = Yii::$app->request->get('name');
  587. $py = stringUtil::py($name);
  588. util::success(['py' => $py]);
  589. }
  590. //复制花材
  591. public function actionCopy()
  592. {
  593. $shopAdmin = $this->shopAdmin;
  594. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  595. util::fail('超管才能添加花材');
  596. }
  597. $shop = $this->shop;
  598. $default = $shop->default ?? 0;
  599. if ($default != 1) {
  600. util::fail('请在首店添加');
  601. }
  602. $id = Yii::$app->request->get('id');
  603. $productData = ProductClass::getById($id);
  604. if (!$productData) {
  605. util::fail("花材不存在");
  606. }
  607. if ($productData['shopId'] != $this->shopId) {
  608. util::fail("无效花材");
  609. }
  610. if (isset($productData['variety']) && $productData['variety'] != 0) {
  611. util::fail('特殊花材不能复制');
  612. }
  613. //去除无用的字段
  614. unset($productData['stock']); //库存去掉
  615. unset($productData['viewNum']);
  616. unset($productData['actualSold']);
  617. unset($productData['itemId']);
  618. unset($productData['id']);
  619. unset($productData['delStatus']);
  620. $productData['copy'] = 1; //复制标识
  621. ProductClass::addProduct($productData);
  622. util::complete();
  623. }
  624. //下载价格表 shish 20210922
  625. public function actionGeneratePriceTable()
  626. {
  627. $level = Yii::$app->request->get('level', 0);
  628. $respond = ProductService::generatePriceTable($this->sjId, $this->shopId, $level);
  629. $file = $respond['file'] ?? '';
  630. $shortFile = $respond['shortFile'] ?? '';
  631. $fileUrl = Yii::$app->params['ghsHost'] . $file;
  632. util::success(['file' => $fileUrl, 'shortFile' => $shortFile]);
  633. }
  634. }