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(); } }