Beispiel #1
0
 /**
  * @param  $data
  * @return bool
  */
 public function createPOItemTransaction($data, $files)
 {
     /**
      * @var ExpenseTicket $financeService
      * @var \DDD\Domain\WHOrder\Order $order
      */
     $financeService = $this->getServiceLocator()->get('service_finance_expense_expense_ticket');
     $expenseItemDao = new ExpenseItem($this->getServiceLocator(), '\\ArrayObject');
     $orderDao = $this->getOrderDao();
     if (!is_array($data)) {
         $data = json_decode($data, true);
     }
     $order = $orderDao->fetchOne(['id' => $data['order_id']], ['po_item_id']);
     if ($order && $order->getPoItemId()) {
         try {
             $expenseItemDao->beginTransaction();
             $poItem = $expenseItemDao->fetchOne(['id' => $order->getPoItemId()], ['account_id', 'expense_id']);
             if ($poItem && $data['supplier_id'] && $poItem['expense_id']) {
                 $transactionAccountsDao = $this->getServiceLocator()->get('dao_finance_transaction_transaction_accounts');
                 $transactionAccountId = $transactionAccountsDao->fetchOne(['holder_id' => $data['supplier_id'], 'type' => 4], ['id'])->getId();
                 if (is_null($transactionAccountId)) {
                     throw new \Exception();
                 }
                 $moneyAccountDao = new MoneyAccount($this->getServiceLocator(), '\\ArrayObject');
                 $moneyAccountCurrencyId = $moneyAccountDao->getCurrencyId($data['money_account_id']);
                 $newItemAmountInOldItemCurrency = $financeService->recalculateTicketBalance($order->getPoItemId(), $data['amount'], $moneyAccountCurrencyId);
                 if ($newItemAmountInOldItemCurrency === false) {
                     $newItemAmountInOldItemCurrency = $data['amount'];
                 }
                 $expenseItemDao->save(['account_id' => $transactionAccountId, 'amount' => $newItemAmountInOldItemCurrency], ['id' => $order->getPoItemId()]);
                 $transactionData = ['poId' => $poItem['expense_id'], 'itemId' => $order->getPoItemId(), 'accountId' => $transactionAccountId, 'moneyAccount' => $data['money_account_id'], 'transactionDate' => date('Y-m-d', strtotime($data['transaction_date'])), 'amount' => $data['amount']];
                 $orderDao->save(['price' => $data['amount'], 'currency_id' => $moneyAccountCurrencyId], ['id' => $data['order_id']]);
                 $financeService->makeTransaction($transactionData);
                 //attachment processing
                 if ($files->count()) {
                     $errorMessages = $financeService->saveItemFile($files, ['itemId' => $order->getPoItemId()]);
                     if (count($errorMessages)) {
                         throw new \Exception('Cannot save item attachment. ' . print_r($errorMessages, true));
                     }
                     $expenseItemAttachmentsDao = $this->getServiceLocator()->get('dao_finance_expense_expense_item_attachments');
                     $attachmentInfo = $expenseItemAttachmentsDao->getAttachmentsToRemove($order->getPoItemId());
                     $financeService->moveTmpItem($attachmentInfo->getExpenseId(), $order->getPoItemId(), $attachmentInfo->getDateCreated(), $attachmentInfo->getFilename());
                 }
                 $expenseItemDao->commitTransaction();
                 return $moneyAccountCurrencyId;
             }
         } catch (\Exception $e) {
             $expenseItemDao->rollbackTransaction();
             return false;
         }
     }
     return false;
 }
Beispiel #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']]);
 }
Beispiel #3
0
 /**
  * @param int $backofficeUserId
  * @return bool
  */
 public function changePOItemManager($backofficeUserId)
 {
     /**
      * @var User $userService
      */
     $userService = $this->getServiceLocator()->get('service_user');
     $poItemDao = new ExpenseItem($this->getServiceLocator(), '\\ArrayObject');
     $poItemDao->save(['manager_id' => $userService->getBudgetHolderUserManagerId($backofficeUserId)], ['manager_id' => $backofficeUserId]);
 }