ProductController.php 11 KB

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