CostLineSummary.java

package com.tradecloud.domain.costing.clean;

import com.tradecloud.common.base.HibernateUtils;
import com.tradecloud.common.base.PersistenceBase;
import com.tradecloud.domain.common.Currency;
import com.tradecloud.domain.costing.CostFormula;
import com.tradecloud.domain.costing.CostLine;
import com.tradecloud.domain.costing.VatType;
import com.tradecloud.domain.exchangerate.RateOfExchanges;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.hibernate.annotations.ForeignKey;

import javax.persistence.*;
import javax.xml.bind.annotation.*;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Optional;

@Entity
@Table(name = "costlinesummary")
@Access(AccessType.FIELD)
@XmlType(propOrder = {"id", "costingCurrencyTotal", "overrideTransactionCurrency", "transactionCurrencyTotal", "spotRate", "forwardRate",
        "costLine", "dutiable"})
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "CostLineSummary")
public class CostLineSummary extends PersistenceBase implements RateOfExchanges {

    private static final long serialVersionUID = 1L;

    /**
     * TODO - do we want this here?
     */
    @XmlElement(name = "CostLine")
    @ManyToOne
    @ForeignKey(name = "fk_costline")
    private CostLine costLine;

    /**
     * TODO - decide if we want to store the Container Freight rates also...if
     * we store the cost definition I think we should store the container rates.
     * Both or neither...
     */

    @XmlAttribute
    private BigDecimal spotRate;

    @Transient
    @XmlAttribute
    private BigDecimal initialSpotRate;

    @XmlAttribute
    private BigDecimal forwardRate;

    @Transient
    @XmlAttribute
    private BigDecimal initialForwardRate;

    @XmlAttribute
    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    private Date settlementDate;

    @XmlAttribute
    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    private Date maturityDate;

    @XmlAttribute
    private BigDecimal costingCurrencyTotal = BigDecimal.ZERO.setScale(8);

    @XmlAttribute
    private BigDecimal transactionCurrencyTotal = BigDecimal.ZERO.setScale(8);

    @Transient
    @XmlAttribute
    private BigDecimal previousTransactionCurrencyTotal = BigDecimal.ZERO.setScale(8);

    @Transient
    @XmlAttribute
    private BigDecimal previousPercentageRate = BigDecimal.ZERO.setScale(8);

    @XmlAttribute
    private boolean percentageOfSupplyCost = false;

    @XmlAttribute
    private BigDecimal costPercentage;

    @XmlAttribute
    @Transient
    private boolean hasEditedCostLineCurrency;

    @XmlAttribute
    private boolean hasEditedCostLineTransactionAmount;

    @XmlAttribute
    private boolean hasEditedCostLineSpotRate;

    @XmlAttribute
    private boolean hasEditedCostLineForwardRate;

    @XmlAttribute
    private boolean hasEditedPercentage;

    @ManyToOne
    @ForeignKey(name = "fk_overridetransactioncurrency")
    @XmlElement
    private Currency overrideTransactionCurrency;

    private transient CostFormula costFormula;
    @XmlAttribute
    private BigDecimal vat = BigDecimal.ZERO;

    @XmlAttribute
    BigDecimal bondedItemsCustomsTotals = BigDecimal.ZERO;

    private boolean dutiable;
    @XmlAttribute
    @Enumerated(value = EnumType.STRING)
    private VatType vatType;
    @XmlAttribute
    private boolean overriddenVatType;

    private transient boolean displayVatType;
    private transient boolean displayVat;

    @Override
    public String toString() {
        return "CostLineSummary [costLine=" + costLine + ", spotRate=" + spotRate + ", forwardRate=" + forwardRate + ", " +
                "" + "costingCurrencyTotal=" + costingCurrencyTotal + ", transactionCurrencyTotal=" + transactionCurrencyTotal + ", " +
                "" + "overridetransactionCurrency=" + overrideTransactionCurrency + "]";
    }

    public CostLine getCostLine() {
        return costLine;
    }

    public void setCostLine(CostLine costLine) {
        this.costLine = costLine;
    }

    @Override
    public BigDecimal getSpotRate() {
        return spotRate;
    }

    @Override
    public void setSpotRate(BigDecimal spotRate) {
        this.spotRate = spotRate;
    }

    @Override
    public BigDecimal getForwardRate() {
        return forwardRate;
    }

    @Override
    public void setForwardRate(BigDecimal forwardRate) {
        this.forwardRate = forwardRate;
    }

    public BigDecimal getInitialForwardRate() {
        return initialForwardRate;
    }

    public void setInitialForwardRate(BigDecimal initialForwardRate) {
        this.initialForwardRate = initialForwardRate;
    }

    public BigDecimal getInitialSpotRate() {
        return initialSpotRate;
    }

    public void setInitialSpotRate(BigDecimal initialSpotRate) {
        this.initialSpotRate = initialSpotRate;
    }

    public BigDecimal getCostingCurrencyTotal() {
        return costingCurrencyTotal;
    }

    public void setCostingCurrencyTotal(BigDecimal costingCurrencyTotal) {
        this.costingCurrencyTotal = costingCurrencyTotal;
    }

    public BigDecimal getTransactionCurrencyTotal() {
        return transactionCurrencyTotal;
    }

    public void setTransactionCurrencyTotal(BigDecimal transactionCurrencyTotal) {
        previousTransactionCurrencyTotal = this.transactionCurrencyTotal;
        if (transactionCurrencyTotal != null) this.transactionCurrencyTotal = transactionCurrencyTotal;
    }

    public boolean isPercentageOfSupplyCost() {
        return percentageOfSupplyCost;
    }

    public void setPercentageOfSupplyCost(boolean percentageOfSupplyCost) {
        this.percentageOfSupplyCost = percentageOfSupplyCost;
    }

    public Date getSettlementDate() {
        return settlementDate;
    }

    public void setSettlementDate(Date settlementDate) {
        this.settlementDate = settlementDate;
    }

    public Date getMaturityDate() {
        return maturityDate;
    }

    public void setMaturityDate(Date maturityDate) {
        this.maturityDate = maturityDate;
    }

    /**
     * Get the percentage this cost use to calculate with.
     *
     * @return
     */
    public BigDecimal getCostPercentage() {
        return costPercentage;
    }

    public void setCostPercentage(BigDecimal costPercentage) {
        previousPercentageRate = this.costPercentage;
        this.costPercentage = costPercentage;
    }

    /**
     * Returns whether this costed line item has an edited transaction currency.
     *
     * @return
     */
    public boolean getHasEditedCostLineCurrency() {
        return hasEditedCostLineCurrency;
    }

    public void setHasEditedCostLineCurrency(boolean hasEditedCostLineCurrency) {
        this.hasEditedCostLineCurrency = hasEditedCostLineCurrency;
    }

    /**
     * Returns whether this costed line item has an edited transaction amount.
     *
     * @return
     */
    public boolean getHasEditedCostLineTransactionAmount() {
        return hasEditedCostLineTransactionAmount;
    }

    public void setHasEditedCostLineTransactionAmount(boolean hasEditedCostLineTransactionAmount) {
        this.hasEditedCostLineTransactionAmount = hasEditedCostLineTransactionAmount;
    }

    /**
     * Returns whether this costed line item has an edited exchange rate.
     *
     * @return
     */
    public boolean getHasEditedCostLineSpotRate() {
        return hasEditedCostLineSpotRate;
    }

    public void setHasEditedCostLineSpotRate(boolean hasEditedCostLineSpotRate) {
        this.hasEditedCostLineSpotRate = hasEditedCostLineSpotRate;
    }

    public Currency getOverrideTransactionCurrency() {
        return overrideTransactionCurrency;
    }

    public void setOverrideCurrency(Currency overrideTransactionCurrency) {
        this.overrideTransactionCurrency = overrideTransactionCurrency;
    }

    /**
     * Get the transaction currency for this cost line summary.
     *
     * @return The overridden transaction currency if set, else the associated
     * costline transaction currency
     */
    public Currency getTransactionCurrency() {
        if (overrideTransactionCurrency != null) {
            return overrideTransactionCurrency;
        } else if (costLine != null) {
            return costLine.getTransactionCurrency();
        }
        return null;
    }

    /**
     * Override the transaction currency.
     *
     * @param transactionCurrency
     */
    public void setTransactionCurrency(Currency transactionCurrency) {
        setOverrideCurrency(transactionCurrency);
    }

    public BigDecimal getPreviousTransactionCurrencyTotal() {
        return previousTransactionCurrencyTotal;
    }

    public void setPreviousTransactionCurrencyTotal(BigDecimal previousTransactionCurrencyTotal) {
        this.previousTransactionCurrencyTotal = previousTransactionCurrencyTotal;
    }

    public boolean isHasEditedPercentage() {
        return hasEditedPercentage;
    }

    public void setHasEditedPercentage(boolean hasEditedPercentage) {
        this.hasEditedPercentage = hasEditedPercentage;
    }

    public void clear() {
        this.previousTransactionCurrencyTotal = BigDecimal.ZERO;
        this.costingCurrencyTotal = BigDecimal.ZERO;
        this.transactionCurrencyTotal = BigDecimal.ZERO;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((costLine != null) ? costLine.hashCode() : 0);
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (this == obj) {
            return true;
        }
        if (!HibernateUtils.proxyClassEquals(this, obj)) {
            return false;
        }
        CostLineSummary obj1 = (CostLineSummary) obj;
        return new EqualsBuilder().append(costLine, obj1.costLine).isEquals();
    }

    public BigDecimal getPreviousPercentageRate() {
        return previousPercentageRate;
    }

    public void setPreviousPercentageRate(BigDecimal previousPercentageRate) {
        this.previousPercentageRate = previousPercentageRate;
    }

    public boolean getHasEditedCostLineForwardRate() {
        return hasEditedCostLineForwardRate;
    }

    public void setHasEditedCostLineForwardRate(boolean hasEditedCostLineForwardRate) {
        this.hasEditedCostLineForwardRate = hasEditedCostLineForwardRate;
    }

    public CostFormula getCostFormula() {
        return costFormula;
    }

    public void setCostFormula(CostFormula costFormula) {
        this.costFormula = costFormula;
    }

    public BigDecimal getVat() {
        return vat;
    }

    public void setVat(BigDecimal vat) {
        this.vat = vat;
    }

    public BigDecimal getBondedItemsCustomsTotals() {
        return bondedItemsCustomsTotals;
    }

    public void setBondedItemsCustomsTotals(BigDecimal bondedItemsCustomsTotals) {
        this.bondedItemsCustomsTotals = bondedItemsCustomsTotals;
    }

    public BigDecimal getGeneralBondedCombined() {
        return transactionCurrencyTotal.add(Optional.ofNullable(bondedItemsCustomsTotals).orElse(BigDecimal.ZERO));
    }

    public boolean isDutiable() {
        return dutiable;
    }

    public void setDutiable(boolean dutiable) {
        this.dutiable = dutiable;
    }

    public String getCode() {
        return costLine.getCostLineTemplate().getCode();
    }

    public VatType getVatType() {
        return vatType;
    }

    public void setVatType(VatType vatType) {
        if (vatType != null) {
            this.vatType = vatType;
        }
    }

    public boolean isOverriddenVatType() {
        return overriddenVatType;
    }

    public void setOverriddenVatType(boolean overriddenVatType) {
        this.overriddenVatType = overriddenVatType;
    }

    public boolean isDisplayVatType() {
        return displayVatType;
    }

    public void setDisplayVatType(boolean displayVatType) {
        this.displayVatType = displayVatType;
    }

    public boolean isDisplayVat() {
        return displayVat;
    }

    public void setDisplayVat(boolean displayVat) {
        this.displayVat = displayVat;
    }
}