orgId]; $map[] = ['del','=',0]; $list = Db::name('mate_goods') ->field('id as goods_id,price,nums,type,title,unit,brand,spec,consume,create_time') ->where($map) ->page($page,$size) ->order('id desc') ->select(); foreach ($list as $k=>$v){ if($v['type'] == 1){ $list[$k]['cate_name'] ='固定资产'; }elseif ($v['type'] == 2){ $list[$k]['cate_name'] ='消耗品'; }else{ $list[$k]['cate_name'] = ''; } } HelpHander::success($list); } //入库/出库记录 public function storageList(){ $type = input('type/d',0); $length = input('size',10); //每页条数 $page = input('page',1); //第几页 $start = ($page - 1) * $length; //分页开始位置 $map[] = ['org_id','=',$this->orgId]; $map[] = ['del','=',0]; $map[] = ['user_id','=',$this->userId]; if($type == 0){ $type = 1; }else if($type == 1){ $type = 2; } $map[] = ['type','=',$type]; $lists = Db::name('mate_apply') ->where($map) ->field('id,org_id,user_id,remark,name,phone,create_time') ->limit($start,$length) ->order('id','desc') ->select(); foreach ($lists as $k=>$v){ $lists[$k]['userName'] = Db::name('user') ->where('id',$v['user_id']) ->value('real_name'); } HelpHander::success($lists); } public function storageDetail(){ $id = input('id/d',0); $info = db('mate_apply') ->where('id',$id) ->where('del',0) ->field('id,org_id,user_id,remark,name,phone,create_time,sign') ->find(); if(!$info){ HelpHander::error('信息不存在'); } $info['sign'] = $info['sign']?$info['sign']:''; $info['userName'] = Db::name('user') ->where('id',$info['user_id']) ->value('real_name'); $info['goods'] = Db::name('mate_apply_goods') ->alias('a') ->join('mate_goods b','a.goods_id=b.id') ->where('a.apply_id',$info['id']) ->field('a.*,b.title,b.unit,b.brand,b.spec') ->select(); HelpHander::success($info); } public function scanGoods(){ $code = input('code',''); if(empty($code)){ HelpHander::error('二维码为空'); } $info = get_qrcode_arr($code); if(!$info || $info['ucode'] != config('app.ucode') || $info['type'] != 'mate_goods'){ HelpHander::error('二维码不正确'); } $goods = Db::name('mate_goods') ->where('id',$info['id']) ->field('img,news_status,total_price,consume,buy_time,update_time,del,often',true) ->find(); HelpHander::success($goods); } public function addStorage(){ $name =input('name',''); $phone = input('phone',''); $remark = input('remark',''); $sign = input('sign',''); $options = input('options'); $type = input('type/d',2); if(empty($name)){ HelpHander::error($type==2?'入库':'出库'.'人不能为空'); } if(empty($phone)){ HelpHander::error('电话'); } if(empty($options)){ HelpHander::error('物品信息不能为空'); } $option = json_decode($options,true); if(empty($option)){ HelpHander::error('物品信息不能为空'); } Db::startTrans(); try{ foreach($option as $k=>$v){ $g = Db::name('mate_goods') ->where('id',$v['id']) ->find(); if($type==2){ // if(empty($v['price']) || $v['price'] <=0){ // exception($g['title'].'价格不能为空'); // } }else{ $goods = db('mate_goods') ->where('id',$v['id']) ->find(); if($goods['nums']<$v['nums']){ exception($g['title'].'出库数量超过库存数量'); } } if(empty($v['nums']) || $v['nums'] <=0){ exception($g['title'].'数量不能为空'); } } //$post['type']==2 入库单 else 调拨单 $apply = [ 'org_id'=>$this->orgId, 'name'=>$name, 'phone'=>$phone, 'remark'=>$remark, 'sign'=>$sign, 'type'=>$type==2?1:2, 'create_time'=>getTime(), 'user_id'=>$this->userId, 'sn'=>get_unique_id() ]; $applyid = db('mate_apply')->insertGetId($apply); if(!$applyid){ exception('添加入库单失败'); } foreach($option as $k=>$v){ $goods = db('mate_goods') ->where('id',$v['id']) ->find(); $items =[ 'apply_id'=>$applyid, 'goods_id'=>$v['id'], 'nums'=>$v['nums'], //'price'=>$type==2?$v['price']:$goods['price'], 'price'=>$type==1?$goods['price']:$goods['price'], ]; $res = db('mate_apply_goods') ->insertGetId($items); if(!$res){ exception($v['title'].'添加mate_apply_goods失败'); } $old = $goods['nums']*$goods['price']; $new = $items['nums']*$items['price']; $nums = $goods['nums']+$items['nums']; $price = ($old+$new)/$nums; if($type==1){ //调拨单 $u = [ 'nums'=>$goods['nums']-$items['nums'], 'total_price'=>($goods['nums']-$items['nums'])*$goods['price'], 'update_time'=>getTime(), ]; }else{ //入库单 $u = [ 'nums'=>$nums, 'price'=>$price, 'total_price'=>$nums*$price, 'update_time'=>getTime(), ]; } $ret = db('mate_goods') ->where('id',$v['id'])->update($u); if(!$ret){ exception($v['title'].'更新数量失败'); } } Db::commit(); HelpHander::success([],'操作成功'); }catch (Exception $e) { Db::rollback(); HelpHander::error($e->getmessage()); } } public function goodsDetail(){ $code = input('code',''); if(empty($code)){ HelpHander::error('二维码为空'); } $info = get_qrcode_arr($code); if(!$info || $info['ucode'] != config('app.ucode') || $info['type'] != 'mate_goods'){ HelpHander::error('二维码不正确'); } $goods = Db::name('mate_goods') ->where('id',$info['id']) ->field('img,news_status,total_price,consume,buy_time,update_time,del,often',true) ->find(); $rk = Db::name('mate_apply_goods') ->alias('a') ->join('mate_apply c','a.apply_id=c.id') ->join('mate_goods b','a.goods_id=b.id') ->where('c.type',1) ->where('c.del',0) ->where('c.org_id',$this->orgId) ->where('b.id',$goods['id']) ->field('a.*,b.title,b.unit,b.brand,b.spec,c.id as dispatch_id') ->select(); foreach ($rk as $k=>$v){ $dispatch = Db::name('mate_apply') ->where('id',$v['apply_id']) ->find(); $rk[$k]['userName'] = Db::name('user') ->where('id',$dispatch['user_id']) ->value('real_name'); $rk[$k]['create_time'] = $dispatch['create_time']; $rk[$k]['remark'] = $dispatch['remark']; $rk[$k]['name'] = $dispatch['name']; $rk[$k]['phone'] = $dispatch['phone']; } $goods['rk'] = $rk; $ck = Db::name('mate_apply_goods') ->alias('a') ->join('mate_apply c','a.apply_id=c.id') ->join('mate_goods b','a.goods_id=b.id') ->where('c.type',2) ->where('c.del',0) ->where('c.org_id',$this->orgId) ->where('b.id',$goods['id']) ->field('a.*,b.title,b.unit,b.brand,b.spec,c.id as dispatch_id') ->select(); foreach ($ck as $k=>$v){ $dispatch = Db::name('mate_apply') ->where('id',$v['apply_id']) ->find(); $ck[$k]['userName'] = Db::name('user') ->where('id',$dispatch['user_id']) ->value('real_name'); $ck[$k]['create_time'] = $dispatch['create_time']; $ck[$k]['remark'] = $dispatch['remark']; $ck[$k]['name'] = $dispatch['name']; $ck[$k]['phone'] = $dispatch['phone']; } $goods['ck'] = $ck; HelpHander::success($goods); } }