Subversion Repositories SmartDukaan

Rev

Rev 35316 | Rev 35864 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.dao.entity.transaction;

import com.spice.profitmandi.dao.model.DailyStatementModel;

import javax.persistence.*;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Objects;

@Entity
@Table(name = "transaction.loan")
@NamedQueries({
        @NamedQuery(name = "Loan.getLoansCountWithFofoID", query = "select new com.spice.profitmandi.dao.model.LoanCountByFofoIdModel(" +
                " cast(fofoId as int), count(id))" +
                " from Loan " +
                " where fofoId in (:fofoIds) " +
                " group by fofoId "),

        @NamedQuery(name = "Loan.findBlockedLoans", query = "SELECT l FROM Loan l WHERE l.freeDays > 364"),
        @NamedQuery(name = "Loan.getLoansForSettlement", query = "SELECT l FROM Loan l join UserWallet  uw on uw.userId=l.fofoId WHERE uw.amount > 0 and l.freeDays < 365 and l.settledOn is null"),
        @NamedQuery(name = "Loan.getStateWiseLoan", query = "SELECT l FROM Loan l join FofoStore  fs on fs.id=l.fofoId join AST ast on ast.id = fs.astId join State st on st.id= ast.stateId WHERE st.name = :stateName and  l.pendingAmount > 0 and l.settledOn is null"),

        @NamedQuery(name = "Loan.getDueLoansByFofoId", query = "SELECT new com.spice.profitmandi.dao.model.loan.TotalLoanAmountDueModel(ls.loanId, l.pendingAmount, SUM(ls.amount)) " +
                "FROM Loan l JOIN LoanStatement ls on ls.loanId=l.id " +
                "WHERE l.fofoId = :fofoId AND l.pendingAmount > 0 " +
                "GROUP BY ls.loanId")
})

public class Loan {

    @Id
    @Column(name = "id", unique = true, updatable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @Column(name = "fofo_id")
    private int fofoId;

    @Column(name = "interest_rate")
    private BigDecimal interestRate;

    @Column(name = "intial_amount")
    private BigDecimal intialAmount;

    @Column(name = "pending_amount")
    private BigDecimal pendingAmount;

    @Column(name = "created_on")
    private LocalDateTime createdOn;

    @Column(name = "settled_on")
    private LocalDateTime settledOn;

    @Column(name = "due_date")
    private LocalDateTime dueDate;

    @Column(name = "interest_accured")
    private BigDecimal interestAccrued;

    @Column(name = "interest_paid")
    private BigDecimal interestPaid;

    @Column(name = "free_days")
    private int freeDays;

    @Column(name = "cd_free_days")
    private int cdFreeDays;


    @Column(name = "invoiceNumber")
    private String invoiceNumber;

    @Transient
    private boolean loanStatus;

    @Transient
    private BigDecimal totalPending;
    @Transient
    private List<DailyStatementModel> dailyStatementModel;

    public long getDays() {
        return ChronoUnit.DAYS.between(this.getCreatedOn().toLocalDate(), this.settledOn == null ? LocalDateTime.now() : this.getSettledOn()) + 1;
    }

    public boolean isDue() {
        return this.canBeSettled() && !this.getDueDate().toLocalDate().isBefore(LocalDate.now());
    }

    public boolean isOverdue() {
        return this.canBeSettled() && this.getDueDate().toLocalDate().isBefore(LocalDate.now()) &&
                !this.getDueDate().plusDays(15).toLocalDate().isBefore(LocalDate.now());
    }

    public boolean isDefault() {
        return canBeSettled() && this.isDefault(LocalDate.now());
    }

    public boolean isDefault(LocalDate onDate) {
        return this.getSettledOn() == null && this.getDueDate().plusDays(15).toLocalDate().isBefore(onDate);
    }

    public boolean canBeSettled() {
        return this.getFreeDays() < 365 && this.settledOn == null && (this.getCdFreeDays() == 0 || this.getDays() > this.getCdFreeDays());
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public BigDecimal getTotalPending() {
        return totalPending;
    }

    public void setTotalPending(BigDecimal totalPending) {
        this.totalPending = totalPending;
    }

    public int getFofoId() {
        return fofoId;
    }

    public void setFofoId(int fofoId) {
        this.fofoId = fofoId;
    }

    public BigDecimal getInterestRate() {
        return interestRate;
    }

    public void setInterestRate(BigDecimal interestRate) {
        this.interestRate = interestRate;
    }

    public BigDecimal getIntialAmount() {
        return intialAmount;
    }

    public void setIntialAmount(BigDecimal intialAmount) {
        this.intialAmount = intialAmount;
    }

    public BigDecimal getPendingAmount() {
        return pendingAmount;
    }

    public void setPendingAmount(BigDecimal pendingAmount) {
        this.pendingAmount = pendingAmount;
    }

    public LocalDateTime getCreatedOn() {
        return createdOn;
    }

    public void setCreatedOn(LocalDateTime createdOn) {
        this.createdOn = createdOn;
    }

    public LocalDateTime getDueDate() {
        return dueDate;
    }

    public void setDueDate(LocalDateTime dueDate) {
        this.dueDate = dueDate;
    }

    public BigDecimal getInterestAccrued() {
        return interestAccrued;
    }

    public void setInterestAccrued(BigDecimal interestAccrued) {
        this.interestAccrued = interestAccrued;
    }

    public BigDecimal getInterestPaid() {
        return interestPaid;
    }

    public void setInterestPaid(BigDecimal interestPaid) {
        this.interestPaid = interestPaid;
    }

    public int getFreeDays() {
        return freeDays;
    }

    public void setFreeDays(int freeDays) {
        this.freeDays = freeDays;
    }

    public List<DailyStatementModel> getDailyStatementModel() {
        return dailyStatementModel;
    }

    public void setDailyStatementModel(List<DailyStatementModel> dailyStatementModel) {
        this.dailyStatementModel = dailyStatementModel;
    }

    public boolean isLimit() {
        return this.getFreeDays() >= 365;
    }

    public String getInvoiceNumber() {
        return invoiceNumber;
    }

    public void setInvoiceNumber(String invoiceNumber) {
        this.invoiceNumber = invoiceNumber;
    }

    @Override
    public String toString() {
        return "Loan{" +
                "id=" + id +
                ", fofoId=" + fofoId +
                ", interestRate=" + interestRate +
                ", intialAmount=" + intialAmount +
                ", pendingAmount=" + pendingAmount +
                ", createdOn=" + createdOn +
                ", settledOn=" + settledOn +
                ", dueDate=" + dueDate +
                ", interestAccrued=" + interestAccrued +
                ", interestPaid=" + interestPaid +
                ", freeDays=" + freeDays +
                ", loanStatus=" + loanStatus +
                ", totalPending=" + totalPending +
                ", dailyStatementModel=" + dailyStatementModel +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Loan loan = (Loan) o;
        return id == loan.id && fofoId == loan.fofoId && freeDays == loan.freeDays && loanStatus == loan.loanStatus && Objects.equals(interestRate, loan.interestRate) && Objects.equals(intialAmount, loan.intialAmount) && Objects.equals(pendingAmount, loan.pendingAmount) && Objects.equals(createdOn, loan.createdOn) && Objects.equals(settledOn, loan.settledOn) && Objects.equals(dueDate, loan.dueDate) && Objects.equals(interestAccrued, loan.interestAccrued) && Objects.equals(interestPaid, loan.interestPaid) && Objects.equals(totalPending, loan.totalPending) && Objects.equals(dailyStatementModel, loan.dailyStatementModel);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, fofoId, interestRate, intialAmount, pendingAmount, createdOn, settledOn, dueDate, interestAccrued, interestPaid, freeDays, loanStatus, totalPending, dailyStatementModel);
    }

    public LocalDateTime getSettledOn() {
        return settledOn;
    }

    public void setSettledOn(LocalDateTime settledOn) {
        this.settledOn = settledOn;
    }

    public boolean isLoanStatus() {
        return loanStatus;
    }

    public void setLoanStatus(boolean loanStatus) {
        this.loanStatus = loanStatus;
    }

    public int getCdFreeDays() {
        return cdFreeDays;
    }

    public void setCdFreeDays(int cdFreeDays) {
        this.cdFreeDays = cdFreeDays;
    }

    public long getLoanAgeInDays() {
        return ChronoUnit.DAYS.between(this.getCreatedOn().toLocalDate(), LocalDate.now());
    }

}