123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633 |
- <?php
- declare (strict_types = 1);
- namespace app\Admin\controller;
- use app\BaseController;
- use think\App;
- use think\facade\Log;
- use think\Request;
- use think\facade\Db;
- class Invoice extends BaseController
- {
- /**
- * 显示资源列表
- *
- * @return \think\Response
- */
- public function index()
- {
- $post =$this->request->post();
- $token = isset($post['token']) ? trim($post['token']) : "";
- if($token==""){
- return error_show(101,'token不能为空');
- }
- $effetc = VerifyTokens($token);
- if(!empty($effetc) && $effetc['code']!=0){
- return error_show($effetc['code'],$effetc['message']);
- }
- $sid = isset($post['sid'])&& $post['sid']!="" ? intval($post['sid']) :"";
- if($sid==""){
- return error_show(1004,"参数sid 不能为空");
- }
- $invoice = Db::name("invoice")->where([["payid","=",$sid]])->select();
- $list=["buyer"=>[],"seller"=>[],"invoice"=>[],"old"=>[]];
- foreach ($invoice as $key=>$value){
- $value['invoice_info']=isset($value['invoice_info'])&&$value['invoice_info']!="" ? json_decode
- ($value['invoice_info'],
- true) :[];
- $value['item_list']=isset($value['item_list'])&&$value['item_list']!="" ? json_decode
- ($value['item_list'],
- true) :[];
- if($value['status']==0){
- $list["buyer"]['buyer_name'] = $value['buyer_name'];
- $list["buyer"]['buyer_id'] = $value['buyer_id'];
- $list["buyer"]['buyer_address'] = $value['buyer_address'];
- $list["buyer"]['buyer_bank'] = $value['buyer_bank'];
- $list["seller"]['seller_name'] =$value["seller_name"];
- $list["seller"]['seller_id'] =$value["seller_id"];
- $list["seller"]['seller_address'] =$value["seller_address"];
- $list["seller"]['seller_bank'] =$value["seller_bank"];
- if($list["old"]==[]){
- $list["old"]['buyer_name'] = $value['invoice_info']['buyer_name'];
- $list["old"]['buyer_id'] = $value['invoice_info']['buyer_id'];
- $list["old"]['buyer_address'] = $value['invoice_info']['buyer_address'];
- $list["old"]['buyer_bank'] = $value['invoice_info']['buyer_bank'];
- $list["old"]['seller_name'] = $value['invoice_info']["seller_name"];
- $list["old"]['seller_id'] = $value['invoice_info']["seller_id"];
- $list["old"]['seller_address'] = $value['invoice_info']["seller_address"];
- $list["old"]['seller_bank'] = $value['invoice_info']["seller_bank"];
- }
- }
- $list["invoice"][]=$value;
- }
- return app_show(0,"获取成功",$list);
- }
- /**
- * 显示创建资源表单页.
- *
- * @return \think\Response
- */
- public function InvoiceAdd()
- {
- $post =$this->request->post();
- $token = isset($post['token']) ? trim($post['token']) : "";
- if($token==""){
- return error_show(101,'token不能为空');
- }
- $effetc = VerifyTokens($token);
- if(!empty($effetc) && $effetc['code']!=0){
- return error_show($effetc['code'],$effetc['message']);
- }
- $sid = isset($post['sid'])&& $post['sid']!="" ? intval($post['sid']) :"";
- if($sid==""){
- return error_show(1004,"参数sid 不能为空");
- }
- $image = isset($post['image'])&&is_array($post['image'])? $post['image'] :[];
- if(empty($image)){
- return error_show(1004,"参数image 不能为空");
- }
- $payifo = Db::name("pay_stages")->where("id","=",$sid)->find();
- if(empty($payifo)){
- return error_show(1004,"付款信息未找到");
- }
- if(!in_array($payifo['status'],[3,9])){
- return error_show(1004,"付款信息状态有误");
- }
- Db::startTrans();
- try{
- $data=[];
- foreach ($image as $value){
- $temp=[];
- $temp['payid']=$sid;
- $temp['invoice_type']=1;
- $temp['image']=$value['url'];
- $temp['name']=$value['name'];
- $temp['invoice_info']="";
- $temp['status']=3;
- $temp['addtime']=date("Y-m-d H:i:s");
- $temp['updatetime']=date("Y-m-d H:i:s");
- $data[]=$temp;
- }
- $insert= Db::name("invoice_img")->insertAll($data);
- if($insert){
- $upd=Db::name("pay_stages")->where([['id',"=",$sid],['status',"in",[3,9]]])->save(['status'=>4,
- "updatetime"=>date("Y-m-d H:i:s")]);
- if($upd){
- Db::commit();
- return app_show(0,"发票上传成功等待识别");
- }
- }
- Db::rollback();
- return error_show(1005,"发票提交失败");
- }catch(\Exception $e){
- Db::rollback();
- return error_show(1005,$e->getMessage());
- }
- }
- /**
- * 保存新建的资源
- *
- * @param \think\Request $request
- * @return \think\Response
- */
- public function InvoiceSave()
- {
- $post =$this->request->post();
- $token = isset($post['token']) ? trim($post['token']) : "";
- if($token==""){
- return error_show(101,'token不能为空');
- }
- $effetc = VerifyTokens($token);
- if(!empty($effetc) && $effetc['code']!=0){
- return error_show($effetc['code'],$effetc['message']);
- }
- $id = isset($post['id'])&& $post['id']!="" ? intval($post['id']) :"";
- if($id==""){
- return error_show(1004,"参数id 不能为空");
- }
- $payifo = Db::name("invoice")->where("id","=",$id)->find();
- if(empty($payifo)){
- return error_show(1004,"发票信息未找到");
- }
- if($payifo['status']!=0){
- return error_show(1004,"发票信息未识别请等待");
- }
- $info=[];
- isset($post['type'])&&$post['type']!="" ?$info['type']= $post['type'] :"";
- isset($post['code'])&&$post['code']!=""? $info['code']= $post['code'] :"";
- isset($post['check_code'])&&$post['check_code']!=""? $info['check_code']= $post['check_code'] :"";
- isset($post['issue_date'])&&$post['issue_date']!=""? $info['issue_date']= $post['issue_date'] :"";
- isset($post['subtotal_amount'])&&$post['subtotal_amount']!=""? $info['subtotal_amount']= $post['subtotal_amount'] :"";
- isset($post['subtotal_tax'])&&$post['subtotal_tax']!=""? $info['subtotal_tax']= $post['subtotal_tax'] :"";
- isset($post['total'])&&$post['total']!=""? $info['total']= $post['total'] :"";
- isset($post['item_list'])&&$post['item_list']!=""? $info['item_list']= json_encode($post['item_list'],JSON_UNESCAPED_UNICODE) :"";
- if(isset($post['item_list'])){
- foreach ($post["item_list"] as $value){
- if($value['tax_rate']=='' || $value['tax_rate']==0){
- // var_dump($value['tax']!=''||$value['tax']!=0);
- if($value['tax']!=''&&$value['tax']!=0){
- return error_show(1004,"商品明细税率与税额不匹配");
- }
- }else{
- if($value['tax']==''|| $value['tax']==0){
- return error_show(1003,"商品明细税率与税额不匹配");
- }
- }
- }
- }
- isset($post['field']) ?$info['change_field']= $post['field'] :"" ;
- $info['status'] = isset($post['field'])&& $post['field']!="" ? 1 :0;
- $info['updatetime'] = date("Y-m-d H:i:s");
- $inse=[];
- if($info['change_field']!=""){
- $field = explode(",",$info['change_field']);
- $arr =["buyer_name","buyer_id","buyer_address","buyer_bank","seller_name","seller_id","seller_address","seller_bank"];
- $interset = array_intersect($field,$arr);
- if(!empty($interset)){
- $field = implode(",",$interset);
- isset($post['buyer_name'])&&$post['buyer_name']!=""? $inse['buyer_name']= $post['buyer_name'] :"";
- isset($post['buyer_id'])&&$post['buyer_id']!=""? $inse['buyer_id']= $post['buyer_id'] :"";
- isset($post['buyer_address'])&&$post['buyer_address']!=""? $inse['buyer_address']= $post['buyer_address'] :"";
- isset($post['buyer_bank'])&&$post['buyer_bank']!=""? $inse['buyer_bank']= $post['buyer_bank'] :"";
- isset($post['seller_name'])&&$post['seller_name']!=""? $inse['seller_name']= $post['seller_name'] :"";
- isset($post['seller_id'])&&$post['seller_id']!=""? $inse['seller_id']= $post['seller_id'] :"";
- isset($post['seller_address'])&&$post['seller_address']!=""? $inse['seller_address']= $post['seller_address'] :"";
- isset($post['seller_bank'])&&$post['seller_bank']!=""? $inse['seller_bank']= $post['seller_bank'] :"";
- $inse['change_field']= Db::raw("concat(IFNULL(change_field,''),',','{$field}')");
- $inse["status"]=1;
- $inse["updatetime"]=date("Y-m-d H:i:s");
- $check = $this->checkField($inse,$payifo['payid']);
- if($check['code']==false){
- return app_show(1005,"发票企业信息验证失败",$check['msg']);
- }
- }
- }
- Db::startTrans();
- try{
- $insert = Db::name("invoice_info")->where("invid","=",$id)->update($info);
- if($insert){
- if(!empty($inse)){
- $updat = Db::name("invoice_info")->where("payid","=",$payifo['payid'])->update($inse);
- if($updat){
- Db::commit();
- return app_show(0,"发票信息修改成功");
- }else{
- Db::rollback();
- return error_show(1005,"发票信息修改失败");
- }
- }
- Db::commit();
- return app_show(0,"发票信息修改成功");
- }
- Db::rollback();
- return error_show(1005,"发票信息修改失败");
- }catch(\Exception $e){
- Db::rollback();
- return error_show(1005,$e->getMessage());
- }
- }
- /**
- * 显示指定的资源
- *
- * @param int $id
- * @return \think\Response
- */
- public function InvoiceDel()
- {
- $post =$this->request->post();
- $token = isset($post['token']) ? trim($post['token']) : "";
- if($token==""){
- return error_show(101,'token不能为空');
- }
- $effetc = VerifyTokens($token);
- if(!empty($effetc) && $effetc['code']!=0){
- return error_show($effetc['code'],$effetc['message']);
- }
- $id = isset($post['id'])&& $post['id']!="" ? intval($post['id']) :"";
- if($id==""){
- return error_show(1004,"参数id 不能为空");
- }
- $payifo = Db::name("invoice")->where("id","=",$id)->find();
- if(empty($payifo)){
- return error_show(1004,"发票信息未找到");
- }
- $data = ["id"=>$id,"is_del"=>1,"updatetime"=>date("Y-m-d H:i:s")];
- $save = Db::name("invoice_img")->save($data);
- return $save? app_show(0,"发票删除成功"):error_show(1004,"发票删除失败");
- }
- /**
- * 显示编辑资源表单页.
- *
- * @param int $id
- * @return \think\Response
- */
- public function InvoiceCheck()
- {
- $post =$this->request->post();
- $token = isset($post['token']) ? trim($post['token']) : "";
- if($token==""){
- return error_show(101,'token不能为空');
- }
- $effetc = VerifyTokens($token);
- if(!empty($effetc) && $effetc['code']!=0){
- return error_show($effetc['code'],$effetc['message']);
- }
- $id = isset($post['sid'])&& $post['sid']!="" ? intval($post['sid']) :"";
- if($id==""){
- return error_show(1004,"参数sid 不能为空");
- }
- $type= isset($post['type'])&& $post['type']!="" ? intval($post['type']) :1;
- $payifo = Db::name("invoice_img")->where([["payid","=",$id],["is_del","=",0]])->select();
- if(empty($payifo->toArray())){
- return error_show(1004,"发票信息未找到");
- }
- $list=[];
- $n=0;
- $inBoole =false;
- foreach ($payifo as $key=>$value){
- if($value['status']==2){
- return error_show(1004,"存在不能识别的发票,请先删除!");
- }
- if($value['status']==4){
- return error_show(1004,"存在审核退回的发票,请先修改!");
- }
- if($value['status']==3){
- $list[]=$value['id'];
- }
- $inv_info = Db::name("invoice_info")->where([['invid',"=",$value['id']]])->find();
- if($value['status']==0 ){
- $n++;
- if(isset($inv_info['status']) ||$inv_info['status']==0){
- $inBoole = true;
- } ;
- }
- }
- if($n==count($payifo)){
- $check= $this->lastCheck($id);
- if($check["code"]){
- if($type==1){
- Db::name("pay_stages")->where("id","=",$id)->save(["status"=>5,"updatetime"=>date("Y-m-d H:i:s")]);
- return app_show(0,"提交成功");
- }else{
- Db::name("pay_stages")->where("id","=",$id)->save(["status"=>6,"inv_fee"=>$check['invtotal'],
- "updatetime"=>date("Y-m-d H:i:s")]);
- return app_show(0,"提交成功");
- }
- }else{
- return app_show(1003,"{$check['remark']}",$check);
- }
- }
- if(!empty($list)){
- Db::name("invoice_img")->where([["id","in",$list],["is_del","=",0],['status',"=",3]])->save
- (['status'=>1,"updatetime"=>date("Y-m-d H:i:s")]);
- }
- return app_show(0,"提交成功");
- }
- /**
- * 保存更新的资源
- *
- * @param \think\Request $request
- * @param int $id
- * @return \think\Response
- */
- public function update()
- {
- $post =$this->request->post();
- $token = isset($post['token']) ? trim($post['token']) : "";
- if($token==""){
- return error_show(101,'token不能为空');
- }
- $effetc = VerifyTokens($token);
- if(!empty($effetc) && $effetc['code']!=0){
- return error_show($effetc['code'],$effetc['message']);
- }
- $id = isset($post['sid'])&& $post['sid']!="" ? intval($post['sid']) :"";
- if($id==""){
- return error_show(1004,"参数id 不能为空");
- }
- $payifo = Db::name("invoice")->where("payid","=",$id)->select()->toArray();
- if(empty($payifo)){
- return error_show(1004,"发票信息未找到");
- }
- $n=0;
- foreach ($payifo as $key=>$value){
- if($value['status']==0){
- $n++;
- }
- }
- $invtime = isset($post['invtime'])&&$post['invtime']!='' ? $post['invtime']:date("Y-m-d H:i:s");
- Db::startTrans();
- try{
- $payno = Db::name("pay_stages")->where([['id',"=",$id],["status","=",6]])->find();
- if($n<count($payifo)){
- $msg ="审核未通过";
- $updat= Db::name("pay_stages")->where([['id',"=",$id],["status","=",6]])->save(['status'=>9,"updatetime"=>date("Y-m-d
- H:i:s")]);
- if($updat){
- Db::commit();
- return app_show(0,"审核未通过");
- }else{
- Db::rollback();
- return error_show(1004,"审核失败");
- }
- }else{
- $updat= Db::name("pay_stages")->where([['id',"=",$id],["status","=",6]])->save(['status'=>7,
- "invtime"=>$invtime,"updatetime"=>date("Y-m-d H:i:s")]);
- if($updat){
- $pay=[];
- $num = Db::name("pay_stages")->whereOr([['status',"<>",7],['pay_status',"<>",4]])->where
- ([['payNo',"=", $payno['payNo']],["is_del","=",0]])->count();
- if($num==0){
- $pay['status']=2;
- $pay['updatetime']=date("Y-m-d H:i:s");
- $pay_up = Db::name("pay")->where('payNo',"=", $payno['payNo'])->save($pay);
- if($pay_up){
- Db::commit();
- return app_show(0,"审核通过");
- }else{
- Db::rollback();
- return error_show(1004,"审核失败");
- }
- }
- Db::commit();
- return app_show(0,"审核通过");
- }
- Db::rollback();
- return error_show(1004,"审核失败");
- }
- }catch (\Exception $e){
- Db::rollback();
- return error_show(1004,$e->getMessage());
- }
- }
- /**
- * @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 InvoiceStatus()
- {
- $post =$this->request->post();
- $token = isset($post['token']) ? trim($post['token']) : "";
- if($token==""){
- return error_show(101,'token不能为空');
- }
- $effetc = VerifyTokens($token);
- if(!empty($effetc) && $effetc['code']!=0){
- return error_show($effetc['code'],$effetc['message']);
- }
- $id = isset($post['invid'])&& $post['invid']!="" ? intval($post['invid']) :"";
- if($id==""){
- return error_show(1004,"参数invid 不能为空");
- }
- $payifo = Db::name("invoice_img")->where("id","=",$id)->find();
- if(empty($payifo)){
- return error_show(1004,"发票信息未找到");
- }
- $status=isset($post['status'])&& $post['status']!="" ? intval($post['status']) :"";
- $remark=isset($post['remark'])&& $post['remark']!="" ? trim($post['remark']) :"";
- if($status==""){
- return error_show(1004,"参数status 不能为空");
- }
- if(!in_array($status,[0,1,2,3,4])){
- return error_show(1004,"参数status 无效参数");
- }
- Db::startTrans();
- try {
- $payifo['status']=$status;
- $payifo['remark']=$remark;
- $payifo['updatetime']=date("Y-m-d H:i:s");
- $update=Db::name("invoice_img")->save($payifo);
- if($update){
- // if(in_array($status,[2,4])){
- // $pay=["status"=>9,"updatetime"=>date("Y-m-d H:i:s")];
- // $ins= Db::name("pay_stages")->where("id","=",$payifo["payid"])->save($pay);
- // if($ins){
- // Db::commit();
- // return app_show(0,"修改成功");
- // }else{
- // Db::rollback();
- // return error_show(1003,"修改失败");
- // }
- // }
- Db::commit();
- return app_show(0,"修改成功");
- }
- Db::rollback();
- return error_show(1003,"修改失败");
- }catch (\Exception $e){
- Db::rollback();
- return error_show(1005,$e->getMessage());
- }
- }
- private function checkField($invocie,$payid){
- $paystage = Db::name("pay_stages")->where("id","=",$payid)->find();
- $remak="";
- if($paystage['companyNo']!=""){
- $company = Db::name("company_info")->where("companyNo","=",$paystage['companyNo'])->find();
- if(!empty($company)){
- if(isset($invocie['buyer_name'])&&$company['company_name']!=$invocie['buyer_name']){
- $remak.="\r\n 发票购买方企业名称不匹配;";
- }
- if(isset($invocie['buyer_id'])&&$company['company_license']!=$invocie['buyer_id']){
- $remak.="\r\n 发票购买方企业纳税识别号不匹配;";
- }
- if(isset($invocie['buyer_address'])&&stripos($company['company_address'].$company['mobile'],trim($invocie['buyer_address']))===false){
- $remak.="\r\n 发票购买方企业地址不匹配;";
- }
- if(isset($invocie['buyer_bank'])&&stripos($company['bank_name'].$company['bankNo'],trim($invocie['buyer_bank']))===false){
- $remak.="\r\n 发票购买方企业银行账户不匹配;";
- }
- }
- }
- if($remak!=""){
- return ["code"=>false,"msg"=>$remak];
- }else{
- return ["code"=>true,"msg"=>$remak];
- }
- }
- private function lastCheck($id){
- $list = Db::name("invoice")->where("payid","=",$id)->select();
- $isbool = True;
- $remark="";
- $rate=0;
- $total = 0;//发票总额;
- $invid=[];
- $paystage = Db::name("pay_stages")->where("id","=",$id)->find();
- if(!empty($list)){
- foreach ($list as $key=>$value){
- $row=$key+1;
- $item= json_decode($value['item_list'],true);
- if($value['subtotal_amount']===''){
- $isbool=false;
- $remark.="第{$row}张发票总额不能为空;";
- $invid[]=$value['id'];
- break;
- }
- if($value['subtotal_tax']===''){
- $isbool=false;
- $remark.="第{$row}张发票税额不能为空;";
- $invid[]=$value['id'];
- break;
- }
- if($value['total']===''){
- $isbool=false;
- $remark.="第{$row}张发票税后总额不能为空;";
- $invid[]=$value['id'];
- break;
- }
- $value['subtotal_amount']= str_replace("¥",'',$value['subtotal_amount']);
- $value['subtotal_tax']=str_replace("¥",'',$value['subtotal_tax']);
- $value['total']= str_replace("¥",'',$value['total']);
- $total +=$value['total'];
- $temp_total = round($value['subtotal_amount']+$value['subtotal_tax'],2);
- if($temp_total!=$value["total"]){
- $isbool=false;
- $remark.="第{$row}张发票金额总计不匹配;";
- $invid[]=$value['id'];
- break;
- }
- if(!empty($item)){
- $totla_amount =0;
- $totla_tax =0;
- foreach ($item as $k=>$val){
- if($val['tax_rate']===''){
- $isbool=false;
- $remark.="第{$row}张发票明细税率不能为空;";
- $invid[]=$value['id'];
- break;
- }
- if($val['tax']===''){
- $isbool=false;
- $remark.="第{$row}张发票明细税额不能为空;";
- $invid[]=$value['id'];
- break;
- }
- if($val['amount']===''){
- $isbool=false;
- $remark.="第{$row}张发票明细金额不能为空;";
- $invid[]=$value['id'];
- break;
- }
- $rate_tax=substr($val['tax_rate'],0,-1);
- $totla_amount+=floatval($val['amount']);
- $totla_tax+=floatval($val['tax']);
- if($rate==0){
- $rate=$rate_tax;
- }else{
- if($rate_tax!=$rate){
- $isbool=false;
- $remark.="第{$row}张发票税率不匹配;";
- $invid[]=$value['id'];
- break;
- }
- }
- }
- $totla_amount = round($totla_amount,2);
- $totla_tax = round($totla_tax,2);
- if($totla_amount!=$value['subtotal_amount']){
- $isbool=false;
- $remark.="第{$row}张发票明细总计金额不匹配;";
- $invid[]=$value['id'];
- break;
- }
- if($totla_tax!=$value['subtotal_tax']){
- $isbool=false;
- $remark.="第{$row}张发票总税额不匹配;";
- $invid[]=$value['id'];
- break;
- }
- }
- }
- $pay = Db::name("pay")->where("payNo","=",$paystage['payNo'])->find();
- $payrate= json_decode($pay['pay_rate'],true);
- $paytotal = sprintf("%.2f",$pay ['winv_fee']);
- $total = round($total,2);
- // //var_dump($paytotal,$total,(count($payrate)==$paystage['pay_stages']&& $paytotal!=$total));
- // if($paytotal<$total||(count($payrate)==$paystage['pay_stages']&& $paytotal!=$total)){
- // $isbool=false;
- // $remark.="发票总计金额不匹配;";
- // $invid[]=$value['id'];
- // }
- }
- return ["code"=>$isbool,"remark"=>$remark,"invid"=>array_unique($invid),"invtotal"=>$total];
- }
- }
|