123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- <?php
- declare (strict_types = 1);
- namespace app\admin\command;
- use app\admin\model\CompanyInfo;use app\admin\model\InvoicePool;
- use app\admin\model\InvoiceTicket;
- use think\facade\Cache;use think\facade\Config;
- 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\Db;
- class CheckTicket extends Command
- {
- protected $dateTime;
- protected $output;
- protected function configure()
- {
- // 指令配置
- $this->setName('checkticket')
- ->setDescription('the checkticket command');
- }
- protected function execute(Input $input, Output $output)
- {
- $this->dateTime = date("Y-m-d H:i:s");
- if(Cache::get('checkTicketP')==1 || date("H")<12)return;
- Cache::set('checkTicketP',1,1800);
- $this->output=$output;
- $ticket = $this->getTicket();
- while ($ticket->valid()) {
- Cache::set('checkTicketP',1,1800);
- $data = $ticket->current();
- $this->output->writeln('[' . date(DATE_W3C) . ']' . $data['invNo'] . '验票');
- $this->checkTicket($data);
- $ticket->next();
- }
- Cache::set('checkTicketP',0);
- // 指令输出
- // $output->writeln('checkticket');
- }
- protected function getTicket(){
- $ticket = InvoiceTicket::where([["is_del","=",0],["type","=",0],["status","=",0],['open_date','<',date("Y-m-d 00:00:00")],['updatetime','<',date('Y-m-d H:i:s')]])
- ->field("id,invNo,type,inv_type,inv_code,inv_number,inv_total,inv_subtotal,open_date,check_code")
- ->cursor();
- foreach ($ticket as $key => $value) {
- yield $value;
- }
- }
- protected function checkTicket($ticket){
- $info =InvoicePool::where(["invNo"=>$ticket['invNo'],"status"=>3])->findOrEmpty();
- if(!$info->isEmpty()){
- $companyInfo = CompanyInfo::where(["companyNo"=>$info->inv_out,"is_del"=>0,"status"=>1])->findOrEmpty();
- $isTicket = $companyInfo['out_ticket']??0;
- $ticket['is_comon'] = $info->is_comon;
- $this->taxCheck($ticket);
- }
- }
- protected function taxCheck($info){
- $invoiceConf=Config::get('invoice');
- $invoice = $invoiceConf['91110113MA004JNJ28'];
- $Tax =new \TaxInvoice($invoice['appKey'],$invoice['appSecret'],$invoice['entCode']);
- $opentime = date("Ymd",strtotime($info['open_date']));
- $checkNum= $info['check_code'];
- $subtotal=$info['inv_subtotal'];
- if($info['inv_type']!="016")$checkNum= substr($info['check_code'],-6);
- if(in_array($info['inv_type'],["022",'021'])) $subtotal= $info['inv_total'];
- $result=$Tax->CheckInvoiceSingle( $checkNum,$subtotal,$info['inv_code'],$info['inv_number'],$opentime,$info['inv_type']);
- $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,$info);
- }elseif (in_array($result['data']['cyjg'],['0002','1014'])){
- $this->lastUpdate($info['invNo']);
- }else $this->faild($info['invNo'],$result['data']['cyjgxx']);
- }else $this->faild($info['invNo'],'验票失败');
- Db::commit();
- }catch (\Exception $e){
- Db::rollback();
- echo $e->getMessage()."\r\n";
- }
- }
- protected function changeFiled($data,$info){
- $item = $this->itemFiled($data['detailList']);
- $tick = (new InvoiceTicket)->where(['invNo'=>$info['invNo'],'type'=>0,'status'=>0,'is_del'=>0])->findOrEmpty();
- if(!$tick->isEmpty()){
- $tick->save(['inv_total'=>$data['jshj'],
- 'inv_subtotal'=>$data['fpje'],
- 'buyer_id'=>$data['gmfsbh'],
- 'buyer_title'=>$data['gmfmc'],
- 'buyer_addr'=>$data['gmfdzdh'],
- 'buyer_bank'=>$data['gmfyhzh'],
- 'seller_id'=>$data['xhfsbh'],
- 'seller_title'=>$data['xhfmc'],
- 'seller_addr'=>$data['xhfdzdh'],
- 'seller_bank'=>$data['xhfyhzh'],
- 'tax_fee'=>$data['fpse'],
- 'remark'=>$data['bz'],
- 'issuer'=>$data['kpr'],
- 'reciver'=>$data['skr'],
- 'reviewer'=>$data['fhr'],
- 'item'=>$item,
- 'status'=>1,
- 'updatetime'=>$this->dateTime,
- ]);
- }
- $wq=InvoicePool::where(['invNo'=>$info['invNo']])->save(["status"=>4,"updatetime"=>$this->dateTime]);
- if($wq==false) throw new \Exception("更新发票状态失败");
- \app\admin\model\Assoc::subOrder($info['invNo']);
- if($info['is_comon']==1)event('comonOrder',['invNo'=>$info['invNo'],'type'=>'inv']);
- }
- protected function itemFiled($detail){
- if(!empty($detail)) return array_map(function ($item) {
- return [
- 'XMMC'=>$item['hwmc'],
- 'DW'=>$item['jldw'],
- "GGXH"=>$item['ggxh'],
- 'SPSL'=>$item['spsl'],
- 'DJ'=>$item['bhsdj'],
- 'JE'=>$item['je'],
- 'SE'=>$item['se'],
- 'SL'=>$item['sl'],
- 'SPBM'=>$item['ssflbm'],
- ];
- },$detail);
- return [];
- }
- protected function faild($invNo,$msg=''){
- InvoiceTicket::where(['invNo'=>$invNo,"type"=>0,"status"=>0,"is_del"=>0])->save(["status"=>5,"updatetime"=>$this->dateTime]);
- InvoicePool::where(['invNo'=>$invNo])->save(["status"=>5,"updatetime"=>$this->dateTime,"check_remark"=>$msg]);
- }
- protected function lastUpdate($invNo){
- InvoiceTicket::where(['invNo'=>$invNo,"type"=>0,"status"=>0,"is_del"=>0])->save(["updatetime"=> date("Y-m-d H:i:s",strtotime("+1 day"))]);
- }
- protected function getInvoice($invNo){
- $info = (new InvoiceTicket)->where(['invNo'=>"INV2407160957046808",'type'=>0,'is_del'=>0])->findOrEmpty();
- echo $info->toJson();
- if(!$info->isEmpty()){
- $info->save(["status"=>rand(1,6)]);
- // InvoiceTicket::where(['invNo'=>'INV2407160957046808','type'=>0,'is_del'=>0])->update(['status'=>rand(1,10)]);
- }
- }
- }
|