| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 | <?phpnamespace app\admin\controller;use app\common\model\MateGoodsLog;use app\common\util\ExcelUtil;use think\App;use think\Db;use think\Exception;class MateCheck extends Auth{    public function __construct(App $app = null) {        parent::__construct($app);        $this->model = new \app\common\model\MateCheck();        $this->table = $this->model->table;    }    public function index(){        if(request()->isAjax()){            //分页参数            $length = input('rows',10,'intval');   //每页条数            $page = input('page',1,'intval');      //第几页            $start = ($page - 1) * $length;     //分页开始位置            //排序            $sortRow = input('sidx','id','trim');      //排序列            $sort = input('sord','desc','trim');        //排序方式            $order = $sortRow.' '.$sort;            $enable = input('enable','','trim');            if($enable != ''){                $map[] = ['status','=',$enable];            }            $map[] = ['del','=',0];            $map[] = ['org_id','=',$this->orgId];            $map= empty($map) ? true: $map;            //数据查询            $lists =db($this->table)                ->where($map)->limit($start,$length)                ->order($order)->select();            $lists = $lists?$lists:[];            foreach ($lists as $k=>$v){                $lists[$k]['userName'] = Db::name('user')->where('id',$v['user_id'])->value('real_name');            }            //数据返回            $totalCount = db($this->table)->where($map)->count();            $totalPage = ceil($totalCount/$length);            $result['page'] = $page;            $result['total'] = $totalPage;            $result['records'] = $totalCount;            $result['rows'] = $lists;            return json($result);        }else{            $this->assign('meta_title','盘库列表');            return $this->fetch();        }    }    /**     * 新增/编辑     */    public function add(){        if(request()->isPost()){            $res = $this->model->updates($this->userId);            if($res){                $this->success('操作成功',url('index'));            }else{                $this->error($this->model->getError());            }        }else{            $meta_title = '新增盘库';            $this->assign('meta_title',$meta_title);            return $this->fetch();        }    }    //详情    public function info($id){        $info = $this->model->getInfo($id);        if(!$info) $this->error('记录不存在');        $this->assign('info',$info);        return $this->fetch();    }    //excel导出    public function export($id=0) {        set_time_limit(0);        ini_set("memory_limit","1024M");        $info = $this->model->getInfo($id);        $lists = $info['goods']?$info['goods']:[];        foreach ($lists as $k=>$v){            $lists[$k]['check_nums'] = "";            $lists[$k]['cy_nums'] = "";            $lists[$k]['check_time'] = "";            $lists[$k]['remark'] = "";        }        $header = [            ['title' => '名称', 'name' => 'title','width'=>'20'],            ['title' => '规格', 'name' => 'spec','width'=>'20'],            ['title' => '品牌', 'name' => 'brand','width'=>'20'],            ['title' => '单价', 'name' => 'price','width'=>'20'],            ['title' => '应盘数量', 'name' => 'nums','width'=>'20'],            ['title' => '实盘数量', 'name' => 'check_nums','width'=>'20'],            ['title' => '数量差异', 'name' => 'cy_nums','width'=>'20'],            ['title' => '盘点时间', 'name' => 'check_time','width'=>'20'],            ['title' => '盘点说明', 'name' => 'remark','width'=>'20'],        ];        $filename = '盘库';        ExcelUtil::export($filename,$header,$lists);    }    public function check($id=0){        if(request()->isPost()){            $res = $this->model->checkGoods($this->userId);            if($res){                $this->success('操作成功',url('index'));            }else{                $this->error($this->model->getError());            }        }else{            $this->assign('id',$id);            return $this->fetch();        }    }    public function delGoods($id=0){        $info = Db::name('mate_check_goods')->where('id',$id)->where('del',0)->find();        if(!$info){            $this->error('记录不存在');        }        $ret = Db::name('mate_check_goods')->where('id',$id)->update(['user_id'=>$this->userId,'del'=>1]);        if($ret){            $this->success('操作成功');        }else{            $this->error('操作失败');        }    }    public function del($id=0){        $info = Db::name('mate_check')->where('id',$id)->where('del',0)->find();        if(!$info){            $this->error('记录不存在');        }        Db::startTrans();        try{            $ret = Db::name('mate_check')->where('id',$id)->update(['del_user'=>$this->userId,'del'=>1,'del_time'=>date('Y-m-d H:i:s')]);            if(!$ret){                \exception('操作成功');            }            Db::name('mate_check_goods')->where('check_id',$id)->update(['user_id'=>$this->userId,'del'=>1]);            Db::commit();        }catch (Exception $e){            Db::rollback();            $this->error('操作失败');        }        $this->success('操作成功');    }    public function finish($id=0){        $info = Db::name('mate_check')->where('id',$id)->where('del',0)->find();        if(!$info){            $this->error('记录不存在');        }        if($info['status'] != 0){            $this->error('盘库已完成');        }        $ginfo = Db::name('mate_check_goods')->where('check_id',$id)->where('status',0)->where('del',0)->find();        if($ginfo){            $this->error('还有物品未盘库');        }        Db::startTrans();        try{            $ret = Db::name('mate_check')->where('id',$id)->update(['status'=>1,'finish_time'=>date('Y-m-d H:i:s')]);            if(!$ret){                \exception("操作失败");            }            $goodsList = Db::name('mate_check_goods')                ->where('check_id',$id)                ->where('del',0)                ->where('status','>',0)                ->select();            $goodsList = $goodsList?$goodsList:[];            foreach ($goodsList as $k=>$v){                $save = Db::name('mate_goods')                    ->where('id',$v['goods_id'])                    ->update(['nums'=>$v['check_nums'],'update_time'=>date("Y-m-d H:i:s")]);                if(!$save){                    \exception("操作失败");                }            }            Db::commit();        }catch (\Exception $e){            Db::rollback();            $this->error('操作失败');        }                $this->success('操作成功');    }}
 |