ProductController.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  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. $classId = Yii::$app->request->get('classId', 0);
  161. if (!empty($classId)) {
  162. $where['classId'] = $classId;
  163. }
  164. $status = Yii::$app->request->get('status', 0);
  165. if (!empty($status)) {
  166. $where['status'] = $status;
  167. }
  168. $delStatus = Yii::$app->request->get('delStatus', 0);
  169. if ($delStatus != 2) {
  170. $where['delStatus'] = $delStatus;
  171. }
  172. //输出标准会员价,即花店价,默认显示的是花店价
  173. $level = 1;
  174. $data = ProductClass::getAllByCondition($where, ['actualSold' => SORT_DESC, 'inTurn' => SORT_DESC], "*");
  175. $data = ProductClass::groupProductInfo($data, $level);
  176. util::success(['list' => $data]);
  177. }
  178. //批量改价
  179. public function actionBatchChangePrice()
  180. {
  181. $shopAdmin = $this->shopAdmin;
  182. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  183. util::fail('超管才能修改');
  184. }
  185. //花材价格只能在首店修改,并同步到分店
  186. $shop = $this->shop;
  187. $default = $shop->default ?? 0;
  188. if ($default != 1) {
  189. util::fail('请在首店修改');
  190. }
  191. $data = Yii::$app->request->post('data', '');
  192. if (empty($data)) {
  193. util::fail('没有修改');
  194. }
  195. $arr = json_decode($data, true);
  196. if (empty($arr)) {
  197. util::fail('没有修改!');
  198. }
  199. $connection = Yii::$app->db;
  200. $transaction = $connection->beginTransaction();
  201. try {
  202. $ids = array_column($arr, 'id');
  203. $ids = array_unique(array_filter($ids));
  204. if (empty($ids)) {
  205. util::fail('请选择花材');
  206. }
  207. $productList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id', true);
  208. foreach ($arr as $product) {
  209. if (isset($product['price']) == false) {
  210. util::fail('没有价格');
  211. }
  212. if (isset($product['id']) == false) {
  213. util::fail('没有花材');
  214. }
  215. $price = $product['price'];
  216. $productId = $product['id'];
  217. $product = $productList[$productId] ?? [];
  218. if (empty($product)) {
  219. util::fail('没有找到花材');
  220. }
  221. if (isset($product->shopId) == false || $product->shopId != $this->shopId) {
  222. util::fail('没有权限访问');
  223. }
  224. if (empty($price)) {
  225. continue;
  226. }
  227. ProductClass::changePrice($product, $price, ProductClass::PRICE_LABEL_CHANGE);
  228. }
  229. $transaction->commit();
  230. util::complete('修改成功');
  231. } catch (\Exception $e) {
  232. $transaction->rollBack();
  233. Yii::info("修改失败原因:" . $e->getMessage());
  234. util::fail('修改失败');
  235. }
  236. }
  237. //改价
  238. public function actionChangePrice()
  239. {
  240. $shopAdmin = $this->shopAdmin;
  241. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  242. util::fail('超管才能修改');
  243. }
  244. //花材价格只能在首店修改,并同步到分店
  245. $shop = $this->shop;
  246. $default = $shop->default ?? 0;
  247. if ($default != 1) {
  248. util::fail('请在首店修改');
  249. }
  250. $productId = Yii::$app->request->post('productId', 0);
  251. $productInfo = ProductClass::getById($productId, true);
  252. if (empty($productInfo)) {
  253. util::fail("花材不存在");
  254. }
  255. if ($productInfo->shopId != $this->shopId) {
  256. util::fail('没有权限');
  257. }
  258. $price = Yii::$app->request->post('price', '');
  259. if (is_numeric($price) && $price > 0) {
  260. ProductClass::changePrice($productInfo, $price, ProductClass::PRICE_LABEL_CHANGE);
  261. } else {
  262. $price = bcadd($productInfo->cost, $productInfo->addPrice, 2);
  263. ProductClass::changePrice($productInfo, $price, ProductClass::PRICE_LABEL_AUTO);
  264. }
  265. util::success(['price' => $price]);
  266. }
  267. //恢复自动调价
  268. public function actionAutoPrice()
  269. {
  270. $productId = Yii::$app->request->post('productId', 0);
  271. $productData = ProductClass::getById($productId);
  272. if (!$productData) {
  273. util::fail("花材不存在");
  274. }
  275. //2021.04.06改为用product中的成本价+加价
  276. $price = bcadd($productData['cost'], $productData['addPrice'], 2);
  277. ProductClass::changePrice($productId, $this->shopId, $price, ProductClass::PRICE_LABEL_AUTO);
  278. util::complete("操作成功");
  279. }
  280. //修改加价
  281. public function actionChangeAddPrice()
  282. {
  283. $productId = Yii::$app->request->post('productId', 0);
  284. $addPrice = Yii::$app->request->post('addPrice', '');
  285. if ($addPrice < 0) {
  286. util::fail("加价不能小于0");
  287. }
  288. $productData = ProductClass::getProductData($productId, $this->shopId);
  289. if (!$productData) {
  290. util::fail("花材不存在");
  291. }
  292. ProductClass::changeAddPrice($productId, $addPrice);
  293. util::complete();
  294. }
  295. //批量修改更多,包括排序、重量和保质期 shish 20211211
  296. public function actionBatchChangeMore()
  297. {
  298. $shopAdmin = $this->shopAdmin;
  299. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  300. util::fail('超管才能操作');
  301. }
  302. $addData = Yii::$app->request->post('addData', '');
  303. if (empty($addData)) {
  304. util::fail('没有花材');
  305. }
  306. $itemData = json_decode($addData, true);
  307. if (is_array($itemData) == false) {
  308. util::fail('没有花材...');
  309. }
  310. $ids = array_column($itemData, 'id');
  311. $infoList = ProductClass::getByIds($ids);
  312. if (empty($infoList)) {
  313. util::fail('没有花材');
  314. }
  315. foreach ($infoList as $info) {
  316. if (isset($info['shopId']) == false || $info['shopId'] != $this->shopId) {
  317. util::fail('非常访问,不是你的花材不能修改');
  318. }
  319. }
  320. foreach ($itemData as $key => $data) {
  321. $id = $data['id'] ?? 0;
  322. ProductClass::updateById($id, $data);
  323. }
  324. util::complete('修改成功');
  325. }
  326. //批量修改加价 shish 20211211
  327. public function actionBatchChangeAddPrice()
  328. {
  329. $shopAdmin = $this->shopAdmin;
  330. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  331. util::fail('超管才能操作');
  332. }
  333. $addData = Yii::$app->request->post('addData', '');
  334. if (empty($addData)) {
  335. util::fail('没有花材');
  336. }
  337. $itemData = json_decode($addData, true);
  338. if (is_array($itemData) == false) {
  339. util::fail('没有花材...');
  340. }
  341. $ids = array_column($itemData, 'id');
  342. $infoList = ProductClass::getByIds($ids);
  343. if (empty($infoList)) {
  344. util::fail('没有花材');
  345. }
  346. foreach ($infoList as $info) {
  347. if (isset($info['shopId']) == false || $info['shopId'] != $this->shopId) {
  348. util::fail('非常访问,不是你的花材不能修改');
  349. }
  350. }
  351. foreach ($itemData as $key => $data) {
  352. $id = $data['id'] ?? 0;
  353. ProductClass::updateById($id, $data);
  354. }
  355. util::complete('修改成功');
  356. }
  357. //批量修改product 排序,成本价,加价
  358. public function actionBatchUpdate()
  359. {
  360. $shopAdmin = $this->shopAdmin;
  361. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  362. util::fail('超管才能修改');
  363. }
  364. //花材价格只能在首店修改,并同步到分店
  365. $shop = $this->shop;
  366. $default = $shop->default ?? 0;
  367. if ($default != 1) {
  368. util::fail('请在首店修改');
  369. }
  370. $data = Yii::$app->request->post('data', '');
  371. $type = Yii::$app->request->post('type', '');
  372. if (empty($data) || empty($type)) {
  373. util::fail("参数错误");
  374. }
  375. if (in_array($type, ['inTurn', 'addPrice', 'skAddPrice', 'hjAddPrice', 'zsAddPrice', 'cost', 'weight', 'ratio', 'ratio', 'price', 'stockWarning', 'shelfLife']) == false) {
  376. util::fail('非法数据修改');
  377. }
  378. $data = json_decode($data, true);
  379. foreach ($data as $v) {
  380. if ($type == 'status') {
  381. $val = $v[$type] ?? '';
  382. if (!in_array($val, [1, 2])) {
  383. util::fail("参数错误");
  384. }
  385. }
  386. }
  387. foreach ($data as $v) {
  388. $id = $v['id'];
  389. $val = $v[$type] ?? '';
  390. //shish 20210703
  391. if ($type == 'ratio') {
  392. if (preg_match("/^[1-9][0-9]*$/", $val) == false || $val <= 0) {
  393. util::fail('单位比请填写整数,并且要大于0');
  394. }
  395. }
  396. //修改售价 shish 20210703
  397. if ($type == 'price') {
  398. $product = ProductClass::getLockById($id);
  399. $currentName = $product->name ?? '';
  400. if (empty($product)) {
  401. continue;
  402. }
  403. if ($product->shopId != $this->shopId) {
  404. util::fail("不是您的产品");
  405. }
  406. if (empty($val)) {
  407. util::fail("没有填写价格:" . $currentName);
  408. }
  409. if (is_numeric($val) == false) {
  410. util::fail("价格不是数值:" . $currentName);
  411. }
  412. $currentItemId = $product->itemId ?? 0;
  413. if (empty($currentItemId)) {
  414. continue;
  415. }
  416. if (empty($val)) {
  417. continue;
  418. }
  419. //成本价需要修改的花材id
  420. $costChangeIds = Yii::$app->request->post('costChangeIds', []);
  421. if (!empty($costChangeIds)) {
  422. $arrIds = json_decode($costChangeIds, true);
  423. $costIds = is_array($arrIds) ? $arrIds : [];
  424. if (!empty($costIds) && in_array($id, $costIds)) {
  425. $currentAddPrice = bcsub($val, $product->cost, 2);
  426. $skDiff = bcsub($product->skAddPrice, $product->addPrice, 2);
  427. //大众会员价比标准会员价高,所以是相加
  428. $newSkAddPrice = bcadd($currentAddPrice, $skDiff, 2);
  429. $hjDiff = bcsub($product->addPrice, $product->hjAddPrice, 2);
  430. $newHjAddPrice = bcsub($currentAddPrice, $hjDiff, 2);
  431. $zsDiff = bcsub($product->addPrice, $product->zsAddPrice, 2);
  432. $newZsAddPrice = bcsub($currentAddPrice, $zsDiff, 2);
  433. $params = [];
  434. $params['skAddPrice'] = $newSkAddPrice;
  435. $params['addPrice'] = $currentAddPrice;
  436. $params['hjAddPrice'] = $newHjAddPrice;
  437. $params['zsAddPrice'] = $newZsAddPrice;
  438. $sjId = $product->sjId ?? 0;
  439. $shopAll = ShopClass::getAllShop($sjId);
  440. if ($shopAll) {
  441. foreach ($shopAll as $shop) {
  442. $currentId = $shop['id'] ?? 0;
  443. $itemId = $product->itemId ?? 0;
  444. ProductClass::updateByCondition(['shopId' => $currentId, 'itemId' => $itemId], $params);
  445. }
  446. }
  447. }
  448. }
  449. ProductClass::changePrice($product, $val, ProductClass::PRICE_LABEL_CHANGE);
  450. continue;
  451. }
  452. // $where = [];
  453. // $where['id'] = $id;
  454. // $where['shopId'] = $this->shopId;
  455. // $where['sjId'] = $this->sjId;
  456. // if (empty($val)) {
  457. // util::fail("参数错误!");
  458. // }
  459. // $data = [$type => $val];
  460. // ProductClass::updateByCondition($where, $data);
  461. $data = [$type => $val];
  462. $currentProduct = ProductClass::getById($id, true);
  463. if ($currentProduct->shopId != $this->shopId) {
  464. util::fail('非常操作');
  465. }
  466. $currentProduct->$type = $val;
  467. if ($type == 'addPrice') {
  468. $skAddPrice = $currentProduct->skAddPrice;
  469. if ($skAddPrice <= 0) {
  470. $currentProduct->skAddPrice = $val - 1 > 0 ? $val - 1 : 1;
  471. $data['skAddPrice'] = $currentProduct->skAddPrice;
  472. }
  473. $hjAddPrice = $currentProduct->hjAddPrice;
  474. if ($hjAddPrice <= 0) {
  475. $currentProduct->hjAddPrice = $val - 3 > 0 ? $val - 3 : 1;
  476. $data['hjAddPrice'] = $currentProduct->hjAddPrice;
  477. }
  478. $zsAddPrice = $currentProduct->zsAddPrice;
  479. if ($zsAddPrice <= 0) {
  480. $currentProduct->zsAddPrice = $val - 4 > 0 ? $val - 4 : 1;
  481. $data['zsAddPrice'] = $currentProduct->zsAddPrice;
  482. }
  483. }
  484. $currentProduct->save();
  485. //2021.7.6 linqh 如果是首店,则更新全部分店
  486. ProductClass::updateSyncProduct($id, $this->shopId, $data);
  487. }
  488. util::complete();
  489. }
  490. public function actionUpdate()
  491. {
  492. $post = Yii::$app->request->post();
  493. $shopAdmin = $this->shopAdmin;
  494. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  495. util::fail('超管才能修改');
  496. }
  497. $shop = $this->shop;
  498. $default = $shop->default ?? 0;
  499. if ($default != 1) {
  500. //除了上下架其它只能在首店修改
  501. if (isset($post['status']) == false) {
  502. util::fail('请在首店修改');
  503. }
  504. }
  505. $post['adminId'] = $this->adminId;
  506. ProductClass::updateProduct($post);
  507. util::complete();
  508. }
  509. //上下架状态控制 shish 20210713
  510. public function actionStatusUpdate()
  511. {
  512. $post = Yii::$app->request->post();
  513. $shopAdmin = $this->shopAdmin;
  514. $roleId = $shopAdmin['roleId'] ?? 0;
  515. $role = AdminRoleClass::getById($roleId);
  516. $roleName = $role['roleName'] ?? '';
  517. if ($roleName == '员工') {
  518. util::fail('没有权限操作哦');
  519. }
  520. $id = $post['id'] ?? 0;
  521. $status = $post['status'] ?? 1;
  522. $product = ProductClass::getById($id, true);
  523. if ($product->shopId != $this->shopId) {
  524. util::fail('没有权限操作');
  525. }
  526. $product->status = $status;
  527. if ($status == 2) {
  528. $product->discountPrice = 0;
  529. }
  530. $product->save();
  531. util::complete();
  532. }
  533. //花材展示状态控制 shish 20210804
  534. public function actionDelStatusUpdate()
  535. {
  536. $post = Yii::$app->request->post();
  537. $shopAdmin = $this->shopAdmin;
  538. $roleId = $shopAdmin['roleId'] ?? 0;
  539. $role = AdminRoleClass::getById($roleId);
  540. $roleName = $role['roleName'] ?? '';
  541. if ($roleName == '员工') {
  542. util::fail('没有权限操作');
  543. }
  544. $id = $post['id'] ?? 0;
  545. $delStatus = $post['delStatus'] ?? 0;
  546. $product = ProductClass::getById($id, true);
  547. if ($product->shopId != $this->shopId) {
  548. util::fail('没有权限操作');
  549. }
  550. $product->delStatus = $delStatus;
  551. $product->save();
  552. util::complete();
  553. }
  554. //详情 ruidian 2021.5.3
  555. public function actionDetail()
  556. {
  557. $id = Yii::$app->request->get('id', 0);
  558. $respond = ProductClass::getItemInfo($id);
  559. util::success($respond);
  560. }
  561. //新增花材 2021.6.21
  562. public function actionAdd()
  563. {
  564. $post = Yii::$app->request->post();
  565. $shopAdmin = $this->shopAdmin;
  566. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  567. util::fail('超管才能添加花材');
  568. }
  569. $shop = $this->shop;
  570. $default = $shop->default ?? 0;
  571. if ($default != 1) {
  572. util::fail('请在首店添加');
  573. }
  574. $post['sjId'] = $this->sjId;
  575. $post['adminId'] = $this->adminId;
  576. $post['shopId'] = $this->shopId;
  577. $price = $post['price'] ?? 0;
  578. $addPrice = $post['addPrice'] ?? 0;
  579. if ($addPrice > $price) {
  580. util::fail('加价不能大于售价');
  581. }
  582. $cost = bcsub($price, $addPrice, 2);
  583. $post['cost'] = $cost;
  584. ProductClass::addProduct($post);
  585. util::complete('添加成功');
  586. }
  587. //获取拼音缩写 shish 20210707
  588. public function actionPy()
  589. {
  590. $name = Yii::$app->request->get('name');
  591. $py = stringUtil::py($name);
  592. util::success(['py' => $py]);
  593. }
  594. //复制花材
  595. public function actionCopy()
  596. {
  597. $shopAdmin = $this->shopAdmin;
  598. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  599. util::fail('超管才能添加花材');
  600. }
  601. $shop = $this->shop;
  602. $default = $shop->default ?? 0;
  603. if ($default != 1) {
  604. util::fail('请在首店添加');
  605. }
  606. $id = Yii::$app->request->get('id');
  607. $productData = ProductClass::getById($id);
  608. if (!$productData) {
  609. util::fail("花材不存在");
  610. }
  611. if ($productData['shopId'] != $this->shopId) {
  612. util::fail("无效花材");
  613. }
  614. if (isset($productData['variety']) && $productData['variety'] != 0) {
  615. util::fail('特殊花材不能复制');
  616. }
  617. //去除无用的字段
  618. unset($productData['stock']); //库存去掉
  619. unset($productData['viewNum']);
  620. unset($productData['actualSold']);
  621. unset($productData['itemId']);
  622. unset($productData['id']);
  623. unset($productData['delStatus']);
  624. $productData['copy'] = 1; //复制标识
  625. ProductClass::addProduct($productData);
  626. util::complete();
  627. }
  628. //下载价格表 shish 20210922
  629. public function actionGeneratePriceTable()
  630. {
  631. $level = Yii::$app->request->get('level', 0);
  632. $respond = ProductService::generatePriceTable($this->sjId, $this->shopId, $level);
  633. $file = $respond['file'] ?? '';
  634. $shortFile = $respond['shortFile'] ?? '';
  635. $fileUrl = Yii::$app->params['ghsHost'] . $file;
  636. util::success(['file' => $fileUrl, 'shortFile' => $shortFile]);
  637. }
  638. //下载条形码 linqh 20211227
  639. public function actionGenerateBarcodeTable()
  640. {
  641. $respond = ProductService::generateBarcodeTable($this->sjId, $this->shopId);
  642. $file = $respond['file'] ?? '';
  643. $shortFile = $respond['shortFile'] ?? '';
  644. $fileUrl = Yii::$app->params['ghsHost'] . $file;
  645. util::success(['file' => $fileUrl, 'shortFile' => $shortFile]);
  646. }
  647. }