ProductController.php 25 KB

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