/** * Get the upcoming leave for company (getter) */ public function getLeaveUpcomingDatesAttribute() { $string = ''; $leave = CompanyLeave::where('to', '>=', Carbon::today()->toDateTimeString())->where('company_id', $this->id)->orderBy('from')->get(); foreach ($leave as $l) { if ($l->from == $l->to) { $string .= $l->from->format('d/m') . ', '; } else { $string .= $l->from->format('d/m') . ' - ' . $l->to->format('d/m') . ', '; } } return rtrim($string, ', '); }
private function getCompanyLeave() { $company_list = Auth::user()->company->companyList()->pluck('id')->toArray(); $leave_records = CompanyLeave::where('to', '>=', Carbon::today()->toDateTimeString())->whereIn('company_id', $company_list)->orderBy('from')->get(); $company_leave = []; foreach ($leave_records as $leave) { $company = Company::find($leave->company_id); $array = ['summary' => $company->leave_upcoming_dates]; // Loop through leave 'from' -> 'to' skipping weekends and add each date to array $current_date = $leave->from; $notes = "on leave"; if ($leave->notes) { $notes = $leave->notes; } while ($current_date->lte($leave->to)) { //echo $leave->id . " E:" . $leave->company_id . ' D:' . $current_date->format('Y-m-d') . '<br>'; if (array_key_exists($leave->company_id, $company_leave)) { // if not in array then add otherwise increment number of occurances if (!array_key_exists($current_date->format('Y-m-d'), $company_leave[$leave->company_id])) { $company_leave[$leave->company_id][$current_date->format('Y-m-d')] = $notes; } } else { $array[$current_date->format('Y-m-d')] = $notes; $company_leave[$leave->company_id] = $array; } $current_date->addDay(); } } return $company_leave; }
/** * Get Clients current user is authorised to manage + Process datatables ajax request. */ public function getCompanyLeave(Request $request) { $sign = '<'; if ($request->get('status')) { $sign = '>='; } $companies = []; if (Auth::user()->can('edit.company')) { $company_list = Auth::user()->company->companyList()->pluck('id')->toArray(); } $leave_records = CompanyLeave::select(['company_leave.id', 'company_leave.notes', 'company_leave.from', DB::raw('DATE_FORMAT(company_leave.from, "%d/%m/%y") AS datefrom'), DB::raw('DATE_FORMAT(company_leave.to, "%d/%m/%y") AS dateto'), 'companys.name', 'companys.slug'])->join('companys', 'company_leave.company_id', '=', 'companys.id')->where('company_leave.to', $sign, Carbon::today()->toDateTimeString())->whereIn('company_leave.company_id', $company_list)->orderBy('company_leave.from'); $dt = Datatables::of($leave_records)->editColumn('id', '<div class="text-center"><a href="/company/leave/{{$id}}/edit"><i class="fa fa-search"></i></a></div>')->addColumn('action', function ($leave) { if ($leave->from > Carbon::today()->toDateTimeString()) { //return '<button class="btn dark btn-xs" data-record-id="' . $leave->id . '"data-record-title="' . $leave->name . '" //data-toggle="modal" data-target="#confirm-delete"><i class="fa fa-trash-o"></i></button>'; return '<button class="btn dark btn-xs sbold uppercase margin-bottom btn-delete " data-remote="/company/leave/' . $leave->id . '" data-name="' . $leave->name . '"><i class="fa fa-trash"></i></button>'; } else { return ''; } })->make(true); return $dt; }
/** * Migrate Company Leave */ public function companyleave() { echo "<h1>Migrating Company Leave</h1>"; $companyleave = zCompanyLeave::all(); $companyleave->each(function ($leave) { echo "<b>" . $leave->cid . "</b> ({$leave->start_date})<br>"; $lookup = DB::table('z_lookup_company')->where('old', $leave->cid)->get(); $user = User::where('username', $leave->updated_by)->first(); $newLeave = CompanyLeave::create(array('company_id' => $lookup[0]->new, 'from' => $leave->start_date, 'to' => $leave->end_date, 'notes' => reformatOldStr($leave->notes), 'created_by' => $user->id, 'updated_by' => $user->id, 'created_at' => $leave->updated_at, 'updated_at' => $leave->updated_at)); }); echo "<h1>Completed</h1>"; }