Orders.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <?php
  2. namespace app\common\model;
  3. use app\common\service\UserSvc;
  4. use think\db\Query;
  5. use think\Model;
  6. use Yansongda\Supports\Arr;
  7. /**
  8. * @property OrderInfo info
  9. * @property User user
  10. * @property bool is_wait_pay
  11. * @property Payment payment
  12. * @property int user_id
  13. * @property int status
  14. * @method static static payed()
  15. * @method Query hasGoods($goods_id)
  16. */
  17. class Orders extends Model
  18. {
  19. protected $type=[
  20. 'tax'=>'json',
  21. ];
  22. #未支付过期时间
  23. const EXP_PAY=1800;
  24. #线下付款超时
  25. const EXP_PAY_OFFLINE=3*86400;
  26. #代付超时
  27. const EXP_PAY_DF=3*86400;
  28. #待收货过期时间
  29. const EXP_REC=30*86400;
  30. #已完成可售后时间
  31. const EXP_OVER=7*86400;
  32. const PT_QYWY=1;
  33. const PT_WX=2;
  34. const PT_ZFB=3;
  35. const PT_YL=4;
  36. const PT_DF=5;
  37. const PT_OFF=6;
  38. public static $pay_types=[
  39. self::PT_QYWY=>'企业网银',
  40. self::PT_WX=>'微信',
  41. self::PT_ZFB=>'支付宝',
  42. self::PT_YL=>'银联',
  43. self::PT_DF=>'代付',
  44. self::PT_OFF=>'线下支付',
  45. ];
  46. const S_WAIT_PAY=0;
  47. const S_WAIT_SEND=5;
  48. const S_WAIT_REC=10;
  49. const S_OVER=20;
  50. const S_CANCEL=30;
  51. //const S_REFUND=40;
  52. public static $status=[
  53. self::S_WAIT_PAY=>'待支付',
  54. self::S_WAIT_SEND=>'待发货',
  55. self::S_WAIT_REC=>'待收货',
  56. self::S_OVER=>'已完成',
  57. self::S_CANCEL=>'已取消',
  58. //self::S_REFUND=>'退款退货',
  59. ];
  60. /**
  61. * @return string[]
  62. */
  63. public static function getStatus(): array
  64. {
  65. return self::$status;
  66. }
  67. protected $autoWriteTimestamp=true;
  68. public function info(){
  69. return $this->hasMany(OrderInfo::class,'order_id');
  70. }
  71. public function user(){
  72. return $this->belongsTo(User::class);
  73. }
  74. public function payment(){
  75. return $this->belongsTo(Payment::class);
  76. }
  77. public function address(){
  78. return $this->hasOne(OrderAddress::class,'order_id');
  79. }
  80. public function logistics(){
  81. return $this->hasOne(OrderLogistics::class,'order_id');
  82. }
  83. public function voucher(){
  84. return $this->hasOne(OrderVoucher::class,'order_id');
  85. }
  86. /*public function getGoodsAttr(){
  87. $info=$this->info()->with(['goodsBak'])->find();
  88. $goods=$info['goodsBak'];
  89. return [
  90. 'goods'=>$goods['goods'],
  91. 'sku'=>$goods['sku'],
  92. ];
  93. }*/
  94. public function getIsWaitPayAttr($_,$model){
  95. return $model['status']==self::S_WAIT_PAY;
  96. }
  97. public function getIsEvaledAttr($_,$model){
  98. $goodsIds=array_unique(OrderInfo::where('order_id',$model['id'])->column('goods_id'));
  99. $has=GoodsEval::where('order_id',$model['id'])->whereIn('goods_id',$goodsIds)->count();
  100. return $has==count($goodsIds);
  101. }
  102. public function getPayTypeTextAttr($_,$model){
  103. if(empty($model['pay_type'])){
  104. return null;
  105. }
  106. return self::getPayTypes()[$model['pay_type']];
  107. }
  108. /**
  109. * @return string[]
  110. */
  111. public static function getPayTypes(): array
  112. {
  113. return self::$pay_types;
  114. }
  115. public static function continue($status){
  116. $now=time();
  117. return self::where('status',$status)->limit(20)->where('continue_expire_time','<',$now);
  118. }
  119. #未支付过期
  120. public function makeCancel(){
  121. $this['status']=self::S_CANCEL;
  122. $this['cancel_time']=time();
  123. foreach ($this->info as $orderInfo){
  124. Goods::where('id',$orderInfo['goods_id'])->setDec('num_sell',$orderInfo['num']);
  125. GoodsSku::where('id',$orderInfo['goods_sku_id'])->setDec('num_sell',$orderInfo['num']);
  126. }
  127. $this->save();
  128. }
  129. #待收货过期
  130. public function makeRec(){
  131. $this['status']=self::S_OVER;
  132. $this['rec_time']=time();
  133. $this->save();
  134. }
  135. #支付
  136. public function makePayInfo($pay_type){
  137. $user=$this->user;
  138. if($pay_type==self::PT_OFF){
  139. $this['continue_expire_time']=strtotime(date('Y-m-d 00:00:00'))+self::EXP_PAY_OFFLINE+86400-1;
  140. $this->save();
  141. return [
  142. 'account_name'=>config('site.account_name'),
  143. 'bank_no'=>config('site.account_bank_no'),
  144. 'bank_name'=>config('site.accout_bank_name'),
  145. ];
  146. }elseif($pay_type==self::PT_DF){
  147. $this['continue_expire_time']=strtotime(date('Y-m-d 00:00:00'))+self::EXP_PAY_DF+86400-1;
  148. $this->save();
  149. return [
  150. 'expire'=>$this['continue_expire_time'],
  151. ];
  152. }else{
  153. $this['continue_expire_time']=time()+self::EXP_PAY;
  154. $this->save();
  155. }
  156. if($pay_type==self::PT_OFF){
  157. return [
  158. 'account_name'=>config('site.account_name'),
  159. 'bank_no'=>config('site.account_bank_no'),
  160. 'bank_name'=>config('site.accout_bank_name'),
  161. ];
  162. }
  163. return Payment::pay(
  164. $user,
  165. $pay_type,
  166. $this['amount_pay'],
  167. $this['id'],
  168. "订单【{$this['order_no']}】付款",
  169. $this->getTable(),
  170. );
  171. }
  172. #支付后
  173. public static function makePayed(Payment $payment){
  174. $order=Orders::find($payment['payment_id']);
  175. if(!$order){
  176. return false;
  177. }
  178. if(!$order->isNotPay()){
  179. return false;
  180. }
  181. $order['payment_id']=$payment['id'];
  182. #代付
  183. if($order['user_id']!=$payment->user_id){
  184. }
  185. $order->save();
  186. $order->makePay($payment['pay_type']);
  187. return true;
  188. }
  189. public function makePay($payType=self::PT_OFF){
  190. $order=$this;
  191. $order['status']=self::S_WAIT_SEND;
  192. $order['pay_time']=time();
  193. $order['pay_type']=$payType;
  194. $order->save();
  195. }
  196. #发货
  197. public function makeSend($logistics,$data){
  198. $newData=Arr::only($data,['com_id','trans_no','remark']);
  199. if(!$logistics) {
  200. $this->logistics()->save($newData);
  201. $this['status']=self::S_WAIT_REC;
  202. $this['send_time']=time();
  203. $this->save();
  204. }else{
  205. $logistics->save($newData);
  206. }
  207. }
  208. #确认收货
  209. public function makeOver(){
  210. $this['status']=self::S_OVER;
  211. $this->save();
  212. }
  213. /*
  214. * 是否未支付
  215. */
  216. public function isNotPay(){
  217. return $this['status']===self::S_WAIT_PAY;
  218. }
  219. /**
  220. * 是否允许退款
  221. */
  222. public function allowRefund(){
  223. return !in_array($this['status'],[
  224. self::S_WAIT_PAY,
  225. self::S_CANCEL,
  226. ]);
  227. }
  228. /**
  229. * 是否允许取消
  230. */
  231. public function allowCancel(){
  232. return in_array($this['status'],[
  233. self::S_WAIT_PAY,
  234. ]);
  235. }
  236. /**
  237. * 是否允许确认收货
  238. */
  239. public function allowOver(){
  240. return in_array($this['status'],[
  241. self::S_WAIT_REC,
  242. ]);
  243. }
  244. public function scopePayed(Query $query){
  245. $query->whereNotIn('status',[self::S_CANCEL,self::S_WAIT_PAY]);
  246. }
  247. public function scopeHasGoods(Query $query,$goods_id){
  248. $query->whereExists(
  249. OrderInfo::whereRaw("orders.id=order_info.order_id and order_info.goods_id={$goods_id}")->buildSql()
  250. );
  251. }
  252. protected static function init()
  253. {
  254. self::beforeInsert(function (self $orders){
  255. #优惠总金额
  256. //$orders['amount_discount']=bcAddAll($orders['amount_coupon']??0,$orders['amount_coupon_kill']);
  257. #过期时间
  258. $orders['continue_expire_time']=time()+self::EXP_PAY;
  259. #去除无发票的
  260. if(empty($orders['tax']) || !in_array($orders['tax']['paper_type']??0,[1,2])){
  261. $orders['tax']=null;
  262. }
  263. #属于哪个销售员
  264. $orders['admin_id']=UserSvc::getSellerId($orders->user_id);
  265. });
  266. self::afterInsert(function (self $orders){
  267. #添加发票
  268. UserTax::fromOrder($orders);
  269. #合同链接
  270. $contract_link=request()->root()."/contract/view/show/{$orders['id']}";
  271. $orders->where('id',$orders['id'])->update([
  272. 'contract_link'=>$contract_link,
  273. ]);
  274. });
  275. self::beforeUpdate(function (self $order){
  276. $data=$order->getChangedData();
  277. if(!empty($data['status'])){
  278. #已完成可售后时间
  279. if($data['status']==self::S_OVER){
  280. $order['continue_expire_time']=time()+self::EXP_OVER;
  281. }
  282. #待收货过期时间
  283. elseif ($data['status']==self::S_WAIT_REC){
  284. $order['continue_expire_time']=time()+self::EXP_REC;
  285. }
  286. }
  287. });
  288. self::afterUpdate(function (self $orders){
  289. if(!empty($orders->status) && $orders->status==self::S_WAIT_SEND){
  290. Transaction::addTransaction($orders);
  291. }
  292. });
  293. }
  294. }