'json', ]; #未支付过期时间 const EXP_PAY=1800; const EXP_PAY_OFFLINE=3*86400; #待收货过期时间 const EXP_REC=30*86400; #已完成可售后时间 const EXP_OVER=7*86400; const PT_QYWY=1; const PT_WX=2; const PT_ZFB=3; const PT_YL=4; const PT_DF=5; const PT_OFF=6; public static $pay_types=[ self::PT_QYWY=>'企业网银', self::PT_WX=>'微信', self::PT_ZFB=>'支付宝', self::PT_YL=>'银联', self::PT_DF=>'代付', self::PT_OFF=>'线下支付', ]; const S_WAIT_PAY=0; const S_WAIT_SEND=5; const S_WAIT_REC=10; const S_OVER=20; const S_CANCEL=30; //const S_REFUND=40; public static $status=[ self::S_WAIT_PAY=>'待支付', self::S_WAIT_SEND=>'待发货', self::S_WAIT_REC=>'待收货', self::S_OVER=>'已完成', self::S_CANCEL=>'已取消', //self::S_REFUND=>'退款退货', ]; /** * @return string[] */ public static function getStatus(): array { return self::$status; } protected $autoWriteTimestamp=true; public function info(){ return $this->hasMany(OrderInfo::class,'order_id'); } public function user(){ return $this->belongsTo(User::class); } public function address(){ return $this->hasOne(OrderAddress::class,'order_id'); } public function logistics(){ return $this->hasOne(OrderLogistics::class,'order_id'); } public function voucher(){ return $this->hasOne(OrderVoucher::class,'order_id'); } /*public function getGoodsAttr(){ $info=$this->info()->with(['goodsBak'])->find(); $goods=$info['goodsBak']; return [ 'goods'=>$goods['goods'], 'sku'=>$goods['sku'], ]; }*/ public function getIsWaitPayAttr($_,$model){ return $model['status']==self::S_WAIT_PAY; } public function getPayTypeTextAttr($_,$model){ if(empty($model['pay_type'])){ return null; } return self::getPayTypes()[$model['pay_type']]; } /** * @return string[] */ public static function getPayTypes(): array { return self::$pay_types; } public static function continue($status){ $now=time(); return self::where('status',$status)->limit(20)->where('continue_expire_time','<',$now); } #未支付过期 public function makeCancel(){ $this['status']=self::S_CANCEL; $this['cancel_time']=time(); foreach ($this->info as $orderInfo){ Goods::where('id',$orderInfo['goods_id'])->setDec('num_sell',$orderInfo['num']); GoodsSku::where('id',$orderInfo['goods_sku_id'])->setDec('num_sell',$orderInfo['num']); } $this->save(); } #待收货过期 public function makeRec(){ $this['status']=self::S_OVER; $this['rec_time']=time(); $this->save(); } #支付 public function makePayInfo($pay_type){ $user=$this->user; if($pay_type==self::PT_OFF){ $this['continue_expire_time']=strtotime(date('Y-m-d 00:00:00',time()+self::EXP_PAY_OFFLINE+86400))-1; return [ 'account_name'=>config('site.account_name'), 'bank_no'=>config('site.account_bank_no'), 'bank_name'=>config('site.accout_bank_name'), ]; }else{ $this['continue_expire_time']=time()+self::EXP_PAY; } $this->save(); if($pay_type==self::PT_OFF){ return [ 'account_name'=>config('site.account_name'), 'bank_no'=>config('site.account_bank_no'), 'bank_name'=>config('site.accout_bank_name'), ]; } return Payment::pay( $user, $pay_type, $this['amount_pay'], $this['id'], "订单【{$this['order_no']}】付款", $this->getTable(), ); } #支付后 public static function makePayed(Payment $payment){ $order=Orders::find($payment['payment_id']); if(!$order){ return false; } if(!$order->isNotPay()){ return false; } $order['payment_id']=$payment['id']; $order->makePay($payment['pay_type']); return true; } public function makePay($payType=self::PT_OFF){ $order=$this; $order['status']=self::S_WAIT_SEND; $order['pay_time']=time(); $order['pay_type']=$payType; $order->save(); } #发货 public function makeSend($logistics,$data){ $newData=Arr::only($data,['com_id','trans_no','remark']); if(!$logistics) { $this->logistics()->save($newData); $this['status']=self::S_WAIT_REC; $this['send_time']=time(); $this->save(); }else{ $logistics->save($newData); } } #确认收货 public function makeOver(){ $this['status']=self::S_OVER; $this->save(); } /* * 是否未支付 */ public function isNotPay(){ return $this['status']===self::S_WAIT_PAY; } /** * 是否允许退款 */ public function allowRefund(){ return !in_array($this['status'],[ self::S_WAIT_PAY, self::S_CANCEL, ]); } /** * 是否允许取消 */ public function allowCancel(){ return in_array($this['status'],[ self::S_WAIT_PAY, ]); } /** * 是否允许确认收货 */ public function allowOver(){ return in_array($this['status'],[ self::S_WAIT_REC, ]); } protected static function init() { self::beforeInsert(function (self $orders){ #优惠总金额 $orders['amount_discount']=bcAddAll($orders['amount_coupon']??0,$orders['amount_coupon_kill']); #过期时间 $orders['continue_expire_time']=time()+self::EXP_PAY; }); self::afterInsert(function (self $orders){ #添加发票 UserTax::fromOrder($orders); }); self::beforeUpdate(function (self $order){ $data=$order->getChangedData(); if(!empty($data['status'])){ if($data['status']==self::S_OVER){ $order['continue_expire_time']=time()+self::EXP_OVER; } } }); } }