Ejemplo n.º 1
0
 public function subirArchivo(Request $request)
 {
     $file = $request->file('image');
     if (!file_exists($file)) {
         die("File not found. Make sure you specified the correct path.");
     }
     try {
         $pdo = \DB::connection()->getPdo();
     } catch (PDOException $e) {
         die("database connection failed: " . $e->getMessage());
     }
     $columns = '(telefono, periodo, valor)';
     $pdo->exec('SET foreign_key_checks = 0');
     $affectedRows = $pdo->exec("\n            LOAD DATA LOCAL INFILE " . $pdo->quote($file) . " INTO TABLE `comisiones`\n              FIELDS TERMINATED BY " . $pdo->quote(";") . "\n              LINES TERMINATED BY " . $pdo->quote("\n") . "\n              IGNORE 0 LINES " . $columns . "\n              SET ID = NULL");
     $huerfanas = \DB::select("select distinct(comisiones.telefono) from simcards RIGHT join comisiones on simcards.numero = comisiones.telefono where simcards.numero is null");
     $ICC = \DB::table('simcards')->select('ICC')->orderBy(\DB::raw('ICC*1'))->first();
     $ICC = $ICC->ICC;
     $fecha_vencimiento = date_add(new \DateTime(), date_interval_create_from_date_string("6 months"));
     foreach ($huerfanas as $huerfana) {
         $ICC = $ICC - 1;
         try {
             \App\Simcard::create(['numero' => $huerfana->telefono, 'ICC' => $ICC, 'fecha_vencimiento' => $fecha_vencimiento, 'fecha_activacion' => null, 'nombreSubdistribuidor' => 'SIN ASIGNAR', 'tipo' => 1, 'paquete' => 0, 'fecha_entrega' => null]);
         } catch (Exception $e) {
         }
     }
     $pdo->exec('SET foreign_key_checks = 1');
     return \Redirect::route('finanzas')->with('result', $affectedRows);
 }
 /**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     $this->command->info('Subir archivo simcards...');
     if (file_exists('public/temp/simcards.csv')) {
         if (($gestor = fopen('public/temp/simcards.csv', "r")) !== FALSE) {
             $opcion = fgetcsv($gestor, 1000, ",");
             if ($opcion[0] == 'AGREGAR') {
                 $this->command->info('AGREGANDO...');
                 while (($vars = fgetcsv($gestor, 1000, ",")) !== FALSE) {
                     $fecha_vencimiento = date_create_from_format("d/m/Y", $vars[3]);
                     $ICC = $vars[1];
                     if ($ICC == '0') {
                         $simc = \DB::table('simcards')->where('numero', '=', $vars[0])->first();
                     } else {
                         $simc = \DB::table('simcards')->where('numero', '=', $vars[0])->orwhere('ICC', '=', $vars[1])->first();
                     }
                     if ($simc != null) {
                         $sim = \App\Simcard::find($simc->ICC);
                         $sim->nombreSubdistribuidor = $vars[2];
                         $sim->fecha_vencimiento = $fecha_vencimiento;
                         $sim->tipo = $vars[4];
                         $sim->save();
                     } else {
                         if ($ICC == '0') {
                             $ICC = \DB::table('simcards')->select('ICC')->orderBy(\DB::raw('ICC*1'))->first();
                             $ICC = $ICC->ICC - 1;
                         }
                         \App\Simcard::create(['numero' => $vars[0], 'ICC' => $ICC, 'fecha_vencimiento' => $fecha_vencimiento, 'fecha_activacion' => null, 'nombreSubdistribuidor' => $vars[2], 'tipo' => $vars[4], 'paquete' => 0, 'fecha_entrega' => null]);
                     }
                 }
             } else {
                 if ($opcion[0] == 'ACTIVAR') {
                     $this->command->info('ACTIVANDO...');
                     while (($vars = fgetcsv($gestor, 1000, ",")) !== FALSE) {
                         $fecha_activacion = date_create_from_format("d/m/y", $vars[1]);
                         $simc = \DB::table('simcards')->where('numero', '=', $vars[0])->first();
                         if ($simc == null) {
                             $ICC = \DB::table('simcards')->select('ICC')->orderBy(\DB::raw('ICC*1'))->first();
                             $ICC = $ICC->ICC - 1;
                             $fecha_vencimiento = date_add($fecha_activacion, date_interval_create_from_date_string("7 months"));
                             $fecha_activacion = date_create_from_format("d/m/y", $vars[1]);
                             \App\Simcard::create(['numero' => $vars[0], 'ICC' => $ICC, 'fecha_vencimiento' => $fecha_vencimiento, 'fecha_activacion' => null, 'nombreSubdistribuidor' => 'SIN ASIGNAR', 'tipo' => 1, 'paquete' => 0, 'fecha_entrega' => null]);
                         } else {
                             $ICC = $simc->ICC;
                         }
                         $sim = \App\Simcard::find($ICC);
                         if ($sim->tipo == 1) {
                             $fecha_vencimiento = date_add($fecha_activacion, date_interval_create_from_date_string("7 months"));
                         } else {
                             $fecha_vencimiento = date_add($fecha_activacion, date_interval_create_from_date_string("12 months"));
                         }
                         $fecha_activacion = date_create_from_format("d/m/y", $vars[1]);
                         $sim->fecha_activacion = $fecha_activacion;
                         $sim->fecha_vencimiento = $fecha_vencimiento;
                         $sim->save();
                     }
                 }
             }
             fclose($gestor);
             unlink('public/temp/simcards.csv');
         }
     }
 }