Assoc.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace app\cxinv\model;
  3. class Assoc extends Base
  4. {
  5. //设置字段信息
  6. protected $schema = [
  7. 'id' =>'bigint',//
  8. 'assocNo' =>'varchar',//关联编号
  9. 'apply_id' =>'int',//操作人id
  10. 'apply_name' =>'varchar',//操作人名称
  11. 'type' =>'tinyint',//操作类型 1 单票 2 单款
  12. 'orderCode' =>'varchar',//销售订单编号
  13. 'customerNo' =>'varchar',//客户编号
  14. 'viceCode' =>'varchar',//发票编号/回款编号
  15. 'order_total' =>'decimal',//订单总金额
  16. 'vice_total' =>'decimal',//发票面额/回款金额
  17. 'cancel_fee' =>'decimal',//核销金额
  18. 'status' =>'tinyint',//核销状态 1 审核中 2 核算完成3 退款退票
  19. 'is_del' =>'tinyint',//是否删除
  20. 'exam_remark' =>'varchar',//审核备注
  21. 'assoc_time' =>'varchar',//开票时间/回款时间
  22. 'updatetime' =>'datetime',//
  23. 'addtime' =>'datetime',//
  24. ];
  25. protected $createTime = 'addtime';
  26. protected $updateTime = 'updatetime';
  27. # 关联查询
  28. public static function CheckTrad($logNo,$status){
  29. $Ascc = self::where(["viceCode"=>$logNo,"is_del"=>0])->select();
  30. if($Ascc->isEmpty()) throw new \Exception("资金{$logNo}关联订单不存在");
  31. $Ascc->each(function($item) use($status){
  32. if($status==2) self::CheckFinsh($item);
  33. if($status==3||$status==5) self::CheckBack($item);
  34. if($status==4) self::CheckRefund($item);
  35. });
  36. }
  37. # 核销金额 付款完成
  38. public static function CheckFinsh($data){
  39. $qrd = QrdInfo::where(["sequenceNo"=>$data->orderCode,"is_del"=>0])->findOrEmpty();
  40. if($qrd->isEmpty()) throw new \Exception("关联订单{$data->orderCode}不存在");
  41. if($qrd->pay_fee<$data->cancel_fee) throw new \Exception("关联订单{$data->orderCode}核销金额大于支付金额");
  42. $qrd->pay_fee = $qrd->pay_fee-$data->cancel_fee;
  43. $qrd->apay_fee = $qrd->apay_fee+$data->cancel_fee;
  44. $qrd->pay_time = date("Y-m-d H:i:s");
  45. $qrd->pay_status = $qrd->wpay_fee==0 && $qrd->pay_fee==0?3:2;
  46. $qrdsav=$qrd->save();
  47. if(!$qrdsav) throw new \Exception("关联订单{$data->orderCode}更新失败");
  48. if($qrd->pay_status==3 && $qrd->is_comon==0 && $qrd->cxCode!=''){
  49. ComonOrder::where(['cxCode'=>$qrd['cxCode'],'status'=>-1])->save(['status'=>0]);
  50. }
  51. $data->status = 2;
  52. $data->assoc_time = date("Y-m-d H:i:s");
  53. $assup=$data->save();
  54. if(!$assup) throw new \Exception("关联信息{$data->AssocNo}更新失败");
  55. }
  56. # 付款流程解除
  57. public static function CheckBack($data){
  58. $qrd = QrdInfo::where(["sequenceNo"=>$data->orderCode,"is_del"=>0])->findOrEmpty();
  59. if($qrd->isEmpty()) throw new \Exception("关联订单{$data->orderCode}不存在");
  60. if($qrd->pay_fee<$data->cancel_fee) throw new \Exception("关联订单{$data->orderCode}核销金额大于支付金额");
  61. $qrd->wpay_fee = $qrd->wpay_fee+$data->cancel_fee;
  62. $qrd->pay_fee = $qrd->pay_fee-$data->cancel_fee;
  63. $qrd->pay_status = $qrd->apay_fee==0?1:2;
  64. $qrd->status =$qrd->pay_status==1 && $qrd->inv_status==1?0:1;
  65. $qrdsav=$qrd->save();
  66. if(!$qrdsav) throw new \Exception("关联订单{$data->orderCode}更新失败");
  67. if($qrd->pay_status==1 && $qrd->is_comon==0 && $qrd->cxCode!='') {
  68. ComonOrder::where(['cxCode'=>$qrd->cxCode,'status'=>0])->save(['status'=>-1]);
  69. }
  70. $data->status = 3;
  71. $assup=$data->save();
  72. if(!$assup) throw new \Exception("关联信息{$data->AssocNo}更新失败");
  73. }
  74. # 已付款数据退款
  75. public static function CheckRefund($data){
  76. $qrd = QrdInfo::where(["sequenceNo"=>$data->orderCode,"is_del"=>0])->findOrEmpty();
  77. if($qrd->isEmpty()) throw new \Exception("关联订单{$data->orderCode}不存在");
  78. if($qrd->apay_fee<$data->cancel_fee) throw new \Exception("关联订单{$data->orderCode}核销金额大于已支付金额");
  79. $qrd->apay_fee = $qrd->apay_fee-$data->cancel_fee;
  80. $qrd->wpay_fee = $qrd->wpay_fee+$data->cancel_fee;
  81. $qrd->pay_status = $qrd->apay_fee==0?1:2;
  82. $qrd->status =$qrd->pay_status==1 && $qrd->inv_status==1?0:1;
  83. $qrdsav=$qrd->save();
  84. if(!$qrdsav) throw new \Exception("关联订单{$data->orderCode}更新失败");
  85. if($qrd->pay_status==1 && $qrd->is_comon==0 && $qrd->cxCode!='') {
  86. ComonOrder::where(['cxCode'=>$qrd->cxCode,'status'=>0])->save(['status'=>-1]);
  87. }
  88. $data->status = 3;
  89. $assup=$data->save();
  90. if(!$assup) throw new \Exception("关联信息{$data->AssocNo}更新失败");
  91. }
  92. public static function CheckQrd($orderCode,&$data)
  93. {
  94. $asscoinfo =self::where(['orderCode'=>$orderCode,'status'=>[1,2],'is_del'=>0])->column
  95. ('viceCode','type');
  96. if(isset($asscoinfo[1])){
  97. $pool = InvoicePool::where('invNo',$asscoinfo[1])->findOrEmpty();
  98. $temp=['type'=>'结算销售单开票申请','username'=> $pool->apply_name,'orderCode'=>$asscoinfo[1],'companyName'=>$pool->inv_company];
  99. $data[]=$temp;
  100. }
  101. if(isset($asscoinfo[2])){
  102. $pool = TradePool::with(['company'])->where('logNo',$asscoinfo[2])->findOrEmpty();
  103. $temp=['type'=>'结算销售单资金认领','username'=> $pool->apply_name,'orderCode'=>$asscoinfo[2],'companyName'=>$pool->companyName];
  104. $data[]=$temp;
  105. }
  106. }
  107. }