/** * boot * observing model * */ public static function boot() { parent::boot(); GivenWorkleave::observe(new GivenWorkleaveObserver()); }
/** * observe AttendanceLog event saved * 1. check workleave * 2. check unpaid workleave * 3. check changed workleave * 4. act, accept or refuse * * @param $model * @return bool */ public function saved($model) { $errors = new MessageBag(); if ($model->processlog()->count()) { $on = $model->processlog->format('Y-m-d'); //1. check workleave if (in_array(strtoupper($model->modified_status), ['CN', 'CB'])) { //check if pw on that day were provided $taken = TakenWorkleave::personid($model->processlog->person_id)->ondate([$on, $on])->status(strtoupper($model->modified_status))->first(); if (!$taken) { //count left over quota $given = GivenWorkleave::personid($model->processlog->person_id)->ondate([$on, null])->status('CN')->get(); $takenw = 0; $givenw = 0; $checkp = -1; foreach ($given as $key => $value) { $taking = TakenWorkleave::personid($model->processlog->person_id)->ondate([$value->start->format('Y-m-d'), $value->end->format('Y-m-d')])->status('CN')->sum('amount'); $takenw = $takenw + $taking; $givenw = $givenw + $value['amount']; if ($takenw + $givenw > 0) { $checkp = $key; } } $person = new Employee(); $person->workend = $on; $person = $person->id($model->processlog->person_id)->first(); if ($person && $checkp != -1) { $pworkleave = new TakenWorkleave(); $pworkleave->fill(['work_id' => $person->current_work_id, 'person_id' => $person->id, 'person_workleave_id' => $given[$checkp]->id, 'created_by' => $model->modified_by, 'name' => 'Pengambilan ' . $pwP->name, 'status' => $model->modified_status, 'notes' => isset($model->notes) ? $model->notes : '', 'start' => $on, 'end' => $on, 'quota' => -1]); if (!$pworkleave->save()) { $errors->add('Workleave', $pworkleave->getError()); } } else { $prev_status = AttendanceLog::processlogid($model->process_log_id)->orderby('created_at', 'desc')->first(); if (!$prev_status || strtoupper($prev_status->modified_status) != 'UL') { $alog = new AttendanceLog(); $alog->fill(['process_log_id' => $model->process_log_id, 'margin_start' => $model->margin_start, 'margin_end' => $model->margin_end, 'count_status' => $model->count_status, 'actual_status' => $model->actual_status, 'modified_status' => 'UL', 'modified_at' => $model->modified_at, 'notes' => 'Auto generated dari attendance log, karena tidak ada cuti.']); if (!$alog->save()) { $errors->add('Workleave', $alog->getError()); } } } } } elseif (strtoupper($model->actual_status) == 'AS' && in_array(strtoupper($model->modified_status), ['UL'])) { //check if pw on that day were provided $taken = TakenWorkleave::personid($model->processlog->person_id)->ondate([$on, $on])->status(strtoupper($model->modified_status))->first(); if (!$taken) { //count left over quota $given = GivenWorkleave::personid($model->processlog->person_id)->ondate([$on, null])->status('CN')->get(); $takenw = 0; $givenw = 0; $checkp = -1; foreach ($given as $key => $value) { $taking = TakenWorkleave::personid($model->processlog->person_id)->ondate([$value->start->format('Y-m-d'), $value->end->format('Y-m-d')])->status('CN')->sum('amount'); $takenw = $takenw + $taking; $givenw = $givenw + $value['amount']; if ($takenw + $givenw > 0) { $checkp = $key; } } $person = new Employee(); $person->workend = $on; $person = $person->id($model->processlog->person_id)->first(); if ($person && $checkp != -1) { $prev_status = AttendanceLog::processlogid($model->process_log_id)->orderby('created_at', 'desc')->first(); if (!$prev_status || strtoupper($prev_status->modified_status) != 'CN') { $alog = new AttendanceLog(); $alog->fill(['process_log_id' => $model->process_log_id, 'margin_start' => $model->margin_start, 'margin_end' => $model->margin_end, 'count_status' => $model->count_status, 'actual_status' => $model->actual_status, 'modified_status' => 'CN', 'modified_at' => $model->modified_at, 'notes' => 'Auto generated dari attendance log, karena masih ada cuti.']); if (!$alog->save()) { $errors->add('Workleave', $alog->getError()); } } } } } //3. check changed workleave $current_status = AttendanceLog::processlogid($model->process_log_id)->orderBy('created_at', 'desc')->first(); if ($current_status && in_array($current_status->modified_status, ['CN', 'CB']) && !in_array($model->modified_status, ['CN', 'CB'])) { $taken = TakenWorkleave::personid($model->processlog->person_id)->ondate([$on, $on])->status(strtoupper($model->modified_status))->first(); if ($taken && !$taken->delete()) { $errors->add('Workleave', $taken->getError()); } } } if ($errors->count()) { $model['errors'] = $errors; return false; } return true; }