Exemplo n.º 1
0
 /**
  * @return bool
  * @throws \Exception
  */
 public function process()
 {
     /**
      * @var Expenses $expenseDao
      */
     $expenseDao = $this->getServiceLocator()->get('dao_finance_expense_expenses');
     $expenseDao->setEntity(new \ArrayObject());
     $finance = new Finance($this->getServiceLocator());
     $expenseList = $this->getExpenses();
     if (!count($expenseList)) {
         throw new \Exception('No expense attached.');
     }
     $transactionIdList = parent::processGeneralTransaction();
     if (count($transactionIdList) > 1) {
         throw new \RuntimeException('It is impossible to store more than one transaction id for expense transaction.');
     } else {
         $moneyTransactionId = array_shift($transactionIdList);
     }
     foreach ($expenseList as $expense) {
         $expenseData = $expenseDao->fetchOne(['id' => $expense['id']]);
         $expenseTicket = $finance->getExpense($expense['id']);
         $expenseTicket->prepare($this->getExpenseTicketData($expenseData, $expense['amount']));
         $expenseTicket->addTransaction($this->getTransactionData($moneyTransactionId, $expense['amount']));
         $expenseTicket->save();
     }
     // Set as verified transaction
     $this->changeVerifyStatus($moneyTransactionId, self::IS_VERIFIED);
     return $moneyTransactionId;
 }
Exemplo n.º 2
0
 /**
  * @param Finance $finance
  * @param null|int $expenseId
  */
 public function __construct(Finance $finance, $expenseId)
 {
     $this->finance = $finance;
     $this->setServiceLocator($finance->getServiceLocator());
     $this->expenseId = $expenseId;
     $this->dao = new Expenses($this->getServiceLocator());
     $this->transactionAccounDao = new TransactionAccounts($this->getServiceLocator());
 }
Exemplo n.º 3
0
 /**
  * @param array $data
  * @param ParametersInterface|bool $files
  * @param array $extra
  * @return bool
  * @throws \Exception
  */
 public function saveExpenseTicket($data, ParametersInterface $files, &$extra = [])
 {
     $data = $this->prepareData($data);
     if ($this->checkExpenseData($data)) {
         $finance = new Finance($this->getServiceLocator());
         $expenseTicket = $finance->getExpense($data['isEdit'] ? $data['ticket']['id'] : null);
         // Exact expense (ticket)
         $expenseTicket->prepare($data['ticket']);
         if (!empty($data['items']['add'])) {
             foreach ($data['items']['add'] as $item) {
                 $expenseTicket->addItem($item['data']);
             }
         }
         if (!empty($data['transactions']['add'])) {
             foreach ($data['transactions']['add'] as $item) {
                 $expenseTicket->addTransaction($item);
             }
         }
         $expenseTicket->save();
         list($ticketFiles, $itemFiles) = $this->separateAttachments($expenseTicket, $files);
         // Files
         $errorMessages = array_merge_recursive($this->saveFiles($ticketFiles, $expenseTicket), $this->saveItemFiles($itemFiles, $expenseTicket), $this->removeFiles($data['attachments']));
         // Passed by reference. Look up
         $extra = ['isEdit' => $data['isEdit'], 'ticketId' => $expenseTicket->getExpenseId(), 'errorMessages' => $errorMessages];
         return true;
     }
     return false;
 }
Exemplo n.º 4
0
 /**
  * @param int $status
  * @param int $supplierId
  * @return boolean
  */
 public function changeStatus($supplierId, $status)
 {
     $finance = new Finance($this->getServiceLocator());
     $supplier = $finance->getSupplier($supplierId);
     $supplier->save(['active' => $status]);
     return true;
 }
Exemplo n.º 5
0
 /**
  * @param int $moneyTransactionId
  * @return int
  */
 private function createExpense($moneyTransactionId)
 {
     $data = $this->constructExpenseData($moneyTransactionId);
     $finance = new Finance($this->getServiceLocator());
     $expenseTicket = $finance->getExpense();
     $expenseTicket->prepare($data['ticket']);
     if (count($data['items'])) {
         foreach ($data['items'] as $item) {
             $expenseTicket->addItem($item);
         }
     }
     $expenseTicket->addTransaction($data['transaction']);
     $expenseTicket->save();
     return $expenseTicket->getExpenseId();
 }
Exemplo n.º 6
0
 /**
  * @param \ArrayObject $postData
  * @param null|int $moneyAccountId
  * @param int $creatorId
  *
  * @return bool
  */
 public function saveMoneyAccount($postData, &$moneyAccountId, $creatorId)
 {
     try {
         // Remove submit button data
         if (isset($postData['save'])) {
             unset($postData['save']);
         }
         // Remove view_transactions data
         if (isset($postData['view_transactions'])) {
             $viewTransactions = $postData['view_transactions'];
             unset($postData['view_transactions']);
         }
         // Remove add_transactions data
         if (isset($postData['add_transactions'])) {
             $addTransactions = $postData['add_transactions'];
             unset($postData['add_transactions']);
         }
         // Remove manage_transactions data
         if (isset($postData['manage_transactions'])) {
             $manageTransactions = $postData['manage_transactions'];
             unset($postData['manage_transactions']);
         }
         // Remove manage_transactions data
         if (isset($postData['manage_account'])) {
             $accountManagers = $postData['manage_account'];
             unset($postData['manage_account']);
         }
         $finance = new Finance($this->getServiceLocator());
         $moneyAccount = $finance->getMoneyAccount($moneyAccountId);
         $postData = iterator_to_array($postData);
         if ($moneyAccountId) {
             $moneyAccount->save($postData);
         } else {
             // Bank account should be added as active
             $postData['active'] = 1;
             $moneyAccountId = $moneyAccount->create($postData);
         }
         // Delete all relations on edit
         if ($moneyAccountId) {
             $this->getMoneyAccountUsersDao()->delete(['money_account_id' => $moneyAccountId]);
         }
         // Save Money Account <-> User relations for transaction viewers
         if (isset($viewTransactions) && is_array($viewTransactions)) {
             foreach ($viewTransactions as $userId) {
                 // Create new stack of relations
                 $this->getMoneyAccountUsersDao()->save(['money_account_id' => $moneyAccountId, 'user_id' => $userId, 'operation_type' => self::OPERATION_VIEW_TRANSACTION]);
             }
         }
         // Save Money Account <-> User relations for transaction ceators
         if (isset($addTransactions) && is_array($addTransactions)) {
             foreach ($addTransactions as $userId) {
                 $this->getMoneyAccountUsersDao()->save(['money_account_id' => $moneyAccountId, 'user_id' => $userId, 'operation_type' => self::OPERATION_ADD_TRANSACTION]);
             }
         }
         // Save Money Account <-> User relations for transaction managers
         $isset = false;
         if (isset($manageTransactions) && is_array($manageTransactions)) {
             foreach ($manageTransactions as $userId) {
                 if ($userId == $creatorId) {
                     $isset = true;
                 }
                 $this->getMoneyAccountUsersDao()->save(['money_account_id' => $moneyAccountId, 'user_id' => $userId, 'operation_type' => self::OPERATION_MANAGE_TRANSACTION]);
             }
         }
         // Save Money Account <-> User relations for account managers
         $isset = false;
         if (isset($accountManagers) && is_array($accountManagers)) {
             foreach ($accountManagers as $userId) {
                 if ($userId == $creatorId) {
                     $isset = true;
                 }
                 $this->getMoneyAccountUsersDao()->save(['money_account_id' => $moneyAccountId, 'user_id' => $userId, 'operation_type' => self::OPERATION_MANAGE_ACCOUNT]);
             }
         }
         // Save creator as account manager
         if (!$isset && !$moneyAccountId) {
             $this->getMoneyAccountUsersDao()->save(['money_account_id' => $moneyAccountId, 'user_id' => $creatorId, 'operation_type' => self::OPERATION_MANAGE_ACCOUNT]);
         }
     } catch (\Exception $ex) {
         return false;
     }
     return true;
 }