123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- <?php
- declare (strict_types = 1);
- namespace app\admin\command;
- use app\admin\model\CompanyInfo;
- use app\admin\model\InvoiceInfo;
- use app\admin\model\Pay;
- use app\admin\model\PayInvoice;
- use app\admin\model\SupplierInfo;
- use think\console\Command;
- use think\console\Input;
- use think\console\input\Argument;
- use think\console\input\Option;
- use think\console\Output;
- use think\facade\Cache;
- use think\facade\Config;
- use think\facade\Db;
- class CheckInvoice extends Command
- {
- protected $datetime;
- protected $InvoiceNum;
- protected $taxStatus = [
- 'Y'=>'2','N'=>'1','H'=>'3'
- ];
- protected $output;
- protected function configure()
- {
- // 指令配置
- $this->setName('checkinvoice')
- ->setDescription('the checkinvoice command');
- }
- // 执行
- protected function execute(Input $input, Output $output)
- {
- $this->datetime = date("Y-m-d");
- if(Cache::get('checkInvoiceP')==1 || date('H')<12)return;
- $list = $this->getInvoice();
- $invoice =Config::get('invoiceType');
- $this->InvoiceNum = $invoice['KingInvoice'];
- $this->output=$output;
- while ($list->valid()){
- Cache::set('checkInvoiceP',1,1800);
- $invoice = $list->current();
- $this->checkTicket($invoice);
- $this->output->writeln("[".date(DATE_W3C)."]".$invoice['hpNo']."验票");
- $list->next();
- }
- Cache::set('checkInvoiceP',0);
- }
- // 获取待验票列表
- protected function getInvoice(){
- $list = PayInvoice::where([["status","in",[1,9]],["open_time","<",$this->datetime],["updatetime","<=",date("Y-m-d H:i:s")],['is_del',"=",0]])->cursor();
- foreach ($list as $item){
- yield $item;
- }
- }
- // 验票
- protected function checkTicket($invoice){
- $pay = Pay::where(["payNo"=>$invoice['payNo'],"is_del"=>0,"status"=>2])->findOrEmpty();
- if(!$pay->isEmpty()){
- if($pay->winv_fee <= 0) return PayInvoice::where(["id"=>$invoice['id']])->update(["status"=>8,"check_remark"=>"待开票金额不足","updatetime"=>date("Y-m-d H:i:s")]);
- $this->taxCheck($invoice);
- }
- }
- // 验票
- protected function taxCheck($invoice){
- $invoiceConf=Config::get('invoice');
- $iinvoicenfo = $invoiceConf['91110113MA004JNJ28'];
- $Tax =new \TaxInvoice($iinvoicenfo['appKey'],$iinvoicenfo['appSecret'],$iinvoicenfo['entCode']);
- $opentime = date("Ymd",strtotime($invoice['open_time']));
- $checkNum= $invoice['checkNumber'];
- $invNum= $this->InvoiceNum[$invoice['invoiceType']];
- $subtotal= $invoice['inv_subtotal_amount'];
- if(!in_array($invNum,['012','022',"028"]))$checkNum= substr($invoice['checkNumber'],-6);
- if(in_array($invNum,['021','022'])) $subtotal= $invoice['inv_amount'];
- $result=$Tax->CheckInvoiceSingle( $checkNum,$subtotal,$invoice['invoiceCode'],$invoice['invoiceNumber'],$opentime,$invNum);;
- $this->output->writeln('[' . date(DATE_W3C) . ']' . json_encode($result,JSON_UNESCAPED_UNICODE));
- Db::startTrans();
- try{
- if(isset($result['code']) && $result['code']=='0000' && isset($result['data']) && count($result['data'])>0){
- if($result['data']['cyjg']=='0001'){
- $data= $result['data'];
- $this->changeFiled($data,$invoice);
- }elseif (in_array($result['data']['cyjg'],['0002','1014'])){
- $this->lastUpdate($invoice['id']);
- }else $this->faild($invoice['id'],$result['data']['cyjgxx']);
- }else {
- if($result['code']=='0000' && $result['message']=='fail'){
- sleep(100);
- }else$this->faild($invoice['id'],"验票失败");
- }
- }catch (\Exception $e){
- Db::rollback();
- echo $e->getMessage();
- }
- Db::commit();
- }
- protected function changeFiled($data,$invoice){
- $save = [
- "payNo"=>$invoice['payNo'],
- "hpNo"=>$invoice['hpNo'],
- "type"=>$invoice['invoiceType'],
- "title"=>"",
- "code"=>$data['fpdm'],
- "check_code"=>$data['jym'],
- "number"=>$data['fphm'],
- "issue_date"=>$data['kprq'],
- "encryption_block"=>"",
- "buyer_name"=>$data['gmfmc'],
- "buyer_id"=>$data['gmfsbh'],
- "buyer_address"=>$data['gmfdzdh'],
- "buyer_bank"=>$data['gmfyhzh'],
- "seller_name"=>$data['xhfmc'],
- "seller_id"=>$data['xhfsbh'],
- "seller_address"=>$data['xhfdzdh'],
- "seller_bank"=>$data['xhfyhzh'],
- "subtotal_amount"=>$data['fpje'],
- "subtotal_tax"=>$data['fpse'],
- "machine_number"=>$data['jqbh'],
- "status"=>$this->taxStatus[$data['zfbz']]??0,
- "total"=>$data['jshj'],
- "total_in_words"=>"",
- "receiver"=>$data['skr'],
- "issuer"=>$data['kpr'],
- "reviewer"=>$data['fhr'],
- "remarks"=>$data['bz'],
- "change_field"=>"",
- "item_list"=>$this->checkItem($data['detailList'])
- ];
- $pay = Pay::where(["payNo"=>$invoice['payNo'],"is_del"=>0,"status"=>2])->findOrEmpty();
- $inv_fee=($pay->winv_fee <= $save['total'])?$pay->winv_fee:$save['total'];
- $pay->winv_fee = $pay->winv_fee - $inv_fee;
- $pay->inv_fee = $pay->inv_fee+$inv_fee;
- $up=$pay->save();
- if($up==false) throw new \Exception("对账单更新失败");
- $buyerCode=CompanyInfo::getRegisterCode($pay->companyNo);
- $sellerCode=SupplierInfo::getRegisterCode($pay->supplierNo);
- $sa= PayInvoice::where(["hpNo"=>$invoice['hpNo'],"status"=>[1,9]])->update([
- "status"=>$pay->pay_type==1?11:2,
- "invStatus"=>$this->taxStatus[$data['zfbz']]??0,
- "check_remark"=>'',
- "inv_fee"=>$inv_fee,
- "inv_amount"=>$save['total'],
- "buyer_check"=>$buyerCode==$save['buyer_id']?1:2,
- "seller_check"=>$sellerCode==$save['seller_id']?1:2,
- "inv_subtotal_amount"=>$save['subtotal_amount'],
- "updatetime"=>date("Y-m-d H:i:s")
- ]);
- if($sa==false) throw new \Exception("{$invoice['hpNo']}验票数据更新失败");
- $cres=InvoiceInfo::create($save);
- if($cres==false) throw new \Exception("{$invoice['hpNo']}验票数据写入失败");
- }
- protected function checkItem($detail){
- if(!empty($detail)) return array_map(function ($item) {
- return [
- 'name'=>$item['hwmc'],
- 'specification'=>$item['ggxh'],
- 'unit'=>$item['jldw'],
- 'quantity'=>$item['spsl'],
- 'unit_price'=>$item['bhsdj'],
- 'amount'=>$item['je'],
- 'tax'=>$item['se'],
- 'tax_rate'=>$item['sl'],
- 'license_plate_number'=>$item['ssflbm'],
- ];
- }, $detail);
- return [];
- }
- protected function faild($id,$msg=''){
- PayInvoice::where(["id"=>$id])->update(["status"=>5,"check_remark"=>$msg,"updatetime"=>date("Y-m-d H:i:s")]);
- }
- protected function lastUpdate($id){
- PayInvoice::where(["id"=>$id])->update(['status'=>9,'check_remark'=>'验证失败,等待第二天验证', "updatetime"=> date("Y-m-d H:i:s",strtotime("+1 day"))]);
- }
- }
|