ProductController.php 25 KB

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