/*
 * 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.views;

import com.arrebol.apc.model.enums.ActiveStatus;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.Immutable;

/**
 *
 * @author Oscar Armando Vargas Cardenas <oscar.vargas@arrebol.com.mx>
 */
@Entity
@Immutable
@Table(name = "APC_INFORMATION_LOAN_LAST_WEEK_VIEW")
public class InformationLoanLastWeekView implements Serializable{

    private static final long serialVersionUID = -6384789811291615117L;
    
    @Id
    @Column(name = "id", length = 36)
    private String id;
    
    @Column(name = "loan_type_name")
    private String loanTypeName;
    
    @Column(name = "id_office")
    private String idOffice;
    
    @Column(name = "id_user")
    private String idUser;
    
    @Column(name = "estatus_prestamo")
    private String estatusPrestamo;
    
    @Column(name = "fecha")
    private Date fecha;
    
    @Column(name = "apoyos")
    private BigDecimal apoyos;
    
    @Column(name = "apoyos_total")
    private BigDecimal apoyosTotal;
    
    @Column(name = "comision_apertura")
    private BigDecimal comisionApertura;
    
    @Column(name = "aval")
    private String aval;
    
    @Column(name = "customer")
    private String customer;
    
    @Column(name = "documento_por")
    private BigDecimal documentoPor;
    
    @Column(name = "abono_diario")
    private BigDecimal abonoDiario;
    
    @Column(name = "amount_paid")
    private BigDecimal amountPaid;
    
    @Column(name = "saldo_insoluto")
    private BigDecimal saldoInsoluto;
    
    @Column(name = "route_name")
    private String routeName;
    
    @Column(name = "asesor")
    private String asesor;
    
    @Column(name = "num_fee")
    private int numFee;
    
    @Column(name = "payment_monday")
    private BigDecimal paymentMonday;
    
    @Column(name = "fee_monday")
    private BigDecimal feeMonday;
    
    @Column(name = "payment_tuesday")
    private BigDecimal paymentTuesday;
    
    @Column(name = "fee_tuesday")
    private BigDecimal feeTuesday;
    
    @Column(name = "payment_wednesday")
    private BigDecimal paymentWednesday;
    
    @Column(name = "fee_wednesday")
    private BigDecimal feeWednesday;
    
    @Column(name = "payment_thursday")
    private BigDecimal paymentThursday;
    
    @Column(name = "fee_thursday")
    private BigDecimal feeThursday;
    
    @Column(name = "payment_friday")
    private BigDecimal paymentFriday;
    
    @Column(name = "fee_friday")
    private BigDecimal feeFriday;
    
    @Column(name = "payment_saturday")
    private BigDecimal paymentSaturday;
    
    @Column(name = "fee_saturday")
    private BigDecimal feeSaturday;
    
    @Column(name = "faltante")
    private BigDecimal faltante;
    
    @Column(name = "new_customer")
    private String newCustomer;
    
    @Column(name = "renovation")
    private String renovation;
    
    @Column(name = "abono_semana_actual")
    private BigDecimal abonoSemanaActual;
    
    @Column(name = "fee_semana_actual")
    private BigDecimal feeSemanaActual;
    
    @Column(name = "num_pagos_all")
    private BigDecimal numPagosAll;
    
    @Column(name = "num_pagos_week")
    private BigDecimal numPagosWeek;
    
    @Column(name = "fee_todos")
    private BigDecimal feeTodos;
    
    @Enumerated(EnumType.STRING)
    @Column(name = "frozen")
    private ActiveStatus frozen;

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

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

    public String getId() {
        return id;
    }

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

    public void setLoanTypeName(String loanTypeName) {
        this.loanTypeName = loanTypeName;
    }
    
    public String getEstatusPrestamo() {
        return estatusPrestamo;
    }

    public void setEstatusPrestamo(String estatusPrestamo) {
        this.estatusPrestamo = estatusPrestamo;
    }

    public String getIdOffice() {
        return idOffice;
    }

    public String getIdUser() {
        return idUser;
    }

    public void setIdUser(String idUser) {
        this.idUser = idUser;
    }

    public void setIdOffice(String idOffice) {
        this.idOffice = idOffice;
    }

    public Date getFecha() {
        return fecha;
    }

    public void setFecha(Date fecha) {
        this.fecha = fecha;
    }

    public BigDecimal getApoyos() {
        return apoyos;
    }

    public void setApoyos(BigDecimal apoyos) {
        this.apoyos = apoyos;
    }

    public BigDecimal getApoyosTotal() {
        return apoyosTotal;
    }

    public void setApoyosTotal(BigDecimal apoyosTotal) {
        this.apoyosTotal = apoyosTotal;
    }

    public BigDecimal getComisionApertura() {
        return comisionApertura;
    }

    public void setComisionApertura(BigDecimal comisionApertura) {
        this.comisionApertura = comisionApertura;
    }

    public String getAval() {
        return aval;
    }

    public void setAval(String aval) {
        this.aval = aval;
    }

    public String getCustomer() {
        return customer;
    }

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

    public BigDecimal getDocumentoPor() {
        return documentoPor.subtract(feeSemanaActual);
    }

    public void setDocumentoPor(BigDecimal documentoPor) {
        this.documentoPor = documentoPor;
    }

    public BigDecimal getAbonoDiario() {
        return abonoDiario;
    }

    public void setAbonoDiario(BigDecimal abonoDiario) {
        this.abonoDiario = abonoDiario;
    }

    public BigDecimal getAmountPaid() {
        return amountPaid.subtract(abonoSemanaActual);
    }

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

    public BigDecimal getSaldoInsoluto() {
        return (documentoPor.subtract(feeSemanaActual)).subtract((amountPaid.subtract(abonoSemanaActual)));
    }

    public void setSaldoInsoluto(BigDecimal saldoInsoluto) {
        this.saldoInsoluto = saldoInsoluto;
    }

    public String getRouteName() {
        return routeName;
    }

    public void setRouteName(String routeName) {
        this.routeName = routeName;
    }

    public String getAsesor() {
        return asesor;
    }

    public void setAsesor(String asesor) {
        this.asesor = asesor;
    }

    public int getNumFee() {
        return numFee;
    }

    public void setNumFee(int numFee) {
        this.numFee = numFee;
    }

    public BigDecimal getPaymentMonday() {
        return paymentMonday;
    }

    public void setPaymentMonday(BigDecimal paymentMonday) {
        this.paymentMonday = paymentMonday;
    }

    public BigDecimal getFeeMonday() {
        return feeMonday;
    }

    public void setFeeMonday(BigDecimal feeMonday) {
        this.feeMonday = feeMonday;
    }

    public BigDecimal getPaymentTuesday() {
        return paymentTuesday;
    }

    public void setPaymentTuesday(BigDecimal paymentTuesday) {
        this.paymentTuesday = paymentTuesday;
    }

    public BigDecimal getFeeTuesday() {
        return feeTuesday;
    }

    public void setFeeTuesday(BigDecimal feeTuesday) {
        this.feeTuesday = feeTuesday;
    }

    public BigDecimal getPaymentWednesday() {
        return paymentWednesday;
    }

    public void setPaymentWednesday(BigDecimal paymentWednesday) {
        this.paymentWednesday = paymentWednesday;
    }

    public BigDecimal getFeeWednesday() {
        return feeWednesday;
    }

    public void setFeeWednesday(BigDecimal feeWednesday) {
        this.feeWednesday = feeWednesday;
    }

    public BigDecimal getPaymentThursday() {
        return paymentThursday;
    }

    public void setPaymentThursday(BigDecimal paymentThursday) {
        this.paymentThursday = paymentThursday;
    }

    public BigDecimal getFeeThursday() {
        return feeThursday;
    }

    public void setFeeThursday(BigDecimal feeThursday) {
        this.feeThursday = feeThursday;
    }

    public BigDecimal getPaymentFriday() {
        return paymentFriday;
    }

    public void setPaymentFriday(BigDecimal paymentFriday) {
        this.paymentFriday = paymentFriday;
    }

    public BigDecimal getFeeFriday() {
        return feeFriday;
    }

    public void setFeeFriday(BigDecimal feeFriday) {
        this.feeFriday = feeFriday;
    }

    public BigDecimal getPaymentSaturday() {
        return paymentSaturday;
    }

    public void setPaymentSaturday(BigDecimal paymentSaturday) {
        this.paymentSaturday = paymentSaturday;
    }

    public BigDecimal getFeeSaturday() {
        return feeSaturday;
    }

    public void setFeeSaturday(BigDecimal feeSaturday) {
        this.feeSaturday = feeSaturday;
    }

    public BigDecimal getFeeTodos() {
        return feeTodos;
    }

    public void setFeeTodos(BigDecimal feeTodos) {
        this.feeTodos = feeTodos;
    }

    public BigDecimal getFaltante() {
        
        BigDecimal montoPagadoTotalEsperado = (numPagosAll.subtract(numPagosWeek)).multiply(abonoDiario);
        
        BigDecimal montoPagadoSemana = paymentFriday.add(paymentMonday).add(paymentSaturday).add(paymentThursday).add(paymentTuesday).add(paymentWednesday);
        BigDecimal montoPagadoTotal = getAmountPaid().subtract(montoPagadoSemana);
        
        BigDecimal faltanteAcomulado = (montoPagadoTotalEsperado.add(feeTodos)).subtract(montoPagadoTotal);
        
        
        BigDecimal montoEsperadoSemana = numPagosWeek.multiply(abonoDiario);
        
        BigDecimal feeSemana =  feeFriday.add(feeMonday).add(feeSaturday).add(feeThursday).add(feeTuesday).add(feeWednesday);
        
        //RN1
        //Si el pago esperado por semana es igual al total de abonos en la semana, entonces faltante 0
        if(montoPagadoSemana.compareTo(montoEsperadoSemana) == 0)
        {
            return BigDecimal.ZERO;
        }
                
        // RN4
        if(renovation.equalsIgnoreCase("Si") && faltante.compareTo(BigDecimal.ZERO) > 0){
            return BigDecimal.ZERO;
        }
        
        // RN5
        if (numPagosAll.compareTo(BigDecimal.valueOf(22)) > 0 && faltante.compareTo(BigDecimal.ZERO) > 0) {
            return BigDecimal.ZERO;
        }
        
        // RN6
        if (loanTypeName.toLowerCase().contains("con") || loanTypeName.toLowerCase().contains("conv")
                || loanTypeName.toLowerCase().contains("conve") || loanTypeName.toLowerCase().contains("convenio")) {
            return BigDecimal.ZERO;
        }
        
        //RN7
        if(getFrozen().equals(ActiveStatus.ENEBLED)){
            return BigDecimal.ZERO;
        }
        
        if(numPagosAll.compareTo(BigDecimal.valueOf(22)) > 0)
        {
            if((numPagosAll.subtract(numPagosWeek)).compareTo(BigDecimal.valueOf(22)) > 0)
            {
                return montoPagadoSemana.negate();
            }
            BigDecimal temp = numPagosAll.subtract(BigDecimal.valueOf(22));
            if(temp.compareTo(BigDecimal.ONE) == 0)
            {
                return (paymentFriday.add(paymentSaturday)).negate();
            }
            if(temp.compareTo(BigDecimal.valueOf(2)) == 0)
            {
                return (paymentFriday.add(paymentSaturday).add(paymentThursday)).negate();
            }
            if(temp.compareTo(BigDecimal.valueOf(3)) == 0)
            {
                return (paymentFriday.add(paymentSaturday).add(paymentThursday).add(paymentWednesday)).negate();
            }
            if(temp.compareTo(BigDecimal.valueOf(4)) == 0)
            {
                return (paymentFriday.add(paymentSaturday).add(paymentThursday).add(paymentWednesday).add(paymentTuesday)).negate();
            }
        }
        
        //RN2
        //Si el total de abonos por semana es menor al total de abonos esperado, entonces se muestra la diferencia
        if(montoPagadoSemana.compareTo(montoEsperadoSemana) < 0)
        {
            if(getSaldoInsoluto().compareTo(BigDecimal.ZERO) == 0)
            {
                if(apoyosTotal.compareTo(getAmountPaid()) < 0)
                {
                    BigDecimal esperadoAntesDe = (numPagosAll.subtract(numPagosWeek)).multiply(abonoDiario);
                    BigDecimal pagadoAntesDe = getAmountPaid().subtract(montoPagadoSemana);
                    BigDecimal faltanteAntesDe = feeTodos.subtract(feeSemana);
                    BigDecimal faltanteAcomuladoAntesDe = (esperadoAntesDe.add(faltanteAntesDe)).subtract(pagadoAntesDe);
                    if(faltanteAcomuladoAntesDe.compareTo(BigDecimal.ZERO) > 0)
                    {
                        if(faltanteAcomuladoAntesDe.compareTo(montoPagadoSemana) == 0)
                        {
                            return montoPagadoSemana.negate();
                        }
                        if(faltanteAcomuladoAntesDe.compareTo(montoPagadoSemana) < 0)
                        {
                            return faltanteAcomuladoAntesDe.negate();
                        }
                        if(faltanteAcomuladoAntesDe.compareTo(montoPagadoSemana) > 0)
                        {
                            return montoPagadoSemana.negate();
                        }
                    }
                }
                return BigDecimal.ZERO;
            }
            if(faltanteAcomulado.compareTo(BigDecimal.ZERO) < 0)
            {
                if(faltanteAcomulado.abs().compareTo((montoEsperadoSemana.subtract(montoPagadoSemana))) == 0)
                {
                    return BigDecimal.ZERO;
                }
                if(faltanteAcomulado.abs().compareTo((montoEsperadoSemana.subtract(montoPagadoSemana))) < 0)
                {
                    return (montoEsperadoSemana.subtract(montoPagadoSemana)).subtract(faltanteAcomulado.abs());
                }
                if(faltanteAcomulado.abs().compareTo((montoEsperadoSemana.subtract(montoPagadoSemana))) > 0)
                {
                    return BigDecimal.ZERO;
                }
            }
            
            if(getSaldoInsoluto().compareTo((montoEsperadoSemana.subtract(montoPagadoSemana))) == 0)
            {
                return montoEsperadoSemana.subtract(montoPagadoSemana);
            }
            
            if(getSaldoInsoluto().compareTo((montoEsperadoSemana.subtract(montoPagadoSemana))) < 0)
            {
                return getSaldoInsoluto();
            }
            
            if(getSaldoInsoluto().compareTo((montoEsperadoSemana.subtract(montoPagadoSemana))) > 0)
            {
                return montoEsperadoSemana.subtract(montoPagadoSemana);
            }
        }
        
        //RN3
        //Si el total de abonos por semana es mayor al total de abonos esperado, 
        if(montoPagadoSemana.compareTo(montoEsperadoSemana) > 0)
        {
            if(faltanteAcomulado.compareTo(BigDecimal.ZERO) < 0)
            {
                return BigDecimal.ZERO;
            }
            
            if(faltanteAcomulado.compareTo(BigDecimal.ZERO) == 0)
            {
                if(feeSemana.compareTo(BigDecimal.ZERO) > 0)
                {
                  if(feeSemana.compareTo(montoPagadoSemana.subtract(montoEsperadoSemana)) > 0)
                  {
                      return (montoPagadoSemana.subtract(montoEsperadoSemana)).negate();
                  }
                  else
                  {
                      return feeSemana.negate();
                  }
                }
                return BigDecimal.ZERO;
            }
            
            if(faltanteAcomulado.compareTo(BigDecimal.ZERO) > 0)
            {
                if(faltanteAcomulado.compareTo((montoPagadoSemana.subtract(montoEsperadoSemana))) == 0)
                {
                   return faltanteAcomulado.negate();
                }
                if(faltanteAcomulado.compareTo((montoPagadoSemana.subtract(montoEsperadoSemana))) > 0)
                {
                   return (montoPagadoSemana.subtract(montoEsperadoSemana)).negate();
                }
                if(faltanteAcomulado.compareTo((montoPagadoSemana.subtract(montoEsperadoSemana))) < 0)
                {
                   return faltanteAcomulado.negate();
                }
            }
        }      
        return faltante;
    }

    public void setFaltante(BigDecimal faltante) {
        this.faltante = faltante;
    }

    public String getNewCustomer() {
        return newCustomer;
    }

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

    public String getRenovation() {
        return renovation;
    }

    public void setRenovation(String renovation) {
        this.renovation = renovation;
    }

    public BigDecimal getAbonoSemanaActual() {
        return abonoSemanaActual;
    }

    public void setAbonoSemanaActual(BigDecimal abonoSemanaActual) {
        this.abonoSemanaActual = abonoSemanaActual;
    }

    public BigDecimal getFeeSemanaActual() {
        return feeSemanaActual;
    }

    public void setFeeSemanaActual(BigDecimal feeSemanaActual) {
        this.feeSemanaActual = feeSemanaActual;
    }

    public BigDecimal getNumPagosAll() {
        return numPagosAll;
    }

    public void setNumPagosAll(BigDecimal numPagosAll) {
        this.numPagosAll = numPagosAll;
    }

    public BigDecimal getNumPagosWeek() {
        return numPagosWeek;
    }

    public void setNumPagosWeek(BigDecimal numPagosWeek) {
        this.numPagosWeek = numPagosWeek;
    }

    public String getConditionLunes()   {
        if(feeMonday.compareTo(BigDecimal.ZERO) > 0)
            return "redRow";
        else if(paymentMonday.compareTo(BigDecimal.ZERO) == 0 && feeMonday.compareTo(BigDecimal.ZERO) == 0)
            return "greenRow";
        else if(feeMonday.compareTo(BigDecimal.ZERO) == 0 && paymentMonday.compareTo(BigDecimal.ZERO) > 0 && (paymentMonday.compareTo(abonoDiario) < 0))
            return "yellowRow";
        else if(feeMonday.compareTo(BigDecimal.ZERO) == 0 && paymentMonday.compareTo(BigDecimal.ZERO) > 0 && (paymentMonday.compareTo(abonoDiario) > 0))
            return "blueRow";
        else
            return null;
    }
    
    public String getConditionMartes()   {
        if(feeTuesday.compareTo(BigDecimal.ZERO) > 0)
            return "redRow";
        else if(paymentTuesday.compareTo(BigDecimal.ZERO) == 0 && feeTuesday.compareTo(BigDecimal.ZERO) == 0)
            return "greenRow";
        else if(feeTuesday.compareTo(BigDecimal.ZERO) == 0 && paymentTuesday.compareTo(BigDecimal.ZERO) > 0 && (paymentTuesday.compareTo(abonoDiario) < 0))
            return "yellowRow";
        else if(feeTuesday.compareTo(BigDecimal.ZERO) == 0 && paymentTuesday.compareTo(BigDecimal.ZERO) > 0 && (paymentTuesday.compareTo(abonoDiario) > 0))
            return "blueRow";
        else
            return null;
    }
          
    public String getConditionMiercoles()   {
        if(feeWednesday.compareTo(BigDecimal.ZERO) > 0)
            return "redRow";
        else if(paymentWednesday.compareTo(BigDecimal.ZERO) == 0 && feeWednesday.compareTo(BigDecimal.ZERO) == 0)
            return "greenRow";
        else if(feeWednesday.compareTo(BigDecimal.ZERO) == 0 && paymentWednesday.compareTo(BigDecimal.ZERO) > 0 && (paymentWednesday.compareTo(abonoDiario) < 0))
            return "yellowRow";
        else if(feeWednesday.compareTo(BigDecimal.ZERO) == 0 && paymentWednesday.compareTo(BigDecimal.ZERO) > 0 && (paymentWednesday.compareTo(abonoDiario) > 0))
            return "blueRow";
        else
            return null;
    }
    
    public String getConditionJueves()   {
        if(feeThursday.compareTo(BigDecimal.ZERO) > 0)
            return "redRow";
        else if(paymentThursday.compareTo(BigDecimal.ZERO) == 0 && feeThursday.compareTo(BigDecimal.ZERO) == 0)
            return "greenRow";
        else if(feeThursday.compareTo(BigDecimal.ZERO) == 0 && paymentThursday.compareTo(BigDecimal.ZERO) > 0 && (paymentThursday.compareTo(abonoDiario) < 0))
            return "yellowRow";
        else if(feeThursday.compareTo(BigDecimal.ZERO) == 0 && paymentThursday.compareTo(BigDecimal.ZERO) > 0 && (paymentThursday.compareTo(abonoDiario) > 0))
            return "blueRow";
        else
            return null;
    }
    
    public String getConditionViernes()   {
        if(feeFriday.compareTo(BigDecimal.ZERO) > 0)
            return "redRow";
        else if(paymentFriday.compareTo(BigDecimal.ZERO) == 0 && feeFriday.compareTo(BigDecimal.ZERO) == 0)
            return "greenRow";
        else if(feeFriday.compareTo(BigDecimal.ZERO) == 0 && paymentFriday.compareTo(BigDecimal.ZERO) > 0 && (paymentFriday.compareTo(abonoDiario) < 0))
            return "yellowRow";
        else if(feeFriday.compareTo(BigDecimal.ZERO) == 0 && paymentFriday.compareTo(BigDecimal.ZERO) > 0 && (paymentFriday.compareTo(abonoDiario) > 0))
            return "blueRow";
        else
            return null;
    }
    
    public String getConditionSabado()   {
        if(feeSaturday.compareTo(BigDecimal.ZERO) > 0)
            return "redRow";
        else if(paymentSaturday.compareTo(BigDecimal.ZERO) == 0 && feeSaturday.compareTo(BigDecimal.ZERO) == 0)
            return "greenRow";
        else if(feeSaturday.compareTo(BigDecimal.ZERO) == 0 && paymentSaturday.compareTo(BigDecimal.ZERO) > 0 && (paymentSaturday.compareTo(abonoDiario) < 0))
            return "yellowRow";
        else if(feeSaturday.compareTo(BigDecimal.ZERO) == 0 && paymentSaturday.compareTo(BigDecimal.ZERO) > 0 && (paymentSaturday.compareTo(abonoDiario) > 0))
            return "blueRow";
        else
            return null;
    }
    
    public String getConditionNewCustomer()   {
        if(newCustomer.equalsIgnoreCase("Si"))
            return "greenRow";
        else
            return null;
    }
       
    public String getConditionRenovation()   {
        if (getFrozen().equals(ActiveStatus.ENEBLED)) {
            return "blueLightRow";
        }
        if (newCustomer.equalsIgnoreCase("Si"))
            return "orangeRow";
        else if(renovation.equalsIgnoreCase("Si"))
            return "greenLigthRow";
        else if(getSaldoInsoluto().compareTo(BigDecimal.ZERO) == 0)
            return "greenStrongRow";
        else
            return null;
    }
    
    public int getWeekLoanDate() {
        Calendar c2 = Calendar.getInstance();
        c2.setTime(getFecha());
        int weekLoanDate = c2.get(Calendar.WEEK_OF_YEAR);

        return weekLoanDate;
    }

    public int getWeekCurrentDate() {
        Calendar c2 = Calendar.getInstance();
        int weekCurrentDate = c2.get(Calendar.WEEK_OF_YEAR);
        weekCurrentDate = weekCurrentDate - 1;
        return weekCurrentDate;
    }
    
}
