ldc-bogt/app/Console/Commands/ImportMoba.php
Jérémy Munsch 283a357281
phpcbf pass
2022-03-08 09:42:26 +01:00

104 lines
3.2 KiB
PHP

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Moba;
use Carbon\Carbon;
class ImportMoba extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'import:moba';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Import des CSV MOBA';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
setlocale(LC_CTYPE, 'en_US.utf8');
$path = base_path("resources/flux_in/*.csv");
$logpath = base_path("resources/flux_in/log/log.txt");
$logs = fopen($logpath, 'a+');
foreach (glob($path) as $file) {
$flag = true;
$lines = 0;
$filename = substr($file, -14);
$year = substr($filename, 0, 4);
$month = substr($filename, 5, 2);
$day = substr($filename, 8, 2);
$date_emballage_array = Carbon::createFromFormat('Y-m-d', $year . '-' . $month . '-' . $day)->subDay()->toArray();
$date_emballage = $date_emballage_array['year'] . '-' . $date_emballage_array['month'] . '-' . $date_emballage_array['day'];
if (($handle = fopen($file, "r")) !== false) {
while (($data = fgetcsv($handle, 50000, ";", '"')) !== false) {
if ($flag) {
$flag = false;
continue;
}
$code_boite = $data[0];
$code_ede = $data[1];
// format dans le fichier Y-m-d
$date_ponte = $data[2];
$code_produit = $data[3];
$libelle_produit = $data[4];
$lot_externe = $data[5];
// format dans le fichier Y-m-d
$dcr = $data[6];
$ean = $data[7];
$moba = new Moba();
$moba->code_boite = $code_boite;
$moba->code_ede = $code_ede;
$moba->date_ponte = $date_ponte;
$moba->code_produit = $code_produit;
$moba->libelle_produit = $libelle_produit;
$moba->lot_externe = $lot_externe;
$moba->dcr = $dcr;
$moba->date_emballage = $date_emballage;
$moba->ean13 = $ean;
$moba->save();
$lines++;
}//end while
}//end if
fclose($handle);
$logtxt = $file . " : " . $lines . " lignes insérées" . PHP_EOL;
fwrite($logs, $logtxt);
// scopy($file, 'archives/'.$file);
unlink($file);
// il n'y a plus de fichier .ok apparement
@unlink($file . '.ok');
}//end foreach
fclose($logs);
return 0;
}
}