Package org.zeroexchange.model.resource.money.che

Examples of org.zeroexchange.model.resource.money.che.CHECreditStatus


    /**
     * {@inheritDoc}
     */
    @Override
    public void markReserved(BigDecimal amount, Integer userId) {
        CHECreditStatus status = getOrCreateByUserId(userId);
        status.setReserved(status.getReserved().add(amount));
        getSession().save(status);
    }
View Full Code Here


    /**
     * {@inheritDoc}
     */
    @Override
    public BigDecimal getReserved(Integer userId) {
        CHECreditStatus status = getOrCreateByUserId(userId);
        return status.getReserved();
    }
View Full Code Here

    /**
     * {@inheritDoc}
     */
    @Override
    public void freeAmount(BigDecimal amount, Integer userId) {
        CHECreditStatus status = getOrCreateByUserId(userId);
        if(status != null && amount != null) {
            if(status.getReserved().compareTo(amount) > 0) {
                status.setReserved(status.getReserved().subtract(amount));
            } else {
                status.setReserved(BigDecimal.ZERO);
            }
            getSession().save(status);
        }
    }
View Full Code Here

     * Returns credit status by the user's id OR
     * creates it if not exists.
     */
    @Override
    public CHECreditStatus getOrCreateByUserId(Integer userId) {
        CHECreditStatus status = getByUserId(userId);
        if(status == null) {
            status = new CHECreditStatus();
            User userStub = new User();
            userStub.setId(userId);
            status.setUser(userStub);
        }
        return status;
    }
View Full Code Here

     */
    @Override
    public BigDecimal getAvailableAmount(ZECurrency currencyCode, User user) {
        BigDecimal amount = getPaymentsSum(currencyCode, user);
       
        CHECreditStatus status = creditStatusDAO.getOrCreateByUserId(user.getId());
       
        BigDecimal mutualBalance = creditLineReader.getMutualBalance(user);
        amount = amount.add(mutualBalance);
        BigDecimal reserved = status.getReserved();
        if(reserved != null) {
            amount = amount.subtract(reserved);
        }
        return amount;
    }
View Full Code Here

    /**
     * Returns payments sum value.
     */
    protected BigDecimal getPaymentsSum(ZECurrency currencyCode, User user) {
        CHECreditStatus status = creditStatusDAO.getOrCreateByUserId(user.getId());
        Collection<CHEPayment> payments = status.getPayments();
        BigDecimal amount = BigDecimal.ZERO;
               
        long currentMills = System.currentTimeMillis();
        for(CHEPayment payment: payments) {
            BigDecimal paymentCurrentAmount = calculatePaymentCurrentAmount(payment, currentMills);
View Full Code Here

        if(BigDecimal.ZERO.compareTo(amount) > 0) {
            //Free reserved amount
            creditStatusDAO.freeAmount(amount.negate(), consumer.getId());
        }
       
        CHECreditStatus status = creditStatusDAO.getOrCreateByUserId(consumer.getId());
       
        BigDecimal debt = status.getDebt();
       
        if(amount.compareTo(BigDecimal.ZERO) > 0) {
            //amount > 0
            if(debt.compareTo(amount) > 0) {
                status.setDebt(debt.subtract(amount));
            } else {
                status.setDebt(BigDecimal.ZERO);
                amount = amount.subtract(debt);
               
                CHEPayment newPayment = new CHEPayment();
                newPayment.setCreditStatus(status);
                newPayment.setValue(amount);
                newPayment.setPaymentDate(new Date());
                status.getPayments().add(newPayment);
            }
        } else {
            //amount <= 0
           
            //'Eat' payments first
            Collection<CHEPayment> payments = status.getPayments();
            for(Iterator<CHEPayment> paymentIt = payments.iterator(); paymentIt.hasNext();) {
                CHEPayment payment = paymentIt.next();
                BigDecimal paymentValue = payment.getValue();
                if(paymentValue.compareTo(amount.negate()) < 0) {
                    //paymentValue < -amount
                    amount = amount.add(paymentValue);//amount <= 0,paymentValue >= 0
                    paymentIt.remove();
                } else {
                    //paymentValue >= -amount
                    payment.setValue(paymentValue.add(amount)); //amount <= 0
                    amount = BigDecimal.ZERO;
                    break;
                }
            }
           
            //Update debt if no more payments exists
            status.setDebt(debt.subtract(amount));//NOTE: amount <= 0, debt >= 0
        }

        creditStatusDAO.save(status);//Commit status changes
    }
View Full Code Here

            if(log.isWarnEnabled()) {
                log.warn("UserLogged event does not contain user!");
            }
            return;
        }
        CHECreditStatus status = creditStatusDAO.getOrCreateByUserId(user.getId());
        Collection<CHEPayment> payments = status.getPayments();
       
        long currentMills = System.currentTimeMillis();
        for(Iterator<CHEPayment> paymentIt = payments.iterator(); paymentIt.hasNext();) {
            CHEPayment payment = paymentIt.next();
            BigDecimal paymentCurrentAmount = calculatePaymentCurrentAmount(payment, currentMills);
View Full Code Here

TOP

Related Classes of org.zeroexchange.model.resource.money.che.CHECreditStatus

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.