ProductController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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 bizGhs\item\classes\ItemClassClass;
  9. use bizGhs\merchant\classes\ShopClass;
  10. use bizGhs\product\classes\ProductClass;
  11. use bizGhs\product\models\Product;
  12. use bizGhs\product\services\ProductService;
  13. use common\components\stringUtil;
  14. use common\components\util;
  15. use Yii;
  16. class ProductController extends BaseController
  17. {
  18. public $guestAccess = ['index'];
  19. //开单时获取所有的分类花材信息
  20. //库存预警
  21. public function actionIndex()
  22. {
  23. $py = Yii::$app->request->get('py', '');
  24. $type = Yii::$app->request->get('type', '');
  25. //默认只显示上架的商品
  26. $status = Yii::$app->request->get('status', 1);
  27. //获取所有当前供应商的分类 (全部、常用)
  28. //根据分类组装分类下的花材信息
  29. $classIds = ItemClassClass::getGhsItemClassAll($this->sjId);
  30. $where['sjId'] = $this->sjId;
  31. if ($py) {
  32. $pyName = strtolower($py);
  33. $where['py'] = $pyName;
  34. }
  35. $shopId = $this->shopId;
  36. //兼容客户根据门店下单
  37. if ($this->lookShopId) {
  38. $shopId = $this->lookShopId;
  39. }
  40. //库存预警
  41. if ($type == 'waring') {
  42. //shopId 改为可以前端传,默认是当前shopId linqh 2021.1.26
  43. $shopId = Yii::$app->request->get('shopId', 0);
  44. if (!$shopId) {
  45. $shopId = $this->shopId;
  46. }
  47. }
  48. $where['shopId'] = $shopId;
  49. if (!empty($status)) {
  50. $where['status'] = $status;
  51. }
  52. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], "*");
  53. $itemInfoData = ProductClass::groupProductInfo($itemInfoData);
  54. //常用的itemIds
  55. $oftenItemIds = [];
  56. //$oftenItemIds = ProductClass::getOftenItemIds($shopId);
  57. //获取供应商下所有花材ID (itemIds)
  58. $itemIdsInfo = ProductClass::getGhsItemIds($this->sjId);
  59. //dd($itemInfoData);
  60. //组装数据: [{classId:'','className:'',child:[{itemInfoData}]}]
  61. $data = ProductService::assembleData($classIds, $itemIdsInfo, $itemInfoData, $oftenItemIds, $type);
  62. util::success($data);
  63. }
  64. //当前门店所有花材
  65. public function actionList()
  66. {
  67. $where = [];
  68. $where['sjId'] = $this->sjId;
  69. $classId = Yii::$app->request->get('classId', 0);
  70. if (!empty($classId)) {
  71. $where['classId'] = $classId;
  72. }
  73. $name = Yii::$app->request->get('name', '');
  74. if (empty($name)) {
  75. $py = Yii::$app->request->get('py', '');
  76. $name = !empty($py) ? $py : '';
  77. }
  78. if (!empty($name)) {
  79. if (stringUtil::isLetter($name)) {
  80. $where['py'] = ['like', $name];
  81. } else {
  82. $where['name'] = ['like', $name];
  83. }
  84. }
  85. $status = Yii::$app->request->get('status', 0);
  86. if (!empty($status)) {
  87. $where['status'] = $status;
  88. }
  89. $shopId = Yii::$app->request->get('shopId', 0);
  90. if (!$shopId) {
  91. $shopId = $this->shopId;
  92. }
  93. $where['shopId'] = $shopId;
  94. $productIds = Yii::$app->request->get('ids', '');
  95. if (!empty($productIds)) {
  96. $productIdArr = explode(',', $productIds);
  97. if (!empty($productIdArr)) {
  98. $where['id'] = ['in', $productIdArr];
  99. }
  100. }
  101. $type = Yii::$app->request->get('type', '');
  102. $showPage = Yii::$app->request->get('showPage', 0);
  103. if ($showPage) {
  104. if ($type == 'waring') {
  105. $pro = Product::find()->where("shopId={$shopId}")->andWhere('`stock`<`stockWarning`')->select('id')->asArray()->all();
  106. $lackIds = array_column($pro, 'id');
  107. if (empty($lackIds)) {
  108. util::success([]);
  109. }
  110. $lackIds = array_filter(array_unique($lackIds));
  111. $where['id'] = ['in', $lackIds];
  112. $respond = ProductClass::getList("*", $where, "stock asc");
  113. } else {
  114. $respond = ProductClass::getList("*", $where, "inTurn desc");
  115. }
  116. $respond['list'] = ProductClass::groupProductInfo($respond['list']);
  117. $respond['list'] = ProductClass::groupClassName($this->sjId, $respond['list']);
  118. util::success($respond);
  119. }
  120. if ($type == 'waring') {
  121. //库存预警 按库存从小到大排序
  122. $data = ProductClass::getAllByCondition($where, ['stock' => SORT_ASC, 'inTurn' => SORT_DESC], "*");
  123. } else {
  124. $data = ProductClass::getAllByCondition($where, ['actualSold' => SORT_DESC, 'inTurn' => SORT_DESC], "*");
  125. }
  126. $data = ProductClass::groupProductInfo($data);
  127. $data = ProductClass::groupClassName($this->sjId, $data);
  128. if ($type == 'waring') {
  129. //库存预警
  130. $newData = [];
  131. foreach ($data as $v) {
  132. if ($v['stock'] <= $v['stockWarning']) {
  133. $newData[] = $v;
  134. }
  135. }
  136. util::success(['list' => $newData]);
  137. }
  138. util::success(['list' => $data]);
  139. }
  140. //批量改价
  141. public function actionBatchChangePrice()
  142. {
  143. //花材价格只能在首店修改,并同步到分店
  144. $shop = $this->shop;
  145. $default = $shop->default ?? 0;
  146. if ($default != 1) {
  147. util::fail('请在首店修改');
  148. }
  149. $shopAdmin = $this->shopAdmin;
  150. $roleId = $shopAdmin['roleId'] ?? 0;
  151. $role = AdminRoleClass::getById($roleId);
  152. $roleName = $role['roleName'] ?? '';
  153. if ($roleName == '员工') {
  154. util::fail('没有权限操作哦');
  155. }
  156. $data = Yii::$app->request->post('data', '');
  157. if (empty($data)) {
  158. util::fail('没有修改');
  159. }
  160. $arr = json_decode($data, true);
  161. if (empty($arr)) {
  162. util::fail('没有修改!');
  163. }
  164. $connection = Yii::$app->db;
  165. $transaction = $connection->beginTransaction();
  166. try {
  167. $ids = array_column($arr, 'id');
  168. $ids = array_unique(array_filter($ids));
  169. if (empty($ids)) {
  170. util::fail('请选择花材');
  171. }
  172. $productList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id', true);
  173. foreach ($arr as $product) {
  174. if (isset($product['price']) == false) {
  175. util::fail('没有价格');
  176. }
  177. if (isset($product['id']) == false) {
  178. util::fail('没有花材');
  179. }
  180. $price = $product['price'];
  181. $productId = $product['id'];
  182. $product = $productList[$productId] ?? [];
  183. if (empty($product)) {
  184. util::fail('没有找到花材');
  185. }
  186. if (isset($product->shopId) == false || $product->shopId != $this->shopId) {
  187. util::fail('没有权限访问');
  188. }
  189. if (empty($price)) {
  190. //恢复自动调价
  191. $price = bcadd($product->cost, $product->addPrice, 2);
  192. ProductClass::changePrice($product, $price, ProductClass::PRICE_LABEL_AUTO);
  193. } else {
  194. //改价
  195. ProductClass::changePrice($product, $price, ProductClass::PRICE_LABEL_CHANGE);
  196. }
  197. }
  198. $transaction->commit();
  199. util::complete('修改成功');
  200. } catch (\Exception $e) {
  201. $transaction->rollBack();
  202. Yii::info("修改失败原因:" . $e->getMessage());
  203. util::fail('修改失败');
  204. }
  205. }
  206. //改价
  207. public function actionChangePrice()
  208. {
  209. //花材价格只能在首店修改,并同步到分店
  210. $shop = $this->shop;
  211. $default = $shop->default ?? 0;
  212. if ($default != 1) {
  213. util::fail('请在首店修改');
  214. }
  215. $shopAdmin = $this->shopAdmin;
  216. $roleId = $shopAdmin['roleId'] ?? 0;
  217. $role = AdminRoleClass::getById($roleId);
  218. $roleName = $role['roleName'] ?? '';
  219. if ($roleName == '员工') {
  220. util::fail('没有权限操作哦');
  221. }
  222. $productId = Yii::$app->request->post('productId', 0);
  223. $price = Yii::$app->request->post('price', '');
  224. if (is_numeric($price) && $price > 0) {
  225. //判断是否有效
  226. $productData = ProductClass::getProductData($productId, $this->shopId);
  227. if (!$productData) {
  228. util::fail("花材不存在");
  229. }
  230. ProductClass::changePrice($productId, $this->shopId, $price, ProductClass::PRICE_LABEL_CHANGE);
  231. } else {
  232. //自动调价
  233. $productData = ProductClass::getById($productId);
  234. if (!$productData) {
  235. util::fail("花材不存在");
  236. }
  237. $price = bcadd($productData['cost'], $productData['addPrice'], 2);
  238. ProductClass::changePrice($productId, $this->shopId, $price, ProductClass::PRICE_LABEL_AUTO);
  239. }
  240. util::success(['price' => $price]);
  241. }
  242. //恢复自动调价
  243. public function actionAutoPrice()
  244. {
  245. $productId = Yii::$app->request->post('productId', 0);
  246. $productData = ProductClass::getById($productId);
  247. if (!$productData) {
  248. util::fail("花材不存在");
  249. }
  250. //2021.04.06改为用product中的成本价+加价
  251. $price = bcadd($productData['cost'], $productData['addPrice'], 2);
  252. ProductClass::changePrice($productId, $this->shopId, $price, ProductClass::PRICE_LABEL_AUTO);
  253. util::complete("操作成功");
  254. }
  255. //修改加价
  256. public function actionChangeAddPrice()
  257. {
  258. $productId = Yii::$app->request->post('productId', 0);
  259. $addPrice = Yii::$app->request->post('addPrice', '');
  260. if ($addPrice < 0) {
  261. util::fail("加价不能小于0");
  262. }
  263. $productData = ProductClass::getProductData($productId, $this->shopId);
  264. if (!$productData) {
  265. util::fail("花材不存在");
  266. }
  267. ProductClass::changeAddPrice($productId, $addPrice);
  268. util::complete();
  269. }
  270. //批量修改product 排序,成本价,加价
  271. public function actionBatchUpdate()
  272. {
  273. $shop = $this->shop;
  274. $default = $shop->default ?? 0;
  275. if ($default != 1) {
  276. util::fail('请在首店修改');
  277. }
  278. $shopAdmin = $this->shopAdmin;
  279. $roleId = $shopAdmin['roleId'] ?? 0;
  280. $role = AdminRoleClass::getById($roleId);
  281. $roleName = $role['roleName'] ?? '';
  282. if ($roleName == '员工') {
  283. util::fail('没有权限操作哦');
  284. }
  285. $data = Yii::$app->request->post('data', ''); //data = [{id:1,inTurn:10,addPrice:2,cost:10}]
  286. $type = Yii::$app->request->post('type', '');
  287. if (empty($data) || empty($type)) {
  288. util::fail("参数错误");
  289. }
  290. if (in_array($type, ['inTurn', 'addPrice', 'cost', 'weight', 'ratio', 'ratio', 'price', 'stockWarning', 'shelfLife'])) {
  291. $data = json_decode($data, true);
  292. } else {
  293. util::fail('非法操作');
  294. }
  295. foreach ($data as $v) {
  296. if ($type == 'status') {
  297. $val = $v[$type] ?? '';
  298. if (!in_array($val, [1, 2])) {
  299. util::fail("参数错误");
  300. }
  301. }
  302. }
  303. foreach ($data as $v) {
  304. $id = $v['id'];
  305. $val = $v[$type] ?? '';
  306. //shish 20210703
  307. if ($type == 'ratio') {
  308. if (preg_match("/^[1-9][0-9]*$/", $val) == false || $val <= 0) {
  309. util::fail('单位比请填写整数,并且要大于0');
  310. }
  311. }
  312. //修改售价 shish 20210703
  313. if ($type == 'price') {
  314. $product = ProductClass::getLockById($id);
  315. if (empty($product)) {
  316. continue;
  317. }
  318. $currentItemId = $product->itemId ?? 0;
  319. if (empty($currentItemId)) {
  320. continue;
  321. }
  322. //留空恢复自动调价
  323. //所有门店同步调整
  324. if (empty($val)) {
  325. $currentPrice = bcadd($product->cost, $product->addPrice, 2);
  326. $upData = ['priceLabel' => ProductClass::PRICE_LABEL_AUTO, 'price' => $currentPrice];
  327. } else {
  328. $upData = ['priceLabel' => ProductClass::PRICE_LABEL_CHANGE, 'price' => $val];
  329. }
  330. $shopAll = ShopClass::getAllShop($this->sjId);
  331. if (!empty($shopAll)) {
  332. foreach ($shopAll as $currentShop) {
  333. $currentShopId = $currentShop['id'] ?? 0;
  334. if (empty($currentShopId)) {
  335. continue;
  336. }
  337. ProductClass::updateByCondition(['shopId' => $currentShopId, 'itemId' => $currentItemId], $upData);
  338. }
  339. }
  340. continue;
  341. }
  342. $where = [];
  343. $where['id'] = $id;
  344. $where['shopId'] = $this->shopId;
  345. $where['sjId'] = $this->sjId;
  346. if (empty($val)) {
  347. util::fail("参数错误!");
  348. }
  349. $data = [
  350. $type => $val
  351. ];
  352. ProductClass::updateByCondition($where, $data);
  353. ProductClass::autoPriceById($id);
  354. //2021.7.6 linqh 如果是首店,则更新全部分店
  355. ProductClass::updateSyncProduct($id, $this->shopId, $data);
  356. }
  357. util::complete();
  358. }
  359. public function actionUpdate()
  360. {
  361. $post = Yii::$app->request->post();
  362. $shop = $this->shop;
  363. $default = $shop->default ?? 0;
  364. if ($default != 1) {
  365. //除了上下架其它只能在首店修改
  366. if (isset($post['status']) == false) {
  367. util::fail('请在首店修改');
  368. }
  369. }
  370. $shopAdmin = $this->shopAdmin;
  371. $roleId = $shopAdmin['roleId'] ?? 0;
  372. $role = AdminRoleClass::getById($roleId);
  373. $roleName = $role['roleName'] ?? '';
  374. if ($roleName == '员工') {
  375. util::fail('没有权限操作哦');
  376. }
  377. $post['adminId'] = $this->adminId;
  378. ProductClass::updateProduct($post);
  379. util::complete();
  380. }
  381. //上下架状态控制 shish 20210713
  382. public function actionStatusUpdate()
  383. {
  384. $post = Yii::$app->request->post();
  385. $shopAdmin = $this->shopAdmin;
  386. $roleId = $shopAdmin['roleId'] ?? 0;
  387. $role = AdminRoleClass::getById($roleId);
  388. $roleName = $role['roleName'] ?? '';
  389. if ($roleName == '员工') {
  390. util::fail('没有权限操作哦');
  391. }
  392. $id = $post['id'] ?? 0;
  393. $status = $post['status'] ?? 1;
  394. $product = ProductClass::getById($id, true);
  395. if ($product->shopId != $this->shopId) {
  396. util::fail('没有权限操作');
  397. }
  398. $product->status = $status;
  399. $product->save();
  400. util::complete();
  401. }
  402. //详情 ruidian 2021.5.3
  403. public function actionDetail()
  404. {
  405. $id = Yii::$app->request->get('id', 0);
  406. $respond = ProductClass::getItemInfo($id);
  407. util::success($respond);
  408. }
  409. //新增花材 2021.6.21
  410. public function actionAdd()
  411. {
  412. $post = Yii::$app->request->post();
  413. $shop = $this->shop;
  414. $default = $shop->default ?? 0;
  415. if ($default != 1) {
  416. util::fail('请在首店添加');
  417. }
  418. $shopAdmin = $this->shopAdmin;
  419. $roleId = $shopAdmin['roleId'] ?? 0;
  420. $role = AdminRoleClass::getById($roleId);
  421. $roleName = $role['roleName'] ?? '';
  422. if ($roleName == '员工') {
  423. util::fail('没有权限操作哦');
  424. }
  425. $post['sjId'] = $this->sjId;
  426. $post['adminId'] = $this->adminId;
  427. $post['shopId'] = $this->shopId;
  428. ProductClass::addProduct($post);
  429. util::complete('添加成功');
  430. }
  431. //获取拼音缩写 shish 20210707
  432. public function actionPy()
  433. {
  434. $name = Yii::$app->request->get('name');
  435. $py = stringUtil::py($name);
  436. util::success(['py' => $py]);
  437. }
  438. //复制花材
  439. public function actionCopy()
  440. {
  441. $id = Yii::$app->request->get('id');
  442. $productData = ProductClass::getById($id);
  443. if (!$productData) {
  444. util::fail("花材不存在");
  445. }
  446. if ($productData['shopId'] != $this->shopId) {
  447. util::fail("无效花材");
  448. }
  449. //去除无用的字段
  450. unset($productData['stock']); //库存去掉
  451. unset($productData['viewNum']);
  452. unset($productData['actualSold']);
  453. unset($productData['itemId']);
  454. unset($productData['id']);
  455. unset($productData['delStatus']);
  456. $productData['copy'] = 1; //复制标识
  457. ProductClass::addProduct($productData);
  458. util::complete();
  459. }
  460. }