GoodsReceivedReceipt.java

package com.tradecloud.domain.model.goodsreceivedreceipt;

import com.tradecloud.common.base.PersistenceBase;
import com.tradecloud.domain.model.ordermanagement.Order;
import org.hibernate.annotations.JoinFormula;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.Objects;
import java.util.Set;

@Entity
@Table(name = "GoodsReceivedReceipt", uniqueConstraints = {@UniqueConstraint(columnNames = {"reference"})})
@Access(AccessType.FIELD)
public class GoodsReceivedReceipt extends PersistenceBase {
    @NotNull(message = "GRR Reference should not be null")
    private String reference;
    @ManyToOne(fetch = FetchType.LAZY)
    @NotNull(message = "order should not be null")
    private Order order;
    private Date receiptDate;
    private boolean positiveType;
    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private Set<GoodsReceivedReceiptItem> items = new LinkedHashSet<GoodsReceivedReceiptItem>();
    @Enumerated(EnumType.STRING)
    private ReceiptLevel receiptLevel;
    @JoinFormula("(SELECT SUM(grri.localUnitCost * grri.quantity) " +
            "FROM goodsReceivedReceiptItem grri" +
            "WHERE grri.id IN (SELECT items_id from goodsreceivedreceipt_goodsreceivedreceiptitem WHERE goodsreceivedreceipt_id = id))")
    private BigDecimal totalGRNCost;
    private boolean syncWithDutyDrawBack;

    public GoodsReceivedReceipt() {
    }

    public GoodsReceivedReceipt(String reference, Order order) {
        this.reference = reference;
        this.order = order;
    }

    public String getReference() {
        return reference;
    }

    public void setReference(String reference) {
        this.reference = reference;
    }

    public Order getOrder() {
        return order;
    }

    public void setOrder(Order order) {
        this.order = order;
    }

    public Date getReceiptDate() {
        return receiptDate;
    }

    public void setReceiptDate(Date receiptDate) {
        this.receiptDate = receiptDate;
    }

    public boolean isPositiveType() {
        return positiveType;
    }

    public void setPositiveType(boolean positiveType) {
        this.positiveType = positiveType;
    }

    public Set<GoodsReceivedReceiptItem> getItems() {
        return items;
    }

    public void setItems(Set<GoodsReceivedReceiptItem> items) {
        this.items = items;
    }

    public void add(GoodsReceivedReceiptItem goodsReceivedReceiptItem) {
        this.items.add(goodsReceivedReceiptItem);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        if (!super.equals(o)) return false;
        GoodsReceivedReceipt that = (GoodsReceivedReceipt) o;
        return Objects.equals(reference, that.reference);
    }

    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), reference);
    }

    public ReceiptLevel getReceiptLevel() {
        return receiptLevel;
    }

    public void setReceiptLevel(ReceiptLevel receiptLevel) {
        this.receiptLevel = receiptLevel;
    }

    public BigDecimal getTotalGRNCost() {
        this.totalGRNCost = BigDecimal.ZERO;
        this.items.forEach(x -> this.totalGRNCost = this.totalGRNCost.add(x.getLocalUnitCost().multiply(x.getQuantity())));
        return totalGRNCost;
    }

    public void setTotalGRNCost(BigDecimal totalGRNCost) {
        this.totalGRNCost = totalGRNCost;
    }

    public boolean isSyncWithDutyDrawBack() {
        return syncWithDutyDrawBack;
    }

    public void setSyncWithDutyDrawBack(boolean syncWithDutyDrawBack) {
        this.syncWithDutyDrawBack = syncWithDutyDrawBack;
    }
}