walletservice/app/Http/Controllers/HelperController.php

93 lines
3.5 KiB
PHP
Executable File

<?php
namespace App\Http\Controllers;
use App\Models\ConfigWallet;
use App\Models\Country;
use App\Models\User;
use App\Models\WalletsUser;
use App\Traits\ApiResponser;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class HelperController extends Controller
{
use ApiResponser;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
//
}
public function countries($id_wallet_user)
{
$walletUser = WalletsUser::findOrFail($id_wallet_user);
$init_country = $walletUser->user->network->country->id;
$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_user' => 'required|integer|min:0|not_in:0',
]);
$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';
}
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;
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;
}
}