walletservice/app/Http/Controllers/HelperController.php

225 lines
9.5 KiB
PHP
Executable File

<?php
namespace App\Http\Controllers;
use App\Exports\RefundAmountExport;
use App\Imports\RefundAmountImport;
use App\Jobs\TestJob;
use App\Models\AgentPlus;
use App\Models\ConfigWallet;
use App\Models\Country;
use App\Models\NetworksAgent;
use App\Models\User;
use App\Models\Wallet;
use App\Models\WalletAgent;
use App\Models\WalletsUser;
use App\Models\WalletTransaction;
use App\Traits\ApiResponser;
use App\Traits\Helper;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Facades\Excel;
use Throwable;
class HelperController extends Controller
{
use ApiResponser;
use Helper;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
//
}
public function countries()
{
$countries = DB::select('SELECT id , name , code_dial , code_country FROM countries_currencies WHERE id IN (
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
);');
return $this->successResponse($countries);
}
public function paying_networks(Request $request)
{
$this->validate($request,[
'id_country' => 'required|integer|min:0|not_in:0',
'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',
]);
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'));
}
}
$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]);
foreach ($networks as $network){
if($network->type == 'ilink')
$network->type = 'ilink-world';
}
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);
}
return $this->successResponse($networks);
}
public function other_paying_networks(Request $request)
{
$this->validate($request,[
'id_country' => 'required|integer|min:0|not_in:0',
'id_wallet_agent' => 'required|integer|min:0|not_in:0',
]);
$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();
$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 c.type <> \'ilink\' AND country_id = :id_country;',['id_country'=>$request->id_country, 'id_config'=> $config->id]);
return $this->successResponse($networks);
}
public function country($code_dial){
return $this->successResponse(Country::where('code_dial',$code_dial)->firstOrFail());
}
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();
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);
$user->user_code = $user_code;
$wallet = WalletsUser::where('idUser', $user->id)->first();
if (!$wallet) {
DB::insert('INSERT INTO wallets_users (idUser) VALUES (?);', [$user->id]);
$user->save();
}
}
return $this->successResponse('OK :-) , Have a nice day dear ! ');
}
private function generateUserCode($length = 10)
{
$characters = '23456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
// 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);
}
public function calculateTransactionAmount()
{
$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]);
}
}
$export = new RefundAmountExport($data);
return Excel::download($export, 'refund.xlsx');
}
public function fixTransactionAmount()
{
$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])) {
$wallet = Wallet::find($r[0]);
$wallet->balance_com -= $r[6];
$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'));
}
}
}