ProductController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. $delStatus = Yii::$app->request->get('delStatus', 0);
  29. //获取所有当前供应商的分类 (全部、常用)
  30. //根据分类组装分类下的花材信息
  31. $classIds = ItemClassClass::getGhsItemClassAll($this->sjId);
  32. $where['sjId'] = $this->sjId;
  33. if ($py) {
  34. $pyName = strtolower($py);
  35. $where['py'] = $pyName;
  36. }
  37. $shopId = $this->shopId;
  38. //兼容客户根据门店下单
  39. if ($this->lookShopId) {
  40. $shopId = $this->lookShopId;
  41. }
  42. //库存预警
  43. if ($type == 'waring') {
  44. //shopId 改为可以前端传,默认是当前shopId linqh 2021.1.26
  45. $shopId = Yii::$app->request->get('shopId', 0);
  46. if (!$shopId) {
  47. $shopId = $this->shopId;
  48. }
  49. }
  50. $where['shopId'] = $shopId;
  51. // delStatus = 2 表示取出上下架的花材 shish 20210713
  52. if ($delStatus != 2) {
  53. $where['delStatus'] = $delStatus;
  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 = ItemService::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. $classId = Yii::$app->request->get('classId', 0);
  71. // $itemInfo = ItemClass::getGhsItemAll($this->sjId);
  72. // $itemIds = array_column($itemInfo, 'itemId');
  73. $where['sjId'] = $this->sjId;
  74. $name = Yii::$app->request->get('name', '');
  75. if (empty($name)) {
  76. $py = Yii::$app->request->get('py', '');
  77. $name = !empty($py) ? $py : '';
  78. }
  79. if (!empty($name)) {
  80. if (stringUtil::isLetter($name)) {
  81. $where['py'] = ['like', $name];
  82. } else {
  83. $where['name'] = ['like', $name];
  84. }
  85. }
  86. if ($classId) {
  87. //获取itemIds
  88. //$itemIds = ItemClass::getItemIdsByClassId($this->sjId, $classId);
  89. //$where['itemId'] = ['in', $itemIds];
  90. $where['classId'] = $classId;
  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', 'delStatus', 'ratio', 'price', 'stockWarning', 'shelfLife'])) {
  236. $data = json_decode($data, true);
  237. } else {
  238. util::fail('不允许的类型');
  239. }
  240. foreach ($data as $v) {
  241. if ($type == 'delStatus') {
  242. $val = $v[$type] ?? '';
  243. if (!in_array($val, [0, 1])) {
  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['delStatus']) == 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. ProductClass::updateProduct($post);
  323. util::complete();
  324. }
  325. //上下架状态控制 shish 20210713
  326. public function actionDelStatusUpdate()
  327. {
  328. $post = Yii::$app->request->post();
  329. $shopAdmin = $this->shopAdmin;
  330. $roleId = $shopAdmin['roleId'] ?? 0;
  331. $role = AdminRoleClass::getById($roleId);
  332. $roleName = $role['roleName'] ?? '';
  333. if ($roleName == '员工') {
  334. util::fail('没有权限操作哦');
  335. }
  336. $id = $post['id'] ?? 0;
  337. $delStatus = $post['delStatus'] ?? 0;
  338. $product = ProductClass::getById($id, true);
  339. if ($product->shopId != $this->shopId) {
  340. util::fail('没有权限操作');
  341. }
  342. $product->delStatus = $delStatus;
  343. $product->save();
  344. util::complete();
  345. }
  346. //详情 ruidian 2021.5.3
  347. public function actionDetail()
  348. {
  349. $id = Yii::$app->request->get('id', 0);
  350. $respond = ProductClass::getItemInfo($id);
  351. util::success($respond);
  352. }
  353. //新增花材 2021.6.21
  354. public function actionAdd()
  355. {
  356. $post = Yii::$app->request->post();
  357. $shop = $this->shop;
  358. $default = $shop->default ?? 0;
  359. if ($default != 1) {
  360. util::fail('请在首店添加');
  361. }
  362. $shopAdmin = $this->shopAdmin;
  363. $roleId = $shopAdmin['roleId'] ?? 0;
  364. $role = AdminRoleClass::getById($roleId);
  365. $roleName = $role['roleName'] ?? '';
  366. if ($roleName == '员工') {
  367. util::fail('没有权限操作哦');
  368. }
  369. $post['sjId'] = $this->sjId;
  370. $post['adminId'] = $this->adminId;
  371. $post['shopId'] = $this->shopId;
  372. ProductClass::addProduct($post);
  373. util::complete('添加成功');
  374. }
  375. //获取拼音缩写 shish 20210707
  376. public function actionPy()
  377. {
  378. $name = Yii::$app->request->get('name');
  379. $py = stringUtil::py($name);
  380. util::success(['py' => $py]);
  381. }
  382. }