model = model('\app\admin\model\stock\Goods'); $goodscategorylist = collection(model('\app\admin\model\stock\Goodscategory')->select())->toArray(); Tree::instance()->init($goodscategorylist); $goodscategorylist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'name'); $categorylist = array('' => '==请选择=='); $this->categoryList = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'name'); foreach ($goodscategorylist as $k => $v) { $categorylist[$v['id']] = $v['name']; } $measureunit = model('\app\admin\model\stock\Datadict')->getDatadic('JiLiangDW'); $this->view->assign('goodscategorylist', $categorylist); $this->view->assign('measureunit', $measureunit); } /** * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法 * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑 * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改 */ /** * 查看 */ public function index() { //设置过滤方法 $this->request->filter(['strip_tags']); if ($this->request->isAjax()) { //如果发送的来源是Selectpage,则转发到Selectpage if ($this->request->request('keyField')) { return $this->selectpage(); } list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $categoryids = $this->request->request("categoryids"); $query = []; if ($categoryids !== "") { $query["goods.goodscategoryid"] = ["in", $categoryids]; } $total = $this->model ->with('goodscategory') ->where($where) ->where($query) ->order($sort, $order) ->count(); $list = $this->model ->with('goodscategory') ->where($where) ->where($query) ->order($sort, $order) ->limit($offset, $limit) ->select(); $list = collection($list)->toArray(); $result = array("total" => $total, "rows" => $list); return json($result); } return $this->view->fetch(); } /** * 获取所有商品列表 */ public function getlist() { //设置过滤方法 if ($this->request->isAjax()) { list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $searchvalue = $this->request->request("searchvalue"); $query = []; if ($searchvalue) { $query["volnum|goodsname|spell"] = ["LIKE", '%' . $searchvalue . '%']; } $total = $this->model ->with('goodscategory') ->where($where) ->where($query) ->order($sort, $order) ->count(); $list = $this->model ->with('goodscategory') ->where($where) ->where($query) ->order($sort, $order) ->limit($offset, $limit) ->select(); $list = collection($list)->toArray(); $result = array("total" => $total, "rows" => $list); return json($result); } } /** * 添加 */ public function add() { if ($this->request->isPost()) { $params = $this->request->post("row/a"); if ($params) { if ($this->dataLimit && $this->dataLimitFieldAutoFill) { $params[$this->dataLimitField] = $this->auth->id; } $map = [ 'goodsname' => $params['goodsname'], 'productmodel' => $params['productmodel'], ]; $list = $this->model->where($map)->count(); if ($list == 0) { try { //是否采用模型验证 if ($this->modelValidate) { $name = basename(str_replace('\\', '/', get_class($this->model))); $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate; $this->model->validate($validate); } $result = $this->model->allowField(true)->save($params); if ($result !== false) { $this->success(); } else { $this->error($this->model->getError()); } } catch (\think\exception\PDOException $e) { $this->error($e->getMessage()); } } else { $this->error(__('商品已存在', '')); } } $this->error(__('Parameter %s can not be empty', '')); } return $this->view->fetch(); } /** * 删除 */ public function del($ids = "") { if ($ids) { $pk = $this->model->getPk(); $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds)) { $count = $this->model->where($this->dataLimitField, 'in', $adminIds); } $list = $this->model->where($pk, 'in', $ids)->select(); $count = 0; foreach ($list as $k => $v) { $goodinfo = model('\app\admin\model\stock\Stockinlist')->where('stock_goods_id', '=', $v->id)->limit(1)->select(); if ($goodinfo) { $this->error('该商品存在入库信息,不能删除!'); } else { $count += $v->delete(); } } if ($count) { $this->success(); } else { $this->error(__('No rows were deleted')); } } $this->error(__('Parameter %s can not be empty', 'ids')); } /** * 出入明细 */ public function detail($ids = null) { if ($this->request->isAjax()) { $ids = $this->request->request('ids'); //如果发送的来源是Selectpage,则转发到Selectpage if ($this->request->request('keyField')) { return $this->selectpage(); } list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $subQuery = \think\Db::field('\'入库\' AS leixing, `stock_goods_id`,`inboundtime` AS riqi,stockinnums,\'\' as stockoutnums,amount AS stockinamount,\'\' as stockoutamount') ->name('stock_stockin_list') ->alias('list') ->join('stock_stockin stockin','stockin.id=list.stock_stockin_id') ->union(function ($query) { $query->field('\'出库\' AS leixing,`stock_goods_id`,`outboundtime` AS riqi,\'\' as stockinnums,stockoutnums,\'\' as stockinamount,amount AS stockoutamount') ->name('stock_stockout_list') ->alias('list') ->join('stock_stockout stockout','stockout.id=list.stock_stockout_id'); }) ->buildSql(); $sum = \think\Db::table($subQuery . ' a') ->field('COUNT(*) AS total, SUM(a.stockinnums) AS totalstockinnums,SUM(a.stockinamount) AS totalstockinamount,SUM(a.stockoutnums) AS totalstockoutnums,SUM(a.stockoutamount) AS totalstockoutamount') ->where('a.stock_goods_id', $ids)->select(); $total = $sum[0]['total']; $list = \think\Db::view([$subQuery => 'a'], '*') ->view('stock_goods goods', '*', 'a.stock_goods_id=goods.id', 'left') ->where('goods.id', $ids)->where($where)->order($sort, $order)->limit($offset, $limit)->select(); $list = collection($list)->toArray(); if (count($list) > 0) { $list[0]["sum"] = $sum[0]; } $result = array("total" => $total, "rows" => $list); return json($result); } $this->view->assign("ids", $ids); return $this->view->fetch(); } /** * 根据商品类别生成商品代码 */ public function madevolnum() { if ($this->request->isPost()) { $goodscategoryid = $this->request->post('goodscategoryid'); if ($goodscategoryid) { try { //生产商品代码 $aVolnum = $this->model->where('goodscategoryid', $goodscategoryid)->field('volnum')->order('volnum desc')->limit(1)->select(); $volnum = ''; if (count($aVolnum) > 0) { $volnumArr = explode('.', $aVolnum[0]['volnum']); $volnumArr[count($volnumArr) - 1] = sprintf("%03d", $volnumArr[count($volnumArr) - 1] + 1); $volnum = implode('.', $volnumArr); } else { $bVolnum = model('\app\admin\model\stock\Goodscategory')->where('id', $goodscategoryid)->column('value'); if (count($bVolnum) > 0) { $volnum = ($bVolnum[0] . '.001'); } else { $volnum = "001"; } } //查询商品类别名称 $result['volnum'] = $volnum; $result['goodscategory'] = $goodscategoryid; $this->success("获取成功!", "", $result); } catch (\think\exception\PDOException $e) { $this->error($e->getMessage()); } } $this->error(__('Parameter %s can not be empty', '')); } $this->error('只接受POST请求', ''); } public function getgoodsbybarcodeorid() { if ($this->request->isPost()) { $barcode = $this->request->post('barcode'); $id = $this->request->post('id'); if ($barcode) { $goods = $this->model->where("barcode",$barcode)->find(); if ($goods) { // $goods["curstocknum"]=$goods->curstocknum; $this->success("查询成功!", "", $goods); } else { $this->success("没有找到商品!", "", $goods); } }else if($id) { $goods = $this->model->get($id); if ($goods) { // $goods["curstocknum"]=$goods->curstocknum; $this->success("查询成功!", "", $goods); } else { $this->success("没有找到商品!", "", $goods); } } $this->error(__('参数不能为空!', '')); } $this->error('只接受POST请求', ''); } /** * 导入 */ public function import() { return parent::import(); } }