public function onReceive(PaymentEvent $event)
 {
     $payment = $event->getPayment();
     $cpRepo = $this->em->getRepository('TSK\\PaymentBundle\\Entity\\ChargePayment');
     $chargePayments = $cpRepo->findBy(array('payment' => $payment));
     foreach ($chargePayments as $cp) {
         if ($cp->getCharge()->getAccount()->getName() == 'Inc Fm Students') {
             $results = array();
             $contracts = $cp->getCharge()->getContracts();
             foreach ($contracts as $contract) {
                 $inits = $this->getMonthlyPrepayments($contract);
                 $today = new \DateTime();
                 $terms = $contract->getPaymentTerms();
                 $obj = json_decode($terms['paymentsData']);
                 $d = new Deferral($obj->principal, $contract->getDeferralRate(), $contract->getDeferralDurationMonths(), $inits, $contract->getContractStartDate());
                 $numFirsts = $this->countFirstOfMonthsSince($contract->getContractStartDate());
                 $deferrals = $d->distributePaymentMax($cp->getAmount(), $contract->getDeferralDurationMonths() - $numFirsts);
                 $Deferrals = $d->datestampPayments($deferrals);
                 $debitAccount = $payment->getPaymentMethod()->getAccount();
                 $chargeDeferralAccount = $cp->getCharge()->getDeferralAccount();
                 $chargeAccount = $cp->getCharge()->getAccount();
                 foreach ($Deferrals as $DeferralDate => $DeferralAmount) {
                     $DD = new \DateTime($DeferralDate);
                     if ($DeferralAmount) {
                         if ($DD <= $today) {
                             $results[] = array('date' => $DeferralDate, 'creditAccount' => $chargeAccount, 'debitAccount' => $debitAccount, 'debitAmount' => $DeferralAmount, 'creditAmount' => $DeferralAmount, 'chargePayment' => $cp, 'description' => $cp->getCharge()->getDescription());
                             if ($cp->getAmount() > $DeferralAmount) {
                                 $results[] = array('date' => $DeferralDate, 'creditAccount' => $chargeDeferralAccount, 'debitAccount' => $debitAccount, 'debitAmount' => $cp->getAmount() - $DeferralAmount, 'creditAmount' => $cp->getAmount() - $DeferralAmount, 'chargePayment' => $cp, 'description' => $cp->getCharge()->getDescription());
                             }
                         } else {
                             $results[] = array('date' => $DeferralDate, 'creditAccount' => $chargeAccount, 'debitAccount' => $chargeDeferralAccount, 'debitAmount' => $DeferralAmount, 'creditAmount' => $DeferralAmount, 'chargePayment' => $cp, 'description' => $cp->getCharge()->getDescription());
                         }
                     }
                 }
             }
             // Here is where we actually insert into journal ...
             if (count($results)) {
                 foreach ($results as $result) {
                     $journal = $this->generateJournal($result);
                     $this->em->persist($journal);
                 }
                 $this->em->flush();
             }
         }
     }
 }
 /**
  * @Route("/defer/{id}/{amount}/{contractStartDate}")
  * @Template()
  * @Method({"GET"})
  */
 public function testAction(Payment $payment, $amount, \DateTime $contractStartDate = null)
 {
     $em = $this->getDoctrine()->getManager();
     $cpRepo = $em->getRepository('TSK\\PaymentBundle\\Entity\\ChargePayment');
     $chargePayments = $cpRepo->findBy(array('payment' => $payment));
     foreach ($chargePayments as $cp) {
         if ($cp->getCharge()->getAccount()->getName() == 'Inc Fm Students') {
             $contracts = $cp->getCharge()->getContracts();
             foreach ($contracts as $contract) {
                 $inits = $this->getMonthlyPrepayments($contract, $contractStartDate);
                 print '<pre>inits';
                 print_r($inits) . '</pre>';
                 $today = new \DateTime();
                 $terms = $contract->getPaymentTerms();
                 $obj = json_decode($terms['paymentsData']);
                 $d = new Deferral($obj->principal, $contract->getDeferralRate(), $contract->getDeferralDurationMonths(), $inits, $contractStartDate);
                 $numFirsts = $this->countFirstOfMonthsSince($contractStartDate);
                 // $numFirsts = $this->countFirstOfMonthsSince($contract->getContractStartDate);
                 // $deferrals = $d->distributePaymentMax($cp->getAmount(), 9);
                 $deferrals = $d->distributePaymentMax($amount, $contract->getDeferralDurationMonths() - $numFirsts);
                 $Deferrals = $d->datestampPayments($deferrals);
                 $debitAccountName = $payment->getPaymentMethod()->getAccount()->getName();
                 $chargeDeferralAccountName = $cp->getCharge()->getDeferralAccount()->getName();
                 $chargeAccountName = $cp->getCharge()->getAccount()->getName();
                 foreach ($Deferrals as $DeferralDate => $DeferralAmount) {
                     print "{$DeferralDate} - {$DeferralAmount}<br>";
                     $DD = new \DateTime($DeferralDate);
                     if ($DeferralAmount) {
                         if ($DD <= $today) {
                             $results[] = array('date' => $DeferralDate, 'creditAccount' => $chargeAccountName, 'debitAccount' => $debitAccountName, 'debitAmount' => $DeferralAmount, 'creditAmount' => $DeferralAmount);
                             if ($amount > $DeferralAmount) {
                                 $results[] = array('date' => $DeferralDate, 'creditAccount' => $chargeDeferralAccountName, 'debitAccount' => $debitAccountName, 'debitAmount' => $amount - $DeferralAmount, 'creditAmount' => $amount - $DeferralAmount);
                             }
                         } else {
                             $results[] = array('date' => $DeferralDate, 'creditAccount' => $chargeAccountName, 'debitAccount' => $chargeDeferralAccountName, 'debitAmount' => $DeferralAmount, 'creditAmount' => $DeferralAmount);
                         }
                     }
                 }
                 function sorter($key)
                 {
                     return function ($a, $b) use($key) {
                         $ad = new \DateTime($a[$key]);
                         $bd = new \DateTime($b[$key]);
                         return $ad > $bd;
                     };
                 }
                 usort($results, sorter('date'));
             }
         }
     }
     return array('today' => $today, 'results' => $results, 'foo' => '$1200 contract, 10 payments @ $120, starting on ' . $contractStartDate->format('Y-m-d') . ', deferral rate of 0.75, deferred over 10 months');
     // return array('foo' => '$1200 contract, 10 payments @ $120, starting on '.$contract->getContractStartDate()->format('Y-m-d').', deferral rate of 0.75, deferred over 10 months');
 }