ProductController.php 11 KB

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