ProductController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. $shopAdmin = $this->shopAdmin;
  144. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  145. util::fail('超管才能修改');
  146. }
  147. //花材价格只能在首店修改,并同步到分店
  148. $shop = $this->shop;
  149. $default = $shop->default ?? 0;
  150. if ($default != 1) {
  151. util::fail('请在首店修改');
  152. }
  153. $data = Yii::$app->request->post('data', '');
  154. if (empty($data)) {
  155. util::fail('没有修改');
  156. }
  157. $arr = json_decode($data, true);
  158. if (empty($arr)) {
  159. util::fail('没有修改!');
  160. }
  161. $connection = Yii::$app->db;
  162. $transaction = $connection->beginTransaction();
  163. try {
  164. $ids = array_column($arr, 'id');
  165. $ids = array_unique(array_filter($ids));
  166. if (empty($ids)) {
  167. util::fail('请选择花材');
  168. }
  169. $productList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id', true);
  170. foreach ($arr as $product) {
  171. if (isset($product['price']) == false) {
  172. util::fail('没有价格');
  173. }
  174. if (isset($product['id']) == false) {
  175. util::fail('没有花材');
  176. }
  177. $price = $product['price'];
  178. $productId = $product['id'];
  179. $product = $productList[$productId] ?? [];
  180. if (empty($product)) {
  181. util::fail('没有找到花材');
  182. }
  183. if (isset($product->shopId) == false || $product->shopId != $this->shopId) {
  184. util::fail('没有权限访问');
  185. }
  186. if (empty($price)) {
  187. //恢复自动调价
  188. $price = bcadd($product->cost, $product->addPrice, 2);
  189. ProductClass::changePrice($product, $price, ProductClass::PRICE_LABEL_AUTO);
  190. } else {
  191. //改价
  192. ProductClass::changePrice($product, $price, ProductClass::PRICE_LABEL_CHANGE);
  193. }
  194. }
  195. $transaction->commit();
  196. util::complete('修改成功');
  197. } catch (\Exception $e) {
  198. $transaction->rollBack();
  199. Yii::info("修改失败原因:" . $e->getMessage());
  200. util::fail('修改失败');
  201. }
  202. }
  203. //改价
  204. public function actionChangePrice()
  205. {
  206. $shopAdmin = $this->shopAdmin;
  207. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  208. util::fail('超管才能修改');
  209. }
  210. //花材价格只能在首店修改,并同步到分店
  211. $shop = $this->shop;
  212. $default = $shop->default ?? 0;
  213. if ($default != 1) {
  214. util::fail('请在首店修改');
  215. }
  216. $productId = Yii::$app->request->post('productId', 0);
  217. $productInfo = ProductClass::getById($productId, true);
  218. if (empty($productInfo)) {
  219. util::fail("花材不存在");
  220. }
  221. if ($productInfo->shopId != $this->shopId) {
  222. util::fail('没有权限');
  223. }
  224. $price = Yii::$app->request->post('price', '');
  225. if (is_numeric($price) && $price > 0) {
  226. ProductClass::changePrice($productInfo, $price, ProductClass::PRICE_LABEL_CHANGE);
  227. } else {
  228. $price = bcadd($productInfo->cost, $productInfo->addPrice, 2);
  229. ProductClass::changePrice($productInfo, $price, ProductClass::PRICE_LABEL_AUTO);
  230. }
  231. util::success(['price' => $price]);
  232. }
  233. //恢复自动调价
  234. public function actionAutoPrice()
  235. {
  236. $productId = Yii::$app->request->post('productId', 0);
  237. $productData = ProductClass::getById($productId);
  238. if (!$productData) {
  239. util::fail("花材不存在");
  240. }
  241. //2021.04.06改为用product中的成本价+加价
  242. $price = bcadd($productData['cost'], $productData['addPrice'], 2);
  243. ProductClass::changePrice($productId, $this->shopId, $price, ProductClass::PRICE_LABEL_AUTO);
  244. util::complete("操作成功");
  245. }
  246. //修改加价
  247. public function actionChangeAddPrice()
  248. {
  249. $productId = Yii::$app->request->post('productId', 0);
  250. $addPrice = Yii::$app->request->post('addPrice', '');
  251. if ($addPrice < 0) {
  252. util::fail("加价不能小于0");
  253. }
  254. $productData = ProductClass::getProductData($productId, $this->shopId);
  255. if (!$productData) {
  256. util::fail("花材不存在");
  257. }
  258. ProductClass::changeAddPrice($productId, $addPrice);
  259. util::complete();
  260. }
  261. //批量修改product 排序,成本价,加价
  262. public function actionBatchUpdate()
  263. {
  264. $shop = $this->shop;
  265. $default = $shop->default ?? 0;
  266. if ($default != 1) {
  267. util::fail('请在首店修改');
  268. }
  269. $shopAdmin = $this->shopAdmin;
  270. $roleId = $shopAdmin['roleId'] ?? 0;
  271. $role = AdminRoleClass::getById($roleId);
  272. $roleName = $role['roleName'] ?? '';
  273. if ($roleName == '员工') {
  274. util::fail('没有权限操作哦');
  275. }
  276. $data = Yii::$app->request->post('data', ''); //data = [{id:1,inTurn:10,addPrice:2,cost:10}]
  277. $type = Yii::$app->request->post('type', '');
  278. if (empty($data) || empty($type)) {
  279. util::fail("参数错误");
  280. }
  281. if (in_array($type, ['inTurn', 'addPrice', 'cost', 'weight', 'ratio', 'ratio', 'price', 'stockWarning', 'shelfLife'])) {
  282. $data = json_decode($data, true);
  283. } else {
  284. util::fail('非法操作');
  285. }
  286. foreach ($data as $v) {
  287. if ($type == 'status') {
  288. $val = $v[$type] ?? '';
  289. if (!in_array($val, [1, 2])) {
  290. util::fail("参数错误");
  291. }
  292. }
  293. }
  294. foreach ($data as $v) {
  295. $id = $v['id'];
  296. $val = $v[$type] ?? '';
  297. //shish 20210703
  298. if ($type == 'ratio') {
  299. if (preg_match("/^[1-9][0-9]*$/", $val) == false || $val <= 0) {
  300. util::fail('单位比请填写整数,并且要大于0');
  301. }
  302. }
  303. //修改售价 shish 20210703
  304. if ($type == 'price') {
  305. $product = ProductClass::getLockById($id);
  306. if (empty($product)) {
  307. continue;
  308. }
  309. $currentItemId = $product->itemId ?? 0;
  310. if (empty($currentItemId)) {
  311. continue;
  312. }
  313. //留空恢复自动调价
  314. //所有门店同步调整
  315. if (empty($val)) {
  316. $currentPrice = bcadd($product->cost, $product->addPrice, 2);
  317. $upData = ['priceLabel' => ProductClass::PRICE_LABEL_AUTO, 'price' => $currentPrice];
  318. } else {
  319. $upData = ['priceLabel' => ProductClass::PRICE_LABEL_CHANGE, 'price' => $val];
  320. }
  321. $shopAll = ShopClass::getAllShop($this->sjId);
  322. if (!empty($shopAll)) {
  323. foreach ($shopAll as $currentShop) {
  324. $currentShopId = $currentShop['id'] ?? 0;
  325. if (empty($currentShopId)) {
  326. continue;
  327. }
  328. ProductClass::updateByCondition(['shopId' => $currentShopId, 'itemId' => $currentItemId], $upData);
  329. }
  330. }
  331. continue;
  332. }
  333. $where = [];
  334. $where['id'] = $id;
  335. $where['shopId'] = $this->shopId;
  336. $where['sjId'] = $this->sjId;
  337. if (empty($val)) {
  338. util::fail("参数错误!");
  339. }
  340. $data = [
  341. $type => $val
  342. ];
  343. ProductClass::updateByCondition($where, $data);
  344. ProductClass::autoPriceById($id);
  345. //2021.7.6 linqh 如果是首店,则更新全部分店
  346. ProductClass::updateSyncProduct($id, $this->shopId, $data);
  347. }
  348. util::complete();
  349. }
  350. public function actionUpdate()
  351. {
  352. $post = Yii::$app->request->post();
  353. $shop = $this->shop;
  354. $default = $shop->default ?? 0;
  355. if ($default != 1) {
  356. //除了上下架其它只能在首店修改
  357. if (isset($post['status']) == false) {
  358. util::fail('请在首店修改');
  359. }
  360. }
  361. $shopAdmin = $this->shopAdmin;
  362. $roleId = $shopAdmin['roleId'] ?? 0;
  363. $role = AdminRoleClass::getById($roleId);
  364. $roleName = $role['roleName'] ?? '';
  365. if ($roleName == '员工') {
  366. util::fail('没有权限操作哦');
  367. }
  368. $post['adminId'] = $this->adminId;
  369. ProductClass::updateProduct($post);
  370. util::complete();
  371. }
  372. //上下架状态控制 shish 20210713
  373. public function actionStatusUpdate()
  374. {
  375. $post = Yii::$app->request->post();
  376. $shopAdmin = $this->shopAdmin;
  377. $roleId = $shopAdmin['roleId'] ?? 0;
  378. $role = AdminRoleClass::getById($roleId);
  379. $roleName = $role['roleName'] ?? '';
  380. if ($roleName == '员工') {
  381. util::fail('没有权限操作哦');
  382. }
  383. $id = $post['id'] ?? 0;
  384. $status = $post['status'] ?? 1;
  385. $product = ProductClass::getById($id, true);
  386. if ($product->shopId != $this->shopId) {
  387. util::fail('没有权限操作');
  388. }
  389. $product->status = $status;
  390. $product->save();
  391. util::complete();
  392. }
  393. //详情 ruidian 2021.5.3
  394. public function actionDetail()
  395. {
  396. $id = Yii::$app->request->get('id', 0);
  397. $respond = ProductClass::getItemInfo($id);
  398. util::success($respond);
  399. }
  400. //新增花材 2021.6.21
  401. public function actionAdd()
  402. {
  403. $post = Yii::$app->request->post();
  404. $shop = $this->shop;
  405. $default = $shop->default ?? 0;
  406. if ($default != 1) {
  407. util::fail('请在首店添加');
  408. }
  409. $shopAdmin = $this->shopAdmin;
  410. $roleId = $shopAdmin['roleId'] ?? 0;
  411. $role = AdminRoleClass::getById($roleId);
  412. $roleName = $role['roleName'] ?? '';
  413. if ($roleName == '员工') {
  414. util::fail('没有权限操作哦');
  415. }
  416. $post['sjId'] = $this->sjId;
  417. $post['adminId'] = $this->adminId;
  418. $post['shopId'] = $this->shopId;
  419. ProductClass::addProduct($post);
  420. util::complete('添加成功');
  421. }
  422. //获取拼音缩写 shish 20210707
  423. public function actionPy()
  424. {
  425. $name = Yii::$app->request->get('name');
  426. $py = stringUtil::py($name);
  427. util::success(['py' => $py]);
  428. }
  429. //复制花材
  430. public function actionCopy()
  431. {
  432. $id = Yii::$app->request->get('id');
  433. $productData = ProductClass::getById($id);
  434. if (!$productData) {
  435. util::fail("花材不存在");
  436. }
  437. if ($productData['shopId'] != $this->shopId) {
  438. util::fail("无效花材");
  439. }
  440. //去除无用的字段
  441. unset($productData['stock']); //库存去掉
  442. unset($productData['viewNum']);
  443. unset($productData['actualSold']);
  444. unset($productData['itemId']);
  445. unset($productData['id']);
  446. unset($productData['delStatus']);
  447. $productData['copy'] = 1; //复制标识
  448. ProductClass::addProduct($productData);
  449. util::complete();
  450. }
  451. }