Example #1
0
 /** 
  * observe Employee event saving
  * 1. check need rehash
  * 2. unique username
  * 3. act, accept or refuse
  * 
  * @param $model
  * @return bool
  */
 public function saving($model)
 {
     $errors = new MessageBag();
     //1. check need rehash
     if (Hash::needsRehash($model->password)) {
         $model->password = bcrypt($model->password);
     }
     if (is_null($model->id)) {
         $id = 0;
     } else {
         $id = $model->id;
     }
     //2. unique username
     if (!is_null($model->username)) {
         $other_employee = Employee::username($model->uniqid)->notid($id)->first();
         if ($other_employee) {
             $errors->add('Employee', 'Username sudah terdaftar');
         }
         if ($errors->count()) {
             $model['errors'] = $errors;
             return false;
         }
     }
     return true;
 }
 /**
  * set pasword
  *
  * 1. get activation link
  * 2. validate activation
  * @param activation link
  * @return array of employee
  */
 public function setPassword($activation_link)
 {
     if (!Input::has('activation')) {
         return new JSend('error', (array) Input::all(), 'Tidak ada data activation.');
     }
     $errors = new MessageBag();
     DB::beginTransaction();
     //1. Validate activation Parameter
     $activation = Input::get('activation');
     //1. get activation link
     $employee = Employee::activationlink($activation_link)->first();
     if (!$employee) {
         $errors->add('Activation', 'Invalid activation link');
     }
     //2. validate activation
     $rules = ['password' => 'required|min:8|confirmed'];
     $validator = Validator::make($activation, $rules);
     if ($validator->passes()) {
         $employee->password = $activation['password'];
         $employee->activation_link = '';
         if (!$employee->save()) {
             $errors->add('Activation', $employee->getError());
         }
     } else {
         $errors->add('Activation', $validator->errors());
     }
     if ($errors->count()) {
         DB::rollback();
         return new JSend('error', (array) Input::all(), $errors);
     }
     DB::commit();
     return new JSend('success', ['employee' => $employee->toArray()]);
 }
 /**
  * auto generate username
  *
  * 1. check existance
  * 2. get firstname
  * @param code and employee name
  * @return $username
  */
 public function generateUsername($code, $id = 0)
 {
     //1. check existance
     $uname = Employee::id($id)->first();
     if ($uname && !empty($uname['username'])) {
         return new JSend('success', ['username' => $uname['username']]);
     }
     //2. get firstname
     if (!Input::has('name')) {
         return new JSend('error', (array) Input::all(), 'No Name');
     }
     $name = Input::get('name');
     $username = UsernameGenerator::generate($code, $id, $name);
     return new JSend('success', ['username' => $username]);
 }
Example #4
0
 /**
  * validate input parameter (need to parse) based on policy code
  *
  * @param array of policy (contain code)
  * @return boolean
  */
 public static function generate($code, $id, $name)
 {
     $original = explode(' ', strtolower($name));
     $modify = $original[0];
     $countog = count($original) - 1;
     foreach ($original as $keyx => $valuex) {
         if (is_array($valuex) || $valuex != '') {
             $countog = $keyx;
         }
     }
     $idxuname = 0;
     do {
         $uname = Employee::username($modify . '.' . $code)->first();
         if ($uname) {
             if (isset($original[$countog])) {
                 $modify = $modify . $original[$countog][$idxuname];
             } else {
                 $modify = $modify . $modify;
             }
             $idxuname++;
         }
     } while ($uname);
     return $modify . '.' . $code;
 }
 /**
  * Delete an Schdule
  *
  * @return Response
  */
 public function delete($org_id = null, $emp_id = null, $id = null)
 {
     //check branch
     $schedule = \App\ThunderID\EmploymentSystemV1\Models\Employee::id($emp_id)->organisationid($org_id)->first();
     if (!$schedule) {
         \App::abort(404);
     }
     $schedule = \App\ThunderID\WorkforceManagementV1\Models\PersonSchedule::personid($emp_id)->id($id)->first();
     if (!$schedule) {
         return new JSend('error', (array) Input::all(), 'Schdule tidak ditemukan.');
     }
     $result = $schedule->toArray();
     if ($schedule->delete()) {
         return new JSend('success', (array) $result);
     }
     return new JSend('error', (array) $result, $schedule->getError());
 }
 /**
  * post import document
  *
  * 1. check organisation
  * 2. generate username
  * 3. save profile
  * @return $array of document
  */
 public function post()
 {
     set_time_limit(0);
     if (!Input::has('employee')) {
         return new JSend('error', (array) Input::all(), 'Tidak ada data employee.');
     }
     $data = Input::get('employee');
     $globalnotify = [];
     foreach ($data as $col => $row) {
         $errors = new MessageBag();
         $organisation = Organisation::code($row['work']['kode_perusahaan'])->first();
         if ($organisation) {
             DB::beginTransaction();
             $send_mail = false;
             //2. generate username
             $username = UsernameGenerator::generate($row['work']['kode_perusahaan'], 0, $row['profile']['name']);
             $row['profile']['username'] = $username;
             //3. save profile
             $person = new Employee();
             $row['profile']['date_of_birth'] = Carbon::parse($row['profile']['date_of_birth'])->format('Y-m-d H:i:s');
             $person->fill($row['profile']);
             if (!$person->save()) {
                 $errors->add('profile', $person->getError());
             }
             //4. save contact
             if (!$errors->count()) {
                 if (!empty($row['email'])) {
                     $contact = new Contact();
                     $contact->fill(['contactable_id' => $person['id'], 'contactable_type' => get_class($person), 'type' => 'email', 'value' => $row['email'], 'is_default' => true]);
                     if (!$contact->save()) {
                         $errors->add('profile', $contact->getError());
                     }
                     $send_mail = true;
                 }
                 if (!empty($row['phone'])) {
                     $contact = new Contact();
                     $contact->fill(['contactable_id' => $person['id'], 'contactable_type' => get_class($person), 'type' => 'phone', 'value' => $row['phone'], 'is_default' => true]);
                     if (!$contact->save()) {
                         $errors->add('profile', $contact->getError());
                     }
                 }
                 if (!empty($row['address'])) {
                     $contact = new Contact();
                     $contact->fill(['contactable_id' => $person['id'], 'contactable_type' => get_class($person), 'type' => 'address', 'value' => $row['address'], 'is_default' => true]);
                     if (!$contact->save()) {
                         $errors->add('profile', $contact->getError());
                     }
                 }
             }
             //5. save marital status
             if (!$errors->count()) {
                 if (!empty($row['marital_status'])) {
                     $marital = new MaritalStatus();
                     $marital->fill(['person_id' => $person['id'], 'status' => $row['marital_status'], 'ondate' => Carbon::now()->format('Y-m-d H:i:s')]);
                     if (!$marital->save()) {
                         $errors->add('profile', $marital->getError());
                     }
                 }
             }
             //6. save work
             if (!$errors->count()) {
                 //6a. check branch
                 $branch = Branch::organisationid($organisation['id'])->name($row['work']['kantor'])->first();
                 if (!$branch) {
                     $branch = new Branch();
                     $branch->fill(['name' => $row['work']['kantor'], 'organisation_id' => $organisation['id']]);
                     if (!$branch->save()) {
                         $errors->add('work', $branch->getError());
                     }
                 }
                 //6b. check chart
                 if (!$errors->count()) {
                     $chart = Chart::branchid($branch['id'])->name($row['work']['jabatan'])->first();
                     if (!$chart) {
                         $chart = new Chart();
                         $chart->fill(['name' => $row['work']['jabatan'], 'branch_id' => $branch['id']]);
                         if (!$chart->save()) {
                             $errors->add('work', $chart->getError());
                         }
                     }
                 }
                 //6c. save work
                 if (!$errors->count()) {
                     $join_year = Carbon::parse($row['work']['tanggal_mulai_status'])->format('y');
                     $nik = NIKGenerator::generate($organisation['code'], $person['id'], $join_year);
                     $row['work']['chart_id'] = $chart['id'];
                     $row['work']['person_id'] = $person['id'];
                     $row['work']['nik'] = $nik;
                     $row['work']['status'] = $row['work']['status_kerja'];
                     $row['work']['start'] = Carbon::parse($row['work']['tanggal_mulai_status'])->format('Y-m-d H:i:s');
                     if (strtotime($row['work']['tanggal_akhir_status'])) {
                         $row['work']['end'] = Carbon::parse($row['work']['tanggal_akhir_status'])->format('Y-m-d H:i:s');
                     }
                     $row['work']['reason_end_job'] = $row['work']['alasan_akhir_status'];
                     $work = new Work();
                     $work->fill($row['work']);
                     if (!$work->save()) {
                         $errors->add('work', $work->getError());
                     }
                 }
             }
             //7. save relative
             foreach ($row['relatives'] as $key => $value) {
                 if (!$errors->count() && empty($value)) {
                     $relative = new Person();
                     $value['date_of_birth'] = Carbon::parse($value['date_of_birth'])->format('Y-m-d H:i:s');
                     $relative->fill($value);
                     if (!$relative->save()) {
                         $errors->add('relatives', $relative->getError());
                     } else {
                         $relation = new Relative();
                         $relation->fill(['relative_id' => $relative['id'], 'person_id' => $person['id'], 'relationship' => $value['relationship']]);
                         if (!$relation->save()) {
                             $errors->add('relative', $relation->getError());
                         }
                     }
                 }
             }
             //8. save document
             foreach ($row['document'] as $key => $value) {
                 if (!$errors->count()) {
                     $document = new PersonDocument();
                     $doc['person_id'] = $person['id'];
                     $doc['documents'] = array_merge(['code' => $key], ['document' => $value]);
                     $validating_document = new VOD();
                     if (!$validating_document->validate($doc['documents'])) {
                         $errors->add('PersonDocument', $validating_document->getError());
                     } else {
                         $doc['documents'] = json_encode($doc['documents']);
                         $document = $document->fill($doc);
                         if (!$document->save()) {
                             $errors->add('PersonDocument', $document->getError());
                         }
                     }
                 }
             }
             if (!$errors->count()) {
                 DB::commit();
                 $globalnotify[$col] = json_encode(['status' => 'sukses', 'data' => $col, 'message' => ['Data Karyawan Tersimpan']]);
                 if ($send_mail) {
                     Event::fire(new EmployeeCreated($final_employee));
                 }
             } else {
                 DB::rollback();
                 $globalnotify[$col] = json_encode(['status' => 'error', 'data' => $col, 'message' => $errors]);
             }
         } else {
             $globalnotify[$col] = json_encode(['status' => 'error', 'data' => $col, 'message' => ['Kode Perusahaan tidak valid']]);
         }
     }
     return new JSend('success', $globalnotify);
 }
Example #7
0
 /**
  * boot
  *
  */
 public static function boot()
 {
     parent::boot();
     Employee::observe(new EmployeeObserver());
 }
 /**
  * Delete an employee
  *
  * @return Response
  */
 public function delete($org_id = null, $id = null)
 {
     //
     $employee = \App\ThunderID\EmploymentSystemV1\Models\Employee::id($id)->organisationid($org_id)->currentgrade(true)->currentmaritalstatus(true)->with(['persondocuments', 'maritalstatuses', 'relatives', 'relatives.relative', 'contacts', 'works', 'works.contractworks', 'works.contractworks.contractelement'])->first();
     if (!$employee) {
         return new JSend('error', (array) Input::all(), 'Karyawan tidak ditemukan.');
     }
     $result = $employee->toArray();
     if ($employee->delete()) {
         return new JSend('success', (array) $result);
     }
     return new JSend('error', (array) $result, $employee->getError());
 }