post(); $data['org_id'] = cur_org_id(); $data['user_id'] = is_login(); $data['create_time'] = date('Y-m-d H:i:s'); $data['status'] = 0; $result = validate('AssetCheck')->check($data,[],''); if(true !== $result){ $this->error = validate('AssetCheck')->getError(); return false; } $itemsIds = []; foreach ($data['items'] as $k=>$v){ $itemsIds[] = $v['id']; } $items = $itemsIds; unset($data['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)){ $this->error = '有部分资产状态不可盘库'; return false; } 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(); $this->error = $e->getMessage(); return false; } return true; } public function info($id){ $info = Db::name('asset_check')->where('id',$id)->where('del',0)->find(); if(!$info){ $this->error ='数据不存在'; return false; } $info['userName'] = Db::name('user')->where('id',$info['user_id'])->value('real_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 finish($id){ $info = Db::name('asset_check')->where('id',$id)->where('del',0)->find(); if(!$info){ $this->error ='记录不存在'; return false; } if($info['status'] == 1){ $this->error ='已完成无需重复操作'; return false; } $ret = Db::name('asset_check')->where('id',$id)->update(['status' => 1,'finish_time'=>date('Y-m-d H:i:s')]); if(!$ret){ $this->error ='操作失败'; return false; } return true; } 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('title'); $lists[$k]['userName'] = Db::name('user')->where('user_id',$v['dep_id'])->value('real_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){ $this->error = '记录不存在'; return false; } $check = $this->info($info['check_id']); if($check['status'] == 1){ $this->error = '盘点已完成,无法删除'; return false; } $ret = Db::name('asset_check_items')->where('id',$id)->setField('del',1); if(!$ret){ $this->error = '操作失败'; return false; } return true; } public function finishItems($id){ $info = Db::name('asset_check_items')->where('id',$id)->where('del',0)->find(); if(!$info){ $this->error = '记录不存在'; return false; } if($info['status'] != 0){ $this->error = '已盘点'; return false; } $check = $this->info($info['check_id']); if($check['status'] == 1){ $this->error = '盘点已完成,无法操作'; return false; } $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){ $this->error = '操作失败'; return false; } return true; } }