package com.uas.platform.b2b.model;

import com.alibaba.fastjson.annotation.JSONField;
import org.codehaus.jackson.annotate.JsonIgnore;

import javax.persistence.*;
import java.util.*;

/**
 * 买家的应付发票明细
 * 
 * @author suntg
 * 
 */
@Table(name = "purc$apbillitem")
@Entity
public class PurchaseApBillItem {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "papi_id")
	private Long id;

	/**
	 * 明细序号
	 */
	@Column(name = "papi_number")
	private Short number;

	/**
	 * 采购单号
	 */
	@Column(name = "papi_ordercode")
	private String orderCode;

	/**
	 * 采购单明细序号
	 */
	@Column(name = "papi_orderdetno")
	private Short orderDetno;

	/**
	 * 出入库单号
	 */
	@Column(name = "papi_pdinoutno")
	private String pdinoutNo;

	/**
	 * 物料
	 */
	@OneToOne(cascade = { CascadeType.REFRESH })
	@JoinColumn(name = "papi_product", insertable = false, updatable = false)
	private Product product;

	/**
	 * 物料id
	 */
	@Column(name = "papi_product")
	private Long productId;

	/**
	 * 数量
	 */
	@Column(name = "papi_thisvoqty")
	private Double thisvoQty;

	/**
	 * 采购单价
	 */
	@Column(name = "papi_price")
	private Double price;

	/**
	 * 税率s
	 */
	@Column(name = "papi_taxrate")
	private Double taxrate;

	/**
	 * 单据金额
	 */
	@Column(name = "papi_amount")
	private Double amount;

	/**
	 * 累计开票金额
	 */
	@Column(name = "papi_totalbillprice")
	private Double totalBillPrice;

	/**
	 * 本次开票数量
	 */
	@Column(name = "papi_qty")
	private Double qty;

	/**
	 * 本次开票单价
	 */
	@Column(name = "papi_thisvoprice")
	private Double thisvoPrice;

	/**
	 * 本次开票金额
	 */
	@Column(name = "papi_apamount")
	private Double apAmount;

	/**
	 * 本次 开票不含税金额
	 */
	@Column(name = "papi_noapamount")
	private Double noApAmount;

	/**
	 * 本次开票税额
	 */
	@Column(name = "papi_taxamount")
	private Double taxAmount;

	/**
	 * 备注
	 */
	@Column(name = "papi_remark")
	private String remark;

	/**
	 * 已开票数
	 */
	@Column(name = "papi_invoqty")
	private Double invoQty;

	/**
	 * 已转开票记录数
	 */
	@Column(name = "papi_yqty")
	private Double yQty;

	/**
	 * 关联应付发票
	 */
	@ManyToOne(cascade = CascadeType.ALL, optional = true)
	@JoinColumn(name = "papi_papid", nullable = false)
	private PurchaseApBill aPBill;

	@Column(name = "papi_erpdate")
	private Date erpDate;

	public Long getId() {
		return id;
	}

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

	public Short getNumber() {
		return number;
	}

	public void setNumber(Short number) {
		this.number = number;
	}

	public String getOrderCode() {
		return orderCode;
	}

	public void setOrderCode(String orderCode) {
		this.orderCode = orderCode;
	}

	public Short getOrderDetno() {
		return orderDetno;
	}

	public void setOrderDetno(Short orderDetno) {
		this.orderDetno = orderDetno;
	}

	public String getPdinoutNo() {
		return pdinoutNo;
	}

	public void setPdinoutNo(String pdinoutNo) {
		this.pdinoutNo = pdinoutNo;
	}

	public Product getProduct() {
		return product;
	}

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

	public Double getThisvoQty() {
		return thisvoQty;
	}

	public void setThisvoQty(Double thisvoQty) {
		this.thisvoQty = thisvoQty;
	}

	public Double getPrice() {
		return price;
	}

	public void setPrice(Double price) {
		this.price = price;
	}

	public Double getTaxrate() {
		return taxrate;
	}

	public void setTaxrate(Double taxrate) {
		this.taxrate = taxrate;
	}

	public Double getAmount() {
		return amount;
	}

	public void setAmount(Double amount) {
		this.amount = amount;
	}

	public Double getTotalBillPrice() {
		return totalBillPrice;
	}

	public void setTotalBillPrice(Double totalBillPrice) {
		this.totalBillPrice = totalBillPrice;
	}

	public Double getQty() {
		return qty;
	}

	public void setQty(Double qty) {
		this.qty = qty;
	}

	public Double getThisvoPrice() {
		return thisvoPrice;
	}

	public void setThisvoPrice(Double thisvoPrice) {
		this.thisvoPrice = thisvoPrice;
	}

	public Double getApAmount() {
		return apAmount;
	}

	public void setApAmount(Double apAmount) {
		this.apAmount = apAmount;
	}

	public Double getNoApAmount() {
		return noApAmount;
	}

	public void setNoApAmount(Double noApAmount) {
		this.noApAmount = noApAmount;
	}

	public Double getTaxAmount() {
		return taxAmount;
	}

	public void setTaxAmount(Double taxAmount) {
		this.taxAmount = taxAmount;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	@JsonIgnore
	@JSONField(serialize = false)
	public PurchaseApBill getaPBill() {
		return aPBill;
	}

	public void setaPBill(PurchaseApBill aPBill) {
		this.aPBill = aPBill;
	}

	public Double getInvoQty() {
		return invoQty;
	}

	public void setInvoQty(Double invoQty) {
		this.invoQty = invoQty;
	}

	public Double getyQty() {
		return yQty;
	}

	public void setyQty(Double yQty) {
		this.yQty = yQty;
	}

	public Long getProductId() {
		return productId;
	}

	public void setProductId(Long productId) {
		this.productId = productId;
	}

	public Date getErpDate() {
		return erpDate;
	}

	public void setErpDate(Date erpDate) {
		this.erpDate = erpDate;
	}

	public static List<PurchaseApBill> distinct(List<PurchaseApBillItem> items) {
		List<PurchaseApBill> bills = new ArrayList<PurchaseApBill>();
		Set<Long> keys = new HashSet<Long>();
		for (PurchaseApBillItem item : items) {
			if (!keys.contains(item.getaPBill().getId())) {
				bills.add(item.getaPBill());
				keys.add(item.getaPBill().getId());
			}
		}
		return bills;
	}

}
