252 lines
10 KiB
PHP
252 lines
10 KiB
PHP
<?php
|
|
|
|
|
|
namespace App\Traits;
|
|
|
|
|
|
use App\Exceptions\AppException;
|
|
use App\HealthCareSheetType;
|
|
use App\InsuranceState;
|
|
use App\InsuranceSubscriptionAffiliation;
|
|
use App\InsuranceSubscriptionState;
|
|
use App\Models\AgentPlus;
|
|
use App\Models\CountriesCurrency;
|
|
use App\Models\Country;
|
|
use App\Models\NhHavingRight;
|
|
use App\Models\NhHealthCareSheet;
|
|
use App\Models\NhInfosHealthCareSheets;
|
|
use App\Models\NhInsurance;
|
|
use App\Models\NhInsurancesHavingRight;
|
|
use App\Models\NhInsurancesSubscription;
|
|
use App\Models\NhMonthsPricesGrid;
|
|
use App\Models\User;
|
|
use Brick\Money\Context\AutoContext;
|
|
use Brick\Money\Money;
|
|
use DateTime;
|
|
use DateTimeZone;
|
|
use Exception;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\UploadedFile;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Str;
|
|
|
|
trait Helper
|
|
{
|
|
public function getNetworkCurrency($networkId)
|
|
{
|
|
$currency = collect(DB::select('SELECT cu.code FROM networks n INNER JOIN countries c ON c.id = n.country_id INNER JOIN currencies cu ON cu.id = c.idCurrency
|
|
WHERE n.id = :id', ['id' => $networkId]))->first();
|
|
|
|
return isset($currency) ? $currency->code : 'XAF';
|
|
}
|
|
|
|
public function toMoneyWithNetwork($amount, $id_network)
|
|
{
|
|
$currency = collect(DB::select('SELECT cu.code FROM networks n INNER JOIN countries c ON c.id = n.country_id INNER JOIN currencies cu ON cu.id = c.idCurrency
|
|
WHERE n.id = :id', ['id' => $id_network]))->first();
|
|
|
|
$money = Money::of(round($amount, 2), $currency ? $currency->code : 'XAF', new AutoContext());
|
|
return $money->formatTo(app()->getLocale());
|
|
}
|
|
|
|
public function toMoney($amount, $id_country)
|
|
{
|
|
$country = Country::findOrFail($id_country);
|
|
$money = Money::of(round($amount, 2), $country->currency->code, new AutoContext());
|
|
return $money->formatTo(app()->getLocale());
|
|
}
|
|
|
|
public function toMoneyWithCurrencyCode($amount, $currency_code)
|
|
{
|
|
$money = Money::of(round($amount, 2), $currency_code, new AutoContext());
|
|
return $money->formatTo(app()->getLocale());
|
|
}
|
|
|
|
public function uploadImage(UploadedFile $file, $imageCode, $folderName)
|
|
{
|
|
$original_filename = $file->getClientOriginalName();
|
|
$original_filename_arr = explode('.', $original_filename);
|
|
$file_ext = end($original_filename_arr);
|
|
$image = $imageCode . '-' . Str::uuid() . '.' . $file_ext;
|
|
|
|
//Check if the directory already exists.
|
|
$directoryName = './' . $folderName;
|
|
if (!is_dir($directoryName)) {
|
|
//Directory does not exist, so lets create it.
|
|
mkdir($directoryName, 0755);
|
|
}
|
|
|
|
$compressedImage = compressImage($file, './' . $folderName . '/' . $image, 70);
|
|
if ($compressedImage) {
|
|
return $image;
|
|
} else {
|
|
return $this->errorResponse(trans('errors.compression_failed'));
|
|
}
|
|
}
|
|
|
|
// Obtenir l'heure en fonction du pays de l'utilisateur
|
|
public function getCurrentTime($id_country)
|
|
{
|
|
$country = CountriesCurrency::find($id_country);
|
|
$country_code = isset($country) ? $country->code_country : 'GA';
|
|
$timezone = DateTimeZone::listIdentifiers(DateTimeZone::PER_COUNTRY, $country_code);
|
|
$date = (sizeof($timezone) > 0) ? new DateTime('now', new DateTimeZone($timezone[0])) : new DateTime();
|
|
return $date->format('Y-m-d H:i:s');
|
|
}
|
|
|
|
// Obtenir l'heure en fonction du code du pays de l'utilisateur
|
|
public function getCurrentTimeByCountryCode($country_code = 'GA')
|
|
{
|
|
$timezone = DateTimeZone::listIdentifiers(DateTimeZone::PER_COUNTRY, $country_code);
|
|
$date = (sizeof($timezone) > 0) ? new DateTime('now', new DateTimeZone($timezone[0])) : new DateTime();
|
|
return $date->format('Y-m-d H:i:s');
|
|
}
|
|
|
|
// Caculer le montant de la prime d'un ayant droit ou beneficiaire
|
|
public function calculateBeneficiaryBonusAmount(NhHavingRight $beneficiary, Collection $yearsPricesGrid,
|
|
NhMonthsPricesGrid $monthPrice)
|
|
{
|
|
$bonus = 0;
|
|
if ($beneficiary->affiliation == 'CHILD') {
|
|
$age = date_diff(date_create($beneficiary->birthdate), date_create('now'))->y;
|
|
$levels = $yearsPricesGrid->filter(function ($level) use ($age) {
|
|
return $level->min_age <= $age && $level->max_age >= $age;
|
|
});
|
|
|
|
foreach ($levels as $level) {
|
|
$bonus += (100 + $level->markup_percentage) * $monthPrice->min_amount / 100;
|
|
}
|
|
} else {
|
|
$bonus = $monthPrice->min_amount;
|
|
}
|
|
|
|
return $bonus;
|
|
}
|
|
|
|
|
|
public function storeBeneficiariesAndGetBonus(NhInsurancesSubscription $subscription, Request $request,
|
|
$networkConfig, NhMonthsPricesGrid $monthPrice, string $datetime)
|
|
{
|
|
$subscription->state = InsuranceSubscriptionState::UNDER_VALIDATION;
|
|
|
|
$beneficiariesBonus = 0;
|
|
foreach ($request->input('beneficiaries', []) as $b) {
|
|
$beneficiary = new NhHavingRight($b);
|
|
$beneficiary->bonus_amount = $this->calculateBeneficiaryBonusAmount($beneficiary, $networkConfig->yearsPricesGrid, $monthPrice);
|
|
$beneficiariesBonus += $beneficiary->bonus_amount;
|
|
if ($beneficiary->affiliation == InsuranceSubscriptionAffiliation::CHILD) {
|
|
$beneficiary->marriage_certificate_doc = null;
|
|
$beneficiary->id_document_type = null;
|
|
$beneficiary->id_document_back = null;
|
|
$beneficiary->id_document_front = null;
|
|
} else {
|
|
$beneficiary->justice_doc = null;
|
|
$beneficiary->birthdate_proof_doc = null;
|
|
$beneficiary->birthdate_proof = null;
|
|
}
|
|
$beneficiary->created_at = $beneficiary->updated_at = $datetime;
|
|
$beneficiary->save();
|
|
NhInsurancesHavingRight::create([
|
|
'insurance_subscription_id' => $subscription->id,
|
|
'having_right_id' => $beneficiary->id,
|
|
]);
|
|
}
|
|
return $beneficiariesBonus;
|
|
}
|
|
|
|
public function generateSubscriptionID(): string
|
|
{
|
|
do {
|
|
$code = generateTransactionCode();
|
|
$codeCorrect = NhInsurancesSubscription::where('insurance_subscription_id', $code)->count() < 0;
|
|
} while ($codeCorrect);
|
|
return $code;
|
|
}
|
|
|
|
public function generateInvoiceID($agent_code)
|
|
{
|
|
return date('d') . '/' . date('m') . '/' . date('Y') . '/' . $agent_code;
|
|
}
|
|
|
|
public function fetchHealthCareSheetAmounts($sheet)
|
|
{
|
|
$insurerAmount = 0;
|
|
$insuredAmount = 0;
|
|
|
|
if ($sheet->type == HealthCareSheetType::CONSULTATION) {
|
|
$sum = current(DB::select("SELECT SUM(moderator_ticket) as moderator_ticket , SUM(insurance_amount) as insurance_amount FROM nh_performances p INNER JOIN
|
|
nh_health_care_sheets_performances hp ON p.id = hp.performance_id WHERE hp.sheet_id = :sheet_id LIMIT 1", ['sheet_id' => $sheet->id]));
|
|
if (isset($sum)) {
|
|
$insuredAmount += $sum->moderator_ticket;
|
|
$insurerAmount += $sum->insurance_amount;
|
|
}
|
|
} else {
|
|
$sum = current(DB::select("SELECT SUM(insured_paid_amount) as insured_paid_amount , SUM(insurer_paid_amount) as insurer_paid_amount FROM nh_medical_prescriptions p INNER JOIN
|
|
nh_health_care_sheets_prescriptions hp ON p.id = hp.prescription_id WHERE hp.sheet_id = :sheet_id LIMIT 1", ['sheet_id' => $sheet->id]));
|
|
if (isset($sum)) {
|
|
$insuredAmount += $sum->insured_paid_amount;
|
|
$insurerAmount += $sum->insurer_paid_amount;
|
|
}
|
|
|
|
|
|
$sum = current(DB::select("SELECT SUM(insured_paid_amount) as insured_paid_amount , SUM(insurer_paid_amount) as insurer_paid_amount FROM nh_exams e INNER JOIN
|
|
nh_health_care_sheets_exams he ON e.id = he.exam_id WHERE he.sheet_id = :sheet_id LIMIT 1", ['sheet_id' => $sheet->id]));
|
|
if (isset($sum)) {
|
|
$insuredAmount += $sum->insured_paid_amount;
|
|
$insurerAmount += $sum->insurer_paid_amount;
|
|
}
|
|
}
|
|
|
|
$sheet->insurer_amount = $insurerAmount;
|
|
$sheet->insured_amount = $insuredAmount;
|
|
}
|
|
|
|
/**
|
|
* @throws AppException
|
|
*/
|
|
public function insuranceVerification(NhInsurance $insurance)
|
|
{
|
|
if ($insurance->state != InsuranceState::PAID) {
|
|
throw new AppException(__('errors.insurance_expired', ['state' => mb_strtolower(trans('states.' . $insurance->state), 'UTF-8')]), 400);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @throws AppException
|
|
*/
|
|
public function latestSubscriptionVerification($network_id, $user_id)
|
|
{
|
|
$latestSubscription = NhInsurancesSubscription::where('network_id', $network_id)->where('user_id', $user_id)
|
|
->whereIn('state', [InsuranceSubscriptionState::UNDER_VALIDATION, InsuranceSubscriptionState::AWAITING_FURTHER_INFORMATION])->orderBy('created_at', 'DESC')->first();
|
|
|
|
if (isset($latestSubscription)) {
|
|
throw new AppException(trans('errors.subscription_cannot_be_submitted', ['state' => mb_strtolower(trans('states.' . $latestSubscription->state), 'UTF-8')]));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @throws AppException
|
|
*/
|
|
public function userCredentialsVerification(User $user, $password)
|
|
{
|
|
if (!isset($user->identification) || $user->identification->status == 0)
|
|
throw new AppException(trans('errors.user_identification_required'));
|
|
|
|
if (!checkPassword($password, $user->encrypted_password, $user->salt))
|
|
throw new AppException(trans('messages.incorrect_user_password'));
|
|
}
|
|
|
|
/**
|
|
* @throws AppException
|
|
*/
|
|
public function agentCredentialsVerification($network_agent_id, $password)
|
|
{
|
|
$agent = AgentPlus::where('network_agent_id', $network_agent_id)->first();
|
|
if (!checkPassword($password, $agent->encrypted_password, $agent->salt))
|
|
throw new AppException(trans('messages.incorrect_user_password'));
|
|
}
|
|
}
|