123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- <?php
- namespace app\common\model;
- use app\common\service\OrderRefundService;
- use think\db\Query;
- use think\Model;
- /**
- * 短信验证码
- * @property Orders orders
- * @property User user
- * @property int refund_status
- * @method static static|Query FilterRefund()
- */
- class Refund Extends Model
- {
- protected $name='order_info_refund';
- const REFUND_ING=10;
- const REFUND_PASS=20;
- const REFUND_REJECT=30;
- const REFUND_CANCEL=40;
- public static $refundStatus=[
- self::REFUND_ING=>'申请处理中',
- 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;
- public static $refundTypes=[
- self::REFUND_TYPE_MONEY=>'仅退款',
- self::REFUND_TYPE_ALL=>'退款退货',
- self::REFUND_TYPE_GOODS=>'仅退货',
- ];
- public static $refundTypeGoods=[self::REFUND_TYPE_ALL,self::REFUND_TYPE_GOODS];
- public static $refundTypeMoney=[self::REFUND_TYPE_MONEY,self::REFUND_TYPE_ALL];
- public static $reasons=[
- 1=>'质量问题',
- 2=>'卖家发错货',
- ];
- /**
- * @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 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();
- OrderInfo::where('id',$this['order_info_id'])->update([
- 'refund_id'=>null,
- ]);
- }
- 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
- );
- }
- /**
- * 售后中及已售后的
- */
- public function scopeFilterRefund(Query $query){
- $query->whereBetween("{$this->getTable()}.refund_status",[self::REFUND_ING,self::REFUND_PASS]);
- }
- 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);
- }
- });
- }
- }
|