ProductController.php 27 KB

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