ldc-bogt/app/Console/Commands/ImportMoba.php
Vincent BRIDIER 69a2aed8b9 - clean routes
- update bo route
- add translations
- set timezone and locale
- Add Handler
- Bump to Laravel 8.x
- update .env.example
2022-01-13 18:53:54 +01:00

105 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];
$date_ponte = $data[2];
list($d, $m, $y) = explode('/', $date_ponte);
$datePonte = $y . '-' . $m . '-' . $d;
$code_produit = $data[3];
$libelle_produit = $data[4];
$lot_externe = $data[5];
$dcr = $data[6];
$ean = $data[7];
list($d_dcr, $m_dcr, $y_dcr) = explode('/', $dcr);
$dateDcr = $y_dcr . '-' . $m_dcr . '-' . $d_dcr;
$moba = new Moba();
$moba->code_boite = $code_boite;
$moba->code_ede = $code_ede;
$moba->date_ponte = $datePonte;
$moba->code_produit = $code_produit;
$moba->libelle_produit = $libelle_produit;
$moba->lot_externe = $lot_externe;
$moba->dcr = $dateDcr;
$moba->date_emballage = $date_emballage;
$moba->ean13 = $ean;
$moba->save();
$lines++;
}
}
fclose($handle);
$logtxt = $file . " : " . $lines . " lignes insérées" . PHP_EOL;
fwrite($logs, $logtxt);
//scopy($file, 'archives/'.$file);
unlink($file);
unlink($file . '.ok');
}
fclose($logs);
return 0;
}
}