2020-08-03 07:42:28 +00:00
< ? php
namespace App\Http\Controllers ;
2020-08-18 14:10:03 +00:00
use App\Models\AgentPlus ;
2020-08-03 07:42:28 +00:00
use App\Models\ConfigWallet ;
2020-08-19 18:46:52 +00:00
use App\Models\Identification ;
2020-08-03 07:42:28 +00:00
use App\Models\NetworksAgent ;
2020-09-28 15:39:57 +00:00
use App\Models\User ;
2020-08-14 16:14:49 +00:00
use App\Models\UsersDemandesCredit ;
2020-08-31 13:54:10 +00:00
use App\Models\UsersEpargne ;
2020-08-03 07:42:28 +00:00
use App\Models\UsersGroup ;
2020-08-24 16:29:50 +00:00
use App\Models\UsersGroupsDemandesValidation ;
2020-08-03 07:42:28 +00:00
use App\Models\Wallet ;
2020-08-18 14:10:03 +00:00
use App\Models\WalletAgent ;
2020-08-06 06:21:41 +00:00
use App\Models\WalletsUser ;
2020-08-03 07:42:28 +00:00
use App\Traits\ApiResponser ;
use App\Traits\Helper ;
use Illuminate\Http\Request ;
use Illuminate\Support\Facades\DB ;
class NanoCreditController extends Controller
{
use ApiResponser ;
use Helper ;
/**
* Create a new controller instance .
*
* @ return void
*/
public function __construct ()
{
//
}
2020-08-14 16:14:49 +00:00
// Gestion du nano credit
public function askNanoCredit ( Request $request )
{
$this -> validate ( $request , [
'type_caution' => 'required|in:groupe,individuel' ,
'duree_mois' => 'required|integer|min:0|not_in:0' ,
'id_user' => 'required|integer|min:0|not_in:0' ,
'password' => 'required' ,
'montant' => 'required|numeric|min:0|not_in:0' ,
]);
$user = User :: findOrFail ( $request -> id_user );
if ( ! $this -> checkPassword ( $request -> password , $user -> encrypted_password , $user -> salt ))
return $this -> errorResponse ( trans ( 'messages.incorrect_user_password' ));
2020-08-19 18:46:52 +00:00
$identfication = $this -> checkMyIdentification ( $request -> id_user );
if ( ! ( $identfication instanceof Identification ))
return $identfication ;
2020-08-14 16:14:49 +00:00
$init_country = $user -> network -> country -> id ;
$result = ConfigWallet :: join ( 'networks' , 'networks.id' , '=' , 'configWallet.id_network' )
-> where ( 'networks.country_id' , $init_country ) -> where ( 'configWallet.type' , 'ilink' )
-> select ( 'configWallet.id' ) -> first ();
if ( $result ) {
$config = ConfigWallet :: findOrFail ( $result -> id );
} else {
return $this -> errorResponse ( trans ( 'errors.no_ilink_network' ));
}
2020-08-26 18:31:51 +00:00
if ( ! $config -> has_nano_credit )
return $this -> errorResponse ( trans ( 'errors.nano_credit_not_available' ));
2020-08-14 16:14:49 +00:00
$taxes = array_values ( array_filter ( $config -> taxes -> all (), function ( $tax ) {
return $tax -> categorie == 'nano_credit' ;
}));
2020-08-26 18:31:51 +00:00
$paliers = array_values ( array_filter ( $config -> paliers_config_nano_credits -> all (), function ( $taux ) {
return $taux -> type == 'nano_credit' ;
}));
2020-08-14 16:14:49 +00:00
$demande_credit = new UsersDemandesCredit ();
$demande_credit -> fill ( $request -> all ());
2020-08-26 18:31:51 +00:00
$demande_credit -> id_network = $config -> id_network ;
2020-08-14 16:14:49 +00:00
2020-08-18 14:10:03 +00:00
// Derniere demande de nano credit
$last_demand = UsersDemandesCredit :: where ( 'id_user' , $request -> id_user ) -> where ( 'etat' , 'VALIDE' ) -> first ();
if ( $last_demand )
return $this -> errorResponse ( trans ( 'errors.last_nano_credit_not_refunded' ));
2020-08-14 16:14:49 +00:00
if ( $request -> type_caution == 'groupe' ) {
$group = UsersGroup :: find ( $user -> group_id );
if ( ! $group )
return $this -> errorResponse ( trans ( 'errors.not_belongs_to_any_group' ));
if ( ! $group -> actif )
return $this -> errorResponse ( trans ( 'errors.your_group_not_active' ));
if ( $group -> nombre_utilisateurs < 10 )
return $this -> errorResponse ( trans ( 'errors.your_group_not_valid' ));
2020-08-31 13:54:10 +00:00
if ( $request -> montant > $group -> limite_credit || $request -> montant < $config -> limite_credit_min )
return $this -> errorResponse ( trans ( 'errors.nano_credit_amount_must_be_between_the_group_limit' ,
[ 'min_limit' => $this -> toMoney ( $config -> limite_credit_min , $init_country ), 'max_limit' => $this -> toMoney ( $group -> limite_credit , $init_country )]));
2020-08-14 16:14:49 +00:00
2020-08-24 16:29:50 +00:00
//Verifier la capacité d'emprunt
$CE = $this -> capaciteEmprunt ( $group -> id );
if ( $CE < 0 )
2020-08-31 13:54:10 +00:00
return $this -> errorResponse ( trans ( 'errors.group_not_allow_to_borrow' ));
2020-08-24 16:29:50 +00:00
if ( $request -> montant > $CE )
2020-08-31 13:54:10 +00:00
return $this -> errorResponse ( trans ( 'errors.borrowing_capacity_exceeded' ));
2020-08-14 16:14:49 +00:00
2020-08-24 16:29:50 +00:00
//Verifier si le solde principal de l'hyperviseur est superieur au montant
$walletHyper = WalletAgent :: where ( 'category' , 'hyper' ) -> where ( 'network_id' , $config -> id_network ) -> firstOrFail ();
2020-08-14 16:14:49 +00:00
2020-08-31 13:54:10 +00:00
$demande_credit -> interet = $this -> calculateFees ( $paliers , $request -> montant , $request -> duree_mois );
$demande_credit -> taxe = $this -> calculateTax ( $taxes , $demande_credit -> interet );
2020-08-14 16:14:49 +00:00
$demande_credit -> id_demande = $this -> getNanoCreditDemandID ();
2020-08-24 16:29:50 +00:00
if ( $request -> montant > $walletHyper -> balance_princ ) {
$demande_credit -> etat = 'EN_ATTENTE_DE_VALIDATION' ;
$demande_credit -> save ();
$demande = new UsersGroupsDemandesValidation ();
$demande -> id_group = $group -> id ;
$demande -> id_user = $user -> id ;
$demande -> id_demande = $demande_credit -> id_demande ;
$demande -> id_agent = $walletHyper -> agent_id ;
$demande -> date_creation = new \DateTime ();
$demande -> type = 'nano_credit' ;
$demande -> save ();
$data = new \stdClass ();
$data -> screen = " notificationview " ;
2020-08-31 13:54:10 +00:00
$demande -> statut = false ;
2020-08-24 16:29:50 +00:00
$data -> data = new \stdClass ();
$data -> data -> id = $demande -> id ;
$this -> sendPushNotificationToAgent ( $walletHyper -> codeMembre ,
trans ( 'notifications.group_nano_credit_request' , [ 'name' => $group -> nom ]), $data );
return $this -> successResponse ( trans ( 'messages.successful_nano_credit_sent_to_hypervisor' ));
} else {
$walletHyper = Wallet :: findOrFail ( $walletHyper -> wallet_id );
$walletUser = WalletsUser :: where ( 'idUser' , $request -> id_user ) -> firstOrFail ();
2020-10-05 16:32:31 +00:00
$demande_credit -> date_validation = new \DateTime ();
$demande_credit -> date_remboursement_prevu = $demande_credit -> date_validation -> modify ( '+' . $request -> duree_mois . ' month' );
2020-08-24 16:29:50 +00:00
$demande_credit -> etat = 'VALIDE' ;
2020-08-31 13:54:10 +00:00
$montant_total = $demande_credit -> montant + $demande_credit -> interet + $demande_credit -> taxe ;
2020-08-24 16:29:50 +00:00
$user -> balance_credit += $montant_total ;
$walletUser -> balance += $demande_credit -> montant ;
$walletHyper -> balance_princ -= $demande_credit -> montant ;
$user -> save ();
$walletUser -> save ();
$walletHyper -> save ();
$demande_credit -> save ();
$message = trans ( 'messages.successful_user_group_nano_credit_demand' ,
2020-08-31 13:54:10 +00:00
[ 'id_demand' => $demande_credit -> id_demande , 'amount' => $this -> toMoney ( $montant_total , $init_country ), 'duration' => $demande_credit -> duree_mois ,
2020-09-09 07:30:15 +00:00
'net' => $this -> toMoney ( $demande_credit -> montant , $init_country ), 'fees' => $this -> toMoney ( $demande_credit -> interet , $init_country ),
'date' => $demande_credit -> date_remboursement_prevu , 'tax' => $this -> toMoney ( $demande_credit -> taxe , $init_country )]);
2020-08-24 16:29:50 +00:00
$this -> sendMail ( $user -> email , trans ( 'messages.successful_nano_credit_demand' ), $message );
return $this -> successResponse ( $message . trans ( 'messages.sent_by_mail' ));
}
2020-08-14 16:14:49 +00:00
}
if ( $request -> type_caution == 'individuel' ) {
2020-08-31 13:54:10 +00:00
$demande_credit -> interet = $this -> calculateFees ( $paliers , $request -> montant , $request -> duree_mois );
$demande_credit -> taxe = $this -> calculateTax ( $taxes , $demande_credit -> interet );
2020-08-14 16:14:49 +00:00
$demande_credit -> etat = 'EN_ATTENTE_DE_VALIDATION' ;
2020-08-31 13:54:10 +00:00
$montant_total = $demande_credit -> montant + $demande_credit -> interet + $demande_credit -> taxe ;
2020-08-14 16:14:49 +00:00
$demande_credit -> id_demande = $this -> getNanoCreditDemandID ();
$demande_credit -> save ();
$message = trans ( 'messages.successful_user_individual_nano_credit_demand' ,
[ 'id_demand' => $demande_credit -> id_demande , 'amount' => $this -> toMoney ( $montant_total , $init_country ),
2020-08-31 13:54:10 +00:00
'net' => $this -> toMoney ( $demande_credit -> montant , $init_country ), 'duration' => $demande_credit -> duree_mois ,
2020-09-09 07:30:15 +00:00
'fees' => $this -> toMoney ( $demande_credit -> interet , $init_country ),
'tax' => $this -> toMoney ( $demande_credit -> taxe , $init_country )]);
2020-08-14 16:14:49 +00:00
$this -> sendMail ( $user -> email , trans ( 'messages.successful_nano_credit_demand' ), $message );
return $this -> successResponse ( $message . trans ( 'messages.sent_by_mail' ));
}
}
2020-10-02 15:55:56 +00:00
// Demandes de nano credit et d'epargnes
public function getAllNanoCreditsDemands ( $id_user , Request $request )
{
$demandes = DB :: select ( " SELECT 'N' as type_historique , i.* FROM infos_users_demandes_credits i WHERE id_user = :id ; " , [ 'id' => $id_user ]);
$savings = DB :: select ( " SELECT 'E' as type_historique , i.* FROM infos_users_epargnes i WHERE id_user = :id; " , [ 'id' => $id_user ]);
2020-10-05 16:32:31 +00:00
// Supprimer les underscore sur les etats
$merge = array_map ( function ( $demand ) {
$demand -> etat = str_replace ( '_' , ' ' , $demand -> etat );
return $demand ;
}, array_merge ( $demandes , $savings ));
2020-10-02 15:55:56 +00:00
2020-10-05 16:32:31 +00:00
usort ( $merge , array ( $this , 'sortFunction' )); // Trier le tout par date
2020-10-02 15:55:56 +00:00
2020-10-07 09:07:11 +00:00
// return $this->successResponse($this->arrayPaginator($merge, $request));
return $this -> successResponse ( $merge );
2020-10-02 15:55:56 +00:00
}
2020-08-27 17:29:25 +00:00
public function getNanoCreditsDemands ( $id_user )
{
2020-10-02 15:55:56 +00:00
$demandes = DB :: select ( 'SELECT * FROM infos_users_demandes_credits WHERE id_user = :id ORDER BY date_creation DESC;' , [ 'id' => $id_user ]);
2020-10-05 16:32:31 +00:00
$result = array_map ( function ( $demand ) {
$demand -> etat = str_replace ( '_' , ' ' , $demand -> etat );
return $demand ;
}, $demandes );
return $this -> successResponse ( $result );
2020-10-02 15:55:56 +00:00
}
// Demandes de credits en cours pour le remboursement
public function getNanoCreditsDemandsInProgress ( $id_user )
{
$demandes = DB :: select ( " SELECT * FROM infos_users_demandes_credits WHERE id_user = :id AND etat = 'VALIDE' ORDER BY date_creation DESC; " , [ 'id' => $id_user ]);
2020-10-05 16:32:31 +00:00
$result = array_map ( function ( $demand ) {
$demand -> etat = str_replace ( '_' , ' ' , $demand -> etat );
return $demand ;
}, $demandes );
return $this -> successResponse ( $result );
2020-10-02 15:55:56 +00:00
}
public function getGuaranteeNanoCreditsDemands ( $id_wallet_agent )
{
$demandes = DB :: select ( 'SELECT * FROM infos_users_demandes_credits WHERE id_wallet_agent = :id ORDER BY date_creation DESC;'
, [ 'id' => $id_wallet_agent ]);
2020-10-05 16:32:31 +00:00
$result = array_map ( function ( $demand ) {
$demand -> etat = str_replace ( '_' , ' ' , $demand -> etat );
return $demand ;
}, $demandes );
return $this -> successResponse ( $result );
2020-08-27 17:29:25 +00:00
}
2020-09-09 07:30:15 +00:00
public function getSavingsDemands ( $id_user )
{
2020-10-02 15:55:56 +00:00
$savings = DB :: select ( 'SELECT * FROM infos_users_epargnes WHERE id_user = :id ORDER BY date_creation DESC;' , [ 'id' => $id_user ]);
2020-10-05 16:32:31 +00:00
$result = array_map ( function ( $demand ) {
$demand -> etat = str_replace ( '_' , ' ' , $demand -> etat );
return $demand ;
}, $savings );
return $this -> successResponse ( $result );
2020-10-02 15:55:56 +00:00
}
// Demandes d'epargnes pour la cassation
public function getSavingsDemandsInProgress ( $id_user )
{
$savings = DB :: select ( " SELECT * FROM infos_users_epargnes WHERE id_user = :id AND etat = 'EN_COURS' ORDER BY date_creation DESC; " , [ 'id' => $id_user ]);
2020-10-05 16:32:31 +00:00
$result = array_map ( function ( $demand ) {
$demand -> etat = str_replace ( '_' , ' ' , $demand -> etat );
return $demand ;
}, $savings );
return $this -> successResponse ( $result );
2020-09-09 07:30:15 +00:00
}
2020-08-31 13:54:10 +00:00
// Cautionner une demande de credit
public function guaranteeCredit ( Request $request )
2020-08-18 14:10:03 +00:00
{
$this -> validate ( $request , [
'id_demande' => 'required' ,
'id_wallet_agent' => 'required|integer|min:0|not_in:0' ,
2020-08-24 16:29:50 +00:00
'retrait_cash' => 'required|boolean' ,
2020-08-18 14:10:03 +00:00
'password' => 'required' ,
]);
$walletAgent = Wallet :: findOrFail ( $request -> get ( 'id_wallet_agent' ));
$network_agent = NetworksAgent :: findOrFail ( $walletAgent -> id_networkAgent );
$agent_country = $network_agent -> network -> country -> id ;
$agent = AgentPlus :: findOrFail ( $network_agent -> agent_id );
if ( ! $this -> checkPassword ( $request -> password , $agent -> encrypted_password , $agent -> salt ))
return $this -> errorResponse ( trans ( 'messages.incorrect_user_password' ));
$demande_credit = UsersDemandesCredit :: where ( 'id_demande' , $request -> id_demande ) -> first ();
2020-08-24 16:29:50 +00:00
$demande_credit -> retrait_cash = $request -> retrait_cash ;
2020-08-18 14:10:03 +00:00
if ( ! $demande_credit )
return $this -> errorResponse ( trans ( 'errors.nano_credit_not_found' ));
if ( $demande_credit -> type_caution != 'individuel' )
return $this -> errorResponse ( trans ( 'errors.nano_credit_not_individual' ));
if ( $demande_credit -> etat == 'VALIDE' )
return $this -> errorResponse ( trans ( 'messages.treated_demand' ));
$user = User :: findOrFail ( $demande_credit -> id_user );
$user_country = $user -> network -> country -> id ;
if ( $user_country != $agent_country )
2020-09-28 15:39:57 +00:00
return $this -> errorResponse ( trans ( 'errors.operation_cannot_performed_in_country' ));
2020-08-18 14:10:03 +00:00
2020-08-31 13:54:10 +00:00
$montant_total = $demande_credit -> montant + $demande_credit -> interet + $demande_credit -> taxe ;
2020-08-18 14:10:03 +00:00
if ( $montant_total > $walletAgent -> balance_princ )
return $this -> errorResponse ( trans ( 'errors.insufficient_balance' ));
2020-10-05 16:32:31 +00:00
$demande_credit -> date_validation = new \DateTime ();
$demande_credit -> date_remboursement_prevu = $demande_credit -> date_validation -> modify ( '+' . $demande_credit -> duree_mois . ' month' );
2020-08-18 14:10:03 +00:00
$demande_credit -> etat = 'VALIDE' ;
$demande_credit -> id_wallet_agent = $walletAgent -> id ;
$user -> balance_credit += $montant_total ;
$walletAgent -> balance_princ -= $montant_total ;
2020-08-24 16:29:50 +00:00
//Crediter le wallet de client s'il ne retire pas en cash
if ( ! $request -> retrait_cash ) {
2020-08-24 20:39:10 +00:00
$walletUser = WalletsUser :: where ( 'idUser' , $demande_credit -> id_user ) -> firstOrFail ();
2020-08-24 16:29:50 +00:00
$walletUser -> balance += $demande_credit -> montant ;
$walletUser -> save ();
}
2020-08-18 14:10:03 +00:00
$walletAgent -> save ();
$user -> save ();
$demande_credit -> save ();
$user_message = trans ( 'messages.successful_guarantee_user_individual_nano_credit_demand' ,
2020-08-31 13:54:10 +00:00
[ 'id_demand' => $demande_credit -> id_demande , 'amount' => $this -> toMoney ( $montant_total , $agent_country ), 'duration' => $demande_credit -> duree_mois ,
2020-09-09 07:30:15 +00:00
'net' => $this -> toMoney ( $demande_credit -> montant , $agent_country ), 'fees' => $this -> toMoney ( $demande_credit -> interet , $agent_country ),
'tax' => $this -> toMoney ( $demande_credit -> taxe , $agent_country ),
2020-08-18 14:10:03 +00:00
'date' => $demande_credit -> date_remboursement_prevu , 'agent_name' => $agent -> lastname . ' ' . $agent -> firstname , 'code_agent' => $agent -> code_membre ]);
$agent_message = trans ( 'messages.successful_guarantee_agent_individual_nano_credit_demand' ,
2020-08-31 13:54:10 +00:00
[ 'id_demand' => $demande_credit -> id_demande , 'amount' => $this -> toMoney ( $montant_total , $agent_country ), 'duration' => $demande_credit -> duree_mois ,
2020-09-09 07:30:15 +00:00
'net' => $this -> toMoney ( $demande_credit -> montant , $agent_country ), 'fees' => $this -> toMoney ( $demande_credit -> interet , $agent_country ),
'tax' => $this -> toMoney ( $demande_credit -> taxe , $agent_country ),
2020-08-18 14:10:03 +00:00
'date' => $demande_credit -> date_remboursement_prevu , 'user_name' => $user -> lastname . ' ' . $user -> firstname , 'code_user' => $user -> user_code ]);
$this -> sendMail ( $user -> email , trans ( 'messages.successful_guarantee_nano_credit_demand' ), $user_message );
$this -> sendMail ( $agent -> email , trans ( 'messages.successful_guarantee_nano_credit_demand' ), $agent_message );
return $this -> successResponse ( $agent_message . trans ( 'messages.sent_by_mail' ));
}
2020-08-31 13:54:10 +00:00
public function refundCredit ( Request $request )
2020-08-18 14:10:03 +00:00
{
$this -> validate ( $request , [
'id_demande' => 'required' ,
'id_user' => 'required|integer|min:0|not_in:0' ,
'password' => 'required' ,
]);
$user = User :: findOrFail ( $request -> id_user );
if ( ! $this -> checkPassword ( $request -> password , $user -> encrypted_password , $user -> salt ))
return $this -> errorResponse ( trans ( 'messages.incorrect_user_password' ));
$walletUser = WalletsUser :: where ( 'idUser' , $request -> id_user ) -> firstOrFail ();
$demande_credit = UsersDemandesCredit :: where ( 'id_demande' , $request -> id_demande ) -> first ();
if ( ! $demande_credit )
return $this -> errorResponse ( trans ( 'errors.nano_credit_not_found' ));
if ( $demande_credit -> etat == 'REMBOURSE' )
return $this -> errorResponse ( trans ( 'errors.nano_credit_already_refunded' ));
2020-10-02 15:55:56 +00:00
$message = $this -> refundNanoCredit ( $demande_credit , $user , $walletUser );
2020-08-18 14:10:03 +00:00
return $this -> successResponse ( $message . trans ( 'messages.sent_by_mail' ));
}
2020-08-24 16:29:50 +00:00
// Calculer la capacité d'emprunt
private function capaciteEmprunt ( $id_group )
{
$users = User :: where ( 'group_id' , $id_group ) -> get ();
$sommeCredits = 0 ;
$sommeEpargnes = 0 ;
foreach ( $users as $user ) {
$sommeCredits += $user -> balance_credit ;
$sommeEpargnes += $user -> balance_epargne ;
}
return $sommeEpargnes - $sommeCredits ;
}
2020-08-31 13:54:10 +00:00
// Recuperer les durees en mois
2020-09-09 07:30:15 +00:00
public function getDurations ( Request $request )
2020-08-26 18:31:51 +00:00
{
2020-09-09 07:30:15 +00:00
$this -> validate ( $request , [
'type' => 'required|in:nano_credit,epargne' ,
'id_user' => 'required|integer|min:0|not_in:0' ,
]);
$user = User :: findOrFail ( $request -> id_user );
2020-08-26 18:31:51 +00:00
$init_country = $user -> network -> country -> id ;
$result = ConfigWallet :: join ( 'networks' , 'networks.id' , '=' , 'configWallet.id_network' )
-> where ( 'networks.country_id' , $init_country ) -> where ( 'configWallet.type' , 'ilink' )
-> select ( 'configWallet.id' ) -> first ();
if ( $result ) {
$config = ConfigWallet :: findOrFail ( $result -> id );
} else {
return $this ->
errorResponse ( trans ( 'errors.no_ilink_network' ));
}
if ( ! $config -> has_nano_credit )
return $this -> errorResponse ( trans ( 'errors.nano_credit_not_available' ));
2020-09-09 07:30:15 +00:00
$paliers = array_values ( array_filter ( $config -> paliers_config_nano_credits -> all (), function ( $taux ) use ( $request ) {
return $taux -> type == $request -> type ;
2020-08-26 18:31:51 +00:00
}));
$mois = array_map ( function ( $palier ) {
2020-08-27 19:36:27 +00:00
return [ 'value' => $palier -> duree_mois ];
2020-08-26 18:31:51 +00:00
}, $paliers );
return $this -> successResponse ( $mois );
}
2020-08-31 13:54:10 +00:00
// Faire une epargne
public function makeSavings ( Request $request )
{
$this -> validate ( $request , [
'type' => 'required|in:simple,blocked' ,
'duree_mois' => 'required_if:type,blocked|integer|min:0|not_in:0' ,
'id_user' => 'required|integer|min:0|not_in:0' ,
'password' => 'required' ,
'montant' => 'required|numeric|min:0|not_in:0' ,
]);
$user = User :: findOrFail ( $request -> id_user );
if ( ! $this -> checkPassword ( $request -> password , $user -> encrypted_password , $user -> salt ))
return $this -> errorResponse ( trans ( 'messages.incorrect_user_password' ));
$init_country = $user -> network -> country -> id ;
$result = ConfigWallet :: join ( 'networks' , 'networks.id' , '=' , 'configWallet.id_network' )
-> where ( 'networks.country_id' , $init_country ) -> where ( 'configWallet.type' , 'ilink' )
-> select ( 'configWallet.id' ) -> first ();
if ( $result ) {
$config = ConfigWallet :: findOrFail ( $result -> id );
} else {
return $this -> errorResponse ( trans ( 'errors.no_ilink_network' ));
}
if ( ! $config -> has_nano_credit )
return $this -> errorResponse ( trans ( 'errors.nano_credit_not_available' ));
$saving = new UsersEpargne ();
$saving -> fill ( $request -> all ());
$saving -> id_network = $config -> id_network ;
$sumFees = 0 ;
$saving -> date_creation = new \DateTime ();
if ( $request -> type == 'blocked' ) {
$taxes = array_values ( array_filter ( $config -> taxes -> all (), function ( $tax ) {
return $tax -> categorie == 'epargne' ;
}));
$paliers = array_values ( array_filter ( $config -> paliers_config_nano_credits -> all (), function ( $taux ) {
return $taux -> type == 'epargne' ;
}));
$saving -> interet = $this -> calculateFees ( $paliers , $request -> montant , $request -> duree_mois );
$saving -> taxe = $this -> calculateTax ( $taxes , $saving -> interet );
$saving -> type = 'BLOQUE' ;
$saving -> date_fin = $saving -> date_creation -> modify ( '+' . $request -> duree_mois . ' month' );
} else {
$saving -> interet = 0 ;
$saving -> taxe = 0 ;
$saving -> type = 'SIMPLE' ;
}
$walletHyper = WalletAgent :: where ( 'category' , 'hyper' ) -> where ( 'network_id' , $config -> id_network ) -> firstOrFail ();
$walletHyper = Wallet :: findOrFail ( $walletHyper -> wallet_id );
$walletUser = WalletsUser :: where ( 'idUser' , $request -> id_user ) -> firstOrFail ();
$montant_total = $saving -> montant + $saving -> interet - $saving -> taxe ;
if ( $saving -> montant > $walletUser -> balance )
return $this -> errorResponse ( trans ( 'errors.insufficient_balance' ));
$saving -> etat = 'EN_COURS' ;
$walletUser -> balance -= $saving -> montant ;
$user -> balance_epargne += $montant_total ;
$walletHyper -> balance_com -= ( $saving -> interet - $saving -> taxe );
$saving -> id_epargne = $this -> getSavingID ();
$walletHyper -> save ();
$walletUser -> save ();
$user -> save ();
$saving -> save ();
$message = trans ( 'messages.successful_saving' ) . trans ( $request -> type == 'blocked' ?
'messages.successful_blocked_saving_details' : 'messages.successful_simple_saving_details' ,
[ 'id_saving' => $saving -> id_epargne , 'amount' => $this -> toMoney ( $montant_total , $init_country ), 'type' => $saving -> type ,
2020-09-09 07:30:15 +00:00
'net' => $this -> toMoney ( $saving -> montant , $init_country ), 'fees' => $this -> toMoney ( $saving -> interet , $init_country ),
'tax' => $this -> toMoney ( $saving -> taxe , $init_country ),
2020-08-31 13:54:10 +00:00
'date' => $saving -> date_fin , 'duration' => $saving -> duree_mois ]);
$this -> sendMail ( $user -> email , trans ( 'messages.successful_saving' ), $message );
return $this -> successResponse ( $message . trans ( 'messages.sent_by_mail' ));
}
// Casser une epargne
public function breakSavings ( Request $request )
{
$this -> validate ( $request , [
'id_epargne' => 'required' ,
'id_user' => 'required|integer|min:0|not_in:0' ,
]);
$saving = UsersEpargne :: where ( 'id_epargne' , $request -> id_epargne ) -> firstOrFail ();
if ( $saving -> id_user != $request -> id_user )
return $this -> errorResponse ( trans ( 'errors.not_authorized_to_process_request' ));
if ( $saving -> etat == 'CASSE' )
return $this -> errorResponse ( trans ( 'errors.savings_already_broken' ));
$user = User :: findOrFail ( $request -> id_user );
$init_country = $user -> network -> country -> id ;
$walletUser = WalletsUser :: where ( 'idUser' , $request -> id_user ) -> firstOrFail ();
if ( $saving -> type == 'BLOQUE' ) {
$montant_total = $saving -> montant + $saving -> interet - $saving -> taxe ;
} else {
$montant_total = $saving -> montant ;
}
$user -> balance_epargne -= $montant_total ;
$walletUser -> balance += $montant_total ;
$saving -> etat = 'CASSE' ;
$saving -> date_cassation = new \DateTime ();
$user -> save ();
$walletUser -> save ();
$saving -> save ();
$message = trans ( 'messages.successful_broken_saving' ) . trans ( $saving -> type == 'BLOQUE' ?
'messages.successful_blocked_saving_details' : 'messages.successful_simple_saving_details' ,
[ 'id_saving' => $saving -> id_epargne , 'amount' => $this -> toMoney ( $montant_total , $init_country ), 'type' => $saving -> type ,
2020-09-09 07:30:15 +00:00
'net' => $this -> toMoney ( $saving -> montant , $init_country ), 'fees' => $this -> toMoney ( $saving -> interet , $init_country ),
'tax' => $this -> toMoney ( $saving -> taxe , $init_country ),
2020-08-31 13:54:10 +00:00
'date' => $saving -> date_fin , 'duration' => $saving -> duree_mois ]);
$this -> sendMail ( $user -> email , trans ( 'messages.successful_broken_saving' ), $message );
return $this -> successResponse ( $message . trans ( 'messages.sent_by_mail' ));
}
public function getInfosNanoCredit ( $id_demand )
{
$demand = UsersDemandesCredit :: where ( 'id_demande' , $id_demand ) -> first ();
if ( $demand ) {
2020-10-05 16:32:31 +00:00
$demand -> etat = str_replace ( '_' , ' ' , $demand -> etat );
2020-08-31 13:54:10 +00:00
return $this -> successResponse ( $demand );
} else {
return $this -> errorResponse ( trans ( 'errors.nano_credit_not_found' ));
}
}
public function getInfosSavings ( $id_saving )
{
$demand = UsersEpargne :: where ( 'id_epargne' , $id_saving ) -> first ();
if ( $demand ) {
2020-10-05 16:32:31 +00:00
$demand -> etat = str_replace ( '_' , ' ' , $demand -> etat );
2020-08-31 13:54:10 +00:00
return $this -> successResponse ( $demand );
} else {
return $this -> errorResponse ( trans ( 'errors.savings_not_found' ));
}
}
2020-09-11 16:28:33 +00:00
public function getNanoCreditAccount ( $id_user ){
$user = User :: findOrFail ( $id_user );
$data = new \stdClass ();
$data -> balance_credit = $user -> balance_credit ;
$data -> balance_epargne = $user -> balance_epargne ;
return $this -> successResponse ( $data );
}
2020-08-31 13:54:10 +00:00
// Calculer les interet
2020-08-14 16:14:49 +00:00
private function calculateFees ( array $paliers , $montant , $duree )
{
$size = sizeof ( $paliers );
if ( $size > 0 ) {
$palier = null ;
foreach ( $paliers as $p ) {
if ( $p -> duree_mois == $duree ) {
$palier = $p ;
break ;
}
}
if ( $palier ) {
return $palier -> taux * $montant / 100 ;
}
}
return 0 ;
}
private function getNanoCreditDemandID ()
{
do {
$code = $this -> generateGroupCode ();
$result = collect ( DB :: select ( 'SELECT * FROM users_demandes_credits WHERE id_demande = :code' , [ 'code' => $code ]));
$codeCorrect = sizeof ( $result ) < 0 ;
} while ( $codeCorrect );
return $code ;
}
2020-08-31 13:54:10 +00:00
private function getSavingID ()
{
do {
$code = $this -> generateGroupCode ();
$result = collect ( DB :: select ( 'SELECT * FROM users_epargnes WHERE id_epargne = :code' , [ 'code' => $code ]));
$codeCorrect = sizeof ( $result ) < 0 ;
} while ( $codeCorrect );
return $code ;
}
2020-08-03 07:42:28 +00:00
}