123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370 |
- <?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 app\cxinv\model\PayInvoice;use think\App;
- use think\facade\Validate;
- class InvoiceItem extends Base{
- public function __construct(App $app) {
- parent::__construct($app);
- $this->model = new \app\cxinv\model\InvoiceItem();
- }
- //查询
- 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","in",$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,
- b.ainv_fee,b.winv_fee,b.apay_fee,b.wpay_fee,c.merge_code,c.cat_code,c.cat_name,c.short_name,c.tax,c.inv_good_name")
- ->select();
- 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();
- 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 = $this->model->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 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 = InvoiceOrder::where(['id'=>$param["id"]])->findOrEmpty();
- if($info->isEmpty()) return error("发票明细对应关系不存在");
- $item = $this->model->where(['id'=>$info['itemId']])->findOrEmpty();
- if($item->isEmpty()) return error("发票明细不存在");
- InvoiceOrder::startTrans();
- try{
- $res = $info->delete();
- if($res){
- $count = 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 = InvoiceOrder::where(['id'=>$param['id']])->findOrEmpty();
- if($info->isEmpty()) return error('发票明细对应关系不存在');
- $item = $this->model->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('备注添加成功');
- }
- /**
- * 发票明细正负数合并保留整数
- * @param $main_id 正数id
- * $voice_id 负数id
- */
- public function mergeItem(){
- $data=$this->request->post("data",[]);
- if(empty($data)) return error('参数错误');
- $valid=Validate::rule([
- 'main_id|正数id' => 'require|number|gt:0',
- 'voice_id|负数id' => 'require|number|gt:0',
- ]);
- $mainArr = $this->model->where(['id'=>array_column($data,"main_id")])->column("id,amount,invoiceCode,total_amount,balance_amount","id");
- $voiceArr = $this->model->where(['id'=>array_column($data,"voice_id")])->column("id,invoiceCode,amount,total_amount,balance_amount","id");
- $inoviceCode = array_unique(array_merge(array_column($voiceArr,"invoiceCode"),array_column($mainArr,'invoiceCode')));
- if(count($inoviceCode)>1) return error("正负数发票明细回票申请编号不一致");
- $temop=[];
- $remove=[];
- foreach ($data as $k=>$v){
- if(!$valid->check($v)) return error($valid->getError());
- if(!isset($mainArr[$v['main_id']])) return error("正数发票明细不存在");
- if(!isset($voiceArr[$v['voice_id']])) return error("负数发票明细不存在");
- if($mainArr[$v['main_id']]['amount']<0) return error("正数发票明细金额必须大于0");
- if($voiceArr[$v['voice_id']]['amount']>=0) return error("负数发票明细金额必须小于0");
- if($mainArr[$v['main_id']]['invoiceCode']!=$voiceArr[$v['voice_id']]['invoiceCode']) return error("正负数发票明细回票申请编号不一致");
- if(abs($voiceArr[$v['voice_id']]['amount'])> $mainArr[$v['main_id']]['amount']) return error("负数发票明细金额不能大于正数发票明细金额");
- $temop[]=[
- "id"=>$v['main_id'],
- "amount"=>bcadd($mainArr[$v['main_id']]['amount'],$voiceArr[$v['voice_id']]['amount'],2),
- "balance_amount"=>bcadd($mainArr[$v['main_id']]['balance_amount'],$voiceArr[$v['voice_id']]['balance_amount'],2),
- "total_amount"=> bcadd($mainArr[$v['main_id']]['total_amount'],$voiceArr[$v['voice_id']]['total_amount'],2),
- "status"=>bcadd($mainArr[$v['main_id']]['total_amount'],$voiceArr[$v['voice_id']]['total_amount'],2)==0?1:0
- ];
- $remove[]=$v['voice_id'];
- }
- $pinv= PayInvoice::where('hpNo',$inoviceCode[0])->findOrEmpty();
- if($pinv->isEmpty()) return error('未找到关联的回票申请');
- if($pinv['status']!=14) return error('回票申请状态必须为待确认');
- $this->model->startTrans();
- try{
- $up=$this->model->saveAll($temop);
- if($up==false) throw new \Exception("更新失败");
- $delte=$this->model->whereIn('id',$remove)->select()->delete();
- if($delte==false) throw new \Exception("删除失败");
- $ist = $this->model->where([['invoiceCode',"=",$inoviceCode[0]],["amount","<",0]])->count();
- if($ist==0){
- $pinv->status=11;
- $save=$pinv->save();
- if($save==false) throw new \Exception("回票申请状态修改失败");
- }
- $this->model->commit();
- }catch (\Exception $e){
- $this->model->rollback();
- return error($e->getMessage());
- }
- return success('合并成功');
- }
- /**
- * 删除无效发票明细
- */
- public function removeItem(){
- $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 = $this->model->where(['id'=>$param['id']])->findOrEmpty();
- if($info->isEmpty()) return error("发票明细不存在");
- $pinv= PayInvoice::where("hpNo",$info['invoiceCode'])->findOrEmpty();
- if($pinv->isEmpty()) return error("未找到关联的回票申请");
- if($pinv['status']!=14) return error("回票申请状态必须为待确认");
- $this->model->startTrans();
- try{
- $info->delete();
- $this->model->commit();
- }catch (\Exception $e){
- $this->model->rollback();
- return error($e->getMessage());
- }
- }
- 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= $this->model->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= $this->model->whereIn('id',array_column($param['list'],"itemId"))->column("id,invoiceCode,order_type,balance_amount,cat_code,tax","id");
- $cgdInfo = 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([['a.status','=',1],['a.is_del','=',0],['a.cgdNo','in',array_column($param['list'],'code')]])
- ->column('b.sequenceNo,b.goodNo,b.goodName,(b.goodNum-thNum) as goodNum,b.goodPrice,b.totalPrice,b.ainv_fee,b.winv_fee,b.apay_fee,b.wpay_fee,
- c.merge_code,c.cat_code,c.cat_name,c.short_name,c.tax,c.inv_good_name',"b.sequenceNo");
- $orderItem=[];
- foreach($param["list"] as $v){
- if(!$listValid->check($v)) return error($listValid->getError());
- if(!isset($itemArr[$v['itemId']])) return error("发票明细不存在");
- if(!isset($cgdInfo[$v['code']]))return error("订单{$v['code']}信息不存在或未对账");
- if($cgdInfo[$v['code']]['winv_fee']<$v['total_amount']) return error("{$v["code"]}订单金额不足");
- if($itemArr[$v['itemId']]['balance_amount']<=0) return error("发票明细ID {$v["itemId"]} 可关联金额不足");
- if($cgdInfo[$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);
- }
- $cgdInfo[$v['code']]['winv_fee'] =bcsub( $cgdInfo[$v['code']]['winv_fee'],bcsub($v['total_amount'],$balance,2),2);
- $tax = $cgdInfo[$v['code']]['tax'];
- $merge_code = $cgdInfo[$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'=>$cgdInfo[$v['code']]['goodNo'],
- 'good_name'=>$cgdInfo[$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\cxinv\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("导入成功");
- }
- }
|