123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 |
- <?php
- declare (strict_types = 1);
- namespace app\cxinv\listener;
- use app\cxinv\model\Assoc;
- use app\cxinv\model\InvoiceInfo;
- use app\cxinv\model\InvoicePay;
- use app\cxinv\model\InvoicePool;
- use app\cxinv\model\InvoiceTicket;
- use app\cxinv\model\Pay;
- use app\cxinv\model\PayInfo;
- use app\cxinv\model\PayInvoice;
- use app\cxinv\model\QrdInfo;
- use app\cxinv\model\TagLog;
- use think\helper\Str;
- class ComonQrd
- {
- /**
- * 事件监听处理
- * @return mixed
- */
- public function handle($event)
- {
- if($event['type']=='pay')$this->payComon($event);
- if($event['type']=='repay')$this->repayComon($event);
- if($event['type']=='inv'){
- $this->invComon($event);
- //$this->InvToPay($event);
- }
- if($event['type']=='rinv'){
- $this->returnInv($event);
- $this->InvToPay($event,false);
- }
- if($event['type']=='check')$this->checkPay($event);
- }
-
- public function InvToPay($data,$bool=True){
- $invinfo =InvoicePool::with(["poolInfo"])->where(['invNo'=>$data['invNo']])->findOrEmpty();
- if($invinfo->isEmpty())return;
- if($invinfo->buyer_code !='911100006337468570')return;
- if($bool){
- if($invinfo->status!=4 ||$invinfo->is_comon!=1 )return;
- $assoc_status=2;
- }else{
- $assoc_status=3;
- if(!in_array($invinfo->status,[5,6,7,8,9])||$invinfo->is_comon!=1 )return;
- }
- $assoc = Assoc::where(["viceCode"=>$data['invNo'],"status"=>$assoc_status,"is_del"=>0,"type"=>1])->select();
- if($assoc->isEmpty()) return;
- $orderArr = QrdInfo::whereIn("sequenceNo",array_column($assoc->toArray(),"orderCode"))->select();
- if($orderArr->isEmpty())return;
- $payArr=array_column($assoc->toArray(),"cancel_fee","orderCode");
- $tag=[];
- $qrdSave=[];
- IF($bool==false){
- $tagLog=TagLog::where(["code"=>$data['invNo'],"tag_id"=>107])->column("id","code");
- }
- foreach ($orderArr as $item){
- if(!isset($payArr[$item->sequenceNo]))continue;
- if($bool==false &&!isset($tagLog[$data['invNo']])) continue;
- $tag_temp=[
- "id"=>$bool?null:$tagLog[$data['invNo']],
- 'code'=>$item->sequenceNo,
- 'tag_id'=>107,//开票申请同步QR
- 'creater'=>'system',
- 'createrid'=>0,
- 'tag_fee'=>0,
- 'tag_img'=>'',
- "tag_code"=>$data['invNo'],
- 'status'=>$bool?1:0
- ];
- $qrd=[
- 'id'=>$item->id,
- 'wpay_fee'=>$item->wpay_fee,
- 'apay_fee'=>$item->apay_fee,
- 'pay_status'=>$item->pay_status,
- 'pay_tag_fee'=>$item->pay_tag_fee,
- 'pay_tag' => 1
- ];
- if($bool){
- if($item->wpay_fee>$payArr[$item->sequenceNo]){
- $tag_temp['tag_fee']= $payArr[$item->sequenceNo];
- $qrd['pay_tag_fee'] =$item->pay_tag_fee+$payArr[$item->sequenceNo];
- $qrd['wpay_fee'] = $item->wpay_fee-$payArr[$item->sequenceNo];
- $qrd['pay_status'] = ($qrd['wpay_fee']==0 &&$item->pay_fee==0) ? 3:($qrd['apay_fee']==0?1:2);
- }else{
- $tag_temp['tag_fee'] =$item->wpay_fee;
- $qrd['pay_tag_fee'] =$item->pay_tag_fee+ $item->wpay_fee;
- $qrd['wpay_fee'] = 0;
- $qrd['pay_status'] = ($qrd['wpay_fee']==0 &&$item->pay_fee==0) ? 3:($qrd['apay_fee']==0?1:2);
- }
- }else{
- if($item->pay_tag_fee==0) continue;
- $tag_temp['tag_fee']= $payArr[$item->sequenceNo];
- $tag_fee = ($item->pay_tag_fee>$payArr[$item->sequenceNo])?$payArr[$item->sequenceNo]:$item->pay_tag_fee;
- $qrd['pay_tag_fee']=$item->pay_tag_fee-$tag_fee;
- $qrd['wpay_fee'] = $item->wpay_fee+$tag_fee;
- $qrd['pay_status'] = ($qrd['apay_fee']==0 &&$item->pay_fee==0) ? 1:2;
- if($qrd['pay_tag_fee']==0) $qrd['pay_tag']=0;
- }
- $qrdSave[]=$qrd;
- $tag[]=$tag_temp;
- };
- (new \app\admin\model\QrdInfo)->saveAll($qrdSave);
- (new \app\admin\model\TagLog)->saveAll($tag);
- }
-
- public function payComon($data){
- $payinfo = Pay::where("payNo",$data['payNo'])->findOrEmpty();
- if($payinfo->isEmpty()) return;
- if($payinfo->is_comon!=1)return;
- if(!in_array($payinfo->status,[1,2]))return;
- $qrdNo = PayInfo::alias("a")->leftJoin("ComonOrder b","a.cgdNo=b.cgdNo")
- ->where(["payNo"=>$data['payNo'],"is_del"=>0,"a.status"=>1])->column("orderCode");
- $qrdinfo = QrdInfo::where([["sequenceNo","in",$qrdNo],["wpay_fee",">",0]])->select();
- if($qrdinfo->isEmpty())return;
- $this->CheckComonOrder($payinfo);
- $tagInfo = TagLog::where(["code"=>$qrdNo,"tag_id"=>108,"status"=>1])->column("id,code,tag_id,creater,createrid,tag_fee,tag_img,status","code");
- $tag=[];
- $qrdSave=[];
- $payfee=$data['pay_fee'];
- foreach ($qrdinfo as $item){
- if(isset($tagInfo[$item->sequenceNo])){
- $tag_temp = $tagInfo[$item->sequenceNo];
- }else{
- $tag_temp=[
- "id"=>null,
- 'code'=>$item->sequenceNo,
- 'tag_id'=>108,//对账付款同步至QR
- 'creater'=>'system',
- 'createrid'=>0,
- 'tag_fee'=>0,
- 'tag_img'=>'',
- 'status'=>1
- ];
- }
- $qrd=[
- "id"=>$item->id,
- "apay_fee"=>$item->apay_fee,
- "wpay_fee"=>$item->wpay_fee,
- "pay_status"=>$item->pay_status,
- "pay_tag_fee"=>0,
- "pay_tag" => 1
- ];
- if($item->wpay_fee>=$payfee){
- $tag_temp['tag_fee'] += $payfee;
- $qrd['pay_tag_fee'] = $item->pay_tag_fee+$payfee;
- $qrd['wpay_fee'] = ($item->wpay_fee>=$payfee)? $item->wpay_fee-$payfee:0;
- $qrd['pay_status'] = ($qrd['wpay_fee']==0 &&$item->pay_fee==0) ? 3:($item->apay_fee==0?1:2);
- $payfee=0;
- }else{
- $payfee=$payfee-$item->wpay_fee;
- $tag_temp['tag_fee'] +=$item->wpay_fee;
- $qrd['pay_tag_fee'] = $item->pay_tag_fee+$item->wpay_fee;
- $qrd['wpay_fee'] = 0;
- $qrd['pay_status'] = ($qrd['wpay_fee']==0 &&$item->pay_fee==0) ? 3:($item->apay_fee==0?1:2);
- }
- $qrdSave[]=$qrd;
- $tag[]=$tag_temp;
- if($payfee==0) break;
- }
- (new \app\admin\model\QrdInfo)->saveAll($qrdSave);
- (new \app\admin\model\TagLog)->saveAll($tag);
- }
- public function repayComon($data)
- {
- $payinfo = Pay::where('payNo', $data['payNo'])->findOrEmpty();
- if ($payinfo->isEmpty()) return;
- if ($payinfo->is_comon != 1) return;
- if (!in_array($payinfo->status, [1, 2])) return;
- $qrdNo = PayInfo::alias('a')->leftJoin('ComonOrder b', 'a.cgdNo=b.cgdNo')->where(['payNo' => $data['payNo'], 'is_del' => 0, 'a.status' => 1])->column('orderCode');
- $qrdinfo = QrdInfo::where([['sequenceNo', 'in', $qrdNo], ['pay_tag_fee', '>', 0], ['pay_tag', '=', 1]])
- ->select();
- if ($qrdinfo->isEmpty()) return;
- $this->CheckComonOrder($payinfo);
- $tagInfo = TagLog::where(['code'=>$qrdNo,'tag_id'=>108,'status'=>1])->column('id,code,tag_id,creater,createrid,tag_fee,tag_img,status','code');
- $qrdSave =$tagsave =$del= [];
- $payfee=$data['pay_fee'];
- foreach ($qrdinfo as $item) {
- if(!isset($tagInfo[$item->sequenceNo]))continue;
- $tag_temp = $tagInfo[$item->sequenceNo];
- $qrd = [
- 'id' => $item->id,
- 'apay_fee'=>$item->apay_fee,
- 'wpay_fee' => $item->wpay_fee,
- 'pay_status' => $item->pay_status,
- 'pay_tag_fee' => 0,
- 'pay_tag' => 1
- ];
- if ($item->pay_tag_fee >=$payfee) {
- $tag_temp['tag_fee']-= $payfee;
- $qrd['pay_tag_fee'] = $item->pay_tag_fee-$payfee;
- $qrd['pay_tag'] =$qrd['pay_tag_fee']>0?1:0;
- $qrd['wpay_fee'] = $item->wpay_fee + $payfee;
- $qrd['pay_status'] = ($qrd['wpay_fee'] == 0 && $item->pay_fee == 0) ? 3 : ($item->apay_fee == 0 ? 1 : 2);
- } else {
- $tag_temp['tag_fee']-= $item->pay_tag_fee;
- $qrd['pay_tag_fee'] = 0;
- $qrd['pay_tag'] = 0;
- $qrd['wpay_fee'] = $item->wpay_fee + $item->pay_tag_fee;
- $qrd['pay_status'] = ($qrd['wpay_fee'] == 0 && $item->pay_fee == 0) ? 3 : ($item->apay_fee== 0 ? 1 : 2);
- }
- $qrdSave[] = $qrd;
- if($tag_temp['tag_fee']<=0)$del[]=$tag_temp['id'];
- else $tagsave[]=$tag_temp;
- }
- (new \app\admin\model\QrdInfo)->saveAll($qrdSave);
- if(!empty($del))(new \app\admin\model\TagLog)->whereIn("id",$del)->delete();
- if(!empty($tagsave))(new \app\admin\model\TagLog)->saveAll($tagsave);
- }
- public function returnInv($data){
- $invinfo =InvoicePool::where(['invNo'=>$data['invNo'],"status"=>[5,6,7,8,9]])->findOrEmpty();
- if($invinfo->isEmpty())return;
- if($invinfo->is_comon!=1 )return;
- $invpay =InvoicePay::where(['invNo'=>$data['invNo']])->findOrEmpty();
- if($invpay->isEmpty())return;
- if($invpay->status!=1 )return;
- $payinfo = Pay::where('payNo',$invpay['payNo'])->findOrEmpty();
- if($payinfo->isEmpty()) return;
- if($payinfo->is_comon!=1)return;
- $payinvoice = PayInvoice::where(["hpNo"=>$invpay->hpNo,"status"=>4])->findOrEmpty();
- if($payinvoice->isEmpty())return;
- $payinvoice->status=10;
- $payinvoice->save();
- $payinfo->winv_fee = $payinfo->winv_fee+$payinvoice->inv_fee;
- $payinfo->ainv_fee = $payinfo->ainv_fee-$payinvoice->inv_fee;
- $payinfo->inv_status = ($payinfo->winv_fee==0 &&$payinfo->inv_fee==0)?3:($payinfo->ainv_fee==0?2:1);
- $this->CheckComonOrder($payinfo);
- $payinfo->save();
- $invpay->status =2;
- $invpay->save();
- }
- protected function checkPay($data){
- $payinfo = Pay::where('payNo',$data['payNo'])->findOrEmpty();
- if($payinfo->isEmpty()) return;
- if($payinfo->is_comon!=1)return;
- $this->CheckComonOrder($payinfo);
- }
- public function CheckComonOrder($payinfo){
- if($payinfo->status==3||$payinfo->status==4 ){
- PayInfo::alias('a')
- ->leftJoin('ComonOrder b','a.cgdNo=b.cgdNo')
- ->where(['payNo'=>$payinfo->payNo,'is_del'=>0,'a.status'=>0])
- ->update(['b.status'=>0]);
- }else{
- if($payinfo->inv_status==3 &&$payinfo->pay_status==3){
- PayInfo::alias('a')
- ->leftJoin('ComonOrder b','a.cgdNo=b.cgdNo')
- ->where(['payNo'=>$payinfo->payNo,'is_del'=>0,'a.status'=>1])
- ->update(["b.status"=>2]);
- }elseif($payinfo->inv_status!=3|| $payinfo->pay_status!=3){
- PayInfo::alias('a')
- ->leftJoin('ComonOrder b','a.cgdNo=b.cgdNo')
- ->where(['payNo'=>$payinfo->payNo,'is_del'=>0,'a.status'=>1])
- ->update(['b.status'=>1]);
- }
- }
- }
-
- //新开票完成 创建对应的标签;
- protected function invComon($data){
- $invinfo =InvoicePool::where(['invNo'=>$data['invNo']])->findOrEmpty();
- if($invinfo->isEmpty())return;
- if($invinfo->status!=4 ||$invinfo->is_comon!=1 )return;
- $invpay =InvoicePay::where(['invNo'=>$data['invNo']])->findOrEmpty();
- if($invpay->isEmpty())return;
- if($invpay->status!=0 )return;
- $payinfo = Pay::where('payNo',$invpay['payNo'])->findOrEmpty();
- if($payinfo->isEmpty()) return;
- if($payinfo->is_comon!=1)return;
- if(!in_array($payinfo->status,[1,2]))return;
- $ticket =InvoiceTicket::where(["invNo"=>$data['invNo'],"type"=>0])->findOrEmpty();
- if($ticket->isEmpty())return;
- $pay_fee=0;
- if($payinfo->winv_fee >=$invinfo->inv_value){
- $pay_fee = $invinfo->inv_value;
- }else{
- $pay_fee = $payinfo->winv_fee;
- }
- $payinvoice = [
- "payNo"=>$invpay['payNo'],
- "hpNo"=>makeNo("hp"),
- "apply_id"=>0,
- "apply_name"=>"system",
- "invType"=>$invinfo->open_type==3?1:3,
- "invoiceType"=>$invinfo->inv_type,
- "inv_fee"=>$pay_fee,
- "inv_subtotal_amount"=>$ticket->inv_subtotal,
- "inv_amount"=>$ticket->inv_total,
- "invStatus"=>1,
- "status"=>4,
- "check_time"=>date("Y-m-d H:i:s"),
- "open_time"=>$ticket->open_date,
- "invoiceNumber"=>$ticket->inv_number,
- "seller_check"=>1,
- "buyer_check"=>1,
- "invoiceCode"=>$ticket->inv_code,
- "checkNumber"=>$ticket->check_code,
- "checkApi"=>$data['checkApi']??0
- ];
- $payinfo->winv_fee = $payinfo->winv_fee-$pay_fee;
- $payinfo->ainv_fee = $payinfo->ainv_fee+$pay_fee;
- $payinfo->inv_status = ($payinfo->winv_fee==0 &&$payinfo->inv_fee==0)?3:($payinfo->ainv_fee==0?1:2);
- $this->CheckComonOrder($payinfo);
- $payinfo->save();
- PayInvoice::create($payinvoice);
- if(!empty($ticket->item)){
- $itemArr=[];
- foreach ($ticket->item as $item){
- $temp=[];
- $temp['name']=$item['XMMC'];
- $temp['unit']=$item['DW'];
- $temp['quantity']=$item['SPSL'];
- $temp['unit_price']=$item['DJ'];
- $temp['amount']=$item['JE'];
- $temp['tax']=$item['SE'];
- $temp['tax_rate']= Str::contains($item['SL'],"%")?$item['SL']:strval($item['SL']*100)."%" ;
- $itemArr[]=$temp;
- }
- }
- $info=[
- 'payNo' =>$invpay['payNo'] ,
- 'hpNo' =>$payinvoice['hpNo'] ,
- 'type' =>$invinfo['inv_type'] ,
- 'number' =>$ticket->inv_number ,
- 'code' =>$ticket->inv_code ,
- 'check_code' =>$ticket->check_code ,
- 'issue_date' =>date('Ymd' ,strtotime($ticket->open_date)) ,
- 'buyer_name' =>$ticket->buyer_title ,
- 'buyer_id' =>$ticket->buyer_id ,
- 'buyer_address' =>$ticket->buyer_addr . $ticket->buyer_mobile ,
- 'buyer_bank' =>$ticket->buyer_bank . $ticket->buyer_bankNo ,
- 'seller_name' =>$ticket->seller_title ,
- 'seller_id' =>$ticket->seller_id ,
- 'seller_address' =>$ticket->seller_addr . $ticket->seller_mobile ,
- 'seller_bank' =>$ticket->seller_bank . $ticket->seller_bankNo ,
- 'subtotal_amount'=>$ticket->inv_subtotal ,
- 'subtotal_tax' =>$ticket->tax_fee ,
- 'total' =>$ticket->inv_total ,
- 'item_list' =>$itemArr,
- 'receiver' =>$ticket->reciver ,
- 'issuer' =>$ticket->issuer ,
- 'reviewer' =>$ticket->reviewer ,
- 'change_field' =>'' ,
- "status"=>1,
- ];
- InvoiceInfo::create($info);
- // $invoiceItem = InvoiceItem::where(["invoiceCode"=>$data['invNo'],"status"=>[1,2]])->select();
- // if(!$invoiceItem->isEmpty()){
- // InvoiceItem::CopyItem($data['invNo'],$payinvoice['hpNo']);
- // }
- $invpay->hpNo = $payinvoice['hpNo'];
- $invpay->status =1;
- $invpay->save();
- }
- }
|