Improve pay subscription to enable to pay when add beneficairy
This commit is contained in:
parent
ed503cc60e
commit
408b38a840
|
@ -154,7 +154,7 @@ class InsuranceController extends Controller
|
|||
});
|
||||
|
||||
if ($request->has('type') && $type == 'EDITABLE') {
|
||||
$query = $query->whereIn('state', [InsuranceState::PAID, InsuranceState::REMAINS]);
|
||||
$query = $query->whereIn('state', [InsuranceState::PAID]);
|
||||
}
|
||||
|
||||
$insurances = $query->orderBy('created_at', 'DESC')->get();
|
||||
|
@ -163,7 +163,7 @@ class InsuranceController extends Controller
|
|||
$insurance->state = trans('states.' . $insurance->state);
|
||||
|
||||
if ($type == 'EDITABLE') {
|
||||
//Necessaire seulement lors de la modification ( ajout de ayant droit;
|
||||
// Nécessaire seulement lors de la modification ( ajout de ayant droit)
|
||||
$config = NhNetworksConfig::where('network_id', $insurance->subscription->network->id)->firstOrFail();
|
||||
$insurance->subscription->network->age_limit_of_insured_and_spouse = $config->age_limit_of_insured_and_spouse;
|
||||
$insurance->subscription->network->age_limit_of_child_beneficiary = $config->age_limit_of_child_beneficiary;
|
||||
|
@ -259,7 +259,7 @@ class InsuranceController extends Controller
|
|||
$insurance = NhInsurance::findOrFail($id);
|
||||
$subscription = $insurance->subscription;
|
||||
if ($subscription->state != InsuranceSubscriptionState::ACCEPTED) {
|
||||
return $this->errorResponse(trans('errors.subscription_cannot_be_updated'));
|
||||
return $this->errorResponse(trans('errors.subscription_cannot_be_submitted'));
|
||||
}
|
||||
$user = $subscription->user;
|
||||
$identification = $subscription->user->identification;
|
||||
|
@ -308,7 +308,7 @@ class InsuranceController extends Controller
|
|||
$newSubscription->save();
|
||||
|
||||
$insurance->insurance_subscription_id = $newSubscription->insurance_subscription_id;
|
||||
$insurance->state = InsuranceState::REMAINS;
|
||||
$insurance->state = InsuranceState::ADDITION_OF_BENEFICIARY;
|
||||
$insurance->remaining_amount = $beneficiariesBonus;
|
||||
$insurance->save();
|
||||
|
||||
|
@ -320,12 +320,12 @@ class InsuranceController extends Controller
|
|||
'created_at' => $datetime, 'updated_at' => $datetime,
|
||||
]);
|
||||
|
||||
Event::dispatch(new InsuranceEvent($newSubscription, trans('messages.insurance_subscription_updated'), trans('messages.insurance_subscription_mail', ['name' => $newSubscription->user->lastname, 'subscription_id' => $newSubscription->insurance_subscription_id,
|
||||
Event::dispatch(new InsuranceEvent($newSubscription, trans('messages.insurance_addition_beneficiary'), trans('messages.insurance_addition_beneficiary_mail', ['name' => $newSubscription->user->lastname, 'subscription_id' => $newSubscription->insurance_subscription_id,
|
||||
'bonus_amount' => $this->toMoneyWithNetwork($newSubscription->total_bonus_amount, $newSubscription->network_id), 'number_of_beneficiaries' => $newSubscription->number_of_beneficiaries,
|
||||
'gender' => trans('states.' . $identification->gender), 'insurance_name' => $networkConfig->network->name])));
|
||||
|
||||
DB::commit();
|
||||
return $this->successResponse(trans('messages.insurance_subscription_updated_successful'));
|
||||
return $this->successResponse(trans('messages.insurance_addition_beneficiary_successful'));
|
||||
} catch (Throwable $e) {
|
||||
Log::error($e->getMessage() . '\n' . $e->getTraceAsString());
|
||||
DB::rollBack();
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Events\InsuranceEvent;
|
||||
use App\InsurancePaymentReason;
|
||||
use App\InsuranceState;
|
||||
use App\InsuranceSubscriptionAffiliation;
|
||||
use App\InsuranceSubscriptionState;
|
||||
use App\Models\AgentPlus;
|
||||
|
@ -538,11 +540,14 @@ class InsuranceSubscriptionController extends Controller
|
|||
]);
|
||||
|
||||
$subscription = NhInsurancesSubscription::findOrFail($id);
|
||||
if ($subscription->state != InsuranceSubscriptionState::ACCEPTED) {
|
||||
if ($subscription->state != InsuranceSubscriptionState::ACCEPTED || isset($subscription->payment)) {
|
||||
return $this->errorResponse(trans('errors.subscription_cannot_be_paid'));
|
||||
}
|
||||
|
||||
$currency = $this->getNetworkCurrency($subscription->network_id);
|
||||
|
||||
$insurance = NhInsurance::where('insurance_subscription_id', $subscription->insurance_subscription_id)->first();
|
||||
if (isset($insurance)) {
|
||||
if (isset($insurance) && $insurance->state == InsuranceState::PAID) {
|
||||
return $this->errorResponse(trans('errors.subscription_be_already_paid'));
|
||||
}
|
||||
|
||||
|
@ -551,8 +556,14 @@ class InsuranceSubscriptionController extends Controller
|
|||
return $this->errorResponse(trans('messages.incorrect_user_password'));
|
||||
}
|
||||
|
||||
if ($user->wallet->balance < $subscription->total_bonus_amount) {
|
||||
return $this->errorResponse(trans('errors.insufficient_balance'));
|
||||
$amountToPaid = $subscription->total_bonus_amount;
|
||||
if (isset($insurance) && $insurance->state == InsuranceState::ADDITION_OF_BENEFICIARY) {
|
||||
$amountToPaid = $insurance->remaining_amount;
|
||||
}
|
||||
|
||||
if ($user->wallet->balance < $amountToPaid) {
|
||||
$amount = $amountToPaid - $user->wallet->balance;
|
||||
return $this->errorResponse(trans('errors.insufficient_balance', ['amount' => $this->toMoneyWithCurrencyCode($amount, $currency)]));
|
||||
}
|
||||
|
||||
try {
|
||||
|
@ -562,33 +573,46 @@ class InsuranceSubscriptionController extends Controller
|
|||
|
||||
$hyperviseur = AgentPlus::where('category', 'hyper')->where('network_id', $subscription->network_id)->firstOrFail();
|
||||
$walletHyperviseur = Wallet::where('id_networkAgent', $hyperviseur->network_agent_id)->firstOrFail();
|
||||
$walletHyperviseur->balance_princ += $subscription->total_bonus_amount;
|
||||
$walletHyperviseur->balance_princ += $amountToPaid;
|
||||
$walletHyperviseur->save();
|
||||
|
||||
$user->balance_nano_health += $subscription->total_bonus_amount;
|
||||
$user->wallet->balance -= $subscription->total_bonus_amount;
|
||||
$user->balance_nano_health += $amountToPaid;
|
||||
$user->wallet->balance -= $amountToPaid;
|
||||
$user->wallet->save();
|
||||
$user->save();
|
||||
|
||||
$insuredId = $this->generateInsuredID();
|
||||
if (isset($insurance) && $insurance->state == InsuranceState::ADDITION_OF_BENEFICIARY) {
|
||||
$insurance->state = InsuranceState::PAID;
|
||||
$insurance->remaining_amount = 0;
|
||||
$insurance->save();
|
||||
$reason = InsurancePaymentReason::ADDITION_OF_BENEFICIARY;
|
||||
|
||||
} else {
|
||||
$insuredId = $this->generateInsuredID();
|
||||
$insurance = NhInsurance::create([
|
||||
'insurance_subscription_id' => $subscription->insurance_subscription_id,
|
||||
'insured_id' => $insuredId,
|
||||
'created_at' => $datetime, 'updated_at' => $datetime,
|
||||
'state' => InsuranceState::PAID,
|
||||
'start_at' => $datetime,
|
||||
'end_at' => DateTime::createFromFormat('Y-m-d H:i:s', $datetime)->modify('+' . $subscription->number_of_months . 'months')
|
||||
]);
|
||||
$reason = InsurancePaymentReason::ACTIVATION;
|
||||
}
|
||||
|
||||
NhInsurance::create([
|
||||
'insurance_subscription_id' => $subscription->insurance_subscription_id,
|
||||
'insured_id' => $insuredId,
|
||||
'created_at' => $datetime, 'updated_at' => $datetime,
|
||||
'start_at' => $datetime,
|
||||
'end_at' => DateTime::createFromFormat('Y-m-d H:i:s', $datetime)->modify('+' . $subscription->number_of_months . 'months')
|
||||
]);
|
||||
|
||||
NhInsurancesPayment::create([
|
||||
'insured_id' => $insuredId,
|
||||
'amount' => $subscription->total_bonus_amount,
|
||||
'insurance_subscription_id' => $subscription->insurance_subscription_id,
|
||||
'insured_id' => $insurance->insured_id,
|
||||
'amount' => $amountToPaid,
|
||||
'reason' => $reason,
|
||||
'insurance_details' => json_encode($insurance),
|
||||
'created_at' => $datetime, 'updated_at' => $datetime,
|
||||
]);
|
||||
|
||||
|
||||
Event::dispatch(new InsuranceEvent($subscription, trans('messages.insurance_subscription_paid'), trans('messages.insurance_subscription_paid_mail', ['name' => $subscription->user->lastname, 'subscription_id' => $subscription->insurance_subscription_id,
|
||||
'bonus_amount' => $this->toMoneyWithNetwork($subscription->total_bonus_amount, $subscription->network_id), 'insured_id' => $insuredId, 'number_of_beneficiaries' => $subscription->number_of_beneficiaries,
|
||||
'bonus_amount' => $this->toMoneyWithCurrencyCode($subscription->total_bonus_amount, $currency), 'insured_id' => $insurance->insured_id, 'number_of_beneficiaries' => $subscription->number_of_beneficiaries,
|
||||
'gender' => trans('states.' . $subscription->user->identification->gender), 'insurance_name' => $subscription->network->name, 'months' => $subscription->number_of_months])));
|
||||
DB::commit();
|
||||
return $this->successResponse(trans('messages.insurance_subscription_paid'));
|
||||
|
@ -673,7 +697,8 @@ class InsuranceSubscriptionController extends Controller
|
|||
if ($request->has('type')) {
|
||||
$type = $request->input('type');
|
||||
if ($type != 'ALL') {
|
||||
$query = $query->where('state', $type);
|
||||
// Les souscriptions payables
|
||||
$query = $query->where('state', $type)->whereDoesntHave('payment');
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
abstract class InsurancePaymentReason
|
||||
{
|
||||
const ACTIVATION = 'ACTIVATION';
|
||||
const RENEWAL = 'RENEWAL';
|
||||
const ADDITION_OF_BENEFICIARY = 'ADDITION_OF_BENEFICIARY';
|
||||
const DELETION_OF_BENEFICIARY = 'DELETION_OF_BENEFICIARY';
|
||||
|
||||
}
|
|
@ -7,5 +7,6 @@ abstract class InsuranceState
|
|||
const PAID = 'PAID';
|
||||
const UNDER_STOPPING = 'UNDER_STOPPING';
|
||||
const STOPPED = 'STOPPED';
|
||||
const REMAINS = 'REMAINS';
|
||||
const ADDITION_OF_BENEFICIARY = 'ADDITION_OF_BENEFICIARY';
|
||||
const DELETION_OF_BENEFICIARY = 'DELETION_OF_BENEFICIARY';
|
||||
}
|
||||
|
|
|
@ -13,8 +13,11 @@ use Illuminate\Database\Eloquent\Model;
|
|||
* Class NhInsurancesPayment
|
||||
*
|
||||
* @property int $id
|
||||
* @property string $insurance_subscription_id
|
||||
* @property string $insured_id
|
||||
* @property float $amount
|
||||
* @property string $reason
|
||||
* @property string $insurance_details
|
||||
* @property Carbon $created_at
|
||||
* @property Carbon $updated_at
|
||||
*
|
||||
|
@ -29,7 +32,10 @@ class NhInsurancesPayment extends Model
|
|||
];
|
||||
|
||||
protected $fillable = [
|
||||
'insurance_subscription_id',
|
||||
'insured_id',
|
||||
'amount'
|
||||
'amount',
|
||||
'reason',
|
||||
'insurance_details'
|
||||
];
|
||||
}
|
||||
|
|
|
@ -61,4 +61,9 @@ class NhInsurancesSubscription extends Model
|
|||
return $this->belongsTo(NhNetworksConfig::class, 'network_id', 'network_id');
|
||||
}
|
||||
|
||||
public function payment()
|
||||
{
|
||||
return $this->belongsTo(NhInsurancesPayment::class, 'insurance_subscription_id', 'insurance_subscription_id');
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class UpdateNhInsurancesPaymentsTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('nh_insurances_payments', function (Blueprint $table) {
|
||||
$table->string('insurance_subscription_id')->after('id');
|
||||
$table->enum('reason', ['ACTIVATION', 'ADDITION_OF_BENEFICIARY', 'DELETION_OF_BENEFICIARY', 'RENEWAL'])->default('ACTIVATION')->after('amount');
|
||||
$table->text('insurance_details')->after('reason');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::table('nh_insurances_payments', function (Blueprint $table) {
|
||||
$table->dropColumn(['insurance_subscription_id', 'reason', 'insurance_details']);
|
||||
});
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class UpdateStateInNhInsurancesTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('nh_insurances', function (Blueprint $table) {
|
||||
DB::statement("ALTER TABLE nh_insurances MODIFY state
|
||||
ENUM('PAID', 'UNDER_STOPPING', 'STOPPED', 'ADDITION_OF_BENEFICIARY', 'DELETION_OF_BENEFICIARY') DEFAULT 'PAID' NOT NULL;");
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::table('nh_insurances', function (Blueprint $table) {
|
||||
//
|
||||
});
|
||||
}
|
||||
}
|
|
@ -11,7 +11,7 @@ return [
|
|||
'failed_transaction' => 'Failed transaction',
|
||||
'user_phone_not_exist' => 'This customer number does not exist',
|
||||
'wallet_not_defined' => 'This recipient wallet code does not exist',
|
||||
'insufficient_balance' => 'The balance is insufficient to complete this transaction',
|
||||
'insufficient_balance' => 'The balance is insufficient to complete this transaction. You will need to charge :amount',
|
||||
'no_ilink_network' => 'Sorry, there is no iLink World network in your country',
|
||||
'wallet_country_not_match' => 'This recipient wallet code is not registered in the country :country',
|
||||
'no_bank_card_attached' => 'No bank card is attached to your account',
|
||||
|
@ -29,5 +29,6 @@ return [
|
|||
'cannot_subscribe_again' => "You can no longer subscribe to this insurance. You already have a request :state",
|
||||
"subscription_cannot_be_updated" => "This subscription request cannot be modified",
|
||||
"subscription_cannot_be_paid" => "This subscription request cannot be paid",
|
||||
'subscription_be_already_paid' => "This subscription has already been paid. You can just renew it"
|
||||
'subscription_be_already_paid' => "This subscription has already been paid. You can just renew it",
|
||||
"subscription_cannot_be_submitted" => "Your previous application is being validated. You cannot submit another one at this time",
|
||||
];
|
||||
|
|
|
@ -78,5 +78,16 @@ Your application is waiting for more information.
|
|||
- Number of beneficiaries : :number_of_beneficiaries
|
||||
|
||||
Message: :reason",
|
||||
'insurance_subscription_awaiting_more_information_notification' => "Your :subscription_id application is waiting for more information"
|
||||
'insurance_subscription_awaiting_more_information_notification' => "Your :subscription_id application is waiting for more information",
|
||||
'insurance_addition_beneficiary_successful' => "Adding a beneficiary to your insurance successful",
|
||||
'insurance_addition_beneficiary' => "Adding a beneficiary to your insurance",
|
||||
'insurance_addition_beneficiary_mail' => ":gender :name ,
|
||||
|
||||
Your request to add a beneficiary to your insurance is being validated.
|
||||
Application information :
|
||||
- ID : :subscription_id
|
||||
- Name of the insurance: :insurance_name
|
||||
- Premium amount: :bonus_amount
|
||||
- Number of beneficiaries : :number_of_beneficiaries
|
||||
",
|
||||
];
|
||||
|
|
|
@ -12,5 +12,6 @@ return [
|
|||
"F" => "Mrs",
|
||||
"AWAITING_FURTHER_INFORMATION" => "AWAITING FURTHER INFORMATION",
|
||||
"ENDED" => 'ENDED',
|
||||
"REMAINS" => "REMAINS"
|
||||
"ADDITION_OF_BENEFICIARY" => "ADDITION OF BENEFICIARY",
|
||||
"DELETION_OF_BENEFICIARY" => "DELETION OF BENEFICIARY"
|
||||
];
|
||||
|
|
|
@ -11,7 +11,7 @@ return [
|
|||
'failed_transaction' => 'Transaction échouée',
|
||||
'user_phone_not_exist' => 'Ce numéro client n\'existe pas',
|
||||
'wallet_not_defined' => 'Ce code wallet destinataire n\'existe pas',
|
||||
'insufficient_balance' => 'Le solde est insuffisant pour effectuer cette transaction',
|
||||
'insufficient_balance' => 'Le solde est insuffisant pour effectuer cette transaction. Vous devrez recharger :amount',
|
||||
'no_ilink_network' => 'Désolé , il n\'existe pas de reseau iLink World dans votre pays',
|
||||
'wallet_country_not_match' => 'Ce code wallet destinataire n\'est pas enregistré dans le pays :country',
|
||||
'no_bank_card_attached' => 'Aucune carte bancaire n\'est rattachée à votre compte',
|
||||
|
@ -29,5 +29,6 @@ return [
|
|||
'cannot_subscribe_again' => "Vous ne pouvez plus souscrire à cette assurance. Vous avez déjà une demande :state",
|
||||
"subscription_cannot_be_updated" => "Cette demande de souscription ne peut etre modifiée",
|
||||
"subscription_cannot_be_paid" => "Cette demande de souscription ne peut etre payée",
|
||||
'subscription_be_already_paid' => "Cette souscription a déjà été payée. Vous pouvez juste la renouveler"
|
||||
'subscription_be_already_paid' => "Cette souscription a déjà été payée. Vous pouvez juste la renouveler",
|
||||
"subscription_cannot_be_submitted" => "Votre demande précedente est en cours de validation. Vous ne pouvez pas en soumettre une autre pour l'instant",
|
||||
];
|
||||
|
|
|
@ -79,4 +79,15 @@ Votre demande de souscription est en attente de plus d'informations.
|
|||
|
||||
Message : :reason",
|
||||
'insurance_subscription_awaiting_more_information_notification' => "Votre demande de souscription :subscription_id est en attente de plus d'informations.",
|
||||
'insurance_addition_beneficiary_successful' => "Ajout d'ayant droit à votre assurance réussie",
|
||||
'insurance_addition_beneficiary' => "Ajout d'ayant droit à votre assurance",
|
||||
'insurance_addition_beneficiary_mail' => ":gender :name ,
|
||||
|
||||
Votre demande d'ajout d'ayant droit à votre assurance est en cours de validation.
|
||||
Informations de la demande :
|
||||
- ID : :subscription_id
|
||||
- Nom de l'assurance : :insurance_name
|
||||
- Montant de la prime : :bonus_amount
|
||||
- Nombre d'ayants droit : :number_of_beneficiaries
|
||||
",
|
||||
];
|
||||
|
|
|
@ -12,5 +12,6 @@ return [
|
|||
"F" => "Mme",
|
||||
"AWAITING_FURTHER_INFORMATION" => "EN ATTENTE D'INFORMATIONS COMPLÉMENTAIRES",
|
||||
"ENDED" => 'TERMINÉE',
|
||||
"REMAINS" => "EN RESTE"
|
||||
"ADDITION_OF_BENEFICIARY" => "AJOUT D'AYANT DROIT",
|
||||
"DELETION_OF_BENEFICIARY" => "SUPPRESSION D'AYANT DROIT"
|
||||
];
|
||||
|
|
Loading…
Reference in New Issue