ComonQrd.php 15 KB

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