/*
 * Arrebol Consultancy copyright.
 * 
 * This code belongs to Arrebol Consultancy
 * its use, redistribution or modification are prohibited 
 * without written authorization from Arrebol Consultancy.
 */
package com.arrebol.apc.model.loan;

import com.arrebol.apc.model.catalog.People;
import com.arrebol.apc.model.catalog.RouteCtlg;
import com.arrebol.apc.model.enums.ActiveStatus;
import com.arrebol.apc.model.enums.LoanStatus;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.hibernate.annotations.GenericGenerator;

/**
 *
 * @author Carlos Janitzio Zavala Lopez <janitzio.zavala@arrebol.com.mx>
 */
@Entity
@Table(name = "APC_LOAN")
public class Loan implements Serializable {

    private static final long serialVersionUID = -601238781806641208L;

    @Id
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid2")
    @Column(name = "id", length = 36)
    private String id;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(
            name = "id_loan_type",
            referencedColumnName = "id",
            nullable = false
    )
    private LoanType loanType;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(
            name = "id_customer",
            referencedColumnName = "id",
            nullable = false
    )
    private People customer;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(
            name = "id_endorsement",
            referencedColumnName = "id",
            nullable = false
    )
    private People endorsement;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(
            name = "id_route",
            referencedColumnName = "id",
            nullable = false
    )
    private RouteCtlg routeCtlg;

    @Enumerated(EnumType.STRING)
    @Column(name = "loan_status", nullable = false)
    private LoanStatus loanStatus;

    @Enumerated(EnumType.STRING)
    @Column(name = "new_customer")
    private ActiveStatus newCustomer;

    @Column(name = "amount_paid", nullable = false)
    private BigDecimal amountPaid;

    @Column(name = "amount_to_pay", nullable = false)
    private BigDecimal amountToPay;

    @Column(name = "last_reference_number", nullable = false)
    private Integer lastReferenceNumber;

    @Column(name = "comments", length = 200)
    private String comments;

    @Column(name = "created_by", nullable = false, length = 36)
    private String createdBy;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "created_on", length = 19)
    private Date createdOn;

    @Column(name = "last_updated_by", length = 36)
    private String lastUpdatedBy;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "last_updated_on", length = 19)
    private Date lastUpdatedOn;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "juridical_date", length = 19)
    private Date juridicalDate;

    @Enumerated(EnumType.STRING)
    @Column(name = "frozen")
    private ActiveStatus frozen;

    @OneToMany(
            mappedBy = "loan",
            cascade = CascadeType.ALL,
            fetch = FetchType.LAZY,
            orphanRemoval = true
    )
    private List<LoanDetails> loanDetailses;

    @OneToMany(
            mappedBy = "loan",
            cascade = CascadeType.ALL,
            fetch = FetchType.LAZY,
            orphanRemoval = true
    )
    private List<LoanByUser> loanByUsers;

    @OneToMany(
            mappedBy = "loan",
            cascade = CascadeType.ALL,
            fetch = FetchType.LAZY,
            orphanRemoval = true
    )
    private List<LoanFeeNotification> loanFeeNotifications;

    @OneToMany(
            mappedBy = "loanOld",
            cascade = CascadeType.ALL,
            fetch = FetchType.LAZY,
            orphanRemoval = true
    )
    private List<LoanByRenovation> loanOldByRenovations;

    @OneToMany(
            mappedBy = "loanNew",
            cascade = CascadeType.ALL,
            fetch = FetchType.LAZY,
            orphanRemoval = true
    )
    private List<LoanByRenovation> loanNewByRenovations;

    @OneToMany(
            mappedBy = "loan",
            cascade = CascadeType.ALL,
            fetch = FetchType.LAZY,
            orphanRemoval = true
    )
    private List<Delivery> deliverys;

    public Loan() {
    }

    /**
     *
     * @param loanType
     * @param customer
     * @param endorsement
     * @param routeCtlg
     * @param loanStatus
     * @param amountPaid
     * @param amountToPay
     * @param lastReferenceNumber
     * @param createdBy
     * @param createdOn
     * @param newCustomer
     */
    public Loan(LoanType loanType, People customer, People endorsement, RouteCtlg routeCtlg, LoanStatus loanStatus, BigDecimal amountPaid, BigDecimal amountToPay, Integer lastReferenceNumber, String createdBy, Date createdOn, ActiveStatus newCustomer) {
        this.loanType = loanType;
        this.customer = customer;
        this.endorsement = endorsement;
        this.routeCtlg = routeCtlg;
        this.loanStatus = loanStatus;
        this.amountPaid = amountPaid;
        this.amountToPay = amountToPay;
        this.lastReferenceNumber = lastReferenceNumber;
        this.createdBy = createdBy;
        this.createdOn = createdOn;
        this.newCustomer = newCustomer;
    }

    /**
     *
     * @param loanType
     * @param customer
     * @param endorsement
     * @param routeCtlg
     * @param loanStatus
     * @param amountPaid
     * @param amountToPay
     * @param lastReferenceNumber
     * @param createdBy
     * @param createdOn
     * @param newCustomer
     * @param frozen
     */
    public Loan(LoanType loanType, People customer, People endorsement, RouteCtlg routeCtlg, LoanStatus loanStatus, BigDecimal amountPaid, BigDecimal amountToPay, Integer lastReferenceNumber, String createdBy, Date createdOn, ActiveStatus newCustomer, ActiveStatus frozen) {
        this.loanType = loanType;
        this.customer = customer;
        this.endorsement = endorsement;
        this.routeCtlg = routeCtlg;
        this.loanStatus = loanStatus;
        this.amountPaid = amountPaid;
        this.amountToPay = amountToPay;
        this.lastReferenceNumber = lastReferenceNumber;
        this.createdBy = createdBy;
        this.createdOn = createdOn;
        this.newCustomer = newCustomer;
        this.frozen = frozen;
    }

    /**
     *
     * @param id
     */
    public Loan(String id) {
        this.id = id;
    }

    /**
     *
     * @param loanType
     * @param customer
     * @param endorsement
     * @param routeCtlg
     * @param loanStatus
     * @param amountPaid
     * @param amountToPay
     * @param lastReferenceNumber
     * @param comments
     * @param createdBy
     */
    public Loan(LoanType loanType, People customer, People endorsement, RouteCtlg routeCtlg, LoanStatus loanStatus, BigDecimal amountPaid, BigDecimal amountToPay, Integer lastReferenceNumber, String comments, String createdBy) {
        this.loanType = loanType;
        this.customer = customer;
        this.endorsement = endorsement;
        this.routeCtlg = routeCtlg;
        this.loanStatus = loanStatus;
        this.amountPaid = amountPaid;
        this.amountToPay = amountToPay;
        this.lastReferenceNumber = lastReferenceNumber;
        this.comments = comments;
        this.createdBy = createdBy;
        this.createdOn = new Date();
    }

    public Loan(LoanType loanType, People customer, People endorsement, RouteCtlg routeCtlg, LoanStatus loanStatus, BigDecimal amountPaid, BigDecimal amountToPay, Integer lastReferenceNumber, String createdBy) {
        this.loanType = loanType;
        this.customer = customer;
        this.endorsement = endorsement;
        this.routeCtlg = routeCtlg;
        this.loanStatus = loanStatus;
        this.amountPaid = amountPaid;
        this.amountToPay = amountToPay;
        this.lastReferenceNumber = lastReferenceNumber;
        this.createdBy = createdBy;
        this.createdOn = new Date();
    }

    /**
     * Save create on from mobile devices.
     *
     * @param loanType
     * @param customer
     * @param endorsement
     * @param routeCtlg
     * @param loanStatus
     * @param amountPaid
     * @param amountToPay
     * @param lastReferenceNumber
     * @param createdBy
     * @param createdOn
     */
    public Loan(LoanType loanType, People customer, People endorsement, RouteCtlg routeCtlg, LoanStatus loanStatus, BigDecimal amountPaid, BigDecimal amountToPay, Integer lastReferenceNumber, String comments, String createdBy, Date createdOn) {
        this.loanType = loanType;
        this.customer = customer;
        this.endorsement = endorsement;
        this.routeCtlg = routeCtlg;
        this.loanStatus = loanStatus;
        this.amountPaid = amountPaid;
        this.amountToPay = amountToPay;
        this.lastReferenceNumber = lastReferenceNumber;
        this.comments = comments;
        this.createdBy = createdBy;
        this.createdOn = createdOn;
    }

    public String getId() {
        return id;
    }

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

    public LoanType getLoanType() {
        return loanType;
    }

    public void setLoanType(LoanType loanType) {
        this.loanType = loanType;
    }

    public People getCustomer() {
        return customer;
    }

    public void setCustomer(People customer) {
        this.customer = customer;
    }

    public People getEndorsement() {
        return endorsement;
    }

    public void setEndorsement(People endorsement) {
        this.endorsement = endorsement;
    }

    public RouteCtlg getRouteCtlg() {
        return routeCtlg;
    }

    public void setRouteCtlg(RouteCtlg routeCtlg) {
        this.routeCtlg = routeCtlg;
    }

    public LoanStatus getLoanStatus() {
        return loanStatus;
    }

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

    public ActiveStatus getNewCustomer() {
        return newCustomer;
    }

    public void setNewCustomer(ActiveStatus newCustomer) {
        this.newCustomer = newCustomer;
    }

    public BigDecimal getAmountPaid() {
        return amountPaid;
    }

    public void setAmountPaid(BigDecimal amountPaid) {
        this.amountPaid = amountPaid;
    }

    public BigDecimal getAmountToPay() {
        return amountToPay;
    }

    public void setAmountToPay(BigDecimal amountToPay) {
        this.amountToPay = amountToPay;
    }

    public Integer getLastReferenceNumber() {
        return lastReferenceNumber;
    }

    public void setLastReferenceNumber(Integer lastReferenceNumber) {
        this.lastReferenceNumber = lastReferenceNumber;
    }

    public String getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    public Date getCreatedOn() {
        return createdOn;
    }

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

    public String getLastUpdatedBy() {
        return lastUpdatedBy;
    }

    public void setLastUpdatedBy(String lastUpdatedBy) {
        this.lastUpdatedBy = lastUpdatedBy;
    }

    public Date getLastUpdatedOn() {
        return lastUpdatedOn;
    }

    public void setLastUpdatedOn(Date lastUpdatedOn) {
        this.lastUpdatedOn = lastUpdatedOn;
    }

    public List<LoanDetails> getLoanDetailses() {
        return loanDetailses;
    }

    public void setLoanDetailses(List<LoanDetails> loanDetailses) {
        this.loanDetailses = loanDetailses;
    }

    public List<LoanByUser> getLoanByUsers() {
        return loanByUsers;
    }

    public void setLoanByUsers(List<LoanByUser> loanByUsers) {
        this.loanByUsers = loanByUsers;
    }

    public List<LoanFeeNotification> getLoanFeeNotifications() {
        return loanFeeNotifications;
    }

    public void setLoanFeeNotifications(List<LoanFeeNotification> loanFeeNotifications) {
        this.loanFeeNotifications = loanFeeNotifications;
    }

    public List<LoanByRenovation> getLoanOldByRenovations() {
        return loanOldByRenovations;
    }

    public void setLoanOldByRenovations(List<LoanByRenovation> loanOldByRenovations) {
        this.loanOldByRenovations = loanOldByRenovations;
    }

    public List<LoanByRenovation> getLoanNewByRenovations() {
        return loanNewByRenovations;
    }

    public void setLoanNewByRenovations(List<LoanByRenovation> loanNewByRenovations) {
        this.loanNewByRenovations = loanNewByRenovations;
    }

    public String getComments() {
        return comments;
    }

    public void setComments(String comments) {
        this.comments = comments;
    }

    public Integer getTotalFeeByLoan() {
        if (getLoanFeeNotifications() != null) {
            return getLoanFeeNotifications().size();
        } else {
            return 0;
        }
    }

    public String getAsesor() {
        if (getLoanByUsers() != null && !getLoanByUsers().isEmpty()) {
            return getLoanByUsers().get(0).getUser().getHumanResource().getFirstName() + " "
                    + getLoanByUsers().get(0).getUser().getHumanResource().getLastName();
        } else {
            return "";
        }
    }

    public List<Delivery> getDeliverys() {
        return deliverys;
    }

    public void setDeliverys(List<Delivery> deliverys) {
        this.deliverys = deliverys;
    }

    public Date getJuridicalDate() {
        return juridicalDate;
    }

    public void setJuridicalDate(Date juridicalDate) {
        this.juridicalDate = juridicalDate;
    }

    public ActiveStatus getFrozen() {
        if (frozen == null) {
            frozen = ActiveStatus.DISABLED;
        }

        return frozen;
    }

    public void setFrozen(ActiveStatus frozen) {
        this.frozen = frozen;
    }

    @Override
    public String toString() {
        return "Loan{" + "loanType=" + loanType + ", createdBy=" + createdBy + '}';
    }
    
    public String getLoanTypeByDays(){
    
    return getLoanType().getLoanTypeName() + " a " +getLoanType().getTotalDays() + " dias";
    }

}
