GetOrderInfoAndInvoiceInfo.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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\console\input\Argument;
  7. use think\console\input\Option;
  8. use think\console\Output;
  9. use think\facade\Cache;
  10. use think\facade\Config;
  11. use think\facade\Db;
  12. class GetOrderInfoAndInvoiceInfo extends Command
  13. {
  14. protected function configure()
  15. {
  16. // 指令配置
  17. $this->setName('getorderinfoandinvoiceinfo')
  18. ->setDescription('the getorderinfoandinvoiceinfo command');
  19. }
  20. protected function execute(Input $input, Output $output)
  21. {
  22. // 指令输出
  23. $GetOrderInfoAndInvoiceInfo= Cache::store("redis")->get("GetOrderInfoAndInvoiceInfo");
  24. if($GetOrderInfoAndInvoiceInfo==1) return '';
  25. Cache::store("redis")->set("GetOrderInfoAndInvoiceInfo",1,180);
  26. Db::startTrans();
  27. try{
  28. $data = $this->GetData();
  29. if(empty($data)) {
  30. Db::rollback();
  31. return '';
  32. }
  33. $date=date("Y-m-d H:i:s");
  34. foreach ($data as $value){
  35. if($value['status']==1){
  36. $this->GetOrderInfo($value);
  37. }
  38. if($value['status']==2){
  39. if($value['KPLX']==1){
  40. $red=Db::name("invoice_red")->where(["invRed"=>$value['invNo'],"is_del"=>0])->find();
  41. if($red){
  42. $invNo=$red['invNo'];
  43. Db::name("invoice_return")->where(["returnCode"=>$red['returnCode'],"status"=>1])->update(["status"=>2,"updatetime"=>date("Y-m-d H:i:s")]);
  44. Db::name("invoice_red")->where($red)->update(["status"=>4,"remark"=>"开票成功","updatetime"=>date("Y-m-d H:i:s")]);
  45. Db::name("invoice_pool")->where(["invNo"=>$red['invNo'],"is_del"=>0])->update(["status"=>6,"updatetime"=>date("Y-m-d H:i:s")]);
  46. }
  47. }else{
  48. $invNo = $value['invNo'];
  49. Db::name("invoice_pool")->where(["invNo"=>$value['invNo'],"is_del"=>0])->update(["status"=>4,"seller_check"=>1,"buyer_check"=>1,"updatetime"=>date("Y-m-d H:i:s")]);
  50. }
  51. $da=Db::name("invoice_interface")->where($value)->update(["is_check"=>1,"updatetime"=>date("Y-m-d H:i:s")]);
  52. if($da) $this->insertTicket($value['result'],$invNo);
  53. }
  54. if($value['status']==3 || $value['status']==4){
  55. if($value['KPLX']==1){
  56. $red=Db::name('invoice_red')->where(['invRed'=>$value['invNo'],'is_del'=>0])->find();
  57. Db::name("invoice_red")->where(["invRed"=>$value['invNo'],"is_del"=>0])->update(["status"=>6,"remark"=>"红冲开票失败","updatetime"=>date("Y-m-d H:i:s")]);
  58. Db::name("invoice_return")->where(["returnCode"=>$red['returnCode'],"status"=>1])->update(["status"=>4,"remark"=>"红冲开票失败","updatetime"=>date("Y-m-d H:i:s")]);
  59. }else{
  60. Db::name("invoice_pool")->where(["invNo"=>$value['invNo'],"is_del"=>0])->update(["status"=>5,"check_remark"=>$value['remark'],"updatetime"=>date("Y-m-d H:i:s")]);
  61. }
  62. Db::name("invoice_interface")->where($value)->update(["is_check"=>1,"updatetime"=>date("Y-m-d H:i:s")]);
  63. }
  64. }
  65. $output->writeln( "【 $date 】验票完成".$value['invNo']);
  66. Db::commit();
  67. Cache::store("redis")->set("GetOrderInfoAndInvoiceInfo",0);
  68. }catch (\Exception $exception){
  69. Cache::store("redis")->set("generrateinvoice",0);
  70. Db::rollback();
  71. $output->writeln( "【 $date 】开票申请失败".$exception->getMessage().$exception->getLine());
  72. }
  73. Cache::store("redis")->set("GetOrderInfoAndInvoiceInfo",0);
  74. }
  75. protected function GetData(){
  76. $list=Db::name("invoice_interface")->where([["status",">=",1],["is_check","=",0],["updatetime","<=",date("Y-m-d
  77. H:i:s")]])->limit(20)->lock(true)->select()->toArray();
  78. return $list;
  79. }
  80. protected function GetOrderInfo($order){
  81. // $invoice=Config::get("invoice");
  82. $invoiceConf=Config::get('invoice');
  83. if(!isset($invoiceConf[$order['XHFSBH']])){
  84. Db::name('invoice_interface')->where($order)->update(['status'=>4,'remark'=>"金税配置不存在",'updatetime'=>date('Y-m-d H:i:s')]);
  85. echo '['.date('Y-m-d H:i:s').']';
  86. echo "金税配置不存在\r\n";
  87. return '';
  88. }
  89. $invoice = $invoiceConf[$order['XHFSBH']];
  90. $tax =new \TaxInvoice($invoice['appKey'],$invoice['appSecret'],$invoice['entCode']);
  91. $result=$tax->GetOrderInfoAndInvoiceInfo($order['XHFSBH'],$order['DDQQLSH']);
  92. if(isset($result['ZTDM'])&& $result['ZTDM']=='000000'){
  93. foreach ($result['DDFPZXX'] as $value){
  94. $zt=$value['DDFPXX']['DDZT'];
  95. $ztxx=$value['DDFPXX']['DDZTXX'];
  96. if($zt=='030000'){
  97. $data= $value['DDFPXX'];
  98. $data['itemlist'] = $value['DDMXXX'];
  99. if($data['KPLX']==0)$this->updateGood($value['DDMXXX'],$order['invNo'],4);
  100. else $this->updateGood($value['DDMXXX'],$order['invNo'],6);
  101. $exit= json_encode($data,JSON_UNESCAPED_UNICODE);
  102. Db::name("invoice_interface")->where(["DDQQLSH"=>$value['DDFPXX']['DDQQLSH']])->update(["status"=>2,"result"=>$exit,"remark"=>$ztxx,"updatetime"=>date("Y-m-d H:i:s")]);
  103. }
  104. if($zt=='031999' ||$zt=='032997'||$zt=='032999'||$zt=='032998' ){
  105. Db::name("invoice_interface")->where(["DDQQLSH"=>$value['DDFPXX']['DDQQLSH']])->update(["status"=>4,"remark"=>$ztxx,"updatetime"=>date("Y-m-d H:i:s")]);
  106. }
  107. if($zt=='031000' ||$zt=='032000'){
  108. Db::name("invoice_interface")->where(["DDQQLSH"=>$value['DDFPXX']['DDQQLSH']])->update(["remark"=>$ztxx,"updatetime"=>date("Y-m-d H:i:s")]);
  109. }
  110. }
  111. }else{
  112. Db::name("invoice_interface")->where($order)->update(["status"=>4,"remark"=>$result['message']?? $result['ZTXX'],"updatetime"=>date("Y-m-d H:i:s")]);
  113. echo "[".date("Y-m-d H:i:s")."]";
  114. echo $result['message']?? $result['ZTXX'] ."\r\n";
  115. }
  116. }
  117. protected function insertTicket($data,$invNo=''){
  118. $json= json_decode($data,true);
  119. if(is_array($json)&&!empty($json)){
  120. $invinfo =Db::name("invoice_ticket")->where(["invNo"=>$invNo,'type'=>$json['KPLX'],'is_del'=>0])->findOrEmpty();
  121. if(empty($invinfo)){
  122. $invinfo["invNo"]=$invNo;
  123. $invinfo["status"]=1;
  124. $invinfo["addtime"]=date("Y-m-d H:i:s");
  125. }
  126. $invinfo["inv_type"]=$json['FPLXDM'];
  127. $invinfo["type"]=$json['KPLX'];
  128. $invinfo["inv_code"]=$json['FPDM'];
  129. $invinfo["inv_number"]=$json['FPHM'];
  130. $invinfo["inv_total"]=$json['JSHJ'];
  131. $invinfo["inv_subtotal"]=$json['HJJE'];
  132. $invinfo["open_date"]=$json['KPRQ'];
  133. $invinfo["seller_id"]=$json['XHFSBH'];
  134. $invinfo["seller_title"]=$json['XHFMC'];
  135. $invinfo["seller_addr"]=$json['XHFDZ'];
  136. $invinfo["seller_mobile"]=$json['XHFDH'];
  137. $invinfo["seller_bank"]=$json['XHFYH'];
  138. $invinfo["seller_bankNo"]=$json['XHFZH'];
  139. $invinfo["buyer_id"]=$json['GMFSBH'];
  140. $invinfo["buyer_title"]=$json['GMFMC'];
  141. $invinfo["buyer_addr"]=$json['GMFDZ'];
  142. $invinfo["buyer_mobile"]=$json['GMFDH'];
  143. $invinfo["buyer_bank"]=$json['GMFYH'];
  144. $invinfo["buyer_bankNo"]=$json['GMFZH'];
  145. $invinfo["tax_fee"]=$json['HJSE'];
  146. $invinfo["remark"]=$json['BZ']??$json['CHBZ'];
  147. $invinfo["issuer"]=$json['KPR'];
  148. $invinfo["reciver"]=$json['SKR'];
  149. $invinfo["reviewer"]=$json['FHR'];
  150. $invinfo["check_code"]=$json['JYM'];
  151. $invinfo["pdfstream"]=$json['PDFZJL']??"";
  152. $invinfo["item"]=json_encode($json['itemlist'],JSON_UNESCAPED_UNICODE);
  153. Db::name("invoice_ticket")->save($invinfo);
  154. }
  155. }
  156. private function updateGood($data,$invNo,$status=4){
  157. if(empty($data))return;
  158. if($status==6)$invNo =Db::name("invoice_red")->where(["invRed"=>$invNo,"is_del"=>0])->value("invNo",'');
  159. foreach ($data as $value){
  160. if($value['ZXBM']!=''){
  161. $invinfo =Db::name("invoice_good")->where(["invNo"=>$invNo,"id"=>$value['ZXBM'],'is_del'=>0])->findOrEmpty();
  162. if(empty($invinfo)) continue;
  163. if($status==4){
  164. $invinfo['goodPrice'] = $value['DJ'];
  165. $invinfo['totalPrice'] = $value['JE'];
  166. $invinfo['taxPrice'] = $value['SE'];
  167. }else{
  168. $invinfo['goodNum'] =0;
  169. }
  170. $invinfo['updatetime'] = date("Y-m-d H:i:s");
  171. Db::name("invoice_good")->save($invinfo);
  172. }
  173. }
  174. $this->updateQRd($invNo,$status);
  175. }
  176. private function updateQRd($invNo,$status){
  177. $qrdArr=Db::name("assoc")->where(["viceCode"=>$invNo,"is_del"=>0])->column("id,orderCode,cancel_fee");
  178. if(!empty($qrdArr)){
  179. foreach ($qrdArr as $value){
  180. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  181. if($qrdinfo==false){
  182. echo "$invNo 确认单信息未找到\r\n";break;
  183. }
  184. if($status==4){
  185. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  186. echo "$invNo 确认单信息开票金额不足 \r\n";
  187. break;
  188. }
  189. $update =[
  190. "ainv_fee"=>$qrdinfo['ainv_fee']+$value['cancel_fee'],
  191. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  192. "invtime"=>date("Y-m-d H:i:s"),
  193. "inv_status"=>$qrdinfo['winv_fee']==0 &&($qrdinfo['inv_fee']-$value['cancel_fee'])==0 ? 3 : 2,
  194. "updatetime"=>date("Y-m-d H:i:s"),
  195. ];
  196. }
  197. if($status==6){
  198. if($qrdinfo['ainv_fee']<$value['cancel_fee']){
  199. echo "$invNo 确认单信息开票金额不足 \r\n";break;
  200. }
  201. $update =[
  202. "ainv_fee"=>$qrdinfo['ainv_fee']-$value['cancel_fee'],
  203. "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'],
  204. "invtime"=>'',
  205. "inv_status"=>$qrdinfo['inv_fee']==0 &&($qrdinfo['ainv_fee']-$value['cancel_fee'])==0 ? 1 : 2,
  206. "status"=>$qrdinfo['inv_fee']==0 &&($qrdinfo['ainv_fee']-$value['cancel_fee'])==0&& $qrdinfo['pay_status']==1 ? 0 : 1,
  207. "updatetime"=>date("Y-m-d H:i:s"),
  208. ];
  209. }
  210. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  211. if($qrdup==false){
  212. // return "确认单信息更新失败";
  213. echo "$invNo 确认单信息更新失败 \r\n";break;
  214. }
  215. $assoc=["status"=>$status==4?2:3,"assoc_time"=>date("Y-m-d H:i:s"),"updatetime"=>date("Y-m-d H:i:s")];
  216. $assocup =Db::name("assoc")->where($value)->update($assoc);
  217. if($assocup==false){
  218. // return "确认单关联信息更新失败";
  219. echo "$invNo 确认单关联信息更新失败 \r\n";break;
  220. }
  221. }
  222. }
  223. }
  224. }