'json', ]; #未支付过期时间 const EXP_PAY=1800; #线下付款超时 const EXP_PAY_OFFLINE=3*86400; #代付超时 const EXP_PAY_DF=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 payment(){ return $this->belongsTo(Payment::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 getIsEvaledAttr($_,$model){ $goodsIds=array_unique(OrderInfo::where('order_id',$model['id'])->column('goods_id')); $has=GoodsEval::where('order_id',$model['id'])->whereIn('goods_id',$goodsIds)->count(); return $has==count($goodsIds); } 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'))+self::EXP_PAY_OFFLINE+86400-1; $this->save(); return [ 'account_name'=>config('site.account_name'), 'bank_no'=>config('site.account_bank_no'), 'bank_name'=>config('site.accout_bank_name'), ]; }elseif($pay_type==self::PT_DF){ $this['continue_expire_time']=strtotime(date('Y-m-d 00:00:00'))+self::EXP_PAY_DF+86400-1; $this->save(); return [ 'expire'=>$this['continue_expire_time'], ]; }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']; #代付 if($order['user_id']!=$payment->user_id){ } $order->save(); $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 makeSendCommission(){ if($this->admin_id){ } } /* * 是否未支付 */ 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, ]); } public function scopePayed(Query $query){ $query->whereNotIn('status',[self::S_CANCEL,self::S_WAIT_PAY]); } public function scopeHasGoods(Query $query,$goods_id){ $query->whereExists( OrderInfo::whereRaw("orders.id=order_info.order_id and order_info.goods_id={$goods_id}")->buildSql() ); } 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; #去除无发票的 if(empty($orders['tax']) || !in_array($orders['tax']['paper_type']??0,[1,2])){ $orders['tax']=null; } #属于哪个销售员 $orders['admin_id']=UserSvc::getSellerId($orders->user_id); }); self::afterInsert(function (self $orders){ #添加发票 UserTax::fromOrder($orders); #合同链接 $contract_link=request()->root(true)."/contract/view/show/{$orders['id']}"; $orders->where('id',$orders['id'])->update([ 'contract_link'=>$contract_link, ]); }); 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; } #待收货过期时间 elseif ($data['status']==self::S_WAIT_REC){ $order['continue_expire_time']=time()+self::EXP_REC; } } }); self::afterUpdate(function (self $orders){ if(!empty($orders->status) && $orders->status==self::S_WAIT_SEND){ Transaction::addTransaction($orders); } #如果已完成发放提成 if(!empty($orders->status) && $orders->status==self::S_OVER){ } }); } }