RefundController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. $post = Yii::$app->request->post();
  34. $id = isset($post['id']) ? $post['id'] : 0;
  35. $order = OrderService::getOrderInfo($id, false, false, false, false);
  36. OrderClass::valid($order, $this->shopId);
  37. if ($order['status'] != OrderClass::ORDER_STATUS_COMPLETE) {
  38. util::fail("订单完成了才能发起退款");
  39. }
  40. if ($order['refund'] == 2) {
  41. util::fail("已经退过了");
  42. }
  43. $addTime = $order['addTime'] ?? '';
  44. $current = time();
  45. if ((strtotime($addTime) + 3 * 30 * 24 * 60 * 60) < $current) {
  46. util::fail('历史订单,不能再发起退款');
  47. }
  48. //根据退款类型:1退货并退款(需要减库存) 2:仅退款(不用减库存,不用传花材)
  49. $refundType = $post['refundType'] ?? 0;
  50. if ($refundType != RefundOrderClass::REFUND_TYPE_MONEY_GOOD && $refundType != RefundOrderClass::REFUND_TYPE_MONEY) {
  51. util::fail("请选择退款类型");
  52. }
  53. if ($refundType == RefundOrderClass::REFUND_TYPE_MONEY_GOOD) {
  54. $infoProduct = $order['product'];
  55. $productIds = array_unique(array_filter(array_column($infoProduct, 'productId')));
  56. $productData = ProductClass::getProductByIds($productIds);
  57. $productData = array_column($productData, NULL, 'id');
  58. $orderProductMap = [];
  59. foreach ($infoProduct as $v) {
  60. $orderProductMap[$v['productId']] = $v;
  61. }
  62. //[{productId:0,bigNum:1,smallNum:0}]
  63. $productJson = $post['product'] ?? '';
  64. if (empty($productJson)) {
  65. util::fail('请选择花材');
  66. }
  67. $productList = json_decode($productJson, true);
  68. if (!is_array($productList)) {
  69. util::fail('花材格式不正确');
  70. }
  71. //合并
  72. $productList = ProductClass::mergeItemInfo($productList);
  73. if (empty($productList)) {
  74. util::fail('请选择花材');
  75. }
  76. //判定花材数不能大于原订单数
  77. foreach ($productList as $v) {
  78. $orderProductNum = $orderProductMap[$v['productId']]['num'] ?? 0;
  79. $productName = $productData[$v['productId']]['name'] ?? '';
  80. if (!$orderProductNum) {
  81. util::fail($productName . "花材数量错误");
  82. }
  83. //数量
  84. $bigNum = $v['bigNum'] ?? 0;
  85. $smallNum = $v['smallNum'] ?? 0;
  86. $ratio = $productData[$v['productId']]['ratio'] ?? 0;
  87. $num = ProductClass::mergeItemNum($bigNum, $smallNum, $ratio);
  88. if ($num > $orderProductNum) {
  89. util::fail($productName . "花材数量错误");
  90. }
  91. }
  92. $post['product'] = $productList;
  93. }else{
  94. //如果仅退款花材直接设置为空
  95. $post['product'] = '';
  96. }
  97. $post['price'] = $post['price'] ?? 0;
  98. if ($post['price'] <= 0) {
  99. util::fail("退款金额不能小于0");
  100. }
  101. if (bccomp($post['price'], $order['realPrice'], 2) == 1) {
  102. util::fail("退款金额不能大于订单金额");
  103. }
  104. $connection = Yii::$app->db;
  105. $transaction = $connection->beginTransaction();
  106. try {
  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. }