98 lines
3.8 KiB
PHP
Executable File
98 lines
3.8 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Wallet;
|
|
use App\Traits\ApiResponser;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Response;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class WalletController extends Controller
|
|
{
|
|
use ApiResponser;
|
|
|
|
/**
|
|
* Create a new controller instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
//
|
|
}
|
|
|
|
public function activated($id_agent)
|
|
{
|
|
$networks = DB::select('SELECT ne.name as network , countries.name AS country, w.id, w.balance_princ , w.balance_com, w.created_date, na.id AS id_networkAgent , cw.taux_com_client_depot , na.id AS id_networkAgent , cg.category FROM agents ag
|
|
INNER JOIN networks_agents na ON ag.id=na.agent_id INNER JOIN codeGenerer cg ON cg.id=na.codeGenerer_id INNER JOIN networks ne ON ne.id=na.network_id INNER JOIN configWallet cw ON ne.id = cw.id_network
|
|
INNER JOIN countries ON ne.country_id=countries.id LEFT JOIN wallets w ON na.id = w.id_networkAgent WHERE ag.id= :id AND network_id IN (
|
|
SELECT networks.id FROM networks LEFT JOIN configWallet ON configWallet.id_network = networks.id WHERE status = 1 AND id_network IS NOT NULL)', ['id' => $id_agent]);
|
|
|
|
// Create wallet if is not exist
|
|
$category = null;
|
|
if($networks){
|
|
$reload = false;
|
|
$id = $id_agent;
|
|
foreach ($networks as $network){
|
|
$category = $network->category;
|
|
// Create wallet if is not exist
|
|
if(!$network->id){
|
|
DB::insert('INSERT INTO wallets (id_networkAgent) VALUES (?);', [$network->id_networkAgent]);
|
|
$reload = true;
|
|
}
|
|
}
|
|
if($reload)
|
|
return $this->activated($id);
|
|
}
|
|
|
|
|
|
// Return only single wallet if it is hypervisor or supervisor
|
|
if(in_array( $category , ['hyper','super'])){
|
|
// Remove unnecessary fields
|
|
$networks = $this->array_except($networks,['id_networkAgent','category']);
|
|
return $this->successResponse(collect($networks)->first());
|
|
}else{
|
|
// Remove unnecessary fields
|
|
$networks = $this->array_except($networks,['id_networkAgent','category','balance_princ','balance_com','created_date','taux_com_client_depot']);
|
|
return $this->successResponse($networks);
|
|
}
|
|
|
|
}
|
|
|
|
private function array_except($array, $keys){
|
|
foreach ($array as $row){
|
|
foreach($keys as $key){
|
|
unset($row->$key);
|
|
}
|
|
}
|
|
return $array;
|
|
}
|
|
|
|
public function show($id_wallet)
|
|
{
|
|
// $wallet = Wallet::findOrFail($id_wallet);
|
|
$wallet = collect(DB::select('SELECT wa.wallet_id AS id, wa.balance_princ, wa.balance_com, wa.created_date, wa.network , cw.taux_com_client_depot, c.name AS country
|
|
FROM wallet_agent wa INNER JOIN configWallet cw ON wa.network_id = cw.id_network INNER JOIN networks n ON n.id = wa.network_id
|
|
INNER JOIN countries c ON c.id = n.country_id
|
|
WHERE wa.wallet_id = :id',['id' => $id_wallet]))->first();
|
|
if($wallet)
|
|
return $this->successResponse($wallet);
|
|
else
|
|
return $this->errorResponse(trans('errors.model_not_found',['model'=>'wallet']),Response::HTTP_BAD_REQUEST);
|
|
}
|
|
|
|
public function create(Request $request)
|
|
{
|
|
$rules = [
|
|
'id_networkAgent'=>'required|integer|min:1'
|
|
];
|
|
|
|
$this->validate($request,$rules);
|
|
|
|
DB::insert('INSERT INTO wallets (id_networkAgent) VALUES (?);', [$request->id_networkAgent]);
|
|
return $this->successResponse(trans('messages.new_wallet_added'));
|
|
|
|
}
|
|
}
|