EditController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 2017/4/14
  6. * Time: 上午11:59
  7. */
  8. namespace app\frontend\modules\refund\controllers;
  9. use app\common\components\ApiController;
  10. use app\common\exceptions\AppException;
  11. use app\common\models\Order;
  12. use app\frontend\modules\refund\models\RefundApply;
  13. use app\frontend\modules\refund\services\operation\RefundEditApply;
  14. use app\frontend\modules\refund\services\RefundService;
  15. class EditController extends ApiController
  16. {
  17. public function index(\Illuminate\Http\Request $request){
  18. $this->validate([
  19. 'refund_id' => 'required|integer',
  20. ]);
  21. $refundApply = RefundApply::detail()->find($request->query('refund_id'));
  22. if(!isset($refundApply)){
  23. throw new AppException('未找到该退款申请');
  24. }
  25. $order = Order::find($refundApply->order_id);
  26. if (!isset($order)) {
  27. throw new AppException('订单不存在');
  28. }
  29. $refundTypes = RefundService::getOptionalType($order);
  30. $send_back_way = RefundService::getSendBackWay($order);
  31. $send_back_way_data = RefundService::getSendBackWayData($refundApply);
  32. $data = compact('refundApply','refundTypes','send_back_way','send_back_way_data');
  33. $refundedPrice = \app\common\models\refund\RefundApply::uniacid()
  34. ->select('order_id','price','apply_price', 'freight_price', 'other_price')
  35. ->where('order_id', $order->id)
  36. ->where('status', '>=', RefundApply::COMPLETE)
  37. ->get();
  38. $orderOtherPrice = $this->getOrderOtherPrice($order);
  39. //可退运费
  40. $data['refundable_freight'] = max(bcsub($order->dispatch_price, $refundedPrice->sum('freight_price'),2),0);
  41. //订单可退其他费用
  42. $data['refundable_other'] = max(bcsub($orderOtherPrice, $refundedPrice->sum('other_price'),2),0);
  43. return $this->successJson('成功',$data);
  44. }
  45. //订单其他费用退款
  46. protected function getOrderOtherPrice($order)
  47. {
  48. //预约商品服务费不退
  49. if (!is_null(\app\common\modules\shop\ShopConfig::current()->get('store_reserve_refund_price')) && $order->status == Order::COMPLETE) {
  50. $class = array_get(\app\common\modules\shop\ShopConfig::current()->get('store_reserve_refund_price'), 'class');
  51. $function = array_get(\app\common\modules\shop\ShopConfig::current()->get('store_reserve_refund_price'), 'function');
  52. $plugin_res = $class::$function($order);
  53. if($plugin_res['res']) {
  54. return $order->service_fee_amount;
  55. }
  56. }
  57. return $order->fee_amount + $order->service_fee_amount;
  58. }
  59. public function store(\Illuminate\Http\Request $request)
  60. {
  61. $this->validate([
  62. // 'reason' => 'required|string',
  63. 'content' => 'sometimes|string',
  64. 'refund_type' => 'required|integer',
  65. 'refund_id' => 'required|integer'
  66. ],request(), []);
  67. $refundApply = RefundEditApply::find($request->input('refund_id'));
  68. if ($refundApply->uid != \YunShop::app()->getMemberId()) {
  69. throw new AppException('无效申请,该订单属于其他用户');
  70. }
  71. if (!isset($refundApply)) {
  72. throw new AppException('退款申请不存在');
  73. }
  74. $refundApply->execute();
  75. return $this->successJson('成功', $refundApply->toArray());
  76. }
  77. }