ComonQrd.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\cxinv\listener;
  4. use app\cxinv\model\Assoc;
  5. use app\cxinv\model\InvoiceInfo;
  6. use app\cxinv\model\InvoicePay;
  7. use app\cxinv\model\InvoicePool;
  8. use app\cxinv\model\InvoiceTicket;
  9. use app\cxinv\model\Pay;
  10. use app\cxinv\model\PayInfo;
  11. use app\cxinv\model\PayInvoice;
  12. use app\cxinv\model\QrdInfo;
  13. use app\cxinv\model\TagLog;
  14. use think\helper\Str;
  15. class ComonQrd
  16. {
  17. /**
  18. * 事件监听处理
  19. * @return mixed
  20. */
  21. public function handle($event)
  22. {
  23. if($event['type']=='pay')$this->payComon($event);
  24. if($event['type']=='repay')$this->repayComon($event);
  25. if($event['type']=='inv'){
  26. $this->invComon($event);
  27. $this->InvToPay($event);
  28. }
  29. if($event['type']=='rinv'){
  30. $this->returnInv($event);
  31. $this->InvToPay($event,false);
  32. }
  33. if($event['type']=='check')$this->checkPay($event);
  34. }
  35. public function InvToPay($data,$bool=True){
  36. $invinfo =InvoicePool::with(["poolInfo"])->where(['invNo'=>$data['invNo']])->findOrEmpty();
  37. if($invinfo->isEmpty())return;
  38. if($invinfo->buyer_code !=='911100006337468570')return;
  39. if($bool){
  40. if($invinfo->status!=4 ||$invinfo->is_comon!=1 )return;
  41. $assoc_status=2;
  42. }else{
  43. $assoc_status=3;
  44. if(in_array($invinfo->status,[5,6,7,8,9])||$invinfo->is_comon!=1 )return;
  45. }
  46. $assoc = Assoc::where(["viceCode"=>$data['invNo'],"status"=>$assoc_status,"is_del"=>0,"type"=>1])->select();
  47. if($assoc->isEmpty()) return;
  48. $orderArr = QrdInfo::whereIn("sequenceNo",array_column($assoc->toArray(),"orderCode"))->select();
  49. if($orderArr->isEmpty())return;
  50. $payArr=array_column($assoc->toArray(),"cancel_fee","orderCode");
  51. $tag=[];
  52. $qrdSave=[];
  53. foreach ($orderArr as $item){
  54. if(!isset($payArr[$item->sequenceNo]))continue;
  55. $tag_temp=[
  56. 'code'=>$item->sequenceNo,
  57. 'tag_id'=>107,//开票申请同步QR
  58. 'creater'=>'system',
  59. 'createrid'=>0,
  60. 'tag_fee'=>0,
  61. 'tag_img'=>'',
  62. "tag_code"=>$data['invNo'],
  63. 'status'=>$bool?1:0
  64. ];
  65. $qrd=[
  66. 'id'=>$item->id,
  67. 'wpay_fee'=>$item->wpay_fee,
  68. 'pay_status'=>$item->pay_status,
  69. 'pay_tag_fee'=>$item->pay_tag_fee,
  70. 'pay_tag' => 1
  71. ];
  72. if($bool){
  73. if($item->wpay_fee>$payArr[$item->sequenceNo]){
  74. $tag_temp['tag_fee']= $payArr[$item->sequenceNo];
  75. $qrd['pay_tag_fee'] =$item->pay_tag_fee+$payArr[$item->sequenceNo];
  76. $qrd['wpay_fee'] = $item->wpay_fee-$payArr[$item->sequenceNo];
  77. $qrd['pay_status'] = ($qrd['wpay_fee']==0 &&$item->pay_fee==0) ? 3:($qrd['apay_fee']==0?1:2);
  78. }else{
  79. $tag_temp['tag_fee'] =$item->wpay_fee;
  80. $qrd['pay_tag_fee'] =$item->pay_tag_fee+ $item->wpay_fee;
  81. $qrd['wpay_fee'] = 0;
  82. $qrd['pay_status'] = ($qrd['wpay_fee']==0 &&$item->pay_fee==0) ? 3:($qrd['apay_fee']==0?1:2);
  83. }
  84. }else{
  85. $tag_temp['tag_fee']= $payArr[$item->sequenceNo];
  86. $tag_fee = ($item->pay_tag_fee>$payArr[$item->sequenceNo])?$payArr[$item->sequenceNo]:$item->pay_tag_fee;
  87. $qrd['pay_tag_fee']=$item->pay_tag_fee-$tag_fee;
  88. $qrd['wpay_fee'] = $item->wpay_fee+$tag_fee;
  89. $qrd['pay_status'] = ($qrd['wpay_fee']==0 &&$item->pay_fee==0) ? 3:($qrd['apay_fee']==0?2:1);
  90. }
  91. $qrdSave[]=$qrd;
  92. $tag[]=$tag_temp;
  93. };
  94. (new \app\admin\model\QrdInfo)->saveAll($qrdSave);
  95. (new \app\admin\model\TagLog)->saveAll($tag);
  96. }
  97. public function payComon($data){
  98. $payinfo = Pay::where("payNo",$data['payNo'])->findOrEmpty();
  99. if($payinfo->isEmpty()) return;
  100. if($payinfo->is_comon!=1)return;
  101. if(!in_array($payinfo->status,[1,2]))return;
  102. $qrdNo = PayInfo::alias("a")->leftJoin("ComonOrder b","a.cgdNo=b.cgdNo")
  103. ->where(["payNo"=>$data['payNo'],"is_del"=>0,"a.status"=>1])->column("orderCode");
  104. $qrdinfo = QrdInfo::where([["sequenceNo","in",$qrdNo],["wpay_fee",">",0]])->select();
  105. if($qrdinfo->isEmpty())return;
  106. $this->CheckComonOrder($payinfo);
  107. $tagInfo = TagLog::where(["code"=>$qrdNo,"tag_id"=>108,"status"=>1])->column("id,code,tag_id,creater,createrid,tag_fee,tag_img,status","code");
  108. $tag=[];
  109. $qrdSave=[];
  110. $payfee=$data['pay_fee'];
  111. foreach ($qrdinfo as $item){
  112. if(isset($tagInfo[$item->sequenceNo])){
  113. $tag_temp = $tagInfo[$item->sequenceNo];
  114. }else{
  115. $tag_temp=[
  116. "id"=>null,
  117. 'code'=>$item->sequenceNo,
  118. 'tag_id'=>108,//对账付款同步至QR
  119. 'creater'=>'system',
  120. 'createrid'=>0,
  121. 'tag_fee'=>0,
  122. 'tag_img'=>'',
  123. 'status'=>1
  124. ];
  125. }
  126. $qrd=[
  127. "id"=>$item->id,
  128. "wpay_fee"=>$item->wpay_fee,
  129. "pay_status"=>$item->pay_status,
  130. "pay_tag_fee"=>0,
  131. "pay_tag" => 1
  132. ];
  133. if($item->wpay_fee>=$payfee){
  134. $tag_temp['tag_fee'] += $payfee;
  135. $qrd['pay_tag_fee'] = $item->pay_tag_fee+$payfee;
  136. $qrd['wpay_fee'] = ($item->wpay_fee>=$payfee)? $item->wpay_fee-$payfee:0;
  137. $qrd['pay_status'] = ($qrd['wpay_fee']==0 &&$item->pay_fee==0) ? 3:($item->apay_fee==0?1:2);
  138. $payfee=0;
  139. }else{
  140. $payfee=$payfee-$item->wpay_fee;
  141. $tag_temp['tag_fee'] +=$item->wpay_fee;
  142. $qrd['pay_tag_fee'] = $item->pay_tag_fee+$item->wpay_fee;
  143. $qrd['wpay_fee'] = 0;
  144. $qrd['pay_status'] = ($qrd['wpay_fee']==0 &&$item->pay_fee==0) ? 3:($item->apay_fee==0?1:2);
  145. }
  146. $qrdSave[]=$qrd;
  147. $tag[]=$tag_temp;
  148. if($payfee==0) break;
  149. }
  150. (new \app\admin\model\QrdInfo)->saveAll($qrdSave);
  151. (new \app\admin\model\TagLog)->saveAll($tag);
  152. }
  153. public function repayComon($data)
  154. {
  155. $payinfo = Pay::where('payNo', $data['payNo'])->findOrEmpty();
  156. if ($payinfo->isEmpty()) return;
  157. if ($payinfo->is_comon != 1) return;
  158. if (!in_array($payinfo->status, [1, 2])) return;
  159. $qrdNo = PayInfo::alias('a')->leftJoin('ComonOrder b', 'a.cgdNo=b.cgdNo')->where(['payNo' => $data['payNo'], 'is_del' => 0, 'a.status' => 1])->column('orderCode');
  160. $qrdinfo = QrdInfo::where([['sequenceNo', 'in', $qrdNo], ['pay_tag_fee', '>', 0], ['pay_tag', '=', 1]])
  161. ->select();
  162. if ($qrdinfo->isEmpty()) return;
  163. $this->CheckComonOrder($payinfo);
  164. $tagInfo = TagLog::where(['code'=>$qrdNo,'tag_id'=>108,'status'=>1])->column('id,code,tag_id,creater,createrid,tag_fee,tag_img,status','code');
  165. $qrdSave =$tagsave =$del= [];
  166. $payfee=$data['pay_fee'];
  167. foreach ($qrdinfo as $item) {
  168. if(!isset($tagInfo[$item->sequenceNo]))continue;
  169. $tag_temp = $tagInfo[$item->sequenceNo];
  170. $qrd = [
  171. 'id' => $item->id,
  172. 'wpay_fee' => $item->wpay_fee,
  173. 'pay_status' => $item->pay_status,
  174. 'pay_tag_fee' => 0,
  175. 'pay_tag' => 1
  176. ];
  177. if ($item->pay_tag_fee >=$payfee) {
  178. $tag_temp['tag_fee']-= $payfee;
  179. $qrd['pay_tag_fee'] = $item->pay_tag_fee-$payfee;
  180. $qrd['pay_tag'] =$qrd['pay_tag_fee']>0?1:0;
  181. $qrd['wpay_fee'] = $item->wpay_fee + $payfee;
  182. $qrd['pay_status'] = ($qrd['wpay_fee'] == 0 && $item->pay_fee == 0) ? 3 : ($item->apay_fee == 0 ? 1 : 2);
  183. } else {
  184. $tag_temp['tag_fee']-= $item->pay_tag_fee;
  185. $qrd['pay_tag_fee'] = 0;
  186. $qrd['pay_tag'] = 0;
  187. $qrd['wpay_fee'] = $item->wpay_fee + $item->pay_tag_fee;
  188. $qrd['pay_status'] = ($qrd['wpay_fee'] == 0 && $item->pay_fee == 0) ? 3 : ($item->apay_fee== 0 ? 1 : 2);
  189. }
  190. $qrdSave[] = $qrd;
  191. if($tag_temp['tag_fee']<=0)$del[]=$tag_temp['id'];
  192. else $tagsave[]=$tag_temp;
  193. }
  194. (new \app\admin\model\QrdInfo)->saveAll($qrdSave);
  195. if(!empty($del))(new \app\admin\model\TagLog)->whereIn("id",$del)->delete();
  196. if(!empty($tagsave))(new \app\admin\model\TagLog)->saveAll($tagsave);
  197. }
  198. public function returnInv($data){
  199. $invinfo =InvoicePool::where(['invNo'=>$data['invNo'],"status"=>[5,6,7,8,9]])->findOrEmpty();
  200. if($invinfo->isEmpty())return;
  201. if($invinfo->is_comon!=1 )return;
  202. $invpay =InvoicePay::where(['invNo'=>$data['invNo']])->findOrEmpty();
  203. if($invpay->isEmpty())return;
  204. if($invpay->status!=1 )return;
  205. $payinfo = Pay::where('payNo',$invpay['payNo'])->findOrEmpty();
  206. if($payinfo->isEmpty()) return;
  207. if($payinfo->is_comon!=1)return;
  208. $payinvoice = PayInvoice::where(["hpNo"=>$invpay->hpNo,"status"=>4])->findOrEmpty();
  209. if($payinvoice->isEmpty())return;
  210. $payinvoice->status=10;
  211. $payinvoice->save();
  212. $payinfo->winv_fee = $payinfo->winv_fee+$payinvoice->inv_fee;
  213. $payinfo->ainv_fee = $payinfo->ainv_fee-$payinvoice->inv_fee;
  214. $payinfo->inv_status = ($payinfo->winv_fee==0 &&$payinfo->inv_fee==0)?3:($payinfo->ainv_fee==0?2:1);
  215. $this->CheckComonOrder($payinfo);
  216. $payinfo->save();
  217. $invpay->status =2;
  218. $invpay->save();
  219. }
  220. protected function checkPay($data){
  221. $payinfo = Pay::where('payNo',$data['payNo'])->findOrEmpty();
  222. if($payinfo->isEmpty()) return;
  223. if($payinfo->is_comon!=1)return;
  224. $this->CheckComonOrder($payinfo);
  225. }
  226. public function CheckComonOrder($payinfo){
  227. if($payinfo->status==3||$payinfo->status==4 ){
  228. PayInfo::alias('a')
  229. ->leftJoin('ComonOrder b','a.cgdNo=b.cgdNo')
  230. ->where(['payNo'=>$payinfo->payNo,'is_del'=>0,'a.status'=>0])
  231. ->update(['b.status'=>0]);
  232. }else{
  233. if($payinfo->inv_status==3 &&$payinfo->pay_status==3){
  234. PayInfo::alias('a')
  235. ->leftJoin('ComonOrder b','a.cgdNo=b.cgdNo')
  236. ->where(['payNo'=>$payinfo->payNo,'is_del'=>0,'a.status'=>1])
  237. ->update(["b.status"=>2]);
  238. }elseif($payinfo->inv_status!=3|| $payinfo->pay_status!=3){
  239. PayInfo::alias('a')
  240. ->leftJoin('ComonOrder b','a.cgdNo=b.cgdNo')
  241. ->where(['payNo'=>$payinfo->payNo,'is_del'=>0,'a.status'=>1])
  242. ->update(['b.status'=>1]);
  243. }
  244. }
  245. }
  246. //新开票完成 创建对应的标签;
  247. protected function invComon($data){
  248. $invinfo =InvoicePool::where(['invNo'=>$data['invNo']])->findOrEmpty();
  249. if($invinfo->isEmpty())return;
  250. if($invinfo->status!=4 ||$invinfo->is_comon!=1 )return;
  251. $invpay =InvoicePay::where(['invNo'=>$data['invNo']])->findOrEmpty();
  252. if($invpay->isEmpty())return;
  253. if($invpay->status!=0 )return;
  254. $payinfo = Pay::where('payNo',$invpay['payNo'])->findOrEmpty();
  255. if($payinfo->isEmpty()) return;
  256. if($payinfo->is_comon!=1)return;
  257. if(!in_array($payinfo->status,[1,2]))return;
  258. $ticket =InvoiceTicket::where(["invNo"=>$data['invNo'],"type"=>0])->findOrEmpty();
  259. if($ticket->isEmpty())return;
  260. $pay_fee=0;
  261. if($payinfo->winv_fee >=$invinfo->inv_value){
  262. $pay_fee = $invinfo->inv_value;
  263. }else{
  264. $pay_fee = $payinfo->winv_fee;
  265. }
  266. $payinvoice = [
  267. "payNo"=>$invpay['payNo'],
  268. "hpNo"=>makeNo("hp"),
  269. "apply_id"=>0,
  270. "apply_name"=>"system",
  271. "invType"=>$invinfo->open_type==3?1:3,
  272. "invoiceType"=>$invinfo->inv_type,
  273. "inv_fee"=>$pay_fee,
  274. "inv_subtotal_amount"=>$ticket->inv_subtotal,
  275. "inv_amount"=>$ticket->inv_total,
  276. "invStatus"=>1,
  277. "status"=>4,
  278. "check_time"=>date("Y-m-d H:i:s"),
  279. "open_time"=>$ticket->open_date,
  280. "invoiceNumber"=>$ticket->inv_number,
  281. "seller_check"=>1,
  282. "buyer_check"=>1,
  283. "invoiceCode"=>$ticket->inv_code,
  284. "checkNumber"=>$ticket->check_code,
  285. "checkApi"=>$data['checkApi']??0
  286. ];
  287. $payinfo->winv_fee = $payinfo->winv_fee-$pay_fee;
  288. $payinfo->ainv_fee = $payinfo->ainv_fee+$pay_fee;
  289. $payinfo->inv_status = ($payinfo->winv_fee==0 &&$payinfo->inv_fee==0)?3:($payinfo->ainv_fee==0?1:2);
  290. $this->CheckComonOrder($payinfo);
  291. $payinfo->save();
  292. PayInvoice::create($payinvoice);
  293. if(!empty($ticket->item)){
  294. $itemArr=[];
  295. foreach ($ticket->item as $item){
  296. $temp=[];
  297. $temp['name']=$item['XMMC'];
  298. $temp['unit']=$item['DW'];
  299. $temp['quantity']=$item['SPSL'];
  300. $temp['unit_price']=$item['DJ'];
  301. $temp['amount']=$item['JE'];
  302. $temp['tax']=$item['SE'];
  303. $temp['tax_rate']= Str::contains($item['SL'],"%")?$item['SL']:strval($item['SL']*100)."%" ;
  304. $itemArr[]=$temp;
  305. }
  306. }
  307. $info=[
  308. 'payNo' =>$invpay['payNo'] ,
  309. 'hpNo' =>$payinvoice['hpNo'] ,
  310. 'type' =>$invinfo['inv_type'] ,
  311. 'number' =>$ticket->inv_number ,
  312. 'code' =>$ticket->inv_code ,
  313. 'check_code' =>$ticket->check_code ,
  314. 'issue_date' =>date('Ymd' ,strtotime($ticket->open_date)) ,
  315. 'buyer_name' =>$ticket->buyer_title ,
  316. 'buyer_id' =>$ticket->buyer_id ,
  317. 'buyer_address' =>$ticket->buyer_addr . $ticket->buyer_mobile ,
  318. 'buyer_bank' =>$ticket->buyer_bank . $ticket->buyer_bankNo ,
  319. 'seller_name' =>$ticket->seller_title ,
  320. 'seller_id' =>$ticket->seller_id ,
  321. 'seller_address' =>$ticket->seller_addr . $ticket->seller_mobile ,
  322. 'seller_bank' =>$ticket->seller_bank . $ticket->seller_bankNo ,
  323. 'subtotal_amount'=>$ticket->inv_subtotal ,
  324. 'subtotal_tax' =>$ticket->tax_fee ,
  325. 'total' =>$ticket->inv_total ,
  326. 'item_list' =>$itemArr,
  327. 'receiver' =>$ticket->reciver ,
  328. 'issuer' =>$ticket->issuer ,
  329. 'reviewer' =>$ticket->reviewer ,
  330. 'change_field' =>'' ,
  331. "status"=>1,
  332. ];
  333. InvoiceInfo::create($info);
  334. // $invoiceItem = InvoiceItem::where(["invoiceCode"=>$data['invNo'],"status"=>[1,2]])->select();
  335. // if(!$invoiceItem->isEmpty()){
  336. // InvoiceItem::CopyItem($data['invNo'],$payinvoice['hpNo']);
  337. // }
  338. $invpay->hpNo = $payinvoice['hpNo'];
  339. $invpay->status =1;
  340. $invpay->save();
  341. }
  342. }