package ee.ut.math.tvt2008.salessystem.domain.data;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * Sale object. Contains client and sold items.
 */
public class Sale implements DisplayableItem {

    private Long id;
    private Set<SoldItem> soldItems = new HashSet<SoldItem>();
    private Client client;
    private Date sellingTime;

    public Date getSellingTime() {
        return sellingTime;
    }

    public void setSellingTime(Date sellingTime) {
        this.sellingTime = sellingTime;
    }

    public Set<SoldItem> getSoldItems() {
        return soldItems;
    }

    public void setSoldItems(Set<SoldItem> soldItems) {
        this.soldItems = soldItems;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void addSoldItem(SoldItem item) {
        item.setSale(this);
        soldItems.add(item);
    }

    public BigDecimal getSum() {
        BigDecimal sum = BigDecimal.ZERO;
        for (SoldItem item : soldItems) {
            sum = sum.add(item.getValue());
        }
        return sum;
    }

    public Client getClient() {
        return client;
    }

    public void setClient(Client client) {
        this.client = client;
    }
}
