Orders.php 6.7 KB

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