ProductController.php 15 KB

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