Orders.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. public function getPayTypeTextAttr($_,$model){
  81. if(empty($model['pay_type'])){
  82. return null;
  83. }
  84. return self::getPayTypes()[$model['pay_type']];
  85. }
  86. /**
  87. * @return string[]
  88. */
  89. public static function getPayTypes(): array
  90. {
  91. return self::$pay_types;
  92. }
  93. public static function continue($status){
  94. $now=time();
  95. return self::where('status',$status)->limit(20)->where('continue_expire_time','<',$now);
  96. }
  97. #未支付过期
  98. public function makeCancel(){
  99. $this['status']=self::S_CANCEL;
  100. $this['cancel_time']=time();
  101. foreach ($this->info as $orderInfo){
  102. Goods::where('id',$orderInfo['goods_id'])->setDec('num_sell',$orderInfo['num']);
  103. GoodsSku::where('id',$orderInfo['goods_sku_id'])->setDec('num_sell',$orderInfo['num']);
  104. }
  105. $this->save();
  106. }
  107. #待收货过期
  108. public function makeRec(){
  109. $this['status']=self::S_OVER;
  110. $this['rec_time']=time();
  111. $this->save();
  112. }
  113. #支付
  114. public function makePayInfo($pay_type){
  115. $user=$this->user;
  116. return Payment::pay(
  117. $user,
  118. $pay_type,
  119. $this['amount_pay'],
  120. $this['id'],
  121. "订单【{$this['order_no']}】付款",
  122. $this->getTable(),
  123. );
  124. }
  125. #支付后
  126. public static function makePayed(Payment $payment){
  127. $order=Orders::find($payment['payment_id']);
  128. if(!$order){
  129. return false;
  130. }
  131. if(!$order->isNotPay()){
  132. return false;
  133. }
  134. $order['status']=self::S_WAIT_SEND;
  135. $order['pay_time']=$payment['pay_time'];
  136. $order->save();
  137. return true;
  138. }
  139. #发货
  140. public function makeSend($logistics,$data){
  141. $newData=Arr::only($data,['com_id','trans_no']);
  142. if(!$logistics) {
  143. $this->logistics()->save($newData);
  144. $this['status']=self::S_WAIT_REC;
  145. $this['send_time']=time();
  146. $this->save();
  147. }else{
  148. $logistics->save($newData);
  149. }
  150. }
  151. #确认收货
  152. public function makeOver(){
  153. $this['status']=self::S_OVER;
  154. $this->save();
  155. }
  156. /*
  157. * 是否未支付
  158. */
  159. public function isNotPay(){
  160. return $this['status']===self::S_WAIT_PAY;
  161. }
  162. /**
  163. * 是否允许退款
  164. */
  165. public function allowRefund(){
  166. return !in_array($this['status'],[
  167. self::S_WAIT_PAY,
  168. self::S_CANCEL,
  169. ]);
  170. }
  171. /**
  172. * 是否允许取消
  173. */
  174. public function allowCancel(){
  175. return in_array($this['status'],[
  176. self::S_WAIT_PAY,
  177. ]);
  178. }
  179. /**
  180. * 是否允许确认收货
  181. */
  182. public function allowOver(){
  183. return in_array($this['status'],[
  184. self::S_WAIT_REC,
  185. ]);
  186. }
  187. protected static function init()
  188. {
  189. self::beforeInsert(function (self $orders){
  190. #优惠总金额
  191. $orders['amount_discount']=bcAddAll($orders['amount_coupon']??0,$orders['amount_coupon_kill']);
  192. #过期时间
  193. $orders['continue_expire_time']=time()+self::EXP_PAY;
  194. });
  195. self::afterInsert(function (self $orders){
  196. #添加发票
  197. UserTax::fromOrder($orders);
  198. });
  199. self::beforeUpdate(function (self $order){
  200. $data=$order->getChangedData();
  201. if(!empty($data['status'])){
  202. if($data['status']==self::S_WAIT_REC){
  203. $order['continue_expire_time']=time()+self::EXP_REC;
  204. }elseif($data['status']==self::S_OVER){
  205. $order['continue_expire_time']=time()+self::EXP_OVER;
  206. }
  207. }
  208. });
  209. }
  210. }