2021-10-04 16:24:39 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console;
|
|
|
|
|
2022-01-25 23:16:26 +00:00
|
|
|
use App\BillingPeriodType;
|
|
|
|
use App\InsuranceSubscriptionState;
|
|
|
|
use App\Models\AgentPlus;
|
|
|
|
use App\Models\CountriesCurrency;
|
|
|
|
use App\Models\NhHealthCareSheet;
|
|
|
|
use App\Models\NhInvoice;
|
|
|
|
use App\Models\NhNetworksConfig;
|
|
|
|
use App\Traits\Helper;
|
|
|
|
use Barryvdh\DomPDF\Facade as PDF;
|
|
|
|
use DateTime;
|
2021-10-04 16:24:39 +00:00
|
|
|
use Illuminate\Console\Scheduling\Schedule;
|
2022-01-25 23:16:26 +00:00
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
use Illuminate\Support\Facades\Mail;
|
2021-10-04 16:24:39 +00:00
|
|
|
use Laravel\Lumen\Console\Kernel as ConsoleKernel;
|
|
|
|
|
|
|
|
class Kernel extends ConsoleKernel
|
|
|
|
{
|
2022-01-25 23:16:26 +00:00
|
|
|
use Helper;
|
|
|
|
|
2021-10-04 16:24:39 +00:00
|
|
|
/**
|
|
|
|
* The Artisan commands provided by your application.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $commands = [
|
|
|
|
//
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Define the application's command schedule.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Console\Scheduling\Schedule $schedule
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function schedule(Schedule $schedule)
|
|
|
|
{
|
2022-01-25 23:16:26 +00:00
|
|
|
// Generer les factures chaque dimanche à minuit
|
|
|
|
$schedule->call(function () {
|
|
|
|
|
|
|
|
$date = new DateTime();
|
|
|
|
Log::info('-------- Weekly Invoice Generation Start ' . $date->format("d/m/Y") . ' -----------');
|
|
|
|
$hypers = AgentPlus::whereHas('network', function ($query) {
|
|
|
|
return $query->has('nh_network_config');
|
|
|
|
})->where('category', 'hyper')->get();
|
|
|
|
|
|
|
|
foreach ($hypers as $hyper) {
|
|
|
|
$config = NhNetworksConfig::where('network_id', $hyper->network_id)->first();
|
|
|
|
if (!isset($config)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$agents = AgentPlus::where('network_id', $hyper->network_id)->where('category', 'geolocated')->get();
|
|
|
|
foreach ($agents as $agent) {
|
|
|
|
|
|
|
|
$start_at = new DateTime();
|
|
|
|
switch ($config->provider_billing_period) {
|
|
|
|
case BillingPeriodType::WEEKLY:
|
|
|
|
$start_at->modify('-7 days');
|
|
|
|
break;
|
|
|
|
case BillingPeriodType::BIMONTHLY:
|
|
|
|
$start_at->modify('-14 days');
|
|
|
|
break;
|
|
|
|
case BillingPeriodType::MONTHLY:
|
|
|
|
$start_at->modify('-28 days');
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$start_at = new DateTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
$end_at = new DateTime();
|
|
|
|
|
|
|
|
|
|
|
|
$invoice_id = $this->generateInvoiceID($agent->code_membre);
|
|
|
|
$country = CountriesCurrency::findOrFail($agent->country_id);
|
|
|
|
$datetime = $this->getCurrentTimeByCountryCode($country->code_country);
|
|
|
|
|
|
|
|
$totalInsurerAmount = 0;
|
|
|
|
$totalInsuredAmount = 0;
|
|
|
|
$sheets = NhHealthCareSheet::with(['insurance'])->where('network_agent_id', $agent->network_agent_id)->where('state', InsuranceSubscriptionState::ACCEPTED)
|
|
|
|
->where('created_at', '>=', $start_at)->where('created_at', '<=', $end_at)->orderBy('created_at', 'DESC')->get();
|
|
|
|
|
|
|
|
if (sizeof($sheets) == 0) {
|
|
|
|
Log::info("Periode " . $date->format("d/m/Y") . " - " . $date->format("d/m/Y")
|
|
|
|
. "\n Network " . $hyper->network . "\n Aucune feuille de soins acceptée");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($sheets as $sheet) {
|
|
|
|
$this->fetchHealthCareSheetAmounts($sheet);
|
|
|
|
|
|
|
|
$totalInsuredAmount += $sheet->insured_amount;
|
|
|
|
$totalInsurerAmount += $sheet->insurer_amount;
|
|
|
|
|
|
|
|
$sheet->date = $sheet->created_at->format('d/m/Y');
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
DB::beginTransaction();
|
|
|
|
|
|
|
|
$directoryName = '/invoices-docs/';
|
2022-01-28 18:25:18 +00:00
|
|
|
$filename = $start_at->format('dmY') . '_' . $end_at->format('dmY') . '_' . $agent->code_membre . '_' . time() . '.pdf';
|
2022-01-25 23:16:26 +00:00
|
|
|
|
|
|
|
$invoice = NhInvoice::create([
|
|
|
|
'invoice_id' => $invoice_id,
|
|
|
|
'network_agent_id' => $agent->network_agent_id,
|
|
|
|
'amount' => $totalInsuredAmount + $totalInsurerAmount,
|
|
|
|
'insured_amount' => $totalInsuredAmount,
|
|
|
|
'insurer_amount' => $totalInsurerAmount,
|
|
|
|
'period_start_at' => $start_at,
|
|
|
|
'period_end_at' => $end_at,
|
|
|
|
'file_url' => config('services.app_url') . $directoryName . $filename,
|
|
|
|
'created_at' => $datetime,
|
|
|
|
'updated_at' => $datetime
|
|
|
|
]);
|
|
|
|
|
|
|
|
$invoice->amount = $this->toMoneyWithCurrencyCode($invoice->amount, $country->currency_code);
|
|
|
|
$invoice->insured_amount = $this->toMoneyWithCurrencyCode($invoice->insured_amount, $country->currency_code);
|
|
|
|
$invoice->insurer_amount = $this->toMoneyWithCurrencyCode($invoice->insurer_amount, $country->currency_code);
|
|
|
|
$invoice->home_visit_fees = $this->toMoneyWithCurrencyCode($invoice->home_visit_fees, $country->currency_code);
|
|
|
|
|
|
|
|
$ids = array_map(function ($r) {
|
|
|
|
return $r['id'];
|
|
|
|
}, $sheets->toArray());
|
|
|
|
|
|
|
|
DB::update("UPDATE nh_health_care_sheets SET state = :state_ , invoice_id = :id WHERE id IN (" . implode(',', $ids) . ")",
|
|
|
|
['id' => $invoice->id, 'state_' => InsuranceSubscriptionState::INVOICE_ISSUED]);
|
|
|
|
|
|
|
|
$invoice->sheets = $sheets;
|
|
|
|
$invoice->agent = $agent;
|
|
|
|
$invoice->hyper = $hyper;
|
|
|
|
$invoice->period = $start_at->format('d/m/Y') . ' ' . trans('messages.to') . ' ' . $end_at->format('d/m/Y');
|
|
|
|
|
|
|
|
//Check if the directory already exists.
|
|
|
|
if (!is_dir(public_path($directoryName))) {
|
|
|
|
//Directory does not exist, so lets create it.
|
|
|
|
mkdir(public_path($directoryName), 0755);
|
|
|
|
}
|
|
|
|
|
2022-02-01 10:58:58 +00:00
|
|
|
$title = $agent->lastname . ' - ' . trans('messages.invoice') . ' ' . $invoice->invoice_id;
|
|
|
|
$message = __('messages.generated_invoice_mail', ['agent' => $agent->lastname, 'period' => $invoice->period]);
|
2022-01-25 23:16:26 +00:00
|
|
|
|
|
|
|
$file = PDF::loadView('emails.invoice', $invoice->toArray())->setPaper('a4', 'landscape')->setWarnings(false)->save(public_path($directoryName . $filename));
|
|
|
|
|
|
|
|
DB::commit();
|
|
|
|
$recipients = [preg_replace("/\s+/", "", $hyper->email)]; // Supprimer les espaces dans le mail
|
|
|
|
Mail::mailer('smtp')->raw($message, function ($message) use ($recipients, $title, $file) {
|
|
|
|
$message->subject($title)
|
|
|
|
->to($recipients)
|
|
|
|
->attachData($file->output(), str_replace(' ', '-', $title) . '.pdf');
|
|
|
|
});
|
|
|
|
|
|
|
|
} catch (\Throwable $t) {
|
|
|
|
DB::rollBack();
|
|
|
|
Log::error('-------- Mail not sent -----------');
|
|
|
|
Log::error($t->getMessage() . " :\n" . $t->getTraceAsString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Log::info('-------- Weekly Invoice Generation End ' . $date->format("d/m/Y") . '-----------');
|
|
|
|
})->weekly()->runInBackground();
|
2021-10-04 16:24:39 +00:00
|
|
|
}
|
|
|
|
}
|