package info.bukova.isspst.data;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import java.math.BigDecimal;
import java.util.List;

@Entity
@Table(name = "INVOICING")
public class Invoicing extends BaseData implements Cloneable
{
	
	@OneToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "REQUIREMENT_ID")
	@Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
	private Requirement requirement;
	
	@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
	@LazyCollection(LazyCollectionOption.TRUE)
	@JoinColumn(name = "INVOICING_ID")
	private List<InvoicingItem> items; 
	
	@Column(name = "TOTAL_INVOICED", precision = 15, scale = 4)
	private BigDecimal totalInvoiced;

	@Column(name = "COMPLETED")
	private boolean completed;

	public Requirement getRequirement() {
		return requirement;
	}

	public void setRequirement(Requirement requirement) {
		this.requirement = requirement;
	}

	public List<InvoicingItem> getItems() {
		return items;
	}

	public void setItems(List<InvoicingItem> items) {
		this.items = items;
	}

	public BigDecimal getTotalInvoiced() {
		return totalInvoiced;
	}

	public void setTotalInvoiced(BigDecimal totalInvoiced) {
		this.totalInvoiced = totalInvoiced;
	}

	public boolean isCompleted()
	{
		return completed;
	}

	public void setCompleted(boolean completed)
	{
		this.completed = completed;
	}

	public String getInvoiceNumbers() {
		String ret = "";
		for (InvoicingItem invNum : items) {
			if (!ret.isEmpty()) {
				ret += ", ";
			}
			ret += invNum.getInvoiceNumber();
		}

		return ret;
	}

	@Override
	public Object clone() throws CloneNotSupportedException
	{
		Invoicing cloned = (Invoicing) super.clone();
		return cloned;
	}
}
