52 lines
1.6 KiB
PHP
52 lines
1.6 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace App\Http\Controllers;
|
||
|
|
||
|
use App\Models\AgentPlus;
|
||
|
use App\Models\DemandeCredit;
|
||
|
use App\Models\Wallet;
|
||
|
use App\Models\WalletAgent;
|
||
|
use App\Traits\ApiResponser;
|
||
|
use Illuminate\Http\Request;
|
||
|
use Illuminate\Http\Response;
|
||
|
use Illuminate\Support\Facades\DB;
|
||
|
|
||
|
class CreditController extends Controller
|
||
|
{
|
||
|
use ApiResponser;
|
||
|
|
||
|
/**
|
||
|
* Create a new controller instance.
|
||
|
*a
|
||
|
* @return void
|
||
|
*/
|
||
|
public function __construct()
|
||
|
{
|
||
|
//
|
||
|
}
|
||
|
|
||
|
public function treatDemand($id_demand){
|
||
|
$demand = DemandeCredit::where('id',$id_demand)->firstOrFail();
|
||
|
if($demand->status == 1)
|
||
|
return $this->errorResponse('Demande deja traitée',Response::HTTP_BAD_REQUEST);
|
||
|
|
||
|
$parrain = AgentPlus::where('code_membre',$demand->code_parrain)->firstOrFail();
|
||
|
|
||
|
$walletAgentParrain = WalletAgent::where('agent_id',$parrain->id)->firstOrFail();
|
||
|
|
||
|
$walletAgent = Wallet::where('id_networkAgent',$demand->network_agent_id)->firstOrFail();
|
||
|
$walletParrain = Wallet::findOrFail($walletAgentParrain->wallet_id);
|
||
|
|
||
|
if($walletParrain->balance_princ < $demand->montant)
|
||
|
return $this->errorResponse('Solde principal du parrain inférieur au montant de la demande',Response::HTTP_BAD_REQUEST);
|
||
|
|
||
|
$walletAgent->balance_princ += $demand->montant;
|
||
|
$walletParrain->balance_princ -= $demand->montant;
|
||
|
|
||
|
DB::update('UPDATE demandeCredits SET status = 1 WHERE ( id = ? );',[$demand->id]);
|
||
|
$walletAgent->save();
|
||
|
$walletParrain->save();
|
||
|
return $this->successResponse($walletAgent);
|
||
|
}
|
||
|
}
|