wugg 2 years ago
parent
commit
f0116bfaaa
1 changed files with 78 additions and 0 deletions
  1. 78 0
      app/admin/controller/OrderInv.php

+ 78 - 0
app/admin/controller/OrderInv.php

@@ -10,6 +10,7 @@ use think\facade\Db;
 use think\facade\Cache;
 use think\facade\Cache;
 
 
 class OrderInv extends BaseController{
 class OrderInv extends BaseController{
+	private $typename=["004"=>"增值税专用发票",'007'=>"增值税普通发票",'026'=>'增值税电子普通发票','028'=>'增值税电子专用发票'];
     public function __construct(App $app) {
     public function __construct(App $app) {
         parent::__construct($app);
         parent::__construct($app);
     }
     }
@@ -2069,4 +2070,81 @@ class OrderInv extends BaseController{
       ];
       ];
        $li = excelExport("待开票",$header,$data);
        $li = excelExport("待开票",$header,$data);
     }
     }
+
+    public function  importInv(){
+		$param =$this->request->only(["data"=>[]],"post");
+		if(empty($param['data']))return error_show(1004,"参数 data 不能为空");
+		$total=[];
+		$list=[];
+		 foreach ($param['data'] as $key=>$value){
+
+            if($value['invNo']==""){
+                return error_show(1005,"第".($key+1)."行缺少发票申请编号数据");
+            }
+            if($value["inv_type"]===""){
+                return error_show(1005,"第".($key+1)."行缺少发票类型数据");
+            }
+            if($value["inv_code"]==""){
+                return error_show(1005,"第".($key+1)."行缺少发票代码数据");
+            }
+            if($value["inv_number"]==""){
+                return error_show(1005,"第".($key+1)."行缺少发票号码数据");
+            }
+            if($value["inv_total"]==""){
+                return error_show(1005,"第".($key+1)."行缺少发票金额数据");
+            }
+             if($value["inv_subtotal"]==""){
+                return error_show(1005,"第".($key+1)."行缺少发票税后金额数据");
+            }
+            if($value["open_date"]==""){
+                return error_show(1005,"第".($key+1)."行缺少发票开票日期数据");
+            }
+			!isset($total[$value['invNo']]) ? $total[$value['invNo']]['total']=0:"";
+
+            $temp=[];
+            $temp['invNo'] = $value["invNo"];
+            $temp['type'] = 0;
+            $temp['inv_type'] =array_search($value['inv_type'],$this->typename) ;
+            $temp['inv_code'] = $value['inv_code'];
+            $temp['inv_number'] = $value['inv_number'];
+            $temp['inv_subtotal'] = $value['inv_subtotal'];
+            $temp['inv_total'] = $value['inv_total'];
+            $temp['open_date'] =  date("Y-m-d H:i:s",strtotime($value['open_date']));
+            $temp['status'] = 1;
+            $temp['is_del'] = 0;
+            $temp['addtime'] = date("Y-m-d H:i:s");
+            $temp['updatetime'] = date("Y-m-d H:i:s");
+            $total[$value["invNo"]]['total']+=$value['inv_total'];
+            $total[$value["invNo"]]['list'][]=$temp;
+        }
+		 Db::startTrans();
+		 try{
+		 	$keyarr =array_keys($total);
+		 	$inpool = Db::name("invoice_pool")->where([["invNo","in",$keyarr],["open_type","in",[2,3]],["is_del","=",0],["status","=",0]])->column("winv_fee","invNo");
+		 	foreach ($total as $key=>$value){
+				if(!isset($inpool[$key])) continue;
+				if($inpool[$key]['winv_fee']> $value['total']){
+					throw new Exception("{$key}票面金额小于未开票金额");
+				}
+				array_merge($list,$value['list']);
+				 $up =Db::name("invoice_pool")->where([["invNo","=",$key],["is_del","=",0]])->save(["status"=>2,"updatetime"=>date("Y-m-d H:i:s")]);
+                if(!$up){
+                    Db::rollback();
+                    return error_show(1005,"发票状态更新失败");
+                }
+		 	}
+		 	$inticket =Db::name("invoice_ticket")->insertAll($list);
+		 	 if(!$inticket){
+                Db::rollback();
+                return error_show(1005,"发票数据录入失败");
+            }else{
+                Db::commit();
+                return app_show(0,"发票数据录入成功");
+            }
+		 }catch (\Exception $e){
+		 	Db::rollback();
+		 	return error_show(1008,$e->getMessage());
+		 }
+
+    }
 }
 }