Refund.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace app\common\model;
  3. use app\common\service\OrderRefundService;
  4. use think\db\Query;
  5. use think\Model;
  6. /**
  7. * 短信验证码
  8. * @property Orders orders
  9. * @property User user
  10. * @property int refund_status
  11. * @method static static|Query FilterRefund()
  12. */
  13. class Refund Extends Model
  14. {
  15. protected $name='order_info_refund';
  16. const REFUND_ING=10;
  17. const REFUND_PASS=20;
  18. const REFUND_REJECT=30;
  19. const REFUND_CANCEL=40;
  20. public static $refundStatus=[
  21. self::REFUND_ING=>'申请处理中',
  22. self::REFUND_PASS=>'申请通过',
  23. self::REFUND_REJECT=>'申请驳回',
  24. self::REFUND_CANCEL=>'已取消',
  25. ];
  26. /**
  27. * @return string[]
  28. */
  29. public static function getRefundStatus(): array
  30. {
  31. return self::$refundStatus;
  32. }
  33. const REFUND_TYPE_MONEY=1;
  34. const REFUND_TYPE_ALL=2;
  35. const REFUND_TYPE_GOODS=3;
  36. public static $refundTypes=[
  37. self::REFUND_TYPE_MONEY=>'仅退款',
  38. self::REFUND_TYPE_ALL=>'退款退货',
  39. self::REFUND_TYPE_GOODS=>'仅退货',
  40. ];
  41. public static $refundTypeGoods=[self::REFUND_TYPE_ALL,self::REFUND_TYPE_GOODS];
  42. public static $refundTypeMoney=[self::REFUND_TYPE_MONEY,self::REFUND_TYPE_ALL];
  43. /**
  44. * @return string[]
  45. */
  46. public static function getRefundTypes(): array
  47. {
  48. return self::$refundTypes;
  49. }
  50. /**
  51. * @return int[]
  52. */
  53. public static function getRefundTypeGoods(): array
  54. {
  55. return self::$refundTypeGoods;
  56. }
  57. /**
  58. * @return int[]
  59. */
  60. public static function getRefundTypeMoney(): array
  61. {
  62. return self::$refundTypeMoney;
  63. }
  64. protected $autoWriteTimestamp=true;
  65. public function orderInfo(){
  66. return $this->belongsTo(OrderInfo::class);
  67. }
  68. public function orders(){
  69. return $this->belongsTo(Orders::class,'order_id');
  70. }
  71. public function user(){
  72. return $this->belongsTo(User::class);
  73. }
  74. public function allowCancel(){
  75. return in_array($this['refund_status'],[
  76. self::REFUND_ING,
  77. ]);
  78. }
  79. public function makeCancel(){
  80. $this['refund_status']=self::REFUND_CANCEL;
  81. $this->save();
  82. OrderInfo::where('id',$this['order_info_id'])->update([
  83. 'refund_id'=>null,
  84. ]);
  85. }
  86. public function allowAudit(){
  87. return $this['refund_status']==self::REFUND_ING;
  88. }
  89. #退款相关
  90. /**
  91. * 退款金额
  92. */
  93. public function getRefundAmount(){
  94. return $this['amount'];
  95. }
  96. public function refundResult($succ,$remark=''){
  97. if(is_bool($succ)){
  98. $this['pay_status']=$succ?1:2;
  99. }else{
  100. $this['pay_status']=$succ;
  101. }
  102. $this['pay_remark']=$remark;
  103. $this->save();
  104. }
  105. public function isRefundMoney(){
  106. return in_array($this['refund_type'],self::getRefundTypeMoney());
  107. }
  108. #end
  109. public function makeAudit($pass){
  110. $this['refund_status']=$pass?self::REFUND_PASS:self::REFUND_REJECT;
  111. $refundMoney=false;
  112. if($pass){
  113. #如果退货
  114. if(in_array($this['refund_type'],self::getRefundTypeGoods())){
  115. $this->orderInfo()->update(['is_return_goods'=>1]);
  116. }
  117. #如果退款,执行退款
  118. if(in_array($this['refund_type'],self::getRefundTypeMoney())){
  119. $this['order_no']=order_no('TK');
  120. $refundMoney=true;
  121. }
  122. }
  123. if(!$this->save()){
  124. throw_user('保存失败');
  125. }
  126. if($refundMoney){
  127. $payment=$this->orders->payment??null;
  128. if($payment){
  129. $refund=new OrderRefundService();
  130. $refund->setPayment($payment);
  131. $refund->setRefund($this);
  132. $refund->setBody("订单[{$this->orders->order_no}]退款");
  133. $refund->pay();
  134. }
  135. }
  136. SiteMsg::sendMsg(
  137. $pass?SiteMsg::TYPE_ORDER_REFUND_PASS:SiteMsg::TYPE_ORDER_REFUND_REJECT,
  138. $this->user
  139. );
  140. }
  141. /**
  142. * 售后中及已售后的
  143. */
  144. public function scopeFilterRefund(Query $query){
  145. $query->whereBetween("{$this->getTable()}.refund_status",[self::REFUND_ING,self::REFUND_PASS]);
  146. }
  147. protected static function init()
  148. {
  149. self::beforeInsert(function (self $refund){
  150. $refund['refund_status']=self::REFUND_ING;
  151. });
  152. self::afterUpdate(function (self $refund){
  153. if($refund->refund_status==self::REFUND_PASS && $refund->isRefundMoney()){
  154. Transaction::addTransaction($refund);
  155. }
  156. });
  157. }
  158. }