package com.dev.swms.server.model.receiving;

import java.util.Date;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

import com.dev.swms.server.model.base.ItemKey;
import com.dev.swms.server.model.base.Location;
import com.dev.swms.server.model.base.PackageUnit;
import com.dev.swms.server.model.base.WarehouseArea;
import com.dev.swms.server.model.base.Worker;
import com.dev.swms.server.model.inventory.Stock;
import com.dev.swms.server.utils.DoubleUtils;
import com.dev.energy.server.exception.BusinessException;
import com.dev.energy.server.model.Entity;

public class ReceivedRecord extends Entity {
	private static final long serialVersionUID = 7341876321963548338L;
	/** 所属ASN单 */
	private ASN asn;
	/** 所属ASN单明细 */
	private ASNDetail asnDetail;
	/** 具体商品 */
	private ItemKey itemKey;
	/** 收货库区 */
	private WarehouseArea zone;
	/** 收货库位 */
	private Location location;
	/** 收货数量 */
	private double receivedQuantity;
	/** 收货数量MU */
	private double receivedQuantityMU;
	/** 包装单位 */
	private PackageUnit packageUnit;
	/** 收货日期 */
	private Date receiveDate;
	/** 入库单 */
	private ReceivedDoc receivedDoc;
	/** 发生时间 */
	private Date occurTime;
	/** 操作员 */
	private String operator;
	/** 备注 */
	private String description;
	/** 关联上架计划号 */
	private String moveDocCode;
	/** 作业人员 */
	private Worker worker;
	/** 质检数量 */
	private double qualityQty = 0;
	/** 质损代码 */
	private String qualityCode;
	/** 是否已上架 */
	private boolean beMoved = Boolean.FALSE;
	/** 是否打印入库单 */
	private boolean bePrint = Boolean.FALSE;
	
	/** 托盘号 */
	private Stock srcLP;
	
	private String srcLPCode;
	
	private String barCode;//现品票
	
	private double weight;//收货重量
	
	private double volume;//体积
	
	public WarehouseArea getZone() {
		return zone;
	}
	public void setZone(WarehouseArea zone) {
		this.zone = zone;
	}
	public double getWeight() {
		return weight;
	}
	public void setWeight(double weight) {
		this.weight = weight;
	}
	public double getVolume() {
		return volume;
	}
	public void setVolume(double volume) {
		this.volume = volume;
	}
	public String getBarCode() {
		return barCode;
	}
	public void setBarCode(String barCode) {
		this.barCode = barCode;
	}
	public Stock getSrcLP() {
		return srcLP;
	}
	public void setSrcLP(Stock srcLP) {
		this.srcLP = srcLP;
	}
	public String getSrcLPCode() {
		return srcLPCode;
	}
	public void setSrcLPCode(String srcLPCode) {
		this.srcLPCode = srcLPCode;
	}
	public boolean isBePrint() {
		return bePrint;
	}
	public void setBePrint(boolean bePrint) {
		this.bePrint = bePrint;
	}
	public double getReceivedQuantityMU() {
		return receivedQuantityMU;
	}
	public void setReceivedQuantityMU(double receivedQuantityMU) {
		this.receivedQuantityMU = receivedQuantityMU;
	}
	public boolean isBeMoved() {
		return beMoved;
	}
	public boolean getBeMoved() {
		return beMoved;
	}
	public void setBeMoved(boolean beMoved) {
		this.beMoved = beMoved;
	}

	public String getQualityCode() {
		return qualityCode;
	}

	public void setQualityCode(String qualityCode) {
		this.qualityCode = qualityCode;
	}

	public Worker getWorker() {
		return worker;
	}

	public void setWorker(Worker worker) {
		this.worker = worker;
	}

	protected ReceivedRecord(){
		
	}
	
	public String getMoveDocCode() {
		return moveDocCode;
	}
	public void setMoveDocCode(String moveDocCode) {
		this.moveDocCode = moveDocCode;
	}
	public Date getReceiveDate() {
		return receiveDate;
	}
	public void setReceiveDate(Date receiveDate) {
		this.receiveDate = receiveDate;
	}
	public ItemKey getItemKey() {
		return itemKey;
	}
	public void setItemKey(ItemKey itemKey) {
		this.itemKey = itemKey;
	}
	public Location getLocation() {
		return location;
	}
	public void setLocation(Location location) {
		this.location = location;
	}
	public ReceivedDoc getReceivedDoc() {
		return receivedDoc;
	}
	public void setReceivedDoc(ReceivedDoc receivedDoc) {
		this.receivedDoc = receivedDoc;
	}
	public String getOperator() {
		return operator;
	}
	public void setOperator(String operator) {
		this.operator = operator;
	}
	public PackageUnit getPackageUnit() {
		return packageUnit;
	}
	public void setPackageUnit(PackageUnit packageUnit) {
		this.packageUnit = packageUnit;
	}
	public double getReceivedQuantity() {
		return receivedQuantity;
	}
	public void setReceivedQuantity(double receivedQuantity) {
		this.receivedQuantity = receivedQuantity;
	}
	public Date getOccurTime() {
		return occurTime;
	}
	public void setOccurTime(Date occurTime) {
		this.occurTime = occurTime;
	}	
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	
	public boolean equals(final Object other) {
		if (!(other instanceof ReceivedRecord))
			return false;
		ReceivedRecord castOther = (ReceivedRecord) other;
		return new EqualsBuilder().append(asn, castOther.getAsn())
				.append(asnDetail, castOther.getAsnDetail())
				.append(itemKey, castOther.getItemKey())
				.append(location, castOther.getLocation())
				.append(receivedQuantity, castOther.getReceivedQuantity())
				.append(packageUnit, castOther.getPackageUnit())
				.append(occurTime, castOther.getOccurTime())
				.append(operator, castOther.getOperator()).isEquals();
	}
	public int hashCode() {
		return new HashCodeBuilder().append(asn).append("|")
				.append(asnDetail).append("|")
				.append(itemKey).append("|")
				.append(location).append("|")
				.append(receivedQuantity).append("|")
				.append(packageUnit).append("|")
				.append(occurTime).append("|")
				.append(operator).append("|")
				.toHashCode();
	}

	public ASN getAsn() {
		return asn;
	}

	public void setAsn(ASN asn) {
		this.asn = asn;
	}

	public ASNDetail getAsnDetail() {
		return asnDetail;
	}

	public void setAsnDetail(ASNDetail asnDetail) {
		this.asnDetail = asnDetail;
	}

	public double getQualityQty() {
		return qualityQty;
	}

	public void setQualityQty(double qualityQty) {
		this.qualityQty = qualityQty;
	}

	public void quality(Double quantity) {
		this.qualityQty += quantity;
	}
	
	
	public void receiveBack(double backQty) {
		if (this.receivedQuantityMU - backQty < this.qualityQty) {
			throw new BusinessException("receiveBack.quantity.error");
		}
		this.receivedQuantityMU -= backQty;
//		if (this.packageUnit.getPrecision() == 0) {
//			this.receivedQuantity = Math.ceil(this.receivedQuantityMU/this.packageUnit.getConvertFigure());
//		} else {
//			this.receivedQuantity = DoubleUtils.formatByPrecision(this.receivedQuantityMU
//					/ this.packageUnit.getConvertFigure(), this.packageUnit.getPrecision());
//		}
	}
}
