/**
  * 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 createSupplierInvoiceTransaction(Document $document)
 {
     $transaction = new Transaction();
     $description = $this->translator->trans("transaction.description.document", array("%document_code%" => $document->getType()->getCode(), "%document_number%" => $document->getId(), "%document_to%" => $document->getSupplier()->getName()), 'Finance');
     $transaction->setDescription($description);
     $transaction->setDate(new \DateTime());
     $payableAccount = $this->accountRepository->findOneBy(array('subtype' => Account::SUBTYPE_LIABILITY_PAYABLE));
     $journalEntryPayable = new JournalEntry();
     $journalEntryPayable->setAccount($payableAccount);
     $journalEntryPayable->setTransaction($transaction);
     $journalEntryPayable->setDebit($document->getTotal());
     $journalEntryPayable->setDate(new \DateTime());
     $transaction->addJournalEntry($journalEntryPayable);
     $journalEntrySupplierAccount = new JournalEntry();
     $journalEntrySupplierAccount->setAccount($document->getSupplier()->getFinanceAccount());
     $journalEntrySupplierAccount->setTransaction($transaction);
     $journalEntrySupplierAccount->setCredit($document->getTotal());
     $journalEntrySupplierAccount->setDate(new \DateTime());
     $transaction->addJournalEntry($journalEntrySupplierAccount);
     /* persist */
     $this->transactionRepository->save($transaction);
     return $transaction;
 }
 /**
  * Deletes a Document entity.
  *
  * @Route("/{id}/delete", name="finance_document_ci_delete", requirements={"id"="\d+"})
  * @Method("DELETE")
  */
 public function deleteAction(Document $document, Request $request)
 {
     $form = $this->createDeleteForm($document->getId(), 'finance_document_ci_delete');
     if ($form->handleRequest($request)->isValid()) {
         $em = $this->getDoctrine()->getManager();
         $em->remove($document);
         $em->flush();
     }
     return $this->redirect($this->generateUrl('finance_document_ci'));
 }
 /**
  * Displays a form to create a new Payment entity.
  *
  * @Route("/new_simple_expense", name="finance_payment_simple_expense_create")
  * @Method("POST")
  * @Template("FlowerFinancesBundle:Payment:newSimpleExpense.html.twig")
  */
 public function createSimpleExpenseAction(Request $request)
 {
     $payment = new Payment();
     $payment->setType(Payment::TYPE_EXPENSE);
     $em = $this->getDoctrine()->getManager();
     $expenseAccount = $em->getRepository('FlowerFinancesBundle:Account')->find($request->get('expense_account_id'));
     $assetAccount = $em->getRepository('FlowerFinancesBundle:Account')->find($request->get('asset_account_id'));
     $form = $this->createForm(new SimpleExpensePaymentType(), $payment);
     if ($form->handleRequest($request)->isValid()) {
         /* has receipt */
         if ($request->get('receipt_number')) {
             $receipt = new Document();
             $type = $this->getDoctrine()->getManager()->getRepository('FlowerFinancesBundle:DocumentType')->findOneBy(array('name' => \Flower\FinancesBundle\Entity\DocumentType::TYPE_RECEIPT));
             $receipt->setCode($request->get('receipt_number'));
             $receipt->setType($type);
             $receipt->setTotal($payment->getAmount());
             $em->persist($receipt);
             $payment->addDocument($receipt);
         }
         $transaction = $this->get('finances.service.transaction')->createSimpleExpenseTransaction($expenseAccount, $assetAccount, $payment->getAmount(), $payment->getDate());
         $payment->setTransaction($transaction);
         $em->persist($payment);
         $em->flush();
         return $this->redirect($this->generateUrl('finance_payment_show', array('id' => $payment->getId())));
     }
     $assetAccounts = $em->getRepository('FlowerFinancesBundle:Account')->findBy(array('type' => Account::TYPE_ASSET));
     $expenseAccounts = $em->getRepository('FlowerFinancesBundle:Account')->findBy(array('type' => Account::TYPE_EXPENSE));
     return array('form' => $form->createView(), 'assetAccounts' => $assetAccounts, 'expenseAccounts' => $expenseAccounts);
 }