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

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.BillType;
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.Warehouse;
import com.dev.swms.server.model.base.WarehouseArea;
import com.dev.swms.server.utils.DoubleUtils;
import com.dev.energy.server.model.Entity;
import com.dev.energy.server.web.security.UserHolder;

public class InventoryLog extends Entity {
	private static final long serialVersionUID = 5643534902003464831L;
	/** 所属仓库 */
	private Warehouse warehouse;
	/** 所属库区 */
	private WarehouseArea zone;
	/** 业务类型 */
	private String logType;
	/** 增减类型 */
	private int incOrDec;
	/** 相关单号 */
	private String relatedBill;
	/** 相关单据的单据类型*/
	private BillType billType;
	/** 发生库位 */
	private Location location;
	/** 具体商品 */
	private ItemKey itemKey;
	/** 库存状态 */
	private String status;
	/** 存货日期 **/
	private Date storageDate;
	/** SOI **/
	private String SOI;
	/** 发生数量(MU) */
	private double occurQuantity;
	/** 发生数量(包装) */
	private double occurPackQty;
	/** 发生包装 */
	private PackageUnit packageUnit;
	/** 托盘号 */
	private String LP;
	/** 发生时间 */
	private Date occurTime;
	/** 操作员 */
	private String operator;
	/** 备货人*/
	private String tph;
	/** 备货人*/
	private String bhr;
	/** 备货人*/
	private String cph;
	/** 备注 */
	private String description;
	/** 汇总日期 */
	private Date computeDate;
	/** 关联库存日志ID */
	private Long referenceLog;
	
	/**制造库位*/
	private Location zzkw;
	
	/** 操作备注 */
	private String operDes;
	
	public String getOperDes() {
		return operDes;
	}

	public void setOperDes(String operDes) {
		this.operDes = operDes;
	}

	public Location getZzkw() {
		return zzkw;
	}

	public void setZzkw(Location zzkw) {
		this.zzkw = zzkw;
	}
	
	public String getTph() {
		return tph;
	}

	public void setTph(String tph) {
		this.tph = tph;
	}

	public String getBhr() {
		return bhr;
	}

	public void setBhr(String bhr) {
		this.bhr = bhr;
	}

	public String getCph() {
		return cph;
	}

	public void setCph(String cph) {
		this.cph = cph;
	}

	protected InventoryLog() {
		super();
	}
	
	public WarehouseArea getZone() {
		return zone;
	}

	public void setZone(WarehouseArea zone) {
		this.zone = zone;
	}

	public InventoryLog(String logType, int incOrDec, String relatedBill,BillType billType, Location location, 
			String LP, ItemKey itemKey, Date storageDate, String soi, double occurQuantity, 
			PackageUnit packageUnit, String status,	Long referenceLog, String description,WarehouseArea zone) {
		super();
		this.warehouse =zone.getWarehouse();// location.getWarehouse();
		this.zone = zone;
		this.logType = logType;
		this.incOrDec = incOrDec;
		this.relatedBill = relatedBill;
		this.billType = billType;
		this.location = location;
		this.itemKey = itemKey;
		this.storageDate = storageDate;
		this.SOI = soi;
		this.occurQuantity = occurQuantity;
		this.packageUnit = packageUnit;
		if (packageUnit != null) {
			if (packageUnit.getPrecision() == 0) {
				this.setOccurPackQty(Math.ceil(occurQuantity/packageUnit.getConvertFigure()));
			} else {
				this.setOccurPackQty(DoubleUtils.formatByPrecision(occurQuantity
						/ packageUnit.getConvertFigure(), packageUnit.getPrecision()));
			}
		}
		this.LP = LP;
		this.status = status;
		this.referenceLog = referenceLog;
		this.operator = UserHolder.getUser()==null?"app":UserHolder.getUser().getName();
		this.occurTime = new Date();
		this.description = description;
	}
	
	
	public boolean equals(final Object other) {
		if (!(other instanceof InventoryLog))
			return false;
		InventoryLog castOther = (InventoryLog) other;
		return new EqualsBuilder().append(warehouse, castOther.getWarehouse())
				.append(logType, castOther.getLogType())
				.append(incOrDec, castOther.getIncOrDec())
				.append(relatedBill, castOther.getRelatedBill())
				.append(billType, castOther.getBillType())
				.append(location, castOther.getLocation())
				.append(itemKey, castOther.getItemKey())
				.append(occurQuantity, castOther.getOccurQuantity())
				.append(packageUnit, castOther.getPackageUnit())
				.append(LP, castOther.getLP())
				.append(status, castOther.getStatus())
				.append(occurTime, castOther.getOccurTime())
				.append(operator, castOther.getOperator())
				.isEquals();
	}
	
	public int hashCode() {
		return new HashCodeBuilder().append(warehouse).append("|")
				.append(logType).append("|")
				.append(incOrDec).append("|")
				.append(relatedBill).append("|")
				.append(billType == null ? 0 : billType.getId()).append("|")
				.append(location == null ? 0 : location.getId()).append("|")
				.append(itemKey == null ? 0 : itemKey.getId()).append("|")
				.append(occurQuantity).append("|")
				.append(packageUnit == null ? 0 : packageUnit.getId()).append("|")
				.append(LP).append("|")
				.append(status).append("|")
				.append(occurTime).append("|")
				.append(operator).append("|")
				.toHashCode();
	}
	
	public double getOccurPackQty() {
		return occurPackQty;
	}
	public void setOccurPackQty(double occurPackQty) {
		this.occurPackQty = occurPackQty;
	}
	public Long getReferenceLog() {
		return referenceLog;
	}
	public void setReferenceLog(Long referenceLog) {
		this.referenceLog = referenceLog;
	}
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
	public Date getStorageDate() {
		return storageDate;
	}
	public void setStorageDate(Date storageDate) {
		this.storageDate = storageDate;
	}
	public String getSOI() {
		return SOI;
	}
	public void setSOI(String soi) {
		SOI = soi;
	}
	public String getLP() {
		return LP;
	}
	public void setLP(String lp) {
		LP = lp;
	}
	public int getIncOrDec() {
		return incOrDec;
	}
	public void setIncOrDec(int incOrDec) {
		this.incOrDec = incOrDec;
	}
	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 String getLogType() {
		return logType;
	}
	public void setLogType(String logType) {
		this.logType = logType;
	}
	public double getOccurQuantity() {
		return occurQuantity;
	}
	public void setOccurQuantity(double occurQuantity) {
		this.occurQuantity = occurQuantity;
	}
	public Date getOccurTime() {
		return occurTime;
	}
	public void setOccurTime(Date occurTime) {
		this.occurTime = occurTime;
	}
	public String getOperator() {
		return operator;
	}
	public void setOperator(String operator) {
		this.operator = operator;
	}
	public String getRelatedBill() {
		return relatedBill;
	}
	public void setRelatedBill(String relatedBill) {
		this.relatedBill = relatedBill;
	}
	public Warehouse getWarehouse() {
		return warehouse;
	}
	public void setWarehouse(Warehouse warehouse) {
		this.warehouse = warehouse;
	}	
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}	
	public Date getComputeDate() {
		return computeDate;
	}
	public void setComputeDate(Date computeDate) {
		this.computeDate = computeDate;
	}
	public PackageUnit getPackageUnit() {
		return packageUnit;
	}
	public void setPackageUnit(PackageUnit packageUnit) {
		this.packageUnit = packageUnit;
	}
	public BillType getBillType() {
		return billType;
	}
	public void setBillType(BillType billType) {
		this.billType = billType;
	}
}
