Orders.php 5.7 KB

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