wugg 6 miesięcy temu
rodzic
commit
45dadfd5b3

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

@@ -0,0 +1,152 @@
+<?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\Config;
+
+class CheckInvoice extends Command
+{
+    protected $datetime;
+    protected $InvoiceNum;
+//   self.taxStatus={'Y':'2','N':'1','H':'3'}
+    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'];
+        while ($list->valid()){
+             $invoice = $list->current();
+            $this->checkTicket($invoice);
+            $list->next();
+        }
+
+    }
+
+    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');
+        $invoice = $invoiceConf['91110113MA004JNJ28'];
+        $Tax =new \TaxInvoice($invoice['appKey'],$invoice['appSecret'],$invoice['entCode']);
+        $opentime = date("Ymd",strtotime($invoice['open_time']));
+        $checkNum= $invoice['checkNumber'];
+        $invNum= $this->InvoiceNum[$invoice['invoiceType']];
+        if(!in_array($invNum,['012','022',"004","028"]))$checkNum= substr($invoice['checkNumber'],-6);
+        if(in_array($invNum,['021','022'])) $subtotal= $invoice['inv_amount'];
+        $result=$Tax->CheckInvoiceSingle( $checkNum,$subtotal,$invoice['inv_code'],$invoice['inv_number'],$opentime,$invNum);
+        if(isset($result['code']) && $result['code']=='0000' and count($result['data'])>0){
+            if($result['data']['cyjg']=="0001"){
+                $data= $result['data'];
+                 $this->changeFiled($data,$invoice);
+            }
+            if (in_array($result['data']['cyjg'],["0002","1014"])){
+                $this->lastUpdate($invoice['id']);
+            }
+        }
+         $this->faild($invoice['id']);
+    }
+
+    protected function changeFiled($data,$invoice){
+        $save = [
+            "payNo"=>$invoice['payNo'],
+            "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'],
+            "items"=>$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){
+        PayInvoice::where(["id"=>$id])->update(["status"=>5,"check_remark"=>"验证失败","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"))]);
+    }
+
+}

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

@@ -0,0 +1,122 @@
+<?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\Config;
+use think\console\Command;
+use think\console\Input;
+use think\console\input\Argument;
+use think\console\input\Option;
+use think\console\Output;
+
+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");
+        $ticket = $this->getTicket();
+        while ($ticket->valid()) {
+            $ticket = $ticket->current();
+            $this->checkTicket($ticket);
+        }
+        // 指令输出
+        $output->writeln('checkticket');
+    }
+    protected function getTicket(){
+        $ticket = InvoiceTicket::where([["is_del","=",0],["status","=",0]])
+        ->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"=>4])->findOrEmpty();
+        if(!$info->isEmpty()){
+            $companyInfo = CompanyInfo::where(["companyNo"=>$info->inv_out,"is_del"=>0,"status"=>1])->findOrEmpty();
+            $isTicket = $companyInfo['out_ticket']??0;
+            $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'],["012","022"])) $subtotal= $info['inv_total'];
+        $result=$Tax->CheckInvoiceSingle( $checkNum,$subtotal,$info['inv_code'],$info['inv_number'],$opentime,$info['inv_type']);
+        if(isset($result['code']) && $result['code']=='0000' and count($result['data'])>0){
+            if($result['data']['cyjg']=="0001"){
+                $data= $result['data'];
+                 $this->changeFiled($data,$info);
+            }
+            if (in_array($result['data']['cyjg'],["0002","1014"])){
+                $this->lastUpdate($info['invNo']);
+            }
+        }
+        $this->faild($info['invNo']);
+
+    }
+    protected function changeFiled($data,$info){
+      InvoiceTicket::where(['invNo'=>$info['invNo'],"type"=>0,"status"=>0,"is_del"=>0])->save([
+            "inv_total"=>$data['je'],
+            "inv_subtotal"=>$data['se'],
+            "seller_id"=>$data["gmfsbh"],
+            'seller_title'=>$data['gmfmc'],
+            'seller_addr'=>$data['gmfdzdh'],
+            'seller_bank'=>$data['gmfyhzh'],
+            'buyer_id'=>$data['xhfsbh'],
+            'buyer_title'=>$data['xhfmc'],
+            'buyer_addr'=>$data['xhfdzdh'],
+            'buyer_bank'=>$data['xhfyhzh'],
+            "tax_fee"=>$data['fpse'],
+            "remark"=>$data['bz'],
+            'issuer'=>$data['kpr'],
+            'reciver'=>$data['skr'],
+            'reviewer'=>$data['fhr'],
+            'item'=>$this->itemFiled($data['detailList']),
+            "status"=>1,
+            "updatetime"=>$this->dateTime,
+        ]);
+        InvoicePool::where(['invNo'=>$info['invNo']])->save(["status"=>10,"updatetime"=>$this->dateTime]);
+    }
+    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){
+        InvoiceTicket::where(['invNo'=>$invNo,"type"=>0,"status"=>0,"is_del"=>0])->save(["status"=>5,"updatetime"=>$this->dateTime]);
+        InvoicePool::where(['invNo'=>$invNo])->save(["status"=>6,"updatetime"=>$this->dateTime]);
+    }
+    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"))]);
+    }
+}

+ 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('提交数据采购供应商存在不一致');

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

@@ -0,0 +1,166 @@
+<?php
+
+
+namespace app\admin\controller;
+
+use app\admin\model\InvoiceGood;
+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"=>""],"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","=",$param["merge_code"]];
+        if($param["inv_good_name"]!=="") $where[]=["c.inv_good_name","like","%{$param["inv_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();
+         $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();
+         $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|number|gt:0',
+            'spuCode|商品编码' => 'require|number|gt:0',
+            '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($item['tax']!=$taxInfo['tax']){
+                $diff["tax"] = [
+                    "inv_tax"=>$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"],
+                "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"],
+            ];
+        }
+        $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("发票明细不存在");
+        $info->startTrans();
+        try{
+            $res = $info->delete();
+            if($res){
+                $item->balance_amount = bcadd($item['balance_amount'],bcsub($info['total_amount'],$info['balance_amount'],2),2);
+                $item->status = $item->balance_amount==$item->total_amount?0:1;
+                $item->save();
+            }else 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);
+    }
+}

Plik diff jest za duży
+ 585 - 529
app/admin/controller/OrderInv.php


+ 126 - 97
app/admin/controller/Payment.php

@@ -4,7 +4,7 @@ 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\PayInfo;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 +1462,133 @@ 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"=>""],"post","trim");
+        $valid = Validate::rule(["hpNo|回票申请编号"=>"require|max:255","status"=>"require|number|in:1,2,3,4,6,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($param['hpNo'],$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(in_array($param['status'],[5,6,7])){
+                    Pay::subInv($param['hpNo'],$info->inv_fee);
+                    event("reportCode",["param"=>["key"=>"hpNo","value"=>$param['hpNo']],"type"=>"sub"]);
                 }
-                Db::commit();
-                return app_show(0,"回票申请更新成功");
-            }throw new \Exception("回票申请状态更新失败");
+            }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());
+//        }
+//
+//    }
 
 
 
@@ -1978,24 +2015,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 +2023,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(){
@@ -2295,7 +2324,7 @@ 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();

+ 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']==''){

+ 19 - 25
app/admin/listener/ComonQrd.php

@@ -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){
@@ -276,11 +270,11 @@ class ComonQrd
     	$ticket =InvoiceTicket::where(["invNo"=>$data['invNo'],"type"=>0])->json(['item'])->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"),

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

@@ -0,0 +1,40 @@
+<?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'])->rmField($param['key'],$param['value'])->save();
+        }
+        if(is_array($param['value'])){
+           foreach ($param['value'] as $v){
+               $this->model->whereFindInSet($param['key'],$v)->rmField($param['key'],$v)->save();
+           }
+        }
+    }
+
+    private function addField($param){
+        $info= $this->model->where([$param['main']['key']=>$param['main']['value']])->setField($param['key'],$param['value']);
+    }
+}

+ 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(["invNo"=>$invNo,"status"=>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(['invNo'=>$invNo,'status'=>1])->save(['status'=>2,'assoc_time'=>date("Y-m-d H:i:s")]);
+        }
+    }
+    public static function addOrder($invNo)
+    {
+        $list = self::where(['invNo'=>$invNo,'status'=>2])->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(['invNo'=>$invNo,'status'=>2])->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(['invNo'=>$invNo,'status'=>2])->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(['invNo'=>$invNo,'status'=>2])->save(['status'=>3,'assoc_time'=>date('Y-m-d H:i:s')]);
+            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","");
+    }
 }

+ 20 - 1
app/admin/model/InvoiceInfo.php

@@ -10,5 +10,24 @@ use think\Model;
  */
 class InvoiceInfo extends Model
 {
-    //
+    public static function OnAfterWrite(Model $model){
+       if($model->status==1 && !empty($model->item)){
+          (new  InvoiceItem)->saveAll(array_map(function ($item)use($model) {
+              return [
+                  'invoice_code'=>$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));
+       }
+   }
 }

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

@@ -0,0 +1,66 @@
+<?php
+
+
+namespace app\admin\model;
+
+
+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");
+    }
+
+    public static function getNumAttr($val){
+        return  strval(floatval($val));
+    }
+
+    public static function setNumAttr($val){
+        return  floatval($val);
+    }
+    public static function onAfterWrite($model){
+        $code=$model->invoiceCode;
+        $orderType=$model->order_type;
+        $change = $model->getChangedData();
+        if(in_array("status",$change) && ($model->status==1||$model->status==2)){
+             $num = self::where(['invoiceCode'=>$code,'order_type'=>$orderType,'status'=>0])->count(1);
+            if($num==0){
+                if($orderType==1){
+                  $info= InvoicePool::where(['invNo'=>$code,'is_del'=>0])->findOrEmpty();
+                  if(!$info->isEmpty() && $info->status==10){
+                            $info->status=11;
+                            $info->save();
+                  }
+                }
+                if($orderType==2){
+                    $info= PayInvoice::where(['hpNo'=>$code,'is_del'=>0])->findOrEmpty();
+                    if(!$info->isEmpty()){
+                        $pay = Pay::where(['payNo'=>$info->payNo,'is_del'=>0])->findOrEmpty();
+                        if(!$pay->isEmpty() && $pay->status==2 &&$info->status==11){
+                                    $info->status=12;
+                                    $info->save();
+                        }
+                    }
+                }
+            }
+        }
+    }
+
+    public static function rmInvoice($code,$orderType){
+        $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]);
+       }
+    }
+
+
+}

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

@@ -0,0 +1,52 @@
+<?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){
+        return  (new InvoiceOrder)->withJoin(["ItemInfo"],"left")->where(['code'=>$code,"status"=>1])->sum('totalFee');
+    }
+
+    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);
+    }
+}

+ 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{
+
+}

+ 26 - 1
app/admin/model/InvoiceTicket.php

@@ -10,5 +10,30 @@ use 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);
+    }
+   public static function OnAfterUpdate(Model $model){
+       if($model->status==1 && $model->type==0 && $model->is_del==0 && !empty($model->item)){
+          (new  InvoiceItem)->saveAll(array_map(function ($item)use($model) {
+              return [
+                  'invoice_code'=>$model->invNo,
+                  'order_type'=>1,
+                  'good_name'=>$item['XMMC'],
+                  'unit'=>$item['DW'],
+                  'num'=>$item['SPSL'],
+                  'unit_price'=>$item['DJ'],
+                  'amount'=>$item['JE'],
+                  "tax_amount"=>$item['SE'],
+                  "tax"=>$item['SL'],
+                  "cat_code"=>$item['SPBM'],
+                  "total_amount"=>bcadd($item['JE'],$item['SE'],2),
+                  "balance_amount"=>bcadd($item['JE'],$item['SE'],2),
+              ];
+          },$model->item));
+       }
+   }
 }

+ 40 - 0
app/admin/model/Pay.php

@@ -13,4 +13,44 @@ class Pay extends Model
 	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("对账单不存在");
+        if($pay->winv_fee<$payfee)throw new \Exception('对账单未开票金额不足');
+        $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("对账单不存在");
+        if($pay->inv_fee<$payfee)throw new \Exception('对账单开票中金额不足');
+        $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("对账单不存在");
+        if($pay->ainv_fee<$payfee)throw new \Exception('对账单开票金额不足');
+        $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("对账单不存在");
+        if($pay->inv_fee<$payfee)throw new \Exception('对账单开票中金额不足');
+        $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'=>'订单关联完毕财务驳回'
+        ];
 }

+ 12 - 3
app/admin/model/ReportCode.php

@@ -17,9 +17,18 @@ class ReportCode extends Model
     }
     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","");
+    }
 }

+ 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' => [

Niektóre pliki nie zostały wyświetlone z powodu dużej ilości zmienionych plików