GetOrderInfoAndInvoiceInfo.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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']])->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. $this->insertTicket($value['result'],$invNo);
  48. }else{
  49. $invNo = $value['invNo'];
  50. 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")]);
  51. $this->insertTicket($value['result'],$invNo);
  52. }
  53. if(isset($invNo)&&$invNo!=''){
  54. $is_comon= Db::name('invoice_pool')->where(['invNo'=>$invNo,'is_del'=>0])
  55. ->value('is_comon');
  56. if ($is_comon == 1) {
  57. event('comonOrder' ,['invNo'=>$invNo ,'type' =>$value['KPLX']==0?'inv':'rinv']);
  58. $output->writeln( "【 $date 】通用订单验票完成".$invNo.$value['KPLX']==0?'开票':'退票');
  59. }
  60. }
  61. Db::name("invoice_interface")->where($value)->update(["is_check"=>1,"updatetime"=>date("Y-m-d H:i:s")]);
  62. }
  63. if($value['status']==3 || $value['status']==4){
  64. if($value['KPLX']==1){
  65. $red=Db::name('invoice_red')->where(['invRed'=>$value['invNo'],'is_del'=>0])->find();
  66. Db::name("invoice_red")->where(["invRed"=>$value['invNo'],"is_del"=>0])->update(["status"=>6,"remark"=>"红冲开票失败","updatetime"=>date("Y-m-d H:i:s")]);
  67. Db::name("invoice_return")->where(["returnCode"=>$red['returnCode'],"status"=>1])->update(["status"=>4,"remark"=>"红冲开票失败","updatetime"=>date("Y-m-d H:i:s")]);
  68. }else{
  69. 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")]);
  70. }
  71. Db::name("invoice_interface")->where($value)->update(["is_check"=>1,"updatetime"=>date("Y-m-d H:i:s")]);
  72. }
  73. }
  74. $output->writeln( "【 $date 】验票完成".$value['invNo']);
  75. Db::commit();
  76. Cache::store("redis")->set("GetOrderInfoAndInvoiceInfo",0);
  77. }catch (\Exception $exception){
  78. Cache::store("redis")->set("generrateinvoice",0);
  79. Db::rollback();
  80. $output->writeln( "【 $date 】开票申请失败".$exception->getMessage().$exception->getLine().$exception->getFile());
  81. }
  82. Cache::store("redis")->set("GetOrderInfoAndInvoiceInfo",0);
  83. }
  84. protected function GetData(){
  85. $list=Db::name("invoice_interface")->where([["status",">=",1],["is_check","=",0],["updatetime","<=",date("Y-m-d
  86. H:i:s")]])->limit(20)->lock(true)->select()->toArray();
  87. return $list;
  88. }
  89. protected function GetOrderInfo($order){
  90. $invoiceConf=Config::get('invoice');
  91. if(!isset($invoiceConf[$order['XHFSBH']])){
  92. Db::name('invoice_interface')->where($order)->update(['status'=>4,'remark'=>"金税配置不存在",'updatetime'=>date('Y-m-d H:i:s')]);
  93. echo '['.date('Y-m-d H:i:s').']';
  94. echo "金税配置不存在\r\n";
  95. return '';
  96. }
  97. $invoice = $invoiceConf[$order['XHFSBH']];
  98. $tax =new \TaxInvoice($invoice['appKey'],$invoice['appSecret'],$invoice['entCode']);
  99. $result=$tax->GetOrderInfoAndInvoiceInfo($order['XHFSBH'],$order['DDQQLSH']);
  100. if(isset($result['ZTDM'])&& $result['ZTDM']=='000000'){
  101. foreach ($result['DDFPZXX'] as $value){
  102. $zt=$value['DDFPXX']['DDZT'];
  103. $ztxx=$value['DDFPXX']['DDZTXX'];
  104. if($zt=='030000'){
  105. $data= $value['DDFPXX'];
  106. $data['itemlist'] = $value['DDMXXX'];
  107. if($data['KPLX']==0)$this->updateGood($value['DDMXXX'],$order['invNo'],4);
  108. else $this->updateGood($value['DDMXXX'],$order['invNo'],6);
  109. $exit= json_encode($data,JSON_UNESCAPED_UNICODE);
  110. Db::name("invoice_interface")->where(["DDQQLSH"=>$value['DDFPXX']['DDQQLSH']])->update(["status"=>2,"result"=>$exit,"remark"=>$ztxx,"updatetime"=>date("Y-m-d H:i:s")]);
  111. }
  112. if($zt=='031999' ||$zt=='032997'||$zt=='032999'||$zt=='032998' ){
  113. Db::name("invoice_interface")->where(["DDQQLSH"=>$value['DDFPXX']['DDQQLSH']])->update(["status"=>4,"remark"=>$ztxx,"updatetime"=>date("Y-m-d H:i:s")]);
  114. }
  115. if($zt=='031000' ||$zt=='032000'){
  116. Db::name("invoice_interface")->where(["DDQQLSH"=>$value['DDFPXX']['DDQQLSH']])->update(["status"=>4,"remark"=>$ztxx,"updatetime"=>date("Y-m-d H:i:s")]);
  117. }
  118. }
  119. }else{
  120. Db::name("invoice_interface")->where($order)->update(["status"=>4,"remark"=>$result['message']?? $result['ZTXX'],"updatetime"=>date("Y-m-d H:i:s")]);
  121. echo "[".date("Y-m-d H:i:s")."]";
  122. echo $result['message']?? $result['ZTXX'] ."\r\n";
  123. }
  124. }
  125. protected function insertTicket($data,$invNo=''){
  126. $json= json_decode($data,true);
  127. if(is_array($json)&&!empty($json)){
  128. $invinfo =Db::name("invoice_ticket")->where(["invNo"=>$invNo,'type'=>$json['KPLX'],'is_del'=>0])->findOrEmpty();
  129. if(empty($invinfo)){
  130. $invinfo["invNo"]=$invNo;
  131. $invinfo["status"]=1;
  132. $invinfo["addtime"]=date("Y-m-d H:i:s");
  133. }
  134. $invinfo["inv_type"]=$json['FPLXDM'];
  135. $invinfo["type"]=$json['KPLX'];
  136. $invinfo["inv_code"]=$json['FPDM'];
  137. $invinfo["inv_number"]=$json['FPHM'];
  138. $invinfo["inv_total"]=$json['JSHJ'];
  139. $invinfo["inv_subtotal"]=$json['HJJE'];
  140. $invinfo["open_date"]=$json['KPRQ'];
  141. $invinfo["seller_id"]=$json['XHFSBH'];
  142. $invinfo["seller_title"]=$json['XHFMC'];
  143. $invinfo["seller_addr"]=$json['XHFDZ'];
  144. $invinfo["seller_mobile"]=$json['XHFDH'];
  145. $invinfo["seller_bank"]=$json['XHFYH'];
  146. $invinfo["seller_bankNo"]=$json['XHFZH'];
  147. $invinfo["buyer_id"]=$json['GMFSBH'];
  148. $invinfo["buyer_title"]=$json['GMFMC'];
  149. $invinfo["buyer_addr"]=$json['GMFDZ'];
  150. $invinfo["buyer_mobile"]=$json['GMFDH'];
  151. $invinfo["buyer_bank"]=$json['GMFYH'];
  152. $invinfo["buyer_bankNo"]=$json['GMFZH'];
  153. $invinfo["tax_fee"]=$json['HJSE'];
  154. $invinfo["remark"]=$json['BZ']??$json['CHBZ'];
  155. $invinfo["issuer"]=$json['KPR'];
  156. $invinfo["reciver"]=$json['SKR'];
  157. $invinfo["reviewer"]=$json['FHR'];
  158. $invinfo["check_code"]=$json['JYM'];
  159. $invinfo["pdfstream"]=$json['PDFZJL']??"";
  160. $invinfo["item"]=json_encode($json['itemlist'],JSON_UNESCAPED_UNICODE);
  161. Db::name("invoice_ticket")->save($invinfo);
  162. }
  163. }
  164. private function updateGood($data,$invNo,$status=4){
  165. if(empty($data))return;
  166. if($status==6){
  167. $invinfo =Db::name('invoice_red')->where(['invRed'=>$invNo,'is_del'=>0])->findOrEmpty();
  168. $invNo = $invinfo['invNo']??'';
  169. if(!key_exists('ZXBM' , $data[0])){
  170. $data =Db::name('invoice_good')
  171. ->where(['invNo'=>$invNo,'is_del'=>0])
  172. ->field("0 as FPHXZ,
  173. catNo as SPBM,
  174. id as ZXBM,
  175. is_discount as YHZCBS,
  176. if(inv_tag=0,'',inv_tag) as LSLBS,
  177. addTax as ZZSTSGL,
  178. goodName as XMMC,
  179. '' as DW,
  180. '' as SPSL,
  181. '' as DJ,
  182. (-totalPrice) as JE,
  183. 0 HSBZ,
  184. tax as SL,
  185. -taxPrice as SE")->select()->toArray();
  186. }
  187. }
  188. foreach ($data as $value){
  189. if($value['ZXBM']!=''){
  190. $invinfo =Db::name("invoice_good")->where(["invNo"=>$invNo,"id"=>$value['ZXBM'],'is_del'=>0])->findOrEmpty();
  191. if(empty($invinfo)) continue;
  192. if($status==4){
  193. $invinfo['goodPrice'] = $value['DJ'];
  194. $invinfo['totalPrice'] = $value['JE'];
  195. $invinfo['taxPrice'] = $value['SE'];
  196. }else{
  197. $invinfo['goodNum'] =0;
  198. }
  199. $invinfo['updatetime'] = date("Y-m-d H:i:s");
  200. Db::name("invoice_good")->save($invinfo);
  201. }
  202. }
  203. $this->updateQRd($invNo,$status);
  204. }
  205. private function updateQRd($invNo,$status){
  206. $qrdArr=Db::name("assoc")->where(["viceCode"=>$invNo,"is_del"=>0])->column("id,orderCode,cancel_fee");
  207. if(!empty($qrdArr)){
  208. foreach ($qrdArr as $value){
  209. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  210. if($qrdinfo==false){
  211. echo "$invNo 确认单信息未找到\r\n";break;
  212. }
  213. if($status==4){
  214. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  215. echo "$invNo 确认单信息开票金额不足 \r\n";
  216. break;
  217. }
  218. $update =[
  219. "ainv_fee"=>$qrdinfo['ainv_fee']+$value['cancel_fee'],
  220. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  221. "invtime"=>date("Y-m-d H:i:s"),
  222. "inv_status"=>$qrdinfo['winv_fee']==0 &&($qrdinfo['inv_fee']-$value['cancel_fee'])==0 ? 3 : 2,
  223. "updatetime"=>date("Y-m-d H:i:s"),
  224. ];
  225. }
  226. if($status==6){
  227. if($qrdinfo['ainv_fee']<$value['cancel_fee']){
  228. echo "$invNo 确认单信息开票金额不足 \r\n";break;
  229. }
  230. $update =[
  231. "ainv_fee"=>$qrdinfo['ainv_fee']-$value['cancel_fee'],
  232. "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'],
  233. "invtime"=>'',
  234. "inv_status"=>$qrdinfo['inv_fee']==0 &&($qrdinfo['ainv_fee']-$value['cancel_fee'])==0 ? 1 : 2,
  235. "status"=>$qrdinfo['inv_fee']==0 &&($qrdinfo['ainv_fee']-$value['cancel_fee'])==0&& $qrdinfo['pay_status']==1 ? 0 : 1,
  236. "updatetime"=>date("Y-m-d H:i:s"),
  237. ];
  238. }
  239. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  240. if($qrdup==false){
  241. echo "$invNo 确认单信息更新失败 \r\n";break;
  242. }
  243. $assoc=["status"=>$status==4?2:3,"assoc_time"=>date("Y-m-d H:i:s"),"updatetime"=>date("Y-m-d H:i:s")];
  244. $assocup =Db::name("assoc")->where($value)->update($assoc);
  245. if($assocup==false){
  246. echo "$invNo 确认单关联信息更新失败 \r\n";break;
  247. }
  248. }
  249. }
  250. }
  251. }