ProductController.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  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. $shop = $this->shop;
  170. $default = $shop->default ?? 0;
  171. $data = Yii::$app->request->post('data', '');
  172. if (empty($data)) {
  173. util::fail('没有修改');
  174. }
  175. $arr = json_decode($data, true);
  176. if (empty($arr)) {
  177. util::fail('没有修改哦');
  178. }
  179. $connection = Yii::$app->db;
  180. $transaction = $connection->beginTransaction();
  181. try {
  182. $ids = array_column($arr, 'id');
  183. $ids = array_unique(array_filter($ids));
  184. if (empty($ids)) {
  185. util::fail('请选择花材');
  186. }
  187. $sjId = $shop->sjId ?? 0;
  188. $chainShopList = [];
  189. if ($default == 1) {
  190. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId, 'join' => 0], null, '*', null, true);
  191. }
  192. $productList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id', true);
  193. foreach ($arr as $product) {
  194. if (isset($product['price']) == false) {
  195. util::fail('没有价格');
  196. }
  197. if (isset($product['id']) == false) {
  198. util::fail('没有花材');
  199. }
  200. $price = $product['price'];
  201. $productId = $product['id'];
  202. $product = $productList[$productId] ?? [];
  203. if (empty($product)) {
  204. util::fail('没有找到花材');
  205. }
  206. if (isset($product->mainId) == false || $product->mainId != $this->mainId) {
  207. util::fail('没有权限访问');
  208. }
  209. if (empty($price)) {
  210. continue;
  211. }
  212. $ptItemId = $product->itemId;
  213. ProductClass::changeSinglePrice($shop, $ptItemId, $price);
  214. //在首店修改需要同步到所有直营店
  215. if ($default == 1) {
  216. foreach ($chainShopList as $chain) {
  217. if ($chain->id == $shop->id) {
  218. continue;
  219. }
  220. ProductClass::changeSinglePrice($chain, $ptItemId, $price);
  221. }
  222. }
  223. }
  224. $transaction->commit();
  225. util::complete('修改成功');
  226. } catch (\Exception $e) {
  227. $transaction->rollBack();
  228. Yii::info("修改失败原因:" . $e->getMessage());
  229. util::fail('修改失败');
  230. }
  231. }
  232. //改价
  233. public function actionChangePrice()
  234. {
  235. $shopAdmin = $this->shopAdmin;
  236. if (isset($shopAdmin->super) == false || $shopAdmin->super != 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->mainId != $this->mainId) {
  245. util::fail('没有权限');
  246. }
  247. $price = Yii::$app->request->post('price', '');
  248. if (is_numeric($price) && $price > 0) {
  249. ProductClass::changeSinglePrice($productInfo, $price, ProductClass::PRICE_LABEL_CHANGE);
  250. } else {
  251. $price = bcadd($productInfo->cost, $productInfo->addPrice, 2);
  252. ProductClass::changeSinglePrice($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::changeSinglePrice($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->mainId);
  278. if (!$productData) {
  279. util::fail("花材不存在");
  280. }
  281. ProductClass::changeAddPrice($productId, $addPrice);
  282. util::complete();
  283. }
  284. //批量修改更多,包括排序、重量和保质期 ssh 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['mainId']) == false || $info['mainId'] != $this->mainId) {
  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. //批量修改加价 ssh 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['mainId']) == false || $info['mainId'] != $this->mainId) {
  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. $data = Yii::$app->request->post('data', '');
  354. $type = Yii::$app->request->post('type', '');
  355. if (empty($data) || empty($type)) {
  356. util::fail("参数错误");
  357. }
  358. if (in_array($type, ['inTurn', 'addPrice', 'skAddPrice', 'hjAddPrice', 'zsAddPrice', 'cost', 'weight', 'ratio', 'ratio', 'price', 'stockWarning', 'shelfLife']) == false) {
  359. util::fail('非法数据修改');
  360. }
  361. $data = json_decode($data, true);
  362. foreach ($data as $v) {
  363. if ($type == 'status') {
  364. $val = $v[$type] ?? '';
  365. if (!in_array($val, [1, 2])) {
  366. util::fail("参数错误");
  367. }
  368. }
  369. }
  370. foreach ($data as $v) {
  371. $id = $v['id'];
  372. $val = $v[$type] ?? '';
  373. //ssh 20210703
  374. if ($type == 'ratio') {
  375. if (preg_match("/^[1-9][0-9]*$/", $val) == false || $val <= 0) {
  376. util::fail('单位比请填写整数,并且要大于0');
  377. }
  378. }
  379. //修改售价 ssh 20210703
  380. if ($type == 'price') {
  381. $product = ProductClass::getLockById($id);
  382. $currentName = $product->name ?? '';
  383. if (empty($product)) {
  384. continue;
  385. }
  386. if ($product->mainId != $this->mainId) {
  387. util::fail("不是您的产品");
  388. }
  389. if (empty($val)) {
  390. util::fail("没有填写价格:" . $currentName);
  391. }
  392. if (is_numeric($val) == false) {
  393. util::fail("价格不是数值:" . $currentName);
  394. }
  395. $currentItemId = $product->itemId ?? 0;
  396. if (empty($currentItemId)) {
  397. continue;
  398. }
  399. if (empty($val)) {
  400. continue;
  401. }
  402. //成本价需要修改的花材id
  403. $costChangeIds = Yii::$app->request->post('costChangeIds', []);
  404. if (!empty($costChangeIds)) {
  405. $arrIds = json_decode($costChangeIds, true);
  406. $costIds = is_array($arrIds) ? $arrIds : [];
  407. if (!empty($costIds) && in_array($id, $costIds)) {
  408. $currentAddPrice = bcsub($val, $product->cost, 2);
  409. $skDiff = bcsub($product->skAddPrice, $product->addPrice, 2);
  410. //大众会员价比标准会员价高,所以是相加
  411. $newSkAddPrice = bcadd($currentAddPrice, $skDiff, 2);
  412. $hjDiff = bcsub($product->addPrice, $product->hjAddPrice, 2);
  413. $newHjAddPrice = bcsub($currentAddPrice, $hjDiff, 2);
  414. $zsDiff = bcsub($product->addPrice, $product->zsAddPrice, 2);
  415. $newZsAddPrice = bcsub($currentAddPrice, $zsDiff, 2);
  416. $params = [];
  417. $params['skAddPrice'] = $newSkAddPrice;
  418. $params['addPrice'] = $currentAddPrice;
  419. $params['hjAddPrice'] = $newHjAddPrice;
  420. $params['zsAddPrice'] = $newZsAddPrice;
  421. $sjId = $product->sjId ?? 0;
  422. $shopAll = ShopClass::getAllShop($sjId);
  423. if ($shopAll) {
  424. foreach ($shopAll as $shop) {
  425. $currentId = $shop['mainId'] ?? 0;
  426. $itemId = $product->itemId ?? 0;
  427. ProductClass::updateByCondition(['mainId' => $currentId, 'itemId' => $itemId], $params);
  428. }
  429. }
  430. }
  431. }
  432. ProductClass::changeSinglePrice($product, $val, ProductClass::PRICE_LABEL_CHANGE);
  433. continue;
  434. }
  435. $data = [$type => $val];
  436. $currentProduct = ProductClass::getById($id, true);
  437. if ($currentProduct->mainId != $this->mainId) {
  438. util::fail('非常操作');
  439. }
  440. $currentProduct->$type = $val;
  441. if ($type == 'addPrice') {
  442. $skAddPrice = $currentProduct->skAddPrice;
  443. if ($skAddPrice <= 0) {
  444. $currentProduct->skAddPrice = $val - 1 > 0 ? $val - 1 : 1;
  445. $data['skAddPrice'] = $currentProduct->skAddPrice;
  446. }
  447. $hjAddPrice = $currentProduct->hjAddPrice;
  448. if ($hjAddPrice <= 0) {
  449. $currentProduct->hjAddPrice = $val - 3 > 0 ? $val - 3 : 1;
  450. $data['hjAddPrice'] = $currentProduct->hjAddPrice;
  451. }
  452. $zsAddPrice = $currentProduct->zsAddPrice;
  453. if ($zsAddPrice <= 0) {
  454. $currentProduct->zsAddPrice = $val - 4 > 0 ? $val - 4 : 1;
  455. $data['zsAddPrice'] = $currentProduct->zsAddPrice;
  456. }
  457. }
  458. $currentProduct->save();
  459. //2021.7.6 lqh 如果是首店,则更新全部分店
  460. ProductClass::updateSyncChainShop($id, $this->shopId, $data);
  461. }
  462. util::complete();
  463. }
  464. public function actionUpdate()
  465. {
  466. $shopAdmin = $this->shopAdmin;
  467. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  468. util::fail('超管才能操作');
  469. }
  470. $post = Yii::$app->request->post();
  471. $post['adminId'] = $this->adminId;
  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['stock']); //库存去掉
  584. unset($post['viewNum']);
  585. unset($post['actualSold']);
  586. unset($post['itemId']);
  587. unset($post['id']);
  588. unset($post['delStatus']);
  589. ProductClass::addProduct($post);
  590. util::complete('复制成功');
  591. }
  592. //(管理后台)复制花材
  593. public function actionCopy()
  594. {
  595. util::fail('停用');
  596. $shopAdmin = $this->shopAdmin;
  597. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  598. util::fail('超管才能添加花材');
  599. }
  600. $id = Yii::$app->request->get('id');
  601. $productData = ProductClass::getById($id);
  602. if (!$productData) {
  603. util::fail("花材不存在");
  604. }
  605. if ($productData['mainId'] != $this->mainId) {
  606. util::fail("无效花材");
  607. }
  608. if (isset($productData['variety']) && $productData['variety'] != 0) {
  609. util::fail('特殊花材不能复制');
  610. }
  611. //去除无用的字段
  612. unset($productData['stock']); //库存去掉
  613. unset($productData['viewNum']);
  614. unset($productData['actualSold']);
  615. unset($productData['itemId']);
  616. unset($productData['id']);
  617. unset($productData['delStatus']);
  618. $productData['copy'] = 1; //复制标识
  619. ProductClass::addProduct($productData);
  620. util::complete();
  621. }
  622. //下载价格表 ssh 20210922
  623. public function actionGeneratePriceTable()
  624. {
  625. $level = Yii::$app->request->get('level', 0);
  626. $respond = ProductService::generatePriceTable($this->sjId, $this->shopId, $level, $this->mainId);
  627. $file = $respond['file'] ?? '';
  628. $shortFile = $respond['shortFile'] ?? '';
  629. $fileUrl = Yii::$app->params['ghsHost'] . $file;
  630. util::success(['file' => $fileUrl, 'shortFile' => $shortFile]);
  631. }
  632. //下载条形码 lqh 20211227
  633. public function actionGenerateBarcodeTable()
  634. {
  635. $respond = ProductService::generateBarcodeTable($this->sjId, $this->shopId, $this->mainId);
  636. $file = $respond['file'] ?? '';
  637. $shortFile = $respond['shortFile'] ?? '';
  638. $fileUrl = Yii::$app->params['ghsHost'] . $file;
  639. util::success(['file' => $fileUrl, 'shortFile' => $shortFile]);
  640. }
  641. }