ProductController.php 25 KB

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