public function run() { $folderpath = 'database/seeds/seed_files'; $folders = File::directories($folderpath); $latest = '11232015'; foreach ($folders as $value) { $_dir = explode("/", $value); $cnt = count($_dir); $name = $_dir[$cnt - 1]; $latest_date = DateTime::createFromFormat('mdY', $latest); $now = DateTime::createFromFormat('mdY', $name); if ($now > $latest_date) { $latest = $name; } } $file_path = $folderpath . "/" . $latest . "/OSA Target.xlsx"; echo (string) $file_path, "\n"; Model::unguard(); DB::statement('SET FOREIGN_KEY_CHECKS=0;'); DB::table('osa_lookups')->truncate(); DB::table('osa_lookup_targets')->truncate(); $reader = ReaderFactory::create(Type::XLSX); // for XLSX files $filePath = $file_path; $reader->open($filePath); // Accessing the sheet name when reading foreach ($reader->getSheetIterator() as $sheet) { if ($sheet->getName() == 'Sheet1') { $cnt = 0; foreach ($sheet->getRowIterator() as $row) { if (!is_null($row[0])) { if ($cnt > 0) { // dd($row); $customer_id = 0; $customer = Customer::where('customer_code', $row[0])->first(); if (!empty($customer)) { $customer_id = $customer->id; } $region_id = 0; $region = Region::where('region_code', $row[1])->first(); if (!empty($region)) { $region_id = $region->id; } $distributor_id = 0; $distributor = Distributor::where('distributor_code', $row[2])->first(); if (!empty($distributor)) { $distributor_id = $distributor->id; } $store_id = 0; $store = Store::where('store_code', $row[3])->first(); if (!empty($store)) { $store_id = $store->id; } $template_id = 0; $template = AuditTemplate::where('template_code', $row[4])->first(); if (!empty($template)) { $template_id = $template->id; } $category = FormCategory::where('category', $row[5])->first(); if (!empty($category)) { $osalookup_id = 0; $osalookup = OsaLookup::where('customer_id', $customer_id)->where('region_id', $region_id)->where('distributor_id', $distributor_id)->where('store_id', $store_id)->where('template_id', $template_id)->first(); if (empty($osalookup)) { $osalookup = new OsaLookup(); $osalookup->customer_id = $customer_id; $osalookup->region_id = $region_id; $osalookup->distributor_id = $distributor_id; $osalookup->store_id = $store_id; $osalookup->template_id = $template_id; $osalookup->save(); } $osalookup_id = $osalookup->id; OsaLookupTarget::create(array('osa_lookup_id' => $osalookup_id, 'category_id' => $category->id, 'target' => $row[8], 'total' => $row[9])); } } $cnt++; } } } else { } } $reader->close(); DB::statement('SET FOREIGN_KEY_CHECKS=1;'); Model::reguard(); }
/** * Show the form for editing the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function edit($id) { $lookup = OsaLookup::with('categories')->findOrFail($id); $customers = Customer::getLists(); $regions = Region::getLists(); $distributors = Distributor::getLists(); $templates = AuditTemplate::getLists(); $categories = FormCategory::osaTagging(); $stores = Store::getLists(); return view('osalookup.edit', compact('lookup', 'sostags', 'customers', 'regions', 'distributors', 'templates', 'categories', 'stores')); }
public function run() { $start_date = ''; $end_date = ''; $folderpath = 'database/seeds/seed_files'; $folders = File::directories($folderpath); $latest = '11232015'; foreach ($folders as $value) { $_dir = explode("/", $value); $cnt = count($_dir); $name = $_dir[$cnt - 1]; $latest_date = DateTime::createFromFormat('mdY', $latest); $now = DateTime::createFromFormat('mdY', $name); if ($now > $latest_date) { $latest = $name; } } $file_path = $folderpath . "/" . $latest . "/Store Mapping.xlsx"; echo (string) $file_path, "\n"; // dd($file_path); Model::unguard(); DB::statement('SET FOREIGN_KEY_CHECKS=0;'); DB::table('audit_templates')->truncate(); Excel::selectSheets('Sheet1')->load($file_path, function ($reader) { $records = $reader->get(); $records->each(function ($row) { if (!is_null($row->template)) { $template = AuditTemplate::where('template', $row->template)->first(); if (count($template) == 0) { $newtemplate = new AuditTemplate(); $newtemplate->template_code = $row->channel_code; $newtemplate->template = $row->template; $newtemplate->save(); } } }); }); DB::table('grade_matrixs')->truncate(); Excel::selectSheets('Sheet1')->load($file_path, function ($reader) { $records = $reader->get(); $records->each(function ($row) { if (!is_null($row->enrollment_type)) { $matrix = GradeMatrix::where('desc', $row->enrollment_type)->first(); if (count($matrix) == 0) { $newmatrix = new GradeMatrix(); $newmatrix->desc = $row->enrollment_type; $newmatrix->save(); } } }); }); DB::table('users')->truncate(); DB::table('role_user')->truncate(); Excel::selectSheets('Sheet1')->load($file_path, function ($reader) { $records = $reader->get(); $records->each(function ($row) { if (!is_null($row->fullname)) { $userlist = explode("/", $row->fullname); $emaillist = explode("/", $row->email); // dd($row); for ($i = 0; $i < count($userlist); $i++) { $user = User::where('username', $row->username)->first(); if (count($user) == 0) { if (empty($emaillist[$i])) { $email = strtolower($row->username . "@unilever.com"); } else { $email = strtolower($emaillist[$i]); } $newuser = User::create(array('name' => strtoupper($userlist[$i]), 'email' => $email, 'username' => $row->username, 'password' => Hash::make('password'))); $newuser->roles()->attach(3); } else { // $user->name = strtoupper($row->fullname); // $user->username = $row->username; // $user->email = strtolower($row->email); // $user->update(); // if(!$user->hasRole('field')){ // $user->roles()->attach(3); // } // echo $user->hasRole('field'); } } } }); }); DB::table('accounts')->truncate(); Excel::selectSheets('Sheet1')->load($file_path, function ($reader) { $records = $reader->get(); $records->each(function ($row) { if (!is_null($row->account)) { $account = Account::where('account', $row->account)->first(); if (count($account) == 0) { $newaccount = new Account(); $newaccount->account = $row->account; $newaccount->save(); } } }); }); DB::table('customers')->truncate(); Excel::selectSheets('Sheet1')->load($file_path, function ($reader) { $records = $reader->get(); $records->each(function ($row) { if (!is_null($row->account)) { // var_dump($row); $account = Account::where('account', $row->account)->first(); if (!empty($account)) { $customer = Customer::where('account_id', $account->id)->where('customer_code', $row->customer_code)->where('customer', $row->customer)->first(); if (count($customer) == 0) { $newcustomer = new Customer(); $newcustomer->account_id = $account->id; $newcustomer->customer_code = $row->customer_code; $newcustomer->customer = $row->customer; $newcustomer->save(); } } } }); }); DB::table('areas')->truncate(); Excel::selectSheets('Sheet1')->load($file_path, function ($reader) { $records = $reader->get(); $records->each(function ($row) { if (!is_null($row->account)) { $account = Account::where('account', $row->account)->first(); if (!empty($account)) { $customer = Customer::where('account_id', $account->id)->where('customer_code', $row->customer_code)->where('customer', $row->customer)->first(); if (!empty($customer)) { $area = Area::where('customer_id', $customer->id)->where('area', $row->area)->first(); if (count($area) == 0) { $newarea = new Area(); $newarea->customer_id = $customer->id; $newarea->area = $row->area; $newarea->save(); } } } } }); }); DB::table('regions')->truncate(); Excel::selectSheets('Sheet1')->load($file_path, function ($reader) { $records = $reader->get(); $records->each(function ($row) { if (!is_null($row->account)) { $region = Region::where('region_code', $row->region_code)->where('region', $row->region)->first(); if (count($region) == 0) { $newregion = new Region(); $newregion->region_code = $row->region_code; $newregion->region = $row->region; $newregion->save(); } } }); }); DB::table('distributors')->truncate(); Excel::selectSheets('Sheet1')->load($file_path, function ($reader) { $records = $reader->get(); $records->each(function ($row) { if (!is_null($row->account)) { $dis = Distributor::where('distributor_code', $row->distributor_code)->where('distributor', $row->distributor)->first(); if (count($dis) == 0) { $newdis = new Distributor(); $newdis->distributor_code = $row->distributor_code; $newdis->distributor = strtoupper($row->distributor); $newdis->save(); } } }); }); DB::table('stores')->truncate(); DB::table('store_user')->truncate(); Excel::selectSheets('Sheet1')->load($file_path, function ($reader) { $records = $reader->get(); $records->each(function ($row) { if (!is_null($row->account)) { $account = Account::where('account', $row->account)->first(); if (!empty($account)) { $customer = Customer::where('account_id', $account->id)->where('customer_code', $row->customer_code)->where('customer', $row->customer)->first(); if (!empty($customer)) { $region = Region::where('region_code', $row->region_code)->first(); $dis = Distributor::where('distributor_code', $row->distributor_code)->first(); $store = Store::where('account_id', $account->id)->where('customer_id', $customer->id)->where('region_id', $region->id)->where('distributor_id', $dis->id)->where('store_code', $row->store_code)->where('store', $row->store_name)->first(); if (count($store) == 0) { $template = AuditTemplate::where('template', $row->template)->first(); $matrix = GradeMatrix::where('desc', $row->enrollment_type)->first(); $newstore = new Store(); $newstore->account_id = $account->id; $newstore->customer_id = $customer->id; $newstore->region_id = $region->id; $newstore->distributor_id = $dis->id; $newstore->store_code = $row->store_code; $newstore->store = $row->store_name; $newstore->grade_matrix_id = $matrix->id; $newstore->audit_template_id = $template->id; $newstore->save(); $emaillist = explode("/", $row->email); for ($i = 0; $i < count($emaillist); $i++) { if (empty($emaillist[$i])) { $email = strtolower($row->username . "@unilever.com"); } else { $email = strtolower($emaillist[$i]); } $user = User::where('email', $email)->first(); $newstore->users()->attach($user->id); } } else { $emaillist = explode("/", $row->email); for ($i = 0; $i < count($emaillist); $i++) { if (empty($emaillist[$i])) { $email = strtolower($row->username . "@unilever.com"); } else { $email = strtolower($emaillist[$i]); } $user = User::where('email', $email)->first(); $store->users()->attach($user->id); } } } } } }); }); DB::statement('SET FOREIGN_KEY_CHECKS=1;'); Model::reguard(); }
public function storeform(Request $request, $id) { // dd($request->all()); $this->validate($request, ['category' => 'required', 'group' => 'required', 'prompt' => 'required', 'formtype' => 'required']); \DB::beginTransaction(); try { $template = AuditTemplate::find($id); $form_type = FormType::find($request->formtype); $prompt = $request->prompt; foreach ($request->category as $cat_id) { $category = FormCategory::find($cat_id); $cat_order = 1; $a_cat_id = 0; $clast_cnt = AuditTemplateCategory::getLastOrder($template->id); if (empty($clast_cnt)) { $a_cat = AuditTemplateCategory::create(['category_order' => $cat_order, 'audit_template_id' => $template->id, 'category_id' => $category->id]); $a_cat_id = $a_cat->id; } else { $cat = AuditTemplateCategory::categoryExist($template->id, $category->id); if (empty($cat)) { $cat_order = $clast_cnt->category_order + 1; $a_cat = AuditTemplateCategory::create(['category_order' => $cat_order, 'audit_template_id' => $template->id, 'category_id' => $category->id]); $a_cat_id = $a_cat->id; } else { $a_cat_id = $cat->id; } } foreach ($request->group as $grp_id) { $group = FormGroup::find($grp_id); $grp_order = 1; $a_grp_id = 0; $glast_cnt = AuditTemplateGroup::getLastOrder($a_cat_id); if (empty($glast_cnt)) { $a_grp = AuditTemplateGroup::create(['group_order' => $grp_order, 'audit_template_category_id' => $a_cat_id, 'form_group_id' => $group->id]); $a_grp_id = $a_grp->id; } else { $grp = AuditTemplateGroup::categoryExist($a_cat_id, $group->id); if (empty($grp)) { $grp_order = $glast_cnt->group_order + 1; $a_grp = AuditTemplateGroup::create(['group_order' => $grp_order, 'audit_template_category_id' => $a_cat_id, 'form_group_id' => $group->id]); $a_grp_id = $a_grp->id; } else { $a_grp_id = $grp->id; } } $form = Form::create(array('audit_template_id' => $template->id, 'form_type_id' => $form_type->id, 'prompt' => strtoupper($prompt), 'required' => $request->required == '1' ? 1 : 0, 'expected_answer' => $request->expected_answer == '1' ? 1 : 0, 'exempt' => $request->exempt == '1' ? 1 : 0)); if ($request->formtype == 9) { $multiData = array(); foreach ($request->multiselect as $option) { $multiData[] = array('form_id' => $form->id, 'multi_select_id' => $option); } if (count($multiData) > 0) { FormMultiSelect::insert($multiData); } } if ($request->formtype == 10) { $singleData = array(); foreach ($request->singleselect as $option) { $singleData[] = array('form_id' => $form->id, 'single_select_id' => $option); } if (count($singleData) > 0) { FormSingleSelect::insert($singleData); } } if ($request->formtype == 11) { if ($request->has('formula')) { $text = $request->formula; preg_match_all('/:(.*?):/', $text, $matches); $index = array(); foreach ($matches[1] as $value) { $split_up = explode('_', $value); $last_item = $split_up[count($split_up) - 1]; $index[] = $last_item; } $formula1 = $text; foreach ($matches[1] as $key => $a) { $formula1 = str_replace(':' . $a . ':', $index[$key], $formula1); } $formformula = new FormFormula(); $formformula->form_id = $form->id; $formformula->formula = $formula1; $formformula->formula_desc = $request->formula; $formformula->save(); } } if ($request->formtype == 12) { if ($request->has('condition')) { } } $order = 1; $a_frm_id = 0; $last_cnt = AuditTemplateForm::getLastOrder($a_grp_id); if (!empty($last_cnt)) { $order = $last_cnt->order + 1; } AuditTemplateForm::insert(array('audit_template_group_id' => $a_grp_id, 'order' => $order, 'audit_template_id' => $template->id, 'form_id' => $form->id)); } } \DB::commit(); Session::flash('flash_message', 'Template successfully added!'); return redirect()->route("audittemplate.form", $id); } catch (Exception $e) { DB::rollBack(); Session::flash('flash_message', 'An error occured in adding form!'); return redirect()->back(); } }
/** * Show the form for editing the specified resource. * * @param int $id * @return Response */ public function edit($id) { $store = Store::findOrFail($id); $distributors = Distributor::getLists(); $audittemplates = AuditTemplate::getLists(); $passings = GradeMatrix::getLists(); $categories = FormCategory::sosTagging(); $sostags = SosTagging::all(); $storesos = StoreSosTag::where('store_id', $store->id)->get(); return view('store.edit', compact('store', 'distributors', 'audittemplates', 'passings', 'categories', 'sostags', 'storesos')); }
public function run() { Model::unguard(); DB::statement('SET FOREIGN_KEY_CHECKS=0;'); DB::table('form_categories')->truncate(); DB::table('form_groups')->truncate(); DB::table('forms')->truncate(); DB::table('single_selects')->truncate(); DB::table('form_single_selects')->truncate(); DB::table('multi_selects')->truncate(); DB::table('form_multi_selects')->truncate(); DB::table('form_formulas')->truncate(); DB::table('form_conditions')->truncate(); DB::table('audit_template_forms')->truncate(); DB::table('audit_template_groups')->truncate(); DB::table('audit_template_categories')->truncate(); // \DB::beginTransaction(); // try { $folderpath = 'database/seeds/seed_files'; $folders = File::directories($folderpath); $latest = '11232015'; foreach ($folders as $value) { $_dir = explode("/", $value); $cnt = count($_dir); $name = $_dir[$cnt - 1]; $latest_date = DateTime::createFromFormat('mdY', $latest); $now = DateTime::createFromFormat('mdY', $name); if ($now > $latest_date) { $latest = $name; } } // $this->filename = $folderpath.$latest.'/mtprimarysales.csv'; $files = File::allFiles($folderpath . "/" . $latest . "/templates"); foreach ($files as $file) { echo (string) $file, "\n"; $reader = ReaderFactory::create(Type::XLSX); // for XLSX files $reader->open($file); // // Accessing the sheet name when reading foreach ($reader->getSheetIterator() as $sheet) { if ($sheet->getName() == 'Sheet1') { $cnt = 0; foreach ($sheet->getRowIterator() as $row) { if ($cnt > 0) { if ($row[1] != '') { $start_date = $row[4]; $end_date = $row[5]; $template = AuditTemplate::firstOrCreate(['template' => $row[1]]); $template->start_date = $start_date; $template->end_date = $end_date; $template->update(); $category = FormCategory::firstOrCreate(['category' => $row[3]]); // dd($category); $group = FormGroup::firstOrCreate(['group_desc' => $row[6]]); $type = $row[11]; if (strtoupper($type) == 'DOUBLE') { $form_type = FormType::where('form_type', "NUMERIC")->first(); } else { $form_type = FormType::where('form_type', strtoupper($type))->first(); } $image = ""; if (!empty($row[15])) { $image = $row[15]; } if ($form_type->id == 11) { $index1 = array(); $index2 = array(); preg_match_all('/{(.*?)}/', $row[12], $matches); foreach ($matches[1] as $key => $a) { $data = DB::table('temp_forms')->where('code', $a)->first(); if (!empty($data)) { $other_form = FormRepository::insertForm($template, $a, $data->type, $data->required, $data->prompt, $data->choices, $data->expected_answer, $image, null, null, null); $index1[$a] = $other_form->id; $index2[$a] = $other_form->prompt . '_' . $other_form->id; } else { $other_form = DB::table('forms')->where('code', $a)->first(); // echo $a; if (empty($other_form)) { // dd($a); } // $other_form = FormRepository::insertForm($template,$a,$data->form_type_id,$data->required,$data->prompt,null,$data->expected_answer,null,$image); $index1[$a] = '!' . $other_form->id; $index2[$a] = $other_form->prompt . '_' . $other_form->id; } } $formula1 = $row[12]; $formula2 = $row[12]; foreach ($matches[1] as $key => $a) { $formula1 = str_replace('{' . $a . '}', $index1[$a], $formula1); $formula2 = str_replace('{' . $a . '}', ' :' . $index2[$a] . ': ', $formula2); } $form = FormRepository::insertForm($template, $row[7], $row[11], $row[10], $row[9], $formula1, null, $image, $formula2, null, null); } elseif ($form_type->id == 12) { $options = explode("~", $row[12]); preg_match_all('/(.*?){(.*?)}/', $row[12], $matches); $data_con = array(); $options = explode("~", $row[12]); foreach ($options as $option) { $with_value = preg_match('/{(.*?)}/', $option, $match); $x1 = array(); $x2 = array(); $_opt1 = ""; $_opt2 = ""; if ($with_value) { $codes = explode('^', $match[1]); if (count($codes) > 0) { foreach ($codes as $code) { if ($code != '') { $other_data = DB::table('temp_forms')->where('code', $code)->first(); if (empty($other_data)) { $other_data = DB::table('forms')->where('code', $code)->first(); } $other_form = FormRepository::insertForm($template, $code, $other_data->type, $other_data->required, $other_data->prompt, $other_data->choices, $other_data->expected_answer, null, null, null, null); $x1[] = $other_form->id; $x2[] = $other_form->prompt . '_' . $other_form->id; } } } if (count($x1) > 0) { $_opt1 = implode("^", $x1); } if (count($x2) > 0) { $_opt2 = implode("^", $x2); } } $data_con[] = ['option' => strtoupper(strtok($option, '{')), 'condition' => $_opt1, 'condition_desc' => $_opt2]; } // dd($data_con); $form = FormRepository::insertForm($template, $row[7], $row[11], $row[10], $row[9], $row[12], $row[14], $image, array(), $data_con, $row[16]); } else { $form = FormRepository::insertForm($template, $row[7], $row[11], $row[10], $row[9], $row[12], $row[14], $image, null, null, $row[16]); } $cat_order = 1; $a_cat_id = 0; $clast_cnt = AuditTemplateCategory::getLastOrder($template->id); if (empty($clast_cnt)) { $a_cat = AuditTemplateCategory::create(['category_order' => $cat_order, 'audit_template_id' => $template->id, 'category_id' => $category->id]); $a_cat_id = $a_cat->id; } else { $cat = AuditTemplateCategory::categoryExist($template->id, $category->id); if (empty($cat)) { $cat_order = $clast_cnt->category_order + 1; $a_cat = AuditTemplateCategory::create(['category_order' => $cat_order, 'audit_template_id' => $template->id, 'category_id' => $category->id]); $a_cat_id = $a_cat->id; } else { $a_cat_id = $cat->id; } } $grp_order = 1; $a_grp_id = 0; $glast_cnt = AuditTemplateGroup::getLastOrder($a_cat_id); if (empty($glast_cnt)) { $a_grp = AuditTemplateGroup::create(['group_order' => $grp_order, 'audit_template_category_id' => $a_cat_id, 'form_group_id' => $group->id]); $a_grp_id = $a_grp->id; } else { $grp = AuditTemplateGroup::categoryExist($a_cat_id, $group->id); if (empty($grp)) { $grp_order = $glast_cnt->group_order + 1; $a_grp = AuditTemplateGroup::create(['group_order' => $grp_order, 'audit_template_category_id' => $a_cat_id, 'form_group_id' => $group->id]); $a_grp_id = $a_grp->id; } else { $a_grp_id = $grp->id; } } $order = 1; $a_frm_id = 0; $last_cnt = AuditTemplateForm::getLastOrder($a_grp_id); if (!empty($last_cnt)) { $order = $last_cnt->order + 1; } AuditTemplateForm::create(array('audit_template_group_id' => $a_grp_id, 'order' => $order, 'audit_template_id' => $template->id, 'form_id' => $form->id)); } } $cnt++; } } // }elseif($sheet->getName() == 'Others') { // // $cnt = 0; // // foreach ($sheet->getRowIterator() as $row) { // // if($cnt > 0){ // // if(!is_null($row[2])){ // // $prompt = addslashes($row[1]); // // DB::statement('INSERT INTO temp_forms (code, prompt, required, type, choices) VALUES ("'.$row[0].'","'.$prompt.'","'.$row[2].'","'.$row[3].'","'.$row[4].'");'); // // } // // } // // $cnt++; // // } // }else{ // } } $reader->close(); } $users = User::all(); foreach ($users as $user) { $mapping = UserMapping::where('user_name', $user->name)->where('start_date', $start_date)->where('end_date', $end_date)->first(); if (!empty($mapping)) { $mapping->mapped_stores = $user->stores->count(); $mapping->update(); } else { UserMapping::create(['user_name' => $user->name, 'start_date' => $start_date, 'end_date' => $end_date, 'mapped_stores' => $user->stores->count()]); } } DB::statement('SET FOREIGN_KEY_CHECKS=1;'); Model::reguard(); }