/**
 * my-axelor-app
 *
 * Copyright (C) 2018 My Company (<http://my-company.com>).
 *
 * This program is free software: you can redistribute it and/or  modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.axelor.orders.db;

import java.math.BigDecimal;
import java.util.Objects;

import javax.persistence.Cacheable;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

import org.hibernate.annotations.Index;

import com.axelor.auth.db.AuditableModel;
import com.axelor.db.annotations.Widget;
import com.google.common.base.MoreObjects;

@Entity
@Cacheable(false)
@Table(name = "ORDERS_SALES_ORDER_DETAIL")
public class SalesOrderDetail extends AuditableModel {

	@Id
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ORDERS_SALES_ORDER_DETAIL_SEQ")
	@SequenceGenerator(name = "ORDERS_SALES_ORDER_DETAIL_SEQ", sequenceName = "ORDERS_SALES_ORDER_DETAIL_SEQ", allocationSize = 1)
	private Long id;

	@Widget(title = "订单")
	@NotNull
	@Index(name = "ORDERS_SALES_ORDER_DETAIL_SALES_ORDER_IDX")
	@ManyToOne(fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST, CascadeType.MERGE })
	private SalesOrder salesOrder;

	private Integer sequence = 0;

	@Widget(title = "产品")
	@NotNull
	private String product;

	@Widget(title = "单价")
	@NotNull
	private BigDecimal unitPrice = BigDecimal.ZERO;

	@Widget(title = "数量")
	@NotNull
	private BigDecimal quantity = BigDecimal.ZERO;

	public SalesOrderDetail() {
	}

	@Override
	public Long getId() {
		return id;
	}

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

	public SalesOrder getSalesOrder() {
		return salesOrder;
	}

	public void setSalesOrder(SalesOrder salesOrder) {
		this.salesOrder = salesOrder;
	}

	public Integer getSequence() {
		return sequence == null ? 0 : sequence;
	}

	public void setSequence(Integer sequence) {
		this.sequence = sequence;
	}

	public String getProduct() {
		return product;
	}

	public void setProduct(String product) {
		this.product = product;
	}

	public BigDecimal getUnitPrice() {
		return unitPrice == null ? BigDecimal.ZERO : unitPrice;
	}

	public void setUnitPrice(BigDecimal unitPrice) {
		this.unitPrice = unitPrice;
	}

	public BigDecimal getQuantity() {
		return quantity == null ? BigDecimal.ZERO : quantity;
	}

	public void setQuantity(BigDecimal quantity) {
		this.quantity = quantity;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null) return false;
		if (this == obj) return true;
		if (!(obj instanceof SalesOrderDetail)) return false;

		final SalesOrderDetail other = (SalesOrderDetail) obj;
		if (this.getId() != null || other.getId() != null) {
			return Objects.equals(this.getId(), other.getId());
		}

		return false;
	}

	@Override
	public int hashCode() {
		return 0;
	}

	@Override
	public String toString() {
		final MoreObjects.ToStringHelper tsh = MoreObjects.toStringHelper(this);

		tsh.add("id", this.getId());
		tsh.add("sequence", this.getSequence());
		tsh.add("product", this.getProduct());
		tsh.add("unitPrice", this.getUnitPrice());
		tsh.add("quantity", this.getQuantity());

		return tsh.omitNullValues().toString();
	}
}
