Orders.php 6.2 KB

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