其中的save2为入库核心逻辑,save3为出库核心逻辑
~~~
<?php
namespace app\admin\controller;
use app\common\model\Stock as StockModel;
use app\common\controller\AdminBase;
use think\Db;
use think\Session;
use think\Request;
class Stock extends AdminBase
{
protected $stock_model;
protected function _initialize()
{
parent::_initialize();
$this->stock_model = new StockModel();
$stock_list = $this->stock_model->order(['in_time' => 'DESC', 'id' => 'desc'])->paginate(9,false,['query' => request()->param()]);
$this->assign('stock_list', $stock_list);
}
//明细列表
public function index($is_out= 2)
{
if($is_out==0||$is_out==1){
$stock_list = $this->stock_model->where('is_out',$is_out)->order(['in_time' => 'DESC', 'id' => 'desc'])->paginate(9,false,['query' => request()->param()]);
$this->assign('stock_list', $stock_list);
}
return $this->fetch();
}
//出入库1-1
public function instock()
{
/* $alist= db('parts')->where('number',$number)->select();
$this->assign('alist',$alist);*/
return $this->fetch();
}
//出入库1-2
public function outstock()
{
/* $alist= db('parts')->where('number',$number)->select();
$this->assign('alist',$alist);*/
return $this->fetch();
}
//出入库更新1-1-入库
public function save2()
{
$status=0;
$message="操作失败";
$data = $this->request->param();
$arr=array($data['parts_id'],$data['number'],$data['price'],$data['remarks']);//二维数组
$num=count($arr[0]);//插入数量
for($i=0;$i<$num;){//二维数组长度
$arr2 = array_column($arr,$i);//获取二维数组一列并组成新的一维数组
$map=array();//新建初始一维数组
foreach ($arr2 as $k =>$v){//改变索引数组改为键值数组
switch($k){
case 0:
$k='parts_id';
break;
case 1:
$k='number';
break;
case 2:
$k='price';
break;
case 3:
$k='remarks';
break;
}
$map[$k]=$v;
}
$map['in_time']=time();//map为新组合的一维数组
$map['is_out']=0;
$arr3[$i]=$map;//新组合的二维数组
$i=$i+1;
}
$this->stock_model->allowField(true)->saveAll($arr3);//批量插入
$status = 1;
$message = "入库成功";
return ['status'=>$status, 'message'=>$message];
}
//出入库更新1-2-出库
public function save3()
{
$status=0;
$message="操作失败";
$data = $this->request->param();
$arr=array($data['parts_id'],$data['number'],$data['price'],$data['remarks']);//二维数组
$num=count($arr[0]);//插入数量
for($i=0;$i<$num;){//
$arr2 = array_column($arr,$i);//获取二维数组一列并组成新的一维数组
$map=array();//新建初始一维数组
foreach ($arr2 as $k =>$v){//改变索引数组改为键值数组
switch($k){
case 0:
$k='parts_id';
break;
case 1:
$k='number';
break;
case 2:
$k='price';
break;
case 3:
$k='remarks';
break;
}
$map[$k]=$v;
}
$map['in_time']=time();//map为新组合的一维数组
$map['is_out']=1;
$parts_id=$map['parts_id'];
$number=$map['number'];
//1.判断是否有库存,2.判断库存数量是否大于出库数量
$res = getStockNum($parts_id);
if($res > 0){
if($res>=$number){
$iserror=0;
}else{
$iserror=1;
}
}else{
$iserror=1;
}
//放在判断库存的结果
$arr4[$i]=$iserror;
$arr3[$i]=$map;//新组合的二维数组
$i=$i+1;
}
if(in_array(1,$arr4)){//检查数组中某个值是否存在
$status = 0;
$message = "出库失败,选中配件没有库存或者库存量不足,请先入库";
}else{
$this->stock_model->allowField(true)->saveAll($arr3);//批量插入
$status = 1;
$message = "出库成功";
}
return ['status'=>$status, 'message'=>$message];
}
//更新1-1
public function edit($id)
{
$data=db('stock')->find($id);
$this->assign('data',$data);
return $this->fetch();
}
//更新1-2
public function update()
{
$data = $this->request->param();
if($this->stock_model->allowField(true)->save($data,['id'=>$data['id']])){
$this->success('更新成功');
}else{
$this->success('更新失败');
}
}
//删除
public function del($id = 0, $ids = [])
{
$id = $ids ? $ids : $id;
if ($id) {
if ($this->stock_model->destroy($id)) {
$this->success('删除成功');
} else {
$this->error('删除失败');
}
} else {
$this->error('请选择需要删除的信息');
}
}
}
~~~
- 心灵笔记
- tp5在编辑器一行一个添加信息
- 出库入库处理
- 出库
- 入库
- 后台控制器
- tp5加载更多-流加载
- 配件列表
- Parts.php
- add.html
- edit.html
- 图片管理
- controller
- 1.imgfile.php
- 2.imgfiletag.php
- view
- imgfile
- 1.index.html
- 2.show.html
- 3.simple.html
- 4.more.html
- 5.edit.html
- 6.deittag.html
- imgfiletag
- add.htm
- edit.htm
- model
- imgfile.php
- imgfiletag.php
- api
- upload.php
- js
- admin.js
- tp5整合百度编辑器多图上传
- 1.html代码
- 2.js代码
- 3.api代码
- 4.extend中的拓展代码
- tp5删除图片同时删除服务器图片
- 实用字段的的添加更新
- 根据时间戳获取未来几天星期几
- curl
- 城市按字母排版
- 聚合查询及渲染,处理产品首页
- 过滤字段
- 风控
- 查询id是否在数组里
- 判断更新还是新增操作
- tp5使用引入php文件