Browse Source

油墨优化功能

liuhairui 2 years ago
parent
commit
00c5d07988

+ 148 - 0
application/admin/controller/stock/Customer.php

@@ -0,0 +1,148 @@
+<?php
+
+namespace app\admin\controller\stock;
+
+use app\common\controller\Backend;
+use fast\Tree;
+
+/**
+ * 客户管理
+ *
+ * @icon fa fa-circle-o
+ */
+class Customer extends Backend {
+
+    /**
+     * StockCustomer模型对象
+     * @var \app\admin\model\StockCustomer
+     */
+    protected $model = null;
+
+    protected $noNeedLogin = ['madeEncode', 'getwdTree'];
+    public function _initialize() {
+        parent::_initialize();
+        $this->model = model('\app\admin\model\stock\Customer');
+        $customerList = collection($this->model->order('weigh')->select())->toArray();
+        Tree::instance()->init($customerList);
+
+        $customer = [];
+        $this->customerList = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'name');
+        $primary = array(array('name' => '==请选择==', 'id' => '0'));
+        $result = array_merge_recursive($primary, $this->customerList);
+        foreach ($result as $k => $v) {
+            $customer[$v['id']] = $v['name'];
+        }
+        $this->view->assign('customer', $customer);
+        $this->view->assign("enabledmarkList", $this->model->getEnabledmarkList());
+    }
+
+    /**
+     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
+     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
+     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
+     */
+
+    /**
+     * 查看
+     */
+    public function index() {
+        if ($this->request->isAjax()) {
+            $list = $this->customerList;
+            $total = count($this->customerList);
+
+            $result = array("total" => $total, "rows" => $list);
+
+            return json($result);
+        }
+        return $this->view->fetch();
+    }
+
+    /**
+     * 添加
+     */
+    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;
+                }
+                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());
+                }
+            }
+            $this->error(__('Parameter %s can not be empty', ''));
+        }
+        $result = $this->model->where('pid', 0)->order('encode desc')->limit(1)->select();
+        if ($result) {
+            $suffix = (int) ($result[0]['encode']);
+            $suffix ++;
+        } else {
+            $suffix = 0;
+        }
+        $encode = sprintf("%02d", $suffix);
+        $this->view->assign('encode', $encode);
+        return $this->view->fetch();
+    }
+
+    /**
+     * 根据父级部门生成部门代码
+     */
+    public function madeEncode() {
+        if ($this->request->isPost()) {
+            $id = $this->request->post('id');
+            if ($id || $id == 0) {
+                try {
+                    $result = $this->model->where('pid', $id)->field('Encode')->order('Encode desc')->limit(1)->select();
+                    $Encode = '';
+                    if (count($result) > 0) {
+                        $EncodeArr = explode('.', $result[0]['Encode']);
+                        $EncodeArr[count($EncodeArr) - 1] = sprintf("%03d", $EncodeArr[count($EncodeArr) - 1] + 1);
+                        $Encode = implode('.', $EncodeArr);
+                    } else {
+                        $result = $this->model->where('id', $id)->field('Encode')->order('Encode desc')->limit(1)->select();
+                        if (count($result) > 0) {
+                            $Encode = $result[0]['Encode'] . '.001';
+                        } else {
+                            $Encode = '001';
+                        }
+                    }
+                    return json($Encode);
+                } catch (\think\exception\PDOException $e) {
+                    $this->error($e->getMessage());
+                }
+            }
+            $this->error(__('Parameter %s can not be empty', ''));
+        }
+        $this->error('只接受POST请求', '');
+    }
+
+    /**
+     * 获取部门树
+     */
+    public function getwdTree() {
+        if ($this->auth->isSuperAdmin()) {
+            $categoryList = collection($this->model->select())->toArray();
+        } else {
+            $categoryList = collection($this->model->where('id', $this->auth->customerid)->select())->toArray();
+            $categoryList[0]['pid'] = 0;
+        }
+        Tree::instance()->init($categoryList);
+        $result = Tree::instance()->getwdTreeArray(0, 'name');
+        return json($result);
+    }
+
+}

+ 170 - 0
application/admin/controller/stock/Datadict.php

@@ -0,0 +1,170 @@
+<?php
+namespace app\admin\controller\stock;
+
+use app\common\controller\Backend;
+use think\Db;
+use fast\Tree;
+use think\exception\PDOException;
+use think\exception\ValidateException;
+
+/**
+ * 字典分类
+ *
+ * @icon fa fa-circle-o
+ */
+class Datadict extends Backend {
+
+    /**
+     * Datadict模型对象
+     * @var \app\admin\model\stock\Datadict
+     */
+    protected $model = null;
+    protected $dataDiclist = [];
+    protected $noNeedRight = ['getjsTree'];
+
+    public function _initialize() {
+        parent::_initialize();
+        $this->model = model('\app\admin\model\stock\Datadict');
+        $this->view->assign("istreeList", $this->model->getIstreeList());
+        $this->view->assign("enabledList", $this->model->getEnabledList());
+        // 必须将结果集转换为数组
+        $dataDiclist = collection($this->model->order('weigh', 'desc')->select())->toArray();
+        Tree::instance()->init($dataDiclist);
+        $this->dataDiclist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
+        $dataDicdata = [0 => '无'];
+        foreach ($this->dataDiclist as $k => &$v) {
+            $dataDicdata[$v['id']] = $v['name'];
+        }
+        $this->view->assign('dataDicdata', $dataDicdata);
+    }
+
+    /**
+     * 查看
+     */
+    public function index() {
+        if ($this->request->isAjax()) {
+            $list = $this->dataDiclist;
+            $total = count($this->dataDiclist);
+            $result = array("total" => $total, "rows" => $list);
+            return json($result);
+        }
+        return $this->view->fetch();
+    }
+
+    /**
+     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
+     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
+     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
+     */
+    public function getjsTree() {
+        $list = collection($this->model->order('weigh', 'desc')->select())->toArray();
+        $result = [];
+        foreach ($list as $k => $v) {
+            $n = [];
+            $n['id'] = $v['id'];
+            $n['parent'] = $v['pid'] == 0 ? "#" : $v['pid'];
+            $n['text'] = $v['name'];
+            $n['data'] = $v;
+            $n['state'] = ["opend" => true, "disabled" => false];
+            $result[] = $n;
+        }
+        return json($result);
+    }
+
+    /**
+     * 编辑
+     */
+    public function edit($ids = null) {
+        $row = $this->model->get($ids);
+        if ($row->id == 9) {
+            $this->error("系统保留分类,禁止修改!");
+        }
+        if (!$row) {
+            $this->error(__('No Results were found'));
+        }
+        $adminIds = $this->getDataLimitAdminIds();
+        if (is_array($adminIds)) {
+            if (!in_array($row[$this->dataLimitField], $adminIds)) {
+                $this->error(__('You have no permission'));
+            }
+        }
+        if ($this->request->isPost()) {
+            $params = $this->request->post("row/a");
+            if ($params) {
+                $params = $this->preExcludeFields($params);
+                $result = false;
+                Db::startTrans();
+                try {
+                    //是否采用模型验证
+                    if ($this->modelValidate) {
+                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
+                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
+                        $row->validateFailException(true)->validate($validate);
+                    }
+
+                    if ($row->id == 9) {
+                        $this->error("系统保留分类,禁止修改!");
+                    }
+                    $result = $row->allowField(true)->save($params);
+                    Db::commit();
+                } catch (ValidateException $e) {
+                    Db::rollback();
+                    $this->error($e->getMessage());
+                } catch (PDOException $e) {
+                    Db::rollback();
+                    $this->error($e->getMessage());
+                } catch (Exception $e) {
+                    Db::rollback();
+                    $this->error($e->getMessage());
+                }
+                if ($result !== false) {
+                    $this->success();
+                } else {
+                    $this->error(__('No rows were updated'));
+                }
+            }
+            $this->error(__('Parameter %s can not be empty', ''));
+        }
+        $this->view->assign("row", $row);
+        return $this->view->fetch();
+    }
+
+    /**
+     * 删除
+     */
+    public function del($ids = "") {
+        if ($ids) {
+            $pk = $this->model->getPk();
+            $adminIds = $this->getDataLimitAdminIds();
+            if (is_array($adminIds)) {
+                $this->model->where($this->dataLimitField, 'in', $adminIds);
+            }
+            $list = $this->model->where($pk, 'in', $ids)->select();
+
+            $count = 0;
+            Db::startTrans();
+            try {
+                foreach ($list as $k => $v) {
+                    if ($v->id == 9) {
+                        $this->error("系统保留分类,禁止删除!");
+                    }
+                    $count += $v->delete();
+                }
+                Db::commit();
+            } catch (PDOException $e) {
+                Db::rollback();
+                $this->error($e->getMessage());
+            } catch (Exception $e) {
+                Db::rollback();
+                $this->error($e->getMessage());
+            }
+            if ($count) {
+                $this->success();
+            } else {
+                $this->error(__('No rows were deleted'));
+            }
+        }
+        $this->error(__('Parameter %s can not be empty', 'ids'));
+    }
+
+}

+ 105 - 0
application/admin/controller/stock/Datadictitem.php

@@ -0,0 +1,105 @@
+<?php
+
+namespace app\admin\controller\stock;
+
+use app\common\controller\Backend;
+use fast\Tree;
+
+/**
+ * 字典数据
+ *
+ * @icon fa fa-circle-o
+ */
+class Datadictitem extends Backend {
+
+    /**
+     * Datadictitem模型对象
+     * @var \app\admin\model\stock\Datadictitem
+     */
+    protected $model = null;
+    protected $relationSearch = true;
+    protected $searchFields = 'name,value';
+
+    public function _initialize() {
+        parent::_initialize();
+        $this->model = model('\app\admin\model\stock\Datadictitem');
+        $dataDiclist = collection(model('\app\admin\model\stock\Datadict')->order('weigh', 'desc')->select())->toArray();
+        Tree::instance()->init($dataDiclist);
+        $this->dataDiclist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
+        foreach ($this->dataDiclist as $k => &$v) {
+            $dataDicdata[$v['id']] = $v['name'];
+        }
+        $this->view->assign('dataDicdata', $dataDicdata);
+        $this->view->assign("enabledList", $this->model->getEnabledList());
+    }
+
+    /**
+     * 查看
+     */
+    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();
+            $datadict_id = $this->request->request('datadict_id');
+            if ($datadict_id) {
+                $total = $this->model->where($where)->where('datadict_id', 'in', $datadict_id)->order($sort, $order)->count();
+                $list = $this->model->where($where)->where('datadict_id', 'in', $datadict_id)->order($sort, $order)->limit($offset, $limit)->select();
+            } else {
+//                $total = $this->model->where($where)->count();
+//                $list = $this->model->where($where)->order($sort, $order)->limit($offset, $limit)->select();
+                $total = [];
+                $list = [];
+            }
+            $list = collection($list)->toArray();
+            $result = array("total" => $total, "rows" => $list);
+
+            return json($result);
+        }
+        return $this->view->fetch();
+    }
+
+    /**
+     * 添加
+     */
+    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;
+                }
+                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());
+                }
+            }
+            $this->error(__('Parameter %s can not be empty', ''));
+        }
+        $datadict_id = $this->request->param("datadict_id");
+        $this->view->assign("datadict_id", $datadict_id);
+        return $this->view->fetch();
+    }
+
+    /**
+     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
+     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
+     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
+     */
+}

+ 301 - 0
application/admin/controller/stock/Goods.php

@@ -0,0 +1,301 @@
+<?php
+
+namespace app\admin\controller\stock;
+
+use app\common\controller\Backend;
+use fast\Tree;
+
+/**
+ * 商品库
+ *
+ * @icon fa fa-circle-o
+ */
+class Goods extends Backend {
+
+    /**
+     * StockGoods模型对象
+     * @var \app\admin\model\stock\Goods
+     */
+    protected $noNeedRight = ['detail', 'madevolnum', 'getlist','getgoodsbybarcodeorid'];
+    protected $model = null;
+
+    /**
+     * 快速搜索时执行查找的字段
+     */
+    protected $relationSearch = true;
+    protected $searchFields = 'volnum,goodsname';
+
+    public function _initialize() {
+        parent::_initialize();
+        $this->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();
+    }
+
+}

+ 254 - 0
application/admin/controller/stock/Goodscategory.php

@@ -0,0 +1,254 @@
+<?php
+
+namespace app\admin\controller\stock;
+
+use app\common\controller\Backend;
+use think\Db;
+use fast\Tree;
+
+/**
+ * 商品分类
+ *
+ * @icon fa fa-circle-o
+ */
+class Goodscategory extends Backend {
+
+    /**
+     * StockGoodscategory模型对象
+     * @var \app\admin\model\stock\Goodscategory
+     */
+    protected $noNeedRight = ['madevalue', 'getjsTree', 'category'];
+    protected $model = null;
+    protected $categoryList = [];
+
+    public function _initialize() {
+        parent::_initialize();
+        $this->model = model('\app\admin\model\stock\Goodscategory');
+        $goodsCategoryList = collection($this->model->select())->toArray();
+        Tree::instance()->init($goodsCategoryList);
+
+        $goodsCategory = [];
+        $this->categoryList = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'name');
+        $primary = array(array('name' => '无', 'id' => '0'));
+        $result = array_merge_recursive($primary, $this->categoryList);
+        foreach ($result as $k => $v) {
+            $goodsCategory[$v['id']] = $v['name'];
+        }
+
+        $this->view->assign("goodsCategory", $goodsCategory);
+    }
+
+    /**
+     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
+     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
+     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
+     */
+
+    /**
+     * 查看
+     */
+    public function index() {
+        if ($this->request->isAjax()) {
+            $list = $this->categoryList;
+            $total = count($this->categoryList);
+
+            $result = array("total" => $total, "rows" => $list);
+
+            return json($result);
+        }
+        return $this->view->fetch();
+    }
+
+    /**
+     * 添加
+     */
+    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;
+                }
+                try {
+                    //是否采用模型验证
+                    if ($this->modelValidate) {
+                        $name = str_replace("\\model\\", "\\validate\\", 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());
+                }
+            }
+            $this->error(__('Parameter %s can not be empty', ''));
+        }
+        $result = $this->model->where('pid', 0)->order('value desc')->limit(1)->select();
+        if ($result) {
+            $suffix = (int) ($result[0]['value']);
+            $suffix ++;
+        } else {
+            $suffix = 0;
+        }
+        $value = sprintf("%02d", $suffix);
+        $this->view->assign('value', $value);
+        return $this->view->fetch();
+    }
+
+    /**
+     * 根据商品类别生成商品代码
+     */
+    public function madevalue() {
+        if ($this->request->isPost()) {
+            $id = $this->request->post('id');
+            if ($id || $id == 0) {
+                try {
+                    $result = $this->model->where('pid', $id)->field('value')->order('value desc')->limit(1)->select();
+                    $value = '';
+                    if (count($result) > 0) {
+                        $suffix = (int) (substr($result[0]['value'], -2));
+                        $suffix ++;
+                        $suffix = sprintf("%02d", $suffix); //数字自动补零
+                        $prefix = substr($result[0]['value'], 0, -2);
+                        $value = $prefix . $suffix;
+                    } else {
+                        $result = $this->model->where('id', $id)->field('value')->order('value desc')->limit(1)->select();
+                        if (count($result) > 0) {
+                            $value = $result[0]['value'] . '.01';
+                        } else {
+                            $value = '01';
+                        }
+                    }
+                    $this->success("获取成功!","", ["code" => $value]);
+                } catch (\think\exception\PDOException $e) {
+                    $this->error($e->getMessage());
+                }
+            }
+            $this->error(__('Parameter %s can not be empty', ''));
+        }
+        $this->error('只接受POST请求', '');
+    }
+
+    /**
+     * 获取商品类别树
+     */
+    public function getjsTree() {
+        $categoryList = collection($this->model->select())->toArray();
+        $result = [];
+        foreach ($categoryList as $k => $v) {
+            $n = [];
+            $n['id'] = $v['id'];
+            $n['parent'] = $v['pid'] == 0 ? "#" : $v['pid'];
+            $n['text'] = $v['name'];
+            $n['type'] = $v['id'];
+            $n['data'] = $v;
+            $n['state'] = ["opend" => true, "disabled" => false, "selected" => true];
+            $result[] = $n;
+        }
+        return json($result);
+    }
+
+    /**
+     * 获取商品类别名称
+     */
+    public function category() {
+        $result = collection($this->model->select())->toArray();
+        return json($result);
+    }
+
+    /**
+     * 编辑
+     */
+    public function edit($ids = null) {
+        $row = $this->model->get($ids);
+        if (!$row) {
+            $this->error(__('No Results were found'));
+        }
+        $adminIds = $this->getDataLimitAdminIds();
+        if (is_array($adminIds)) {
+            if (!in_array($row[$this->dataLimitField], $adminIds)) {
+                $this->error(__('You have no permission'));
+            }
+        }
+        if ($this->request->isPost()) {
+            $params = $this->request->post("row/a");
+            if ($params) {
+                $params = $this->preExcludeFields($params);
+                $result = false;
+                Db::startTrans();
+                try {
+                    //是否采用模型验证
+                    if ($this->modelValidate) {
+                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
+                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
+                        $row->validateFailException(true)->validate($validate);
+                    }
+                    $result = $row->allowField(true)->save($params);
+                    Db::commit();
+                } catch (ValidateException $e) {
+                    Db::rollback();
+                    $this->error($e->getMessage());
+                } catch (PDOException $e) {
+                    Db::rollback();
+                    $this->error($e->getMessage());
+                } catch (Exception $e) {
+                    Db::rollback();
+                    $this->error($e->getMessage());
+                }
+                if ($result !== false) {
+                    $this->success();
+                } else {
+                    $this->error(__('No rows were updated'));
+                }
+            }
+            $this->error(__('Parameter %s can not be empty', ''));
+        }
+        $this->view->assign("row", $row);
+        return $this->view->fetch();
+    }
+
+    /**
+     * 删除
+     */
+    public function del($ids = "") {
+        if ($ids) {
+            $pk = $this->model->getPk();
+            $adminIds = $this->getDataLimitAdminIds();
+            if (is_array($adminIds)) {
+                $this->model->where($this->dataLimitField, 'in', $adminIds);
+            }
+            $list = $this->model->where($pk, 'in', $ids)->select();
+
+            $count = 0;
+            Db::startTrans();
+            try {
+                foreach ($list as $k => $v) {
+                    $goodscount = model('\app\admin\model\stock\Goods')->where("goodscategoryid", $v->id)->count();
+                    if ($goodscount > 0) {
+                        $this->error("当前分类下面包含" . $goodscount . "种商品,禁止删除!");
+                    }
+                    $count += $v->delete();
+                }
+                Db::commit();
+            } catch (PDOException $e) {
+                Db::rollback();
+                $this->error($e->getMessage());
+            } catch (Exception $e) {
+                Db::rollback();
+                $this->error($e->getMessage());
+            }
+            if ($count) {
+                $this->success();
+            } else {
+                $this->error(__('No rows were deleted'));
+            }
+        }
+        $this->error(__('Parameter %s can not be empty', 'ids'));
+    }
+
+}

+ 179 - 0
application/admin/controller/stock/Stockcur.php

@@ -0,0 +1,179 @@
+<?php
+
+namespace app\admin\controller\stock;
+
+use app\common\controller\Backend;
+
+/**
+ * 商品库存
+ *
+ * @icon fa fa-circle-o
+ */
+class Stockcur extends Backend {
+
+    /**
+     * StockStockcur模型对象
+     * @var \app\admin\model\stock\Stockcur
+     */
+    protected $noNeedLogin = ['getGoodsStoknum', 'countData'];
+    protected $model = null;
+
+    public function _initialize() {
+        parent::_initialize();
+        $this->model = model('\app\admin\model\stock\Stockcur');
+    }
+
+    /**
+     * 默认生成的控制器所继承的父类中有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();
+            }
+            //当前是否为关联查询
+            $this->relationSearch = true;
+            $this->searchFields = 'stockgoods.volnum,stockgoods.goodsname';
+            $categoryids = $this->request->request("categoryids");
+            $query = [];
+            if ($categoryids !== NULL) {
+                $query["stockgoods.goodscategoryid"] = ["in", $categoryids];
+            }
+            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
+            $total = $this->model->with('stockgoods')->field("sum(curnums) as totalcurnums")->where($where)->where($query)->group("stock_goods_id")->order($sort, $order)->count();
+            $list = $this->model->with('stockgoods')->field("sum(curnums) as totalcurnums")->where($where)->where($query)->group("stock_goods_id")->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 mainpage() {
+        $stockinsum=\app\admin\model\stock\Stockinlist::with('stockin')
+        ->field('IFNULL(SUM(stockinnums),0) as totalnums,IFNULL(SUM(amount),0) as totalamount')
+        ->where('stockin.audittime','NOT NULL')
+        ->whereTime("stockin.audittime",'today')
+        ->select();
+//        echo "<pre>";
+//        print_r($stockinsum);
+//        echo "</pre>";
+//        die;
+        $stockoutsum=\app\admin\model\stock\Stockoutlist::with('stockout')
+        ->field('IFNULL(SUM(stockoutnums),0) totalnums,IFNULL(SUM(amount),0) totalamount')
+        ->where('stockout.audittime','NOT NULL')
+        ->whereTime("stockout.audittime",'today')
+        ->select();
+        $stockcursum=\app\admin\model\stock\Stockcur::with('stockinlist')
+        ->field('IFNULL(SUM(curnums),0) totalnums,IFNULL(SUM(stockinlist.inboundprice*curnums),0) totalamount')
+        ->select();
+        $stockin_toaudit=\app\admin\model\stock\Stockin::where('audittime','NULL')->count();
+        $stockout_toaudit=\app\admin\model\stock\Stockout::where('audittime','NULL')->count();
+        if ($this->request->isAjax()) {
+            $stockinsum=\app\admin\model\stock\Stockinlist::field('sum(stockinnums)  totalnums,sum(amount) totalamount')->where("createtime",'today')->select();
+            $result = array("stockinsum" => $stockinsum);
+            return json($result);
+        }
+        $this->view->assign("stockinsum", $stockinsum[0]);
+        $this->view->assign("stockoutsum", $stockoutsum[0]);
+        $this->view->assign("stockcursum", $stockcursum[0]);
+        $this->view->assign("stockin_toaudit", $stockin_toaudit);
+        $this->view->assign("stockout_toaudit", $stockout_toaudit);
+        return $this->view->fetch();
+    }
+
+    /**
+     * 编辑
+     */
+    public function edit($ids = null) {
+        $row = $this->model->get($ids);
+        if (!$row) {
+            $this->error(__('No Results were found'));
+        }
+        $adminIds = $this->getDataLimitAdminIds();
+        if (is_array($adminIds)) {
+            if (!in_array($row[$this->dataLimitField], $adminIds)) {
+                $this->error(__('You have no permission'));
+            }
+        }
+        $stockinmodel = model('StockStockin');
+        if ($this->request->isPost()) {
+            $params = $this->request->post("row/a");
+            if ($params) {
+                try {
+                    //是否采用模型验证
+                    if ($this->modelValidate) {
+                        $name = basename(str_replace('\\', '/', get_class($this->model)));
+                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
+                        $row->validate($validate);
+                    }
+                    $id = $params['stock_stockin_id'];
+                    $allocation = $params['allocation'];
+                    $result = $stockinmodel->where('id', $id)->update(['allocation' => $allocation]);
+                    if ($result !== false) {
+                        $this->success();
+                    } else {
+                        $this->error($row->getError());
+                    }
+                } catch (\think\exception\PDOException $e) {
+                    $this->error($e->getMessage());
+                }
+            }
+            $this->error(__('Parameter %s can not be empty', ''));
+        }
+        $this->view->assign("row", $row);
+        return $this->view->fetch();
+    }
+
+    /**
+     * 出库单,根据选定商品返回库存数
+     */
+    public function getGoodsStoknum($goodsid) {
+        if ($this->request->isPost()) {
+            $goodsid = $this->request->post('goodsid');
+            if ($goodsid) {
+                try {
+                    $curnum = $this->model->getGoodsStoknum($goodsid);
+                    return json($curnum);
+                } catch (\think\exception\PDOException $e) {
+                    $this->error($e->getMessage());
+                }
+            }
+            $this->error(__('Parameter %s can not be empty', ''));
+        }
+        $this->error('只接受POST请求', '');
+    }
+    /**
+     * 库存首页统计
+     */
+    public function countData() {
+        //当前是否为关联查询
+        $this->relationSearch = true;
+
+        if ($this->request->isAjax()) {
+            $total = $this->model
+                    ->group('stock_goods_id')
+                    ->count();
+            $list = $this->model
+                    ->with('StockGoods,Stockinlist')
+                    ->field('sum(curnums*inboundprice) totalitemamount,count(*) totalitem,sum(curnums) totalnum')
+                    ->group('goodscategoryid')
+                    ->select();
+            $list = collection($list)->toArray();
+            $result = array("total" => $total, "rows" => $list);
+            return json($result);
+        }
+    }
+}

+ 477 - 0
application/admin/controller/stock/Stockin.php

@@ -0,0 +1,477 @@
+<?php
+
+namespace app\admin\controller\stock;
+
+use app\common\controller\Backend;
+use DateTime;
+
+/**
+ * 商品入库
+ *
+ * @icon fa fa-circle-o
+ */
+class Stockin extends Backend
+{
+
+    /**
+     * StockStockin模型对象
+     * @var \app\admin\model\stock\Stockin
+     */
+    protected $noNeedRight = ['getlistByDocnum', 'countData','info'];
+    protected $model = null;
+    protected $listmodel = null;
+
+    /**
+     * 快速搜索时执行查找的字段
+     */
+    public function _initialize()
+    {
+        parent::_initialize();
+        $this->model = model('\app\admin\model\stock\Stockin');
+        $this->listmodel = model('\app\admin\model\stock\Stockinlist');
+        $result = collection(model('\app\admin\model\stock\Supplier')->select())->toArray();
+        $supplier = ['' => '==请选择=='];
+        foreach ($result as $k => $v) {
+            $supplier[$v['id']] = $v['name'];
+        };
+        $this->view->assign("supplier", $supplier);
+    }
+
+    /**
+     * 默认生成的控制器所继承的父类中有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();
+            }
+            $this->relationSearch = true;
+            $this->searchFields = 'docnum,supplier.name';
+            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
+            $total = $this->model
+                ->with("supplier")
+                ->where($where)
+                ->order($sort, $order)
+                ->count();
+            $list = $this->model
+                ->with("supplier")
+                ->where($where)
+                ->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()
+    {
+        //设置过滤方法
+        $this->request->filter(['strip_tags']);
+        if ($this->request->isAjax()) {
+            //如果发送的来源是Selectpage,则转发到Selectpage
+            if ($this->request->request('keyField')) {
+                return $this->selectpage();
+            }
+            $this->relationSearch = false;
+            $this->searchFields = 'docnum,goods.goodsname,goods.productmodel';
+            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
+            $total = $this->listmodel
+                ->with(['goods', 'stockin'])
+                ->where('stockin.audittime', 'NOT NULL')
+                ->where($where)
+                ->order($sort, $order)
+                ->count();
+            $list = $this->listmodel
+                ->with(['goods', 'stockin'])
+                ->where('stockin.audittime', 'NOT NULL')
+                ->where($where)
+                ->order($sort, $order)
+                ->limit($offset, $limit)
+                ->select();
+
+            foreach ($list as $k => $v) {
+                $v->stockin->supplier;
+            }
+
+            $list = collection($list)->toArray();
+            $result = array("total" => $total, "rows" => $list);
+
+            return json($result);
+        }
+        return $this->view->fetch();
+    }
+
+    /**
+     * 添加
+     */
+    public function add()
+    {
+        if ($this->request->isPost()) {
+            $params = $this->request->post("row/a");
+            $main = \GuzzleHttp\json_decode($params["main"], true);
+            $goods = \GuzzleHttp\json_decode($params["goodslist"], true);
+            if ($main && $goods) {
+                if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
+                    $params[$this->dataLimitField] = $this->auth->id;
+                }
+                try {
+                    $main["docnum"] = $this->model->createdocnum();
+                    $main['inboundtime'] = strtotime($main['inboundtime']);
+                    $num = count($goods);
+                    $stockinTotalNum = 0;
+                    $totalamount = 0.00;
+                    $result = \app\admin\model\stock\Stockin::create($main);
+                    for ($i = 0; $i < $num; $i++) {
+                        $goods[$i]['stock_stockin_id'] = $result->id;
+                        $goods[$i]['amount'] = bcmul($goods[$i]['stockinnums'], $goods[$i]['inboundprice'], 2);
+                        $totalamount = bcadd($totalamount, $goods[$i]['amount'], 2);
+                        $stockinTotalNum += $goods[$i]['stockinnums'];
+                    }
+                    $result->totalamount = $totalamount;
+                    $result->stockintotalnums = $stockinTotalNum;
+                    $result->save();
+                    $stockinlist = new \app\admin\model\stock\Stockinlist;
+                    $stockinlist->allowField(true)->saveAll($goods);
+                    if ($result !== false) {
+                        $this->success();
+                    } else {
+                        $this->error($this->model->getError());
+                    }
+                } catch (\think\exception\PDOException $e) {
+                    $this->error($e->getMessage());
+                }
+            }
+            $this->error(__('Parameter %s can not be empty', ''));
+        }
+        return $this->view->fetch();
+    }
+
+    /**
+     * 审核
+     */
+    public function audit()
+    {
+        if ($this->request->isPost()) {
+            $params = $this->request->post("row/a");
+            $row = $this->model->get($params["id"]);
+            if ($row) {
+                try {
+                    if ($row->audittime) {
+                        $this->error(__('单据已审核,不可重复审核', ''));
+                    }
+                    $row->audittime = time();
+                    $stockcurlist = [];
+                    $stockinlist = new \app\admin\model\stock\Stockinlist;
+                    $goodslist = $stockinlist->where("stock_stockin_id", $row->id)->select();
+                    foreach ($goodslist as $value) {
+                        $stockcur = ['stock_stockin_id' => $row->id, 'stock_stockin_list_id' => $value->id, 'stock_stockin_docnum' => $row->docnum, 'curnums' => $value->stockinnums, 'stock_goods_id' => $value->stock_goods_id];
+                        $stockcurlist[] = $stockcur;
+                    }
+                    $row->save();
+                    model('\app\admin\model\stock\Stockcur')->allowField(true)->saveAll($stockcurlist);
+                    $this->success();
+                } catch (\think\exception\PDOException $e) {
+                    $this->error($e->getMessage());
+                }
+            }
+            $this->error(__('Parameter %s can not be empty', ''));
+        }
+    }
+    /**
+     * 反审核
+     */
+    public function unaudit()
+    {
+        if ($this->request->isPost()) {
+            $params = $this->request->post("row/a");
+            if ($params) {
+                try {
+                    $row = $this->model->get($params["id"]);
+                    $stockinlist = new \app\admin\model\stock\Stockinlist;
+                    $goodslist = $stockinlist->where("stock_stockin_id", $row->id)->select();
+
+                    if ($goodslist) {
+                        if (!$row->audittime) {
+                            $this->error(__('单据已反审核,不可重复反审核!', ''));
+                        };
+                        foreach ($goodslist as $k => $v) {
+                            if ($v['charged']) {
+                                $this->error(__('存在已出库商品,禁止反审核', ''));
+                            }
+                        };
+                        $row->audittime = null;  //更新审核标记位
+                        $row->save();
+                        model('\app\admin\model\stock\Stockcur')->where('stock_stockin_id', $row->id)->delete();
+                        $this->success();
+                    } else {
+                        $this->error($this->model->getError());
+                    }
+                } catch (\think\exception\PDOException $e) {
+                    $this->error($e->getMessage());
+                }
+            }
+            $this->error(__('Parameter %s can not be empty', ''));
+        }
+    }
+
+    /**
+     * 结算
+     */
+    public function settle()
+    {
+        if ($this->request->isPost()) {
+            $params = $this->request->post("row/a");
+            $row = $this->model->get($params["id"]);
+            if ($row) {
+                try {
+                    if ($row->settletime) {
+                        $this->error(__('单据已结算,不可重复结算', ''));
+                    }
+                    $row->settletime = time();
+                    $row->save();
+                    $this->success();
+                } catch (\think\exception\PDOException $e) {
+                    $this->error($e->getMessage());
+                }
+            }
+            $this->error(__('Parameter %s can not be empty', ''));
+        }
+    }
+
+    /**
+     * 反结算
+     */
+    public function unsettle()
+    {
+        if ($this->request->isPost()) {
+            $params = $this->request->post("row/a");
+            $row = $this->model->get($params["id"]);
+            if ($row) {
+                try {
+                    if (!$row->settletime) {
+                        $this->error(__('单据尚未结算,操作失败', ''));
+                    }
+                    $row->settletime = null;
+                    $row->save();
+                    $this->success();
+                } catch (\think\exception\PDOException $e) {
+                    $this->error($e->getMessage());
+                }
+            }
+            $this->error(__('Parameter %s can not be empty', ''));
+        }
+    }
+
+    /**
+     * 编辑
+     */
+    public function edit($ids = null)
+    {
+        $row = $this->model->get($ids);
+        if (!$row) {
+            $this->error(__('No Results were found'));
+        }
+        $adminIds = $this->getDataLimitAdminIds();
+        if (is_array($adminIds)) {
+            if (!in_array($row[$this->dataLimitField], $adminIds)) {
+                $this->error(__('You have no permission'));
+            }
+        }
+        if ($row['audittime']) {
+            return ($this->error(__('该入库记录已审核,不能编辑!')));
+        }
+        $stockinlist = new \app\admin\model\stock\Stockinlist;
+        $list = collection($stockinlist->where('stock_stockin_id', $row->id)->select())->toArray();
+        foreach ($list as $k => $v) {
+            if ($v['charged']) {
+                return ($this->error(__('该入库记录存在已出库商品,不能编辑!')));
+            }
+        }
+        if ($this->request->isPost()) {
+            $params = $this->request->post("row/a");
+            $main = \GuzzleHttp\json_decode($params["main"], true);
+            $goods = \GuzzleHttp\json_decode($params["goodslist"], true);
+            $delist = \GuzzleHttp\json_decode($params["deletedgoodslist"], true);
+            if ($main && $goods) {
+                try {
+                    $main["inboundtime"] = strtotime($main["inboundtime"]);
+                    $num = count($goods);
+                    $stockintotalnums = 0;
+                    $totalamount = 0.00;
+                    for ($i = 0; $i < $num; $i++) {
+                        $goods[$i]['stock_stockin_id'] = $row->id;
+                        $goods[$i]['amount'] = bcmul($goods[$i]['stockinnums'], $goods[$i]['inboundprice'], 2);
+                        $totalamount = bcadd($totalamount, $goods[$i]['amount'], 2);
+                        $stockintotalnums += $goods[$i]['stockinnums'];
+                    }
+                    $main["totalamount"] = $totalamount;
+                    $main["stockintotalnums"] = $stockintotalnums;
+                    unset($main["supplier"]);
+                    $row->save($main);
+                    $result = $stockinlist->allowField(true)->saveAll($goods);
+                    $stockinlist->destroy($delist);
+                    if ($result !== false) {
+                        $this->success();
+                    } else {
+                        $this->error($this->model->getError());
+                    }
+                } catch (\think\exception\PDOException $e) {
+                    $this->error($e->getMessage());
+                }
+            }
+            $this->error(__('Parameter %s can not be empty', ''));
+        }
+        $this->view->assign("row", $row);
+        return $this->view->fetch();
+    }
+
+    /**
+     * 查看
+     */
+    public function look($ids = null)
+    {
+        $row = $this->model->get($ids);
+        if (!$row) {
+            $this->error(__('No Results were found'));
+        }
+        $operatetype = $this->request->get("operatetype");
+        if ($operatetype  == "audit") {
+            if ($row->settletime !== null) {
+                $this->error("该入库单已结算,请先进行反结算!");
+            }
+        } else if ($operatetype == "settle") {
+            if ($row->audittime == null) {
+                $this->error("该入库单未审核,请先进行审核!");
+            }
+        }
+        $this->view->assign("row", $row);
+        return $this->view->fetch();
+    }
+
+    /**
+     * 删除
+     */
+    public function del($ids = "")
+    {
+        if ($ids) {
+            $row = $this->model->get($ids);
+            if (!$row) {
+                $this->error(__('No Results were found'));
+            }
+            $stockinlist = new \app\admin\model\stock\Stockinlist;
+            $list = collection($stockinlist->where('stock_stockin_id', $row->id)->select())->toArray();
+            if ($row['audittime']) {
+                return ($this->error(__('该入库记录已审核,不能删除!')));
+            }
+            foreach ($list as $k => $v) {
+                if ($v['charged']) {
+                    return ($this->error(__('该入库记录存在已出库商品,不能删除!')));
+                }
+            }
+            $result = $stockinlist->where('stock_stockin_id', $row->id)->delete();
+            $result = $row->delete();
+            if ($result) {
+                $this->success();
+            } else {
+                $this->error(__('No rows were deleted'));
+            }
+        }
+        $this->error(__('Parameter %s can not be empty', 'ids'));
+    }
+
+    /**
+     * 查看页,根据单据编号返回商品信息
+     */
+    public function info($id = null)
+    {
+        if ($this->request->isPost()) {
+            if ($id) {
+                try {
+                    $row = $this->model->get($id);
+                    $row->supplier;
+                    $goodslist = \think\Db::view('stock_stockin_list', '*')
+                        ->view('stock_goods', 'id as stock_goods_id,volnum,goodsname,productmodel,measureunit', 'stock_stockin_list.stock_goods_id=stock_goods.id', 'left')
+                        ->where('stock_stockin_list.stock_stockin_id', $id)->select();
+                    return json(["row" => $row, "goodslist" => $goodslist]);
+                } catch (\think\exception\PDOException $e) {
+                    $this->error($e->getMessage());
+                }
+            }
+            $this->error(__('Parameter %s can not be empty', ''));
+        }
+        $this->error('只接受POST请求', '');
+    }
+
+    /**
+     * 控制台出入库统计
+     */
+    public function countData()
+    {
+        //当前是否为关联查询
+        $this->relationSearch = true;
+        if ($this->request->isAjax()) {
+            $months = array();
+            for ($i = 0; $i <= 12; $i++) {
+                $months[] = date("Y-m", strtotime(date('y-m-01') . '-' . $i . ' months'));
+            }
+            $months=array_reverse($months);
+            $inlist = $this->listmodel
+                ->with('stockin')
+                ->field('FROM_UNIXTIME(stockin.createtime,"%Y-%m") month,sum(amount) amount')
+                ->where('stockin.audittime', 'not null')
+                ->group('FROM_UNIXTIME(stockin.createtime,"%Y-%m")')
+                ->select();
+            $outlistmodel = model('\app\admin\model\stock\Stockoutlist');
+            $outlist = $outlistmodel
+                ->with('stockout')
+                ->field('FROM_UNIXTIME(stockout.createtime,"%Y-%m") month,sum(amount) amount')
+                ->where('stockout.audittime', 'not null')
+                ->group('FROM_UNIXTIME(stockout.createtime,"%Y-%m")')
+                ->select();
+            $indata = [];
+            $outdata = [];
+            foreach ($months as $k => $v) {
+                $indata[$k] = 0;
+                $outdata[$k] = 0;
+                foreach ($inlist as $ink => $inv) {
+
+                    if ($inv->month == $v) {
+                        $indata[$k] = intval($inv->amount);
+                    }
+                }
+                foreach ($outlist as $outk => $outv) {
+                    if ($outv->month == $v) {
+                        $outdata[$k] = intval($outv->amount);
+                    }
+                }
+            }
+//            print_r($indata);die;
+            return json(["month"=>$months,"indata" => $indata, "outdata" => $outdata]);
+        }
+    }
+
+    /**
+     * 打印数据(LODOP)
+     */
+    public function printer()
+    {
+        return $this->view->fetch();
+    }
+}

+ 589 - 0
application/admin/controller/stock/Stockout.php

@@ -0,0 +1,589 @@
+<?php
+
+namespace app\admin\controller\stock;
+
+use app\common\controller\Backend;
+use fast\Tree;
+use app\admin\model;
+use think\Exception;
+
+/**
+ * 商品出库
+ *
+ * @icon fa fa-circle-o
+ */
+class Stockout extends Backend
+{
+
+    /**
+     * StockStockout模型对象
+     * @var \app\admin\modelstock\Stockout
+     */
+    protected $model = null;
+    protected $listmodel = null;
+    protected $noNeedLogin = ['countData','info'];
+
+    public function _initialize()
+    {
+        parent::_initialize();
+        $this->model = model('\app\admin\model\stock\Stockout');
+        $this->listmodel = model('\app\admin\model\stock\Stockoutlist');
+        $customerList = collection(model('\app\admin\model\stock\Customer')->select())->toArray();
+        Tree::instance()->init($customerList);
+        $customerList = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'name');
+        $customer = ['' => '==请选择=='];
+        foreach ($customerList as $k => $v) {
+            $customer[$v['id']] = $v['name'];
+        }
+        $this->view->assign('customer', $customer);
+    }
+
+    /**
+     * 默认生成的控制器所继承的父类中有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();
+            }
+            $this->relationSearch = true;
+            $this->searchFields = 'docnum,customer.name';
+            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
+            $total = $this->model
+                ->where($where)
+                ->with('customer')
+                ->order($sort, $order)
+                ->count();
+
+            $list = $this->model
+                ->where($where)
+                ->with('customer')
+                ->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()
+    {
+        //设置过滤方法
+        $this->request->filter(['strip_tags']);
+        if ($this->request->isAjax()) {
+            //如果发送的来源是Selectpage,则转发到Selectpage
+            if ($this->request->request('keyField')) {
+                return $this->selectpage();
+            }
+            $this->relationSearch = true;
+            $this->searchFields = 'docnum,goods.goodsname,goods.productmodel';
+            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
+            $total = $this->listmodel
+                ->with(['goods','stockinlist','stockout'])
+                ->where('stockout.audittime', 'NOT NULL')
+                ->where($where)
+                ->order($sort, $order)
+                ->count();
+            $list = $this->listmodel
+                ->with(['goods','stockinlist','stockout'])
+                ->where('stockout.audittime', 'NOT NULL')
+                ->where($where)
+                ->order($sort, $order)
+                ->limit($offset, $limit)
+                ->select();
+            foreach ($list as $k => $v) {
+                $v->stockout->customer;
+            }
+            $list = collection($list)->toArray();
+            $result = array("total" => $total, "rows" => $list);
+            return json($result);
+        }
+        return $this->view->fetch();
+    }
+
+    /**
+     * 添加
+     */
+    public function add()
+    {
+        if ($this->request->isPost()) {
+            $params = $this->request->post("row/a");
+            $main = json_decode($params['main'], true);
+            $goods = json_decode($params['goodslist'], true);
+            if ($main && $goods) {
+                try {
+                    $main["docnum"] = $this->model->createdocnum();
+                    $main['outboundtime'] = strtotime($main['outboundtime']);
+                    $num = count($goods);
+                    $stockoutgoods = [];
+                    $stockcurmodel = model('\app\admin\model\stock\Stockcur');
+                    $stockOutTotalNum = 0;       //单据总出库商品总数
+                    $errorgoods = [];
+                    for ($i = 0; $i < $num; $i++) {
+                        //$curtotalnum 要出库的商品当前库存总数
+                        $curtotalnum = $stockcurmodel->where('stock_goods_id', $goods[$i]['stock_goods_id'])->sum('curnums');
+                        $outtotalnum = 0;
+                        foreach ($goods as $v) {
+                            if ($v['stock_goods_id'] == $goods[$i]['stock_goods_id']) {
+                                $outtotalnum += $v['stockoutnums'];
+                            }
+                        }
+                        $stockOutTotalNum += $goods[$i]['stockoutnums'];
+                        //库存总数小于要出库数 出库失败!
+                        if ($curtotalnum < $outtotalnum) {
+                            $errorgoods[] = $goods[$i]['goodsname'] . '当前库存:' . $curtotalnum . ',出库数量' . $outtotalnum . ',出库数大于库存数,操作失败!';
+                        } else {
+                            $stockoutgoods[] = array_merge($params, $goods[$i]);
+                        }
+                    }
+                    if (count($errorgoods) > 0) {
+                        $this->error(implode('</br>', $errorgoods)); //拼接成字符串
+                    }
+                    $main['totaloutamount'] = null;
+                    $main['stockouttotalnums'] = $stockOutTotalNum;
+                    $this->model->save($main);
+                    for ($i = 0; $i < count($stockoutgoods); $i++) {
+                        $stockoutgoods[$i]['stock_stockout_id'] = $this->model->id;
+                    }
+                    $result = $this->listmodel->allowField(true)->saveAll($stockoutgoods, false);
+                    if ($result !== false) {
+                        $this->success();
+                    } else {
+                        $this->error($this->model->getError());
+                    }
+                } catch (\think\exception\PDOException $e) {
+                    $this->error($e->getMessage());
+                }
+            }
+            $this->error(__('Parameter %s can not be empty', ''));
+        }
+        return $this->view->fetch();
+    }
+
+    /**
+     * 编辑
+     */
+    public function edit($ids = null)
+    {
+        $row = $this->model->get($ids);
+        if (!$row) {
+            $this->error(__('No Results were found'));
+        }
+        if ($row['audittime']) {
+            return ($this->error(__('该记录已通过审核,不能修改!')));
+        }
+        if ($row['settletime']) {
+            return ($this->error(__('该记录已结算,不能修改!')));
+        }
+        if ($this->request->isPost()) {
+            $params = $this->request->post("row/a");
+            $main = json_decode($params['main'], true);
+            $goods = json_decode($params['goodslist'], true);
+            $delist = json_decode($params["deletedgoodslist"], true);
+            if ($main && $goods) {
+                try {
+                    $row->outboundtime = strtotime($main['outboundtime']);
+                    $row->stock_customer_id = $main['stock_customer_id'];
+                    $row->remark = $main['remark'];
+                    $num = count($goods);
+                    $stockoutgoods = [];
+                    $stockcurmodel = model('\app\admin\model\stock\Stockcur');
+                    $stockOutTotalNum = 0;       //单据总出库商品总数
+                    $errorgoods = [];
+                    for ($i = 0; $i < $num; $i++) {
+                        //$curtotalnum 要出库的商品当前库存总数
+                        $curtotalnum = $stockcurmodel->where('stock_goods_id', $goods[$i]['stock_goods_id'])->sum('curnums');
+                        $outtotalnum = 0;
+                        foreach ($goods as $v) {
+                            if ($v['stock_goods_id'] == $goods[$i]['stock_goods_id']) {
+                                $outtotalnum += $v['stockoutnums'];
+                            }
+                        }
+                        $stockOutTotalNum += $goods[$i]['stockoutnums'];
+                        //库存总数小于要出库数 出库失败!
+                        if ($curtotalnum < $outtotalnum) {
+                            $errorgoods[] = $goods[$i]['goodsname'] . '当前库存:' . $curtotalnum . ',出库数量' . $outtotalnum . ',出库数大于库存数,操作失败!';
+                        } else {
+                            $stockoutgoods[] = array_merge($params, $goods[$i]);
+                        }
+                    }
+                    if (count($errorgoods) > 0) {
+                        $this->error(implode('</br>', $errorgoods)); //拼接成字符串
+                    }
+                    $row->stockouttotalnums = $stockOutTotalNum;
+                    $row->save();
+                    for ($i = 0; $i < count($stockoutgoods); $i++) {
+                        $stockoutgoods[$i]['stock_stockout_id'] = $row->id;
+                    }
+                    $result = $this->listmodel->allowField(true)->saveAll($stockoutgoods);
+                    $this->listmodel->destroy($delist);
+                    if ($result !== false) {
+                        $this->success();
+                    } else {
+                        $this->error($this->model->getError());
+                    }
+                } catch (\think\exception\PDOException $e) {
+                    $this->error($e->getMessage());
+                }
+            }
+            $this->error(__('Parameter %s can not be empty', ''));
+        }
+        $this->view->assign("row", $row);
+        return $this->view->fetch();
+    }
+    /**
+     * 审核
+     */
+    public function audit()
+    {
+        if ($this->request->isPost()) {
+            $params = $this->request->post("row/a");
+            if ($params) {
+                $stockcurmodel = model('\app\admin\model\stock\Stockcur');
+                $stockinlistmodel = model('\app\admin\model\stock\Stockinlist');
+                $row = $this->model->get($params["id"]);
+                if (!$row) {
+                    $this->error(__('No Results were found'));
+                }
+                $stockcurmodel->startTrans();
+                $stockinlistmodel->startTrans();
+                $this->model->startTrans();
+                $this->listmodel->startTrans();
+                try {
+                    $totalOutAmount = 0;   //单据总出库金额
+                    $stockOutTotalNum = 0;       //单据总出库商品总数
+                    $goods = $this->listmodel->with('goods')->where('stock_stockout_id', $row->id)->select();
+                    $newgoods = [];
+                    $errorgoods = [];
+                    if ($goods) {
+                        if ($row->audittime) {
+                            $this->error(__('出库单据已审核,不可重复审核', ''));
+                        }
+                        $num = count($goods);
+                        for ($i = 0; $i < $num; $i++) {
+                            //$curtotalnum 要出库的商品当前库存总数
+                            $curtotalnum = $stockcurmodel->where('stock_goods_id', $goods[$i]['stock_goods_id'])->sum('curnums');
+                            //库存总数小于此次要出库数 出库失败!
+                            if ($curtotalnum < $goods[$i]['stockoutnums']) {
+                                $errorgoods[] = $goods[$i]['goods']['goodsname'] . '当前库存:' . $curtotalnum . ',出库数量' . $goods[$i]['stockoutnums'] . ',出库数大于库存数,操作失败!';
+                            } else {
+                                //获得要出库商品的所有库存信息
+                                $stockcurgoods = $stockcurmodel->with('stockinlist')->where('stockcur.stock_goods_id', $goods[$i]['stock_goods_id'])->order('id')->select();
+                                $curstockoutgoods = $goods[$i]->toArray();
+                                unset($curstockoutgoods['id']);
+                                //出库初始数量为
+                                $stockoutnum = 0;
+                                $j = 0;
+                                while ($stockoutnum < $goods[$i]['stockoutnums']) {
+                                    $neednum = $goods[$i]['stockoutnums'] - $stockoutnum; //$neednum 还需要出库的数量
+                                    $restnum = $stockcurgoods[$j]['curnums'] - $neednum; //$restnum  该条记录出完库后剩余库存。
+                                    //小于等于0则表示不够出或刚够
+                                    if ($restnum <= 0) {
+                                        $curstockoutgoods['stock_stockout_id'] = $row->id;
+                                        $curstockoutgoods['stockoutgoodsremark'] = $goods[$i]['stockoutgoodsremark'];
+                                        $curstockoutgoods['stock_stockinlist_id'] = $stockcurgoods[$j]['stockinlist']['id'];
+                                        $curstockoutgoods['stockoutnums'] = $stockcurgoods[$j]['curnums'];
+                                        $curstockoutgoods['amount'] = bcmul($stockcurgoods[$j]['curnums'], $stockcurgoods[$j]['stockinlist']['inboundprice'], 2);
+                                        $stockoutnum += $stockcurgoods[$j]['curnums'];
+                                        $totalOutAmount += $curstockoutgoods['amount'];
+                                        $stockOutTotalNum += $stockcurgoods[$j]['curnums'];
+                                        $stockcurmodel->destroy($stockcurgoods[$j]['id']);
+                                        $stockinlist = $stockinlistmodel->get($stockcurgoods[$j]['stockinlist']['id']);
+                                        $stockinlist->charged = true;
+                                        $stockinlist->save();
+                                    } elseif (($restnum > 0)) { //大于0则表示够出
+                                        $curstockoutgoods['stock_stockout_id'] = $row->id;
+                                        $curstockoutgoods['stockoutgoodsremark'] = $goods[$i]['stockoutgoodsremark'];
+                                        $curstockoutgoods['stock_stockinlist_id'] = $stockcurgoods[$j]['stockinlist']['id'];
+                                        $curstockoutgoods['stockoutnums'] = $neednum;
+                                        $curstockoutgoods['amount'] = bcmul($neednum, $stockcurgoods[$j]['stockinlist']['inboundprice'], 2);
+                                        $stockoutnum += $neednum;
+                                        $totalOutAmount += $curstockoutgoods['amount'];
+                                        $stockOutTotalNum += $neednum;
+                                        $stockcurgoods[$j]['curnums'] = $restnum;
+                                        $updatestockcur = $stockcurmodel->get($stockcurgoods[$j]['id']);
+                                        $updatestockcur->curnums = $restnum;
+                                        $updatestockcur->save();
+                                        $stockinlist = $stockinlistmodel->get($stockcurgoods[$j]['stockinlist']['id']);
+                                        $stockinlist->charged = true;
+                                        $stockinlist->save();
+                                    }
+                                    $newgoods[] = $curstockoutgoods;
+                                    $j++;
+                                }
+                            }
+                        };
+                    }
+                    if (count($errorgoods) > 0) {
+                        throw new Exception(implode('</br>', $errorgoods));
+                    }
+                    $row->totaloutamount = $totalOutAmount;
+                    $row->stockouttotalnums = $stockOutTotalNum;
+                    $row->audittime = time();
+                    $row->save();
+                    $goods = $this->listmodel->where('stock_stockout_id', $row->id)->delete();
+                    $result = $this->listmodel->allowField(true)->saveAll($newgoods);  //更新审核标记位
+                    $this->model->commit();
+                    $this->listmodel->commit();
+                    $stockcurmodel->commit();
+                    $stockinlistmodel->commit();
+                    if ($result !== false) {
+                        $this->success();
+                    } else {
+                        $this->error($this->model->getError());
+                    }
+                } catch (\think\exception\PDOException $e) {
+                    $this->model->rollBack();
+                    $this->listmodel->rollBack();
+                    $stockcurmodel->rollBack();
+                    $stockinlistmodel->rollBack();
+                    $this->error($e->getMessage());
+                } catch (\think\Exception $e) {
+                    $this->model->rollBack();
+                    $this->listmodel->rollBack();
+                    $stockcurmodel->rollBack();
+                    $stockinlistmodel->rollBack();
+                    $this->error($e->getMessage());
+                }
+            }
+            $this->error(__('Parameter %s can not be empty', ''));
+        }
+    }
+
+    /**
+     * 反审核
+     */
+    public function unaudit()
+    {
+        if ($this->request->isPost()) {
+            $params = $this->request->post("row/a");
+            $row = $this->model->get($params["id"]);
+            if ($row) {
+                try {
+                    if (!$row->audittime) {
+                        $this->error(__('出库单据已反审核,不可重复反审核', ''));
+                    };
+                    $goods = $this->listmodel->where('stock_stockout_id', $row->id)->select();
+                    if ($goods) {
+                        foreach ($goods as $k => $v) {
+                            $stockinlistmodel = model('\app\admin\model\stock\Stockinlist')->get(["id" => $v['stock_stockinlist_id']]);
+                            $stockinmodel = model('\app\admin\model\stock\Stockin')->get($stockinlistmodel->stock_stockin_id);
+                            $stockcurmodel = model('\app\admin\model\stock\Stockcur')->get(["stock_stockin_list_id" => $v['stock_stockinlist_id']]);
+                            if ($stockcurmodel) {
+                                $stockcurmodel->curnums = $stockcurmodel->curnums + $v->stockoutnums;
+                                if ($stockcurmodel->curnums == $stockinlistmodel->stockinnums) {
+                                    $stockinlistmodel->charged = false;
+                                    $stockinlistmodel->save();
+                                }
+                                $stockcurmodel->save();
+                            } else {
+                                $stockcur = [
+                                    'stock_stockin_list_id' => $stockinlistmodel['id'],
+                                    'stock_stockin_id' => $stockinlistmodel['stock_stockin_id'],
+                                    'curnums' => $v->stockoutnums,
+                                    'stock_goods_id' => $stockinlistmodel['stock_goods_id'],
+                                    'stock_stockin_docnum' => $stockinmodel->docnum,
+                                ];
+                                model('\app\admin\model\stock\Stockcur')->isUpdate(false)->data($stockcur, true)->save();
+                                if ($stockinlistmodel->stockinnums == $v->stockoutnums) {
+                                    $stockinlistmodel->charged = false;
+                                    $stockinlistmodel->save();
+                                }
+                            }
+                            $v->stock_stockinlist_id = null;
+                            $v->amount = null;
+                            $v->save();
+                        };
+                    } else {
+                        $this->error($this->model->getError());
+                    }
+                    $row->audittime = null;
+                    $row->totaloutamount = null;
+                    $result = $row->save();
+
+                    if ($result !== false) {
+                        $this->success();
+                    } else {
+                        $this->error($this->model->getError());
+                    }
+                } catch (\think\exception\PDOException $e) {
+                    $this->error($e->getMessage());
+                }
+            }
+            $this->error(__('Parameter %s can not be empty', ''));
+        }
+    }
+
+    /**
+     * 结算
+     */
+    public function settle()
+    {
+        if ($this->request->isPost()) {
+            $params = $this->request->post("row/a");
+            $row = $this->model->get($params["id"]);
+            if ($row) {
+                try {
+                    $row->settletime = time();
+                    $result = $row->save();
+                    if ($result) {
+                        $this->success('结算成功');
+                    } else {
+                        $this->error(__('结算失败'));
+                    }
+                } catch (\think\exception\PDOException $e) {
+                    $this->error($e->getMessage());
+                }
+            }
+            $this->error(__('Parameter %s can not be empty', ''));
+        }
+    }
+
+    /**
+     * 反结算
+     */
+    public function unsettle()
+    {
+        if ($this->request->isPost()) {
+            $params = $this->request->post("row/a");
+            $row = $this->model->get($params["id"]);
+            if ($row) {
+                try {
+                    $row->settletime = null;
+                    $result = $row->save();
+                    if ($result) {
+                        $this->success('反结算成功');
+                    } else {
+                        $this->error(__('反结算失败'));
+                    }
+                } catch (\think\exception\PDOException $e) {
+                    $this->error($e->getMessage());
+                }
+            }
+            $this->error(__('Parameter %s can not be empty', ''));
+        }
+    }
+
+    /**
+     * 查看
+     */
+    public function look($ids = null)
+    {
+        $row = $this->model->get($ids);
+        if (!$row) {
+            $this->error(__('No Results were found'));
+        }
+        return $this->view->fetch();
+    }
+
+    /**
+     * 删除
+     */
+    public function del($ids = "")
+    {
+        if ($ids) {
+            $row = $this->model->get($ids);
+            if (!$row) {
+                $this->error(__('No Results were found'));
+            }
+            $list = collection($this->listmodel->where('stock_stockout_id', $row->id)->select())->toArray();
+            if ($row['audittime']) {
+                return ($this->error(__('该单据已审核,不能删除!')));
+            }
+            $result = $this->listmodel->where('stock_stockout_id', $row->id)->delete();
+            $result = $row->delete();
+            if ($result) {
+                $this->success();
+            } else {
+                $this->error(__('No rows were deleted'));
+            }
+        }
+        $this->error(__('Parameter %s can not be empty', 'ids'));
+    }
+
+    /**
+     * 查看页,根据单据编号返回商品信息
+     */
+    public function info($id = null)
+    {
+        if ($this->request->isPost()) {
+            if ($id) {
+                try {
+                    $row = $this->model->get($id);
+                    $row->customer;
+                    $goodslist = \think\Db::view('stock_stockout_list', '*')
+                        ->view('stock_goods', 'id as stock_goods_id,volnum,goodsname,productmodel,measureunit', 'stock_stockout_list.stock_goods_id=stock_goods.id', 'left')
+                        ->view('stock_stockin_list', 'id as stock_stockin_list,inboundprice', 'stock_stockin_list.id=stock_stockout_list.stock_stockinlist_id', 'left')
+                        ->where('stock_stockout_list.stock_stockout_id', $id)->select();
+                    return json(["row" => $row, "goodslist" => $goodslist]);
+                } catch (\think\exception\PDOException $e) {
+                    $this->error($e->getMessage());
+                }
+            }
+            $this->error(__('Parameter %s can not be empty', ''));
+        }
+        $this->error('只接受POST请求', '');
+    }
+
+    /**
+     * 控制台出库统计
+     */
+    public function countData()
+    {
+        if ($this->request->isAjax()) {
+            $dateRange = $this->request->request("dateRange");
+            $dateRange = explode(' - ', $dateRange);
+            $dateRange = count($dateRange) > 1 ? $dateRange : $dateRange[0];
+            $list1 = $this->model
+                ->with('stockgoods,customer')
+                ->field('sum(amount) totalitemamount,count(*) totalitem,sum(stockoutnums) totalnum')
+                ->where('audittime', 'not null')
+                ->whereTime('stockout.createtime', $dateRange)
+                ->group('stockout.stock_customer_id')
+                ->select();
+            $list1 = collection($list1)->toArray();
+
+            $list2 = $this->model
+                ->with('stockgoods,customer')
+                ->field('sum(amount) totalitemamount,count(*) totalitem,sum(stockoutnums) totalnum')
+                ->where('audittime', 'not null')
+                ->whereTime('stockout.createtime', $dateRange)
+                ->group('goodscategoryid')
+                ->select();
+            $list2 = collection($list2)->toArray();
+            $result = array("rows1" => $list1, "rows2" => $list2);
+            return json($result);
+        }
+    }
+
+    /**
+     * 打印数据(LODOP)
+     */
+    public function printer($id)
+    {
+        $row = $this->model->get($id);
+        if (!$row) {
+            $this->error(__('No Results were found'));
+        } else if (!$row->audittime) {
+            $this->error(__('单据尚未审核,请先审核!'));
+        }
+        return $this->view->fetch();
+    }
+}

+ 30 - 0
application/admin/controller/stock/Supplier.php

@@ -0,0 +1,30 @@
+<?php
+
+namespace app\admin\controller\stock;
+
+use app\common\controller\Backend;
+
+/**
+ * 供应商
+ *
+ * @icon fa fa-circle-o
+ */
+class Supplier extends Backend {
+
+    /**
+     * StockSupplier模型对象
+     * @var \app\admin\model\stock\Supplier
+     */
+    protected $model = null;
+    protected $searchFields = 'name';
+    public function _initialize() {
+        parent::_initialize();
+        $this->model = model('\app\admin\model\stock\Supplier');
+    }
+
+    /**
+     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
+     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
+     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
+     */
+}

+ 21 - 0
application/admin/lang/zh-cn/stock/customer.php

@@ -0,0 +1,21 @@
+<?php
+
+return [
+    'Id'  =>  'ID',
+    'Pid'  =>  '父ID',
+    'Encode'  =>  '客户编号',
+    'Deptype'  =>  '客户类型',
+    'Name'  =>  '全名',
+    'Shortname'  =>  '名称',
+    'Innerphone'  =>  '内部电话号码',
+    'Advanceamount'  =>  '预收金额',
+    'Receivableamount'  =>  '应收金额',
+    'Level'  =>  '级别',
+    'Weigh'  =>  '排序码',
+    'Enabledmark'  =>  '有效',
+    'Enabledmark 1'  =>  '是',
+    'Enabledmark 0'  =>  '否',
+    'Remark'  =>  '备注',
+    'Createtime'  =>  '创建时间',
+    'Updatetime'  =>  '更新时间'
+];

+ 18 - 0
application/admin/lang/zh-cn/stock/datadict.php

@@ -0,0 +1,18 @@
+<?php
+
+return [
+    'Id'  =>  'ID',
+    'Pid'  =>  '父ID',
+    'Name'  =>  '名称',
+    'Code'  =>  '编码',
+    'Istree'  =>  '树形',
+    'Istree 0'  =>  '否',
+    'Istree 1'  =>  '是',
+    'Createtime'  =>  '创建时间',
+    'Updatetime'  =>  '更新时间',
+    'Weigh'  =>  '权重',
+    'Remark'  =>  '备注',
+    'Enabled'  =>  '有效',
+    'Enabled 1'  =>  '是',
+    'Enabled 0'  =>  '否'
+];

+ 15 - 0
application/admin/lang/zh-cn/stock/datadictitem.php

@@ -0,0 +1,15 @@
+<?php
+
+return [
+    'Id' => 'ID',
+    'Pid' => '父ID',
+    'Datadict_id' => '字典ID',
+    'Name' => '名称',
+    'Value' => '值',
+    'Weigh' => '权重',
+    'Remark' => '备注',
+    'Enabled' => '有效',
+    'Enabled 1' => '是',
+    'Enabled 0' => '否',
+    'Datadict Category' => "字典分类"
+];

+ 31 - 0
application/admin/lang/zh-cn/stock/goods.php

@@ -0,0 +1,31 @@
+<?php
+
+return [
+    'Id' => 'ID',
+    'Volnum' => '商品代码',
+    'goodscategory' => '商品类别',
+    'Goodsname' => '商品名称',
+    'Spell' => '简拼',
+    'Productmodel' => '规格型号',
+    'Measureunit' => '单位',
+    'Univalence' => '单价',
+    'Image' => '图片',
+    'Createtime' => '创建时间',
+    'Updatetime' => '更新时间',
+    'Enablewarn' => '库存告警',
+    'Minstock' => '最低库存',
+    'enablewarn 1' => '是',
+    'enablewarn 0' => '否',
+    'Inoutdetail' => '出入明细',
+    'Please select commodity classification first!' => '请先选择商品分类!',
+    'Only one record can be selected!' => '只能选择一条记录!',
+    'Please select a record!' => '请选择一条记录!',
+    'Summary' => '汇总',
+    'InoutType'=>'出入类型',
+    'stockInNum' => '入库数量',
+    'stockOutNum' => '出库数量',
+    'stockInAmount' => '入库总价',
+    'stockOutAmount' => '出库总价',
+    'InoutTime' => '出入时间',
+    'Barcode' => '商品条码',
+];

+ 7 - 0
application/admin/lang/zh-cn/stock/goodscategory.php

@@ -0,0 +1,7 @@
+<?php
+
+return [
+    'Name'  =>  '商品名',
+    'Value'  =>  '商品码前缀',
+    'Toggle all'=>'展开全部'
+];

+ 9 - 0
application/admin/lang/zh-cn/stock/stockcur.php

@@ -0,0 +1,9 @@
+<?php
+
+return [
+    'Id'  =>  'ID',
+    'Stock_stockin_id'  =>  '入库ID',
+    'Curnum'  =>  '当前数量',
+    'Stock_goods_id'  =>  '商品ID',
+    'stock_goods.goodsname'  =>  '商品名称'
+];

+ 24 - 0
application/admin/lang/zh-cn/stock/stockin.php

@@ -0,0 +1,24 @@
+<?php
+
+return [
+    'Id'  =>  'ID',
+    'docnum'  =>  '入库单号',
+    'Supplierlist'  =>  '供应商',
+    'Allocation'  =>  '货位',
+    'Partno'  =>  '机件号',
+    'Inboundtime'  =>  '入库日期',
+    'Stock_goods_id'  =>  '商品ID',
+    'Noticenum'  =>  '通知数',
+    'Actualnum'  =>  '实收数',
+    'Inboundprice'  =>  '入库单价',
+    'Amount'  =>  '金额',
+    'Grade'  =>  '等级',
+    'Grade 1'  =>  '优等品',
+    'Grade 2'  =>  '一等品',
+    'Grade 3'  =>  '合格品',
+    'Grade 0'  =>  '不合格品',
+    'Remark'  =>  '备注',
+    'Createtime'  =>  '创建时间',
+    'Updatetime'  =>  '更新时间',
+    'stock_goods.goodsname'=>'商品名称'
+];

+ 15 - 0
application/admin/lang/zh-cn/stock/stockout.php

@@ -0,0 +1,15 @@
+<?php
+
+return [
+    'Id'  =>  'ID',
+    'Vouchernum'  =>  '出库凭证',
+    'Outboundtime'  =>  '出库日期',
+    'Docnum'  =>  '文号',
+    'Stock_stockin_id'  =>  '入库ID',
+    'Stockin_vouchernum'  =>  '入库批次',
+    'Stock_goods_id'  =>  '商品ID',
+    'Outnums'  =>  '出库数',
+    'Remark'  =>  '备注',
+    'Createtime'  =>  '创建时间',
+    'Updatetime'  =>  '修改时间'
+];

+ 11 - 0
application/admin/lang/zh-cn/stock/supplier.php

@@ -0,0 +1,11 @@
+<?php
+
+return [
+    'Name'  =>  '名称',
+    'Shortname'  =>  '简称',
+    'Code'  =>  '代码',
+    'Shipaddress'  =>  '发货地址',
+    'Address'  =>  '联系地址',
+    'Contacts'  =>  '联系人',
+    'Telphone'  =>  '联系电话'
+];

+ 21 - 21
application/admin/view/formula/task.html

@@ -87,29 +87,29 @@
         <label class="control-label col-xs-12 col-sm-1"></label>
         <button type="button" id="add" class="btn btn-success">确定生成作业票</button>
     </div>
-    <!--<div class="form-group">-->
-        <!--<table class="table table-striped table-bordered table-hover table-nowrap" id="gy">-->
-            <!--<tr>-->
-                <!--<th class="col-xs-12 col-sm-1">原材料</th>-->
-                <!--<th class="col-xs-12 col-sm-1">百分比</th>-->
-                <!--<th class="col-xs-12 col-sm-1">应加量(KG)</th>-->
-                <!--<th class="col-xs-12 col-sm-6">操作记录</th>-->
-                <!--<th class="col-xs-12 col-sm-1">工序号</th>-->
-            <!--</tr>-->
-            <!--{foreach name='row["gyinfo"]' id='vo'}-->
-            <!--<tr>-->
-                <!--<td><input  class="form-control material" type="text" value="{$vo.material}" readonly></td>-->
-                <!--<td><input  class="form-control percentage" type="text" value="{$vo.percentage}" readonly></td>-->
-                <!--<td><input  class="form-control num" type="text" data-id="{$vo.id}" id="input-{$vo.id}" value="" readonly></td>-->
-                <!--<td><input  class="form-control gy_name" type="text" value="{$vo.gy_name}" readonly></td>-->
-                <!--<td><input  class="form-control gy_num" type="text" value="{$vo.gy_num}" readonly></td>-->
-            <!--</tr>-->
-            <!--{/foreach}-->
-        <!--</table>-->
-    <!--</div>-->
+    <div class="form-group">
+        <table class="table table-striped table-bordered table-hover table-nowrap" id="gy">
+            <tr>
+                <th class="col-xs-12 col-sm-1">原材料</th>
+                <th class="col-xs-12 col-sm-1">百分比</th>
+                <th class="col-xs-12 col-sm-1">应加量(KG)</th>
+                <th class="col-xs-12 col-sm-6">操作记录</th>
+                <th class="col-xs-12 col-sm-1">工序号</th>
+            </tr>
+            {foreach name='row["gyinfo"]' id='vo'}
+            <tr>
+                <td><input  class="form-control material" type="text" value="{$vo.material}" readonly></td>
+                <td><input  class="form-control percentage" type="text" value="{$vo.percentage}" readonly></td>
+                <td><input  class="form-control num" type="text" data-id="{$vo.id}" id="input-{$vo.id}" value="" readonly></td>
+                <td><input  class="form-control gy_name" type="text" value="{$vo.gy_name}" readonly></td>
+                <td><input  class="form-control gy_num" type="text" value="{$vo.gy_num}" readonly></td>
+            </tr>
+            {/foreach}
+        </table>
+    </div>
     <!--===========================================================-->
     <!--<div style="padding: 1000px 0 0 0">-->
-    <div style="">
+    <div style="display: none">
         <div class="form-group" id="print_area" >
             <div id="head">
                 <table class="tg" style="width: 800px;">