php - Symfony how to converte object of class to double and check variables -


i have next code @ symfony2 . when try check variables in public function subtractcredits i'm getting error:

notice: object of class dorent\rentalbundle\entity\credit not converted double

my code:

$credit->setamount($creditstotal - $credit->getamount());         if ($credit <= $creditstotal) {             if ($this->savecredit($credit)) {                 //send credit subtracted notification email.                 if (array_key_exists('emailcreditssubtracted', $credit->getserviceprovider()->getsettings())) {  <?php  namespace dorent\rentalbundle\credit;  use dorent\rentalbundle\entity\credit;  use doctrine\orm\entitymanager; use dorent\basebundle\email\email; use symfony\bridge\monolog\logger;  class creditmanager {     /**      *      * @var entitymanager     */     protected $em;     protected $email;     protected $logger;      public function __construct(entitymanager $entitymanager, email $email, logger $logger)     {         $this->em = $entitymanager;         $this->email = $email;         $this->logger = $logger;     }      public function getnewcredit($serviceprovider, $customer, $user) {         $credit = new credit();         $credit->setserviceprovider($serviceprovider);         $credit->setcustomer($customer);         $credit->setcreatedby($user);         return $credit;     }      public function getnewgiftcard($serviceprovider, $customer, $user) {         $credit = new credit();         $credit->setserviceprovider($serviceprovider);         $credit->setcustomer($customer);         $credit->setcreatedby($user);          $credit->setamount($this->gettotalamountbycustomerandserviceprovider($customer, $serviceprovider));          $category = $this->em->getrepository('dorentrentalbundle:creditcategory')             ->findonebycode('gift-card');          $credit->setcategory($category);          return $credit;     }      public function savecredit($credit) {              $this->em->persist($credit);             $this->em->flush();             return true;         }      public function createcredit($payment) {         $offer = $payment->getrental()->getoffer();         $rental = $payment->getrental();          $category = $this->em->getrepository('dorentrentalbundle:creditcategory')             ->findonebycode('refund');          $credit = new credit();         $credit->setamount($offer->getprice());         $credit->setcustomer($offer->getcustomer());         $credit->setserviceprovider($offer->getserviceprovider());         $credit->setcategory($category);          $credit->setrental($rental);         $rental->setcredit($credit);          $this->em->persist($credit);         $this->em->flush();         return true;     }      public function gettotalamountbycustomerandserviceprovider($customer, $sp) {         $credits = $this->em->getrepository('dorentrentalbundle:credit')             ->findbycustomerandserviceprovider($customer, $sp);          return $this->gettotalamount($credits);     }      public function gettotalamount($credits) {         $t = 0;          foreach ($credits $c) {             $t += $c->getamount();         }          return round($t, 2);     }      public function equals(creditmanager $object, $credit) {         return $this->gettotalamount($credit) == $object->gettotalamount($credit);     }      public function subtractcredits($credit)     {         //here error         $creditstotal = $this->gettotalamount($credit);          $credit->setamount($creditstotal - $credit->getamount());          if ($credit <= $creditstotal) {             if ($this->savecredit($credit)) {                 //send credit subtracted notification email.                 if (array_key_exists('emailcreditssubtracted', $credit->getserviceprovider()->getsettings())) {                     $this->email->sendcreditssubtracted($credit);                 }                 return true;             } else {              }              return false;         }     }           //$credit->setamount($credit->getamount() * -1);      public function calculatecreditexpiration() {         $sps = $this->em->getrepository('dorentserviceproviderbundle:serviceprovider')->findall();          foreach ($sps $key => $sp) {             $validityperiod = 0;             $settings = $sp->getsettings();             if (isset($settings['creditvalidityperioddays'])) {                 $validityperiod = $settings['creditvalidityperioddays'];             }             $notificationperiod = 0;             if (isset($settings['creditexpirationnotificationdays'])) {                 $notificationperiod = $settings['creditexpirationnotificationdays'];             }              if ($validityperiod === 0) {                 $this->logger->info($sp->getname().": validity period not set");                 continue;             } else {                 $this->logger->info($sp->getname().": validity period ".$validityperiod);                 $this->calculateserviceprovider($sp, $validityperiod, $notificationperiod);             }           }         $this->logger->info('end calculation');     }      private function expire($amount, $customer, $serviceprovider) {         $msg = "expire: ".date_create()->format('y-m-d')." ".$amount." ".$serviceprovider->getcode()." ".$customer->getid()." ".$customer->getname()."\n";         $this->logger->info($msg);          $category = $this->em->getrepository('dorentrentalbundle:creditcategory')             ->findonebycode('expired');         $c = $this->getnewcredit($serviceprovider, $customer, null);         $c->setamount($amount * -1);         $c->setcategory($category);         $this->savecredit($c);     }      private function notifyexpiressoon($amount, $customer, $serviceprovider, $date) {         $msg = "expires soon: ".$date->format('y-m-d')." ".$amount." ".$serviceprovider->getcode()." ".$customer->getid()." ".$customer->getname()."\n";         $this->logger->info($msg);          $this->email->notifyexpiressoon($amount, $customer, $serviceprovider, $date);     }      private function calculateserviceprovider($sp, $validityperiod, $notificationperiod) {         $this->logger->info('sp '.$sp->getcode());          //calculate expiring         $date = date_create();         date_sub($date, date_interval_create_from_date_string($validityperiod.' days'));          $customers = $this->em->getrepository('dorentcustomerbundle:customer')->findbyserviceprovider($sp);          foreach($customers $key => $customer) {             if ($this->gettotalamountbycustomerandserviceprovider($customer, $sp) <= 0) {                 // customer has 0 credits                 continue;             }              $exp = $this->calculatecustomer($customer, $sp, $date, 0);             if ($exp > 0) {                 //add expired credit                 $this->expire($exp, $customer, $sp);             }              //notification             if ($notificationperiod !== 0) {                 $this->calculatenotification($customer, $sp, $validityperiod, $notificationperiod);             }           }     }      private function calculatenotification($customer, $sp, $validityperiod, $notificationperiod) {         $lookahead = 0;         $exp = 0;         ($i = 0; $i <= $notificationperiod; $i++) {             $date = date_create();             date_add($date, date_interval_create_from_date_string($i.' days'));              $expdate = clone $date;              date_sub($date, date_interval_create_from_date_string($validityperiod.' days'));             $exp = $this->calculatecustomer($customer, $sp, $date, $lookahead);              if ($exp > 0) {                 $lookahead += $exp * -1;             }         }         if ($exp > 0) {             $this->notifyexpiressoon($exp, $customer, $sp, $expdate);         }     }      private function calculatecustomer($customer, $serviceprovider, $date, $lookahead = 0) {         $cr = $this->em->getrepository('dorentrentalbundle:credit');         $credits = $cr->findbycustomerandserviceproviderafterdate($customer, $serviceprovider, $date);          if (count($credits) === 0) { return 0; }          //sum of credits created < date, if <= 0 return, e.g. 100 + 200 = 300         $sumofoldcredits = $cr->sumbycustomerandserviceproviderbeforedate($customer, $serviceprovider, $date);          if ($sumofoldcredits === null) {             $sumofoldcredits = 0;         }          if ($sumofoldcredits <= 0) { return 0; }          //sum of used credits > date, e.g. -200         $sumofusedcredits = $cr->sumofusedcredits($customer, $serviceprovider, $date);         if ($sumofusedcredits === null) {             $sumofusedcredits = 0;         }          //credits expire = 300 + (-200) = 100 credits, if sum < 0 no credits expire         $expiring = $sumofoldcredits + $sumofusedcredits + $lookahead;          return $expiring;     }  } 

this method didn't work.

public function gettotalamount($credits)     {         if (!is_array($credits)) $credits = array($credits);          $t = 0;          foreach ($credits $c) {             $t += $c->getamount();         }          return round($t, 2);     }       public function subtractcredits($credit)     {         $creditstotal = $this->gettotalamount($credit);         if ($credit->getamount() < $creditstotal) {             $creditstotal = $this->gettotalamount($credit);             $credit->setamount($creditstotal - $credit->getamount());             if ($this->savecredit($credit)) {                 //send credit subtracted notification email.                 if (array_key_exists('emailcreditssubtracted', $credit->getserviceprovider()->getsettings())) {                     $this->email->sendcreditssubtracted($credit);                 }                 return true;             } else {             }             return false;         }     } 

creditscontroller:

<?php  namespace dorent\servicedeskbundle\controller;  use symfony\bundle\frameworkbundle\controller\controller; use sensio\bundle\frameworkextrabundle\configuration\route; use sensio\bundle\frameworkextrabundle\configuration\template; use symfony\component\httpfoundation\request; use symfony\component\validator\constraints\range;  class creditscontroller extends servicedeskbasecontroller {      private function getform($credit) {         return $this->createformbuilder($credit)                    ->add('amount', 'number',                     ['label' => 'credit.labels.amount',                      'constraints' => new range(array('min' => 0, 'minmessage' => 'credit.amount.min')),                     ])                    ->add('category', 'entity', array(                         'label' => 'credit.labels.category',                         'class' => 'dorentrentalbundle:creditcategory',                         'property' => 'description'))                    ->add('description', 'textarea', array(                         'label' => 'credit.labels.description',                         'required' => false))                    ->add('save', 'submit', ['label' => 'credit.labels.save'])                    ->getform();     }      public function addaction(request $request, $provider, $customerid)     {         $sp = $this->getprovider($provider);         $customer = $this->getcustomer($customerid, $sp);         $user= $this->get('security.context')->gettoken()->getuser();          $creditmanager = $this->get('credit_manager');          $credit = $creditmanager->getnewcredit($sp, $customer, $user);          $form = $this->getform($credit);          $form->handlerequest($request);          if ($form->isvalid()) {              if ($creditmanager->savecredit($credit)) {                 $message = $this->get('translator')->trans('credit.message.created');             } else {                 $message = $this->get('translator')->trans('credit.message.create_failed');             }              $this->get('session')->getflashbag()->add(                'notice',                $message             );              return $this->redirect($this->generateurl('service_desk_customer', array(                 'provider' => $sp->getcode(),                 'customerid' => $customerid                 )             ));         }          return $this->render('dorentservicedeskbundle:credits:add.html.twig', array(             'form' => $form->createview(),             'provider' => $sp,             'customer' => $customer,         ));     }      public function subtractaction(request $request, $provider, $customerid)     {           $sp = $this->getprovider($provider);         $customer = $this->getcustomer($customerid, $sp);         $user= $this->get('security.context')->gettoken()->getuser();          $creditmanager = $this->get('credit_manager');          $credit = $creditmanager->getnewcredit($sp, $customer, $user);          $form = $this->getform($credit);          $form->handlerequest($request);          if ($form->isvalid()) {             if ($creditmanager->subtractcredits($credit)) {                 $message = $this->get('translator')->trans('credit.message.created');             } else {                 $message = $this->get('translator')->trans('credit.message.create_failed');             }              $this->get('session')->getflashbag()->add(                'notice',                $message             );              return $this->redirect($this->generateurl('service_desk_customer', array(                 'provider' => $sp->getcode(),                 'customerid' => $customerid                 )             ));         }          return $this->render('dorentservicedeskbundle:credits:subtract.html.twig', array(             'form' => $form->createview(),             'provider' => $sp,             'customer' => $customer,         ));     }      public function giftaction(request $request, $provider, $customerid)     {         $sp = $this->getprovider($provider);         $customer = $this->getcustomer($customerid, $sp);         $user= $this->get('security.context')->gettoken()->getuser();          $creditmanager = $this->get('credit_manager');          $credit = $creditmanager->getnewgiftcard($sp, $customer, $user);          $form = $this->getform($credit);          $form->handlerequest($request);          if ($form->isvalid()) {              $category = $this->getdoctrine()->getrepository('dorentrentalbundle:creditcategory')                 ->findonebycode('gift-card');              $credit->setcategory($category);              if ($credit->getamount() > $creditmanager->gettotalamountbycustomerandserviceprovider($customer, $sp)) {                 $message = $this->get('translator')->trans('credit.message.create_failed');             } else {                  if ($creditmanager->subtractcredits($credit)) {                     $message = $this->get('translator')->trans('credit.message.created');                     $this->get('session')->set('gift-card-created', $credit->getid());                 } else {                     $message = $this->get('translator')->trans('credit.message.create_failed');                 }             }              $this->get('session')->getflashbag()->add(                'notice',                $message             );              return $this->redirect($this->generateurl('service_desk_customer', array(                 'provider' => $sp->getcode(),                 'customerid' => $customerid                 )             ));         }          return $this->render('dorentservicedeskbundle:credits:gift.html.twig', array(             'form' => $form->createview(),             'provider' => $sp,             'customer' => $customer,         ));      }      public function printgiftcardaction(request $request, $provider, $creditid) {         $sp = $this->getprovider($provider);          $category = $this->getdoctrine()->getrepository('dorentrentalbundle:creditcategory')             ->findonebycode('gift-card');          $credit = $this->getdoctrine()->getrepository('dorentrentalbundle:credit')             ->findoneby(array(                 'category' => $category,                 'serviceprovider' => $sp,                 'id' => $creditid             ))         ;          if ($credit === null) {             throw $this->createnotfoundexception('the credit not exist');         }          // not used         // $validityperiod = 12;         // $expirationdate = date_create();         // date_add($expirationdate, date_interval_create_from_date_string($validityperiod.' months'));          return $this->render('dorentservicedeskbundle:credits:gift-card.html.twig', array(             'provider' => $sp,             'credit' => $credit,         ));     } } 

//here error $creditstotal = $this->gettotalamount($credit); 
  1. you need put array of credit objects argument of gettotalamount or can change method that:

    public function gettotalamount($credits) {     if(!is_array($credits)) $credits = array($credits);      $t = 0;      foreach ($credits $c) {         $t += $c->getamount();     }      return round($t, 2); } 
  2. you cannot doing that: if ($credit <= $creditstotal) because $credit object of class credit. should use method $credit $credit->getamount() or whatever want compare $creditstotal.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -