firstOrFail(); if($demand->status == 1) return $this->errorResponse(trans('messages.treated_demand'),Response::HTTP_BAD_REQUEST); else if ($demand->status == 2) return $this->errorResponse(trans('messages.canceled_demand'),Response::HTTP_BAD_REQUEST); $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(); $walletAgent = Wallet::where('id_networkAgent',$demand->network_agent_id)->firstOrFail(); $walletParrain = Wallet::findOrFail($walletAgentParrain->wallet_id); if(isset($request->montant)){ $rules = [ 'montant'=>'required|integer|min:0|not_in:0' ]; $this->validate($request,$rules); if($walletParrain->balance_princ < $request->montant) return $this->errorResponse(trans('messages.princ_balance_inf_to_demand_amount'),Response::HTTP_UPGRADE_REQUIRED); $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) return $this->errorResponse(trans('messages.princ_balance_inf_to_demand_amount'),Response::HTTP_UPGRADE_REQUIRED); $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]); } $walletAgent->save(); $walletParrain->save(); // 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(); // } $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); return $this->successResponse(trans('messages.success_treated_demand')); } public function cancelDemand($id_demand ,Twilio $twilio){ $demand = DemandeCredit::where('id',$id_demand)->firstOrFail(); if($demand->status == 1) return $this->errorResponse(trans('messages.treated_demand'), Response::HTTP_BAD_REQUEST); else if ($demand->status == 2) return $this->errorResponse(trans('messages.canceled_demand'), Response::HTTP_BAD_REQUEST); DB::update('UPDATE demandeCredits SET status = \'2\' 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(); // } $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); return $this->successResponse(trans('messages.canceled_demand')); } }