'申请处理中', self::REFUND_PASS=>'申请通过', self::REFUND_REJECT=>'申请驳回', self::REFUND_CANCEL=>'已取消', ]; /** * @return string[] */ public static function getRefundStatus(): array { return self::$refundStatus; } const REFUND_TYPE_MONEY=1; const REFUND_TYPE_ALL=2; const REFUND_TYPE_GOODS=3; const REFUND_TYPE_CANCEL=4; const REFUND_TYPE_HHBX=5; public static $refundTypes=[ self::REFUND_TYPE_MONEY=>'仅退款', self::REFUND_TYPE_ALL=>'退款退货', self::REFUND_TYPE_GOODS=>'仅退货', self::REFUND_TYPE_CANCEL=>'取消订单', self::REFUND_TYPE_HHBX=>'换货保修', ]; public static $refundTypeGoods=[self::REFUND_TYPE_ALL,self::REFUND_TYPE_GOODS]; public static $refundTypeMoney=[self::REFUND_TYPE_MONEY,self::REFUND_TYPE_CANCEL,self::REFUND_TYPE_ALL]; const REASON_QU=1; public static $reasons=[ self::REASON_QU=>'质量问题', 2=>'7天无理由退货(退回运费需客户承担)', 3=>'协商一致退款', 4=>'快递运输破损', 5=>'其他', ]; const TH_TYPE_NONE=0; const TH_TYPE_SELF_SEND=1; const TH_TYPE_SELF_FEE=2; const TH_TYPE_FREE=3; public static $goodsTypes=[ self::TH_TYPE_NONE=>'无需退货', self::TH_TYPE_SELF_SEND=>'自行邮寄', self::TH_TYPE_SELF_FEE=>'拍维修费', self::TH_TYPE_FREE=>'一年以内免费保修', ]; /** * @return string[] */ public static function getReasons(): array { $reasons=self::$reasons; $arr=[]; foreach ($reasons as $key=>$value){ $arr[$key]=compact('key','value'); } return $arr; } /** * @return string[] */ public static function getRefundBys(): array { $obj=self::$goodsTypes; $arr=[]; foreach ($obj as $key=>$value){ $arr[$key]=compact('key','value'); } return $arr; } /** * @return string[] */ public static function getRefundTypes(): array { return self::$refundTypes; } /** * @return int[] */ public static function getRefundTypeGoods(): array { return self::$refundTypeGoods; } /** * @return int[] */ public static function getRefundTypeMoney(): array { return self::$refundTypeMoney; } protected $autoWriteTimestamp=true; public function orderInfo(){ return $this->belongsTo(OrderInfo::class); } public function orders(){ return $this->belongsTo(Orders::class,'order_id'); } public function user(){ return $this->belongsTo(User::class); } public function allowCancel(){ return in_array($this['refund_status'],[ self::REFUND_ING, ]); } public function makeCancel(){ $this['refund_status']=self::REFUND_CANCEL; $this->save(); $this->orderInfo()->save([ 'refund_id'=>null, ]); } #根据订单状态选择售后 public static function makeRefundConfig(OrderInfo $orderInfo,$delKeys=true,$inject=true){ $order=$orderInfo->orders; $refundConfig=[]; #未发货 if($order['status']==$order::S_WAIT_SEND){ $refundConfig['refund_type']=[ self::REFUND_TYPE_CANCEL=>'取消订单', ]; $refundConfig['type']=[ self::TH_TYPE_NONE=>'无需退货', ]; $refundConfig['reason']=array_values(Refund::getReasons()); $refundConfig['req_amount']=1; $refundConfig['req_order']=0; } #已发货 elseif(in_array($order['status'],[$order::S_WAIT_REC,$order::S_OVER])){ #七天内可以退货退款 if(time()<$order['send_time']+7*86400){ $refundConfig['refund_type']=[ self::REFUND_TYPE_ALL=>'退款退货', ]; $refundConfig['type']=[ self::TH_TYPE_FREE=>'自行邮寄', ]; $refundConfig['reason']=array_values(Refund::getReasons()); $refundConfig['req_amount']=1; $refundConfig['req_order']=0; }else{ $refundConfig['refund_type']=[ self::REFUND_TYPE_HHBX=>'换货保修', ]; $refundConfig['req_order']=0; #一年内 if(time()'一年以内免费保修', ]; }else{ $refundConfig['type']=[ self::TH_TYPE_SELF_FEE=>'拍维修费', ]; $refundConfig['req_order']=1; $refundConfig['req_order_goods']=Goods::getFixGoods(); } $refundConfig['reason']=array_values(Refund::getReasons()); $refundConfig['req_amount']=0; } } if($delKeys) { if (!empty($refundConfig['refund_type'])) { $data = []; foreach ($refundConfig['refund_type'] as $key => $value) { $data[] = compact('key', 'value'); } $refundConfig['refund_type'] = $data; } if (!empty($refundConfig['type'])) { $data = []; foreach ($refundConfig['type'] as $key => $value) { $data[] = compact('key', 'value'); } $refundConfig['type'] = $data; } } if($inject){ $orderInfo['refund_config']=$refundConfig?:null; } return $refundConfig; } public function allowAudit(){ return $this['refund_status']==self::REFUND_ING; } #退款相关 /** * 退款金额 */ public function getRefundAmount(){ return $this['amount']; } public function refundResult($succ,$remark=''){ if(is_bool($succ)){ $this['pay_status']=$succ?1:2; }else{ $this['pay_status']=$succ; } $this['pay_remark']=$remark; $this->save(); } public function isRefundMoney(){ return in_array($this['refund_type'],self::getRefundTypeMoney()); } #end public function makeAudit($pass){ $this['refund_status']=$pass?self::REFUND_PASS:self::REFUND_REJECT; $refundMoney=false; if($pass){ #如果退货 if(in_array($this['refund_type'],self::getRefundTypeGoods())){ $this->orderInfo()->update(['is_return_goods'=>1]); } #如果退款,执行退款 if(in_array($this['refund_type'],self::getRefundTypeMoney())){ $this['order_no']=order_no('TK'); $refundMoney=true; } } if(!$this->save()){ throw_user('保存失败'); } if($refundMoney){ $payment=$this->orders->payment??null; if($payment){ $refund=new OrderRefundService(); $refund->setPayment($payment); $refund->setRefund($this); $refund->setBody("订单[{$this->orders->order_no}]退款"); $refund->pay(); } } SiteMsg::sendMsg( $pass?SiteMsg::TYPE_ORDER_REFUND_PASS:SiteMsg::TYPE_ORDER_REFUND_REJECT, $this->user ); #总订单 $order=$this->orders; if($order && $order->statusfilterHasUnRefund() ->find(); if(!$has){ $order->makeCancel(); } } } /** * 售后中及已售后的 * @param Query $query * @param null $status 1进行中2已完成 */ public function scopeFilterRefund(Query $query,$status=null){ if(is_null($status)) { $query->whereBetween("{$this->getTable()}.refund_status", [self::REFUND_ING, self::REFUND_PASS]); }elseif ($status==1){ $query->where("{$this->getTable()}.refund_status", self::REFUND_ING); }elseif ($status==2){ $query->where("{$this->getTable()}.refund_status", self::REFUND_PASS); } } /** * 投诉类型的售后 * @param Query $query */ public function scopeFilterTs(Query $query){ $query->where("{$this->getTable()}.reason1",self::REASON_QU); } protected static function init() { self::beforeInsert(function (self $refund){ $refund['refund_status']=self::REFUND_ING; }); self::afterUpdate(function (self $refund){ if($refund->refund_status==self::REFUND_PASS && $refund->isRefundMoney()){ Transaction::addTransaction($refund); } }); } }