|
@@ -483,4 +483,189 @@ class ComonOrder extends Base
|
|
|
$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('订单平台类型不一致');
|
|
|
+// return error_show(1004,'订单平台类型不一致');
|
|
|
+ }
|
|
|
+ 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('开票申请创建成功');
|
|
|
+ }
|
|
|
+
|
|
|
}
|