Exemple #1
0
 /** 
  * observe Lumen event saved
  * 1. check if prev day
  * 2. save into queue
  * 3. act, accept or refuse
  * 
  * @param $model
  * @return bool
  */
 public function saved($model)
 {
     //1. check if prev day
     $date_on = $model->on->startOfDay();
     $date_today = Carbon::now()->startOfDay();
     if ($date_today->diffInDays($date_on) >= 1 && $model->person()->count()) {
         //2. save into queue
         $on = $date_on->format('Y-m-d');
         $idle_rule = new Policy();
         $idle_rule_1 = $idle_rule->organisationid($model->person->organisation_id)->type('firstidle')->OnDate($on)->orderBy('started_at', 'desc')->first();
         $idle_rule_2 = $idle_rule->organisationid($model->person->organisation_id)->type('secondidle')->OnDate($on)->orderBy('started_at', 'desc')->first();
         $idle_rule_3 = $idle_rule->organisationid($model->person->organisation_id)->type('thirdidle')->OnDate($on)->orderBy('started_at', 'desc')->first();
         $margin_bottom_idle = 900;
         $idle_1 = 3600;
         $idle_2 = 7200;
         if ($idle_rule_1) {
             $margin_bottom_idle = (int) $idle_rule_1->value;
         }
         if ($idle_rule_2) {
             $idle_1 = (int) $idle_rule_2->value;
         }
         if ($idle_rule_3) {
             $idle_2 = (int) $idle_rule_3->value;
         }
         $employee = new Employee();
         $employee->workend = $date_on->format('Y-m-d H:i:s');
         $employee = count($employee->organisationid($model->person->organisation_id)->get(['id']));
         $parameter['margin_bottom_idle'] = $margin_bottom_idle;
         $parameter['idle_1'] = $idle_1;
         $parameter['idle_2'] = $idle_2;
         $parameter['on'] = $on;
         $parameter['organisation_id'] = $model->person->organisation_id;
         $check = Queue::where('parameter', json_encode($parameter))->where('process_name', 'hr:logobserver')->where('process_number', '0')->first();
         if (!$check) {
             $queue = new Queue();
             $queue->fill(['process_name' => 'hr:logobserver', 'parameter' => json_encode($parameter), 'total_process' => $employee, 'task_per_process' => 1, 'process_number' => 0, 'total_task' => $employee, 'message' => 'Initial Commit']);
             if (!$queue->save()) {
                 $model['errors'] = $queue->getError();
                 return false;
             } else {
                 return true;
             }
         }
     }
     return true;
 }
 /** 
  * 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;
 }