ProductController.php 15 KB

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