123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- <?php
- namespace app\common\model;
- use app\hander\HelpHander;
- use think\Db;
- use think\Exception;
- use think\Model;
- class AssetCheck extends Model
- {
- public function add(){
- $data = [
- 'user_id' => input('userId/d',0),
- 'org_id' => input('orgId/d',0),
- 'remark' => input('remark','','trim'),
- 'create_time' => date('Y-m-d H:i:s'),
- 'status' => 0,
- 'items' => input('items','','trim'),
- ];
- $result = validate('AssetCheck')->check($data,[],'');
- if(true !== $result){
- HelpHander::error(validate('AssetCheck')->getError());
- }
- $items = $data['items'];
- unset($data['items']);
- $items = explode(',',$items);
- $assets = Db::name('asset_items')
- ->where('id','in',$items)
- ->where('del',0)
- ->where('enable','in','1,2')
- ->field('id as items_id,user_id,dep_id,nums,sn,title,spec,price,unit_id')
- ->select();
- $assets = $assets?$assets:[];
- if(count($assets) != count($items)){
- HelpHander::error('有部分资产状态不可盘库');
- }
- Db::startTrans();
- try{
- $checkId = Db::name('asset_check')->insertGetId($data);
- if(!$checkId){
- \exception('操作失败');
- }
- $arr = [];
- foreach ($assets as $k=>$v){
- $v['check_id'] = $checkId;
- $arr[] = $v;
- if(count($arr) == 300){
- $res = Db::name('asset_check_items')->insertAll($arr);
- if(count($arr) != $res){
- \exception('操作失败');
- }
- }
- }
- if($arr){
- $res = Db::name('asset_check_items')->insertAll($arr);
- if(count($arr) != $res){
- \exception('操作失败');
- }
- }
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- HelpHander::error($e->getMessage());
- }
- return true;
- }
- public function info($id){
- $info = Db::name('asset_check')->where('id',$id)->where('del',0)->find();
- if(!$info){
- HelpHander::error('数据不存在');
- }
- $info['userName'] = Db::name('user_info')->where('user_id',$info['user_id'])->value('name');
- $info['totalNums'] = Db::name('asset_check_items')->where('check_id',$id)->where('del',0)->sum('nums');
- $info['totalCheckNums'] = Db::name('asset_check_items')->where('check_id',$id)->where('del',0)->sum('check_nums');
- $info['totalDiffNums'] = $info['totalCheckNums'] - $info['totalNums'];
- return $info;
- }
- public function lists($page,$size,$status,$orgId){
- $map[] = ['org_id','=',$orgId];
- $map[] = ['del','=',0];
- if($status >= 0){
- $map[] = ['status','=',$status];
- }
- $lists = $this
- ->where($map)
- ->page($page,$size)
- ->order('id desc')
- ->select();
- $lists = $lists?$lists->toArray():[];
- foreach ($lists as $k=>$v){
- $lists[$k]['userName'] = Db::name('user_info')->where('user_id',$v['user_id'])->value('name');
- }
- $total = $this->where($map)->count();
- $data = [
- 'total' => $total,
- 'list' => $lists
- ];
- return $data;
- }
- public function del($id){
- $ret = $this->where('id',$id)->setField('del',1);
- if(!$ret){
- HelpHander::error('删除失败');
- }
- return true;
- }
- public function finish($id){
- $info = Db::name('asset_check')->where('id',$id)->where('del',0)->find();
- if(!$info){
- HelpHander::error('记录不存在');
- }
- if($info['status'] == 1){
- HelpHander::error('已完成无需重复操作');
- }
- $ret = Db::name('asset_check')->where('id',$id)->update(['status' => 1,'finish_time'=>date('Y-m-d H:i:s')]);
- if(!$ret){
- HelpHander::error('操作失败');
- }
- return true;
- }
- public function items($page,$size,$checkId,$status,$title){
- $map[] = ['check_id','=',$checkId];
- $map[] = ['del','=',0];
- if($title){
- $map[] = ['sn|title','like','%'.$title.'%'];
- }
- if($status >= 0){
- $map[] = ['status','=',$status];
- }
- $lists = Db::name('asset_check_items')
- ->where($map)
- ->page($page,$size)
- ->order('id asc')
- ->select();
- foreach ($lists as $k=>$v){
- $lists[$k]['depName'] = '';
- $lists[$k]['userName'] = '';
- if($v['dep_id']){
- $lists[$k]['depName'] = Db::name('dep')->where('id',$v['dep_id'])->value('name');
- $lists[$k]['userName'] = Db::name('user_info')->where('user_id',$v['dep_id'])->value('name');
- }
- $lists[$k]['diffNums'] = $v['check_nums'] - $v['nums'];
- }
- $total = Db::name('asset_check_items')->where($map)->count();
- $data = [
- 'total' => $total,
- 'list' => $lists?$lists:[]
- ];
- return $data;
- }
- public function allItems($checkId){
- $map[] = ['check_id','=',$checkId];
- $map[] = ['del','=',0];
- $lists = Db::name('asset_check_items')
- ->where($map)
- ->order('id asc')
- ->select();
- foreach ($lists as $k=>$v){
- $lists[$k]['depName'] = '';
- $lists[$k]['userName'] = '';
- if($v['dep_id']){
- $lists[$k]['depName'] = Db::name('dep')->where('id',$v['dep_id'])->value('name');
- $lists[$k]['userName'] = Db::name('user_info')->where('user_id',$v['dep_id'])->value('name');
- }
- $lists[$k]['diffNums'] = $v['check_nums'] - $v['nums'];
- }
- $check = $this->info($checkId);
- $data = [
- 'check' => $check,
- 'list' => $lists?$lists:[]
- ];
- return $data;
- }
- public function delItems($id){
- $info = Db::name('asset_check_items')->where('id',$id)->where('del',0)->find();
- if(!$info){
- HelpHander::error('记录不存在');
- }
- $check = $this->info($info['check_id']);
- if($check['status'] == 1){
- HelpHander::error('盘点已完成,无法删除');
- }
- $ret = Db::name('asset_check_items')->where('id',$id)->setField('del',1);
- if(!$ret){
- HelpHander::error('操作失败');
- }
- return true;
- }
- public function finishItems($id){
- $info = Db::name('asset_check_items')->where('id',$id)->where('del',0)->find();
- if(!$info){
- HelpHander::error('记录不存在');
- }
- if($info['status'] != 0){
- HelpHander::error('已盘点');
- }
- $check = $this->info($info['check_id']);
- if($check['status'] == 1){
- HelpHander::error('盘点已完成,无法操作');
- }
- $ret = Db::name('asset_check_items')->where('id',$id)->update([
- 'status' => 1,
- 'check_nums' => 1,
- 'check_time' => date('Y-m-d H:i:s'),
- 'remark' => '手动盘点'
- ]);
- if(!$ret){
- HelpHander::error('操作失败');
- }
- return true;
- }
- }
|