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-10-09 16:41:19 +00:00
use App\Traits\Helper ;
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 ;
2020-10-09 16:41:19 +00:00
use Helper ;
2020-04-25 11:41:40 +00:00
/**
* Create a new controller instance .
* a
* @ return void
*/
public function __construct ()
{
//
}
2020-05-01 07:25:55 +00:00
public function treatDemand ( Request $request , $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-05-01 07:25:55 +00:00
else if ( $demand -> status == 2 )
return $this -> errorResponse ( trans ( 'messages.canceled_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 );
2020-05-01 07:25:55 +00:00
if ( isset ( $request -> montant )){
$rules = [
'montant' => 'required|integer|min:0|not_in:0'
];
$this -> validate ( $request , $rules );
2020-04-25 11:41:40 +00:00
2020-05-01 07:25:55 +00:00
if ( $walletParrain -> balance_princ < $request -> montant )
2020-06-02 19:17:45 +00:00
return $this -> errorResponse ( trans ( 'messages.princ_balance_inf_to_demand_amount' ), Response :: HTTP_UPGRADE_REQUIRED );
2020-05-01 07:25:55 +00:00
$walletAgent -> balance_princ += $request -> montant ;
$walletParrain -> balance_princ -= $request -> montant ;
DB :: update ( 'UPDATE demandeCredits SET montant = ? , `status` = \'1\' , date_modification = CURRENT_TIMESTAMP WHERE ( id = ? );' ,[ $request -> montant , $demand -> id ]);
} else {
if ( $walletParrain -> balance_princ < $demand -> montant )
2020-06-02 19:17:45 +00:00
return $this -> errorResponse ( trans ( 'messages.princ_balance_inf_to_demand_amount' ), Response :: HTTP_UPGRADE_REQUIRED );
2020-05-01 07:25:55 +00:00
$walletAgent -> balance_princ += $demand -> montant ;
$walletParrain -> balance_princ -= $demand -> montant ;
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();
// }
2020-10-09 16:41:19 +00:00
$data = new \stdClass ();
$data -> screen = " notificationview " ;
$data -> data = new \stdClass ();
$data -> data -> id = $id_demand ;
$this -> sendPushNotificationToAgent ( $agent -> code_membre ,
trans ( 'notifications.accepted_credit_request' ,
[ 'amount' => $this -> toMoneyWithCurrencyCode ( $demand -> montant , $walletAgentParrain -> currency_code ), " godfather " => $parrain -> lastname . ' ' . $parrain -> lastname ]), $data );
2020-05-01 19:14:33 +00:00
return $this -> successResponse ( trans ( 'messages.success_treated_demand' ));
2020-04-27 13:13:15 +00:00
}
public function cancelDemand ( $id_demand , Twilio $twilio ){
2020-05-01 18:35:53 +00:00
$demand = DemandeCredit :: where ( 'id' , $id_demand ) -> firstOrFail ();
if ( $demand -> status == 1 )
2020-10-09 16:41:19 +00:00
return $this -> errorResponse ( trans ( 'messages.treated_demand' ), Response :: HTTP_BAD_REQUEST );
2020-05-01 18:35:53 +00:00
else if ( $demand -> status == 2 )
2020-10-09 16:41:19 +00:00
return $this -> errorResponse ( trans ( 'messages.canceled_demand' ), Response :: HTTP_BAD_REQUEST );
2020-05-01 18:35:53 +00:00
2020-10-09 16:41:19 +00:00
DB :: update ( 'UPDATE demandeCredits SET status = \'2\' WHERE ( id = ? );' , [ $id_demand ]);
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();
// }
2020-10-09 16:41:19 +00:00
$agent = AgentPlus :: where ( 'code_membre' , $demand -> code_membre ) -> firstOrFail ();
$parrain = AgentPlus :: where ( 'code_membre' , $demand -> code_parrain ) -> firstOrFail ();
$walletAgentParrain = WalletAgent :: where ( 'agent_id' , $parrain -> id ) -> firstOrFail ();
$data = new \stdClass ();
$data -> screen = " notificationview " ;
$data -> data = new \stdClass ();
$data -> data -> id = $id_demand ;
$this -> sendPushNotificationToAgent ( $agent -> code_membre ,
trans ( 'notifications.refused_credit_request' ,
[ 'amount' => $this -> toMoneyWithCurrencyCode ( $demand -> montant , $walletAgentParrain -> currency_code ), " godfather " => $parrain -> lastname . ' ' . $parrain -> lastname ]), $data );
2020-04-28 16:12:05 +00:00
return $this -> successResponse ( trans ( 'messages.canceled_demand' ));
2020-04-25 11:41:40 +00:00
}
}