public function create(Request $request)
 {
     $newBusinessPlan = new BusinessPlan();
     $newBusinessPlan->start = date($request['sYear'] . '-01-01 00:00:00');
     $newBusinessPlan->end = date($request['eYear'] . '-12-31 00:00:00');
     $newBusinessPlan->save();
     foreach (array_keys($request['data']) as $idx) {
         $key = array_keys($request['data'][$idx])[0];
         $newGoal = new Goat();
         $newGoal->type = 'G';
         $newGoal->parent_id = null;
         $newGoal->description = $key;
         $newGoal->priority = 0;
         $newGoal->complete = false;
         $newGoal->goal_type = 'B';
         $newGoal->due_date = null;
         $newGoal->budget = 0;
         $newGoal->bid = $newBusinessPlan->id;
         $newGoal->save();
         foreach ($request['data'][$idx][$key] as $obj) {
             $newObj = new Goat();
             $newObj->type = 'O';
             $newObj->parent_id = $newGoal->id;
             $newObj->description = $obj;
             $newObj->priority = 0;
             $newObj->complete = false;
             $newObj->goal_type = 'B';
             $newObj->due_date = null;
             $newObj->budget = 0;
             $newObj->bid = $newBusinessPlan->id;
             $newObj->save();
         }
     }
     foreach (Department::lists('name') as $dept_name) {
         $newGoal = new Goat();
         $newGoal->type = 'G';
         $newGoal->parent_id = null;
         $newGoal->description = $dept_name . " Goals";
         $newGoal->priority = 0;
         $newGoal->complete = false;
         $newGoal->goal_type = 'D';
         $newGoal->due_date = null;
         $newGoal->budget = 0;
         $newGoal->bid = $newBusinessPlan->id;
         $newGoal->save();
     }
     return response()->json([], 200);
 }
 public function index()
 {
     $businessPlans = BusinessPlan::all();
     $goats = Goat::all();
     $users = User::all();
     return view('manage_plan', compact('businessPlans', 'goats', 'users', 'user'));
 }
 public function login()
 {
     $bp = BusinessPlan::orderBy('created', 'desc')->first()->id;
     $link = "businessplan/";
     $link .= strval($bp);
     if (Auth::guest()) {
         return view('auth/login');
     } else {
         return redirect($link);
     }
 }
 public function show()
 {
     $bp = BusinessPlan::all();
     $idbp = count($bp);
     $bpPlans = DB::select("select * from (select  null as userId, null as progress, null as date, null as successMeasured, null as budget, null as collaborators, goals.group, goals. id, null as description, goals.name, goals.ident from goals ,business_plans where business_plans.id = goals.bpid and business_plans.id = '" . $idbp . "' union all select null as userId, null as progress, null as date, null as successMeasured, null as budget, null as collaborators, objectives.group, objectives.id, null as description, objectives.name, objectives.ident from objectives, goals, business_plans where goals.id = objectives.goal_id and business_plans.id = goals.bpid and business_plans.id = '" . $idbp . "' union all select actions.userId, actions.progress, actions.date, actions.successMeasured, actions.budget, actions.collaborators, actions.group, actions.id, actions.description, null as name, actions.ident from actions, objectives, goals, business_plans where objectives.id = actions.objective_id and goals.id = objectives.goal_id and business_plans.id = goals.bpid and business_plans.id = '" . $idbp . "' union all select tasks.userId, tasks.progress, tasks.date, tasks.successMeasured, tasks.budget, tasks.collaborators, tasks.group, tasks.id, tasks.description, null as name, tasks.ident from tasks, actions, objectives, goals, business_plans where actions.id = tasks.action_id and objectives.id = actions.objective_id and goals.id = objectives.goal_id and business_plans.id = goals.bpid and business_plans.id = '" . $idbp . "')a order by ident");
     $users = User::all();
     $groups = Group::all();
     $filters = null;
     $nameBP = $bp[$idbp - 1]->name;
     //$users[$bp->userId - 1]->name
     $html = View::make('businessPlan.printBP', compact('users', 'groups', 'bpPlans', 'idbp', 'filters', 'nameBP'))->render();
     $pdf = App::make('dompdf.wrapper');
     $pdf->loadHTML($html);
     return $pdf->stream();
 }
 public function groups()
 {
     $bp = BusinessPlan::all();
     $bpid = count($bp);
     $groups = Group::all();
     $tasks = array();
     $actions = array();
     foreach (BusinessPlan::all() as $x) {
         array_push($tasks, DB::select("select t.* from tasks t, actions a, objectives o, goals g, business_plans b where t.action_id = a.id and a.objective_id = o.id and o.goal_id = g.id and g.bpid = b.id and b.id = {$x->id}"));
         array_push($actions, DB::select("select a.* from actions a, objectives o, goals g, business_plans b where a.objective_id = o.id and o.goal_id = g.id and g.bpid = b.id and b.id = {$x->id}"));
     }
     $users = User::all();
     $rosters = Roster::all();
     $businessPlans = BusinessPlan::lists('name', 'id');
     return view('groups', compact('groups', 'actions', 'tasks', 'users', 'rosters', 'businessPlans', 'bpid'));
 }
 /**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */
 public function store(Request $request)
 {
     Department::create($request->input());
     foreach (BusinessPlan::lists('id') as $bid) {
         $newGoal = new Goat();
         $newGoal->type = 'G';
         $newGoal->parent_id = null;
         $newGoal->description = $request['name'] . " Goals";
         $newGoal->priority = 0;
         $newGoal->complete = false;
         $newGoal->goal_type = 'D';
         $newGoal->due_date = null;
         $newGoal->budget = 0;
         $newGoal->bid = $bid;
         $newGoal->save();
     }
     return redirect('admin/depts');
 }
 public function index(Request $request)
 {
     if ($request->bp) {
         $currentBp = BusinessPlan::find($request->bp);
     } else {
         $currentBp = BusinessPlan::where('start', '<=', Carbon::now())->where('end', '>=', Carbon::now())->first();
         if (!$currentBp) {
             $currentBp = BusinessPlan::all()->last();
         }
     }
     $sorted = Goat::where('bid', $currentBp->id)->where('type', 'G')->orderBy('goal_type')->orderBy('description')->get();
     $bp = Goat::where('bid', $currentBp->id)->where('type', '<>', 'G')->orderByRaw("FIELD(type, 'O', 'A', 'T')")->orderBy('description', 'desc')->get();
     foreach ($bp as $goat) {
         if ($goat->type === 'G') {
             $sorted->push($goat);
             continue;
         }
         for ($i = 0, $len = $sorted->count(); $i < $len; $i++) {
             if ($sorted[$i]->id == $goat->parent_id) {
                 // Hacky fix since when you splice $goat into $sorted,
                 // it converts the $goat into an array instead of
                 // keeping it as a Model object...
                 $sorted->splice($i + 1, 0, "temp");
                 $sorted->put($i + 1, $goat);
                 break;
             }
         }
     }
     $leadOf = array();
     if (Auth::user()) {
         foreach (Auth::user()->leadOf as $dept) {
             array_push($leadOf, $dept->id);
         }
     }
     $collaboratorGoals = array();
     foreach ($leadOf as $dept_id) {
         foreach (Department::find($dept_id)->collaboratorOn as $goat) {
             array_push($collaboratorGoals, $goat->id);
         }
     }
     return view('view_plan')->with(['bp' => $sorted, 'users' => User::orderBy('first_name')->get(), 'depts' => Department::orderBy('name')->get(), 'leadOf' => $leadOf, 'plans' => BusinessPlan::orderBy('id', 'desc')->get(), 'query' => $request, 'bp_id' => $currentBp->id, 'is_bplead' => Auth::user() && Auth::user()->is_bplead, 'collaboratorGoals' => $collaboratorGoals]);
 }
 public function update($idbp, $idb, $id)
 {
     $input = Request::all();
     if (Request::has('bp')) {
         $bp = BusinessPlan::all();
         $bp->update($input);
         return redirect("businessplan/" . $idbp . "");
     }
     if (Request::has('goal')) {
         $goal = Goal::findOrFail($id);
         $goal->update($input);
     }
     if (Request::has('objective')) {
         $objective = Objective::findOrFail($id);
         $input['group'] += 1;
         $objective->update($input);
     }
     if (Request::has('action')) {
         $collabs = "";
         if (array_key_exists('collaborators-groups', $input)) {
             foreach ($input['collaborators-groups'] as $x) {
                 $group = Group::find($x);
                 if ($group != null) {
                     $collabs .= Group::find($x)->name;
                     $collabs .= ", ";
                 } else {
                     $collabs .= "";
                 }
             }
         }
         if (array_key_exists('collaborators-users', $input)) {
             foreach ($input['collaborators-users'] as $x) {
                 $user = User::find($x);
                 if ($user != null) {
                     $collabs .= User::find($x)->name;
                     $collabs .= ", ";
                 } else {
                     $collabs .= "";
                 }
             }
         }
         $collabs = rtrim($collabs, ", ");
         $input['collaborators'] = $collabs;
         //return  $input['collaborators'];
         $action = Action::findOrFail($id);
         $input['group'] += 1;
         $input['userId'] += 1;
         $action->update($input);
     }
     if (Request::has('task')) {
         $collabs = "";
         if (array_key_exists('collaborators-groups', $input)) {
             foreach ($input['collaborators-groups'] as $x) {
                 $group = Group::find($x);
                 if ($group != null) {
                     $collabs .= Group::find($x)->name;
                     $collabs .= ", ";
                 } else {
                     $collabs .= "";
                 }
             }
         }
         if (array_key_exists('collaborators-users', $input)) {
             foreach ($input['collaborators-users'] as $x) {
                 $user = User::find($x);
                 if ($user != null) {
                     $collabs .= User::find($x)->name;
                     $collabs .= ", ";
                 } else {
                     $collabs .= "";
                 }
             }
         }
         $collabs = rtrim($collabs, ", ");
         $input['collaborators'] = $collabs;
         $task = Task::findOrFail($id);
         $input['group'] += 1;
         $input['userId'] += 1;
         $task->update($input);
     }
     return redirect("businessplan/" . $idbp . "");
 }