/** * observe Takenworkleave event saving * 1. count quotas * 2. act, accept or refuse * * @param $model * @return bool */ public function saving($model) { $errors = new MessageBag(); //1. count quotas $quota = 0; if ($model->person()->count() && $model->work()->count()) { $from = Carbon::createFromFormat('Y-m-d', $model->start); $to = Carbon::createFromFormat('Y-m-d', $modenl->end); $days = $from->diffInDays($to); $interval = new DateInterval('P1D'); $to->add($interval); $daterange = new DatePeriod($from, $interval, $to); foreach ($daterange as $date) { //1a. check person schedule $ps = \App\Models\PersonSchedule::ondate($date->format('Y-m-d'))->personid($model->person_id)->first(); if (!$ps) { //1b. check schedule $s = \App\Models\Schedule::ondate($date->format('Y-m-d'))->calendarid($model->work->calendar_id)->first(); if (!$s) { //1c. check calendar $c = \App\Models\Calendar::id($model->work->calendar_id)->first(); if ($c) { $harikerja = explode(',', $c['workdays']); $day = ['senin' => 'monday', 'selasa' => 'tuesday', 'rabu' => 'wednesday', 'kamis' => 'thursday', 'jumat' => 'friday', 'sabtu' => 'saturday', 'minggu' => 'sunday', 'monday' => 'monday', 'tuesday' => 'tuesday', 'wednesday' => 'wednesday', 'thursday' => 'thursday', 'friday' => 'friday', 'saturday' => 'saturday', 'sunday' => 'sunday']; $workdays = []; //translate to ing foreach ($harikerja as $key => $value) { $workdays[] = $day[strtolower($value)]; } if (!in_array(strtolower($date->format('l')), $workdays)) { $quota = $quota + 1; } } } elseif (in_array($s['status'], ['HB', 'DN'])) { $quota = $quota + 1; } } elseif (in_array($ps['status'], ['HB', 'DN'])) { $quota = $quota + 1; } } } $model->quota = $quota; if ($errors->count()) { $model['errors'] = $errors; return false; } return true; }
/** * Delete a schedule (throw to queue) * * @return Response */ public function delete($org_id = null, $cal_id = null, $id = null) { // $schedule = \App\Models\Schedule::id($id)->calendarid($cal_id)->with(['calendar', 'calendar.calendars'])->first(); if (!$schedule) { return new JSend('error', (array) Input::all(), 'Jadwal tidak ditemukan.'); } $errors = new MessageBag(); DB::beginTransaction(); //1. store schedule of calendar to queue $schedule = $schedule->toArray(); //1a. Validate Basic schedule Parameter $parameter = $schedule; unset($parameter['calendar']); $total = \App\Models\Work::calendarid($cal_id)->count(); $queue = new \App\Models\Queue(); $queue->fill(['process_name' => 'hr:schedules', 'process_option' => 'delete', 'parameter' => json_encode($parameter), 'total_process' => $total, 'task_per_process' => 1, 'process_number' => 0, 'total_task' => $total, 'message' => 'Initial Commit']); if (!$queue->save()) { $errors->add('Schedule', $queue->getError()); } //End of validate schedule //2. store schedule of calendars to queue if (!$errors->count() && isset($schedule['calendar']['calendars']) && is_array($schedule['calendar']['calendars'])) { foreach ($schedule['calendar']['calendars'] as $key => $value) { $cals_data = \App\Models\Calendar::id($value['id'])->calendarid($cal_id)->first(); if (!$cals_data) { $errors->add('Calendar', 'Tidak ada kalender ' . $value['name']); } if (!$errors->count()) { $total = \App\Models\Work::calendarid($value['id'])->count(); $parameter['calendar_id'] = $value['id']; $queue = new \App\Models\Queue(); $queue->fill(['process_name' => 'hr:schedules', 'process_option' => 'delete', 'parameter' => json_encode($parameter), 'total_process' => $total, 'task_per_process' => 1, 'process_number' => 0, 'total_task' => $total, 'message' => 'Initial Commit']); if (!$queue->save()) { $errors->add('Schedule', $queue->getError()); } } } } //End of validate calendar schedule if ($errors->count()) { DB::rollback(); return new JSend('error', (array) Input::all(), $errors); } DB::commit(); return new JSend('success', (array) $schedule); }
/** * observe GivenWorkleave event saving * Case progressive workleave * 1. check workleave quota * 2. check start work * 3. check start work date * 4. check if progressive workleave is end of year and quota over 12 * Case special workleave * 1. count quota * * @param $model * @return bool */ public function saving($model) { $errors = new MessageBag(); //case a. if given workleave was progressive if ($model->work()->count() && $model->status == 'CN') { //1. check workleave quota if ($model->workleave()->count()) { $wleave_quota = $follow->workleave->quota; } else { $wleave_quota = 12; } //2. check start work $prev_work = Work::personid($model->person_id)->orderby('start', 'asc')->notid($model->work_id)->first(); if ($prev_work) { $start_work = $prev_work->start; } else { $start_work = $model->work->start; } //2a. if work over a year :: //- end was end of start year + 3 months //- start could be taken now if ($start_work->diffInYears($model->start) >= 1) { $extendpolicy = \App\Models\Policy::organisationid($model->person->organisation_id)->type('extendsworkleave')->OnDate($model->start->format('Y-m-d H:i:s'))->orderby('started_at', 'desc')->first(); if ($extendpolicy) { $extends = $extendpolicy['value']; } else { $extends = '+ 3 months'; } $end = Carbon::create('end of December ' . $model->start->format('Y') . ' ' . $extends); $start = $model->start; } else { $extendpolicy = \App\Models\Policy::organisationid($model->person->organisation_id)->type('extendsmidworkleave')->OnDate($model->start->format('Y-m-d H:i:s'))->orderby('started_at', 'desc')->first(); if ($extendpolicy) { $extends = $extendpolicy['value']; } else { $extends = '+ 1 year'; } $end = Carbon::create('end of December ' . $model->start->format('Y') . ' ' . $extends); $start = $start_work->copy()->addYear(); } //3. check start work date //3a. if start work date after 15 if ($start_work->format('d') >= 15) { $next_start = $start->copy()->addMonth(); //3a1. if next start workleave bigger than end :: //-quota set zero if (!is_null($model->work->end) && $next_start->format('Y-m-d H:i:s') > $model->work->end->format('Y-m-d H:i:s')) { $quota = 0; } else { $start = $next_start; $quota = 1; } } else { $quota = 1; } //4. check if progressive workleave is end of year and quota over 12 if ((int) $start->format('m') == 12 && $wleave_quota > 12) { $quota = $quota + ($wleave_quota - 12); } $model->start = $start->format('Y-m-d H:i:s'); $model->end = $end->format('Y-m-d H:i:s'); $model->quota = $quota; } elseif ($model->work()->count() && $model->status == 'CI') { $quota = 0; $from = Carbon::createFromFormat('Y-m-d', $model->start); $to = Carbon::createFromFormat('Y-m-d', $modenl->end); $days = $from->diffInDays($to); $interval = new DateInterval('P1D'); $to->add($interval); $daterange = new DatePeriod($from, $interval, $to); foreach ($daterange as $date) { //1a. check person schedule $ps = \App\Models\PersonSchedule::ondate($date->format('Y-m-d'))->personid($model->person_id)->first(); if (!$ps) { //1b. check schedule $s = \App\Models\Schedule::ondate($date->format('Y-m-d'))->calendarid($model->work->calendar_id)->first(); if (!$s) { //1c. check calendar $c = \App\Models\Calendar::id($model->work->calendar_id)->first(); if ($c) { $harikerja = explode(',', $c['workdays']); $day = ['senin' => 'monday', 'selasa' => 'tuesday', 'rabu' => 'wednesday', 'kamis' => 'thursday', 'jumat' => 'friday', 'sabtu' => 'saturday', 'minggu' => 'sunday', 'monday' => 'monday', 'tuesday' => 'tuesday', 'wednesday' => 'wednesday', 'thursday' => 'thursday', 'friday' => 'friday', 'saturday' => 'saturday', 'sunday' => 'sunday']; $workdays = []; //translate to ing foreach ($harikerja as $key => $value) { $workdays[] = $day[strtolower($value)]; } if (!in_array(strtolower($date->format('l')), $workdays)) { $quota = $quota + 1; } } } elseif (in_array($s['status'], ['HB', 'DN'])) { $quota = $quota + 1; } } elseif (in_array($ps['status'], ['HB', 'DN'])) { $quota = $quota + 1; } } $model->quota = $quota; } if ($errors->count()) { $model['errors'] = $errors; return false; } return true; }