123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- <?php
- namespace app\common\model;
- use think\Model;
- use Yansongda\Supports\Arr;
- /**
- * @property OrderInfo info
- * @property User user
- * @property bool is_wait_pay
- */
- class Orders extends Model
- {
- protected $type=[
- 'tax'=>'json',
- ];
- #未支付过期时间
- const EXP_PAY=1800;
- #待收货过期时间
- 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 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;
- }
- /**
- * @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;
- return Payment::pay(
- $user,
- $pay_type,
- $this['amount_pay'],
- self::class,
- 'whenPaySuccess',
- $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['status']=self::S_WAIT_SEND;
- $order['pay_time']=$payment['pay_time'];
- $order->save();
- return true;
- }
- #发货
- public function makeSend($logistics,$data){
- $newData=Arr::only($data,['com_id','trans_no']);
- 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;
- #添加发票
- if($orders['tax']) {
- UserTax::fromOrder($orders);
- }
- });
- self::beforeUpdate(function (self $order){
- $data=$order->getChangedData();
- if(!empty($data['status'])){
- if($data['status']==self::S_WAIT_REC){
- $order['continue_expire_time']=time()+self::EXP_REC;
- }elseif($data['status']==self::S_OVER){
- $order['continue_expire_time']=time()+self::EXP_OVER;
- }
- }
- });
- }
- }
|