Ejemplo n.º 1
0
 /**
  * @param $purchaseOrderTransactionId
  * @param bool $updatePurchaseOrderBalance
  * @return bool
  */
 public function removePurchaseOrderTransaction($purchaseOrderTransactionId, $updatePurchaseOrderBalance = true)
 {
     /**
      * @var MoneyAccountsDAO $moneyAccountsDao
      */
     $moneyTransactionsDao = new Transactions($this->getServiceLocator(), '\\ArrayObject');
     $purchaseOrderTransactionsDao = new ExpenseTransactions($this->getServiceLocator(), '\\ArrayObject');
     $purchaseOrdersDao = new Expenses($this->getServiceLocator(), '\\ArrayObject');
     $moneyAccountsDao = new MoneyAccountsDAO($this->getServiceLocator(), '\\ArrayObject');
     $purchaseOrderTransaction = $purchaseOrderTransactionsDao->fetchOne(['id' => $purchaseOrderTransactionId], ['money_transaction_id', 'money_account_id', 'amount', 'expense_id', 'creation_date']);
     $moneyTransactionId = $purchaseOrderTransaction['money_transaction_id'];
     $purchaseOrderTransactionAmount = $purchaseOrderTransaction['amount'];
     $purchaseOrderId = $purchaseOrderTransaction['expense_id'];
     $purchaseOrder = $purchaseOrdersDao->fetchOne(['id' => $purchaseOrderId], ['ticket_balance', 'transaction_balance', 'currency_id']);
     $purchaseOrderTransactionMoneyAccountId = $purchaseOrderTransaction['money_account_id'];
     $moneyAccount = $moneyAccountsDao->fetchOne(['id' => $purchaseOrderTransactionMoneyAccountId], ['balance', 'currency_id']);
     // Remove money transaction
     $moneyTransactionsDao->delete(['id' => $moneyTransactionId]);
     // Remove purchase order transaction
     $purchaseOrderTransactionsDao->delete(['id' => $purchaseOrderTransactionId]);
     // Update money account balance
     $moneyAccountBalance = $moneyAccount['balance'];
     $moneyAccountBalance -= $purchaseOrderTransactionAmount;
     $moneyAccountsDao->update(['balance' => $moneyAccountBalance], ['id' => $purchaseOrderTransactionMoneyAccountId]);
     if ($updatePurchaseOrderBalance) {
         // Update purchase order balance
         // Update purchase order transaction balance
         if ($purchaseOrder['currency_id'] != $moneyAccount['currency_id']) {
             // PO balance and PO transaction balance are in PO currency
             // We need to convert currencies to correctly recalculate those balances
             // Please note that we need to make conversion by currency rate for that particular date when transaction was made
             $currencyVaultService = new CurrencyVault();
             $purchaseOrderTransactionAmount = $currencyVaultService->convertCurrency($purchaseOrderTransactionAmount, $moneyAccount['currency_id'], $purchaseOrder['currency_id'], $purchaseOrderTransaction['creation_date']);
             $purchaseOrdersDao->update(['ticket_balance' => $purchaseOrder['balance'] - $purchaseOrderTransactionAmount, 'transaction_balance' => $purchaseOrder['transaction_balance'] - $purchaseOrderTransactionAmount], ['id' => $purchaseOrder['id']]);
         }
     }
     return true;
 }
Ejemplo n.º 2
0
 /**
  * @param array $data
  * @return bool
  * @throws \Exception
  * @throws \RuntimeException
  */
 public function makeTransaction($data)
 {
     /**
      * @var BackofficeAuthenticationService $auth
      * @var MoneyAccount $moneyAccountDao
      * @var \DDD\Domain\Finance\Expense\Expenses $po
      * @var \DDD\Domain\MoneyAccount\MoneyAccount $moneyAccount
      * @var TransactionAccount $transactionAccountService
      */
     $transactionAccountService = $this->getServiceLocator()->get('service_finance_transaction_account');
     $moneyAccountDao = $this->getServiceLocator()->get('dao_money_account_money_account');
     $expenseTransactionDao = new ExpenseTransactions($this->getServiceLocator());
     $auth = $this->getServiceLocator()->get('library_backoffice_auth');
     $transactionDao = new Transactions($this->getServiceLocator());
     $expenseDao = new Expenses($this->getServiceLocator());
     $expenseItemDao = new ExpenseItem($this->getServiceLocator());
     $currencyService = new CurrencyVault();
     $currencyService->setServiceLocator($this->getServiceLocator());
     if (empty($data['poId']) || empty($data['itemId']) || empty($data['accountId']) || empty($data['moneyAccount']) || empty($data['transactionDate']) || empty($data['amount'])) {
         throw new \RuntimeException('Invalid data provided.');
     }
     $isRefund = 0;
     if (isset($data['isRefund']) && $data['isRefund']) {
         $isRefund = 1;
     }
     $amount = $isRefund ? $data['amount'] : -1 * $data['amount'];
     $moneyAccount = $moneyAccountDao->fetchOne(['id' => $data['moneyAccount']], ['currency_id', 'balance']);
     $po = $expenseDao->fetchOne(['id' => $data['poId']], ['ticket_balance', 'transaction_balance', 'currency_id']);
     $accountId = $transactionAccountService->getTransactionAccountIdByIdentity($data['moneyAccount'], Account::TYPE_MONEY_ACCOUNT);
     if (!$moneyAccount || !$po || !$accountId) {
         throw new \RuntimeException('Problem with PO and Money Account.');
     }
     $transactionDao->save(['account_id' => $accountId, 'currency_id' => $moneyAccount->getCurrencyId(), 'date' => $data['transactionDate'], 'description' => '', 'amount' => $amount]);
     $expenseTransactionDao->save(['money_transaction_id' => $transactionDao->lastInsertValue, 'expense_id' => $data['poId'], 'creator_id' => $auth->getIdentity()->id, 'money_account_id' => $data['moneyAccount'], 'account_to_id' => $data['accountId'], 'transaction_date' => $data['transactionDate'], 'creation_date' => date('Y-m-d H:i:s'), 'amount' => $amount, 'is_refund' => $isRefund]);
     $expenseItemDao->save(['transaction_id' => $expenseTransactionDao->lastInsertValue], ['id' => $data['itemId']]);
     $moneyAccountDao->save(['balance' => $moneyAccount->getBalance() + $amount], ['id' => $data['moneyAccount']]);
     if ($po->getCurrencyId() != $moneyAccount->getCurrencyId()) {
         $amount = $currencyService->convertCurrency($amount, (int) $moneyAccount->getCurrencyId(), (int) $po->getCurrencyId());
     }
     $expenseDao->save(['ticket_balance' => $po->getTicketBalance() + $amount, 'transaction_balance' => $po->getTransactionBalance() + $amount], ['id' => $data['poId']]);
 }