Ejemplo n.º 1
0
 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;
 }
 /**
  * Deletes a Transaction entity.
  *
  * @Route("/{id}/delete", name="finance_transaction_delete", requirements={"id"="\d+"})
  * @Method("DELETE")
  */
 public function deleteAction(Transaction $transaction, Request $request)
 {
     $form = $this->createDeleteForm($transaction->getId(), 'finance_transaction_delete');
     if ($form->handleRequest($request)->isValid()) {
         $em = $this->getDoctrine()->getManager();
         $em->remove($transaction);
         $em->flush();
     }
     return $this->redirect($this->generateUrl('finance_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);
 }