Beispiel #1
0
 /**
  * @param array $data
  * @param array $extra
  * @return bool
  * @throws \Exception
  */
 public function createRefundedOrder($data, &$extra)
 {
     /**
      * @var BackofficeAuthenticationService $auth
      * @var User $userService
      */
     $auth = $this->getServiceLocator()->get('library_backoffice_auth');
     $userService = $this->getServiceLocator()->get('service_user');
     $itemDao = new ExpenseItem($this->getServiceLocator());
     $costCenterDao = new ExpenseCost($this->getServiceLocator());
     $expenseTransactionDao = new ExpenseTransactions($this->getServiceLocator(), '\\ArrayObject');
     $data = $this->prepareItemData($data);
     $errorMessages = [];
     $where = [];
     try {
         $itemDao->beginTransaction();
         $saveData = ['expense_id' => $data['poId'], 'account_id' => $data['accountId'], 'account_reference' => $data['accountReference'], 'sub_category_id' => $data['subCategoryId'], 'currency_id' => $data['currencyId'], 'amount' => $data['orderAmount'], 'type' => $data['type'], 'period_from' => $data['period']['from'], 'period_to' => $data['period']['to'], 'comment' => $data['comment'], 'tmp_money_account_id' => $data['moneyAccount'], 'tmp_transaction_date' => $data['transactionDate'], 'is_refund' => $data['isRefund'], 'creator_id' => $auth->getIdentity()->id, 'manager_id' => $userService->getBudgetHolderUserManagerId($auth->getIdentity()->id), 'date_created' => date('Y-m-d H:i:s')];
         $itemDao->save($saveData, $where);
         $data['itemId'] = $itemDao->lastInsertValue;
         if (count($data['costCenters'])) {
             $costCenterDao->delete(['expense_item_id' => $data['itemId']]);
             $costCenterAmount = $data['orderAmount'] / count($data['costCenters']);
             foreach ($data['costCenters'] as $costCenter) {
                 $costCenterDao->save(['expense_item_id' => $data['itemId'], 'cost_center_id' => $costCenter['id'], 'cost_center_type' => $costCenter['type'], 'amount' => $this->calculateCostAmount($costCenterAmount, $data['currencyId'], $costCenter['currencyId'], $data['isRefund'])]);
             }
         }
         // Create refund transaction, If it already has.
         $hasTransaction = false;
         if ($data['transactionId']) {
             $expenseTransactionDetails = $expenseTransactionDao->getTransactionDetails($data['transactionId']);
             if ($expenseTransactionDetails) {
                 $transactionData = ['poId' => $data['poId'], 'moneyAccount' => $expenseTransactionDetails['money_account_id'], 'accountId' => $expenseTransactionDetails['account_to_id'], 'transactionDate' => date('Y-m-d H:i:s'), 'amount' => $data['refundAmount'], 'itemId' => $data['itemId'], 'isRefund' => $data['isRefund']];
                 $this->makeTransaction($transactionData);
                 $hasTransaction = true;
             }
         }
         $extra = ['itemId' => $data['itemId'], 'poId' => $data['poId'], 'errorMessages' => $errorMessages, 'hasTransaction' => $hasTransaction];
         $itemDao->commitTransaction();
     } catch (\Exception $ex) {
         $itemDao->rollbackTransaction();
         throw $ex;
     }
     return true;
 }
Beispiel #2
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;
 }