ComonQrd.php 15 KB

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