input('id/d',0), 'title' => input('title','','trim'), 'org_id' => input('orgId/d',0), 'company_id' => input('companyId/d',0), 'remark' => input('remark','','trim'), 'sorts' => input('sorts/d',0) ]; $result = validate('BudgetItems')->check($data,[],''); if(true !== $result){ HelpHander::error(validate('BudgetItems')->getError()); } $id = $data['id']; unset($data['id']); if($id > 0){ $data['update_time'] = date('Y-m-d H:i:s'); $ret = $this->allowField(true)->save($data,['id'=>$id]); }else{ $data['create_time'] = date('Y-m-d H:i:s'); $ret = $this->allowField(true)->save($data); } if(!$ret){ HelpHander::error('操作失败'); } return true; } public function info($id){ $info = $this->where('id',$id)->find(); if(!$info){ HelpHander::error('数据不存在'); } return $info->toArray(); } public function lists($page,$size,$companyId,$title,$orgId){ $map[] = ['del','=',0]; $map[] = ['org_id','=',$orgId]; if($companyId > 0){ $map[] = ['company_id','=',$companyId]; } if($title){ $map[] = ['title','like','%'.$title.'%']; } $lists = $this ->where($map) ->page($page,$size) // ->order('id desc') ->order('sorts asc,id asc') ->select(); $lists = $lists?$lists->toArray():[]; foreach ($lists as $k=>$v){ $lists[$k]['companyName'] = Db::name('company')->where('id',$v['company_id'])->value('title'); } $total = $this->where($map)->count(); $data = [ 'total' => $total, 'list' => $lists ]; return $data; } public function newlists($companyId,$orgId){ $curTime = date('Y-m-d'); $budget = Db::name('budget') ->where('del',0) ->where('enable',1) ->where('org_id',$orgId) ->where('start_time','<=',$curTime) ->where('end_time','>=',$curTime) ->order('id desc') ->find(); $data = []; if($budget){ $map[] = ['budget_id','=',$budget['id']]; if($companyId > 0){ $map[] = ['company_id','=',$companyId]; } $lists = $this ->where($map) ->order('sorts asc,id asc') ->select(); $data = [ 'title' => $budget['title'], 'items' => $lists?$lists->toArray():[] ]; } return $data; } public function newbudget($orgId,$userId){ $deps = model('Dep')->getUserDep($userId,$orgId); if(empty($deps)){ HelpHander::error('当前时间无法提交预算申请'); } $depids = []; foreach ($deps as $k=>$v){ $depids[] = $v['id']; } $curTime = date('Y-m-d'); // $budget = Db::name('budget_auth') // ->alias('bd') // ->join('budget b','b.id = bd.budget_id') // ->where('b.del',0) // ->where('b.enable',1) // ->where('b.org_id',$orgId) // ->where('b.start_time','<=',$curTime) // ->where('b.end_time','>=',$curTime) // ->where('bd.dep_id','in',$depids) // ->field('b.*') // ->order('b.id desc') // ->find(); $budget = Db::name('budget') ->where('del',0) ->where('enable',1) ->where('org_id',$orgId) ->where('start_time','<=',$curTime) ->where('end_time','>=',$curTime) ->order('id desc') ->find(); if(!$budget){ HelpHander::error('当前时间无法提交预算申请'); } // 检查是否有权限提交 $baret = Db::name('budget_auth') ->where('type',2) ->where('dep_id',$userId) ->where('budget_id',$budget['id']) ->find(); $badret = Db::name('budget_auth') ->where('type',0) ->where('dep_id','in',$depids) ->where('budget_id',$budget['id']) ->find(); if(!$baret && !$badret){ HelpHander::error('当前时间无权限提交预算申请'); } $items = $budget['items']?explode(',',$budget['items']):[]; $company = Db::name('company')->where('org_id',$orgId)->field('id,title')->select(); foreach ($company as $k=>$v){ $lists = Db::name('budget_items') ->where('id','in',$items) ->where('company_id',$v['id']) ->where('org_id',$orgId) // ->order('id desc') ->order('sorts asc,id asc') ->select(); $lists = $lists?$lists:[]; foreach ($lists as $kk=>$vv){ $lists[$kk]['budget_id'] = $budget['id']; } $company[$k]['items'] = $lists?$lists:[]; } $data = [ 'title' => $budget['title'], 'year' => $budget['year'], 'id' => $budget['id'], 'company' => $company?$company:[] ]; return $data; } public function del($id){ $info = Db::name('budget_dep')->where('del',0)->where('items_id',$id)->find(); if($info){ HelpHander::error('已被使用无法删除'); } $ret = Db::name('budget_items')->delete($id); if(!$ret){ HelpHander::error('删除失败'); } return true; } public function curLists($orgId){ $curTime = date('Y-m-d H:i:s'); $lists = Db::name('budget_items') ->alias('bi') ->join('budget b','b.id = bi.budget_id') ->where('b.del',0) ->where('b.start_time','elt',$curTime) ->where('b.end_time','egt',$curTime) ->where('b.org_id',$orgId) ->field('bi.id,bi.title,b.year') ->order('bi.sorts asc,bi.id asc') ->select(); $lists = $lists?$lists:[]; foreach ($lists as $k=>$v){ $lists[$k]['title'] = '['.$v['year'].']'.$v['title']; unset($lists[$k]['year']); } return $lists; } public function all($orgId){ $company = Db::name('company')->field('id,title')->select(); foreach ($company as $k=>$v){ $items = Db::name('budget_items') ->where('company_id',$v['id']) ->where('org_id',$orgId) ->where('del',0) ->field('id,title') ->order('sorts asc,id asc') ->select(); $company[$k]['items'] = $items?$items:[]; } return $company; } }