ProductController.php 14 KB

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