ProductController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. <?php
  2. /**
  3. * User: admin
  4. * Date Time: 2021/3/2 14:34
  5. */
  6. namespace hd\controllers;
  7. use biz\ghs\classes\GhsClass;
  8. use biz\item\classes\PtItemClass;
  9. use biz\shop\classes\ShopClass;
  10. use bizGhs\item\classes\ItemClassClass;
  11. use bizGhs\product\classes\ProductClass;
  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 = ['ghs-item'];
  19. //新增花材 2021.6.21
  20. public function actionAdd()
  21. {
  22. $post = Yii::$app->request->post();
  23. $shopAdmin = $this->shopAdmin;
  24. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  25. util::fail('管理员才能添加花材');
  26. }
  27. $shop = $this->shop;
  28. $default = $shop->default ?? 0;
  29. if ($default != 1) {
  30. util::fail('请在总店添加');
  31. }
  32. $post['sjId'] = $this->sjId;
  33. $post['adminId'] = $this->adminId;
  34. $post['mainId'] = $this->mainId;
  35. $price = $post['price'] ?? 0;
  36. $addPrice = $post['addPrice'] ?? 0;
  37. if ($addPrice > $price) {
  38. util::fail('加价不能大于售价');
  39. }
  40. $cost = bcsub($price, $addPrice, 2);
  41. $post['cost'] = $cost;
  42. ProductClass::addProduct($post);
  43. util::complete('添加成功');
  44. }
  45. //获取拼音缩写 ssh 20210707
  46. public function actionPy()
  47. {
  48. $name = Yii::$app->request->get('name');
  49. $py = stringUtil::py($name);
  50. util::success(['py' => $py]);
  51. }
  52. //所有花材
  53. public function actionAll()
  54. {
  55. $where = [];
  56. $where['mainId'] = $this->mainId;
  57. $classId = Yii::$app->request->get('classId', 0);
  58. if (!empty($classId)) {
  59. $where['classId'] = $classId;
  60. }
  61. $status = Yii::$app->request->get('status', 0);
  62. if (!empty($status)) {
  63. $where['status'] = $status;
  64. }
  65. $delStatus = Yii::$app->request->get('delStatus', 0);
  66. if ($delStatus != 2) {
  67. $where['delStatus'] = $delStatus;
  68. }
  69. //输出标准会员价,即花店价,默认显示的是花店价
  70. $level = 1;
  71. $data = ProductClass::getAllByCondition($where, ['actualSold' => SORT_DESC, 'inTurn' => SORT_DESC], "*");
  72. $data = ProductClass::groupProductInfo($data, $level);
  73. util::success(['list' => $data]);
  74. }
  75. public function actionIndex()
  76. {
  77. $py = Yii::$app->request->get('py', '');
  78. //默认显示上架商品
  79. $status = Yii::$app->request->get('status', 1);
  80. //默认显示未删除商品
  81. $delStatus = Yii::$app->request->get('delStatus', 0);
  82. //获取所有当前供应商的分类 (全部、常用)
  83. //根据分类组装分类下的花材信息
  84. //中央ID
  85. $classIds = ItemClassClass::getGhsItemClassAll($this->mainId);
  86. $where['mainId'] = $this->mainId;
  87. if ($py) {
  88. $pyName = strtolower($py);
  89. $where['py'] = $pyName;
  90. }
  91. if (!empty($status)) {
  92. $where['status'] = $status;
  93. }
  94. $where['delStatus'] = $delStatus;
  95. $level = 0;
  96. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], "*");
  97. $itemInfoData = ProductClass::groupProductInfo($itemInfoData, $level);
  98. $data = ProductService::assembleData($classIds, $itemInfoData, true);
  99. util::success($data);
  100. }
  101. //当前门店所有花材分类
  102. public function actionList()
  103. {
  104. $classId = Yii::$app->request->get('classId', 0);
  105. $pyName = Yii::$app->request->get('py', '');
  106. $where['sjId'] = $this->sjId;
  107. if ($pyName) {
  108. $pyName = strtolower($pyName);
  109. $where['py'] = $pyName;
  110. }
  111. $shopId = Yii::$app->request->get('shopId', 0);
  112. if (!$shopId) {
  113. $shopId = $this->shopId;
  114. }
  115. $where['shopId'] = $shopId;
  116. if (!empty($classId)) {
  117. $where['classId'] = $classId;
  118. }
  119. $type = Yii::$app->request->get('type', '');
  120. $showPage = Yii::$app->request->get('showPage', 0);
  121. //需要显示分页
  122. if ($showPage) {
  123. $respond = ProductClass::getList("*", $where, "inTurn desc");
  124. $respond['list'] = ProductClass::groupClassName($this->sjId, $respond['list']);
  125. $respond['list'] = ProductClass::groupProductInfo($respond['list']);
  126. util::success($respond);
  127. }
  128. $data = ProductClass::getAllByCondition($where, "inTurn desc", "*");
  129. $data = ProductClass::groupProductInfo($data);
  130. if ($type == 'waring') {
  131. //库存预警
  132. $newData = [];
  133. foreach ($data as $v) {
  134. if ($v['stock'] <= $v['stockWarning']) {
  135. $newData[] = $v;
  136. }
  137. }
  138. util::success(['list' => $newData]);
  139. }
  140. util::success(['list' => $data]);
  141. }
  142. //批量改价
  143. public function actionBatchChangePrice()
  144. {
  145. util::fail('暂时无法修改');
  146. $shopAdmin = $this->shopAdmin;
  147. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  148. util::fail('超管才能修改');
  149. }
  150. //花材价格只能在首店修改,并同步到分店
  151. $shop = $this->shop;
  152. $default = $shop->default ?? 0;
  153. if ($default != 1) {
  154. util::fail('请在首店修改');
  155. }
  156. $data = Yii::$app->request->post('data', '');
  157. if (empty($data)) {
  158. util::fail('没有修改');
  159. }
  160. $arr = json_decode($data, true);
  161. if (empty($arr)) {
  162. util::fail('没有修改!');
  163. }
  164. $connection = Yii::$app->db;
  165. $transaction = $connection->beginTransaction();
  166. try {
  167. $ids = array_column($arr, 'id');
  168. $ids = array_unique(array_filter($ids));
  169. if (empty($ids)) {
  170. util::fail('请选择花材');
  171. }
  172. $productList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id', true);
  173. foreach ($arr as $product) {
  174. if (isset($product['price']) == false) {
  175. util::fail('没有价格');
  176. }
  177. if (isset($product['id']) == false) {
  178. util::fail('没有花材');
  179. }
  180. $price = $product['price'];
  181. $productId = $product['id'];
  182. $product = $productList[$productId] ?? [];
  183. if (empty($product)) {
  184. util::fail('没有找到花材');
  185. }
  186. if (isset($product->shopId) == false || $product->shopId != $this->shopId) {
  187. util::fail('没有权限访问');
  188. }
  189. if (empty($price)) {
  190. continue;
  191. }
  192. ProductClass::changePrice($product, $price, ProductClass::PRICE_LABEL_CHANGE);
  193. }
  194. $transaction->commit();
  195. util::complete('修改成功');
  196. } catch (\Exception $e) {
  197. $transaction->rollBack();
  198. Yii::info("修改失败原因:" . $e->getMessage());
  199. util::fail('修改失败');
  200. }
  201. }
  202. //从零售端采购入库,查看供应商的某个门店的product
  203. public function actionGhsItem()
  204. {
  205. $get = Yii::$app->request->get();
  206. $id = $get['id'] ?? 0;
  207. $ghs = GhsClass::getGhsInfo($id);
  208. if (empty($ghs)) {
  209. util::fail('没有找到供应商');
  210. }
  211. $shopId = $ghs['shopId'] ?? 0;
  212. $black = $ghs['black'] ?? 2;
  213. if ($black == 2) {
  214. util::fail('您已被列入黑名单');
  215. }
  216. $connection = Yii::$app->db;//事务处理
  217. $transaction = $connection->beginTransaction();
  218. try {
  219. //获取所有当前供应商的分类 (全部、常用)
  220. //根据分类组装分类下的花材信息
  221. $shop = ShopClass::getById($shopId, true);
  222. $mainId = $shop->mainId ?? 0;
  223. $classIds = ItemClassClass::getGhsItemClassAll($mainId);
  224. $where['mainId'] = $mainId;
  225. $where['delStatus'] = 0;
  226. $where['status'] = 1;
  227. $level = $ghs['giveLevel'] ?? 0;
  228. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], "*");
  229. $itemInfoData = ProductClass::groupProductInfo($itemInfoData, $level);
  230. $data = ProductService::assembleData($classIds, $itemInfoData, true);
  231. util::success($data);
  232. } catch (\Exception $e) {
  233. $transaction->rollBack();
  234. util::fail();
  235. }
  236. }
  237. public function actionDetail()
  238. {
  239. $id = Yii::$app->request->get('id', 0);
  240. $respond = ProductClass::getItemInfo($id);
  241. $ptItemId = $respond['itemId'] ?? 0;
  242. $ptItemInfo = PtItemClass::getById($ptItemId);
  243. $respond['ptItemInfo'] = $ptItemInfo;
  244. util::success($respond);
  245. }
  246. public function actionUpdate()
  247. {
  248. $post = Yii::$app->request->post();
  249. $shopAdmin = $this->shopAdmin;
  250. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  251. util::fail('超管才能修改');
  252. }
  253. $shop = $this->shop;
  254. $pfShopId = $shop->pfShopId ?? 0;
  255. if (!empty($pfShopId)) {
  256. util::fail('请在批发端修改花材');
  257. }
  258. $default = $shop->default ?? 0;
  259. if ($default != 1) {
  260. //除了上下架其它只能在首店修改
  261. if (isset($post['status']) == false) {
  262. util::fail('请在首店修改');
  263. }
  264. }
  265. $post['adminId'] = $this->adminId;
  266. ProductClass::updateProduct($post);
  267. util::complete();
  268. }
  269. //批量修改product 排序,成本价,加价
  270. public function actionBatchUpdate()
  271. {
  272. //data = [{id:1,inTurn:10,addPrice:2,cost:10}]
  273. $data = Yii::$app->request->post('data', '');
  274. $type = Yii::$app->request->post('type', '');
  275. if (empty($data) || empty($type)) {
  276. util::fail("参数错误");
  277. }
  278. if (in_array($type, ['inTurn', 'addPrice', 'cost', 'weight', 'ratio']))
  279. $data = json_decode($data, true);
  280. foreach ($data as $v) {
  281. $id = $v['id'];
  282. $val = $v[$type] ?? '';
  283. $where = [];
  284. $where['id'] = $id;
  285. $where['shopId'] = $this->shopId;
  286. $where['sjId'] = $this->sjId;
  287. if (empty($val)) {
  288. util::fail("参数错误1");
  289. }
  290. $data = [
  291. $type => $val
  292. ];
  293. ProductClass::updateByCondition($where, $data);
  294. ProductClass::autoPriceById($id);
  295. }
  296. util::complete();
  297. }
  298. //供应商产品的详情 ssh 20210903
  299. public function actionGhsProductDetail()
  300. {
  301. $id = Yii::$app->request->get('id', 0);
  302. $ghsId = Yii::$app->request->get('ghsId', 0);
  303. $ghs = GhsClass::getById($ghsId, true);
  304. GhsClass::valid($ghs, $this->shopId);
  305. $shopId = $ghs->shopId ?? 0;
  306. $product = ProductClass::getItemInfo($id, $ghs);
  307. if (isset($product['shopId']) == false || $product['shopId'] != $shopId) {
  308. util::fail('没有权限访问');
  309. }
  310. $original = ProductClass::getById($id);
  311. if (isset($original['discountPrice']) && $original['discountPrice'] > 0) {
  312. $discountPrice = $original['discountPrice'];
  313. $original['price'] = floatval($discountPrice);
  314. $skAddPrice = $original['skAddPrice'] ?? 0;
  315. $addPrice = $original['addPrice'] ?? 0;
  316. $hjAddPrice = $original['hjAddPrice'] ?? 0;
  317. $zsAddPrice = $original['zsAddPrice'] ?? 0;
  318. $sk = bcsub($addPrice, $skAddPrice, 2);
  319. $hj = bcsub($addPrice, $hjAddPrice, 2);
  320. $zs = bcsub($addPrice, $zsAddPrice, 2);
  321. $original['skPrice'] = floatval(bcsub($discountPrice, $sk, 2));
  322. $original['hjPrice'] = floatval(bcsub($discountPrice, $hj, 2));
  323. $original['zsPrice'] = floatval(bcsub($discountPrice, $zs, 2));
  324. }
  325. $product['originalInfo'] = $original;
  326. util::success($product);
  327. }
  328. }