123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- <?php
- namespace app\cxinv\controller;
- use app\cxinv\model\ProductsCombind;
- use think\App;
- class FinancialProducts extends Base{
- public function __construct(App $app) {
- parent::__construct($app);
- $this->model=new \app\cxinv\model\FinancialProducts();
- }
- public function List(){
- $param = $this->request->param(["skuCode"=>"","good_type"=>"","buyer_code"=>"","buyer_name"=>"","start"=>"",
- "end"=>"","page"=>1,"size"=>15],"post","trim");
- $where=[];
- if($param['skuCode']!="") $where[]= ["skuCode","=","%{$param['skuCode']}%"];
- if($param['good_type']!="") $where[]= ["good_type","=",$param['good_type']];
- if($param['buyer_code']!="") $where[]= ["buyer_code","=",$param['buyer_code']];
- if($param['buyer_name']!="") $where[]= ["buyer_name","like","%{$param['buyer_name']}%"];
- if($param['start']!="") $where[]= ["create_time",">=",startTime($param['start'])];
- if($param['end']!="") $where[]= ["create_time","<=",endTime($param['end'])];
- $list = $this->model
- ->where($where)
- ->order("id desc")
- ->paginate(['page'=>$param['page'],'list_rows'=>$param['size']]);
- return success('获取成功',['list'=>$list->items(),'count'=>$list->total()]);
- }
- public function create(){
- $param= $this->request->param(["skuCode"=>"","goodName"=>"","good_type"=>"","buyer_code"=>"","buyer_name"=>"",
- "seller_code"=>"","seller_name"=>"","good_source"=>"","inv_good_name"=>"","is_combind"=>"","spec"=>"","good_code"=>"",
- "unit"=>"","unit_price"=>"","subunit_price"=>"","unit_weight"=>"","cat_code"=>"","cat_tax"=>"","inv_type"=>"",
- "basic_status"=>1,"childArr"=>[]],"post","trim");
- $valid = $this->validate($param,[
- 'skuCode|商品编号'=>'require|max:255',
- 'goodName|商品名称'=>'require|max:255',
- 'good_type|商品类型'=>'require|in:1,2,3',
- 'buyer_code|买方公司纳税识别号'=>'require|max:255',
- 'buyer_name|买方公司名称'=>'require|max:255',
- 'seller_code|卖方公司纳税识别号'=>'require|max:255',
- 'seller_name|卖方公司名称'=>'require|max:255',
- 'good_source|商品来源'=>'require|in:1,2',
- 'inv_good_name|发票商品名称'=>'require|max:255',
- 'is_combind|是否组合商品'=>'require|in:0,1',
- 'spec|规格'=>'max:255',
- 'good_code|商品代码'=>'max:255',
- 'unit|单位'=>'require|max:255',
- 'unit_price|成本税前单价'=>'require|float',
- 'subunit_price|成本税后单价'=>'require|float',
- 'unit_weight|重量'=>'float',
- "spectral|分光"=>'max:255',
- 'cat_code|进项类目'=>'require|max:255',
- 'cat_tax|进项税率'=>'require|max:255',
- 'inv_type|发票类型'=>'require|max:255',
- 'basic_status|进项成本状态'=>'require|in:1,2',
- 'childArr|商品子商品'=>'requireIf:is_combind:1|array'
- ]);
- if($valid!==true) return error($valid);
- $param['apply_id']=$this->uid;
- $param['apply_name']=$this->uname;
- $this->model->startTrans();
- try{
- $res = $this->model->create($param);
- if($res->isEmpty()) throw new \Exception('添加失败');
- if($param['is_combind']==1){
- $parentid = $res->id;
- $childRes = (new ProductsCombind)->saveAll(array_map(function ($item) use ($parentid ){
- $item['parent_id']=$parentid;
- return $item;
- },$param['childArr']));
- if($childRes->isEmpty()) throw new \Exception('添加失败');
- }
- $this->model->commit();
- }catch (\Exception $e){
- $this->model->rollback();
- return error($e->getMessage());
- }
- return success('添加成功');
- }
- public function update(){
- $param= $this->request->param(["id"=>"","skuCode"=>"","goodName"=>"","good_type"=>"","buyer_code"=>"","buyer_name"=>"",
- "seller_code"=>"","seller_name"=>"","good_source"=>"","inv_good_name"=>"","is_combind"=>"","spec"=>"","good_code"=>"",
- "unit"=>"","unit_price"=>"","subunit_price"=>"","unit_weight"=>"","cat_ code"=>"","cat_tax"=>"","inv_type"=>"",
- "basic_status"=>"","childArr"=>[]],"post","trim");
- $valid = $this->validate($param,[
- 'id|商品ID'=>'require|integer',
- 'skuCode|商品编号'=>'require|max:255',
- 'goodName|商品名称'=>'require|max:255',
- 'good_type|商品类型'=>'require|in:1,2,3',
- 'buyer_code|买方公司纳税识别号'=>'require|max:255',
- 'buyer_name|买方公司名称'=>'require|max:255',
- 'seller_code|卖方公司纳税识别号'=>'require|max:255',
- 'seller_name|卖方公司名称'=>'require|max:255',
- 'good_source|商品来源'=>'require|in:1,2',
- 'inv_good_name|发票商品名称'=>'require|max:255',
- 'is_combind|是否组合商品'=>'require|in:0,1',
- 'spec|规格'=>'max:255',
- 'good_code|商品代码'=>'max:255',
- 'unit|单位'=>'require|max:255',
- 'unit_price|成本税前单价'=>'require|float',
- 'subunit_price|成本税后单价'=>'require|float',
- 'unit_weight|重量'=>'float',
- "spectral|分光"=>'max:255',
- 'cat_code|进项类目'=>'require|max:255',
- 'cat_tax|进项税率'=>'require|max:255',
- 'inv_type|发票类型'=>'require|max:255',
- 'basic_status|进项成本状态'=>'require|in:1,2',
- 'childArr|商品子商品'=>'requireIf:is_combind:1|array'
- ]);
- if($valid!==true) return error($valid);
- $info = $this->model->with(['ProductsCombind'])->findOrEmpty($param['id']);
- if($info->isEmpty()) return error('数据不存在');
- if($info->is_combind==1|| $param['is_combind']==1){
- $childIds =array_column($info->ProductsCombind->toArray(),"id") ;
- $paramChilds = array_column($param['childArr'],'id');
- $delIds = array_diff($childIds,$paramChilds);
- $add = array_map(function ($item)use ($param){
- $item['id']= $item['id']??null;
- $item['parent_id']=$param['id'];
- return $item;
- },$param['childArr']);
- }
- $this->model->startTrans();
- try{
- $res = $info->save($param);
- if(!$res) throw new \Exception('更新失败');
- if($param['is_combind']==1){
- if(!empty($delIds)) ProductsCombind::where(["parent_id"=>$param['id'],"id"=>$delIds])->delete();
- if(!empty( $add)){
- $childRes = (new ProductsCombind)->saveAll( $add);
- if($childRes->isEmpty()) throw new \Exception('更新失败');
- }
- }
- $this->model->commit();
- }catch (\Exception $e){
- $this->model->rollback();
- return error($e->getMessage());
- }
- return success('更新成功');
- }
- public function info(){
- $id= $this->request->param("id","0","int");
- $info = $this->model->with(["ProductsCombind"=>['products']])->findOrEmpty($id);
- if($info->isEmpty()) return error('数据不存在');
- return success('获取成功',$info);
- }
- public function delete(){
- $id= $this->request->param("id","0","int");
- $info = $this->model->findOrEmpty($id);
- if($info->isEmpty()) return error('数据不存在');
- $res = $info->delete();
- if($info->is_combind==1){
- ProductsCombind::where("parent_id",$id)->delete();
- }
- return $res?success('删除成功'):error('删除失败');
- }
- public function status(){
- $param= $this->request->param(["id"=>"","status"=>""],"post","trim");
- $valid = $this->validate($param,[
- 'id|商品ID'=>'require|integer',
- 'status|状态'=>'require|in:0,1'
- ]);
- if($valid!==true) return error($valid);
- $info = $this->model->findOrEmpty($param['id']);
- if($info->isEmpty()) return error('数据不存在');
- $res = $info->save(['status'=>$param['status']]);
- return $res?success('修改成功'):error('修改失败');
- }
- public function getGoods(){
- $param= $this->request->param(["skuCode"=>"","good_type"=>"","buyer_code"=>"","seller_code"=>"","start"=>"",
- "end"=>"","limit"=>100],"post","trim");
- $valid = $this->validate($param,[
- 'skuCode|商品编号'=>'max:255',
- 'good_type|商品类型'=>'in:1,2,3',
- 'buyer_code|买方公司纳税识别号'=>'max:255',
- 'seller_code|卖方公司纳税识别号'=>'max:255',
- 'start|开始时间'=>'date',
- 'end|结束时间'=>'date'
- ]);
- if($valid!==true) return error($valid);
- $where=[];
- if(!empty($param['skuCode'])) $where[]=['skuCode','like','%'.$param['skuCode'].'%'];
- if(!empty($param['good_type'])) $where[]=['good_type','=',$param['good_type']];
- if(!empty($param['buyer_code'])) $where[]=['buyer_code','like','%'.$param['buyer_code'].'%'];
- if(!empty($param['seller_code'])) $where[]=['seller_code','like','%'.$param['seller_code'].'%'];
- if(!empty($param['start']) && !empty($param['end'])) $where[]=['create_time','between',[$param['start'],$param['end']]];
- $list = $this->model->where($where)->order('id desc')->limit($param['limit'])->select();
- return success('获取成功',$list);
- }
- public function combindGood(){
- $param= $this->request->param(["parent_id"=>"","childArr"=>[]],"post","trim");
- $valid = $this->validate($param,[
- 'parent_id|商品ID'=>'require|integer',
- 'childArr|子商品'=>'require|array'
- ]);
- if($valid!==true) return error($valid);
- $parent = $this->model->findOrEmpty($param['pid']);
- if($parent->isEmpty()) return error('数据不存在');
- $combind= new ProductsCombind();
- $ist = $combind->where(['parent_id'=>$param['parent_id']])->column("child_id");
- $del=array_diff($ist, array_column($param['childArr'],'child_id'));
- $add=[];
- foreach ($param['childArr'] as $item){
- $vali = $this->validate($item,[
- 'child_id|子商品ID'=>'require|integer',
- 'child_num|子商品数量'=>'require|integer'
- ]);
- if($vali!==true) return error($vali);
- $add[]=[
- "id"=>$ist[$item['child_id']]??null,
- "parent_id"=>$param['parent_id'],
- "child_num"=>$item['child_num'],
- "child_id"=>$item['child_id']
- ];
- }
- $this->model->startTrans();
- try{
- if(!empty($del))$combind->where(["parent_id"=>$param['parent_id'],"child_id"=>$del])->delete();
- if(!empty($add))$combind->saveAll($add);
- $this->model->commit();
- }catch (\Exception $e){
- $this->model->rollback();
- return error($e->getMessage());
- }
- return success('修改成功');
- }
- }
|