RefundController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. //退款退货
  3. namespace ghs\controllers;
  4. use Yii;
  5. use bizGhs\order\classes\RefundOrderClass;
  6. use common\components\util;
  7. use bizGhs\product\classes\ProductClass;
  8. use bizGhs\order\classes\OrderClass;
  9. use bizGhs\order\services\OrderService;
  10. class RefundController extends BaseController
  11. {
  12. //列表
  13. public function actionList()
  14. {
  15. $where = [];
  16. $where['shopId'] = $this->shopId;
  17. $list = RefundOrderClass::getOrderList($where);
  18. util::success($list);
  19. }
  20. public function actionDetail()
  21. {
  22. $orderSn = Yii::$app->request->get('orderSn', '');
  23. $orderData = RefundOrderClass::getOrderDetailByRelateOrder($orderSn, $this->shopId);
  24. util::success($orderData);
  25. }
  26. //退款 linqh 2021.6.25
  27. public function actionCreateOrder()
  28. {
  29. $shopAdmin = $this->shopAdmin;
  30. if (isset($shopAdmin['super']) == false || $shopAdmin['super'] != 1) {
  31. util::fail('超管才能操作退款');
  32. }
  33. $connection = Yii::$app->db;
  34. $transaction = $connection->beginTransaction();
  35. try {
  36. $post = Yii::$app->request->post();
  37. $id = isset($post['id']) ? $post['id'] : 0;
  38. $order = OrderService::getOrderInfo($id, false, false, false, false);
  39. OrderClass::valid($order, $this->shopId);
  40. if ($order['status'] != OrderClass::ORDER_STATUS_COMPLETE) {
  41. util::fail("订单完成了才能发起退款");
  42. }
  43. if ($order['refund'] == 2) {
  44. util::fail("已经退过了");
  45. }
  46. $addTime = $order['addTime'] ?? '';
  47. $current = time();
  48. if ((strtotime($addTime) + 3 * 30 * 24 * 60 * 60) < $current) {
  49. util::fail('历史订单,不能再发起退款');
  50. }
  51. //根据退款类型:1退货并退款(需要减库存) 2:仅退款(不用减库存,不用传花材)
  52. $refundType = $post['refundType'] ?? 0;
  53. if ($refundType != RefundOrderClass::REFUND_TYPE_MONEY_GOOD && $refundType != RefundOrderClass::REFUND_TYPE_MONEY) {
  54. util::fail("请选择退款类型");
  55. }
  56. if ($refundType == RefundOrderClass::REFUND_TYPE_MONEY_GOOD) {
  57. $infoProduct = $order['product'];
  58. $productIds = array_unique(array_filter(array_column($infoProduct, 'productId')));
  59. $productData = ProductClass::getProductByIds($productIds);
  60. $productData = array_column($productData, NULL, 'id');
  61. $orderProductMap = [];
  62. foreach ($infoProduct as $v) {
  63. $orderProductMap[$v['productId']] = $v;
  64. }
  65. //[{productId:0,bigNum:1,smallNum:0}]
  66. $productJson = $post['product'] ?? '';
  67. if (empty($productJson)) {
  68. util::fail('请选择花材');
  69. }
  70. $productList = json_decode($productJson, true);
  71. if (!is_array($productList)) {
  72. util::fail('花材格式不正确');
  73. }
  74. //合并
  75. $productList = ProductClass::mergeItemInfo($productList);
  76. if (empty($productList)) {
  77. util::fail('请选择花材');
  78. }
  79. //判定花材数不能大于原订单数
  80. foreach ($productList as $v) {
  81. $orderProductNum = $orderProductMap[$v['productId']]['num'] ?? 0;
  82. $productName = $productData[$v['productId']]['name'] ?? '';
  83. if (!$orderProductNum) {
  84. util::fail($productName . "花材数量错误");
  85. }
  86. //数量
  87. $bigNum = $v['bigNum'] ?? 0;
  88. $smallNum = $v['smallNum'] ?? 0;
  89. $ratio = $productData[$v['productId']]['ratio'] ?? 0;
  90. $num = ProductClass::mergeItemNum($bigNum, $smallNum, $ratio);
  91. if ($num > $orderProductNum) {
  92. util::fail($productName . "花材数量错误");
  93. }
  94. }
  95. $post['product'] = $productList;
  96. } else {
  97. //仅退款花材直接设置为空
  98. $post['product'] = '';
  99. }
  100. $post['price'] = $post['price'] ?? 0;
  101. if ($post['price'] <= 0) {
  102. util::fail("退款金额不能小于0");
  103. }
  104. if (bccomp($post['price'], $order['realPrice'], 2) == 1) {
  105. util::fail("退款金额不能大于订单金额");
  106. }
  107. $post['shopId'] = $this->shopId;
  108. $post['sjId'] = $this->sjId;
  109. $post['shopAdminId'] = $this->shopAdminId;
  110. $shopAdmin = $this->shopAdmin;
  111. $adminName = $shopAdmin['name'] ?? '';
  112. $post['shopAdminName'] = $adminName;
  113. OrderService::refund($id, $post);
  114. $transaction->commit();
  115. } catch (\Exception $exception) {
  116. $transaction->rollBack();
  117. Yii::info("退款出错了,报错信息:" . $exception->getMessage());
  118. util::fail('操作失败');
  119. }
  120. util::complete();
  121. }
  122. }