2022-03-29 19:20:43 +00:00
< ? php
namespace App\Http\Controllers ;
use App\Events\InsuranceEvent ;
use App\InsuranceAction ;
use App\InsuranceInvoiceState ;
use App\InsuranceState ;
use App\InsuranceSubscriptionAffiliation ;
use App\InsuranceSubscriptionState ;
use App\Models\AgentPlus ;
use App\Models\CountriesCurrency ;
use App\Models\Identification ;
use App\Models\NhHavingRight ;
use App\Models\NhInsurance ;
use App\Models\NhInsurancesHavingRight ;
use App\Models\NhInsurancesInvoice ;
use App\Models\NhInsurancesPayment ;
use App\Models\NhInsurancesSubscription ;
use App\Models\NhInsurancesSubscriptionsHistory ;
use App\Models\NhMonthsPricesGrid ;
use App\Models\NhNetworksConfig ;
2022-10-08 23:58:41 +00:00
use App\Models\PaymentAggregator ;
use App\Models\PaymentTransaction ;
2022-03-29 19:20:43 +00:00
use App\Models\User ;
use App\Models\Wallet ;
2022-04-14 14:21:57 +00:00
use App\Rules\PasswordValidation ;
2022-03-29 19:20:43 +00:00
use App\Traits\Helper ;
use Carbon\Carbon ;
use DateTime ;
2022-10-08 23:58:41 +00:00
use GuzzleHttp\Client ;
2022-03-29 19:20:43 +00:00
use Illuminate\Database\Eloquent\Collection ;
use Illuminate\Http\Request ;
use Illuminate\Http\UploadedFile ;
use Illuminate\Support\Facades\DB ;
use Illuminate\Support\Facades\Event ;
use Illuminate\Support\Facades\Log ;
use Throwable ;
class InsuranceInvoiceController extends Controller
{
/**
* @ OA\Get (
* path = " /insurances/invoices " ,
* summary = " Afficher la liste des factures " ,
* tags = { " Factures de l'assurance " },
* security = {{ " api_key " : {}}},
* @ OA\Parameter (
* parameter = " user_id " ,
* name = " user_id " ,
* description = " ID de l'utilisateur " ,
* in = " query " ,
* required = true ,
* @ OA\Schema (
* type = " integer " ,
* default = 325
* )
* ),
* @ OA\Parameter (
* parameter = " state " ,
* name = " state " ,
* description = " Etat de la facture " ,
* in = " query " ,
* required = false ,
* @ OA\Schema (
* type = " string " ,
2022-04-01 16:10:44 +00:00
* enum = { " PAID " , " UNPAID " }
2022-03-29 19:20:43 +00:00
* )
* ),
* @ OA\Parameter (
* parameter = " page " ,
* name = " page " ,
* description = " Page " ,
* in = " query " ,
* required = false ,
* @ OA\Schema (
* type = " integer "
* )
* ),
* @ OA\Parameter (
* parameter = " perPage " ,
* name = " perPage " ,
* description = " Pas de pagination " ,
* in = " query " ,
* required = false ,
* @ OA\Schema (
* type = " integer "
* )
* ),
* @ OA\Parameter (
* parameter = " pagination " ,
* name = " pagination " ,
* description = " pagination " ,
* in = " query " ,
* required = false ,
* @ OA\Schema (
* type = " boolean " ,
* )
* ),
* @ OA\Response (
* response = 200 ,
* description = " OK " ,
* @ OA\JsonContent (
* ref = " #/components/schemas/ApiResponse " ,
* example = {
* " status " : 200 ,
2022-05-02 07:03:42 +00:00
* " response " : {{ " id " : 1 , " invoice_id " : " SFSF6565656 " , " insurance_id " : 5 , " subscription_id " : 2 , " amount " : " 495 \ u202f000 FCFA " , " next_payment_deadline " : " 2022-04-08 17:31:52 " , " next_payment_reminder " : " 2022-04-05 17:31:52 " , " state " : " NON PAY \ u00c9E " ,
2022-05-04 10:52:01 +00:00
* " reason " : " ACTIVATION DE L'ASSURANCE " , " created_at " : " 2022-03-29T16:31:52.000000Z " , " updated_at " : " 2022-03-29T16:31:52.000000Z " , " insurance " : { " id " : 5 , " network_id " : 250 , " user_id " : 349 , " insured_id " : " 5DSTKZ7PQZX4 " , " remaining_amount " : " 0 FCFA " ,
" paid_amount " : " 495 000 FCFA " , " months_grid_id " : 77 , " bonus_amount " : " 150000.00 " , " number_of_beneficiaries " : 2 , " total_bonus_amount " : " 495000.00 " , " insurance_coverage_amount " : " 0.00 " , " start_at " : null , " end_at " : null , " state " : " UNDER_ACTIVATION " ,
2022-03-29 19:20:43 +00:00
* " deadlines " : 1 , " amount_last_payment " : " 495000.00 " , " amount_per_split " : " 495000.00 " , " paid_deadlines " : 0 , " created_at " : " 2022-03-29T16:31:52.000000Z " , " updated_at " : " 2022-03-29T16:31:52.000000Z " },
* " subscription " : { " id " : 2 , " insurance_subscription_id " : " CX36UNA2VVOP " , " network_id " : 250 , " user_id " : 349 , " months_grid_id " : 77 , " bonus_amount " : " 150000.00 " , " number_of_beneficiaries " : 2 , "
2022-05-04 10:52:01 +00:00
* total_bonus_amount " : " 495000.00 " , " state " : " ACCEPTED " , " insurance_action " : " ACTIVATION " , " created_at " : " 2021 - 11 - 10 T16 : 52 : 32.000000 Z " , " updated_at " : " 2022 - 03 - 29 T17 : 31 : 52.000000 Z " , " reason " :null}, " payments " : { { " id " : 8, " payment_id " : " RRS63SV7VP " , " invoice_id " : 7,
* " amount " : " 200 000 FCFA " , " created_at " : " 2022-04-29T16:09:59.000000Z " , " updated_at " : " 2022-04-29T16:09:59.000000Z " }}}},
2022-03-29 19:20:43 +00:00
* " error " : null
* }
* )
* )
* )
*/
public function getInvoices ( Request $request )
{
$this -> validate ( $request , [
'user_id' => 'required|integer|exists:users,id' ,
2022-04-01 16:10:44 +00:00
'state' => 'nullable|in:PAID,UNPAID' ,
2022-03-29 19:20:43 +00:00
'pagination' => 'nullable|boolean'
]);
$user_id = $request -> input ( 'user_id' );
$user = User :: findOrFail ( $user_id );
$currency_code = $user -> network -> country -> currency_code ;
$pagination = $request -> input ( 'pagination' );
$state = $request -> input ( 'state' );
2022-05-04 10:52:01 +00:00
// $datetime = $this->getCurrentTimeByCountryCode($user->network->country->code_country);
2022-03-29 19:20:43 +00:00
$query = NhInsurancesInvoice :: with ([ 'insurance' , 'subscription' ])
-> whereHas ( 'insurance' , function ( $q ) use ( $user_id ) {
return $q -> where ( 'user_id' , $user_id );
});
if ( ! empty ( $state )) {
2022-04-01 16:10:44 +00:00
// if ($state == 'TO_PAID') {
// $query = $query->where('state', InsuranceInvoiceState::UNPAID)
2022-05-02 07:03:42 +00:00
// ->where('next_payment_deadline', '>=', $datetime);
2022-04-01 16:10:44 +00:00
// } else {
$query = $query -> where ( 'state' , $state );
// }
2022-05-04 10:52:01 +00:00
} else {
2022-05-11 09:46:01 +00:00
$query = $query -> with ([ 'payments' => function ( $q ) {
return $q -> orderBy ( 'created_at' , 'DESC' );
}]);
2022-03-29 19:20:43 +00:00
}
2022-05-10 07:33:23 +00:00
$query = $query -> orderBy ( 'created_at' , 'DESC' );
2022-03-29 19:20:43 +00:00
if ( $pagination ) {
$invoices = $query -> paginate ( $request -> input ( 'perPage' , 10 ));
} else {
$invoices = $query -> get ();
}
$array = $pagination ? $invoices -> items () : $invoices ;
2022-05-11 10:14:23 +00:00
foreach ( $array as $i => $invoice ) {
if ( $i == 0 ) {
$invoice -> insurance -> state = trans ( 'states.' . $invoice -> insurance -> state );
}
2022-05-04 10:52:01 +00:00
$paid_amount = floatval ( $invoice -> paid_amount_non_formatted );
2022-03-29 19:20:43 +00:00
$invoice -> state = trans ( 'states.' . $invoice -> state );
$invoice -> reason = trans ( 'states.' . $invoice -> reason );
2022-05-04 10:52:01 +00:00
$invoice -> remaining_amount = $this -> toMoneyWithCurrencyCode ( $invoice -> amount - $paid_amount , $currency_code );
$invoice -> paid_amount = $this -> toMoneyWithCurrencyCode ( $paid_amount , $currency_code );
2022-03-29 19:20:43 +00:00
$invoice -> amount = $this -> toMoneyWithCurrencyCode ( $invoice -> amount , $currency_code );
2022-05-04 10:52:01 +00:00
if ( empty ( $state )) {
foreach ( $invoice -> payments as $payment ) {
$payment -> amount = $this -> toMoneyWithCurrencyCode ( $payment -> amount , $currency_code );
}
}
2022-03-29 19:20:43 +00:00
}
return $this -> successResponse ( $invoices );
}
/**
* @ OA\Put (
* path = " /insurances/invoices/ { id}/pay " ,
* summary = " Payer la facture de l'assurance " ,
* tags = { " Factures de l'assurance " },
* security = {{ " api_key " : {}}},
* @ OA\Parameter (
* parameter = " id " ,
* name = " id " ,
* description = " ID de la facture " ,
* in = " path " ,
* required = true ,
* @ OA\Schema (
* type = " integer " ,
* default = 12
* )
* ),
* @ OA\RequestBody (
* description = " Corps de la requete " ,
* required = true ,
* @ OA\MediaType (
* mediaType = " application/json " ,
* @ OA\Schema (
* @ OA\Property ( property = " password " ,
* type = " string " ,
* example = " addfdf21 " ,
* description = " Mot de passe de l'utilisateur "
2022-05-02 07:03:42 +00:00
* ),
* @ OA\Property ( property = " amount " ,
* type = " number " ,
* example = 32450 ,
* description = " Montant à payer "
2022-05-05 09:02:30 +00:00
* ),
* @ OA\Property ( property = " payment_method " ,
* type = " string " ,
2022-10-08 23:58:41 +00:00
* enum = { " wallet " , " mobile_money " , " card " , " ALL " },
2022-05-05 09:02:30 +00:00
* example = " wallet " ,
* description = " Methode de paiement "
2022-10-08 23:58:41 +00:00
* ),
* @ OA\Property ( property = " payment_phone " ,
* type = " string " ,
* example = " +237690716639 " ,
* description = " Numero de telephone avec lequel on souhaite payer de paiement "
* ),
* @ OA\Property ( property = " payment_transaction_id " ,
* type = " string " ,
* example = " ASMFOSDFFDODF " ,
* description = " ID de la transaction de paiement "
* ),
* @ OA\Property ( property = " payment_token " ,
* type = " string " ,
* example = " 133232669895665656 " ,
* description = " Token la transaction de paiement "
2022-03-29 19:20:43 +00:00
* )
* ),
2022-05-05 09:02:30 +00:00
* example = { " password " : " adbc1215448 " , " amount " : 50000 , " payment_method " : " wallet " }
2022-03-29 19:20:43 +00:00
* )
* ),
* @ OA\Response (
* response = 200 ,
* description = " OK " ,
* @ OA\JsonContent (
* ref = " #/components/schemas/ApiResponse " ,
* example = { " status " : 200 , " response " : " Transaction réussie " , " error " : null }
* )
* )
* )
* @ throws \App\Exceptions\AppException
*/
public function payInvoice ( $id , Request $request )
{
2022-05-02 07:03:42 +00:00
$this -> validate ( $request , [
'password' => 'required|string' ,
2022-05-05 09:02:30 +00:00
'amount' => 'required|numeric|min:0' ,
2022-10-08 23:58:41 +00:00
'payment_method' => 'required|string' ,
'payment_phone' => 'required_unless:payment_method,wallet|string' ,
'payment_transaction_id' => 'nullable|exists:payment_transactions,transaction_id' , // A envoyer apres avoir effectuer le paiement
'payment_token' => 'required_with:payment_transaction_id|string' , // A envoyer apres avoir effectuer le paiement
'otp' => 'nullable|string'
2022-05-02 07:03:42 +00:00
]);
2022-03-29 19:20:43 +00:00
2022-10-08 23:58:41 +00:00
$transaction_id = $request -> input ( 'payment_transaction_id' );
2022-05-02 07:03:42 +00:00
$amountToPaid = $request -> input ( 'amount' );
2022-05-05 09:02:30 +00:00
$payment_method = $request -> input ( 'payment_method' );
2022-03-29 19:20:43 +00:00
$invoice = NhInsurancesInvoice :: findOrFail ( $id );
$datetime = $this -> getCurrentTimeByCountryCode ( $invoice -> insurance -> network -> country -> code_country );
if ( $invoice -> state == InsuranceInvoiceState :: PAID ) {
return $this -> errorResponse ( trans ( 'errors.invoice_already_paid' ));
}
2022-05-02 07:03:42 +00:00
$networkConfig = NhNetworksConfig :: where ( 'network_id' , $invoice -> insurance -> network_id ) -> first ();
if ( ! isset ( $networkConfig ) || $networkConfig -> configWallet -> type != 'ilink_sante' ) {
return $this -> errorResponse ( trans ( 'errors.nano_health_not_activated' ));
}
2022-03-29 19:20:43 +00:00
$user = $invoice -> insurance -> user ;
2022-04-14 14:21:57 +00:00
$this -> userIdentificationVerification ( $user );
// Validation du mot de passe dépendamment de la configuration du réseau
2022-05-02 07:03:42 +00:00
// $this->validate($request, [
// 'password' => [new PasswordValidation($invoice->insurance->network_id, 'user', $user)]
// ]);
2022-03-29 19:20:43 +00:00
$currency = $this -> getNetworkCurrency ( $invoice -> insurance -> network_id );
2022-05-02 07:03:42 +00:00
$total_paid_amount = NhInsurancesPayment :: where ( 'invoice_id' , $id ) -> sum ( 'amount' );
$total_remains_amount = $invoice -> amount - $total_paid_amount ;
if ( $amountToPaid > $total_remains_amount ) {
return $this -> errorResponse ( " Le montant maximum à payer est de " . $this -> toMoneyWithCurrencyCode ( $total_remains_amount , $currency ));
}
if ( $amountToPaid < $invoice -> amount_per_split && $amountToPaid < $total_remains_amount ) {
if ( $total_remains_amount < $invoice -> amount_per_split ) {
return $this -> errorResponse ( trans ( 'errors.minimum amount_to_paid' , [ 'amount' => $this -> toMoneyWithCurrencyCode ( $total_remains_amount , $currency )]));
}
return $this -> errorResponse ( trans ( 'errors.minimum amount_to_paid' , [ 'amount' => $this -> toMoneyWithCurrencyCode ( $invoice -> amount_per_split , $currency )]));
}
2022-03-29 19:20:43 +00:00
2022-10-08 23:58:41 +00:00
if ( empty ( $transaction_id )) {
if ( $payment_method == 'wallet' ) {
if ( $user -> wallet -> balance < $amountToPaid ) {
$remains_amount = $amountToPaid - $user -> wallet -> balance ;
return $this -> errorResponse ( trans ( 'errors.insufficient_balance' , [ 'amount' => $this -> toMoneyWithCurrencyCode ( $remains_amount , $currency )]));
}
} else {
$aggregator = PaymentAggregator :: where ( 'status' , 1 ) -> first ();
if ( ! $aggregator ) {
return $this -> errorResponse ( trans ( 'errors.payment_not_available' ));
}
}
} else {
$transaction = PaymentTransaction :: where ( 'transaction_id' , $transaction_id ) -> where ( 'payment_token' , $request -> input ( 'payment_token' )) -> where ( 'state' , 'ACCEPTED' ) -> first ();
if ( ! $transaction ) {
return $this -> errorResponse ( trans ( 'errors.payment_not_found' ), 404 );
}
$payment = NhInsurancesPayment :: where ( 'payment_id' , $transaction_id ) -> first ();
if ( $payment ) {
return $this -> errorResponse ( trans ( 'errors.payment_invalid' ), 400 );
}
$amountToPaid = $transaction -> amount ;
2022-03-29 19:20:43 +00:00
}
try {
DB :: beginTransaction ();
$hyperviseur = AgentPlus :: where ( 'category' , 'hyper' ) -> where ( 'network_id' , $invoice -> insurance -> network_id ) -> firstOrFail ();
$walletHyperviseur = Wallet :: where ( 'id_networkAgent' , $hyperviseur -> network_agent_id ) -> firstOrFail ();
$walletHyperviseur -> balance_princ += $amountToPaid ;
$walletHyperviseur -> save ();
$user -> balance_nano_health += $amountToPaid ;
$user -> save ();
2022-10-08 23:58:41 +00:00
if ( empty ( $transaction_id )) {
if ( $payment_method == 'wallet' ) {
$user -> wallet -> balance -= $amountToPaid ;
$user -> wallet -> save ();
} else {
// Pay through payment service
$client = new Client ([
'base_uri' => config ( 'variable.payment_service_url' ),
'headers' => [
'Authorization' => config ( 'variable.payment_service_key' ),
]
]);
$paymentResponse = $client -> post ( '/pay' , [
'json' => [
'aggregator_id' => $aggregator -> id ,
'amount' => $amountToPaid ,
'currency' => $user -> network -> country -> currency_code ,
'customer_id' => $user -> id ,
'customer_email' => $user -> email ,
'customer_name' => $user -> lastname ,
'customer_surname' => $user -> lastname ,
'customer_phone_number' => $request -> input ( 'payment_phone' ),
'customer_address' => $user -> adresse ,
'customer_country' => $user -> network -> country -> code_country ,
'customer_city' => $user -> adresse ,
'customer_state' => $user -> network -> country -> code_country ,
'customer_zip_code' => '00237' ,
'payment_method' => $request -> input ( 'payment_method' ),
2022-10-09 00:25:17 +00:00
'reason' => trans ( 'messages.insurance_invoice' ),
2022-10-08 23:58:41 +00:00
'otp' => $request -> input ( 'otp' )
],
'http_errors' => false
]);
$responseData = json_decode ( $paymentResponse -> getBody () -> getContents ());
$responseCode = $paymentResponse -> getStatusCode ();
if ( $responseCode == 200 ) {
// $transaction_id = $responseData->response->transaction_id;
return $this -> successResponse ( $responseData -> response );
} else {
return $this -> errorResponse ( $responseData -> error ? ? trans ( 'errors.unexpected_error' ), $responseCode );
}
}
}
2022-05-02 07:03:42 +00:00
$payment = NhInsurancesPayment :: create ([
2022-10-08 23:58:41 +00:00
'payment_id' => $transaction_id ,
2022-05-02 07:03:42 +00:00
'invoice_id' => $invoice -> id ,
'amount' => $amountToPaid
]);
2022-05-04 10:52:01 +00:00
// Calcul de l'équivalence en nombre d'échéance
if (( $total_remains_amount - $amountToPaid ) == 0 ) {
$invoice -> paid_deadlines = $invoice -> deadlines ;
$invoice -> next_payment_deadlines_to_paid = 0 ;
} else {
$paid_deadlines = $amountToPaid < $invoice -> amount_per_split ? 1 : intval ( $amountToPaid / $invoice -> amount_per_split );
$invoice -> paid_deadlines += $paid_deadlines ;
$invoice -> next_payment_deadlines_to_paid -= $paid_deadlines ;
}
2022-05-02 07:03:42 +00:00
if ( $invoice -> next_payment_deadlines_to_paid < 0 ) {
$invoice -> next_payment_deadlines_to_paid = 0 ;
}
if ( $invoice -> paid_deadlines == $invoice -> deadlines ) {
2022-03-29 19:20:43 +00:00
$invoice -> insurance -> state = InsuranceState :: PAID ;
2022-05-02 07:03:42 +00:00
$invoice -> state = InsuranceInvoiceState :: PAID ;
2022-03-29 19:20:43 +00:00
$isPartialPayment = false ;
2022-03-31 11:57:44 +00:00
} else {
2022-05-02 07:03:42 +00:00
if ( $invoice -> next_payment_deadlines_to_paid == 0 ) {
2022-04-01 16:10:44 +00:00
$invoice -> insurance -> state = InsuranceState :: PARTIALLY_PAID ;
}
2022-05-02 07:03:42 +00:00
$deadline = $invoice -> next_payment_deadline ;
$invoice -> next_payment_deadline = $this -> addDaysToDateTime ( $deadline , $networkConfig -> reminder_delay_days + $networkConfig -> suspension_delay_days_after_reminder ); // 1 semaines + 3 jours
$invoice -> next_payment_reminder = $this -> addDaysToDateTime ( $deadline , $networkConfig -> reminder_delay_days );
2022-03-31 11:57:44 +00:00
$isPartialPayment = true ;
2022-03-29 19:20:43 +00:00
}
2022-03-31 11:57:44 +00:00
// Si c'est le 1er paiement
2022-05-09 11:04:35 +00:00
$total_payments = NhInsurancesPayment :: where ( 'invoice_id' , $id ) -> count ();
if ( $total_payments == 1 ) {
2022-03-29 19:20:43 +00:00
if ( $invoice -> reason == InsuranceAction :: ADDITION_OF_BENEFICIARY ) {
$invoice -> insurance -> bonus_amount = $invoice -> subscription -> bonus_amount ;
$invoice -> insurance -> total_bonus_amount += $invoice -> subscription -> total_bonus_amount ;
$invoice -> insurance -> number_of_beneficiaries += $invoice -> subscription -> number_of_beneficiaries ;
$invoice -> insurance -> updated_at = $datetime ;
$invoice -> insurance -> save ();
foreach ( $invoice -> subscription -> beneficiaries as $b ) {
NhInsurancesHavingRight :: create ([
'insurance_id' => $invoice -> insurance -> id ,
'having_right_id' => $b -> id
]);
}
}
if ( in_array ( $invoice -> reason , [ InsuranceAction :: ACTIVATION , InsuranceAction :: RENEWAL ])) {
if ( empty ( $invoice -> insurance -> monthsGrid -> waiting_period_days )) {
$start_at = $datetime ;
} else {
2022-03-31 11:57:44 +00:00
$start_at = $this -> addDaysToDateTime ( $datetime , $invoice -> insurance -> monthsGrid -> waiting_period_days ) -> format ( 'Y-m-d H:i:s' );
2022-03-29 19:20:43 +00:00
}
2022-03-31 11:57:44 +00:00
$end_at = $this -> addMonthsToDateTime ( $start_at , $invoice -> insurance -> monthsGrid -> number_of_months );
2022-03-29 19:20:43 +00:00
$invoice -> insurance -> start_at = $start_at ;
$invoice -> insurance -> end_at = $end_at ;
}
}
2022-05-02 07:03:42 +00:00
$invoice -> updated_at = $datetime ;
$invoice -> insurance -> updated_at = $datetime ;
2022-03-29 19:20:43 +00:00
$invoice -> insurance -> save ();
2022-05-02 07:03:42 +00:00
$invoice -> save ();
2022-03-29 19:20:43 +00:00
Event :: dispatch ( new InsuranceEvent ( $invoice -> insurance , $isPartialPayment ? trans ( 'messages.insurance_partially_paid' ) : trans ( 'messages.insurance_subscription_paid' ),
trans ( 'messages.insurance_paid_mail' , [ 'name' => $invoice -> insurance -> user -> lastname , 'insured_id' => $invoice -> insurance -> insured_id ,
'bonus_amount' => $this -> toMoneyWithCurrencyCode ( $invoice -> insurance -> bonus_amount , $currency ), 'total_bonus_amount' => $this -> toMoneyWithCurrencyCode ( $invoice -> insurance -> total_bonus_amount , $currency ), 'number_of_beneficiaries' => $invoice -> insurance -> number_of_beneficiaries ,
'gender' => trans ( 'states.' . $invoice -> insurance -> user -> identification -> gender ), 'insurance_name' => $invoice -> insurance -> network -> name , 'months' => $invoice -> insurance -> monthsGrid -> number_of_months , 'invoice_id' => $invoice -> invoice_id ,
2022-05-04 10:52:01 +00:00
'amount' => $this -> toMoneyWithCurrencyCode ( $amountToPaid , $currency ), 'paid_amount' => $this -> toMoneyWithCurrencyCode ( $total_paid_amount + $amountToPaid , $currency ), 'remains_amount' => $this -> toMoneyWithCurrencyCode ( $total_remains_amount - $amountToPaid , $currency ),
2022-03-31 11:57:44 +00:00
'payment_period' => trans ( 'states.' . $invoice -> insurance -> monthsGrid -> payment_period ), 'reason' => trans ( 'states.' . $invoice -> reason ), 'title' => $isPartialPayment ? trans ( 'messages.insurance_partially_paid_title' ) : trans ( 'messages.insurance_fully_paid_title' ),
'waiting_days' => empty ( $invoice -> insurance -> monthsGrid -> waiting_period_days ) ? trans ( 'messages.none' ) : trans ( 'messages.n_days' , [ 'n' => $invoice -> insurance -> monthsGrid -> waiting_period_days ]),
2022-05-02 07:03:42 +00:00
'start_at' => $invoice -> insurance -> start_at , 'payment_id' => $payment -> payment_id
2022-03-31 11:57:44 +00:00
])));
2022-03-29 19:20:43 +00:00
DB :: commit ();
2022-05-04 15:57:05 +00:00
return $this -> successResponse ( trans ( 'messages.insurance_invoice_paid' , [ 'amount' => $this -> toMoneyWithCurrencyCode ( $amountToPaid , $currency )]));
2022-03-29 19:20:43 +00:00
} catch ( Throwable $e ) {
Log :: error ( $e -> getMessage () . '\n' . $e -> getTraceAsString ());
DB :: rollBack ();
return $this -> errorResponse ( trans ( 'errors.unexpected_error' ), 500 );
}
}
2022-03-30 10:32:40 +00:00
2022-05-02 07:03:42 +00:00
// public function generateInvoices()
// {
// try {
// DB::beginTransaction();
// $this->generateInsurancesInvoices();
// DB::commit();
// return $this->successResponse("Success");
// } catch (\Throwable $t) {
// DB::rollBack();
// Log::error('-------- Insurances Invoices expired insurance-----------');
// Log::error($t->getMessage() . " :\n" . $t->getTraceAsString());
// return $this->errorResponse("Error");
// }
// }
2022-03-30 10:32:40 +00:00
public function reminderInvoices ()
{
try {
$this -> reminderInsurancesInvoices ();
2022-04-01 16:10:44 +00:00
return $this -> successResponse ( " Success " );
2022-03-30 10:32:40 +00:00
} catch ( \Throwable $t ) {
DB :: rollBack ();
Log :: error ( '-------- Insurances Invoices expired insurance-----------' );
Log :: error ( $t -> getMessage () . " : \n " . $t -> getTraceAsString ());
return $this -> errorResponse ( " Error " );
}
}
2022-03-29 19:20:43 +00:00
}