ldc-legaulois/app/Console/Commands/UpdateReductions.php
2021-02-15 11:51:43 +01:00

74 lines
2.1 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Models\Reductions;
use Carbon\Carbon;
use Illuminate\Console\Command;
use LDCWeb\MesBR;
class UpdateReductions extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'reductions:update';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Update the reductions that the application get from MesBR';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
// On récupère les réductions depuis MesBR
$mesBR = new MesBR(config('app.REF_SITE'), config('app.REF_CAMP'));
$reductions = $mesBR->getDetailCampaign();
// Mise à jour et ajout des coupons en BDD
$detailCampagne = $reductions->{'Détails Campagne'};
if( isset($detailCampagne->Coupons)) {
foreach ($detailCampagne->Coupons as $coupon) {
$reduction = Reductions::where('code', $coupon->{'Code traitement'})->first();
if(!$reduction){
$reduction = new Reductions();
$reduction->code = $coupon->{'Code traitement'};
}
$reduction->wording = $coupon->{'Nom'};
$reduction->number_broadcast = $coupon->{'Nb à diffuser'};
$reduction->number_broadcasted = $coupon->{'Nb diffusé'};
$reduction->face_value = floatval($coupon->{'Valeur faciale'});
$reduction->status = $coupon->{'En ligne'} == "oui" ? 1 : 0;
if(isset($coupon->{'Date de fin de diffusion'}) && $coupon->{'Date de fin de diffusion'} != "01/01/1970"){
$endDatetime = Carbon::createFromFormat('Y-m-d', date('Y-m-d', strtotime(str_replace("/", "-", $coupon->{'Date de fin de diffusion'}))));
} else {
$endDatetime = Carbon::createFromFormat('Y-m-d', date('Y-m-d', strtotime("2099-12-30")));
}
$reduction->end_date_broadcast = $endDatetime;
$reduction->save();
}
}
$this->info("Reductions updated successfuly!");
}
}