2020-06-19 13:48:27 +00:00
< ? php
namespace App\Http\Controllers ;
2021-10-04 11:01:26 +00:00
use App\Exports\RefundAmountExport ;
2020-09-28 15:39:57 +00:00
use App\Jobs\TestJob ;
2020-10-14 17:27:52 +00:00
use App\Models\AgentPlus ;
2020-06-24 15:49:41 +00:00
use App\Models\ConfigWallet ;
2020-06-21 21:49:24 +00:00
use App\Models\Country ;
2020-07-04 10:48:06 +00:00
use App\Models\NetworksAgent ;
2020-06-25 16:54:46 +00:00
use App\Models\User ;
2020-07-04 10:48:06 +00:00
use App\Models\Wallet ;
2021-10-03 08:58:41 +00:00
use App\Models\WalletAgent ;
2020-06-24 15:49:41 +00:00
use App\Models\WalletsUser ;
2021-10-03 08:58:41 +00:00
use App\Models\WalletTransaction ;
2020-06-24 15:49:41 +00:00
use Illuminate\Http\Request ;
2020-06-19 13:48:27 +00:00
use Illuminate\Support\Facades\DB ;
2022-01-24 11:04:40 +00:00
use Illuminate\Support\Facades\Log ;
use Illuminate\Support\Facades\Mail ;
2021-10-03 08:58:41 +00:00
use Maatwebsite\Excel\Facades\Excel ;
2020-06-19 13:48:27 +00:00
class HelperController extends Controller
{
/**
* Create a new controller instance .
*
* @ return void
*/
public function __construct ()
{
//
}
2020-06-28 10:45:17 +00:00
public function countries ()
2020-06-19 13:48:27 +00:00
{
2020-06-23 08:29:48 +00:00
$countries = DB :: select ( ' SELECT id , name , code_dial , code_country FROM countries_currencies WHERE id IN (
2020-06-23 05:36:12 +00:00
SELECT distinct c . id FROM networks n INNER JOIN countries_currencies c ON n . country_id = c . id INNER JOIN configWallet cw ON cw . id_network = n . id WHERE status = 1
); ' );
2020-06-19 13:48:27 +00:00
return $this -> successResponse ( $countries );
}
2020-06-21 21:49:24 +00:00
2020-06-24 15:49:41 +00:00
public function paying_networks ( Request $request )
2020-06-21 21:49:24 +00:00
{
2020-06-24 15:49:41 +00:00
$this -> validate ( $request ,[
'id_country' => 'required|integer|min:0|not_in:0' ,
2020-07-04 10:48:06 +00:00
'id_wallet_agent' => 'required_without:id_wallet_user|integer|min:0|not_in:0' ,
'id_wallet_user' => 'required_without:id_wallet_agent|integer|min:0|not_in:0' ,
2020-06-24 15:49:41 +00:00
]);
2020-07-04 10:48:06 +00:00
if ( isset ( $request -> id_wallet_agent )) {
$walletAgent = Wallet :: findOrFail ( $request -> get ( 'id_wallet_agent' ));
$network_agent = NetworksAgent :: findOrFail ( $walletAgent -> id_networkAgent );
$init_country = $network_agent -> network -> country -> id ;
// Configuratrion du wallet
$config = ConfigWallet :: where ( 'id_network' , $network_agent -> network_id ) -> firstOrFail ();
} elseif ( isset ( $request -> id_wallet_user )) {
$walletUser = WalletsUser :: findOrFail ( $request -> id_wallet_user );
$init_country = $walletUser -> 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-06-24 15:49:41 +00:00
}
2020-07-04 10:48:06 +00:00
2020-06-24 15:49:41 +00:00
$networks = DB :: select ( ' SELECT n . id , n . name , c . type FROM networks n INNER JOIN configWallet c ON c . id_network = n . id WHERE n . id
IN ( SELECT distinct id_network FROM paying_networks WHERE id_configWallet = : id_config )
AND status = 1 AND country_id = : id_country ; ',[' id_country '=>$request->id_country, ' id_config ' => $config -> id ]);
2020-06-23 16:21:26 +00:00
foreach ( $networks as $network ){
2020-06-23 08:29:48 +00:00
if ( $network -> type == 'ilink' )
$network -> type = 'ilink-world' ;
}
2020-06-28 10:45:17 +00:00
if ( $request -> id_country == $init_country ){
$currentNetwork = new \stdClass ();
$currentNetwork -> id = $config -> network -> id ;
$currentNetwork -> name = $config -> network -> name ;
$currentNetwork -> type = 'ilink-world' ;
array_unshift ( $networks , $currentNetwork );
}
2020-06-21 21:49:24 +00:00
return $this -> successResponse ( $networks );
}
2020-07-04 10:20:26 +00:00
public function other_paying_networks ( Request $request )
{
$this -> validate ( $request ,[
'id_country' => 'required|integer|min:0|not_in:0' ,
2020-07-04 10:48:06 +00:00
'id_wallet_agent' => 'required|integer|min:0|not_in:0' ,
2020-07-04 10:20:26 +00:00
]);
2020-07-04 10:48:06 +00:00
$walletAgent = Wallet :: findOrFail ( $request -> get ( 'id_wallet_agent' ));
$network_agent = NetworksAgent :: findOrFail ( $walletAgent -> id_networkAgent );
// Configuratrion du wallet
$config = ConfigWallet :: where ( 'id_network' , $network_agent -> network_id ) -> firstOrFail ();
2020-07-04 10:20:26 +00:00
$networks = DB :: select ( ' SELECT n . id , n . name , c . type FROM networks n INNER JOIN configWallet c ON c . id_network = n . id WHERE n . id
IN ( SELECT distinct id_network FROM paying_networks WHERE id_configWallet = : id_config )
2020-07-04 10:48:06 +00:00
AND status = 1 AND c . type <> \ 'ilink\' AND country_id = :id_country;' ,[ 'id_country' => $request -> id_country , 'id_config' => $config -> id ]);
2020-07-04 10:20:26 +00:00
return $this -> successResponse ( $networks );
}
2020-06-21 21:49:24 +00:00
public function country ( $code_dial ){
return $this -> successResponse ( Country :: where ( 'code_dial' , $code_dial ) -> firstOrFail ());
}
2020-06-25 16:54:46 +00:00
public function init (){
//Mettre a jour tous les utilisateurs qui n'ont pas de wallet iLink
$users = User :: whereNull ( 'user_code' ) -> orWhere ( 'user_code' , '' ) -> get ();
2020-09-28 15:39:57 +00:00
foreach ( $users as $user ) {
do {
$user_code = $this -> generateUserCode ();
$result = collect ( DB :: select ( 'SELECT * FROM users WHERE user_code = :code' , [ 'code' => $user_code ]));
$codeCorrect = sizeof ( $result ) < 0 ;
} while ( $codeCorrect );
2020-06-25 16:54:46 +00:00
$user -> user_code = $user_code ;
2020-09-28 15:39:57 +00:00
$wallet = WalletsUser :: where ( 'idUser' , $user -> id ) -> first ();
if ( ! $wallet ) {
DB :: insert ( 'INSERT INTO wallets_users (idUser) VALUES (?);' , [ $user -> id ]);
$user -> save ();
}
2020-06-25 16:54:46 +00:00
}
return $this -> successResponse ( 'OK :-) , Have a nice day dear ! ' );
}
2020-10-14 17:27:52 +00:00
private function generateUserCode ( $length = 10 )
{
2020-06-25 16:54:46 +00:00
$characters = '23456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ' ;
$charactersLength = strlen ( $characters );
$randomString = '' ;
for ( $i = 0 ; $i < $length ; $i ++ ) {
$randomString .= $characters [ rand ( 0 , $charactersLength - 1 )];
}
return $randomString ;
}
2020-10-14 17:27:52 +00:00
// Retourne tous les codes membres d'un agent à partir d'un code
public function agentCodes ( $agent_code )
{
$agent = AgentPlus :: where ( 'code_membre' , $agent_code ) -> first ();
$codes = [];
if ( $agent ) {
$codes = DB :: select ( " SELECT cg.code_membre as code_membre from networks_agents na INNER JOIN codeGenerer cg
ON cg . id = na . codeGenerer_id INNER JOIN networks ne ON ne . id = na . network_id WHERE na . agent_id = : id " , ['id' => $agent->id ]);
}
return $this -> successResponse ( $codes );
}
2021-10-03 08:58:41 +00:00
2021-10-04 11:01:26 +00:00
public function calculateTransactionAmount ()
2021-10-03 08:58:41 +00:00
{
$data = [[ 'ID Wallet' , 'Agent' , 'Contact' , 'Role' , 'Parrain' , 'Nombre de transactions' , 'Montant à rembourser' ]];
// Calcul du montant a prelever pour tous les agents loc du reseau iLink World
$config = ConfigWallet :: where ( 'id_network' , 237 ) -> first ();
if ( isset ( $config )) {
$walletAgents = WalletAgent :: where ( 'network_id' , 237 ) -> orderBy ( 'category' ) -> orderBy ( 'lastname' , 'ASC' ) -> get ();
foreach ( $walletAgents as $wallet ) {
$refund = 0 ;
$totalTransactions = 0 ;
if ( $wallet -> category == 'geolocated' ) {
$totalTransactions = WalletTransaction :: where ( 'id_wallet' , $wallet -> wallet_id ) -> where ( 'type' , 'credit' )
-> where ( 'canceled' , '0' ) -> where ( 'deleted' , 0 ) -> count ();
// Montant à rembourser
$refund = $totalTransactions * $config -> frais_min_banque_depot * $config -> taux_com_ag_depot / 100 ;
} elseif ( $wallet -> category == 'super' ) {
$totalTransactions = WalletTransaction :: where ( 'id_wallet_sup' , $wallet -> wallet_id ) -> where ( 'type' , 'credit' )
-> where ( 'canceled' , '0' ) -> where ( 'deleted' , 0 ) -> count ();
// Montant à rembourser
$refund = $totalTransactions * $config -> frais_min_banque_depot * $config -> taux_com_sup_depot / 100 ;
} elseif ( $wallet -> category == 'hyper' ) {
$totalTransactions = WalletTransaction :: where ( 'id_wallet_hyp' , $wallet -> wallet_id ) -> where ( 'type' , 'credit' )
-> where ( 'canceled' , '0' ) -> where ( 'deleted' , 0 ) -> count ();
// Montant à rembourser
$refund = $totalTransactions * $config -> frais_min_banque_depot * ( 100 - $config -> taux_com_ag_depot - $config -> taux_com_sup_depot ) / 100 ;
}
array_push ( $data , [ $wallet -> wallet_id , $wallet -> lastname , substr ( $wallet -> transactionNumber , 4 ), $wallet -> category , $wallet -> parrain ,
$totalTransactions , $refund ]);
}
}
2021-10-04 11:01:26 +00:00
$export = new RefundAmountExport ( $data );
2021-10-03 08:58:41 +00:00
return Excel :: download ( $export , 'refund.xlsx' );
}
2021-10-04 11:01:26 +00:00
2021-10-20 16:09:29 +00:00
// public function fixFSServicesAmounts()
// {
// $data = Excel::toArray(new RefundAmountImport, storage_path('refund.xlsx'));
// $result = [];
// foreach ($data[0] as $key => $row) {
// if ($key > 0) {
// array_push($result, $row);
// }
// }
//
// try {
// DB::beginTransaction();
// // Fix amount
// foreach ($result as $r) {
// if (isset($r[6])) {
// if ($r[3] == 'hyper') continue;
// $refund = $r[3] == 'geolocated' ? 50 * $r[5] : 25 * $r[5];
// $wallet = Wallet::find($r[0]);
// $wallet->balance_com += $refund;
// $wallet->save();
// }
// }
// DB::commit();
// return $this->successResponse(trans('messages.success_treated_demand'));
// } catch (Throwable $exception) {
// DB::rollBack();
// Log::error($exception->getMessage());
// return $this->errorResponse(trans('errors.unexpected_error'));
// }
// }
2022-01-24 11:04:40 +00:00
public function notifyNewUser ( Request $request )
{
// Notifier le nouvel agent inscrit
$this -> validate ( $request , [
'subject' => 'required|string' ,
'email' => 'required|string' ,
'category' => 'required_if:user_type,agent|string' ,
'message' => 'required|string' ,
'user_id' => 'required|integer' ,
'user_type' => 'required|in:user,agent'
]);
$subject = $request -> input ( 'subject' );
$email = $request -> input ( 'email' );
$message = $request -> input ( 'message' );
$category = $request -> input ( 'category' );
$user_id = $request -> input ( 'user_id' );
$user_type = $request -> input ( 'user_type' );
try {
//Check if the directory already exists.
$qr_code_pdf = null ;
if ( empty ( $category ) || $category == 'geolocated' ) {
$qr_code_pdf = $this -> generateQRCode ( $user_id , $user_type );
}
$recipients = [ preg_replace ( " / \ s+/ " , " " , $email )]; // Supprimer les espaces dans le mail
Mail :: mailer ( 'smtp' ) -> raw ( $message , function ( $message ) use ( $recipients , $subject , $qr_code_pdf ) {
$message -> subject ( $subject )
-> to ( $recipients );
if ( ! empty ( $qr_code_pdf )) {
$message -> attachData ( $qr_code_pdf -> output (), 'qr_code.pdf' );
}
});
} catch ( \Throwable $t ) {
Log :: error ( '-------- Mail not sent -----------' );
Log :: error ( $t -> getMessage () . " : \n " . $t -> getTraceAsString ());
}
}
2022-03-01 17:28:36 +00:00
/**
* @ OA\Post (
* path = " /agents " ,
* summary = " Rechercher un agent " ,
* tags = { " Agents " },
* security = {{ " api_key " : {}}},
* @ OA\RequestBody (
* description = " Corps de la requete " ,
* required = true ,
* @ OA\MediaType (
* mediaType = " application/json " ,
* @ OA\Schema (
* @ OA\Property ( property = " dial_code " ,
* type = " string " ,
* description = " Dial code du pays " ,
* example = " +237 " ,
* ),
* @ OA\Property ( property = " name " ,
* type = " string " ,
* example = " tom " ,
* description = " Nom de l'agent "
* ),
* @ OA\Property ( property = " phone " ,
* type = " string " ,
* example = " 0452366562 " ,
* description = " Numero de telephone "
* ),
* @ OA\Property ( property = " id " ,
* type = " integer " ,
* example = 12 ,
* description = " ID de l'agent "
* )
* )
* )
* ),
* @ OA\Response (
* response = 200 ,
* description = " OK " ,
* @ OA\JsonContent (
* ref = " #/components/schemas/ApiResponse " ,
* example = {
* " status " : 200 ,
* " response " : " Votre requête de rattachement de votre compte bancaire a été prise en compte, vous recevrez un mail de confirmation dès lors que la banque aura validé votre code IBAN " ,
* " error " : null
* }
* )
* )
* )
*/
// Option de recherche des agents geolocalisés
public function getAgents ( Request $request )
{
$this -> validate ( $request , [
'name' => 'string' ,
'phone' => 'string' ,
'id' => 'integer' ,
'dial_code' => 'required|string|min:2'
]);
$name = $request -> input ( 'name' );
$phone = $request -> input ( 'phone' );
$id = $request -> input ( 'id' );
$dial_code = $request -> input ( 'dial_code' );
2022-04-12 10:01:21 +00:00
// $perPage = current(DB::select("SELECT valeur_int FROM adminConfig WHERE cle = 'pas_chargement' LIMIT 1"))->valeur_int ?? 10;
2022-03-01 17:28:36 +00:00
$query = AgentPlus :: where ( 'category' , 'geolocated' ) -> where ( 'code_dial' , $dial_code ) -> where ( 'etat' , 1 ) -> distinct ();
if ( ! empty ( $name )) {
$query = $query -> where ( 'lastname' , 'like' , '%' . $name . '%' ) -> orWhere ( 'firstname' , 'like' , '%' . $name . '%' );
}
if ( ! empty ( $phone )) {
$query = $query -> where ( 'phone' , 'like' , '%' . $phone . '%' ) -> orWhere ( 'transactionNumber' , 'like' , '%' . $phone . '%' );
}
if ( ! empty ( $id )) {
$query = $query -> where ( 'id' , $id );
}
2022-04-12 10:03:11 +00:00
$agents = $query -> selectRaw ( 'id , firstname, lastname, adresse , email , longitude , latitude , phone , transactionNumber , openHours , closeHours , solde , network, country' ) -> get (); //->paginate($perPage);
2022-03-01 17:28:36 +00:00
return $this -> successResponse ( $agents );
}
2020-06-19 13:48:27 +00:00
}