novalidate = ["*"]; parent::__construct($app); } //查询 public function cgdListByPayNo(){ $param = $this->request->param(["payNo"=>"","merge_code"=>"","inv_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","=",$param["merge_code"]]; if($param["inv_good_name"]!=="") $where[]=["c.inv_good_name","like","%{$param["inv_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(); $this->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(); $this->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))$this->error($valid->getError()); $valids=Validate::rule([ 'code|订单号' => 'require|number|gt:0', 'spuCode|商品编码' => 'require|number|gt:0', '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())$this->error("发票明细不存在"); $save=[]; $amount = $item['balance_amount']; foreach ($param["orderArr"] as $k=>$v){ if(!$valids->check($v))$this->error($valids->getError()); $taxInfo = OrderCategory::where(['code'=>$v['code'],'spuCode'=>$v['spuCode']])->findOrEmpty(); if($taxInfo->isEmpty())$this->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($item['tax']!=$taxInfo['tax']){ $diff["tax"] = [ "inv_tax"=>$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"], "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"], ]; } $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(); $this->error($e->getMessage()); } $this->success('添加成功'); } //删除发票明细对应关系 public function delete(){ $param = $this->request->param(["id"=>""],"post","trim"); $valid=Validate::rule([ 'id|发票明细对应id' => 'require|number|gt:0', ]); if(!$valid->check($param))$this->error($valid->getError()); $info = \app\admin\model\InvoiceOrder::where(['id'=>$param["id"]])->findOrEmpty(); if($info->isEmpty())$this->error("发票明细对应关系不存在"); $item = \app\admin\model\InvoiceItem::where(['id'=>$info['itemId']])->findOrEmpty(); if($item->isEmpty())$this->error("发票明细不存在"); $count = \app\admin\model\InvoiceOrder::where(['itemId'=>$info['itemId']])->count(); $info->startTrans(); try{ $res = $info->delete(); if($res){ $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?2:1); $item->save(); }else throw new \Exception("删除失败"); $info->commit(); }catch (\Exception $e){ $info->rollback(); } $this->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(); $this->success('获取成功',$list); } }