GetOrderInfoAndInvoiceInfo.php 9.7 KB

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