ExportInvoice.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tradecloud.domain.export;

import com.tradecloud.domain.costing.clean.Costed;
import com.tradecloud.domain.costing.clean.CostingVisitor;
import com.tradecloud.domain.document.DocumentState;
import com.tradecloud.domain.document.DocumentType;
import com.tradecloud.domain.document.invoice.ActualConsignment;
import com.tradecloud.domain.document.invoice.CostsInvoice;
import com.tradecloud.domain.item.VolumeUnitOfMeasure;
import com.tradecloud.domain.item.WeightUnitOfMeasure;
import com.tradecloud.domain.model.organisationalunit.OrganisationalUnit;
import com.tradecloud.domain.place.Depot;
import com.tradecloud.domain.settlement.PlannedSettlement;
import com.tradecloud.domain.supplier.Creditor;
import org.apache.commons.lang3.NotImplementedException;
import org.hibernate.annotations.ForeignKey;
import org.springframework.stereotype.Component;

import javax.persistence.*;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Set;

@Entity
@Component(value = "ExportInvoice")
@Table(name = "exportinvoice")
@Access(AccessType.FIELD)
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "ExportInvoice")
public class ExportInvoice extends CostsInvoice {

    private static final long serialVersionUID = 1L;
    @OneToOne(fetch = FetchType.LAZY)
    @ForeignKey(name = "export_invoice_export_fk")
    private Export export;

    // this need to be eager, because otherwise the exports shipment screen
    // cannot share a shipToParty
    // with the exportInvoice exportConsignee
    @ManyToOne(fetch = FetchType.EAGER)
    @ForeignKey(name = "export_exportconsignee_fk")
    @XmlElement(name = "ExportConsignee")
    private ExportParty exportConsignee;

    @ManyToOne(fetch = FetchType.LAZY)
    @ForeignKey(name = "fk_exportinvoice_organisationalunit")
    @XmlElement(name = "OrganisationalUnitByID")
    private OrganisationalUnit organisationalUnitById;

    /*
     * This field, on the database table, may seem redundant seeing that it is identical to exportinvoicedepot. The difference is that
     * exportinvoicedepot is used by the integration gateway and thus may be invalid if the incorrect depot code is used by the user. Therefore the
     * exportinvoicedepot serves as a placeholder for the depot code incoming from the integration gateway but the 'depot' field servers as a
     * reference (foreign key) to the actual depot (Depot) used.
     */
    @ManyToOne(fetch = FetchType.EAGER)
    @ForeignKey(name = "fk_exportinvoice_depot")
    @XmlElement(name = "Depot")
    private Depot depot;

    private Boolean ignored;
    private String exportInvoiceDepot;
    private String exportConsigneeReference;
    private String organisationalUnit;
    private boolean consolidated;
    private String originalReference;
    private Date estimatedArrivalDate;

    @Column(precision = 27, scale = 10)
    protected BigDecimal volume;

    @Column(precision = 27, scale = 10)
    protected BigDecimal weight;

    @ManyToOne
    @JoinColumn(name = "weightuom_code")
    protected WeightUnitOfMeasure weightUOM;

    @ManyToOne
    @JoinColumn(name = "volumeuom_code")
    protected VolumeUnitOfMeasure volumeUOM;

    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
    private ExportCosting exportCosting = new ExportCosting();

    private boolean cartonBased;

    public ExportInvoice() {
    }

    public ExportInvoice(Export export, Boolean ignored, String exportinvoicedepot) {
        this.export = export;
        this.ignored = ignored;
        this.exportInvoiceDepot = exportinvoicedepot;
        this.exportCosting = export.getExportCosting();
    }

    public Boolean getIgnored() {
        return ignored;
    }

    public void setIgnored(Boolean ignored) {
        this.ignored = ignored;
    }

    public String getExportInvoiceDepot() {
        return exportInvoiceDepot;
    }

    public void setExportInvoiceDepot(String exportInvoiceDepot) {
        this.exportInvoiceDepot = exportInvoiceDepot;
    }

    public Export getExport() {
        return export;
    }

    public void setExport(Export export) {
        this.export = export;
    }

    public ExportParty getExportConsignee() {
        return exportConsignee;
    }

    public void setExportConsignee(ExportParty exportConsignee) {
        this.exportConsignee = exportConsignee;
    }

    public OrganisationalUnit getOrganisationalUnitById() {
        return organisationalUnitById;
    }

    public void setOrganisationalUnitById(OrganisationalUnit organisationalUnitById) {
        this.organisationalUnitById = organisationalUnitById;
    }

    @Override
    public BigDecimal getWeightedAverageForwardRateOfExchange() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public BigDecimal getWeightedAverageSpotRateOfExchange() {
        throw new NotImplementedException("getWeightedAverageSpotRateOfExchange not implemented for Export invoice");
    }

    @Override
    public DocumentType getDocumentType() {
        return DocumentType.CARTON;
    }

    @Override
    public Creditor getCreditor() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Object getTraversalKey() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void accept(CostingVisitor costingVisitor) {
        for (ActualConsignment actualConsignment : getActualConsignments()) {
            actualConsignment.accept(costingVisitor);
        }
        costingVisitor.visit(this);
    }

    @Override
    public void acceptVisitParentFirst(CostingVisitor costingVisitor) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Set<PlannedSettlement> getPlannedSettlements() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void setPlannedSettlements(Set<PlannedSettlement> plannedSettlements) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<Costed> getCostedChildren() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public String getExportConsigneeReference() {
        return exportConsigneeReference;
    }

    public void setExportConsigneeReference(String exportConsigneeReference) {
        this.exportConsigneeReference = exportConsigneeReference;
    }

    public String getOrganisationalUnitCode() {
        return organisationalUnit;
    }

    public void setOrganisationalUnitCode(String organisationalUnit) {
        this.organisationalUnit = organisationalUnit;
    }

    public Depot getDepot() {
        return depot;
    }

    public void setDepot(Depot depot) {
        this.depot = depot;
    }

    public String getDepotCodeName() {

        return depot != null ? depot.getCode() + " - " + depot.getName() : exportInvoiceDepot;
    }

    public String getConsigneeReferenceName() {
        return exportConsignee != null ? exportConsignee.externalRefAndName() : exportConsigneeReference;
    }

    @Override
    public String getNumber() {
        return null;
    }

    public boolean isConsolidated() {
        return consolidated;
    }

    public void setConsolidated(boolean consolidated) {
        this.consolidated = consolidated;
    }

    @Override
    public String toString() {
        return "ExportInvoice{" + "reference='" + reference + '\'' + '}';
    }

    @Transient
    @Override
    public String getType() {
        return "EI";
    }

    public String getOriginalReference() {
        return originalReference;
    }

    public void setOriginalReference(String originalReference) {
        this.originalReference = originalReference;
    }

    public BigDecimal getWeight() {
        return weight;
    }

    public BigDecimal getVolume() {
        return volume;
    }

    public void setVolume(BigDecimal volume) {
        this.volume = volume;
    }

    public void setWeight(BigDecimal weight) {
        this.weight = weight;
    }

    public WeightUnitOfMeasure getWeightUOM() {
        return weightUOM;
    }

    public void setWeightUOM(WeightUnitOfMeasure weightUOM) {
        this.weightUOM = weightUOM;
    }

    public VolumeUnitOfMeasure getVolumeUOM() {
        return volumeUOM;
    }

    public void setVolumeUOM(VolumeUnitOfMeasure volumeUOM) {
        this.volumeUOM = volumeUOM;
    }

    public Date getEstimatedArrivalDate() {
        return estimatedArrivalDate;
    }

    public void setEstimatedArrivalDate(Date estimatedArrivalDate) {
        this.estimatedArrivalDate = estimatedArrivalDate;
    }

    public ExportCosting getExportCosting() {
        return exportCosting;
    }

    public void setExportCosting(ExportCosting exportCosting) {
        this.exportCosting = exportCosting;
    }

    public boolean isCartonBased() {
        return cartonBased;
    }

    public void setCartonBased(boolean cartonBased) {
        this.cartonBased = cartonBased;
    }

    public boolean isDutyDrawBackState() {
        return getState() == DocumentState.DUTY_DRAWBACK_ISSUE || getState() == DocumentState.DUTY_DRAWBACK_SUCCESS ||
                getState() == DocumentState.NO_DUTY_DRAWBACK;
    }
}