123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676 |
- <?php
- declare (strict_types = 1);
- namespace app\admin\controller;
- use app\admin\model\CgdInfo;
- use app\admin\model\CompanyInfo;
- use app\admin\model\InvoicePay;
- use app\admin\model\InvoicePool;
- use app\admin\model\InvoicePoolInfo;
- use app\admin\model\OrderCategory;use app\admin\model\Pay;
- use app\admin\model\PayInfo;
- use app\admin\model\QrdInfo;
- use app\admin\model\ReportCode;
- use app\admin\model\SupplierInfo;
- use app\admin\model\TagLog;
- use think\App;
- use think\Exception;
- use think\facade\Config;
- use think\facade\Db;
- use think\facade\Validate;
- //通用订单处理
- class ComonOrder extends Base
- {
- public $model=null;
- protected $typename;
- protected $invoiceType;
- protected $invName;
- protected $kingInvoice;
- public function __construct(App $app) {
- parent::__construct($app);
- $this->model = new \app\admin\model\ComonOrder();
- $invoice =Config::get('invoiceType');
- $this->invoiceType = $invoice['invoiceName'];
- $this->invName = $invoice['invoiceType'];
- $this->typename = $invoice['KingName'];
- $this->kingInvoice = $invoice['KingInvoice'];
- }
-
- public function list(){
- $post = $this->request->only(["start"=>"","end"=>"","cxCode"=>"","orderCode"=>[],"customerNo"=>"",
- 'cgd_status'=>'',"companyNo"=>"",'status'=>[],'relaComNo'=>'','platform_type'=>'','inv_status'=>'',
- 'pay_status'=>'',"good_status"=>"", 'page'=>1,'size'=>15],"post","trim");
- $where=[["qrd.is_del","=",0],['cgd.is_del','=',0],['qrd.totalPrice','<>',0],['cgd.totalPrice','<>',0]];
- $post['start']==''?:$where[]=["comon_order.addtime",">=",date("Y-m-d 00:00:00",strtotime($post['start']))];
- $post['end']==''?:$where[]=["comon_order.addtime","<=",date("Y-m-d 23:59:59",strtotime($post['end']))];
- $post['cxCode']==''?:$where[]=["comon_order.cxCode","like","%{$post['cxCode']}%"];
- $post['customerNo']==''?:$where[]=["comon_order.customerNo","=",$post['customerNo']];
- empty($post['status'])?:$where[]=['comon_order.status','in',$post['status']];
- empty($post['orderCode'])?:$where[]=['comon_order.orderCode','in',$post['orderCode']];
- $post['inv_status']==''?:$where[]=["qrd.inv_status","=",$post['inv_status']];
- $post['pay_status']==''?:$where[]=["qrd.pay_status","=",$post['pay_status']];
- $post['platform_type']==''?:$where[]=["comon_order.platform_type","=",$post['platform_type']];
- $post['cgd_status']===''?:$where[]=['cgd.cgd_status','=',$post['cgd_status']];
- $post['companyNo']==''?:$where[]=["comon_order.companyNo|comon_order.supplierNo","=",$post['companyNo']];
- $post['relaComNo']==''?:$where[]=["comon_order.companyNo|comon_order.supplierNo|comon_order.customerNo","=", $post['relaComNo']];
- $post['good_status']===''?:$where[]=["good.status","=",$post['good_status']];
- $list=$this->model->withJoin(["qrd","cgd","good"],"left")->where($where)
- ->order("id desc")->paginate (["page"=>$post['page'],"list_rows"=>$post['size']]);
- $this->success("0",["list"=>$list->items(),"count"=>$list->total()]);
- }
- //通用订单创建对账单
- public function PayCreate(){
- $param=$this->request->only(["common_ids"=>[]],"post","trim");
- $valid=Validate::rule(["common_ids|通用订单id集合"=>"require|array"]);
- if($valid->check($param)==false)$this->error($valid->getError());
- $obj = $this->model->with(['cgd'])->where(["id"=>$param['common_ids']])->select();
- if($obj->isEmpty())$this->error("未找到相关数据");
- $list = $obj->toArray();
- $status = array_unique(array_column($list,"status"));
- $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('提交数据采购供应商存在不一致');
- if(count($customerNo)>1)$this->error('提交数据销售客户存在不一致');
- $good_info = \app\admin\model\Good::where(['spuCode'=>$goodNo])->column('status','spuCode');
- if(empty($good_info))$this->error('商品开票类目信息不存在');
- if(in_array(0,$good_info)){
- $key = array_search(0,$good_info);
- $this->error("商品 $key 采购未确认开票类目");
- }
- if(in_array(1,$good_info)){
- $key = array_search(1,$good_info);
- $this->error("商品 $key 财务未确认开票类目");
- }
- $payNo= makeNo("PAY");
- $this->model->startTrans();
- try {
- $paydata=[
- 'payNo'=>$payNo,
- 'apply_id'=>$this->uid,
- 'apply_name'=>$this->uname,
- 'total_fee'=>"0",
- 'supplierNo'=>$supplierNo[0],
- 'supplierName'=>$supplierName,
- 'companyNo'=>$customerNo[0],
- 'companyName'=>$customerName,
- 'wpay_fee'=>"0",
- 'apay_fee'=>'0',
- 'ainv_fee'=>'0',
- 'winv_fee'=>'0',
- 'remark'=>'',
- 'is_comon'=>1,
- 'status'=>1,
- 'addtime'=>date('Y-m-d H:i:s'),
- 'updatetime'=>date('Y-m-d H:i:s'),
- ];
- foreach ($list as $item){
- if(!isset($item['cgd']) || empty($item['cgd'])) throw new Exception("采购单信息未找到");
- $value= $item['cgd'];
- $temp=[];
- $temp['cgdNo']=$value['sequenceNo'];
- $temp['total_fee']=$value['totalPrice'];
- $temp['apay_fee']=$value['apay_fee'];
- $temp['wpay_fee']=$value['wpay_fee'];
- $temp['winv_fee']=$value['winv_fee'];
- $temp['ainv_fee']=$value['ainv_fee'];
- $temp['payNo']=$payNo;
- $temp['addtime']=date('Y-m-d H:i:s');
- $temp['updatetime']=date('Y-m-d H:i:s');
- $data[]=$temp;
- $paydata['total_fee']= bcadd($paydata['total_fee'],$temp['total_fee'],2);
- $paydata['apay_fee']= bcadd($paydata['apay_fee'],$temp['apay_fee'],2);
- $paydata['wpay_fee']= bcadd($paydata['wpay_fee'],$temp['wpay_fee'],2);
- $paydata['ainv_fee']=bcadd($paydata['ainv_fee'],$temp['ainv_fee'],2);
- $paydata['winv_fee']= bcadd($paydata['winv_fee'],$temp['winv_fee'],2);
- }
- if(empty($data))throw new Exception('采购单对账信息不能为空');
- $save= (new \app\admin\model\PayInfo)->saveAll($data);
- if($save->count()==0)throw new Exception('采购单对账信息录入失败');
- $payIn =Pay::create($paydata);
- if($payIn->id>0){
- $this->model->where(['id'=>$param['common_ids']])->save(["status"=>1]);
- CgdInfo::where(["sequenceNo"=>array_column($list,"cgdNo")])->save(['status'=>1]);
-
- $report =ReportCode::where(['cgdNo'=>array_column($list,'cgdNo')])->select();
- if($report->isEmpty()==false)$report->update(['payNo'=>Db::raw("CONCAT(payNo,',','{$payNo}')")]);
- $this->model->commit();
- }else throw new Exception('采购单对账单创建失败');
- }catch (\Exception $exception){
- $this->model->rollback();
- $this->error($exception->getMessage());
- }
- $this->success("对账单创建成功");
- }
-
- //通用订单修改对账单
- public function PaySave(){
- $param=$this->request->only(['common_ids'=>[],"payNo"=>""],'post','trim');
- $valid=Validate::rule([
- 'common_ids|通用订单id集合'=>'require|array',
- 'payNo|通用订单对账单编号'=>'require',
- ]);
- if($valid->check($param)==false)$this->error($valid->getError());
- $obj = $this->model->with(['cgd'])->where(['id'=>$param['common_ids']])->select();
- if($obj->isEmpty())$this->error('未找到相关数据');
- $payinfo = Pay::where(['payNo'=>$param['payNo'],'is_del'=>0])->findOrEmpty();
- if($payinfo->isEmpty())$this->error('未找到对账单数据');
- if($payinfo->status!=1)$this->error('对账单数据已提交审核');
- $cgdNo =payInfo::where([['payNo','=',$param['payNo']],['is_del','=',0]])->column('cgdNo');
-
- $list = $obj->toArray();
- $cgdArr=array_column($list,'cgdNo');
- $status = array_unique(array_column($list,'status'));
- $supplierNo = array_unique(array_column($list,'supplierNo'));
- $customerNo = array_unique(array_column($list,'customerNo'));
- $goodNo = array_unique(array_column($list,'goodNo'));
- $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('提交数据采购供应商存在不一致');
- if(count($customerNo)>1)$this->error('提交数据销售客户存在不一致');
- $good_info = \app\admin\model\Good::where(['spuCode'=>$goodNo])->column('status','spuCode');
- if(empty($good_info))$this->error("商品开票类目信息不存在");
- if(in_array(0,$good_info)){
- $key = array_search(0,$good_info);
- $this->error("商品 $key 采购未确认开票类目");
- }
- if(in_array(1,$good_info)){
- $key = array_search(1,$good_info);
- $this->error("商品 $key 财务未确认开票类目");
- }
- $add=[];
- $remove=[];
- if(!empty($cgdNo)){
- $remove = array_diff($cgdNo,$cgdArr);
- $add = array_diff($cgdArr,$cgdNo);
- }
- $data=[];
- $this->model->startTrans();
- try {
- $paydata=[
- 'total_fee'=>'0',
- 'supplierNo'=>$supplierNo,
- 'supplierName'=>$supplierName,
- 'companyNo'=>$customerNo,
- 'companyName'=>$customerName,
- 'wpay_fee'=>'0',
- 'apay_fee'=>'0',
- 'ainv_fee'=>'0',
- 'winv_fee'=>'0',
- 'remark'=>'',
- 'is_comon'=>1,
- 'status'=>1,
- 'updatetime'=>date('Y-m-d H:i:s'),
- ];
- foreach ($list as $item){
- if(!isset($item['cgd']) || empty($item['cgd'])) throw new Exception('采购单信息未找到');
- $value= $item['cgd'];
- if(!empty($add)&& in_array($value['sequenceNo'],$add)){
- $temp=[];
- $temp['cgdNo']=$value['sequenceNo'];
- $temp['total_fee']=$value['totalPrice'];
- $temp['apay_fee']=$value['apay_fee'];
- $temp['wpay_fee']=$value['wpay_fee'];
- $temp['winv_fee']=$value['winv_fee'];
- $temp['ainv_fee']=$value['ainv_fee'];
- $temp['payNo']=$param['payNo'];
- $temp['addtime']=date('Y-m-d H:i:s');
- $temp['updatetime']=date('Y-m-d H:i:s');
- $data[]=$temp;
- }
- $paydata['total_fee']= bcadd($paydata['total_fee'],$temp['total_fee'],2);
- $paydata['apay_fee']= bcadd($paydata['apay_fee'],$temp['apay_fee'],2);
- $paydata['wpay_fee']= bcadd($paydata['wpay_fee'],$temp['wpay_fee'],2);
- $paydata['ainv_fee']=bcadd($paydata['ainv_fee'],$temp['ainv_fee'],2);
- $paydata['winv_fee']= bcadd($paydata['winv_fee'],$temp['winv_fee'],2);
- }
- if(!empty($data)){
- $save= (new \app\admin\model\PayInfo)->saveAll($data);
- if($save->count()==0)throw new Exception('采购单对账信息录入失败');
- }
- $payIn =Pay::where($payinfo)->save($paydata);
- if($payIn){
- $this->model->where(['id'=>$param['common_ids']])->save(['status'=>1]);
- $this->model->where(['cgdNo'=>$remove])->save(['status'=>0]);
- CgdInfo::where(['sequenceNo'=>$add])->save(['status'=>1]);
- CgdInfo::where(['sequenceNo'=>$remove])->save(['status'=>0]);
- $report =ReportCode::where(['cgdNo'=>$add])->select();
- if($report->isEmpty()==false)$report->update(['payNo'=>Db::raw("CONCAT(payNo,',','{$param['payNo']}')")]);
- $reports =ReportCode::where(['cgdNo'=>$remove])->select();
- if($reports->isEmpty()==false)$reports->update(['payNo'=>Db::raw("REPLACE(payNo,',{$param['payNo']}','')")]);
- $this->model->commit();
- }else throw new Exception('采购单对账单创建失败');
- }catch (\Exception $exception){
- $this->model->rollback();
- $this->error($exception->getMessage());
- }
- $this->success('对账单创建成功');
- }
- //通用对账单信息
- public function PayInfo()
- {
- $param=$this->request->only(['payNo'=>''],"post","trim");
- $valid=Validate::rule(['payNo|通用订单对账单编号'=>'require']);
- if($valid->check($param)==false)$this->error($valid->getError());
- $payinfo =Pay::where(['payNo'=>$param['payNo'],'is_del'=>0])->append(["cgdlist","qrdlist"])->findOrEmpty();
- if($payinfo->isEmpty())$this->error("对账单数据未找到");
- if($payinfo->is_comon==0)$this->error('非通用对账单数据无法访问数据');
- $pay = PayInfo::where(['payNo'=>$param['payNo'],'is_del'=>0])->select();
- if($pay->isEmpty())$this->error('对账单订单数据未找到');
- $cgdlist = [];
- $qrdlist = [];
- $list=$this->model->with(['qrd','cgd'])->where(['cgdNo'=>array_column($pay->toArray(),"cgdNo")])->select()
- ->toArray();
- $qrdArr= array_column($list,"qrd","cgdNo");
- $cgdArr= array_column($list,"cgd","cgdNo");
- array_walk($qrdArr,function (&$item){
- $item['catInfo'] = json_decode($item['catInfo'],true);
- $item['orderTax'] = OrderCategory::where(['code' => $item['sequenceNo'], 'spuCode' => $item['goodNo']])
- ->field(['cat_code', 'cat_name', 'merge_code', 'short_name', 'tax', 'inv_good_name'])->findOrEmpty();
- });
- array_walk($cgdArr,function (&$item){
- $item['catInfo'] = json_decode($item['catInfo'],true);
- $item['orderTax'] = OrderCategory::where(['code' => $item['sequenceNo'], 'spuCode' => $item['goodNo']])
- ->field(['cat_code', 'cat_name', 'merge_code', 'short_name', 'tax', 'inv_good_name'])->findOrEmpty();
- });
- foreach ($pay as $item){
-
- if(isset($qrdArr[$item['cgdNo']])==false)continue;
- $cgdlist[] =$cgdArr[$item['cgdNo']];
- $invArr = Db::name('assoc')
- ->alias('a')
- ->leftJoin('invoice_good b', 'a.viceCode=b.invNo AND b.orderCode =a.orderCode')
- ->where(['a.status' => [1, 2], 'a.is_del' => 0, 'a.type' => 1, 'a.orderCode' => $qrdArr[$item['cgdNo']]['sequenceNo']])->group
- ('a.status')->column('sum(b.goodNum) as goodNum', 'a.status');
- $qrdArr[$item['cgdNo']]['goodNum'] -= $qrdArr[$item['cgdNo']]['thNum'];
- $qrdArr[$item['cgdNo']]['inv_num'] = $invArr[1]??0;
- $qrdArr[$item['cgdNo']]['ainv_num'] = $invArr[2]??0;
- $qrdArr[$item['cgdNo']]['winv_num'] = $qrdArr[$item['cgdNo']]['goodNum']-$qrdArr[$item['cgdNo']]['inv_num']-$qrdArr[$item['cgdNo']]['ainv_num'];
- $qrdlist[] =$qrdArr[$item['cgdNo']];
- }
- $payinfo->cgdlist =$cgdlist;
- $payinfo->qrdlist =$qrdlist;
- $payinfo['pay_type']=SupplierInfo::where('code',$payinfo['supplierNo'])->value('pay_method','');
- $tag =TagLog::alias('a')->leftJoin('order_tag b','a.tag_id=b.id')->where
- (['code'=>$param['payNo'],'a.status'=>1])->column('b.tag_name,a.tag_img,a.tag_remark','b.type');
- $payinfo['inv_tag_name']=$tag[2]['tag_name']??'';
- $payinfo['pay_tag_name']=$tag[1]['tag_name']??'';
- $payinfo['pay_tag_img']=$tag[1]['tag_img']??'';
- $payinfo['pay_tag_remark']=$tag[1]['tag_remark']??'';
- $payinfo['inv_tag_img']=$tag[2]['tag_img']??'';
- $payinfo['inv_tag_remark']=$tag[2]['tag_remark']??'';
- $this->success('获取成功',$payinfo);
- }
- /**
- * 对账单开票申请
- */
- public function invAdd(){
- $param=$this->request->only(["payNo"=>"","orderArr"=>[],"invtype"=>"","email"=>"","remark"=>"",
- 'exam_remark'=>''],"post","trim");
- $valid =Validate::rule([
- "payNo|对账单编号"=>"require",
- "orderArr|订单数据"=>"require|array",
- "invtype|开票类型"=>["require","in"=>$this->invName],
- "email|邮箱"=>"email",
- ]);
- if($valid->check($param)==false) $this->error($valid->getError());
- $valide = Validate::rule([
- "sequenceNo|销售订单号"=>"require",
- "inv_fee|商品开票金额"=>"require|float|gt:0",
- ]);
-
- $qrdinfo = QrdInfo::whereIn('sequenceNo',array_column($param['orderArr'],"sequenceNo"))
- ->column("id,sequenceNo,customerNo,customerName,companyNo,companyName,goodNo,goodNum,goodPrice,thNum,inv_fee,winv_fee,
- status,goodUnit,goodName,thirdCat,totalPrice,tax,platform_type","sequenceNo");
- if(empty($qrdinfo))$this->error("订单信息不存在");
- $goodInfo= \app\admin\model\Good::whereIn("spuCode",array_unique(array_column($qrdinfo,"goodNo")))
- ->column("inv_good_name,inv_cat_name,inv_cat_code,inv_tax,is_discount,addTax,inv_tag,status","spuCode");
- if(empty($goodInfo))$this->error('商品类目信息不存在');
- $assoc=[];
- $invoiceGood=[];
- $invNo=makeNo('INV');
- $invfee=array_sum(array_column($param['orderArr'],'inv_fee'));
- $companyArr = array_unique(array_column($qrdinfo,'companyName','companyNo'));
- $customerArr = array_unique(array_column($qrdinfo,'customerName','customerNo'));
- if(count($companyArr)>1)$this->error("订单业务公司不一致");
- if(count($customerArr)>1)$this->error("订单客户公司不一致");
- $company = CompanyInfo::whereIn("companyNo",array_keys($companyArr))->findOrEmpty();
- if($company->isEmpty())$this->error('订单业务公司开票信息未找到');
- $customer = CompanyInfo::whereIn('companyNo',array_keys($customerArr))->findOrEmpty();
- if($customer->isEmpty())$this->error('订单客户公司开票信息未找到');
- if (in_array($param['invtype'] ,
- [
- 'fully_digitalized_special_electronic' ,
- 'fully_digitalized_normal_electronic'
- ]
- )) {
- //全电发票限制金额
- if ($invfee > 10000000) {
- $this->error('开票面额不能超过一千万元');
- }
- }
- else {//全电发票
- if ($company['denomination'] != '' && $invfee > intval($company['denomination']) * 10000) {
- $this->error( "开票面额不能超过{$company['denomination']}万元");
- }
- }
- $platform = array_column($qrdinfo,'platform_type');
- if(count(array_unique($platform))!=1){
- $this->error("订单平台类型不一致");
- }
- foreach ($param['orderArr'] as $i=>$item){
- if($valide->check($item)==false) $this->error($valide->getError());
- if(!isset($qrdinfo[$item['sequenceNo']]))$this->error("订单 {$item['sequenceNo']} 信息不存在");
- if($qrdinfo[$item['sequenceNo']]['status']==2)$this->error("订单 {$item['sequenceNo']} 不参与对账");
- if($qrdinfo[$item['sequenceNo']]['winv_fee']<$item['inv_fee'])$this->error("订单 {$item['sequenceNo']} 待开票金额不足");
- // $total = InvoicePool::alias("a")->leftJoin('invoice_good b', 'a.invNo=b.invNo and b.is_del=0')
- // ->where(['a.status' => [0, 1, 2, 3, 4], 'b.orderCode' => $item['sequenceNo']])
- // ->sum('b.goodNum');
- //if($total+$item['num']>($qrdinfo[$item['sequenceNo']]['goodNum']-$qrdinfo[$item['sequenceNo']]['thNum']))$this->error("订单 {$item['sequenceNo']} 待开票数量不足");
- if(!isset($goodInfo[$qrdinfo[$item['sequenceNo']]['goodNo']]))$this->error("订单 {$item['sequenceNo']} 商品类目信息不存在");
- if($goodInfo[$qrdinfo[$item['sequenceNo']]['goodNo']]['status']!=2)$this->error("订单 {$item['sequenceNo']} 未确认开票类目信息");
- // $num = \app\admin\model\Assoc::alias('a')
- // ->leftJoin('invoice_good b', 'a.viceCode=b.invNo AND b.orderCode=a.orderCode')
- // ->where(['a.status' => [1, 2], 'a.is_del' => 0, 'a.type' => 1, 'a.orderCode' => $item['sequenceNo']])
- // ->sum('b.goodNum');
- //if($num+$item['num']>($qrdinfo[$item['sequenceNo']]['goodNum']-$qrdinfo[$item['sequenceNo']]['thNum']))$this->error("订单 {$item['sequenceNo']} 待开票数量不足");
- $assoc[]=[
- 'assocNo'=>substr(makeNo('AS'),0,-4).str_pad(strval($i+1),4,'0',STR_PAD_LEFT),
- 'apply_id'=>$this->uid,
- 'apply_name'=>$this->uname,
- 'type'=>1,
- 'orderCode'=>$item['sequenceNo'],
- 'customerNo'=>$qrdinfo[$item['sequenceNo']]['customerNo'],
- 'viceCode'=>$invNo,
- 'order_total'=>$qrdinfo[$item['sequenceNo']]['totalPrice'],
- 'vice_total'=>$invfee,
- 'cancel_fee'=>$item['inv_fee'],
- 'status'=>1,
- 'addtime'=>date('Y-m-d H:i:s'),
- 'updatetime'=>date('Y-m-d H:i:s')
- ];
- $invoiceGood[]=[
- 'invNo'=>$invNo,
- 'orderCode'=>$item['sequenceNo'],
- 'goodNo'=>$qrdinfo[$item['sequenceNo']]['goodNo'],
- 'goodName'=>$goodInfo[$qrdinfo[$item['sequenceNo']]['goodNo']]['inv_good_name']??$qrdinfo[$item['sequenceNo']]['goodName'] ,
- 'catName'=>$goodInfo[$qrdinfo[$item['sequenceNo']]['goodNo']]['inv_cat_name']??$qrdinfo[$item['sequenceNo']]['thirdCat'],
- 'catNo'=>$goodInfo[$qrdinfo[$item['sequenceNo']]['goodNo']]['inv_cat_code'],
- 'goodPrice'=>$qrdinfo[$item['sequenceNo']]['goodPrice'],
- 'unitName'=>$qrdinfo[$item['sequenceNo']]['goodUnit'],
- 'goodNum'=>bcdiv($item['inv_fee'],$qrdinfo[$item['sequenceNo']]['goodPrice'],8),
- 'specInfo'=>$item['sequenceNo'],
- 'totalPrice'=>$item['inv_fee'],
- 'totalTax'=>$item['inv_fee'],
- 'tax'=>$goodInfo[$qrdinfo[$item['sequenceNo']]['goodNo']]['inv_tax']??$qrdinfo[$item['sequenceNo']]['tax'],
- 'is_discount'=>$goodInfo[$qrdinfo[$item['sequenceNo']]['goodNo']]['is_discount'],
- 'addTax'=>$goodInfo[$qrdinfo[$item['sequenceNo']]['goodNo']]['addTax'],
- 'inv_tag'=>$goodInfo[$qrdinfo[$item['sequenceNo']]['goodNo']]['inv_tag'],
- 'taxPrice'=>round($item['inv_fee']*($goodInfo[$qrdinfo[$item['sequenceNo']]['goodNo']]['inv_tax']??$qrdinfo[$item['sequenceNo']]['tax']),2),
- 'addtime'=>date('Y-m-d H:i:s'),
- 'updatetime'=>date('Y-m-d H:i:s')
- ];
- $qrdinfo[$item['sequenceNo']]["inv_fee"] = $qrdinfo[$item['sequenceNo']]['inv_fee']+$item['inv_fee'];
- $qrdinfo[$item['sequenceNo']]["winv_fee"] = $qrdinfo[$item['sequenceNo']]['winv_fee']-$item['inv_fee'];
- $qrdinfo[$item['sequenceNo']]["inv_status"] = 2;
- $qrdinfo[$item['sequenceNo']]["status"] = 1;
- }
- $inv=[
- 'invNo'=>$invNo,
- 'inv_value'=>$invfee,
- 'inv_out'=> array_keys($companyArr)[0],
- 'inv_company'=> $company['invoice_title']??'',
- 'apply_id'=>$this->uid,
- 'is_comon'=>1,
- 'apply_name'=>$this->uname,
- 'inv_type'=>$param['invtype'],//发票类型 专用 普通 电子专用 电子普通 全电子票
- 'open_type'=>0, //开票类型 金税开票 金税线下 纯线下
- 'platform_type'=>$platform[0]??0, //开票类型 金税开票 金税线下 纯线下
- 'is_ticket'=>$company['out_ticket'], //是否支持金税开票
- 'company_type'=> $customer['company_type']??'01', //类型01 企业,02 机关事业单位,03 个人, 04 其他
- 'remark'=>$param['remark'], //申请备注
- 'exam_remark'=>$param['exam_remark'], //审核备注
- 'ainv_fee'=>0,//已核销金额
- 'winv_fee'=>$invfee,//未核销金额
- 'status'=>1,// 带财务审核
- 'is_del'=>0,
- 'email'=>$param['email'],
- 'addtime'=>date('Y-m-d H:i:s'),
- 'updatetime'=>date('Y-m-d H:i:s')
- ];
- $invinfo=[
- 'buyer_title'=>$customer['invoice_title'],
- 'buyer_code'=>$customer['company_license'],
- 'buyer_addr'=>$customer['company_address'],
- 'buyer_mobile'=>$customer['mobile'],
- 'buyer_bank'=>$customer['bank_name'],
- 'buyer_bankNo'=>$customer['bankNo'],
- 'seller_title'=>$company['invoice_title'],
- 'seller_code'=>$company['company_license'],
- 'seller_addr'=>$company['company_address'],
- 'seller_mobile'=>$company['mobile'],
- 'seller_bank'=>$company['bank_name'],
- 'seller_bankNo'=>$company['bankNo'],
- 'invNo'=>$invNo,
- 'voider'=>$company['voider'],
- 'payee'=>$company['payee'],
- 'drawer'=>$company['drawer'],
- 'reviewer'=>$company['reviewer'],
- 'ownerPlace'=>$company['ownerPlace'],
- 'addtime'=>date('Y-m-d H:i:s')
- ];
- $this->model->startTrans();
- try{
- $assocSa=(new \app\admin\model\Assoc)->saveAll($assoc);
- if($assocSa->isEmpty()) throw new Exception("开票关联数据生成失败");
- $goodInv = (new \app\admin\model\InvoiceGood)->saveAll($invoiceGood);
- if($goodInv->isEmpty()) throw new Exception('开票商品明细数据生成失败');
- $qrdSave = (new \app\admin\model\QrdInfo)->saveAll($qrdinfo);
- if($qrdSave->isEmpty()) throw new Exception('销售单信息更新失败');
- $invoice = InvoicePool::create($inv);
- if($invoice->isEmpty()) throw new Exception('开票信息创建失败');
- $invoiceinf = InvoicePoolInfo::create($invinfo);
- if($invoiceinf->isEmpty()) throw new Exception('开票信息创建失败');
- $payIn = InvoicePay::create(["invNo"=>$invNo,"payNo"=>$param["payNo"]]);
- if($payIn->isEmpty()) throw new Exception('开票信息创建失败');
- $report =ReportCode::where(['qrdNo'=>array_column($param['orderArr'],'sequenceNo')])->select();
- if($report->isEmpty()==false)$report->update(["invNo"=>Db::raw("CONCAT(invNo,',','{$invNo}')")]);
- $this->model->commit();
-
- }catch (\Exception $exception){
- $this->model->rollback();
- $this->error($exception->getMessage());
- }
- $this->success("开票申请创建成功");
- }
-
- /**
- * 对账单开票申请
- */
- public function invAddT(){
- $param=$this->request->only(['payNo'=>'','orderArr'=>[],'invtype'=>'','email'=>'','remark'=>'',
- 'exam_remark'=>''],'post','trim');
- $valid =Validate::rule([
- 'payNo|对账单编号'=>'require',
- 'orderArr|订单数据'=>'require|array',
- 'invtype|开票类型'=>['require','in'=>$this->invName],
- 'email|邮箱'=>'email',
- ]);
- if($valid->check($param)==false) $this->error($valid->getError());
- $valide = Validate::rule([
- 'sequenceNo|销售订单号'=>'require',
- 'num|商品开票数量'=>'require|float|gt:0',
- 'inv_fee|商品开票金额'=>'require|float|gt:0',
- ]);
-
- $qrdinfo = QrdInfo::whereIn('sequenceNo',array_column($param['orderArr'],'sequenceNo'))
- ->column('id,sequenceNo,customerNo,customerName,companyNo,companyName,goodNo,goodNum,thNum,inv_fee,winv_fee,
- status,goodUnit,goodName,thirdCat,totalPrice,tax,platform_type','sequenceNo');
- if(empty($qrdinfo))$this->error('订单信息不存在');
- $goodInfo= \app\admin\model\Good::whereIn('spuCode',array_unique(array_column($qrdinfo,'goodNo')))
- ->column('inv_good_name,inv_cat_name,inv_cat_code,inv_tax,is_discount,addTax,inv_tag,status','spuCode');
- if(empty($goodInfo))$this->error('商品类目信息不存在');
- $assoc=[];
- $invoiceGood=[];
- $invNo=makeNo('INV');
- $invfee=array_sum(array_column($param['orderArr'],'inv_fee'));
- $companyArr = array_unique(array_column($qrdinfo,'companyName','companyNo'));
- $customerArr = array_unique(array_column($qrdinfo,'customerName','customerNo'));
- if(count($companyArr)>1)$this->error('订单业务公司不一致');
- if(count($customerArr)>1)$this->error('订单客户公司不一致');
- $company = CompanyInfo::whereIn('companyNo',array_keys($companyArr))->findOrEmpty();
- if($company->isEmpty())$this->error('订单业务公司开票信息未找到');
- $customer = CompanyInfo::whereIn('companyNo',array_keys($customerArr))->findOrEmpty();
- if($customer->isEmpty())$this->error('订单客户公司开票信息未找到');
- if (in_array($param['invtype'] ,
- [
- 'fully_digitalized_special_electronic' ,
- 'fully_digitalized_normal_electronic'
- ]
- )) {
- //全电发票限制金额
- if ($invfee > 10000000) {
- $this->error('开票面额不能超过一千万元');
- }
- }
- else {//全电发票
- if ($company['denomination'] != '' && $invfee > intval($company['denomination']) * 10000) {
- $this->error( "开票面额不能超过{$company['denomination']}万元");
- }
- }
- $platform = array_column($qrdinfo,'platform_type');
- if(count(array_unique($platform))!=1){
- $this->error('订单平台类型不一致');
- }
- foreach ($param['orderArr'] as $i=>$item){
- if($valide->check($item)==false) $this->error($valide->getError());
- if(!isset($qrdinfo[$item['sequenceNo']]))$this->error("订单 {$item['sequenceNo']} 信息不存在");
- if($qrdinfo[$item['sequenceNo']]['status']==2)$this->error("订单 {$item['sequenceNo']} 不参与对账");
- if($qrdinfo[$item['sequenceNo']]['winv_fee']<$item['inv_fee'])$this->error("订单 {$item['sequenceNo']} 待开票金额不足");
- $total = InvoicePool::alias('a')->leftJoin('invoice_good b', 'a.invNo=b.invNo and b.is_del=0')
- ->where(['a.status' => [0, 1, 2, 3, 4], 'b.orderCode' => $item['sequenceNo']])
- ->sum('b.goodNum');
- if($total+$item['num']>($qrdinfo[$item['sequenceNo']]['goodNum']-$qrdinfo[$item['sequenceNo']]['thNum']))$this->error("订单 {$item['sequenceNo']} 待开票数量不足");
- if(!isset($goodInfo[$qrdinfo[$item['sequenceNo']]['goodNo']]))$this->error("订单 {$item['sequenceNo']} 商品类目信息不存在");
- if($goodInfo[$qrdinfo[$item['sequenceNo']]['goodNo']]['status']!=2)$this->error("订单 {$item['sequenceNo']} 未确认开票类目信息");
- $num = \app\admin\model\Assoc::alias('a')
- ->leftJoin('invoice_good b', 'a.viceCode=b.invNo AND b.orderCode=a.orderCode')
- ->where(['a.status' => [1, 2], 'a.is_del' => 0, 'a.type' => 1, 'a.orderCode' => $item['sequenceNo']])
- ->sum('b.goodNum');
- if($num+$item['num']>($qrdinfo[$item['sequenceNo']]['goodNum']-$qrdinfo[$item['sequenceNo']]['thNum']))$this->error("订单 {$item['sequenceNo']} 待开票数量不足");
- $assoc[]=[
- 'assocNo'=>substr(makeNo('AS'),0,-4).str_pad(strval($i+1),4,'0',STR_PAD_LEFT),
- 'apply_id'=>$this->uid,
- 'apply_name'=>$this->uname,
- 'type'=>1,
- 'orderCode'=>$item['sequenceNo'],
- 'customerNo'=>$qrdinfo[$item['sequenceNo']]['customerNo'],
- 'viceCode'=>$invNo,
- 'order_total'=>$qrdinfo[$item['sequenceNo']]['totalPrice'],
- 'vice_total'=>$invfee,
- 'cancel_fee'=>$item['inv_fee'],
- 'status'=>1,
- 'addtime'=>date('Y-m-d H:i:s'),
- 'updatetime'=>date('Y-m-d H:i:s')
- ];
- $invoiceGood[]=[
- 'invNo'=>$invNo,
- 'orderCode'=>$item['sequenceNo'],
- 'goodNo'=>$qrdinfo[$item['sequenceNo']]['goodNo'],
- 'goodName'=>$goodInfo[$qrdinfo[$item['sequenceNo']]['goodNo']]['inv_good_name']??$qrdinfo[$item['sequenceNo']]['goodName'] ,
- 'catName'=>$goodInfo[$qrdinfo[$item['sequenceNo']]['goodNo']]['inv_cat_name']??$qrdinfo[$item['sequenceNo']]['thirdCat'],
- 'catNo'=>$goodInfo[$qrdinfo[$item['sequenceNo']]['goodNo']]['inv_cat_code'],
- 'goodPrice'=>round($item['inv_fee']/$item['num'],8),
- 'unitName'=>$qrdinfo[$item['sequenceNo']]['goodUnit'],
- 'goodNum'=>$item['num'],
- 'specInfo'=>$item['sequenceNo'],
- 'totalPrice'=>$item['inv_fee'],
- 'totalTax'=>$item['inv_fee'],
- 'tax'=>$goodInfo[$qrdinfo[$item['sequenceNo']]['goodNo']]['inv_tax']??$qrdinfo[$item['sequenceNo']]['tax'],
- 'is_discount'=>$goodInfo[$qrdinfo[$item['sequenceNo']]['goodNo']]['is_discount'],
- 'addTax'=>$goodInfo[$qrdinfo[$item['sequenceNo']]['goodNo']]['addTax'],
- 'inv_tag'=>$goodInfo[$qrdinfo[$item['sequenceNo']]['goodNo']]['inv_tag'],
- 'taxPrice'=>round($item['inv_fee']*($goodInfo[$qrdinfo[$item['sequenceNo']]['goodNo']]['inv_tax']??$qrdinfo[$item['sequenceNo']]['tax']),2),
- 'addtime'=>date('Y-m-d H:i:s'),
- 'updatetime'=>date('Y-m-d H:i:s')
- ];
- $qrdinfo[$item['sequenceNo']]['inv_fee'] = $qrdinfo[$item['sequenceNo']]['inv_fee']+$item['inv_fee'];
- $qrdinfo[$item['sequenceNo']]['winv_fee'] = $qrdinfo[$item['sequenceNo']]['winv_fee']-$item['inv_fee'];
- $qrdinfo[$item['sequenceNo']]['inv_status'] = 2;
- $qrdinfo[$item['sequenceNo']]['status'] = 1;
- }
- $inv=[
- 'invNo'=>$invNo,
- 'inv_value'=>$invfee,
- 'inv_out'=> array_keys($companyArr)[0],
- 'inv_company'=> $company['invoice_title']??'',
- 'apply_id'=>$this->uid,
- 'is_comon'=>1,
- 'apply_name'=>$this->uname,
- 'inv_type'=>$param['invtype'],//发票类型 专用 普通 电子专用 电子普通 全电子票
- 'open_type'=>0, //开票类型 金税开票 金税线下 纯线下
- 'platform_type'=>$platform[0]??0, //开票类型 金税开票 金税线下 纯线下
- 'is_ticket'=>$company['out_ticket'], //是否支持金税开票
- 'company_type'=> $customer['company_type']??'01', //类型01 企业,02 机关事业单位,03 个人, 04 其他
- 'remark'=>$param['remark'], //申请备注
- 'exam_remark'=>$param['exam_remark'], //审核备注
- 'ainv_fee'=>0,//已核销金额
- 'winv_fee'=>$invfee,//未核销金额
- 'status'=>1,// 带财务审核
- 'is_del'=>0,
- 'email'=>$param['email'],
- 'addtime'=>date('Y-m-d H:i:s'),
- 'updatetime'=>date('Y-m-d H:i:s')
- ];
- $invinfo=[
- 'buyer_title'=>$customer['invoice_title'],
- 'buyer_code'=>$customer['company_license'],
- 'buyer_addr'=>$customer['company_address'],
- 'buyer_mobile'=>$customer['mobile'],
- 'buyer_bank'=>$customer['bank_name'],
- 'buyer_bankNo'=>$customer['bankNo'],
- 'seller_title'=>$company['invoice_title'],
- 'seller_code'=>$company['company_license'],
- 'seller_addr'=>$company['company_address'],
- 'seller_mobile'=>$company['mobile'],
- 'seller_bank'=>$company['bank_name'],
- 'seller_bankNo'=>$company['bankNo'],
- 'invNo'=>$invNo,
- 'voider'=>$company['voider'],
- 'payee'=>$company['payee'],
- 'drawer'=>$company['drawer'],
- 'reviewer'=>$company['reviewer'],
- 'ownerPlace'=>$company['ownerPlace'],
- 'addtime'=>date('Y-m-d H:i:s')
- ];
- $this->model->startTrans();
- try{
- $assocSa=(new \app\admin\model\Assoc)->saveAll($assoc);
- if($assocSa->isEmpty()) throw new Exception('开票关联数据生成失败');
- $goodInv = (new \app\admin\model\InvoiceGood)->saveAll($invoiceGood);
- if($goodInv->isEmpty()) throw new Exception('开票商品明细数据生成失败');
- $qrdSave = (new \app\admin\model\QrdInfo)->saveAll($qrdinfo);
- if($qrdSave->isEmpty()) throw new Exception('销售单信息更新失败');
- $invoice = InvoicePool::create($inv);
- if($invoice->isEmpty()) throw new Exception('开票信息创建失败');
- $invoiceinf = InvoicePoolInfo::create($invinfo);
- if($invoiceinf->isEmpty()) throw new Exception('开票信息创建失败');
- $payIn = InvoicePay::create(['invNo'=>$invNo,'payNo'=>$param['payNo']]);
- if($payIn->isEmpty()) throw new Exception('开票信息创建失败');
- $report =ReportCode::where(['qrdNo'=>array_column($param['orderArr'],'sequenceNo')])->select();
- if($report->isEmpty()==false)$report->update(['invNo'=>Db::raw("CONCAT(invNo,',','{$invNo}')")]);
- $this->model->commit();
-
- }catch (\Exception $exception){
- $this->model->rollback();
- $this->error($exception->getMessage());
- }
- $this->success('开票申请创建成功');
- }
-
- }
|