Example usage for org.hibernate.criterion Projections sum

List of usage examples for org.hibernate.criterion Projections sum

Introduction

In this page you can find the example usage for org.hibernate.criterion Projections sum.

Prototype

public static AggregateProjection sum(String propertyName) 

Source Link

Document

A property value sum projection

Usage

From source file:org.linagora.linshare.core.repository.hibernate.GenericStatisticRepositoryImpl.java

License:Open Source License

@Override
public Long sumOfActualOperationSum(AbstractDomain domain, Account account, Date beginDate, Date endDate) {
    DetachedCriteria criteria = DetachedCriteria.forClass(getPersistentClass());
    criteria.add(Restrictions.between("statisticDate", beginDate, endDate));
    if (account != null) {
        criteria.add(Restrictions.eq("account", account));
    }/*  w w w.  j av  a 2  s .c om*/
    if (domain != null) {
        criteria.add(Restrictions.eq("domain", domain));
    }
    criteria.setProjection(Projections.sum("actualOperationSum"));
    List<T> list = findByCriteria(criteria);
    if (list.size() > 0 && list.get(0) != null)
        return DataAccessUtils.longResult(findByCriteria(criteria));
    return (long) 0;
}

From source file:org.linagora.linshare.core.repository.hibernate.OperationHistoryRepositoryImpl.java

License:Open Source License

@Override
public Long sumOperationValue(Account account, AbstractDomain domain, Date creationDate,
        OperationHistoryTypeEnum operationType, ContainerQuotaType containerQuotaType) {
    DetachedCriteria criteria = DetachedCriteria.forClass(getPersistentClass());
    if (account != null) {
        criteria.add(Restrictions.eq("account", account));
    }//from   w  w w .  ja v a 2 s .  c  o m
    if (domain != null) {
        criteria.add(Restrictions.eq("domain", domain));
    }
    if (operationType != null) {
        criteria.add(Restrictions.eq("operationType", operationType));
    }
    if (creationDate != null) {
        criteria.add(Restrictions.le("creationDate", creationDate));
    }
    if (containerQuotaType != null) {
        criteria.add(Restrictions.eq("containerQuotaType", containerQuotaType));
    }
    criteria.setProjection(Projections.sum("operationValue"));
    List<OperationHistory> list = findByCriteria(criteria);
    if (list.size() > 0 && list.get(0) != null) {
        return DataAccessUtils.longResult(findByCriteria(criteria));
    }
    return (long) 0;
}

From source file:org.mifos.accounts.loan.persistance.LegacyLoanDao.java

License:Open Source License

@SuppressWarnings("unchecked")
public BigDecimal getTotalOutstandingPrincipalOfLoanAccountsInActiveGoodStanding(final Short branchId,
        final Short loanOfficerId, final Short loanProductId) throws PersistenceException {
    BigDecimal loanBalanceAmount = new BigDecimal(0);
    try {//w w w.  j a  v  a  2  s .c om
        Session session = StaticHibernateUtil.getSessionTL();
        Criteria criteria = session.createCriteria(LoanBO.class)
                .setProjection(Projections.sum("loanBalance.amount"))
                .add(Restrictions.eq("accountState.id", (short) 5))
                .add(Restrictions.eq("office.officeId", branchId));
        if (loanOfficerId != (short) -1) {
            criteria.add(Restrictions.eq("personnel.personnelId", loanOfficerId));
        }
        if (loanProductId != (short) -1) {
            criteria.add(Restrictions.eq("loanOffering.prdOfferingId", loanProductId));
        }

        List list = criteria.list();
        loanBalanceAmount = (BigDecimal) list.get(0);
    } catch (Exception e) {
        throw new PersistenceException(e);
    }
    return loanBalanceAmount;
}

From source file:org.openbravo.advpaymentmngt.ad_reports.ReportReconciliation.java

License:Open Source License

/**
 * Calculates the sum of outstanding payments/deposits applying the following filters:
 * //from  ww w.j a  v a  2s  . c o  m
 * - They belong to the financial account of the reconciliation.
 * 
 * - The transaction date must be lower than the ending date of the reconciliation.
 * 
 * - They do not belong to any reconciliation.
 * 
 * @param recon
 *          Reconciliation
 * @return List with 2 values. The first one is the sum of outstanding payments (transactions) and
 *         the second is the sum of outstanding deposits (transactions).
 */
private List<BigDecimal> getOutstandingPaymentAndDepositTotal(FIN_Reconciliation recon) {
    List<BigDecimal> outList = new ArrayList<BigDecimal>();
    OBContext.setAdminMode(true);
    try {
        OBCriteria<FIN_FinaccTransaction> obcTrans = OBDal.getInstance()
                .createCriteria(FIN_FinaccTransaction.class);
        obcTrans.add(Restrictions.eq(FIN_FinaccTransaction.PROPERTY_ACCOUNT, recon.getAccount()));
        obcTrans.add(Restrictions.eq(FIN_FinaccTransaction.PROPERTY_PROCESSED, true));
        obcTrans.add(Restrictions.le(FIN_FinaccTransaction.PROPERTY_TRANSACTIONDATE, recon.getEndingDate()));
        List<FIN_Reconciliation> afterReconciliations = MatchTransactionDao
                .getReconciliationListAfterDate(recon);
        if (afterReconciliations.size() > 0) {
            obcTrans.add(Restrictions.or(Restrictions.isNull(FIN_FinaccTransaction.PROPERTY_RECONCILIATION),
                    Restrictions.in(FIN_FinaccTransaction.PROPERTY_RECONCILIATION, afterReconciliations)));
        } else {
            obcTrans.add(Restrictions.isNull(FIN_FinaccTransaction.PROPERTY_RECONCILIATION));
        }

        ProjectionList projections = Projections.projectionList();
        projections.add(Projections.sum(FIN_FinaccTransaction.PROPERTY_PAYMENTAMOUNT));
        projections.add(Projections.sum(FIN_FinaccTransaction.PROPERTY_DEPOSITAMOUNT));
        obcTrans.setProjection(projections);

        if (obcTrans.list() != null && obcTrans.list().size() > 0) {
            @SuppressWarnings("rawtypes")
            List o = obcTrans.list();
            Object[] resultSet = (Object[]) o.get(0);
            BigDecimal paymentAmt = (resultSet[0] != null) ? (BigDecimal) resultSet[0] : BigDecimal.ZERO;
            BigDecimal depositAmt = (resultSet[1] != null) ? (BigDecimal) resultSet[1] : BigDecimal.ZERO;
            outList.add(paymentAmt);
            outList.add(depositAmt);
        }

    } finally {
        OBContext.restorePreviousMode();
    }

    return outList;
}

From source file:org.openbravo.advpaymentmngt.ad_reports.ReportReconciliation.java

License:Open Source License

/**
 * Calculates the sum of un-reconciled bank statement lines applying the following filters:
 * //from www .ja  v a2  s  .  co  m
 * - They belong to the financial account of the reconciliation.
 * 
 * - The transaction date must be lower than the ending date of the reconciliation.
 * 
 * - They are not matched with any transaction.
 * 
 * @param recon
 *          Reconciliation
 * @return Sum of the un-reconciled bank statement lines.
 */
private BigDecimal getUnreconciledBankStatmentLinesTotal(FIN_Reconciliation recon) {
    BigDecimal total = BigDecimal.ZERO;
    OBContext.setAdminMode(true);
    try {
        OBCriteria<FIN_BankStatementLine> obcBsl = OBDal.getInstance()
                .createCriteria(FIN_BankStatementLine.class);
        obcBsl.createAlias(FIN_BankStatementLine.PROPERTY_BANKSTATEMENT, "bs");
        obcBsl.createAlias(FIN_BankStatementLine.PROPERTY_FINANCIALACCOUNTTRANSACTION, "tr",
                OBCriteria.LEFT_JOIN);
        obcBsl.add(Restrictions.le(FIN_BankStatementLine.PROPERTY_TRANSACTIONDATE, recon.getEndingDate()));
        List<FIN_Reconciliation> afterReconciliations = MatchTransactionDao
                .getReconciliationListAfterDate(recon);
        if (afterReconciliations.size() > 0) {
            obcBsl.add(Restrictions.or(
                    Restrictions.isNull(FIN_BankStatementLine.PROPERTY_FINANCIALACCOUNTTRANSACTION),
                    Restrictions.in("tr." + FIN_FinaccTransaction.PROPERTY_RECONCILIATION,
                            afterReconciliations)));
        } else {
            obcBsl.add(Restrictions.isNull(FIN_BankStatementLine.PROPERTY_FINANCIALACCOUNTTRANSACTION));
        }
        obcBsl.add(Restrictions.eq("bs." + FIN_BankStatement.PROPERTY_ACCOUNT, recon.getAccount()));
        obcBsl.add(Restrictions.eq("bs." + FIN_BankStatement.PROPERTY_PROCESSED, true));
        ProjectionList projections = Projections.projectionList();
        projections.add(Projections.sum(FIN_BankStatementLine.PROPERTY_CRAMOUNT));
        projections.add(Projections.sum(FIN_BankStatementLine.PROPERTY_DRAMOUNT));
        obcBsl.setProjection(projections);

        if (obcBsl.list() != null && obcBsl.list().size() > 0) {
            @SuppressWarnings("rawtypes")
            List o = obcBsl.list();
            Object[] resultSet = (Object[]) o.get(0);
            BigDecimal credit = (resultSet[0] != null) ? (BigDecimal) resultSet[0] : BigDecimal.ZERO;
            BigDecimal debit = (resultSet[1] != null) ? (BigDecimal) resultSet[1] : BigDecimal.ZERO;
            total = credit.subtract(debit);
        }

    } finally {
        OBContext.restorePreviousMode();
    }

    return total;
}

From source file:org.openbravo.advpaymentmngt.ad_reports.ReportReconciliation.java

License:Open Source License

/**
 * Calculates the sum of all the transactions in a higher date than the end date of the given
 * reconciliation.//from w w  w . ja  va2 s .c o m
 * 
 * @param recon
 *          Reconciliation.
 * @return Sum of all the transactions in a higher date than the end date of the given
 *         reconciliation.
 */
private BigDecimal getTransactionsTotalAfterReconciliationEndDate(FIN_Reconciliation recon) {
    BigDecimal balance = BigDecimal.ZERO;
    OBContext.setAdminMode(true);
    try {
        OBCriteria<FIN_FinaccTransaction> obcTrans = OBDal.getInstance()
                .createCriteria(FIN_FinaccTransaction.class);
        obcTrans.add(Restrictions.eq(FIN_FinaccTransaction.PROPERTY_ACCOUNT, recon.getAccount()));
        obcTrans.add(Restrictions.eq(FIN_FinaccTransaction.PROPERTY_PROCESSED, true));
        obcTrans.add(Restrictions.gt(FIN_FinaccTransaction.PROPERTY_TRANSACTIONDATE, recon.getEndingDate()));
        ProjectionList projections = Projections.projectionList();
        projections.add(Projections.sum(FIN_FinaccTransaction.PROPERTY_PAYMENTAMOUNT));
        projections.add(Projections.sum(FIN_FinaccTransaction.PROPERTY_DEPOSITAMOUNT));
        obcTrans.setProjection(projections);

        if (obcTrans.list() != null && obcTrans.list().size() > 0) {
            @SuppressWarnings("rawtypes")
            List o = obcTrans.list();
            Object[] resultSet = (Object[]) o.get(0);
            BigDecimal paymentAmt = (resultSet[0] != null) ? (BigDecimal) resultSet[0] : BigDecimal.ZERO;
            BigDecimal depositAmt = (resultSet[1] != null) ? (BigDecimal) resultSet[1] : BigDecimal.ZERO;
            balance = depositAmt.subtract(paymentAmt);
        }

    } finally {
        OBContext.restorePreviousMode();
    }

    return balance;
}

From source file:org.openbravo.advpaymentmngt.dao.MatchTransactionDao.java

License:Open Source License

/**
 * Calculates the ending balance of automatic reconciliations. The sum of all the bank statement
 * lines of the reconciliation financial account that belong to the given reconciliation plus the
 * ones that does not have a transaction associated yet.
 * //from w  w  w .  j  a va  2  s. co m
 * @param reconciliation
 *          Reconciliation.
 * @return Ending balance of an automatic reconciliation.
 */
@Deprecated
public static BigDecimal getReconciliationEndingBalance(FIN_Reconciliation reconciliation) {
    BigDecimal total = BigDecimal.ZERO;
    OBContext.setAdminMode(true);
    try {
        OBCriteria<FIN_BankStatementLine> obcBsl = OBDal.getInstance()
                .createCriteria(FIN_BankStatementLine.class);
        obcBsl.createAlias(FIN_BankStatementLine.PROPERTY_BANKSTATEMENT, "bs");
        obcBsl.createAlias(FIN_BankStatementLine.PROPERTY_FINANCIALACCOUNTTRANSACTION, "tr",
                OBCriteria.LEFT_JOIN);
        obcBsl.add(Restrictions.or(
                Restrictions.isNull(FIN_BankStatementLine.PROPERTY_FINANCIALACCOUNTTRANSACTION),
                Restrictions.eq("tr." + FIN_FinaccTransaction.PROPERTY_RECONCILIATION, reconciliation)));

        obcBsl.add(Restrictions.eq("bs." + FIN_BankStatement.PROPERTY_ACCOUNT, reconciliation.getAccount()));
        obcBsl.add(Restrictions.eq("bs." + FIN_BankStatement.PROPERTY_PROCESSED, true));
        ProjectionList projections = Projections.projectionList();
        projections.add(Projections.sum(FIN_BankStatementLine.PROPERTY_CRAMOUNT));
        projections.add(Projections.sum(FIN_BankStatementLine.PROPERTY_DRAMOUNT));
        obcBsl.setProjection(projections);

        @SuppressWarnings("rawtypes")
        List o = obcBsl.list();
        if (o != null && o.size() > 0) {
            Object[] resultSet = (Object[]) o.get(0);
            BigDecimal credit = (resultSet[0] != null) ? (BigDecimal) resultSet[0] : BigDecimal.ZERO;
            BigDecimal debit = (resultSet[1] != null) ? (BigDecimal) resultSet[1] : BigDecimal.ZERO;
            total = credit.subtract(debit);
        }
        o.clear();

    } finally {
        OBContext.restorePreviousMode();
    }

    return total;
}

From source file:org.openbravo.advpaymentmngt.dao.MatchTransactionDao.java

License:Open Source License

/**
 * Calculates the balance of unmatched bank statements for the given reconciliation
 * /*from   www. j  av a 2s .  c om*/
 * @param lastReconciliation
 *          Reconciliation.
 * @return Last reconciliation UnMatched balance
 */
public static BigDecimal getLastReconciliationUnmatchedBalance(FIN_Reconciliation lastReconciliation) {
    BigDecimal total = BigDecimal.ZERO;
    OBContext.setAdminMode(true);
    try {
        OBCriteria<FIN_BankStatementLine> obcBsl = OBDal.getInstance()
                .createCriteria(FIN_BankStatementLine.class);
        obcBsl.createAlias(FIN_BankStatementLine.PROPERTY_BANKSTATEMENT, "bs");
        obcBsl.createAlias(FIN_BankStatementLine.PROPERTY_FINANCIALACCOUNTTRANSACTION, "tr",
                OBCriteria.LEFT_JOIN);

        List<FIN_Reconciliation> afterReconciliations = getReconciliationListAfterDate(lastReconciliation);
        if (afterReconciliations.size() > 0) {
            obcBsl.add(Restrictions.or(
                    Restrictions.isNull(FIN_BankStatementLine.PROPERTY_FINANCIALACCOUNTTRANSACTION),
                    Restrictions.in("tr." + FIN_FinaccTransaction.PROPERTY_RECONCILIATION,
                            afterReconciliations)));
        } else {
            obcBsl.add(Restrictions.isNull(FIN_BankStatementLine.PROPERTY_FINANCIALACCOUNTTRANSACTION));
        }
        obcBsl.add(
                Restrictions.eq("bs." + FIN_BankStatement.PROPERTY_ACCOUNT, lastReconciliation.getAccount()));
        obcBsl.add(Restrictions.eq("bs." + FIN_BankStatement.PROPERTY_PROCESSED, true));
        obcBsl.add(Restrictions.le(FIN_BankStatementLine.PROPERTY_TRANSACTIONDATE,
                lastReconciliation.getTransactionDate()));
        ProjectionList projections = Projections.projectionList();
        projections.add(Projections.sum(FIN_BankStatementLine.PROPERTY_CRAMOUNT));
        projections.add(Projections.sum(FIN_BankStatementLine.PROPERTY_DRAMOUNT));
        obcBsl.setProjection(projections);

        @SuppressWarnings("rawtypes")
        List o = obcBsl.list();
        if (o != null && o.size() > 0) {
            Object[] resultSet = (Object[]) o.get(0);
            BigDecimal credit = (resultSet[0] != null) ? (BigDecimal) resultSet[0] : BigDecimal.ZERO;
            BigDecimal debit = (resultSet[1] != null) ? (BigDecimal) resultSet[1] : BigDecimal.ZERO;
            total = credit.subtract(debit);
        }
        o.clear();

    } finally {
        OBContext.restorePreviousMode();
    }

    return total;
}

From source file:org.openbravo.advpaymentmngt.dao.MatchTransactionDao.java

License:Open Source License

private static BigDecimal getBSLAmount(FIN_Reconciliation reconciliation) {
    BigDecimal total = BigDecimal.ZERO;
    OBContext.setAdminMode(false);//  www  .ja v  a2  s  .c  o m
    try {
        OBCriteria<FIN_BankStatementLine> obcBsl = OBDal.getInstance()
                .createCriteria(FIN_BankStatementLine.class);
        obcBsl.createAlias(FIN_BankStatementLine.PROPERTY_BANKSTATEMENT, "bs");
        obcBsl.createAlias(FIN_BankStatementLine.PROPERTY_FINANCIALACCOUNTTRANSACTION, "tr",
                OBCriteria.LEFT_JOIN);
        obcBsl.add(Restrictions.eq("bs." + FIN_BankStatement.PROPERTY_ACCOUNT, reconciliation.getAccount()));
        obcBsl.add(Restrictions.eq("bs." + FIN_BankStatement.PROPERTY_PROCESSED, true));
        obcBsl.add(Restrictions.le(FIN_BankStatementLine.PROPERTY_TRANSACTIONDATE,
                reconciliation.getEndingDate()));
        ProjectionList projections = Projections.projectionList();
        projections.add(Projections.sum(FIN_BankStatementLine.PROPERTY_CRAMOUNT));
        projections.add(Projections.sum(FIN_BankStatementLine.PROPERTY_DRAMOUNT));
        obcBsl.setProjection(projections);

        @SuppressWarnings("rawtypes")
        List o = obcBsl.list();
        if (o != null && o.size() > 0) {
            Object[] resultSet = (Object[]) o.get(0);
            BigDecimal credit = (resultSet[0] != null) ? (BigDecimal) resultSet[0] : BigDecimal.ZERO;
            BigDecimal debit = (resultSet[1] != null) ? (BigDecimal) resultSet[1] : BigDecimal.ZERO;
            total = credit.subtract(debit);
        }
        o.clear();

    } finally {
        OBContext.restorePreviousMode();
    }
    return total;
}

From source file:org.openbravo.costing.LandedCostProcess.java

License:Open Source License

private void matchCostWithInvoiceLine(LandedCostCost lcc) {
    LCMatched lcm = OBProvider.getInstance().get(LCMatched.class);
    lcm.setOrganization(lcc.getOrganization());
    lcm.setLandedCostCost(lcc);//ww  w.  j  a v a2  s .co m
    lcm.setAmount(lcc.getAmount());
    lcm.setInvoiceLine(lcc.getInvoiceLine());
    OBDal.getInstance().save(lcm);

    final OBCriteria<ConversionRateDoc> conversionRateDoc = OBDal.getInstance()
            .createCriteria(ConversionRateDoc.class);
    conversionRateDoc
            .add(Restrictions.eq(ConversionRateDoc.PROPERTY_INVOICE, lcm.getInvoiceLine().getInvoice()));
    ConversionRateDoc invoiceconversionrate = (ConversionRateDoc) conversionRateDoc.uniqueResult();
    Currency currency = lcc.getOrganization().getCurrency() != null ? lcc.getOrganization().getCurrency()
            : lcc.getOrganization().getClient().getCurrency();
    ConversionRate landedCostrate = FinancialUtils.getConversionRate(lcc.getLandedCost().getReferenceDate(),
            lcc.getCurrency(), currency, lcc.getOrganization(), lcc.getClient());

    if (invoiceconversionrate != null
            && invoiceconversionrate.getRate() != landedCostrate.getMultipleRateBy()) {
        BigDecimal amount = lcc.getAmount().multiply(invoiceconversionrate.getRate())
                .subtract(lcc.getAmount().multiply(landedCostrate.getMultipleRateBy()))
                .divide(landedCostrate.getMultipleRateBy(), currency.getStandardPrecision().intValue(),
                        RoundingMode.HALF_UP);
        LCMatched lcmCm = OBProvider.getInstance().get(LCMatched.class);
        lcmCm.setOrganization(lcc.getOrganization());
        lcmCm.setLandedCostCost(lcc);
        lcmCm.setAmount(amount);
        lcmCm.setInvoiceLine(lcc.getInvoiceLine());
        lcmCm.setConversionmatching(true);
        OBDal.getInstance().save(lcmCm);

        lcc.setMatched(Boolean.FALSE);
        lcc.setProcessed(Boolean.FALSE);
        lcc.setMatchingAdjusted(true);
        OBDal.getInstance().flush();
        LCMatchingProcess.doProcessLCMatching(lcc);
    }

    lcc.setMatched(Boolean.TRUE);
    lcc.setProcessed(Boolean.TRUE);
    OBCriteria<LCMatched> critMatched = OBDal.getInstance().createCriteria(LCMatched.class);
    critMatched.add(Restrictions.eq(LCMatched.PROPERTY_LANDEDCOSTCOST, lcc));
    critMatched.setProjection(Projections.sum(LCMatched.PROPERTY_AMOUNT));
    BigDecimal matchedAmt = (BigDecimal) critMatched.uniqueResult();
    if (matchedAmt == null) {
        matchedAmt = lcc.getAmount();
    }
    lcc.setMatchingAmount(matchedAmt);
    OBDal.getInstance().save(lcc);
}