downredinvres.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\admin\command;
  4. use think\console\Command;
  5. use think\console\Input;
  6. use think\facade\Config;
  7. use think\console\input\Argument;
  8. use think\console\input\Option;
  9. use think\console\Output;
  10. use think\facade\Cache;
  11. use think\facade\Db;
  12. class downredinvres extends Command
  13. {
  14. protected function configure()
  15. {
  16. // 指令配置
  17. $this->setName('downredinvres')
  18. ->setDescription('the downredinvres command');
  19. }
  20. protected function execute(Input $input, Output $output)
  21. {
  22. $GetOrderInfoAndInvoiceInfo= Cache::store("redis")->get("downredinvres");
  23. if($GetOrderInfoAndInvoiceInfo==1) return '';
  24. Cache::store("redis")->set("downredinvres",1,160);
  25. Db::startTrans();
  26. $date= date("Y-m-d H:i:s");
  27. $row =Db::name("invoice_red")->where(["status"=>1,"is_del"=>0])->where("updatetime","<=",date("Y-m-d H:i:s"))
  28. ->lock(true)->findOrEmpty();
  29. if(empty($row)) {
  30. Db::rollback();
  31. return '';
  32. }
  33. try{
  34. $data=["updatetime"=>date("Y-m-d H:i:s",time()+600)];
  35. $opendatetime =date("Ymd",strtotime($row['addtime']));
  36. $ebddatetime =date("Ymd",strtotime("+2 days",strtotime($row['addtime'])));
  37. $invoiceConf=Config::get('invoice');
  38. if(!isset($invoiceConf[$row['XHFSBH']])){
  39. $data['remark']="金税配置不存在";
  40. Db::name('invoice_red')->where(['id'=>$row['id']])->update($data);
  41. Db::commit();
  42. return '';
  43. }
  44. $invoice = $invoiceConf[$row['XHFSBH']];
  45. $Tax =new \TaxInvoice($invoice['appKey'],$invoice['appSecret'],$invoice['entCode']);
  46. if($row['addtime']==$row['updatetime']){
  47. $pch=randomkeys(8).md5(date("YmdHis").$row['id']);
  48. $data['SQBSCQQPCH']=$pch;
  49. $row['SQBSCQQPCH'] =$pch;
  50. }
  51. $result=$Tax->DownloadRedInvoiceApplicationResult($row['SQBSCQQPCH'],$row['XHFSBH'],$row['FPLXDM'],$opendatetime,$opendatetime);
  52. if(isset($result['ZTDM']) && $result['ZTDM']=='060000'){
  53. if(isset($result['HZSQDXZZXX']) && !empty($result['HZSQDXZZXX'])){
  54. $limimt=[];
  55. foreach ($result['HZSQDXZZXX'] as $item){
  56. $resultChild=$item['HZSQDXZTXX'];
  57. $temp=[];
  58. if(isset($limimt[$resultChild['YFPHM']]) &&$limimt[$resultChild['YFPHM']]['status']==2 ) continue;
  59. $temp['YFPDM']=$resultChild['YFPDM'];
  60. $temp['YFPHM']=$resultChild['YFPHM'];
  61. $temp['XXBBH']=$resultChild['XXBBH'];
  62. $temp['remark']=$resultChild['ZTXX'];
  63. $temp['updatetime']=date("Y-m-d H:i:s");
  64. if($resultChild['ZTDM']=='060000'){
  65. $temp['status']=2;
  66. }elseif($resultChild['ZTDM']=='069999'){
  67. $temp['status']=5;
  68. }else{
  69. $temp['status']=1;
  70. }
  71. $limimt[$resultChild['YFPHM']] = $temp;
  72. }
  73. if(empty($limimt)){
  74. $data['remark']=$result['ZTXX'];
  75. Db::name('invoice_red')->where($row)->update($data);
  76. }else{
  77. $this->checkData($limimt);
  78. }
  79. }
  80. }else{
  81. $data['remark']=$result['ZTXX'];
  82. if(!isset($result['ZTDM']) || $result['ZTDM']!='060111'){
  83. $data['status']=5;
  84. $this->updateReturn($row['returnCode'],$result['ZTXX']);
  85. }
  86. Db::name('invoice_red')->where(['id'=>$row['id']])->update($data);
  87. }
  88. Db::commit();
  89. $output->writeln("[$date] ".$row['invRed'].'发票申请表获取成功');
  90. }catch (\Exception $e){
  91. Db::rollback();
  92. $output->writeln("[$date] ".$row['invRed'].$e->getMessage().$e->getLine());
  93. }
  94. }
  95. //立即提交红冲开票
  96. public function AddRed($fpdm,$fphm){
  97. $cod=["YFPDM"=>$fpdm,"YFPHM"=>$fphm,"status"=>2,"is_del"=>0];
  98. $red=Db::name("invoice_red")->where($cod)->findOrEmpty();
  99. if(empty($red))throw new \Exception("未找到发票红冲申请信息");
  100. $where=["invNo"=>$red["invNo"],"type"=>0,"status"=>1];
  101. $ticket =Db::name("invoice_ticket")->where($where)->findOrEmpty();
  102. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$red['invNo'],"is_del"=>0,"status"=>4])->findOrEmpty();
  103. if(empty($ticket) || empty($invinfo)){
  104. Db::name("invoice_red")->where($red)->update(["status"=>6,"updatetime"=>date("Y-m-d H:i:s"),"remark"=>"申请成功,原票信息本地未找到"]);
  105. Db::name("invoice_return")->where(["returnCode"=>$red['returnCode']])->update(["status"=>4,"updatetime"=>date("Y-m-d H:i:s"),"remark"=>"申请成功,原票信息本地未找到"]);
  106. return ;
  107. }
  108. $data=[ "invNo"=>$red['invRed'],
  109. "KPLX"=>"1",
  110. "XHFSBH"=>$ticket['seller_id'],
  111. "XHFMC"=>$ticket['seller_title'],
  112. "XHFDZ"=>$ticket['seller_addr'],
  113. "XHFDH"=>$ticket['seller_mobile'],
  114. "XHFYH"=>$ticket['seller_bank'],
  115. "XHFZH"=>$ticket['seller_bankNo'],
  116. "GMFBM"=>"",
  117. "GMFSBH"=>$ticket['buyer_id'],
  118. "GMFMC"=>$ticket['buyer_title'],
  119. "GMFDZ"=>$ticket['buyer_addr'],
  120. "GMFDH"=>$ticket['buyer_mobile'],
  121. "GMFYH"=>$ticket['buyer_bank'],
  122. "GMFZH"=>$ticket['buyer_bankNo'],
  123. "GMFLX"=>$invinfo['company_type']??'01',
  124. "GMFSJH"=>"",
  125. "GMFDZYX"=>'',
  126. "KPR"=>$ticket['issuer'],
  127. "SKR"=>$ticket['reciver'],
  128. "FHR"=>$ticket['reviewer'],
  129. "QDBZ"=>"0",
  130. "YFPDM"=>$red['YFPDM'],
  131. "YFPHM"=>$red['YFPHM'],
  132. "YFPLX"=>$red['FPLXDM'] == '004' ||$red['FPLXDM'] =='028'||$red['red_reason']=='' ?'':$red['FPLXDM'],
  133. "YFPKPRQ"=>$red['FPLXDM'] == '004' ||$red['FPLXDM'] =='028'||$red['red_reason']=='' ? '':date("Y-m-d",strtotime($red['YFPKPRQ'])),
  134. "CHYY"=>$red['FPLXDM'] == '004' ||$red['FPLXDM'] =='028' ? '':$red['red_reason'],
  135. "TSCHBZ"=>"0",
  136. "JSHJ"=>"-".$ticket['inv_total'],
  137. "HJJE"=>$red['HJJE'],
  138. "HJSE"=>$red['HJSE'],
  139. "BZ"=> $red['FPLXDM'] == '004' || $red['FPLXDM'] =='028'?"开具红字增值税专用发票信息表编号{$red['XXBBH']}":"对应正数发票代码:{$red['YFPDM']} 号码:{$red['YFPHM']}",
  140. "DDMXXX"=>$red['DDMXXX'],
  141. "FPLXDM"=>$red['FPLXDM'],
  142. "DDQQPCH"=>"",
  143. "DDQQLSH"=>randomkeys(8).md5(date('YmdHis').$red['id']),
  144. "status"=>"0",
  145. "result"=>"",
  146. "addtime"=>date("Y-m-d H:i:s"),
  147. "updatetime"=>date("Y-m-d H:i:s")
  148. ];
  149. $invoice=Db::name("invoice_interface")->insert($data);
  150. if($invoice==false){
  151. throw new \Exception("红冲提交失败");
  152. }
  153. Db::name("invoice_red")->where($red)->update(["status"=>3,"updatetime"=>date("Y-m-d H:i:s"),"remark"=>"红冲开票申请已提交待开票"]);
  154. }
  155. /**红冲申请失败退票信息失败
  156. * @param $fpdm
  157. * @param $fphm
  158. * @param string $remark
  159. * @throws \think\db\exception\DbException
  160. */
  161. public function updateReturn($returnCode,$remark=''){
  162. Db::name("invoice_return")->where(["returnCode"=>$returnCode,"status"=>1])->update(["status"=>4,"updatetime"=>date("Y-m-d H:i:s"),"remark"=>$remark]);
  163. }
  164. public function checkData($resultChildArr){
  165. foreach ($resultChildArr as $resultChild){
  166. $where=['YFPDM'=>$resultChild['YFPDM'] , 'YFPHM'=>$resultChild['YFPHM'] , 'status'=>1];
  167. $row=Db::name('invoice_red')->where($where)->findOrEmpty();
  168. if (!empty($row)) {
  169. Db::name('invoice_red')->where($where)->update($resultChild);
  170. if ($resultChild['status'] == 2) $this->AddRed($resultChild['YFPDM'] , $resultChild['YFPHM']);
  171. if ($resultChild['status'] == 5) $this->updateReturn($row['returnCode'] ,$resultChild['remark']);
  172. }
  173. }
  174. }
  175. }