123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- <?php
- namespace app\cxinv\model;
- use think\facade\Log;use think\Model;
- class PayInvoice extends Base
- {
- //设置字段信息
- protected $schema = [
- 'id' =>'int',//
- 'payNo' =>'varchar',//对账编码
- 'hpNo' =>'varchar',//回票编号
- 'apply_id' =>'int',//申请人id
- 'apply_name' =>'varchar',//申请人名称
- 'invType' =>'tinyint',//发票申请类型 1 手工添加 2 ocr识别 3 金税扫描
- 'invoiceType' =>'varchar',//发票类型 special: 增值税专用发票normal: 增值税普通发票electronic: 增值税电子普通发票special_electronic: 增值税电子专用发票toll: 增值税电子普通发票(通行费)roll: 增值税普通发票(卷票),fully_digitalized_special_electronic:全电专用发票, fully_digitalized_normal_electronic:全电专用发票
- 'invName' =>'varchar',//
- 'inv_img' =>'varchar',//发票图片
- 'inv_fee' =>'decimal',//回票金额
- 'inv_subtotal_amount' =>'decimal',//票面税前金额
- 'inv_amount' =>'decimal',//票面合计金额
- 'invStatus' =>'tinyint',//[0=>"待验证","1"=>'正常',"2"=>'已作废',"3"=>'已红冲']
- 'status' =>'tinyint',// 1待系统验证 2 买方公司审核 3带买方公司认证 4 认证成功 5验证失败 6买方审核驳回 7 认证失败 8 回票流程终止 9 验证超次数 10 回票已退11 待关联订单 12 订单关联完毕待财务审核 13 订单关联完毕财务驳回 14 待确认订单
- 'remark' =>'varchar',//
- 'exam_remark' =>'varchar',//审核备注
- 'check_remark' =>'varchar',//发票验证备注
- 'tag_id' =>'int',//标签id
- 'check_time' =>'varchar',//回票时间
- 'open_time' =>'varchar',//开票时间
- 'invoiceNumber' =>'varchar',//发票号码
- 'seller_check' =>'tinyint',//发票卖方数据验证 0 待验证 1数据一致2数据不一致
- 'buyer_check' =>'tinyint',//发票买方数据验证0 待验证 1数据一致2数据不一致
- 'invoiceCode' =>'varchar',//发票代码
- 'checkNumber' =>'varchar',//效验码
- 'checkApi' =>'tinyint',//0 未确认 1 金税接口 2 华为云
- 'is_del' =>'tinyint',//是否删除 0 否1是
- 'addtime' =>'datetime',//
- 'updatetime' =>'datetime',//
- ];
- protected $createTime='addtime';
- protected $updateTime='updatetime';
- public function Items(){
- return $this->hasMany(InvoiceItem::class,'invoiceCode','hpNo');
- }
- public static function onAfterInsert(Model $model) : void{
- if ($model->invType==4){
- $ticket= InvoiceInfo::where("payNo",$model->payNo)->findOrEmpty();
- if (!$ticket->isEmpty() && !empty($ticket->item_list)){
- $bool = true;
- (new InvoiceItem)->saveAll(array_map(function ($item)use($model,&$bool) {
- if($item['amount']<0) $bool=false;
- return [
- 'invoiceCode'=>$model->hpNo,
- 'order_type'=>2,
- 'good_name'=>$item['name'],
- 'unit'=>$item['unit'],
- 'specinfo'=>$item['specification'],
- 'num'=>$item['quantity'],
- 'unit_price'=>$item['unit_price'],
- 'amount'=>$item['amount'],
- 'tax_amount'=>$item['tax'],
- 'tax'=>$item['tax_rate'],
- 'cat_code'=>$item['license_plate_number'],
- 'total_amount'=>bcadd($item['amount'],$item['tax'],2),
- 'balance_amount'=>bcadd($item['amount'],$item['tax'],2),
- ];
- },$ticket->item_list));
- $ticket->hpNo= $model->hpNo;
- $ticket->save();
- if($bool==false){
- $model->status=14;
- $model->save();
- }
- }
- }
- }
- public static function onAfterUpdate(Model $model) : void{
- Log::info('PayInv回票状态变更onAfterUpdate:model'.json_encode($model->toArray()));
- if ($model->invType==4){
- $change = $model->getChangedData();
- if (isset($change['status'])){
- Log::info("PayInv回票状态变更onAfterUpdate:change".json_encode($change));
- $invocie = Invoice::where("InvCode",$model->payNo)->findOrEmpty();
- if($change['status']==4){
- if (!$invocie->isEmpty()){
- $invocie->status=4;
- $invocie->save();
- }
- $itemid= InvoiceItem::where(['invoiceCode'=>$model->hpNo,'status'=>[1,2]])->column('id');
- if(!empty($itemid)){
- $invo= InvoiceOrder::whereIn('itemId',$itemid)->where(['status'=>[1,2]])->column('code,balance_amount,total_amount');
- if(!empty($invo)){
- foreach ($invo as $key=>$val){
- InvoiceOrder::orderFinish($val['code'],bcsub($val['total_amount'],$val['balance_amount'],2));
- }
- }
- }
- }
- if($change['status']==10){
- Log::info("回票终止,退款开始".$model->hpNo);
- InvoiceItem::refund($model->hpNo);
- if (!$invocie->isEmpty()){
- $invocie->status=2;
- $invocie->save();
- }
- }
- if (in_array($change['status'],[5,6,7,8,13])){
- InvoiceItem::rmInvoice($model->hpNo);
- if (!$invocie->isEmpty()){
- $invocie->status=2;
- $invocie->save();
- }
- }
- }
- }
- }
- }
|