| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477 |
- <?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();
- }
- }
|