/**
  * Displays a form to edit an existing Document entity.
  *
  * @Route("/{id}/payments/create", name="finance_document_ci_payments_create", requirements={"id"="\d+"})
  * @Method("POST")
  * @Template("FlowerFinancesBundle:CustomerInvoice:addPayment.html.twig")
  */
 public function paymentCreateAction(Request $request, Document $document)
 {
     $em = $this->getDoctrine()->getManager();
     if ($request->get('amount')) {
         $amount = $request->get('amount');
         $date = new \DateTime($request->get('date'));
         $accountId = $request->get('account_id');
         /* financial transactions */
         $transaction = new Transaction();
         $transaction->setDate($date);
         $transaction->setDescription('Payment ' . $document->__toString());
         $journalEntryReceivable = new JournalEntry();
         $receivableAccount = $em->getRepository('FlowerFinancesBundle:Account')->findOneBy(array('subtype' => Account::SUBTYPE_ASSET_RECEIVABLE));
         $journalEntryReceivable->setAccount($receivableAccount);
         $journalEntryReceivable->setTransaction($transaction);
         $journalEntryReceivable->setDebit($amount);
         $journalEntryReceivable->setDate($date);
         $transaction->addJournalEntry($journalEntryReceivable);
         $journalEntryCustomerAccount = new JournalEntry();
         $journalEntryCustomerAccount->setAccount($document->getAccount()->getFinanceAccount());
         $journalEntryCustomerAccount->setTransaction($transaction);
         $journalEntryCustomerAccount->setCredit($amount);
         $journalEntryCustomerAccount->setDate($date);
         $transaction->addJournalEntry($journalEntryCustomerAccount);
         $salesAccount = $document->getFinanceAccount();
         if (!$salesAccount) {
             $salesAccount = $em->getRepository('FlowerFinancesBundle:Account')->findOneBy(array('subtype' => Account::SUBTYPE_INCOME_SALES));
         }
         $salesEntry = new JournalEntry();
         $salesEntry->setAccount($salesAccount);
         $salesEntry->setTransaction($transaction);
         $salesEntry->setCredit($amount);
         $salesEntry->setDate($date);
         $transaction->addJournalEntry($salesEntry);
         $incomeAccount = $em->getRepository('FlowerFinancesBundle:Account')->find($accountId);
         $incomeEntry = new JournalEntry();
         $incomeEntry->setAccount($incomeAccount);
         $incomeEntry->setDebit($amount);
         $incomeEntry->setDate($date);
         $incomeEntry->setTransaction($transaction);
         $transaction->addJournalEntry($incomeEntry);
         $em->persist($transaction);
         $em->flush();
         $payment = new Payment();
         $payment->setAmount($amount);
         $payment->setType(Payment::TYPE_INCOME);
         $payment->setName('Payment ' . $document->__toString());
         $payment->setDescription('Payment ' . $document->__toString());
         $payment->addDocument($document);
         $payment->setDate($date);
         $document->addPayment($payment);
         $em->persist($payment);
         $paymentsAmount = 0;
         foreach ($document->getPayments() as $pay) {
             $paymentsAmount += (double) $pay->getAmount();
         }
         if ($paymentsAmount >= $document->getTotal()) {
             $document->setStatus(Document::STATUS_PAID);
         }
         $em->flush();
         return $this->redirect($this->generateUrl('finance_document_ci_show', array('id' => $document->getId())));
     }
     $assetAccounts = $em->getRepository('FlowerFinancesBundle:Account')->findBy(array('type' => Account::TYPE_ASSET));
     return array('assetAccounts' => $assetAccounts, 'document' => $document);
 }