Orders.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. use Yansongda\Supports\Arr;
  5. /**
  6. * @property OrderInfo info
  7. * @property User user
  8. * @property bool is_wait_pay
  9. */
  10. class Orders extends Model
  11. {
  12. protected $type=[
  13. 'tax'=>'json',
  14. ];
  15. #未支付过期时间
  16. const EXP_PAY=1800;
  17. #待收货过期时间
  18. const EXP_REC=30*86400;
  19. const PT_QYWY=1;
  20. const PT_WX=2;
  21. const PT_ZFB=3;
  22. const PT_YL=4;
  23. const PT_DF=5;
  24. const PT_OFF=6;
  25. public static $pay_types=[
  26. self::PT_QYWY=>'企业网银',
  27. self::PT_WX=>'微信',
  28. self::PT_ZFB=>'支付宝',
  29. self::PT_YL=>'银联',
  30. self::PT_DF=>'代付',
  31. self::PT_OFF=>'线下支付',
  32. ];
  33. const S_WAIT_PAY=0;
  34. const S_WAIT_SEND=5;
  35. const S_WAIT_REC=10;
  36. const S_OVER=20;
  37. const S_CANCEL=30;
  38. //const S_REFUND=40;
  39. public static $status=[
  40. self::S_WAIT_PAY=>'待支付',
  41. self::S_WAIT_SEND=>'待发货',
  42. self::S_WAIT_REC=>'待收货',
  43. self::S_OVER=>'已完成',
  44. self::S_CANCEL=>'已取消',
  45. //self::S_REFUND=>'退款退货',
  46. ];
  47. /**
  48. * @return string[]
  49. */
  50. public static function getStatus(): array
  51. {
  52. return self::$status;
  53. }
  54. protected $autoWriteTimestamp=true;
  55. public function info(){
  56. return $this->hasMany(OrderInfo::class,'order_id');
  57. }
  58. public function user(){
  59. return $this->belongsTo(User::class);
  60. }
  61. public function address(){
  62. return $this->hasOne(OrderAddress::class,'order_id');
  63. }
  64. public function logistics(){
  65. return $this->hasOne(OrderLogistics::class,'order_id');
  66. }
  67. /*public function getGoodsAttr(){
  68. $info=$this->info()->with(['goodsBak'])->find();
  69. $goods=$info['goodsBak'];
  70. return [
  71. 'goods'=>$goods['goods'],
  72. 'sku'=>$goods['sku'],
  73. ];
  74. }*/
  75. public function getIsWaitPayAttr($_,$model){
  76. return $model['status']==self::S_WAIT_PAY;
  77. }
  78. /**
  79. * @return string[]
  80. */
  81. public static function getPayTypes(): array
  82. {
  83. return self::$pay_types;
  84. }
  85. public static function continue($status){
  86. $now=time();
  87. return self::where('status',$status)->limit(20)->where('continue_expire_time','<',$now);
  88. }
  89. #未支付过期
  90. public function makeCancel(){
  91. $this['status']=self::S_CANCEL;
  92. $this['cancel_time']=time();
  93. foreach ($this->info as $orderInfo){
  94. Goods::where('id',$orderInfo['goods_id'])->setDec('num_sell',$orderInfo['num']);
  95. GoodsSku::where('id',$orderInfo['goods_sku_id'])->setDec('num_sell',$orderInfo['num']);
  96. }
  97. $this->save();
  98. }
  99. #待收货过期
  100. public function makeRec(){
  101. $this['status']=self::S_OVER;
  102. $this['rec_time']=time();
  103. $this->save();
  104. }
  105. #支付
  106. public function makePayInfo($pay_type){
  107. $user=$this->user;
  108. return Payment::pay(
  109. $user,
  110. $pay_type,
  111. $this['amount_pay'],
  112. self::class,
  113. 'whenPaySuccess',
  114. $this['id'],
  115. "订单【{$this['order_no']}】付款",
  116. $this->getTable(),
  117. );
  118. }
  119. #支付后
  120. public static function makePayed(Payment $payment){
  121. $order=Orders::find($payment['payment_id']);
  122. if(!$order){
  123. return false;
  124. }
  125. if(!$order->isNotPay()){
  126. return false;
  127. }
  128. $order['status']=self::S_WAIT_SEND;
  129. $order['pay_time']=$payment['pay_time'];
  130. $order->save();
  131. return true;
  132. }
  133. #发货
  134. public function makeSend($logistics,$data){
  135. $newData=Arr::only($data,['com_id','trans_no']);
  136. if(!$logistics) {
  137. $this->logistics()->save($newData);
  138. $this['status']=self::S_WAIT_REC;
  139. $this['send_time']=time();
  140. $this->save();
  141. }else{
  142. $logistics->save($newData);
  143. }
  144. }
  145. /*
  146. * 是否未支付
  147. */
  148. public function isNotPay(){
  149. return $this['status']===self::S_WAIT_PAY;
  150. }
  151. /**
  152. * 是否允许退款
  153. */
  154. public function allowRefund(){
  155. return !in_array($this['status'],[
  156. self::S_WAIT_PAY,
  157. self::S_CANCEL,
  158. ]);
  159. }
  160. /**
  161. * 是否允许取消
  162. */
  163. public function allowCancel(){
  164. return in_array($this['status'],[
  165. self::S_WAIT_PAY,
  166. ]);
  167. }
  168. protected static function init()
  169. {
  170. self::beforeInsert(function (self $orders){
  171. #优惠总金额
  172. $orders['amount_discount']=bcAddAll($orders['amount_coupon']??0,$orders['amount_coupon_kill']);
  173. #过期时间
  174. $orders['continue_expire_time']=time()+self::EXP_PAY;
  175. });
  176. self::beforeUpdate(function (self $order){
  177. $data=$order->getChangedData();
  178. if(!empty($data['status'])){
  179. if($data['status']==self::S_WAIT_REC){
  180. $orders['continue_expire_time']=time()+self::EXP_REC;
  181. }
  182. }
  183. });
  184. }
  185. }