/** * 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; }
/** * Display products critical stock * * @param skip, take * @return Response */ public function critical() { $setting = \App\Models\Policy::ondate('now')->type('critical_stock')->first(); if (!$setting) { $critical = 0; } else { $critical = 0 - $setting['value']; } $result = \App\Models\Varian::critical($critical); $count = count($result->get(['id'])); if (Input::has('skip')) { $skip = Input::get('skip'); $result = $result->skip($skip); } if (Input::has('take')) { $take = Input::get('take'); $result = $result->take($take); } $result = $result->with(['product'])->get()->toArray(); return new JSend('success', (array) ['count' => $count, 'data' => $result]); }
/** * Store a setting * * @return Response */ public function store() { if (!Input::has('setting')) { return new JSend('error', (array) Input::all(), 'Tidak ada data setting.'); } $errors = new MessageBag(); DB::beginTransaction(); //1. Validate StoreSetting Parameter $setting = Input::get('setting'); if (is_null($setting['id'])) { $is_new = true; } else { $is_new = false; } //2. Validate setting parameter //2a. Slider if (!$errors->count() && $setting['type'] == 'slider') { $setting_data = \App\Models\Slider::findornew($setting['id']); $setting_rules = ['started_at' => 'date_format:"Y-m-d H:i:s"', 'ended_at' => 'date_format:"Y-m-d H:i:s"|after:started_at']; $validator = Validator::make($setting, $setting_rules); } elseif (!$errors->count() && in_array($setting['type'], ['about_us', 'why_join', 'term_and_condition'])) { $setting_data = \App\Models\StorePage::findornew($setting['id']); $setting_rules = ['started_at' => 'date_format:"Y-m-d H:i:s"']; $validator = Validator::make($setting, $setting_rules); } elseif (!$errors->count() && in_array($setting['type'], ['url', 'logo', 'facebook_url', 'twitter_url', 'instagram_url', 'email', 'phone', 'address', 'bank_information'])) { $setting_data = \App\Models\Store::findornew($setting['id']); $setting_rules = ['started_at' => 'date_format:"Y-m-d H:i:s"']; $validator = Validator::make($setting, $setting_rules); } else { $setting_data = \App\Models\Policy::findornew($setting['id']); $setting_rules = ['started_at' => 'date_format:"Y-m-d H:i:s"']; $validator = Validator::make($setting, $setting_rules); } if (!$validator->passes()) { $errors->add('StoreSetting', $validator->errors()); } else { //if validator passed, save setting $setting_data = $setting_data->fill($setting); if (!$setting_data->save()) { $errors->add('StoreSetting', $setting_data->getError()); } } //3. save image for slider if (!$errors->count() && isset($setting['images']) && is_array($setting['images']) && $setting_data['type'] == 'slider') { $image_current_ids = []; foreach ($setting['images'] as $key => $value) { if (!$errors->count()) { $image_data = \App\Models\Image::findornew($value['id']); $image_rules = ['thumbnail' => 'required|max:255', 'image_xs' => 'required|max:255', 'image_sm' => 'required|max:255', 'image_md' => 'required|max:255', 'image_lg' => 'required|max:255', 'is_default' => 'boolean']; $validator = Validator::make($value, $image_rules); //if there was image and validator false if (!$validator->passes()) { $errors->add('Image', $validator->errors()); } else { $value['imageable_id'] = $setting_data['id']; $value['imageable_type'] = get_class($setting_data); $image_data = $image_data->fill($value); if (!$image_data->save()) { $errors->add('Image', $image_data->getError()); } else { $image_current_ids[] = $image_data['id']; } } } //if there was no error, check if there were things need to be delete if (!$errors->count()) { $images = \App\Models\Image::imageableid($setting['id'])->get(['id'])->toArray(); $image_should_be_ids = []; foreach ($images as $key => $value) { $image_should_be_ids[] = $value['id']; } $difference_image_ids = array_diff($image_should_be_ids, $image_current_ids); if ($difference_image_ids) { foreach ($difference_image_ids as $key => $value) { $image_data = \App\Models\Image::find($value); if (!$image_data->delete()) { $errors->add('Image', $image_data->getError()); } } } } } } if ($errors->count()) { DB::rollback(); return new JSend('error', (array) Input::all(), $errors); } DB::commit(); if ($setting_data['type'] == 'slider') { $final_setting = \App\Models\Slider::id($setting_data['id'])->with(['images'])->first()->toArray(); } else { $final_setting = \App\Models\StoreSetting::id($setting_data['id'])->first()->toArray(); } return new JSend('success', (array) $final_setting); }
$serviceTypeObj = Models\ServiceType::firstOrCreate(array('name' => $serviceType)); $currencyObj = Models\Currency::firstOrCreate(array('code' => $currency)); $regionObj = Models\Region::firstOrCreate(array('name' => $region)); $supplierObj = $regionObj->suppliers()->firstOrCreate(array('name' => $supplierName, 'ts_id' => $supplierId)); if ($occupancyId) { $occupancyObj = Models\Occupancy::firstOrCreate(array('id' => $occupancyId, 'name' => $occupancyName)); } if ($mealName) { $mealObj = Models\Meal::firstOrCreate(array('name' => $mealName)); } // Find or Create Service $serviceParams = array('ts_id' => $serviceId, 'name' => $serviceName, 'region_id' => $regionObj->id, 'currency_id' => $currencyObj->id, 'service_type_id' => $serviceTypeObj->id, 'supplier_id' => $supplierObj->id, 'name' => $serviceName); $serviceObj = Models\Service::firstOrCreate($serviceParams); // Find or Create Policies $policyParams = array('ts_id' => $policyId, 'name' => $policyName); Models\Policy::firstOrCreate($policyParams); // Find or Create Contracts $contractObj = $serviceObj->contracts()->firstOrCreate(array('ts_id' => $contractId, 'name' => $contractName)); $contractPeriodParams = array('ts_id' => $contractPeriodId, 'name' => $contractPeriodName, 'start' => date("Y/m/d", strtotime($contractStart)), 'end' => date("Y/m/d", strtotime($contractEnd))); $contractPeriodObj = $contractObj->contractPeriods()->firstOrCreate($contractPeriodParams); // Find or Create Season $seasonObj = $contractPeriodObj->seasons()->firstOrCreate(array('ts_id' => $seasonId, 'name' => $seasonName)); $seasonPeriodParams = array('start' => date("Y/m/d", strtotime($seasonStart)), 'end' => date("Y/m/d", strtotime($seasonEnd))); $seasonPeriodObj = $seasonObj->seasonPeriods()->firstOrCreate($seasonPeriodParams); // Find or Create Service Extras $extraObj = null; if ($extraId) { $extraParams = array('name' => $extraName, 'ts_id' => $extraId); $extraObj = $serviceObj->serviceExtras()->firstOrCreate($extraParams); } // Find Or Create Service Option
/** * boot * observing model * */ public static function boot() { parent::boot(); Policy::observe(new PolicyObserver()); }
/** * 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; }