Search in sources :

Example 1 with QuotaCreditsVO

use of org.apache.cloudstack.quota.vo.QuotaCreditsVO in project cloudstack by apache.

the class QuotaResponseBuilderImpl method addQuotaCredits.

@Override
public QuotaCreditsResponse addQuotaCredits(Long accountId, Long domainId, Double amount, Long updatedBy, Boolean enforce) {
    Date despositedOn = _quotaService.computeAdjustedTime(new Date());
    QuotaBalanceVO qb = _quotaBalanceDao.findLaterBalanceEntry(accountId, domainId, despositedOn);
    if (qb != null) {
        throw new InvalidParameterValueException("Incorrect deposit date: " + despositedOn + " there are balance entries after this date");
    }
    QuotaCreditsVO credits = new QuotaCreditsVO(accountId, domainId, new BigDecimal(amount), updatedBy);
    credits.setUpdatedOn(despositedOn);
    QuotaCreditsVO result = _quotaCreditsDao.saveCredits(credits);
    final AccountVO account = _accountDao.findById(accountId);
    if (account == null) {
        throw new InvalidParameterValueException("Account does not exist with account id " + accountId);
    }
    final boolean lockAccountEnforcement = "true".equalsIgnoreCase(QuotaConfig.QuotaEnableEnforcement.value());
    final BigDecimal currentAccountBalance = _quotaBalanceDao.lastQuotaBalance(accountId, domainId, startOfNextDay(new Date(despositedOn.getTime())));
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("AddQuotaCredits: Depositing " + amount + " on adjusted date " + despositedOn + ", current balance " + currentAccountBalance);
    }
    // update quota account with the balance
    _quotaService.saveQuotaAccount(account, currentAccountBalance, despositedOn);
    if (lockAccountEnforcement) {
        if (currentAccountBalance.compareTo(new BigDecimal(0)) >= 0) {
            if (account.getState() == Account.State.locked) {
                s_logger.info("UnLocking account " + account.getAccountName() + " , due to positive balance " + currentAccountBalance);
                _accountMgr.enableAccount(account.getAccountName(), domainId, accountId);
            }
        } else {
            // currentAccountBalance < 0 then lock the account
            if (_quotaManager.isLockable(account) && account.getState() == Account.State.enabled && enforce) {
                s_logger.info("Locking account " + account.getAccountName() + " , due to negative balance " + currentAccountBalance);
                _accountMgr.lockAccount(account.getAccountName(), domainId, accountId);
            }
        }
    }
    String creditor = String.valueOf(Account.ACCOUNT_ID_SYSTEM);
    User creditorUser = _userDao.getUser(updatedBy);
    if (creditorUser != null) {
        creditor = creditorUser.getUsername();
    }
    QuotaCreditsResponse response = new QuotaCreditsResponse(result, creditor);
    response.setCurrency(QuotaConfig.QuotaCurrencySymbol.value());
    return response;
}
Also used : User(com.cloud.user.User) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) QuotaBalanceVO(org.apache.cloudstack.quota.vo.QuotaBalanceVO) QuotaCreditsVO(org.apache.cloudstack.quota.vo.QuotaCreditsVO) QuotaAccountVO(org.apache.cloudstack.quota.vo.QuotaAccountVO) AccountVO(com.cloud.user.AccountVO) Date(java.util.Date) BigDecimal(java.math.BigDecimal)

Example 2 with QuotaCreditsVO

use of org.apache.cloudstack.quota.vo.QuotaCreditsVO in project cloudstack by apache.

the class QuotaResponseBuilderImplTest method testAddQuotaCredits.

@Test
public void testAddQuotaCredits() {
    final long accountId = 2L;
    final long domainId = 1L;
    final double amount = 11.0;
    final long updatedBy = 2L;
    QuotaCreditsVO credit = new QuotaCreditsVO();
    credit.setCredit(new BigDecimal(amount));
    Mockito.when(quotaCreditsDao.saveCredits(Mockito.any(QuotaCreditsVO.class))).thenReturn(credit);
    Mockito.when(quotaBalanceDao.lastQuotaBalance(Mockito.anyLong(), Mockito.anyLong(), Mockito.any(Date.class))).thenReturn(new BigDecimal(111));
    Mockito.when(quotaService.computeAdjustedTime(Mockito.any(Date.class))).thenReturn(new Date());
    AccountVO account = new AccountVO();
    account.setState(Account.State.locked);
    Mockito.when(accountDao.findById(Mockito.anyLong())).thenReturn(account);
    QuotaCreditsResponse resp = quotaResponseBuilder.addQuotaCredits(accountId, domainId, amount, updatedBy, true);
    assertTrue(resp.getCredits().compareTo(credit.getCredit()) == 0);
}
Also used : QuotaCreditsVO(org.apache.cloudstack.quota.vo.QuotaCreditsVO) AccountVO(com.cloud.user.AccountVO) BigDecimal(java.math.BigDecimal) Date(java.util.Date) Test(org.junit.Test)

Aggregations

AccountVO (com.cloud.user.AccountVO)2 BigDecimal (java.math.BigDecimal)2 Date (java.util.Date)2 QuotaCreditsVO (org.apache.cloudstack.quota.vo.QuotaCreditsVO)2 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)1 User (com.cloud.user.User)1 QuotaAccountVO (org.apache.cloudstack.quota.vo.QuotaAccountVO)1 QuotaBalanceVO (org.apache.cloudstack.quota.vo.QuotaBalanceVO)1 Test (org.junit.Test)1