Просмотр исходного кода

Merge branch 'dev-wgg' of wugg/cxinv into dev

wugg 6 месяцев назад
Родитель
Сommit
a3b200190f

+ 163 - 0
app/admin/command/CheckInvoice.php

@@ -0,0 +1,163 @@
+<?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 function configure()
+    {
+        // 指令配置
+        $this->setName('checkinvoice')
+            ->setDescription('the checkinvoice command');
+    }
+
+    protected function execute(Input $input, Output $output)
+    {
+        $this->datetime = date("Y-m-d");
+        $list = $this->getInvoice();
+        $invoice =Config::get('invoiceType');
+        $this->InvoiceNum = $invoice['KingInvoice'];
+        if(Cache::get('checkInvoiceP')==1)return;
+        while ($list->valid()){
+            Cache::set('checkInvoiceP',1,1800);
+             $invoice = $list->current();
+            $this->checkTicket($invoice);
+            $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")]])->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);;
+       echo json_encode($result,JSON_UNESCAPED_UNICODE);
+       Db::startTrans();
+        try{
+            if(isset($result['code']) && $result['code']=='0000' and 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 $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;
+        $pay->save();
+        $buyerCode=CompanyInfo::getRegisterCode($pay->companyNo);
+        $sellerCode=SupplierInfo::getRegisterCode($pay->supplierNo);
+        PayInvoice::where(["payNo"=>$invoice['payNo'],"status"=>1])->update([
+            "status"=>11,
+            "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")
+        ]);
+        InvoiceInfo::create($save);
+    }
+
+    protected function checkItem($detail){
+        if(!empty($detail)) return array_map(function ($item) {
+             return [
+              'name'=>$item['hwmc'],
+              'specification'=>$item['jldw'],
+              'unit'=>$item['ggxh'],
+              '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"))]);
+    }
+
+}

+ 149 - 0
app/admin/command/CheckTicket.php

@@ -0,0 +1,149 @@
+<?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 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)return;
+        $ticket = $this->getTicket();
+        while ($ticket->valid()) {
+            Cache::set('checkTicketP',1,1800);
+            $data = $ticket->current();
+            echo  $data['invNo']."\r\n";
+            $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],['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']);
+        echo json_encode($result,JSON_UNESCAPED_UNICODE);
+        Db::startTrans();
+        try{
+            if(isset($result['code']) && $result['code']=='0000' and 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($invoice['id'],$result['data']['cyjgxx']);
+        }else $this->faild($info['invNo']);
+       }catch (\Exception $e){
+            Db::rollback();
+            echo $e->getMessage()."\r\n";
+       }
+        Db::commit();
+    }
+    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,
+            ]);
+        }
+        InvoicePool::where(['invNo'=>$info['invNo']])->save(["status"=>4,"updatetime"=>$this->dateTime]);
+         \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)]);
+        }
+    }
+}

+ 3 - 0
app/admin/controller/Action.php

@@ -8,6 +8,9 @@ use think\facade\Db;
 class Action extends Base{
     public function __construct(App $app) {parent::__construct($app);}
 
+    /**("Post")
+    * @return \think\response\Json|void
+     */
     //获取素有菜单列表数据
     public function index()
     {

+ 0 - 3
app/admin/controller/ComonOrder.php

@@ -168,9 +168,6 @@ class ComonOrder extends Base
         $supplierNo = array_unique(array_column($list,'supplierNo'));
         $customerNo = array_unique(array_column($list,'customerNo'));
         $goodNo = array_unique(array_column($list,'goodNo'));
-//        if(in_array(1,$status)|| in_array(2,$status)){
-//        	$this->error('提交数据存在已对账或对账中的数据');
-//        }
         $supplierName = \app\admin\model\SupplierInfo::whereIn('code',$supplierNo)->value('name','');
         $customerName = \app\admin\model\CompanyInfo::whereIn('companyNo',$customerNo)->value('company_name','');
         if(count($supplierNo)>1)$this->error('提交数据采购供应商存在不一致');

+ 299 - 0
app/admin/controller/InvoiceItem.php

@@ -0,0 +1,299 @@
+<?php
+
+
+namespace app\admin\controller;
+
+use app\admin\model\InvoiceGood;
+use app\admin\model\InvoiceOrder;
+use app\admin\model\OrderCategory;
+use app\admin\model\PayInfo;
+use think\App;
+use think\facade\Validate;
+class InvoiceItem extends Base{
+    public function __construct(App $app) {
+     #  $this->novalidate = ["*"];
+        parent::__construct($app);
+    }
+    //查询
+    public function cgdListByPayNo(){
+        $param = $this->request->param(["payNo"=>"","merge_code"=>"","inv_good_name"=>"",'good_name'=>''],"post","trim");
+        $where=[["a.status","=",1],["a.is_del","=",0]];
+        if($param["payNo"]!=="") $where[]=["a.payNo","=",$param["payNo"]];
+        if($param["merge_code"]!=="") $where[]=["c.merge_code","like","%{$param['merge_code']}%"];
+        if($param["inv_good_name"]!=="") $where[]=["c.inv_good_name","like","%{$param["inv_good_name"]}%"];
+        if($param["good_name"]!=="") $where[]=["b.goodName","like","%{$param["good_name"]}%"];
+        $list = PayInfo::alias('a')
+                ->join('cgd_info b','a.cgdNo=b.sequenceNo',"left")
+                ->join('order_category c','b.sequenceNo=c.code and b.goodNo=c.spuCode and c.order_type=2',"left")
+                ->where($where)
+                ->field("b.sequenceNo,b.goodNo,b.goodName,(b.goodNum-thNum) as goodNum,b.goodPrice,b.totalPrice,
+                c.merge_code,c.cat_code,c.cat_name,c.short_name,c.tax,c.inv_good_name")
+                ->select();
+        $list->each(function (&$item){
+            $item['balance_amount']= bcsub($item['totalPrice'],InvoiceOrder::getInvoiceOrderTotalFee($item['sequenceNo']),2);
+        });
+         $this->success('获取成功',$list);
+    }
+    //查询
+    public function qrdListByInvNo(){
+        $param = $this->request->param(["invNo"=>"","merge_code"=>"","inv_good_name"=>""],"post","trim");
+        $where=[["a.goodNum",">",0],["a.is_del","=",0]];
+        if($param["invNo"]!=="") $where[]=["a.invNo","=",$param["invNo"]];
+        if($param["merge_code"]!=="") $where[]=["c.merge_code","=",$param["merge_code"]];
+        if($param["inv_good_name"]!=="") $where[]=["c.inv_good_name","like","%{$param["inv_good_name"]}%"];
+        $list= InvoiceGood::alias('a')
+                ->join('qrd_info b','a.orderCode=b.sequenceNo','left')
+                ->join('order_category c','b.sequenceNo=c.code and b.goodNo=c.spuCode and  c.order_type=1','left')
+                ->where($where)
+                ->field("b.sequenceNo,b.goodNo,b.goodName,a.goodNum,a.goodPrice,a.totalPrice,
+                                c.merge_code,c.cat_code,c.cat_name,c.short_name,c.tax,c.inv_good_name")
+            ->select();
+          $list->each(function (&$item){
+            $item['balance_amount']= bcsub($item['totalPrice'],InvoiceOrder::getInvoiceOrderTotalFee($item['sequenceNo']),2);
+        });
+         $this->success('获取成功',$list);
+    }
+    //添加发票明细对应关系
+    public function create(){
+        $param = $this->request->param(["itemId"=>"","orderArr"=>[]],"post","trim");
+        $valid=Validate::rule([
+            'itemId|发票明细id' => 'require|number|gt:0',
+            'orderArr|订单信息' => 'require|array',
+        ]);
+        if(!$valid->check($param))$this->error($valid->getError());
+        $valids=Validate::rule([
+            'code|订单号' => 'require|max:255',
+            'spuCode|商品编码' => 'require|max:255',
+            'good_name|商品名称' => 'require|max:255',
+            'num|数量' => 'require|float|gt:0',
+            'good_price|商品价格' => 'require|float|gt:0',
+            'total_amount|总金额' => 'require|float|gt:0',
+            'remark|备注' => 'max:255',
+        ]);
+        $item = \app\admin\model\InvoiceItem::where(['id'=>$param["itemId"]])->findOrEmpty();
+        if($item->isEmpty())$this->error("发票明细不存在");
+        $save=[];
+        $amount = $item['balance_amount'];
+        foreach ($param["orderArr"] as $k=>$v){
+            if(!$valids->check($v))$this->error($valids->getError());
+            $taxInfo = OrderCategory::where(['code'=>$v['code'],'spuCode'=>$v['spuCode']])->findOrEmpty();
+            if($taxInfo->isEmpty())$this->error("商品编码{$v['spuCode']}税目信息不存在");
+            $balance="0";
+            if($amount<$v['total_amount']){
+                $balance = bcsub($v['total_amount'],$amount,2);
+                $amount="0";
+            }else{
+                $balance = "0";
+                $amount = bcsub($amount,$v['total_amount'],2);
+            }
+            $diff=[];
+            $tax_diff=1;
+            $cat_diff=1;
+            if(str_replace("%","",$item['tax'])!=$taxInfo['tax']){
+                $diff["tax"] = [
+                    "inv_tax"=>str_replace('%','',$item['tax']),
+                    "order_tax"=>$taxInfo['tax'],
+                ];
+                $tax_diff=2;
+            }
+            if($item['cat_code']!=$taxInfo['merge_code']){
+                $diff["cat_code"] = [
+                    "inv_cat_code"=>$item['cat_code'],
+                    "order_cat_code"=>$taxInfo['merge_code'],
+                ];
+                $cat_diff=2;
+            }
+            $save[]=[
+                "itemId"=>$param["itemId"],
+                "code"=>$v["code"],
+                "spuCode"=>$v["spuCode"],
+                "good_name"=>$v['good_name'],
+                "num"=>$v["num"],
+                "good_price"=>$v["good_price"],
+                "total_amount"=>$v["total_amount"],
+                "tax_diff"=> $tax_diff,
+                "cat_diff"=>$cat_diff,
+                "balance_amount"=>$balance,
+                "diff_info"=>$diff,
+                "status"=>$balance>0?2:1,
+                "remark"=>$v["remark"],
+                "apply_id"=>$this->uid,
+                "apply_name"=>$this->uname
+            ];
+        }
+        $item->startTrans();
+        try{
+
+            $res=(new \app\admin\model\InvoiceOrder)->saveAll($save);
+            if($res){
+                $item->balance_amount = $amount;
+                $item->status = $amount>0?2:1; //已关联数据 $amont=0 2 还有余额则为1
+                $item->save();
+            }else throw new \Exception("添加失败");
+            $item->commit();
+        }catch (\Exception $e){
+            $item->rollback();
+            $this->error($e->getMessage());
+       }
+        $this->success('添加成功');
+    }
+    //删除发票明细对应关系
+    public function delete(){
+        $param = $this->request->param(["id"=>""],"post","trim");
+        $valid=Validate::rule([
+            'id|发票明细对应id' => 'require|number|gt:0',
+        ]);
+        if(!$valid->check($param))$this->error($valid->getError());
+        $info = \app\admin\model\InvoiceOrder::where(['id'=>$param["id"]])->findOrEmpty();
+        if($info->isEmpty())$this->error("发票明细对应关系不存在");
+        $item = \app\admin\model\InvoiceItem::where(['id'=>$info['itemId']])->findOrEmpty();
+        if($item->isEmpty())$this->error("发票明细不存在");
+        InvoiceOrder::startTrans();
+        try{
+            $res = $info->delete();
+            if($res){
+                $count = \app\admin\model\InvoiceOrder::where(['itemId'=>$info['itemId']])->count();
+                $item->balance_amount = bcadd($item['balance_amount'],bcsub($info['total_amount'],$info['balance_amount'],2),2);
+                $item->status = ($count==0) ? 0 : ($item->balance_amount==$item->total_amount?0:($item->balance_amount>0?2:1));
+                $item->save();
+            }else throw new \Exception("删除失败");
+            InvoiceOrder::commit();
+        }catch (\Exception $e){
+            InvoiceOrder::rollback();
+            $this->error($e->getMessage());
+        }
+        $this->success('删除成功');
+
+    }
+
+    public function addRemark(){
+        $param = $this->request->param(['id'=>''],'post','trim');
+        $valid=Validate::rule([
+            'id|发票明细对应id' => 'require|number|gt:0',
+            'remark|备注' => 'require|max:255',
+        ]);
+        if(!$valid->check($param))$this->error($valid->getError());
+        $info = \app\admin\model\InvoiceOrder::where(['id'=>$param['id']])->findOrEmpty();
+        if($info->isEmpty())$this->error('发票明细对应关系不存在');
+        $item = \app\admin\model\InvoiceItem::where(['id'=>$info['itemId']])->findOrEmpty();
+        if($item->isEmpty())$this->error('发票明细不存在');
+        $info->startTrans();
+        try{
+            $info->remark = $param['remark'] ?? '';
+            $res = $info->save();
+            if($res==false)throw new \Exception('备注添加失败');
+            $info->commit();
+        }catch (\Exception $e){
+            $info->rollback();
+        }
+        $this->success('备注添加成功');
+
+    }
+
+    public function getListByCode(){
+        $param=$this->request->param(["code"=>"","status"=>"","size"=>100]);
+        $where=[];
+        if($param["code"]!=="") $where[]=["invoiceCode","=",$param["code"]];
+        if($param["status"]!=="") $where[]=["status","=",$param["status"]];
+        $list= \app\admin\model\InvoiceItem::with(["OrderInfo"])
+            ->where($where)
+            ->limit($param["size"])
+            ->select();
+        $this->success('获取成功',$list);
+    }
+
+    public function importOrder(){
+        $param=$this->request->param([ "order_type"=>"","list"=>[]],"post","trim");
+        $valid = Validate::rule([
+            'order_type|订单类型' => 'require|in:1,2',
+            'list|发票明细' => 'require|array',
+        ]);
+        if(!$valid->check($param))$this->error($valid->getError());
+        $listValid = Validate::rule([
+            "itemId|发票明细ID"=>'require|number|gt:0',
+            'code|订单编码' => 'require|max:20',
+            'num|数量' => 'float|egt:0',
+            'good_price|商品价格' => 'float|egt:0',
+            'total_amount|总金额' => 'require|float|gt:0',
+            'remark|备注' =>'max:255',
+        ]);
+        $itemArr= \app\admin\model\InvoiceItem::whereIn('id',array_column($param['list'],"itemId"))->column("id,invoiceCode,order_type,balance_amount,cat_code,tax","id");
+        $orderInfo=[];
+        $orderItem=[];
+        foreach($param["list"] as $v){
+            if(!$listValid->check($v))$this->error($listValid->getError());
+            if(!isset($itemArr[$v['itemId']]))$this->error("发票明细不存在");
+            if(!isset($orderInfo[$itemArr[$v['itemId']]['invoiceCode'].$v["code"]])){
+                  $orderInfo[$itemArr[$v['itemId']]['invoiceCode'].$v['code']]= \app\admin\model\InvoiceOrder::getOrderInfo( $itemArr[$v['itemId']]['invoiceCode'],$v['code'],$param['order_type']);
+            }
+
+             if($orderInfo[$itemArr[$v['itemId']]['invoiceCode'].$v['code']]->isEmpty())$this->error("发票{$itemArr[$v['itemId']]['invoiceCode']}关联订单{$v['code']}信息不存在");
+            if($orderInfo[$itemArr[$v['itemId']]['invoiceCode'].$v['code']]['balance_amount']<$v['total_amount'])$this->error("{$v["code"]}订单金额不足");
+            if($itemArr[$v['itemId']]['balance_amount']<=0)$this->error("发票明细ID {$v["itemId"]} 可关联金额不足");
+            if($orderInfo[$itemArr[$v['itemId']]['invoiceCode'].$v['code']]['merge_code']=="")$this->error("{$v["code"]}订单税目信息不存在");
+             if($itemArr[$v['itemId']]['balance_amount']<$v['total_amount']){
+                $balance = bcsub($v['total_amount'],$itemArr[$v['itemId']]['balance_amount'],2);
+                $itemArr[$v['itemId']]['balance_amount']='0';
+            }else{
+                $balance = '0';
+                $itemArr[$v['itemId']]['balance_amount'] = bcsub($itemArr[$v['itemId']]['balance_amount'],$v['total_amount'],2);
+            }
+            $orderInfo[$itemArr[$v['itemId']]['invoiceCode'].$v['code']]['balance_amount'] =
+            bcsub($orderInfo[$itemArr[$v['itemId']]['invoiceCode'].$v['code']]['balance_amount'],bcsub($v['total_amount'],$balance,2),2);
+            $tax = $orderInfo[$itemArr[$v['itemId']]['invoiceCode'].$v['code']]['tax'];
+            $merge_code = $orderInfo[$itemArr[$v['itemId']]['invoiceCode'].$v['code']]['merge_code'];
+            $diff=[];
+            $tax_diff=1;
+            $cat_diff=1;
+            if(str_replace('%','',$itemArr[$v['itemId']]['tax'])!=$tax ){
+                $diff['tax'] = [
+                    'inv_tax'=>str_replace('%','',$itemArr[$v['itemId']]['tax']),
+                    'order_tax'=>$tax ,
+                ];
+                $tax_diff=2;
+            }
+            if($itemArr[$v['itemId']]['cat_code']!=$merge_code){
+                $diff['cat_code'] = [
+                    'inv_cat_code'=>$itemArr[$v['itemId']]['cat_code'],
+                    'order_cat_code'=>$merge_code,
+                ];
+                $cat_diff=2;
+            }
+            $itemArr[$v['itemId']]['status'] = ($itemArr[$v['itemId']]['balance_amount']>0?2:1);
+            $orderItem[]=[
+                'itemId'=>$v['itemId'],
+                'code'=>$v['code'],
+                'spuCode'=>$orderInfo[$itemArr[$v['itemId']]['invoiceCode'].$v['code']]['goodNo'],
+                'good_name'=>$orderInfo[$itemArr[$v['itemId']]['invoiceCode'].$v['code']]['goodName'],
+                'num'=>$v['num']??0,
+                'good_price'=>$v['good_price']??0,
+                'total_amount'=>$v['total_amount'],
+                'tax_diff'=> $tax_diff,
+                'cat_diff'=>$cat_diff,
+                'balance_amount'=>$balance,
+                'diff_info'=>$diff,
+                'status'=>$balance>0?2:1,
+                'remark'=>$v['remark'],
+                'apply_id'=>$this->uid,
+                'apply_name'=>$this->uname
+            ];
+        }
+         $orderItemModel = new \app\admin\model\InvoiceItem();
+        $orderItemModel->startTrans();
+        try{
+           $num=$orderItemModel->saveAll(array_values($itemArr));
+            if(count($num)>0){
+                (new InvoiceOrder())->saveAll($orderItem);
+            }else throw new \Exception("导入失败");
+        }catch (\Exception $e){
+            $orderItemModel->rollback();
+            $this->error($e->getMessage());
+        }
+        $orderItemModel->commit();
+        $this->success("导入成功");
+    }
+
+    public function exportItem(){
+        $param=$this->request->param([ "order_type"=>"","list"=>[]],"post","trim");
+    }
+}

Разница между файлами не показана из-за своего большого размера
+ 590 - 528
app/admin/controller/OrderInv.php


+ 4 - 5
app/admin/controller/OrderPay.php

@@ -584,7 +584,6 @@ class OrderPay extends Base{
          if($relaComNo!=""){
             $condition[]=["a.companyNo","=",$relaComNo];
         }
-        $condition=[["a.is_del","=",0]];
         if($tradNo!=''){
             $condition[]=["a.tradNo","like","%$tradNo%"];
         }
@@ -610,13 +609,13 @@ class OrderPay extends Base{
             $condition[]=["a.status","=",$status];
         }
          $count =Db::name("trade_return")->alias("a")
-         ->leftJoin("assoc b","a.logNo=b.viceCode")
-         ->leftJoin("qrd_info c","c.sequenceNo=b.orderCode")
+         ->leftJoin("assoc b","a.logNo=b.viceCode and b.type=2")
+         ->leftJoin("qrd_info c","c.sequenceNo=b.orderCode and c.is_del=0")
          ->where($condition)->count();
          $total=ceil($count/$size);
          $page = $page>$total ? intval($total):$page;
-         $list=Db::name("trade_return")->alias("a")->leftJoin("assoc b","a.logNo=b.viceCode")
-         ->leftJoin("qrd_info c","c.sequenceNo=b.orderCode")
+         $list=Db::name("trade_return")->alias("a")->leftJoin("assoc b","a.logNo=b.viceCode and b.type=2")
+         ->leftJoin("qrd_info c","c.sequenceNo=b.orderCode  and c.is_del=0")
          ->field("a.*,b.orderCode,c.qrdSource,goodNo,goodName,qrdType,c.ownerName,c.ownerid,c.department,c.poCode,c.platName,b.cancel_fee,c.customerName,c.customerNo")
          ->where($condition)->page($page,$size)->order("a.addtime desc")->select();
          return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);

+ 173 - 143
app/admin/controller/Payment.php

@@ -4,7 +4,17 @@ declare (strict_types = 1);
 namespace app\admin\controller;
 
 
-use app\admin\model\CgdInfo;use app\admin\model\FhdChild;use app\admin\model\GoodCombind;use app\admin\model\InvoicePay;use app\admin\model\InvoicePool;use app\admin\model\OrderCategory;use app\admin\model\Pay;use app\admin\model\PayInfo;use app\admin\model\ReportCode;
+use app\admin\model\CgdInfo;
+use app\admin\model\FhdChild;
+use app\admin\model\GoodCombind;
+use app\admin\model\InvoiceOrder;
+use app\admin\model\InvoicePay;
+use app\admin\model\InvoicePool;
+use app\admin\model\OrderCategory;
+use app\admin\model\Pay;
+use app\admin\model\InvoiceInfo;
+use app\admin\model\PayInvoice;
+use app\admin\model\ReportCode;
 use think\App;use think\Exception;
 use think\facade\Config;
 use think\facade\Db;
@@ -1462,96 +1472,139 @@ class Payment extends Base
 
     /**
 	 * 发票审核状态     //发票验证通过 才会扣减待开票金额 失败的开票申请不用处理
-	 * 0 发票图片识别中 1待系统验证 2 买方公司审核3带买方公司认证 4 认证成功 5验证失败 6买方审核驳回 7 认证失败 8 回票流程终止 9 验证超次数 10 回票已退
-     * @return \think\response\Json|void
+	 * // 1待系统验证 2 买方公司审核 3带买方公司认证 4 认证成功 5验证失败 6买方审核驳回 7 认证失败 8 回票流程终止 9 验证超次数 10 回票已退
+    * 11 待关联订单 12 订单关联完毕待财务审核 13 订单关联完毕财务驳回
+	   * @return \think\response\Json|void
      * @throws \think\db\exception\DataNotFoundException
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
      * @throws \think\exception\DbException
      */
     public function invStatus(){
-        $post = $this->post;
-        $hpNo = isset($post['hpNo'])&& $post['hpNo']!="" ? trim($post['hpNo']) :"";
-        if($hpNo==""){
-            return error_show(1004,"参数 hpNo 不能为空");
-        }
-         $status = isset($post['status'])&& $post['status']!="" ? intval($post['status']) :"";
-        if($status==""){
-            return error_show(1004,"参数status 不能为空");
-        }
-//        if(!in_array($status,[0,1,2,3,4,5,6,7,8,9,10,11,12])){
-//            return error_show(1004,"参数status 无效值");
-//        }
-        if(!in_array($status,[0,1,2,3,4,5,6,7])){
-            return error_show(1004,"参数status 无效值");
-        }
-        $payment= Db::name("pay_invoice")->where([['hpNo',"=",$hpNo],['is_del',"=",0]])->find();
-
-        if($payment==false){
-            return error_show(1005,"未找到回票申请信息");
-        }
-        $payinfo= Db::name("pay")->where([['payNo',"=",$payment['payNo']],['is_del',"=",0]])->find();
-
-       if($payinfo==false){
-            return error_show(1005,"未找到对账信息");
-        }
-        if($payinfo['status']!=2){
-            return error_show(1005,"对账信息未完成审核");
-        }
-         $cgdNo =Db::name("pay_info")->where(["payNo"=>$payment['payNo'],"is_del"=>0])->column("cgdNo");
-        if(empty($cgdNo))  return error_show(1004,"未找到对账数据信息");
-        if($payinfo['inv_fee']< $payment['inv_fee'])return error_show(1005,"对账回票申请金额有误,请确认回票申请金额");
-        $remark = $post['remark']??'';
+        $param = $this->request->param(["hpNo"=>"","status"=>"","remark"=>"","ItemRemark"=>[]],"post","trim");
+        $valid = Validate::rule(["hpNo|回票申请编号"=>"require|max:255","status"=>"require|number|in:1,2,3,4,6,7,8,13"]);
+        if(!$valid->check($param))$this->error($valid->getError());
+        $info = PayInvoice::where(["hpNo"=>$param['hpNo'],"is_del"=>0])->findOrEmpty();
+        if ($info->isEmpty())$this->error("未找到回票申请信息");
+        $payinfo = Pay::where(['payNo'=>$info['payNo'],"is_del"=>0])->findOrEmpty();
+        if ($payinfo->isEmpty())$this->error("未找到对账单信息");
+        if($payinfo['status']!=2)$this->error("对账信息未完成审核");
+        if($payinfo['inv_fee']< $info['inv_fee']) $this->error("回票金额不能大于对账单回票金额");
+        $invup =['remark'=>$param['remark'],'updatetime'=>date('Y-m-d H:i:s')];
+        if($param['status']==3) $invup['check_time']=date("Y-m-d H:i:s");
+        if(in_array($info['invoiceType'],['normal','electronic','toll','roll']) && $param['status']==3){
+            $param['status']=4; //普票无需认证
+        }
+        $invup['status']=$param['status'];
         Db::startTrans();
         try {
-        	Db::name('pay_invoice')->where([['hpNo','=',$hpNo],['is_del','=',0]])->lock(true)->find();
-        	$invup =["remark"=>$remark,"updatetime"=>date("Y-m-d H:i:s")];
-			if($status==3) $invup['check_time']=date("Y-m-d H:i:s");
-            if(in_array($payment['invoiceType'],['normal','electronic','toll','roll']) && $status==3){
-                $status=4; //普票无需认证
-            }
-            $invup['status']=$status;
-            $inv =Db::name("pay_invoice")->where($payment)->update($invup);
-            if($inv){
-                if($status==4){
-                      $payupdate =[
-                            "ainv_fee"=>$payinfo['ainv_fee']+$payment['inv_fee'],
-                            "inv_fee"=>$payinfo['inv_fee']-$payment['inv_fee'],
-                             "inv_status" => ($payinfo['inv_fee']-$payment['inv_fee'])==0 && $payinfo['winv_fee']==0?3:2,
-                             "updatetime" => date("Y-m-d H:i:s")
-                        ];
-                        $pay =Db::name("pay")->where($payinfo)->update($payupdate);
-                        if($pay==false){
-                            Db::rollback();
-                            return error_show(1003,"回票申请状态更新失败");
-                        }
+            $save=$info->save($invup);
+            if($save){
+                if($param['status']==4){
+                    Pay::addAinv($info['payNo'],$info->inv_fee);
                 }
-                if($status==6 || $status==5|| $status==7){
-                    $payupdate =[
-                            "winv_fee"=>$payinfo['winv_fee']+$payment['inv_fee'],
-                            "inv_fee"=>$payinfo['inv_fee']-$payment['inv_fee'],
-                             "inv_status" => ($payinfo['inv_fee']-$payment['inv_fee'])==0 && $payinfo['ainv_fee']==0?1:2,
-                             "updatetime" => date("Y-m-d H:i:s")
-                        ];
-                    $pay =Db::name("pay")->where($payinfo)->update($payupdate);
-                        if($pay==false){
-                            Db::rollback();
-                            return error_show(1003,"回票申请状态更新失败");
-                        }
-                    foreach ($cgdNo as $value){
-                       $report=ReportCode::where(["cgdNo"=>$value])->find();
-                       if($report)$report->rmField("hpNo",$hpNo);
-                    }
+                 if($param['status']==3){
+                   if (!empty($param['ItemRemark'])){
+                     (new InvoiceOrder)->saveAll($param['ItemRemark']);
+                    };
                 }
-                Db::commit();
-                return app_show(0,"回票申请更新成功");
-            }throw new \Exception("回票申请状态更新失败");
+                if(in_array($param['status'],[5,6,7,8,13])){
+                    Pay::subInv($info['payNo'],$info->inv_fee);
+                    if($param['status']==13||$param['status']==7)\app\admin\model\InvoiceItem::rmInvoice($param['hpNo']);
+                    event("reportCode",["param"=>["key"=>"hpNo","value"=>$param['hpNo']],"type"=>"sub"]);
+                }
+            }else throw new \Exception("回票审核失败");
+              Db::commit();
+              
         }catch (\Exception $e){
             Db::rollback();
-            return error_show(1004,$e->getMessage());
+           $this->error($e->getMessage());
         }
-
+        $this->success("回票审核成功");
     }
+//    public function invStatus(){
+//        $post = $this->post;
+//        $hpNo = isset($post['hpNo'])&& $post['hpNo']!="" ? trim($post['hpNo']) :"";
+//        if($hpNo==""){
+//            return error_show(1004,"参数 hpNo 不能为空");
+//        }
+//         $status = isset($post['status'])&& $post['status']!="" ? intval($post['status']) :"";
+//        if($status==""){
+//            return error_show(1004,"参数status 不能为空");
+//        }
+////        if(!in_array($status,[0,1,2,3,4,5,6,7,8,9,10,11,12])){
+////            return error_show(1004,"参数status 无效值");
+////        }
+//        if(!in_array($status,[0,1,2,3,4,5,6,7])){
+//            return error_show(1004,"参数status 无效值");
+//        }
+//        $payment= Db::name("pay_invoice")->where([['hpNo',"=",$hpNo],['is_del',"=",0]])->find();
+//
+//        if($payment==false){
+//            return error_show(1005,"未找到回票申请信息");
+//        }
+//        $payinfo= Db::name("pay")->where([['payNo',"=",$payment['payNo']],['is_del',"=",0]])->find();
+//
+//       if($payinfo==false){
+//            return error_show(1005,"未找到对账信息");
+//        }
+//        if($payinfo['status']!=2){
+//            return error_show(1005,"对账信息未完成审核");
+//        }
+//         $cgdNo =Db::name("pay_info")->where(["payNo"=>$payment['payNo'],"is_del"=>0])->column("cgdNo");
+//        if(empty($cgdNo))  return error_show(1004,"未找到对账数据信息");
+//        if($payinfo['inv_fee']< $payment['inv_fee'])return error_show(1005,"对账回票申请金额有误,请确认回票申请金额");
+//        $remark = $post['remark']??'';
+//        Db::startTrans();
+//        try {
+//        	Db::name('pay_invoice')->where([['hpNo','=',$hpNo],['is_del','=',0]])->lock(true)->find();
+//        	$invup =["remark"=>$remark,"updatetime"=>date("Y-m-d H:i:s")];
+//			if($status==3) $invup['check_time']=date("Y-m-d H:i:s");
+//            if(in_array($payment['invoiceType'],['normal','electronic','toll','roll']) && $status==3){
+//                $status=4; //普票无需认证
+//            }
+//            $invup['status']=$status;
+//            $inv =Db::name("pay_invoice")->where($payment)->update($invup);
+//            if($inv){
+//                if($status==4){
+//                      $payupdate =[
+//                            "ainv_fee"=>$payinfo['ainv_fee']+$payment['inv_fee'],
+//                            "inv_fee"=>$payinfo['inv_fee']-$payment['inv_fee'],
+//                             "inv_status" => ($payinfo['inv_fee']-$payment['inv_fee'])==0 && $payinfo['winv_fee']==0?3:2,
+//                             "updatetime" => date("Y-m-d H:i:s")
+//                        ];
+//                        $pay =Db::name("pay")->where($payinfo)->update($payupdate);
+//                        if($pay==false){
+//                            Db::rollback();
+//                            return error_show(1003,"回票申请状态更新失败");
+//                        }
+//                }
+//                if($status==6 || $status==5|| $status==7){
+//                    $payupdate =[
+//                            "winv_fee"=>$payinfo['winv_fee']+$payment['inv_fee'],
+//                            "inv_fee"=>$payinfo['inv_fee']-$payment['inv_fee'],
+//                             "inv_status" => ($payinfo['inv_fee']-$payment['inv_fee'])==0 && $payinfo['ainv_fee']==0?1:2,
+//                             "updatetime" => date("Y-m-d H:i:s")
+//                        ];
+//                    $pay =Db::name("pay")->where($payinfo)->update($payupdate);
+//                        if($pay==false){
+//                            Db::rollback();
+//                            return error_show(1003,"回票申请状态更新失败");
+//                        }
+//                    foreach ($cgdNo as $value){
+//                       $report=ReportCode::where(["cgdNo"=>$value])->find();
+//                       if($report)$report->rmField("hpNo",$hpNo);
+//                    }
+//                }
+//                Db::commit();
+//                return app_show(0,"回票申请更新成功");
+//            }throw new \Exception("回票申请状态更新失败");
+//        }catch (\Exception $e){
+//            Db::rollback();
+//            return error_show(1004,$e->getMessage());
+//        }
+//
+//    }
 
 
 
@@ -1659,8 +1712,9 @@ class Payment extends Base
             ->toArray();
         //校验是否开通了供应商账号
         $supp_account = check_has_account_by_supplierNos(array_unique(array_column($list,'supplierNo')));
+        $inv_fee=$inv_subtotal_amount = $total = 0;
         foreach ($list as &$value){
-            $invoinfo =Db::name("invoice_info")->where(["hpNo"=>$value['hpNo'],"status"=>1])->find();
+            $invoinfo =InvoiceInfo::where(["hpNo"=>$value['hpNo'],"status"=>1])->find();
             $value['buyer_name'] = $invoinfo['buyer_name']??"";
             $value['buyer_id'] = $invoinfo['buyer_id']??"";
             $value['buyer_address'] = $invoinfo['buyer_address']??"";
@@ -1678,22 +1732,29 @@ class Payment extends Base
             $value['remarks'] = $invoinfo['remarks']??"";
             $value['invoiceType_cn'] = $this->invoiceType[$value['invoiceType']]??'';
             $value['invStatus_cn'] = $this->invStatus[$value['invStatus']]??'';
-            $value['item_list'] = isset($invoinfo['item_list']) &&$invoinfo['item_list']!='' ?json_decode($invoinfo['item_list'],true):"";
+            $value['item_list'] = $invoinfo['item_list']??[];
             $value['has_account'] = (int)isset($supp_account['data'][$value['supplierNo']]);
+            if($value['status']==4){
+                $inv_fee += $value['inv_fee'];
+                $inv_subtotal_amount += $value['inv_subtotal_amount'];
+                $total += $value['total'];
+            }
+
         }
 
         //结算invlist这个接口当用payNo筛选的时候,返回列表里需要加一项统计筛选后列表的inv_subtotal_amount、total字段的和
-        $inv_subtotal_amount = $total = 0;
-        if ($payNo != '') {
-            $inv_subtotal_amount = round(array_sum(array_column($list, 'inv_subtotal_amount')), 2);
-            $total = round(array_sum(array_column($list, 'total')), 2);
-        }
+//       $inv_fee=$inv_subtotal_amount = $total = 0;
+//        if ($payNo != '') {
+//            $inv_subtotal_amount = round(array_sum(array_column($list, 'inv_subtotal_amount')), 2);
+//            $total = round(array_sum(array_column($list, 'total')), 2);
+//        }
 
         return app_show(0, "获取成功", [
             "count" => $count,
             "list" => $list,
             'inv_subtotal_amount' => $inv_subtotal_amount,
-            'total' => $total
+            'total' => $total,
+            "inv_fee"=>$inv_fee
         ]);
 
     }
@@ -1730,7 +1791,7 @@ class Payment extends Base
         if(empty($cgdNo))  return error_show(1004,"未找到对账数据信息");
         Db::startTrans();
         try{
-            $update=["is_del"=>1,"updatetime"=>date("Y-m-d H:i:s")];
+            $update=["status"=>8,"updatetime"=>date("Y-m-d H:i:s")]; //删除状态 流程终止
             $invup =Db::name("pay_invoice")->where($payinv)->update($update);
             if($invup){
                 if($payinv['status']==4){
@@ -1762,6 +1823,7 @@ class Payment extends Base
                      $payupdate =[
                             "inv_fee"=>$payinfo['inv_fee']-$payinv['inv_fee'],
                             "winv_fee"=>$payinfo['winv_fee']+$payinv['inv_fee'],
+                            'inv_status' => ($payinfo['ainv_fee']-$payinv['inv_fee'])==0?1:2,
                              "updatetime" => date("Y-m-d H:i:s")
                         ];
                         $pay =Db::name("pay")->where($payinfo)->update($payupdate);
@@ -1806,10 +1868,7 @@ class Payment extends Base
         $payinv['companyNo'] = $payinfo['companyNo']??'';
         $payinv['companyName'] = $payinfo['companyName']??'';
         $payinv['total_fee'] = $payinfo['total_fee']??'';
-        $invinfo =Db::name("invoice_info")->where(["hpNo"=>$hpNo])->findOrEmpty();
-        if(!empty($invinfo)){
-            $invinfo['item_list'] =json_decode($invinfo['item_list'],true);
-        }
+        $invinfo =InvoiceInfo::where(["hpNo"=>$hpNo])->findOrEmpty();
          $payinv['invoiceType_cn'] = $this->invoiceType[$payinv['invoiceType']]??'';
           $payinv['invStatus_cn'] = $this->invStatus[$payinv['invStatus']]??'';
         $payinv["info"]=$invinfo;
@@ -1844,10 +1903,7 @@ class Payment extends Base
 		$condition[]=["number","=",$num];
 		$invNo = isset($this->post['invNo'])&&$this->post['invNo']!=''?trim($this->post['invNo']):"";
 		if($invNo!=='')$condition[]=["hpNo","=",$invNo];
-		$info =Db::name("invoice_info")->where($condition)->findOrEmpty();
-			if(!empty($info)){
-			$info['item_list']=json_decode($info['item_list'],true);
-		}
+		$info =InvoiceInfo::where($condition)->findOrEmpty();
 		return empty($info)?error_show(1005,"为找到数据"):app_show(0,"获取成功",$info);
     }
 	/**
@@ -1860,12 +1916,10 @@ class Payment extends Base
     	$post=$this->request->only(["hpNo"=>"","page"=>1,"size"=>15],"post","trim");
     	$where=[];
     	if($post["hpNo"]!="") $where[]=["hpNo","=",$post['hpNo']];
-    	 $count=Db::name("invoice_info")->where($where)->count();
+    	 $count=InvoiceInfo::where($where)->count();
     	 $total =ceil($count/$post['size']);
     	 $page= $post['page']>$total ? intval($total):intval($post['page']);
-    	 $list = Db::name("invoice_info")
-    	 ->where($where)
-    	 ->json(["item_list"])
+    	 $list = InvoiceInfo::where($where)
     	 ->page($page,intval($post['size']))
     	 ->order("id desc")
     	 ->select()->toArray();
@@ -1978,24 +2032,6 @@ class Payment extends Base
 		if(empty($returninfo))return error_show(1004,"退票详情未找到");
 		Db::startTrans();
 		try{
-			if($param['status']==2){
-			$invinfo =Db::name("pay_invoice")->where(["hpNo"=>$returninfo['orderCode'],"is_del"=>0])->findOrEmpty();
-			if(empty($invinfo))throw new Exception("回票详情未找到");
-			$pay =Db::name("pay")->where(["payNo"=>$returninfo['payNo'],"is_del"=>0])->findOrEmpty();
-			if(empty($pay))throw new Exception("对账详情未找到");
-			$invup =Db::name("pay_invoice")->where($invinfo)->update(["status"=>10,"updatetime"=>date("Y-m-d H:i:s")]);
-			if($invup==false)throw new Exception("回票更新失败");
-
-			$paydata=[
-				"ainv_fee"=>$pay['ainv_fee']-$invinfo['inv_fee'],
-               "winv_fee"=>$pay['winv_fee']+$invinfo['inv_fee'],
-               "inv_status" => $pay['inv_fee']==0 &&($pay['ainv_fee']-$invinfo['inv_fee'])==0?1:2,
-               "updatetime" => date("Y-m-d H:i:s")
-			];
-			$oayup =Db::name("pay")->where($pay)->update($paydata);
-			if($oayup==false)throw new Exception("对账更新失败");
-
-		}
 		$update=[
 			"status"=>$param['status'],
 			"remark"=>$param['remark'],
@@ -2004,12 +2040,22 @@ class Payment extends Base
 			];
 		$up =Db::name("pay_return")->where($returninfo)->update($update);
 		if($up==false)throw new Exception("退票申请审核失败");
+		if($param['status']==2){
+            $invinfo =Db::name('pay_invoice')->where(['hpNo'=>$returninfo['orderCode'],'is_del'=>0])->findOrEmpty();
+            if(empty($invinfo))throw new Exception('回票详情未找到');
+            $pay =Db::name('pay')->where(['payNo'=>$returninfo['payNo'],'is_del'=>0])->findOrEmpty();
+            if(empty($pay))throw new Exception('对账详情未找到');
+            $invup =Db::name('pay_invoice')->where($invinfo)->update(['status'=>10,'updatetime'=>date('Y-m-d H:i:s')]);
+            if($invup==false)throw new Exception('回票更新失败');
+            Pay::subAinv($returninfo['payNo'],$invinfo['inv_fee']);
+            \app\admin\model\InvoiceItem::rmInvoice($returninfo['orderCode'],2);
+		}
 		Db::commit();
-		return app_show(0,"退票申请审核成功");
 		}catch (\Exception $e){
 			Db::rollback();
 			return error_show(1005,$e->getMessage());
 		}
+		$this->success("退票申请审核成功");
     }
 
      public function hpinvReturnInfo(){
@@ -2234,35 +2280,15 @@ class Payment extends Base
                 if ($value['invoice_fee'] > $payinfo['inv_fee']) throw new Exception("{$value['hpNo']}对账单开票中金额不足");
 
                 if ($val['status'] == 4) {
-                    //审核通过
-                    $payupdate = [
-                        "ainv_fee" => $payinfo['ainv_fee'] + $value['invoice_fee'],
-                        "inv_fee" => $payinfo['inv_fee'] - $value['invoice_fee'],
-                        "inv_status" => ($payinfo['inv_fee'] - $value['invoice_fee']) == 0 && $payinfo['winv_fee'] == 0 ? 3 : 2,
-                        "updatetime" => date("Y-m-d H:i:s")
-                    ];
-
+                    Pay::addAinv($payArr[$val['hpNo']]['payNo'],$value['invoice_fee']);
                     $pay_invoice_allow[] = $value['id'];
 
                 } else {
-                    //审核不通过
-                    $payupdate = [
-                        "winv_fee" => $payinfo['winv_fee'] + $value['invoice_fee'],
-                        "inv_fee" => $payinfo['inv_fee'] - $value['invoice_fee'],
-                        "inv_status" => ($payinfo['inv_fee'] - $value['invoice_fee']) == 0 && $payinfo['ainv_fee'] == 0 ? 1 : 2,
-                        "updatetime" => date("Y-m-d H:i:s")
-                    ];
-
+                   Pay::subInv($payArr[$val['hpNo']]['payNo'],$value['invoice_fee']);
                     Db::name("pay_invoice")
                         ->where(["id" => $value['id'], "status" => 3, "is_del" => 0])
                         ->update(["status" => $val['status'], "updatetime" => date("Y-m-d H:i:s"), 'remark' =>$val['remark']]);
                 }
-                $pay = Db::name("pay")->where($payinfo)->update($payupdate);
-
-                if ($pay == false) {
-                	throw new Exception("{$value['hpNo']}回票申请对账单状态更新失败");
-                }
-
             }
 
             if ($pay_invoice_allow) Db::name("pay_invoice")->where(["id" => $pay_invoice_allow, "status" => 3, "is_del" => 0])->update(["status" => 4, "updatetime" =>date("Y-m-d H:i:s")]);
@@ -2295,12 +2321,16 @@ class Payment extends Base
 		->where(["a.payNo"=>$param['payNo'],"a.status"=>1,"a.is_del"=>0])
 		->field("a.payNo'对账单号',b.sequenceNo '采购单编号',b.qrdCode '销售单编号',b.supplierName '供货商',b.companyName '业务公司',
 		b.goodUnit '单位',(b.goodNum-b.thNum) '商品数量',b.goodPrice'商品单价',b.totalPrice'总货款',
-		if(b.cxCode='',b.qrdCode,b.cxCode) 销售主单号,c.spuCode 商品编码,d.good_name 商品名称,c.cat_name 货物和劳务名称,c.merge_code 商品开票类目,c.short_name 商品和服务分类简称,
+		if(b.cxCode='',b.qrdCode,b.cxCode) 销售主单号,b.goodNo 商品编码,b.goodName 商品名称,c.cat_name 货物和劳务名称,c.merge_code 商品开票类目,c.short_name 商品和服务分类简称,
 		c.tax/100  开票税率,c.inv_good_name 开票商品名称,if(b.open_type =1,'原商品开票',if(b.open_type =2,'子商品开票','')) 开票类型")
-		->select()
-		->toArray();
-		if(empty($cgdlist))$cgdlist=["暂无数据"=>''];
+		->select()->toArray();
 
+		if(empty($cgdlist))$cgdlist=["暂无数据"=>''];
+		else{
+		    array_walk($cgdlist, function (&$item) {
+                $item['采购单可关联金额']= bcsub($item['总货款'],InvoiceOrder::getInvoiceOrderTotalFee($item['采购单编号']),2);
+            });
+		}
 		excelExport("{$param['payNo']}对账采购单详情",array_keys($cgdlist[0]),$cgdlist);
 	}
 		/** 导出对账单采购单信息

+ 2 - 2
app/admin/controller/Sale.php

@@ -395,8 +395,8 @@ class Sale extends Base{
         return app_show(0,"获取成功",$data);
     }
    /**
-* 更改销售单状态 是否需要回款 0 未回款对账 1 回款对账 2 无需汇款操作
- */
+    * 更改销售单状态 是否需要回款 0 未回款对账 1 回款对账 2 无需汇款操作
+    */
     public function status(){
      $post =$this->request->only(["sequenceNo"=>'',"status"=>0],"post","trim");
      if($post['sequenceNo']==''){

+ 30 - 31
app/admin/listener/ComonQrd.php

@@ -5,14 +5,14 @@ namespace app\admin\listener;
 
 use app\admin\model\Assoc;
 use app\admin\model\InvoiceInfo;
-use app\admin\model\InvoicePay;
+use app\admin\model\InvoiceItem;use app\admin\model\InvoicePay;
 use app\admin\model\InvoicePool;
 use app\admin\model\InvoiceTicket;
 use app\admin\model\Pay;
 use app\admin\model\PayInfo;
 use app\admin\model\PayInvoice;
 use app\admin\model\QrdInfo;
-use app\admin\model\TagLog;
+use app\admin\model\TagLog;use think\facade\Db;use think\helper\Str;
 class ComonQrd
 {
     /**
@@ -85,12 +85,11 @@ class ComonQrd
 		            		$qrd['pay_status'] = ($qrd['wpay_fee']==0 &&$item->pay_fee==0) ? 3:($qrd['apay_fee']==0?1:2);
 		            	}
 		            }else{
-
-							$tag_temp['tag_fee']= $payArr[$item->sequenceNo];
-							$tag_fee = ($item->pay_tag_fee>$payArr[$item->sequenceNo])?$payArr[$item->sequenceNo]:$item->pay_tag_fee;
-				            $qrd['pay_tag_fee']=$item->pay_tag_fee-$tag_fee;
-				            $qrd['wpay_fee'] = $item->wpay_fee+$tag_fee;
-				            $qrd['pay_status'] = ($qrd['wpay_fee']==0  &&$item->pay_fee==0) ? 3:($qrd['apay_fee']==0?2:1);
+                        $tag_temp['tag_fee']= $payArr[$item->sequenceNo];
+                        $tag_fee = ($item->pay_tag_fee>$payArr[$item->sequenceNo])?$payArr[$item->sequenceNo]:$item->pay_tag_fee;
+                        $qrd['pay_tag_fee']=$item->pay_tag_fee-$tag_fee;
+                        $qrd['wpay_fee'] = $item->wpay_fee+$tag_fee;
+                        $qrd['pay_status'] = ($qrd['wpay_fee']==0  &&$item->pay_fee==0) ? 3:($qrd['apay_fee']==0?2:1);
 		            }
 		            $qrdSave[]=$qrd;
 		            $tag[]=$tag_temp;
@@ -104,7 +103,8 @@ class ComonQrd
     	if($payinfo->isEmpty()) return;
     	if($payinfo->is_comon!=1)return;
     	if(!in_array($payinfo->status,[1,2]))return;
-    	$qrdNo = PayInfo::alias("a")->leftJoin("ComonOrder b","a.cgdNo=b.cgdNo")->where(["payNo"=>$data['payNo'],"is_del"=>0,"a.status"=>1])->column("orderCode");
+    	$qrdNo = PayInfo::alias("a")->leftJoin("ComonOrder b","a.cgdNo=b.cgdNo")
+    	->where(["payNo"=>$data['payNo'],"is_del"=>0,"a.status"=>1])->column("orderCode");
     	$qrdinfo = QrdInfo::where([["sequenceNo","in",$qrdNo],["wpay_fee",">",0]])->select();
     	if($qrdinfo->isEmpty())return;
     	$this->CheckComonOrder($payinfo);
@@ -130,7 +130,6 @@ class ComonQrd
     		$qrd=[
     			"id"=>$item->id,
 	            "wpay_fee"=>$item->wpay_fee,
-//	            "apay_fee"=>$item->apay_fee,
 	            "pay_status"=>$item->pay_status,
 	            "pay_tag_fee"=>0,
 	            "pay_tag" => 1
@@ -139,7 +138,6 @@ class ComonQrd
     			$tag_temp['tag_fee'] += $payfee;
     			$qrd['pay_tag_fee'] = $item->pay_tag_fee+$payfee;
     			$qrd['wpay_fee'] = ($item->wpay_fee>=$payfee)? $item->wpay_fee-$payfee:0;
-//    			$qrd['apay_fee'] = $item->apay_fee+$payfee;
     			$qrd['pay_status'] = ($qrd['wpay_fee']==0 &&$item->pay_fee==0) ? 3:($item->apay_fee==0?1:2);
     			$payfee=0;
     		}else{
@@ -147,7 +145,6 @@ class ComonQrd
     			$tag_temp['tag_fee'] +=$item->wpay_fee;
     			$qrd['pay_tag_fee'] = $item->pay_tag_fee+$item->wpay_fee;
     			$qrd['wpay_fee'] = 0;
-//    			$qrd['apay_fee'] = $item->apay_fee+$item->wpay_fee;
     			$qrd['pay_status'] = ($qrd['wpay_fee']==0 &&$item->pay_fee==0) ? 3:($item->apay_fee==0?1:2);
     		}
     		$qrdSave[]=$qrd;
@@ -178,7 +175,6 @@ class ComonQrd
              $qrd = [
                 'id'          => $item->id,
                 'wpay_fee'    => $item->wpay_fee,
-//                'apay_fee'    => $item->apay_fee,
                 'pay_status'  => $item->pay_status,
                 'pay_tag_fee' => 0,
                 'pay_tag'     => 1
@@ -189,14 +185,12 @@ class ComonQrd
                 $qrd['pay_tag_fee']  = $item->pay_tag_fee-$payfee;
                 $qrd['pay_tag']  =$qrd['pay_tag_fee']>0?1:0;
                 $qrd['wpay_fee']     = $item->wpay_fee + $payfee;
-//                $qrd['apay_fee']     = ($item->apay_fee>=$payfee)? $item->apay_fee-$payfee:0;
                 $qrd['pay_status']   = ($qrd['wpay_fee'] == 0 && $item->pay_fee == 0) ? 3 : ($item->apay_fee == 0 ? 1 : 2);
             } else {
                 $tag_temp['tag_fee']-= $item->pay_tag_fee;
                 $qrd['pay_tag_fee']  = 0;
                 $qrd['pay_tag']  = 0;
                 $qrd['wpay_fee']     = $item->wpay_fee +  $item->pay_tag_fee;
-//                $qrd['apay_fee']     = ($item->apay_fee>=$item->pay_tag_fee)? $item->apay_fee-$item->pay_tag_fee:0;
                 $qrd['pay_status']   = ($qrd['wpay_fee'] == 0 && $item->pay_fee == 0) ? 3 : ($item->apay_fee== 0 ? 1 : 2);
             }
             $qrdSave[] = $qrd;
@@ -222,13 +216,13 @@ class ComonQrd
         	if($payinvoice->isEmpty())return;
         	$payinvoice->status=10;
         	$payinvoice->save();
-        	 $payinfo->winv_fee = $payinfo->winv_fee+$payinvoice->inv_fee;
-        	 $payinfo->ainv_fee = $payinfo->ainv_fee-$payinvoice->inv_fee;
-        	 $payinfo->inv_status = ($payinfo->winv_fee==0 &&$payinfo->inv_fee==0)?3:($payinfo->ainv_fee==0?2:1);
-        	 $this->CheckComonOrder($payinfo);
-        	  $payinfo->save();
-        	  $invpay->status =2;
-        	  $invpay->save();
+         $payinfo->winv_fee = $payinfo->winv_fee+$payinvoice->inv_fee;
+         $payinfo->ainv_fee = $payinfo->ainv_fee-$payinvoice->inv_fee;
+         $payinfo->inv_status = ($payinfo->winv_fee==0 &&$payinfo->inv_fee==0)?3:($payinfo->ainv_fee==0?2:1);
+         $this->CheckComonOrder($payinfo);
+          $payinfo->save();
+          $invpay->status =2;
+          $invpay->save();
     }
 
      protected function checkPay($data){
@@ -273,14 +267,14 @@ class ComonQrd
     	if($payinfo->isEmpty()) return;
     	if($payinfo->is_comon!=1)return;
     	if(!in_array($payinfo->status,[1,2]))return;
-    	$ticket =InvoiceTicket::where(["invNo"=>$data['invNo'],"type"=>0])->json(['item'])->findOrEmpty();
+    	$ticket =InvoiceTicket::where(["invNo"=>$data['invNo'],"type"=>0])->findOrEmpty();
     	if($ticket->isEmpty())return;
     	$pay_fee=0;
-    	    	if($payinfo->winv_fee >=$invinfo->inv_value){
-    	    		$pay_fee = $invinfo->inv_value;
-    	    	}else{
-    	    		$pay_fee = $payinfo->winv_fee;
-    	    	}
+    	if($payinfo->winv_fee >=$invinfo->inv_value){
+    	  $pay_fee = $invinfo->inv_value;
+    	}else{
+    	   $pay_fee = $payinfo->winv_fee;
+    	}
     	$payinvoice = [
     		"payNo"=>$invpay['payNo'],
     		"hpNo"=>makeNo("hp"),
@@ -318,7 +312,7 @@ class ComonQrd
     	    		    $temp['unit_price']=$item['DJ'];
     	    		    $temp['amount']=$item['JE'];
     	    		    $temp['tax']=$item['SE'];
-    	    		    $temp['tax_rate']=strval($item['SL']*100)."%";
+    	    		    $temp['tax_rate']= Str::contains($item['SL'],"%")?$item['SL']:strval($item['SL']*100)."%" ;
     				    $itemArr[]=$temp;
     			    }
     	}
@@ -341,13 +335,18 @@ class ComonQrd
     			    'subtotal_amount'=>$ticket->inv_subtotal ,
     			    'subtotal_tax'   =>$ticket->tax_fee ,
     			    'total'          =>$ticket->inv_total ,
-    			    'item_list'      =>json_encode($itemArr,JSON_UNESCAPED_UNICODE) ,
+    			    'item_list'      =>$itemArr,
     			    'receiver'       =>$ticket->reciver ,
     			    'issuer'         =>$ticket->issuer ,
     			    'reviewer'       =>$ticket->reviewer ,
     			    'change_field'   =>'' ,
+    			    "status"=>1,
     		    ];
-    		    InvoiceInfo::create($info);
+    	        InvoiceInfo::create($info);
+//    	        $invoiceItem = InvoiceItem::where(["invoiceCode"=>$data['invNo'],"status"=>[1,2]])->select();
+//    	        if(!$invoiceItem->isEmpty()){
+//    	            InvoiceItem::CopyItem($data['invNo'],$payinvoice['hpNo']);
+//    	        }
     		    $invpay->hpNo = $payinvoice['hpNo'];
     		    $invpay->status =1;
     		    $invpay->save();

+ 45 - 0
app/admin/listener/ReportCode.php

@@ -0,0 +1,45 @@
+<?php
+declare (strict_types = 1);
+
+namespace app\admin\listener;
+
+class ReportCode
+{
+    protected $model;
+    /**
+     * 事件监听处理
+     *
+     * @return mixed
+     */
+    public function handle($event)
+    {
+        $this->model = new \app\admin\model\ReportCode();
+        if($event['type']=='add'){
+
+            $this->addField($event['param']);
+        }
+        if ($event['type']=='sub'){
+            $this->subField($event['param']);
+        }
+    }
+
+    private function subField($param){
+        if(is_string($param['value'])){
+            $this->model->whereFindInSet($param['key'],$param['value'])->select()->each(function($item)use($param){
+                $item->rmField($param['key'],$item[$param['key']]);
+            });
+        }
+        if(is_array($param['value'])){
+           foreach ($param['value'] as $v){
+              $this->model->whereFindInSet($param['key'],$v)->select()->each(function($item)use($param){
+                              $item->rmField($param['key'],$item[$param['key']]);
+                          });
+           }
+        }
+    }
+
+    private function addField($param){
+        $info= $this->model->where([$param['main']['key']=>$param['main']['value']])->find()->setField($param['key'],$param['value']);
+        $this->model->save();
+    }
+}

+ 73 - 1
app/admin/model/Assoc.php

@@ -10,5 +10,77 @@ use think\Model;
  */
 class Assoc extends Model
 {
-    //
+    protected $createTime="addtime";
+    protected $updateTime="updatetime";
+
+    public static function subOrder($invNo)
+    {
+        $list = self::where(["viceCode"=>$invNo,"status"=>1,"type"=>"1"])->field("id,orderCode,cancel_fee")->select();
+        if(!$list->isEmpty()){
+            $order= QrdInfo::whereIn("sequenceNo",$list->column("orderCode"))
+            ->column("id,sequenceNo,winv_fee,inv_fee,ainv_fee,invtime,inv_status,status,pay_status","sequenceNo");
+            foreach ($list as $k=>$v){
+                if(!isset($order[$v['orderCode']])) throw new \Exception("订单{$v['orderCode']}未找到数据");
+                if($v['cancel_fee']>0){
+                    if($v['cancel_fee']>$order[$v['orderCode']]['inv_fee']){
+                        throw new \Exception("订单{$v['orderCode']}开票金额{$v['cancel_fee']}超过实际开票中金额{$order[$v['orderCode']]['inv_fee']}");
+                    }
+                    $order[$v['orderCode']]['inv_fee']-=$v['cancel_fee'];
+                    $order[$v['orderCode']]['ainv_fee']+=$v['cancel_fee'];
+                    $order[$v['orderCode']]['invtime']=date("Y-m-d H:i:s");
+                    $order[$v['orderCode']]['inv_status']= $order[$v['orderCode']]['winv_fee']==0 &&$order[$v['orderCode']]['inv_fee']==0 ? 3 : 2;
+                    $order[$v['orderCode']]['status']= $order[$v['orderCode']]['pay_status']==0 &&$order[$v['orderCode']]['inv_status']==0 ? 0 : 1;
+                }
+            }
+           (new QrdInfo())->saveAll($order);
+            self::where(["id"=>$list->column('id')])->save(['status'=>2,'assoc_time'=>date("Y-m-d H:i:s")]);
+        }
+    }
+    public static function addOrder($invNo)
+    {
+        $list = self::where(["viceCode"=>$invNo,"status"=>1,"type"=>"1"])->field('id,orderCode,cancel_fee')->select();
+        if(!$list->isEmpty()){
+            $order= QrdInfo::whereIn('sequenceNo',$list->column('orderCode'))
+            ->column('id,sequenceNo,winv_fee,inv_fee,ainv_fee,invtime,inv_status,status,pay_status','sequenceNo');
+            foreach ($list as $k=>$v){
+                if(!isset($order[$v['orderCode']])) throw new \Exception("订单{$v['orderCode']}未找到数据");
+                if($v['cancel_fee']>0){
+                    if($v['cancel_fee']>$order[$v['orderCode']]['inv_fee']){
+                        throw new \Exception("订单{$v['orderCode']}开票金额{$v['cancel_fee']}超过实际开票中金额{$order[$v['orderCode']]['inv_fee']}");
+                    }
+                    $order[$v['orderCode']]['inv_fee']-=$v['cancel_fee'];
+                    $order[$v['orderCode']]['winv_fee']+=$v['cancel_fee'];
+                    $order[$v['orderCode']]['inv_status']= $order[$v['orderCode']]['ainv_fee']==0 &&$order[$v['orderCode']]['inv_fee']==0 ? 1 : 2;
+                    $order[$v['orderCode']]['status']= $order[$v['orderCode']]['pay_status']==0 &&$order[$v['orderCode']]['inv_status']==0 ? 0 : 1;
+                }
+            }
+           (new QrdInfo())->saveAll($order);
+            self::where(['id'=>$list->column('id')])->save(['status'=>3]);
+            InvoiceGood::where(['is_del' => 0,'invNo' => $invNo,'orderCode' => $list->column('orderCode')])->save(["goodNum"=>0,"updatetime"=>date("Y-m-d H:i:s")]);
+        }
+    }
+    //开票流程已走完 取消发票 从已开票中直接扣除到未开票中
+    public static function addOrderWinv($invNo)
+    {
+        $list = self::where(['viceCode'=>$invNo,'status'=>2,'type'=>'1'])->field('id,orderCode,cancel_fee')->select();
+        if(!$list->isEmpty()){
+            $order= QrdInfo::whereIn('sequenceNo',$list->column('orderCode'))
+            ->column('id,sequenceNo,winv_fee,inv_fee,ainv_fee,invtime,inv_status,status,pay_status','sequenceNo');
+            foreach ($list as $k=>$v){
+                if(!isset($order[$v['orderCode']])) throw new \Exception("订单{$v['orderCode']}未找到数据");
+                if($v['cancel_fee']>0){
+                    if($v['cancel_fee']>$order[$v['orderCode']]['ainv_fee']){
+                        throw new \Exception("订单{$v['orderCode']}开票金额{$v['cancel_fee']}超过实际开票金额{$order[$v['orderCode']]['ainv_fee']}");
+                    }
+                    $order[$v['orderCode']]['ainv_fee']-=$v['cancel_fee'];
+                    $order[$v['orderCode']]['winv_fee']+=$v['cancel_fee'];
+                    $order[$v['orderCode']]['inv_status']= $order[$v['orderCode']]['ainv_fee']==0 &&$order[$v['orderCode']]['inv_fee']==0 ? 1 : 2;
+                    $order[$v['orderCode']]['status']= $order[$v['orderCode']]['pay_status']==0 &&$order[$v['orderCode']]['inv_status']==0 ? 0 : 1;
+                }
+            }
+           (new QrdInfo())->saveAll($order);
+            self::where(['id'=>$list->column('id')])->save(['status'=>3]);
+            InvoiceGood::where(['is_del' => 0,'invNo' => $invNo,'orderCode' => $list->column('orderCode')])->save(['goodNum'=>0,'updatetime'=>date('Y-m-d H:i:s')]);
+        }
+    }
 }

+ 4 - 1
app/admin/model/CompanyInfo.php

@@ -10,5 +10,8 @@ use think\Model;
  */
 class CompanyInfo extends Model
 {
-    //
+
+    public static function getRegisterCode($code){
+       return CompanyInfo::where(['companyNo'=>$code])->value("company_license","");
+    }
 }

+ 33 - 2
app/admin/model/InvoiceInfo.php

@@ -3,12 +3,43 @@ declare (strict_types = 1);
 
 namespace app\admin\model;
 
-use think\Model;
+use think\facade\Log;use think\Model;
 
 /**
  * @mixin \think\Model
  */
 class InvoiceInfo extends Model
 {
-    //
+    protected $updateTime="updatetime";
+    public function getItemListAttr($value){
+        return json_decode($value,true);
+    }
+    public function setItemListAttr($value){
+        return json_encode($value,JSON_UNESCAPED_UNICODE);
+    }
+    public static function onAfterWrite(Model $model){
+        log::info("InvoiceInfo::OnAfterWrite".json_encode($model->toArray()));
+        $pay = Pay::where(["payNo"=>$model->payNo])->findOrEmpty();
+        if(!$pay->isEmpty() && $pay->is_comon!=1){
+            if($model->status==1 && !empty($model->item_list)){
+                          (new  InvoiceItem)->saveAll(array_map(function ($item)use($model) {
+                              return [
+                                  'invoiceCode'=>$model->hpNo,
+                                  'order_type'=>2,
+                                  'good_name'=>$item['name'],
+                                  'unit'=>$item['unit'],
+                                  'num'=>$item['quantity'],
+                                  'unit_price'=>$item['unit_price'],
+                                  'amount'=>$item['amount'],
+                                  'tax_amount'=>$item['tax'],
+                                  'tax'=>$item['tax_rate'],
+                                  'cat_code'=>$item['license_plate_number'],
+                                  'total_amount'=>bcadd($item['amount'],$item['tax'],2),
+                                  'balance_amount'=>bcadd($item['amount'],$item['tax'],2),
+                              ];
+                          },$model->item_list));
+                       }
+        }
+
+   }
 }

+ 100 - 0
app/admin/model/InvoiceItem.php

@@ -0,0 +1,100 @@
+<?php
+
+
+namespace app\admin\model;
+
+
+use think\facade\Log;use think\model\concern\SoftDelete;
+class InvoiceItem extends \think\Model {
+    use SoftDelete;
+    protected $createTime="createTime";
+    protected $updateTime="updateTime";
+    protected $autoWriteTimestamp=true;
+    protected $deleteTime="delete_time";
+
+    public function OrderInfo(){
+        return $this->hasMany(InvoiceOrder::class,"itemId","id")->whereIn('status', [1, 2]);
+    }
+
+    public static function getNumAttr($val){
+        return  strval(floatval($val));
+    }
+
+    public static function setNumAttr($val){
+        return  floatval($val);
+    }
+    public static function onAfterUpdate($model){
+        $code=$model->invoiceCode;
+        $orderType=$model->order_type;
+        $change = $model->getChangedData();
+        Log::info("修改发票明细状态:".json_encode($change,JSON_UNESCAPED_UNICODE));
+        Log::info("跟后数据:".json_encode($model->toArray(),JSON_UNESCAPED_UNICODE));
+        if(isset($change['status'])&&in_array($change["status"],[0,1,2])){
+             Log::info('跟后数据:'.json_encode($model->toArray(),JSON_UNESCAPED_UNICODE));
+             $num = self::where(['invoiceCode'=>$code,'order_type'=>$orderType,'status'=>0])->count();
+//                if($orderType==1){
+//                      $info= InvoicePool::where(['invNo'=>$code,'is_del'=>0])->findOrEmpty();
+//                      if(!$info->isEmpty()){
+//                          if($num==0 && $info->status==10){
+//                               $info->status=11;
+//                          }
+//                          if($num>0 && $info->status==11){
+//                              $info->status=10;
+//                          }
+//                          $info->save();
+//                      }
+//                }
+                if($orderType==2){
+                    $info= PayInvoice::where(['hpNo'=>$code,'is_del'=>0])->findOrEmpty();
+                     Log::info('跟后数据:'.json_encode( $info->toArray(),JSON_UNESCAPED_UNICODE));
+                    if(!$info->isEmpty()){
+                        $pay = Pay::where(['payNo'=>$info->payNo,'is_del'=>0])->findOrEmpty();
+                          Log::info('跟后数据:'.json_encode( $pay->toArray(),JSON_UNESCAPED_UNICODE));
+                        if(!$pay->isEmpty() && $pay->status==2){
+                                   if($num==0 && $info->status==11){
+                                           $info->status=12;
+                                      }
+                                      if($num>0 && $info->status==12){
+                                          $info->status=11;
+                                      }
+                                      $info->save();
+                        }
+                    }
+                }
+        }
+    }
+
+    public static function rmInvoice($code,$orderType=2){
+        $items = self::where(['invoiceCode'=>$code,'order_type'=>$orderType])->select();
+       if(!empty($items)){
+            $orderItems=InvoiceOrder::whereIn('itemId',$items->column('id'))->select();
+            if(!empty($orderItems)){
+               InvoiceOrder::whereIn('itemId',$items->column('id'))->save(["status"=>3]);
+            }
+            self::where(['invoiceCode'=>$code,'order_type'=>$orderType])->save(["status"=>3]);
+       }
+    }
+
+    public static function CopyItem($invNo,$hpNo){
+        $info = self::where(['invoiceCode'=>$invNo,'order_type'=>1])->select();
+        if(!$info->isEmpty()){
+            foreach($info as $item){
+                $temp = $item->toArray();
+                $temp['invoiceCode']=$hpNo;
+                $temp['order_type']=2;
+                unset($temp['id']);
+               $up = self::create($temp);
+               if($up->id>0){
+                   $orders = InvoiceOrder::where(['itemId'=>$item->id,"status"=>[1,2]])->select();
+                   if(!$orders->isEmpty()){
+                      InvoiceOrder::create(array_map(function ($items)use ($up){
+                          $items['itemId']=$up->id;
+                          unset($items['id']);
+                          return $items;
+                      },$orders->toArray()));
+                   }
+               }
+            }
+        }
+    }
+}

+ 76 - 0
app/admin/model/InvoiceOrder.php

@@ -0,0 +1,76 @@
+<?php
+
+
+namespace app\admin\model;
+
+
+use think\model\concern\SoftDelete;
+class InvoiceOrder extends \think\Model{
+
+    use SoftDelete;
+    protected $createTime='createTime';
+    protected $updateTime='updateTime';
+    protected $autoWriteTimestamp=true;
+    protected $deleteTime='delete_time';
+    protected $append=["merge_code","short_name","cat_code","cat_name","tax","inv_good_name"];
+    public function ItemInfo(){
+        return $this->hasOne('InvoiceItem','itemId','id');
+    }
+    public static function getInvoiceOrderTotalFee($code){
+        $banance =  (new InvoiceOrder)->where(['code'=>$code,'status'=>[1,2]])->field('sum(ifnull(total_amount-balance_amount,0)) as total_amount')->find();
+        return $banance['total_amount']??"0";
+    }
+
+    public function getMergeCodeAttr($value,$data){
+        return OrderCategory::where(['code'=>$data['code'],'spuCode'=>$data['spuCode']])->value("merge_code","");
+    }
+    public function getShortNameAttr($value,$data){
+        return OrderCategory::where(['code'=>$data['code'],'spuCode'=>$data['spuCode']])->value("short_name","");
+    }
+    public function getCatCodeAttr($value,$data){
+        return OrderCategory::where(['code'=>$data['code'],'spuCode'=>$data['spuCode']])->value("cat_code","");
+    }
+    public function getCatNameAttr($value,$data){
+        return OrderCategory::where(['code'=>$data['code'],'spuCode'=>$data['spuCode']])->value("cat_name","");
+    }
+
+    public function getTaxAttr($value,$data){
+        return OrderCategory::where(['code'=>$data['code'],'spuCode'=>$data['spuCode']])->value("tax","");
+    }
+    public function getInvGoodNameAttr($value,$data){
+        return OrderCategory::where(['code'=>$data['code'],'spuCode'=>$data['spuCode']])->value("inv_good_name","");
+    }
+     public static function getNumAttr($val){
+        return  strval(floatval($val));
+    }
+
+    public static function setDiffInfoAttr($val){
+        return  json_encode($val,JSON_UNESCAPED_UNICODE);
+    }
+    public static function getDiffInfoAttr($val){
+        return  json_decode($val,true);
+    }
+
+    public static function getOrderInfo($code,$orderCode,$type){
+       if($type==1){
+           $info=InvoiceGood::alias('a')
+                ->join('qrd_info b','a.orderCode=b.sequenceNo','left')
+                ->join('order_category c','b.sequenceNo=c.code and b.goodNo=c.spuCode and  c.order_type=1','left')
+                ->where(["invNo"=>$code,"a.orderCode"=>$orderCode])
+                ->where([['a.goodNum','>',0],['a.is_del','=',0]])
+                ->field('b.sequenceNo,b.goodNo,b.goodName,a.goodNum,a.goodPrice,a.totalPrice,c.merge_code,c.cat_code,c.cat_name,c.short_name,c.tax,c.inv_good_name')
+                ->findOrEmpty();
+          } else{
+           $payNo = PayInvoice::where(['hpNo'=>$code])->value("payNo","");
+           $info = PayInfo::alias('a')
+                ->join('cgd_info b','a.cgdNo=b.sequenceNo','left')
+                ->join('order_category c','b.sequenceNo=c.code and b.goodNo=c.spuCode and c.order_type=2','left')
+                ->where([['a.status','=',1],['a.is_del','=',0],['a.payNo',"=",$payNo ],['a.cgdNo',"=",$orderCode]])
+                ->field('b.sequenceNo,b.goodNo,b.goodName,(b.goodNum-thNum) as goodNum,b.goodPrice,b.totalPrice,
+                c.merge_code,c.cat_code,c.cat_name,c.short_name,c.tax,c.inv_good_name')
+                ->findOrEmpty();
+        }
+       if(!$info->isEmpty())$info['balance_amount']= bcsub($info['totalPrice'],InvoiceOrder::getInvoiceOrderTotalFee($info['sequenceNo']),2);
+       return $info;
+    }
+}

+ 29 - 0
app/admin/model/InvoicePool.php

@@ -10,8 +10,37 @@ use think\Model;
  */
 class InvoicePool extends Model
 {
+     // 1 财务审核 2 待财务上传发票 3 金税开票中/验票中 4 开票成功  5 开票失败 6发票退票/废弃 7取消申请 8 财务驳回 9验票失败
+     // 10 待发票关联订单 11 发票关联订单完毕待财务审核 12 发票关联订单财务驳回
+
+    public static $statusCn = [
+        0=>"待开票",
+        1=>"待财务审核",
+        2=>"待财务上传发票",
+        3=>"验票中",
+        10=>'待发票关联订单',
+        11=>'发票关联订单完毕待财务审核',
+        4=>"开票完成",//待发票关联订单审核通过
+        5=>"开票失败",
+        6=>"发票退票",
+        7=>"取消申请",
+        8=>"财务驳回",
+        9=>"验票失败",
+        12=>"发票关联订单财务驳回"
+     ];
 
     public function PoolInfo(){
         return $this->belongsTo(InvoicePoolInfo::class,'invNo','invNo')->bind(["buyer_code","seller_code"]);
     }
+
+    public static function onAfterUpdate(Model $model) : void{
+        if($model->status==4 && $model->buyer_check==0 && $model->seller_check==0){
+            $ticket = InvoiceTicket::where(["invNo"=>$model->invNo,"status"=>1,"is_del"=>0,"type"=>0])->findOrEmpty();
+            if(!$ticket->isEmpty()){
+                $buyer_check = $model->PoolInfo->buyer_code == $ticket->buyer_id?1:2;
+                $seller_check = $model->PoolInfo->seller_code == $ticket->seller_id?1:2;
+                $model->save(["buyer_check"=>$buyer_check,"seller_check"=>$seller_check]);
+            }
+        }
+    }
 }

+ 9 - 0
app/admin/model/InvoiceReturn.php

@@ -0,0 +1,9 @@
+<?php
+
+
+namespace app\admin\model;
+
+
+class InvoiceReturn extends \think\Model{
+
+}

+ 8 - 2
app/admin/model/InvoiceTicket.php

@@ -3,12 +3,18 @@ declare (strict_types = 1);
 
 namespace app\admin\model;
 
-use think\Model;
+use think\facade\Log;use think\Model;
 
 /**
  * @mixin \think\Model
  */
 class InvoiceTicket extends Model
 {
-    //
+    public function getItemAttr($v){
+        return json_decode($v,true);
+    }
+    public function setItemAttr($v){
+      return json_encode($v,JSON_UNESCAPED_UNICODE);
+    }
+
 }

+ 3 - 3
app/admin/model/OrderCategory.php

@@ -24,11 +24,11 @@ class OrderCategory extends Model{
                       "code"=>$code,
                       "order_type"=>$orderType,
                       "spuCode"=>$value['spuCode'],
-                      "cat_code"=>$value['cat_code'],
-                      "cat_name"=>$value['cat_name'],
+                      "cat_code"=>$value['cat_code']??"",
+                      "cat_name"=>$value['cat_name']??"",
                       "short_name"=>$value['short_name']??"",
                       "merge_code"=>$value['merge_code']??"",
-                      "tax"=>$value['tax'],
+                      "tax"=>$value['tax']??"",
                       "inv_good_name"=>$value['inv_good_name']
                       ];
                   $save[]=$Temp;

+ 42 - 2
app/admin/model/Pay.php

@@ -10,7 +10,47 @@ use think\Model;
  */
 class Pay extends Model
 {
-	protected $createTime='addtime';
-    protected $updateTime='updatetime';
+	protected $createTime="addtime";
+    protected $updateTime="updatetime";
     public static $status=["待提交","待买方审核","审核通过","审核驳回","对账单无效"];
+
+    public static function addInv($payNo,$payfee){
+        $pay = self::where(["payNo"=>$payNo,"status"=>2,"is_del"=>0])->findOrEmpty();
+        if($pay->isEmpty())throw new \Exception("对账单{$payNo}不存在");
+        if($pay->winv_fee<$payfee)throw new \Exception("对账单{$payNo}未开票金额不足");
+        $pay->winv_fee=$pay->winv_fee-$payfee;
+        $pay->inv_fee=$pay->inv_fee+$payfee;
+        $pay->inv_status= 2;
+        $pay->save();
+    }
+
+    public static function addAinv($payNo,$payfee){
+        $pay = self::where(["payNo"=>$payNo,"status"=>2,"is_del"=>0])->findOrEmpty();
+        if($pay->isEmpty())throw new \Exception("对账单{$payNo}不存在");
+        if($pay->inv_fee<$payfee)throw new \Exception("对账单{$payNo}开票中金额不足");
+        $pay->inv_fee=$pay->inv_fee-$payfee;
+        $pay->ainv_fee=$pay->ainv_fee+$payfee;
+        $pay->inv_status= $pay->winv_fee==0 && $pay->inv_fee==0 ? 3 : 2;
+        $pay->save();
+    }
+
+    public static function subAinv($payNo,$payfee){
+        $pay = self::where(["payNo"=>$payNo,"status"=>2,"is_del"=>0])->findOrEmpty();
+        if($pay->isEmpty())throw new \Exception("对账单{$payNo}不存在");
+        if($pay->ainv_fee<$payfee)throw new \Exception("对账单{$payNo}开票金额不足");
+        $pay->ainv_fee=$pay->ainv_fee-$payfee;
+        $pay->winv_fee=$pay->winv_fee+$payfee;
+        $pay->inv_status= $pay->ainv_fee==0 && $pay->inv_fee==0 ? 1 : 2;
+        $pay->save();
+    }
+
+    public static function subInv($payNo,$payfee){
+        $pay = self::where(["payNo"=>$payNo,"status"=>2,"is_del"=>0])->findOrEmpty();
+        if($pay->isEmpty())throw new \Exception("对账单{$payNo}不存在");
+        if($pay->inv_fee<$payfee)throw new \Exception("对账单{$payNo}开票中金额不足");
+        $pay->inv_fee=$pay->inv_fee-$payfee;
+        $pay->winv_fee=$pay->winv_fee+$payfee;
+        $pay->inv_status= $pay->ainv_fee==0 && $pay->inv_fee==0 ? 1 : 2;
+        $pay->save();
+    }
 }

+ 16 - 0
app/admin/model/PayInvoice.php

@@ -12,4 +12,20 @@ class PayInvoice extends Model
 {
     //
     public static $invStatus=[0=>'待验证','1'=>'正常','2'=>'已作废','3'=>'已红冲'];
+   // 1待系统验证 2 买方公司审核 3带买方公司认证 4 认证成功 5验证失败 6买方审核驳回 7 认证失败 8 回票流程终止 9 验证超次数 10 回票已退 11 待关联订单 12 订单关联完毕待财务审核 13 订单关联完毕财务驳回
+    public static $statusCn=[
+            "1"=>"待系统验证",
+            '11'=>'待关联订单',
+            '12'=>'订单关联完毕待财务审核',
+            "2"=>"买方公司审核",//订单关联完毕财务通过
+            "3"=>"待买方公司认证",
+            "4"=>"认证成功",
+            "5"=>"验证失败",
+            "6"=>"买方审核驳回",
+            "7"=>"认证失败",
+            "8"=>"回票流程终止",
+            "9"=>"验证超次数",
+            "10"=>"回票已退",
+            '13'=>'订单关联完毕财务驳回'
+        ];
 }

+ 13 - 4
app/admin/model/ReportCode.php

@@ -11,15 +11,24 @@ use think\Model;
 class ReportCode extends Model
 {
 
-    public  function setField($filed,$vale){
+    public function setField($filed,$vale){
         $arr= explode(",",$this->$filed);
        if(!in_array($vale,$arr)) $this->$filed == '' ? $this->$filed =$vale : $this->$filed .=",".$vale;
     }
     public function rmField($filed,$vale){
           $arr= explode(",",$this->$filed);
-          if(in_array($vale,$arr)){
-            $key = array_search($vale,$arr);
-            unset($arr[$key]);
+          if(is_array($vale)){
+              foreach ($vale as $v){
+                  if(in_array($v,$arr)){
+                      $key = array_search($v,$arr);
+                      unset($arr[$key]);
+                  }
+              }
+          }else{
+              if(in_array($vale,$arr)){
+                  $key = array_search($vale,$arr);
+                  unset($arr[$key]);
+              }
           }
          $this->$filed=implode(",",$arr);
     }

+ 3 - 1
app/admin/model/SupplierInfo.php

@@ -10,5 +10,7 @@ use think\Model;
  */
 class SupplierInfo extends Model
 {
-    //
+    public static function getRegisterCode($code){
+        return SupplierInfo::where(['code'=>$code])->value("registercode","");
+    }
 }

+ 0 - 2
app/command/Report.php

@@ -1333,8 +1333,6 @@ class Report extends Command
 		$list =$Db->name('good_basic')->alias('a')
 		->leftJoin('good_stock b','a.spuCode = b.spuCode')
 		->leftJoin('warehouse_info c','b.wsm_code = c.wsm_code')
-//		->leftJoin('purchease_order d','d.spuCode = a.spuCode  and d.wsm_code = b.wsm_code AND d.is_del = 0')
-//		->leftJoin('purchease e','e.bk_code = d.bkcode AND e.is_del = 0 ')
 		->field("a.spuCode 产品编码,
 			a.good_name 商品名称,
 			0 单日订单发货数量,

+ 8 - 2
app/command/handleCxData.php

@@ -291,6 +291,7 @@ class handleCxData extends command
 							    'sendStatus'=>$data['send_status'] ,//发货状态,
 							    'pay_status'=>$data['total_price'] == 0 ? 3 : 1 ,//收款状态,1未付,2部分,3完结
 							    'inv_status'=>$data['total_price'] == 0 ? 3 : 1 ,//开票状态,,1未付,2部分,3完结
+							    "status"=>$this->GetStatus($data['platform_id'],$data['order_source'],$data['supplierNo']),
 							    'remark'=>'' ,//备注,
 							    'cxCode'=>$data['oldCode'] ?? '' ,//备注,
 							    'diff_weight'=>$data['diff_weight'] ,//工差重量,
@@ -309,7 +310,6 @@ class handleCxData extends command
 							    'pay_tag'=>0 ,//回款标签,
 							    'inv_tag_fee'=>0 ,//开票票标签金额,
 							    'pay_tag_fee'=>0 ,//回款标签金额,
-
 						    ];
 					    }
 						if($data['cgdNo']!=''){
@@ -566,6 +566,7 @@ class handleCxData extends command
 							    'remark'=>'' ,//备注',
 							    'pay_status'=>$data['total_fee'] == 0 ? 3 : 1 ,//付款情况',
 							    'inv_status'=>$data['total_fee'] == 0 ? 3 : 1 ,//开票情况',
+							     'status'=>$this->GetStatus($data['platform_id']??0,$data['order_source'],$data['companyNo']),
 							    'diff_weight'=>$data['diff_weight'] ,//工差重量',
 							    'diff_fee'=>$data['diff_fee'] ,//工差金额',
 							    'thNum'=>$data['th_num'] ,//退货数量',
@@ -605,5 +606,10 @@ class handleCxData extends command
 	    Cache::store('redis')->set('JsHandle',0,180);
 		Db::rollback();
     }
-    
+
+
+    private function GetStatus($platform_id,$source,$companyNo){
+        //元隆 采销导入渠道 结算单E企购 平台数据导入
+       return ($platform_id == 77 && $source == 9 && $companyNo =='GS2404151642335170')?2:0;
+    }
 }

+ 2 - 1
app/event.php

@@ -12,7 +12,8 @@ return [
         'LogLevel' => [],
         'LogWrite' => [],
         "rolesave"=>[\app\listener\roleListen::class],
-        "comonOrder"=>[\app\admin\listener\ComonQrd::class]
+        "comonOrder"=>[\app\admin\listener\ComonQrd::class],
+        "reportCode"=>[\app\admin\listener\ReportCode::class],
     ],
 
     'subscribe' => [

Некоторые файлы не были показаны из-за большого количества измененных файлов