2020-04-25 11:41:40 +00:00
< ? 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 ;
2020-04-27 13:13:15 +00:00
use App\Twilio ;
2020-04-25 11:41:40 +00:00
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 ()
{
//
}
2020-04-27 13:13:15 +00:00
public function treatDemand ( $id_demand , Twilio $twilio ){
2020-04-25 11:41:40 +00:00
$demand = DemandeCredit :: where ( 'id' , $id_demand ) -> firstOrFail ();
if ( $demand -> status == 1 )
2020-04-28 16:12:05 +00:00
return $this -> errorResponse ( trans ( 'messages.treated_demand' ), Response :: HTTP_BAD_REQUEST );
2020-04-25 11:41:40 +00:00
2020-04-27 13:13:15 +00:00
$agent = AgentPlus :: where ( 'code_membre' , $demand -> code_membre ) -> firstOrFail ();
2020-04-25 11:41:40 +00:00
$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 )
2020-04-28 16:12:05 +00:00
return $this -> errorResponse ( trans ( 'messages.princ_balance_inf_to_demand_amount' ), Response :: HTTP_BAD_REQUEST );
2020-04-25 11:41:40 +00:00
$walletAgent -> balance_princ += $demand -> montant ;
$walletParrain -> balance_princ -= $demand -> montant ;
2020-04-27 13:13:15 +00:00
DB :: update ( 'UPDATE demandeCredits SET status = 1 , date_modification = CURRENT_TIMESTAMP WHERE ( id = ? );' ,[ $demand -> id ]);
2020-04-25 11:41:40 +00:00
$walletAgent -> save ();
$walletParrain -> save ();
2020-04-27 13:13:15 +00:00
// try {
// $twilio->notify($agent->phone, 'Votre demande de credit de '.$demand->montant .' aupres de '.$parrain->lastname.' '.$parrain->lastname.'a été traitée');
// }catch (\Exception $e){
// echo "Error: " . $e->getMessage();
// }
return $this -> successResponse ( $walletParrain );
}
public function cancelDemand ( $id_demand , Twilio $twilio ){
DB :: update ( 'UPDATE demandeCredits SET canceled = 1 WHERE ( id = ? );' ,[ $id_demand ]);
// try {
// $twilio->notify($agent->phone, 'Votre demande de credit de '.$demand->montant .' aupres de '.$parrain->lastname.' '.$parrain->lastname.'a été traitée');
// }catch (\Exception $e){
// echo "Error: " . $e->getMessage();
// }
2020-04-28 16:12:05 +00:00
return $this -> successResponse ( trans ( 'messages.canceled_demand' ));
2020-04-25 11:41:40 +00:00
}
}