/**
  * Displays a form to create a new Transaction entity.
  *
  * @Route("/new/Document/{id}", name="finance_transaction_new_from_invoice")
  * @Method("GET")
  * @Template("FlowerFinancesBundle:Transaction:new.html.twig")
  */
 public function newFromDocumentAction(Document $Document)
 {
     $transaction = new Transaction();
     $financeAccount = $Document->getAccount()->getFinanceAccount();
     if ($financeAccount) {
         $desc = "Pago " . $Document->getAccount()->getName();
         $desc .= " por factura " . $Document->getId();
         $transaction->setDescription($desc);
         $journalEntry = new JournalEntry();
         $journalEntry->setAccount($financeAccount);
         $journalEntry->getDate(new \DateTime());
         $transaction->addJournalEntry($journalEntry);
     }
     $form = $this->createForm(new TransactionType(), $transaction);
     return array('transaction' => $transaction, 'form' => $form->createView());
 }
 public function createSimpleExpenseTransaction(Account $expenseAccount, Account $assetAccount, $amount, $date)
 {
     $transaction = new Transaction();
     $description = $this->translator->trans("transaction.description.simple_expense", array("%account_to%" => $expenseAccount->getName()), 'Finance');
     $transaction->setDescription($description);
     $transaction->setCircuit(Transaction::CIRCUIT_TWO);
     $transaction->setDate($date);
     $expenseEntry = new JournalEntry();
     $expenseEntry->setAccount($expenseAccount);
     $expenseEntry->setTransaction($transaction);
     $expenseEntry->setDebit($amount);
     $expenseEntry->setDate($date);
     $transaction->addJournalEntry($expenseEntry);
     $assetEntry = new JournalEntry();
     $assetEntry->setAccount($assetAccount);
     $assetEntry->setCredit($amount);
     $assetEntry->setDate($date);
     $assetEntry->setTransaction($transaction);
     $transaction->addJournalEntry($assetEntry);
     /* persist */
     $this->transactionRepository->save($transaction);
     return $transaction;
 }
 /**
  * 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);
 }
 /**
  * Deletes a JournalEntry entity.
  *
  * @Route("/{id}/delete", name="finance_journal_entry_delete", requirements={"id"="\d+"})
  * @Method("DELETE")
  */
 public function deleteAction(JournalEntry $journalentry, Request $request)
 {
     $form = $this->createDeleteForm($journalentry->getId(), 'finance_journal_entry_delete');
     if ($form->handleRequest($request)->isValid()) {
         $em = $this->getDoctrine()->getManager();
         $em->remove($journalentry);
         $em->flush();
     }
     return $this->redirect($this->generateUrl('finance_journal_entry'));
 }