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(); }
/** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { $lookup = OsaLookup::findOrFail($id); $messages = ['unique_with' => 'This combination of selection already exists.']; $this->validate($request, ['customer_id' => 'required|unique_with:osa_lookups, regions = region_id, distributors = distributor_id, store_id = store_id, templates = template_id,' . $id, 'regions' => 'required', 'distributors' => 'required', 'store_id' => 'required', 'templates' => 'required'], $messages); \DB::beginTransaction(); try { $lookup->customer_id = $request->customer_id; $lookup->region_id = $request->regions; $lookup->distributor_id = $request->distributors; $lookup->store_id = $request->stores; $lookup->template_id = $request->templates; $lookup->update(); OsaLookupTarget::where('osa_lookup_id', $lookup->id)->delete(); foreach ($request->target as $category_id => $value) { if ($category_id > 0) { if (!empty($value)) { $newlookup = new OsaLookupTarget(); $newlookup->osa_lookup_id = $lookup->id; $newlookup->category_id = $category_id; $newlookup->target = $value; $newlookup->total = $request->total[$category_id]; $newlookup->save(); } } } \DB::commit(); Session::flash('flash_message', 'OSA Lookup successfully updated!'); return redirect()->route("osalookup.edit", [$id]); } catch (Exception $e) { dd($e); DB::rollBack(); return redirect()->back(); } }
public function index(Request $request) { $user = $request->id; $type = $request->type; $user = User::find($user); $storelist = $user->stores()->orderBy('store')->get(); $result = array(); foreach ($storelist as $store) { $result[] = $store->audit_template_id; } $list = array_unique($result); // get store list if ($type == "stores") { $writer = WriterFactory::create(Type::CSV); $writer->openToBrowser('stores.txt'); $writer->addRow(['id', 'store', 'grade_matrix_id', 'audit_template_id', 'template', 'start_date', 'end_date', 'store_code']); foreach ($storelist as $store) { $store_ids[] = $store->id; $data[0] = $store->id; $data[1] = $store->store; $data[2] = $store->gradematrix->passing; $data[3] = $store->audit_template_id; $data[4] = $store->audittemplate->template; $data[5] = 0; $data[6] = 0; $data[7] = 0; $data[8] = 0; $data[9] = $store->audittemplate->start_date; $data[10] = $store->audittemplate->end_date; $data[11] = $store->store_code; $data[12] = $store->account->account; $data[13] = $store->customer->customer_code; $data[14] = $store->customer->customer; $data[15] = $store->region->region_code; $data[16] = $store->region->region; $data[17] = $store->distributor->distributor_code; $data[18] = $store->distributor->distributor; $data[19] = $store->audittemplate->template_code; $writer->addRow($data); } $writer->close(); } // get template categories if ($type == "temp_categories") { $categories = AuditTemplateCategory::select('audit_template_categories.id', 'audit_template_categories.audit_template_id', 'audit_template_categories.category_id', 'form_categories.category', 'audit_template_categories.category_order', 'form_categories.perfect_store')->join('form_categories', 'form_categories.id', '=', 'audit_template_categories.category_id')->whereIn('audit_template_id', $list)->orderBy('audit_template_id')->orderBy('category_order')->get(); $writer = WriterFactory::create(Type::CSV); $writer->openToBrowser('temp_category.txt'); $writer->addRow(['id', 'audit_template_id', 'category_id', 'category', 'category_order', 'perfect_store']); foreach ($categories as $category) { $data[0] = $category->id; $data[1] = $category->audit_template_id; $data[2] = $category->category_order; $data[3] = $category->category_id; $data[4] = $category->category; $data[5] = $category->perfect_store; $writer->addRow($data); } $writer->close(); } // get template groups if ($type == "temp_groups") { $c_list = array(); $categories = AuditTemplateCategory::select('audit_template_categories.id', 'audit_template_categories.audit_template_id', 'audit_template_categories.category_id', 'form_categories.category', 'audit_template_categories.category_order')->join('form_categories', 'form_categories.id', '=', 'audit_template_categories.category_id')->whereIn('audit_template_id', $list)->orderBy('audit_template_id')->orderBy('category_order')->get(); foreach ($categories as $category) { $c_list[] = $category->id; } $groups = AuditTemplateGroup::select('audit_template_groups.id', 'audit_template_groups.form_group_id', 'form_groups.group_desc', 'audit_template_categories.audit_template_id', 'audit_template_groups.group_order', 'audit_template_groups.audit_template_category_id', 'form_groups.perfect_store')->join('audit_template_categories', 'audit_template_categories.id', '=', 'audit_template_groups.audit_template_category_id')->join('form_groups', 'form_groups.id', '=', 'audit_template_groups.form_group_id')->whereIn('audit_template_category_id', $c_list)->orderBy('audit_template_id')->orderBy('audit_template_category_id')->orderBy('group_order')->get(); $writer = WriterFactory::create(Type::CSV); $writer->openToBrowser('temp_group.txt'); $writer->addRow(['id', 'audit_template_id', 'audit_template_category_id', 'group_order', 'form_group_id', 'group_desc', 'perfect_store']); foreach ($groups as $group) { $data[0] = $group->id; $data[1] = $group->audit_template_id; $data[2] = $group->audit_template_category_id; $data[3] = $group->group_order; $data[4] = $group->form_group_id; $data[5] = $group->group_desc; $data[6] = $group->perfect_store; $writer->addRow($data); } $writer->close(); } // get template questions if ($type == "temp_questions") { // dd($list); $forms = AuditTemplateForm::select('audit_template_forms.id', 'audit_template_forms.order', 'audit_template_forms.audit_template_group_id', 'audit_template_forms.audit_template_id', 'audit_template_forms.form_id', 'forms.form_type_id', 'forms.prompt', 'forms.required', 'forms.expected_answer', 'forms.exempt', 'forms.image', 'forms.default_answer')->join('audit_template_groups', 'audit_template_groups.id', '=', 'audit_template_forms.audit_template_group_id')->join('audit_template_categories', 'audit_template_categories.id', '=', 'audit_template_groups.audit_template_category_id')->join('forms', 'forms.id', '=', 'audit_template_forms.form_id')->join('form_categories', 'form_categories.id', '=', 'audit_template_categories.category_id')->join('form_groups', 'form_groups.id', '=', 'audit_template_groups.form_group_id')->whereIn('audit_template_forms.audit_template_id', $list)->get(); // dd($forms); $writer = WriterFactory::create(Type::CSV); $writer->openToBrowser('questions.txt'); $writer->addRow(['id', 'order', 'audit_template_group_id', 'audit_template_id', 'form_id', 'form_type_id', 'prompt', 'required', 'expected_answer', 'exempt', 'image', 'default_answer']); foreach ($forms as $form) { $data[0] = $form->id; $data[1] = $form->order; $data[2] = $form->audit_template_group_id; $data[3] = $form->audit_template_id; $data[4] = $form->form_id; $data[5] = $form->form_type_id; $prompt = "null"; if (!empty($form->prompt)) { $prompt = preg_replace("/[\\n\\r]+/", " ", $form->prompt); } $data[6] = $prompt; $data[7] = $form->required; $data[8] = $form->expected_answer; $data[9] = $form->exempt; $data[10] = $form->form->image; $data[11] = $form->default_answer; // var_dump($data); $writer->addRow($data); } $writer->close(); } // get template forms if ($type == "temp_forms") { $forms = Form::whereIn('forms.audit_template_id', $list)->get(); $writer = WriterFactory::create(Type::CSV); $writer->openToBrowser('forms.txt'); $writer->addRow(['id', 'audit_template_id', 'form_type_id', 'prompt', 'required', 'expected_answer', 'exempt', 'image', 'default_answer']); foreach ($forms as $form) { $data[0] = $form->id; $data[1] = $form->audit_template_id; $data[2] = $form->form_type_id; $data[3] = preg_replace("/[\\n\\r]+/", " ", $form->prompt); $data[4] = $form->required; $data[5] = $form->expected_answer; $data[6] = $form->exempt; $data[7] = $form->image; $data[8] = $form->default_answer; $writer->addRow($data); } $writer->close(); } // get single selects if ($type == "single_selects") { $forms = Form::whereIn('forms.audit_template_id', $list)->where('form_type_id', 10)->get(); $form_ids = array(); foreach ($forms as $form) { $form_ids[] = $form->id; } $selections = FormSingleSelect::select('form_single_selects.form_id', 'single_selects.id', 'single_selects.option')->join('single_selects', 'single_selects.id', '=', 'form_single_selects.single_select_id')->whereIn('form_single_selects.form_id', $form_ids)->orderBy('form_single_selects.form_id')->get(); // dd($selections); $writer = WriterFactory::create(Type::CSV); $writer->openToBrowser('single_selects.txt'); $writer->addRow(['form_id', 'id', 'option']); foreach ($selections as $selection) { $data[0] = $selection->form_id; $data[1] = $selection->id; $data[2] = $selection->option; $writer->addRow($data); } $writer->close(); } // get multiple selects if ($type == "multi_selects") { $forms = Form::whereIn('forms.audit_template_id', $list)->where('form_type_id', 9)->get(); $form_ids = array(); foreach ($forms as $form) { $form_ids[] = $form->id; } $selections = FormMultiSelect::select('form_multi_selects.form_id', 'multi_selects.id', 'multi_selects.option')->join('multi_selects', 'multi_selects.id', '=', 'form_multi_selects.multi_select_id')->whereIn('form_multi_selects.form_id', $form_ids)->orderBy('form_multi_selects.form_id')->get(); $writer = WriterFactory::create(Type::CSV); $writer->openToBrowser('multi_selects.txt'); $writer->addRow(['form_id', 'id', 'option']); foreach ($selections as $selection) { $data[0] = $selection->form_id; $data[1] = $selection->id; $data[2] = $selection->option; $writer->addRow($data); } $writer->close(); } // get formulas if ($type == "formulas") { $forms = Form::whereIn('forms.audit_template_id', $list)->where('form_type_id', 11)->get(); $form_ids = array(); foreach ($forms as $form) { $form_ids[] = $form->id; } $formulas = FormFormula::select('form_formulas.form_id', 'formula')->get(); $writer = WriterFactory::create(Type::CSV); $writer->openToBrowser('formula.txt'); $writer->addRow(['form_id', 'formula']); foreach ($formulas as $formula) { $data[0] = $formula->form_id; $data[1] = $formula->formula; $writer->addRow($data); } $writer->close(); } // get conditions if ($type == "conditions") { $forms = Form::whereIn('forms.audit_template_id', $list)->where('form_type_id', 12)->get(); $form_ids = array(); foreach ($forms as $form) { $form_ids[] = $form->id; } $conditions = FormCondition::select('form_conditions.form_id', 'option', 'condition', 'id')->get(); $writer = WriterFactory::create(Type::CSV); $writer->openToBrowser('conditions.txt'); $writer->addRow(['form_id', 'option', 'condition']); foreach ($conditions as $condition) { $data[0] = $condition->form_id; $data[1] = $condition->option; $data[2] = $condition->condition; $data[3] = $condition->id; $writer->addRow($data); } $writer->close(); } // get secondary display lookup if ($type == "secondary_lookups") { $store_ids = array(); foreach ($storelist as $store) { $store_ids[] = $store->id; } $secondarydisplay = SecondaryDisplayLookup::select('store_id', 'category_id', 'brand')->whereIn('store_id', $store_ids)->join('secondary_displays', 'secondary_displays.id', '=', 'secondary_display_lookups.secondary_display_id')->orderBy('store_id')->orderBy('category_id')->get(); $writer = WriterFactory::create(Type::CSV); $writer->openToBrowser('secondarydisplay.txt'); $writer->addRow(['store_id', 'category_id', 'brand']); foreach ($secondarydisplay as $category) { $data[0] = $category->store_id; $data[1] = $category->category_id; $data[2] = strtoupper($category->brand); $writer->addRow($data); } $writer->close(); } // get secondary key list if ($type == "secondary_lists") { $keylist = FormGroup::where('secondary_display', 1)->get(); $writer = WriterFactory::create(Type::CSV); $writer->openToBrowser('secondary_keylist.txt'); $writer->addRow(['id']); foreach ($keylist as $list) { $data[0] = $list->id; $writer->addRow($data); } $writer->close(); } // get osa key list if ($type == "osa_lists") { $keylist = FormGroup::where('osa', 1)->get(); $writer = WriterFactory::create(Type::CSV); $writer->openToBrowser('osa_keylist.txt'); $writer->addRow(['id']); foreach ($keylist as $list) { $data[0] = $list->id; $writer->addRow($data); } $writer->close(); } if ($type == "osa_lookups") { $writer = WriterFactory::create(Type::CSV); $writer->openToBrowser('osa_lookups.txt'); $writer->addRow(['id', 'category_id', 'target', 'total', 'lookup_id']); foreach ($storelist as $store) { $lookup = OsaLookup::getOsaCategory($store->id); foreach ($lookup->categories as $category) { $data[0] = $store->id; $data[1] = $category->category_id; $data[2] = $category->target; $data[3] = $category->total; $data[4] = $lookup->id; $writer->addRow($data); } } $writer->close(); } // sos lookup if ($type == "sos_lists") { $keylist = FormGroup::where('sos', 1)->get(); $writer = WriterFactory::create(Type::CSV); $writer->openToBrowser('sos_keylist.txt'); $writer->addRow(['id']); foreach ($keylist as $list) { $data[0] = $list->id; $writer->addRow($data); } $writer->close(); } if ($type == "sos_lookups") { $writer = WriterFactory::create(Type::CSV); $writer->openToBrowser('sos_lookups.txt'); $writer->addRow(['store_id', 'category_id', 'sos_id', 'less', 'value', 'sos_lookup_id']); foreach ($storelist as $store) { $lookup = SosLookup::getSosCategory($store->id); $results = DB::select(DB::raw("select store_id, category_id, sos_id,less, value,sos_lookup_id from store_sos_tags\n join sos_lookup_percentages on sos_lookup_percentages.category_id = store_sos_tags.`form_category_id`\n where store_id = :store_id\n and sos_lookup_id = :lookup_id\n and store_sos_tags.`sos_tag_id` = sos_lookup_percentages.sos_id"), array('store_id' => $store->id, 'lookup_id' => $lookup->id)); // dd($results); foreach ($results as $result) { $data[0] = $result->store_id; $data[1] = $result->category_id; $data[2] = $result->sos_id; $data[3] = $result->less; $data[4] = $result->value; $data[5] = $result->sos_lookup_id; $writer->addRow($data); } } $writer->close(); } if ($type == "form_types") { $form_types = FormType::all(); $writer = WriterFactory::create(Type::CSV); $writer->openToBrowser('form_types.txt'); $writer->addRow(['id', 'form_type']); foreach ($form_types as $type) { $data[0] = $type->id; $data[1] = $type->form_type; $writer->addRow($data); } $writer->close(); } if ($type == "image_lists") { $writer = WriterFactory::create(Type::CSV); $writer->openToBrowser('image_lists.txt'); $writer->addRow(['image_name']); $results = SurveyImage::all(); // dd($results); foreach ($results as $result) { $data[0] = $result->images; $writer->addRow($data); } $writer->close(); } }