|
@@ -0,0 +1,297 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+
|
|
|
+namespace app\cxinv\controller;
|
|
|
+
|
|
|
+use app\cxinv\model\InvoiceGood;
|
|
|
+use app\cxinv\model\InvoiceOrder;
|
|
|
+use app\cxinv\model\OrderCategory;
|
|
|
+use app\cxinv\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"=>"",'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","like","%{$param['merge_code']}%"];
|
|
|
+ if($param["inv_good_name"]!=="") $where[]=["c.inv_good_name","like","%{$param["inv_good_name"]}%"];
|
|
|
+ if($param["good_name"]!=="") $where[]=["b.goodName","like","%{$param["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();
|
|
|
+ $list->each(function (&$item){
|
|
|
+ $item['balance_amount']= bcsub($item['totalPrice'],InvoiceOrder::getInvoiceOrderTotalFee($item['sequenceNo']),2);
|
|
|
+ });
|
|
|
+ return 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();
|
|
|
+ $list->each(function (&$item){
|
|
|
+ $item['balance_amount']= bcsub($item['totalPrice'],InvoiceOrder::getInvoiceOrderTotalFee($item['sequenceNo']),2);
|
|
|
+ });
|
|
|
+ return 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)) return error($valid->getError());
|
|
|
+ $valids=Validate::rule([
|
|
|
+ 'code|订单号' => 'require|max:255',
|
|
|
+ 'spuCode|商品编码' => 'require|max:255',
|
|
|
+ 'good_name|商品名称' => 'require|max:255',
|
|
|
+ '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()) return error("发票明细不存在");
|
|
|
+ $save=[];
|
|
|
+ $amount = $item['balance_amount'];
|
|
|
+ foreach ($param["orderArr"] as $k=>$v){
|
|
|
+ if(!$valids->check($v)) return error($valids->getError());
|
|
|
+ $taxInfo = OrderCategory::where(['code'=>$v['code'],'spuCode'=>$v['spuCode']])->findOrEmpty();
|
|
|
+ if($taxInfo->isEmpty()) return 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(str_replace("%","",$item['tax'])!=$taxInfo['tax']){
|
|
|
+ $diff["tax"] = [
|
|
|
+ "inv_tax"=>str_replace('%','',$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"],
|
|
|
+ "good_name"=>$v['good_name'],
|
|
|
+ "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"],
|
|
|
+ "apply_id"=>$this->uid,
|
|
|
+ "apply_name"=>$this->uname
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ $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();
|
|
|
+ return error($e->getMessage());
|
|
|
+ }
|
|
|
+ return success('添加成功');
|
|
|
+ }
|
|
|
+ //删除发票明细对应关系
|
|
|
+ public function delete(){
|
|
|
+ $param = $this->request->param(["id"=>""],"post","trim");
|
|
|
+ $valid=Validate::rule([
|
|
|
+ 'id|发票明细对应id' => 'require|number|gt:0',
|
|
|
+ ]);
|
|
|
+ if(!$valid->check($param)) return error($valid->getError());
|
|
|
+ $info = \app\admin\model\InvoiceOrder::where(['id'=>$param["id"]])->findOrEmpty();
|
|
|
+ if($info->isEmpty()) return error("发票明细对应关系不存在");
|
|
|
+ $item = \app\admin\model\InvoiceItem::where(['id'=>$info['itemId']])->findOrEmpty();
|
|
|
+ if($item->isEmpty()) return error("发票明细不存在");
|
|
|
+ InvoiceOrder::startTrans();
|
|
|
+ try{
|
|
|
+ $res = $info->delete();
|
|
|
+ if($res){
|
|
|
+ $count = \app\admin\model\InvoiceOrder::where(['itemId'=>$info['itemId']])->count();
|
|
|
+ $item->balance_amount = bcadd($item['balance_amount'],bcsub($info['total_amount'],$info['balance_amount'],2),2);
|
|
|
+ $item->status = ($count==0) ? 0 : ($item->balance_amount==$item->total_amount?0:($item->balance_amount>0?2:1));
|
|
|
+ $item->save();
|
|
|
+ }else throw new \Exception("删除失败");
|
|
|
+ InvoiceOrder::commit();
|
|
|
+ }catch (\Exception $e){
|
|
|
+ InvoiceOrder::rollback();
|
|
|
+ return error($e->getMessage());
|
|
|
+ }
|
|
|
+ return success('删除成功');
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public function addRemark(){
|
|
|
+ $param = $this->request->param(['id'=>''],'post','trim');
|
|
|
+ $valid=Validate::rule([
|
|
|
+ 'id|发票明细对应id' => 'require|number|gt:0',
|
|
|
+ 'remark|备注' => 'require|max:255',
|
|
|
+ ]);
|
|
|
+ if(!$valid->check($param)) return error($valid->getError());
|
|
|
+ $info = \app\admin\model\InvoiceOrder::where(['id'=>$param['id']])->findOrEmpty();
|
|
|
+ if($info->isEmpty()) return error('发票明细对应关系不存在');
|
|
|
+ $item = \app\admin\model\InvoiceItem::where(['id'=>$info['itemId']])->findOrEmpty();
|
|
|
+ if($item->isEmpty()) return error('发票明细不存在');
|
|
|
+ $info->startTrans();
|
|
|
+ try{
|
|
|
+ $info->remark = $param['remark'] ?? '';
|
|
|
+ $res = $info->save();
|
|
|
+ if($res==false)throw new \Exception('备注添加失败');
|
|
|
+ $info->commit();
|
|
|
+ }catch (\Exception $e){
|
|
|
+ $info->rollback();
|
|
|
+ return error($e->getMessage());
|
|
|
+ }
|
|
|
+ return 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();
|
|
|
+ return success('获取成功',$list);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function importOrder(){
|
|
|
+ $param=$this->request->param([ "order_type"=>"","list"=>[]],"post","trim");
|
|
|
+ $valid = Validate::rule([
|
|
|
+ 'order_type|订单类型' => 'require|in:1,2',
|
|
|
+ 'list|发票明细' => 'require|array',
|
|
|
+ ]);
|
|
|
+ if(!$valid->check($param)) return error($valid->getError());
|
|
|
+ $listValid = Validate::rule([
|
|
|
+ "itemId|发票明细ID"=>'require|number|gt:0',
|
|
|
+ 'code|订单编码' => 'require|max:20',
|
|
|
+ 'num|数量' => 'float|egt:0',
|
|
|
+ 'good_price|商品价格' => 'float|egt:0',
|
|
|
+ 'total_amount|总金额' => 'require|float|gt:0',
|
|
|
+ 'remark|备注' =>'max:255',
|
|
|
+ ]);
|
|
|
+ $itemArr= \app\admin\model\InvoiceItem::whereIn('id',array_column($param['list'],"itemId"))->column("id,invoiceCode,order_type,balance_amount,cat_code,tax","id");
|
|
|
+ $orderInfo=[];
|
|
|
+ $orderItem=[];
|
|
|
+ foreach($param["list"] as $v){
|
|
|
+ if(!$listValid->check($v)) return error($listValid->getError());
|
|
|
+ if(!isset($itemArr[$v['itemId']])) return error("发票明细不存在");
|
|
|
+ if(!isset($orderInfo[$itemArr[$v['itemId']]['invoiceCode'].$v["code"]])){
|
|
|
+ $orderInfo[$itemArr[$v['itemId']]['invoiceCode'].$v['code']]= \app\admin\model\InvoiceOrder::getOrderInfo( $itemArr[$v['itemId']]['invoiceCode'],$v['code'],$param['order_type']);
|
|
|
+ }
|
|
|
+
|
|
|
+ if($orderInfo[$itemArr[$v['itemId']]['invoiceCode'].$v['code']]->isEmpty()) return error("发票{$itemArr[$v['itemId']]['invoiceCode']}关联订单{$v['code']}信息不存在");
|
|
|
+ if($orderInfo[$itemArr[$v['itemId']]['invoiceCode'].$v['code']]['balance_amount']<$v['total_amount']) return error("{$v["code"]}订单金额不足");
|
|
|
+ if($itemArr[$v['itemId']]['balance_amount']<=0) return error("发票明细ID {$v["itemId"]} 可关联金额不足");
|
|
|
+ if($orderInfo[$itemArr[$v['itemId']]['invoiceCode'].$v['code']]['merge_code']=="") return error("{$v["code"]}订单税目信息不存在");
|
|
|
+ if($itemArr[$v['itemId']]['balance_amount']<$v['total_amount']){
|
|
|
+ $balance = bcsub($v['total_amount'],$itemArr[$v['itemId']]['balance_amount'],2);
|
|
|
+ $itemArr[$v['itemId']]['balance_amount']='0';
|
|
|
+ }else{
|
|
|
+ $balance = '0';
|
|
|
+ $itemArr[$v['itemId']]['balance_amount'] = bcsub($itemArr[$v['itemId']]['balance_amount'],$v['total_amount'],2);
|
|
|
+ }
|
|
|
+ $orderInfo[$itemArr[$v['itemId']]['invoiceCode'].$v['code']]['balance_amount'] =
|
|
|
+ bcsub($orderInfo[$itemArr[$v['itemId']]['invoiceCode'].$v['code']]['balance_amount'],bcsub($v['total_amount'],$balance,2),2);
|
|
|
+ $tax = $orderInfo[$itemArr[$v['itemId']]['invoiceCode'].$v['code']]['tax'];
|
|
|
+ $merge_code = $orderInfo[$itemArr[$v['itemId']]['invoiceCode'].$v['code']]['merge_code'];
|
|
|
+ $diff=[];
|
|
|
+ $tax_diff=1;
|
|
|
+ $cat_diff=1;
|
|
|
+ if(str_replace('%','',$itemArr[$v['itemId']]['tax'])!=$tax ){
|
|
|
+ $diff['tax'] = [
|
|
|
+ 'inv_tax'=>str_replace('%','',$itemArr[$v['itemId']]['tax']),
|
|
|
+ 'order_tax'=>$tax ,
|
|
|
+ ];
|
|
|
+ $tax_diff=2;
|
|
|
+ }
|
|
|
+ if($itemArr[$v['itemId']]['cat_code']!=$merge_code){
|
|
|
+ $diff['cat_code'] = [
|
|
|
+ 'inv_cat_code'=>$itemArr[$v['itemId']]['cat_code'],
|
|
|
+ 'order_cat_code'=>$merge_code,
|
|
|
+ ];
|
|
|
+ $cat_diff=2;
|
|
|
+ }
|
|
|
+ $itemArr[$v['itemId']]['status'] = ($itemArr[$v['itemId']]['balance_amount']>0?2:1);
|
|
|
+ $orderItem[]=[
|
|
|
+ 'itemId'=>$v['itemId'],
|
|
|
+ 'code'=>$v['code'],
|
|
|
+ 'spuCode'=>$orderInfo[$itemArr[$v['itemId']]['invoiceCode'].$v['code']]['goodNo'],
|
|
|
+ 'good_name'=>$orderInfo[$itemArr[$v['itemId']]['invoiceCode'].$v['code']]['goodName'],
|
|
|
+ 'num'=>$v['num']??0,
|
|
|
+ 'good_price'=>$v['good_price']??0,
|
|
|
+ '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'],
|
|
|
+ 'apply_id'=>$this->uid,
|
|
|
+ 'apply_name'=>$this->uname
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ $orderItemModel = new \app\admin\model\InvoiceItem();
|
|
|
+ $orderItemModel->startTrans();
|
|
|
+ try{
|
|
|
+ $num=$orderItemModel->saveAll(array_values($itemArr));
|
|
|
+ if(count($num)>0){
|
|
|
+ (new InvoiceOrder())->saveAll($orderItem);
|
|
|
+ }else throw new \Exception("导入失败");
|
|
|
+ }catch (\Exception $e){
|
|
|
+ $orderItemModel->rollback();
|
|
|
+ return error($e->getMessage());
|
|
|
+ }
|
|
|
+ $orderItemModel->commit();
|
|
|
+ return success("导入成功");
|
|
|
+ }
|
|
|
+
|
|
|
+}
|