ApplyController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace app\frontend\modules\refund\controllers;
  3. use app\common\components\ApiController;
  4. use app\common\events\order\OrderRefundApplyDataEvent;
  5. use app\common\events\order\OrderRefundApplyEvent;
  6. use app\common\exceptions\AppException;
  7. use app\common\models\refund\RefundApply;
  8. use app\common\services\SystemMsgService;
  9. use app\framework\Http\Request;
  10. use app\frontend\models\Order;
  11. use app\frontend\modules\refund\services\RefundService;
  12. use app\frontend\modules\refund\services\RefundMessageService;
  13. use app\frontend\modules\order\services\MiniMessageService;
  14. use Illuminate\Support\Facades\DB;
  15. /**
  16. * Created by PhpStorm.
  17. * Author: 芸众商城 www.yunzshop.com
  18. * Date: 2017/4/12
  19. * Time: 下午4:24
  20. */
  21. class ApplyController extends ApiController
  22. {
  23. protected function getOrder()
  24. {
  25. return Order::select(['id', 'status', 'plugin_id', 'goods_price', 'order_goods_price', 'price', 'refund_id', 'dispatch_price', 'fee_amount', 'service_fee_amount', 'pay_time'])
  26. ->with(['orderGoods']);
  27. }
  28. public function index(Request $request)
  29. {
  30. $this->validate([
  31. 'order_id' => 'required|integer'
  32. ]);
  33. $order = $this->getOrder()->find($request->query('order_id'));
  34. if (!isset($order)) {
  35. throw new AppException('订单不存在');
  36. }
  37. if ($order->refund_id) {
  38. throw new AppException('已存在售后申请,处理中');
  39. }
  40. $data = RefundService::refundApplyData($order);
  41. event(($event = new OrderRefundApplyDataEvent($data)));
  42. return $this->successJson('成功', $event->getData());
  43. //预约订单限制
  44. // if (!is_null(\app\common\modules\shop\ShopConfig::current()->get('store_reserve_refund'))) {
  45. // $class = array_get(\app\common\modules\shop\ShopConfig::current()->get('store_reserve_refund'), 'class');
  46. // $function = array_get(\app\common\modules\shop\ShopConfig::current()->get('store_reserve_refund'), 'function');
  47. // $plugin_res = $class::$function($request->query('order_id'));
  48. // if(!$plugin_res['res']) {
  49. // throw new AppException($plugin_res['msg']);
  50. // }
  51. // }
  52. //
  53. //
  54. // //处理订单可退款商品数量
  55. // $order->orderGoods->map(function ($orderGoods) {
  56. // $orderGoods->refundable_total = $orderGoods->total - $orderGoods->after_sales['refunded_total'];
  57. // $orderGoods->unit_price = bankerRounding($orderGoods->payment_amount / $orderGoods->total);
  58. // });
  59. //
  60. //
  61. // $refundTypes = RefundService::getOptionalType($order);
  62. //
  63. // $data = compact('order','refundTypes');
  64. //
  65. // $refundedPrice = \app\common\models\refund\RefundApply::getAfterSales($order->id)->get();
  66. //
  67. //
  68. // $orderOtherPrice = $this->getOrderOtherPrice($order);
  69. //
  70. // //这里减去运费和其他费用是因为前端直接拿这个字段当订单金额,但是售后现在把运费分离出来了。
  71. // $data['order']['price'] = max($order->price - $order->dispatch_price - $orderOtherPrice,0);
  72. //
  73. // //可退运费
  74. // $data['refundable_freight'] = max(bcsub($order->dispatch_price, $refundedPrice->sum('freight_price'),2),0);
  75. // //订单可退其他费用
  76. // $data['refundable_other'] = max(bcsub($orderOtherPrice, $refundedPrice->sum('other_price'),2),0);
  77. //
  78. // //支持部分退款的订单类型,平台订单,供应商订单,中台供应链
  79. // $data['support_batch'] = in_array($order->plugin_id, [0,92,120]);
  80. //
  81. // $data['send_back_way'] = RefundService::getSendBackWay($order);
  82. //
  83. // event(($event = new OrderRefundApplyDataEvent($data)));
  84. //
  85. // return $this->successJson('成功', $event->getData());
  86. }
  87. //订单其他费用退款
  88. protected function getOrderOtherPrice($order)
  89. {
  90. //预约商品服务费不退
  91. if (!is_null(\app\common\modules\shop\ShopConfig::current()->get('store_reserve_refund_price')) && $order->status == Order::COMPLETE) {
  92. $class = array_get(\app\common\modules\shop\ShopConfig::current()->get('store_reserve_refund_price'), 'class');
  93. $function = array_get(\app\common\modules\shop\ShopConfig::current()->get('store_reserve_refund_price'), 'function');
  94. $plugin_res = $class::$function($order);
  95. if($plugin_res['res']) {
  96. return $order->fee_amount;
  97. }
  98. }
  99. return $order->fee_amount + $order->service_fee_amount;
  100. }
  101. public function store(Request $request)
  102. {
  103. $this->validate([
  104. // 'reason' => 'required|string',
  105. 'content' => 'sometimes|string',
  106. 'refund_type' => 'required|integer',
  107. 'order_id' => 'required|integer'
  108. ], $request,[
  109. 'reason.required'=>'退款原因未选择',
  110. 'refund_type.required'=>'退款方式未选择',
  111. ]);
  112. //预约订单限制
  113. if (!is_null(\app\common\modules\shop\ShopConfig::current()->get('store_reserve_refund'))) {
  114. $class = array_get(\app\common\modules\shop\ShopConfig::current()->get('store_reserve_refund'), 'class');
  115. $function = array_get(\app\common\modules\shop\ShopConfig::current()->get('store_reserve_refund'), 'function');
  116. $plugin_res = $class::$function($request->input('order_id'));
  117. if(!$plugin_res['res'])
  118. {
  119. throw new AppException($plugin_res['msg']);
  120. }
  121. }
  122. $order = Order::find($request->input('order_id'));
  123. if (!isset($order)) {
  124. throw new AppException('订单不存在');
  125. }
  126. if ($order->uid != \YunShop::app()->getMemberId()) {
  127. throw new AppException('无效申请,该订单属于其他用户');
  128. }
  129. if ($order->status < Order::WAIT_SEND) {
  130. throw new AppException('订单未付款,无法退款');
  131. }
  132. if ($order->hasOneRefundApply && $order->hasOneRefundApply->isRefunding()) {
  133. throw new AppException('申请已提交,处理中');
  134. }
  135. $refundApply = new \app\frontend\modules\refund\services\operation\RefundApply();
  136. $refundApply->setRelation('order',$order);
  137. DB::transaction(function()use($refundApply){
  138. $refundApply->execute();
  139. });
  140. return $this->successJson('成功', $refundApply->toArray());
  141. }
  142. }