PayInvoice.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace app\cxinv\model;
  3. use think\facade\Log;use think\Model;
  4. class PayInvoice extends Base
  5. {
  6. //设置字段信息
  7. protected $schema = [
  8. 'id' =>'int',//
  9. 'payNo' =>'varchar',//对账编码
  10. 'hpNo' =>'varchar',//回票编号
  11. 'apply_id' =>'int',//申请人id
  12. 'apply_name' =>'varchar',//申请人名称
  13. 'invType' =>'tinyint',//发票申请类型 1 手工添加 2 ocr识别 3 金税扫描
  14. 'invoiceType' =>'varchar',//发票类型 special: 增值税专用发票normal: 增值税普通发票electronic: 增值税电子普通发票special_electronic: 增值税电子专用发票toll: 增值税电子普通发票(通行费)roll: 增值税普通发票(卷票),fully_digitalized_special_electronic:全电专用发票, fully_digitalized_normal_electronic:全电专用发票
  15. 'invName' =>'varchar',//
  16. 'inv_img' =>'varchar',//发票图片
  17. 'inv_fee' =>'decimal',//回票金额
  18. 'inv_subtotal_amount' =>'decimal',//票面税前金额
  19. 'inv_amount' =>'decimal',//票面合计金额
  20. 'invStatus' =>'tinyint',//[0=>"待验证","1"=>'正常',"2"=>'已作废',"3"=>'已红冲']
  21. 'status' =>'tinyint',// 1待系统验证 2 买方公司审核 3带买方公司认证 4 认证成功 5验证失败 6买方审核驳回 7 认证失败 8 回票流程终止 9 验证超次数 10 回票已退11 待关联订单 12 订单关联完毕待财务审核 13 订单关联完毕财务驳回 14 待确认订单
  22. 'remark' =>'varchar',//
  23. 'exam_remark' =>'varchar',//审核备注
  24. 'check_remark' =>'varchar',//发票验证备注
  25. 'tag_id' =>'int',//标签id
  26. 'check_time' =>'varchar',//回票时间
  27. 'open_time' =>'varchar',//开票时间
  28. 'invoiceNumber' =>'varchar',//发票号码
  29. 'seller_check' =>'tinyint',//发票卖方数据验证 0 待验证 1数据一致2数据不一致
  30. 'buyer_check' =>'tinyint',//发票买方数据验证0 待验证 1数据一致2数据不一致
  31. 'invoiceCode' =>'varchar',//发票代码
  32. 'checkNumber' =>'varchar',//效验码
  33. 'checkApi' =>'tinyint',//0 未确认 1 金税接口 2 华为云
  34. 'is_del' =>'tinyint',//是否删除 0 否1是
  35. 'addtime' =>'datetime',//
  36. 'updatetime' =>'datetime',//
  37. ];
  38. protected $createTime='addtime';
  39. protected $updateTime='updatetime';
  40. public function Items(){
  41. return $this->hasMany(InvoiceItem::class,'invoiceCode','hpNo');
  42. }
  43. public static function onAfterInsert(Model $model) : void{
  44. if ($model->invType==4){
  45. $ticket= InvoiceInfo::where("payNo",$model->payNo)->findOrEmpty();
  46. if (!$ticket->isEmpty() && !empty($ticket->item_list)){
  47. $bool = true;
  48. (new InvoiceItem)->saveAll(array_map(function ($item)use($model,&$bool) {
  49. if($item['amount']<0) $bool=false;
  50. return [
  51. 'invoiceCode'=>$model->hpNo,
  52. 'order_type'=>2,
  53. 'good_name'=>$item['name'],
  54. 'unit'=>$item['unit'],
  55. 'specinfo'=>$item['specification'],
  56. 'num'=>$item['quantity'],
  57. 'unit_price'=>$item['unit_price'],
  58. 'amount'=>$item['amount'],
  59. 'tax_amount'=>$item['tax'],
  60. 'tax'=>$item['tax_rate'],
  61. 'cat_code'=>$item['license_plate_number'],
  62. 'total_amount'=>bcadd($item['amount'],$item['tax'],2),
  63. 'balance_amount'=>bcadd($item['amount'],$item['tax'],2),
  64. ];
  65. },$ticket->item_list));
  66. $ticket->hpNo= $model->hpNo;
  67. $ticket->save();
  68. if($bool==false){
  69. $model->status=14;
  70. $model->save();
  71. }
  72. }
  73. }
  74. }
  75. public static function onAfterUpdate(Model $model) : void{
  76. Log::info('PayInv回票状态变更onAfterUpdate:model'.json_encode($model->toArray()));
  77. if ($model->invType==4){
  78. $change = $model->getChangedData();
  79. if (isset($change['status'])){
  80. Log::info("PayInv回票状态变更onAfterUpdate:change".json_encode($change));
  81. $invocie = Invoice::where("InvCode",$model->payNo)->findOrEmpty();
  82. if($change['status']==4){
  83. if (!$invocie->isEmpty()){
  84. $invocie->status=4;
  85. $invocie->save();
  86. }
  87. $itemid= InvoiceItem::where(['invoiceCode'=>$model->hpNo,'status'=>[1,2]])->column('id');
  88. if(!empty($itemid)){
  89. $invo= InvoiceOrder::whereIn('itemId',$itemid)->where(['status'=>[1,2]])->column('code,balance_amount,total_amount');
  90. if(!empty($invo)){
  91. foreach ($invo as $key=>$val){
  92. InvoiceOrder::orderFinish($val['code'],bcsub($val['total_amount'],$val['balance_amount'],2));
  93. }
  94. }
  95. }
  96. }
  97. if($change['status']==10){
  98. Log::info("回票终止,退款开始".$model->hpNo);
  99. InvoiceItem::refund($model->hpNo);
  100. if (!$invocie->isEmpty()){
  101. $invocie->status=2;
  102. $invocie->save();
  103. }
  104. }
  105. if (in_array($change['status'],[5,6,7,8,13])){
  106. InvoiceItem::rmInvoice($model->hpNo);
  107. if (!$invocie->isEmpty()){
  108. $invocie->status=2;
  109. $invocie->save();
  110. }
  111. }
  112. }
  113. }
  114. }
  115. }