123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- <?php
- namespace app\api\controller\v1;
- use app\api\controller\Base;
- use app\hander\HelpHander;
- use think\Db;
- use think\Exception;
- class MateGoods extends Base
- {
- public function lists(){
- $page = input('page',1);
- $size = input('size',10);
- $title = input('title','','trim');
- if($title !=''){
- $map[] = ['title','like','%'.$title.'%'];
- }
- $map[] = ['org_id','=',$this->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);
- }
- }
|