ProductController.php 26 KB

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