OrderCloseAndRefund.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: blank
  5. * Date: 2022/11/22
  6. * Time: 15:20
  7. */
  8. namespace app\backend\modules\refund\services\operation;
  9. use app\common\models\refund\RefundProcessLog;
  10. use app\common\modules\refund\RefundOrderFactory;
  11. use app\frontend\modules\refund\services\RefundService;
  12. class OrderCloseAndRefund extends \app\frontend\modules\refund\services\operation\RefundApply
  13. {
  14. protected $port_type = 'backend';
  15. protected function updateBefore()
  16. {
  17. //售后记录必须是这笔订单所属会员的
  18. if (!isset($this->uid) || $this->uid != $this->order->uid) {
  19. $this->uid = $this->order->uid;
  20. }
  21. $refundApplyData = [
  22. 'order_id' => $this->order->id,
  23. 'refund_type' => static::REFUND_TYPE_REFUND_MONEY,
  24. 'content' => request()->input('reson', ''),
  25. 'reason' => '订单关闭并退款',
  26. 'images' => [],
  27. ];
  28. $refundedPrice = \app\common\models\refund\RefundApply::getAfterSales($this->order->id)->get();
  29. $refundOrder = $this->relatedPluginOrder();
  30. //申请退款金额
  31. $refundApplyData['apply_price'] = $this->getRefundApplyPrice();
  32. //退款运费金额
  33. $refundApplyData['freight_price'] = max(bcsub($refundOrder->getOrderFreightPrice(), $refundedPrice->sum('freight_price'),2),0);
  34. //退款其他金额
  35. $refundApplyData['other_price'] = max(bcsub($refundOrder->getOrderOtherPrice(), $refundedPrice->sum('other_price'),2),0);
  36. //实际退款金额 = 订单实付金额 - 已退款金额
  37. $refundApplyData['price'] = max(bcsub($this->order->price,$refundedPrice->sum('price'),2),0);
  38. $refundApplyData['part_refund'] = static::ORDER_CLOSE;
  39. $refundApplyData['refund_sn'] = RefundService::createOrderRN();
  40. $this->fill($refundApplyData);
  41. }
  42. protected function writeLog()
  43. {
  44. $detail = [
  45. '售后类型:退款',
  46. '退款金额:'.$this->price,
  47. $this->freight_price?'运费:'. $this->freight_price :'',
  48. $this->other_price?'其他费用:'. $this->other_price :'',
  49. '售后原因:'.$this->reason,
  50. '说明:'.$this->content,
  51. ];
  52. $processLog = RefundProcessLog::logInstance($this, RefundProcessLog::OPERATOR_SHOP);
  53. $processLog->setAttribute('operate_type', RefundProcessLog::OPERATE_APPLY_SHOP);
  54. $processLog->saveLog($detail, request()->input());
  55. }
  56. }