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 . "/Store SOS.xlsx";
     echo (string) $file_path, "\n";
     Model::unguard();
     DB::statement('SET FOREIGN_KEY_CHECKS=0;');
     DB::table('store_sos_tags')->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 (!empty($row[0])) {
                     // dd($row);
                     if ($cnt > 0) {
                         // dd($row);
                         $store = Store::where('store_code', $row[0])->first();
                         // dd($store);
                         $category = FormCategory::where('category', strtoupper($row[2]))->first();
                         // dd($category);
                         $sos = SosTagging::where('sos_tag', strtoupper($row[3]))->first();
                         // dd($sos);
                         StoreSosTag::insert(array('store_id' => $store->id, 'form_category_id' => $category->id, 'sos_tag_id' => $sos->id));
                         // echo (string)$row[0], "\n";
                     }
                     $cnt++;
                 }
             }
         } else {
         }
     }
     $reader->close();
     DB::statement('SET FOREIGN_KEY_CHECKS=1;');
     Model::reguard();
 }
 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();
 }
 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 . "/Category Tagging.xlsx";
     echo (string) $file_path, "\n";
     Model::unguard();
     DB::statement('SET FOREIGN_KEY_CHECKS=0;');
     Excel::selectSheets('Sheet1')->load($file_path, function ($reader) {
         $records = $reader->get();
         $records->each(function ($row) {
             if (!is_null($row->category)) {
                 $form_category = FormCategory::where('category', $row->category)->first();
                 // dd($form_category);
                 if (!empty($form_category)) {
                     $form_category->sos_tagging = $row->sos;
                     $form_category->osa_tagging = $row->osa;
                     $form_category->secondary_display = $row->secondary_display;
                     $form_category->update();
                 }
             }
         });
     });
     DB::statement('SET FOREIGN_KEY_CHECKS=1;');
     Model::reguard();
 }