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

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

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

import com.dev.swms.server.model.base.Address;
import com.dev.swms.server.model.base.Contact;
import com.dev.swms.server.model.base.Structure;
import com.dev.swms.server.model.base.PackageUnit;
import com.dev.swms.server.model.base.TransSite;
import com.dev.swms.server.model.base.Warehouse;
import com.dev.swms.server.model.base.Worker;
import com.dev.energy.server.model.Entity;
import com.dev.energy.server.web.security.UserHolder;

public class BOL extends Entity{
	/**
	 * @Description：
	 * @Modified_Reseaon: (1)、增加运抵日期,回单日期,出发地,目的地,实收数量
	 * @Modified_By：(1) <a href="kefei.zhang@dev.com"/>张克非</a>
	 * @Date：2009-2-12
	 */
	private static final long serialVersionUID = 1L;
	/** 所属仓库 */
	private Warehouse warehouse;
	/** 代码 */
	private String code;
	/** 承运商 */
	private Structure carrier;
	/** 拖车号 */
	private String trailer;
	/** 封条号 */
	private String sealNumber;
	/** 状态 */
	private String status;
	/** 描述 */
	private String description;
	/** 最后修改人ID * */
	private Long userID;
	/** 最后修改人姓名 * */
	private String userName;
	/** 最后修改时间 * */
	private Date updateTime;
	/** 收货方名称 */
	private String shipToName;
	/** 收货方地址 */
	private Address shipToAddress;
	/** 收货方联系方式 */
	private Contact shipToContact;
	/** 运输方式 */
	private String shippingMethod;
	/** 总体积 * */
	private double allShippedVolume;
	/** 总重量 * */
	private double allShippedWeight;
	/** 整箱数量 **/	
	private int packageNum;
	/**散件数量  系统生成总是默认为0**/
	private int bulkCargoNum = 0;
	/** BOL明细 * */
	private Set<Package> packages;
	/** 所属MasterBOL **/
	private MasterBOL masterBOL;
	/** 作业人员 */
	private Worker worker;
	/** 发货单 */
	private PickTicket pickTicket;
	//Start => (1)
	private Date shipDate;  //发运时间
	private Date arriveDate;//运抵日期
	private Date returnDate;//回单日期
	private TransSite startPosition;//出发地
	private TransSite aimPosition;//目的地
	private Date signDate; //签收时间
	private double factReceiveQuantity = 0;//实收数量
	//End => (1)
	/** 收货人 */
	private Structure customer;
	/** BOL明细 * */
	private Set<BOLDetail> BOLDetails;
	/** 订单汇总  */
	private String orders;
	
	public Date getShipDate() {
		return shipDate;
	}

	public void setShipDate(Date shipDate) {
		this.shipDate = shipDate;
	}

	public Set<BOLDetail> getBOLDetails() {
		return BOLDetails;
	}

	public void setBOLDetails(Set<BOLDetail> details) {
		BOLDetails = details;
	}

	public Structure getCustomer() {
		return customer;
	}

	public void setCustomer(Structure customer) {
		this.customer = customer;
	}

	public PickTicket getPickTicket() {
		return pickTicket;
	}

	public void setPickTicket(PickTicket pickTicket) {
		this.pickTicket = pickTicket;
	}

	public Worker getWorker() {
		return worker;
	}

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

	public Warehouse getWarehouse() {
		return warehouse;
	}

	public int getPackageNum() {
		return packageNum;
	}

	public void setPackageNum(int packageNum) {
		this.packageNum = packageNum;
	}

	public int getBulkCargoNum() {
		return bulkCargoNum;
	}

	public void setBulkCargoNum(int bulkCargoNum) {
		this.bulkCargoNum = bulkCargoNum;
	}

	public void setWarehouse(Warehouse warehouse) {
		this.warehouse = warehouse;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public Structure getCarrier() {
		return carrier;
	}

	public void setCarrier(Structure carrier) {
		this.carrier = carrier;
	}

	public String getTrailer() {
		return trailer;
	}

	public void setTrailer(String trailer) {
		this.trailer = trailer;
	}

	public String getSealNumber() {
		return sealNumber;
	}

	public void setSealNumber(String sealNumber) {
		this.sealNumber = sealNumber;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Long getUserID() {
		return userID;
	}

	public void setUserID(Long userID) {
		this.userID = userID;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getShipToName() {
		return shipToName;
	}

	public void setShipToName(String shipToName) {
		this.shipToName = shipToName;
	}

	public Address getShipToAddress() {
		return shipToAddress;
	}

	public void setShipToAddress(Address shipToAddress) {
		this.shipToAddress = shipToAddress;
	}

	public Contact getShipToContact() {
		return shipToContact;
	}

	public void setShipToContact(Contact shipToContact) {
		this.shipToContact = shipToContact;
	}

	public String getShippingMethod() {
		return shippingMethod;
	}

	public void setShippingMethod(String shippintMethod) {
		this.shippingMethod = shippintMethod;
	}

	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

	public double getAllShippedVolume() {
		return allShippedVolume;
	}

	public void setAllShippedVolume(double allShippedVolume) {
		this.allShippedVolume = allShippedVolume;
	}

	public double getAllShippedWeight() {
		return allShippedWeight;
	}

	public void setAllShippedWeight(double allShippedWeight) {
		this.allShippedWeight = allShippedWeight;
	}

	public Set<Package> getPackages() {
		return packages;
	}

	public void setPackages(Set<Package> packages) {
		this.packages = packages;
	}

	public MasterBOL getMasterBOL() {
		return masterBOL;
	}

	public void setMasterBOL(MasterBOL masterBOL) {
		this.masterBOL = masterBOL;
	}

	public boolean equals(final Object other) {
		if (!(other instanceof BOL))
			return false;
		BOL castOther = (BOL) other;
		return new EqualsBuilder().append(warehouse, castOther.getWarehouse())
				.append(code, castOther.getCode()).isEquals();
	}

	public int hashCode() {
		return new HashCodeBuilder().append(warehouse).append(code)
				.toHashCode();
	}

	public void setUserTracking() {
		this.setUserID(UserHolder.getUser().getId());
		this.setUserName(UserHolder.getUser().getName());
		this.setUpdateTime(new Date());
	}

	public Date getArriveDate() {
		return arriveDate;
	}

	public void setArriveDate(Date arriveDate) {
		this.arriveDate = arriveDate;
	}

	public double getFactReceiveQuantity() {
		return factReceiveQuantity;
	}

	public void setFactReceiveQuantity(double factReceiveQuantity) {
		this.factReceiveQuantity = factReceiveQuantity;
	}

	public Date getReturnDate() {
		return returnDate;
	}

	public void setReturnDate(Date returnDate) {
		this.returnDate = returnDate;
	}

	public TransSite getAimPosition() {
		return aimPosition;
	}

	public void setAimPosition(TransSite aimPosition) {
		this.aimPosition = aimPosition;
	}

	public TransSite getStartPosition() {
		return startPosition;
	}

	public void setStartPosition(TransSite startPosition) {
		this.startPosition = startPosition;
	}

	public void manulAutoPack(double inputQty,PackageUnit pkg) {
		this.allShippedVolume += inputQty*pkg.getVolume();
		this.allShippedWeight += inputQty*pkg.getWeight();
	}

	public void refreshPackageQty() {
		double q = 0;
		double w = 0;
		double v = 0;
		Set<Package> toBeRemoved = new HashSet<Package>(); 
		for (Package p : this.getPackages()) {
			if (p.getQuantity() == 0) {
				toBeRemoved.add(p);
			} else {
				w += p.getWeight();
				v += p.getVolume();
				q += p.getQuantity();
			}
		}
		
		this.getPackages().removeAll(toBeRemoved);

		this.setPackageNum(Double.valueOf(q).intValue());
		this.setAllShippedWeight(w);
		this.setAllShippedVolume(v);
	}

	public Date getSignDate() {
		return signDate;
	}

	public void setSignDate(Date signDate) {
		this.signDate = signDate;
	}

	public String getOrders() {
		return orders;
	}

	public void setOrders(String orders) {
		this.orders = orders;
	}
	
}
