package com.uas.platform.b2b.erp.model;

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

import org.springframework.util.CollectionUtils;

import com.uas.platform.b2b.model.PurchaseAccept;
import com.uas.platform.b2b.model.PurchaseAcceptItem;
import com.uas.platform.b2b.model.PurchaseBadIn;
import com.uas.platform.b2b.model.PurchaseBadInItem;
import com.uas.platform.b2b.model.PurchaseBadOut;
import com.uas.platform.b2b.model.PurchaseBadOutItem;
import com.uas.platform.b2b.model.PurchaseReturn;
import com.uas.platform.b2b.model.PurchaseReturnItem;
import com.uas.platform.b2b.support.SystemSession;
import com.uas.platform.core.model.Status;

/**
 * 采购方角度ERP采购验收单
 * @author suntg
 * @date 2015年4月17日10:32:24
 *
 */
public class PurchaseProdInOut {

	private String pi_inoutno;//采购检验单号
	private Long pi_vendoruu;//客户uu号
	private String pi_currency;//币别
	private Float pi_rate;//汇率
	private String pi_payment;//付款方式
	private String pi_transport;//运输方式
	private String pi_remark;//备注
	private String pi_inoutman;//过账人
	private Date pi_date1;//过账日期
	private String pi_sendcode;//送货单号
	private List<PurchaseProdInOutDetail> details;//明细
	
	public String getPi_inoutno() {
		return pi_inoutno;
	}
	public void setPi_inoutno(String pi_inoutno) {
		this.pi_inoutno = pi_inoutno;
	}
	public Long getPi_vendoruu() {
		return pi_vendoruu;
	}
	public void setPi_vendoruu(Long pi_vendoruu) {
		this.pi_vendoruu = pi_vendoruu;
	}
	public String getPi_currency() {
		return pi_currency;
	}
	public void setPi_currency(String pi_currency) {
		this.pi_currency = pi_currency;
	}
	public Float getPi_rate() {
		return pi_rate;
	}
	public void setPi_rate(Float pi_rate) {
		this.pi_rate = pi_rate;
	}
	public String getPi_payment() {
		return pi_payment;
	}
	public void setPi_payment(String pi_payment) {
		this.pi_payment = pi_payment;
	}
	public String getPi_transport() {
		return pi_transport;
	}
	public void setPi_transport(String pi_transport) {
		this.pi_transport = pi_transport;
	}
	public String getPi_remark() {
		return pi_remark;
	}
	public void setPi_remark(String pi_remark) {
		this.pi_remark = pi_remark;
	}
	public String getPi_inoutman() {
		return pi_inoutman;
	}
	public void setPi_inoutman(String pi_inoutman) {
		this.pi_inoutman = pi_inoutman;
	}
	public Date getPi_date1() {
		return pi_date1;
	}
	public void setPi_date1(Date pi_date1) {
		this.pi_date1 = pi_date1;
	}
	public String getPi_sendcode() {
		return pi_sendcode;
	}
	public void setPi_sendcode(String pi_sendcode) {
		this.pi_sendcode = pi_sendcode;
	}
	public List<PurchaseProdInOutDetail> getDetails() {
		return details;
	}
	public void setDetails(List<PurchaseProdInOutDetail> details) {
		this.details = details;
	}
	
	public PurchaseAccept convent(){
		PurchaseAccept accept = new PurchaseAccept();
		accept.setCode(this.pi_inoutno);
		accept.setCurrency(this.pi_currency);
		accept.setEnUU(SystemSession.getUser().getEnterprise().getUu());
		accept.setSendCode(this.pi_sendcode);
		accept.setPayments(this.pi_payment);
		accept.setRate(this.pi_rate);
		accept.setRecorder(this.pi_inoutman);
		accept.setRemark(this.pi_remark);
		accept.setDate(this.pi_date1);
		accept.setVendUU(this.pi_vendoruu);
		accept.setSendStatus((short) Status.NOT_UPLOAD.value());
		if(! CollectionUtils.isEmpty(this.details)) {
			Set<PurchaseAcceptItem> acceptItems = new HashSet<PurchaseAcceptItem>();
			for(PurchaseProdInOutDetail detail : this.details) {
				acceptItems.add(detail.convent());
			}
			accept.setAcceptItems(acceptItems);
		}
		return accept;
	}
	
	public PurchaseReturn conventToReturn(){
		PurchaseReturn purchaseReturn = new PurchaseReturn();
		purchaseReturn.setCode(this.pi_inoutno);
		purchaseReturn.setCurrency(this.pi_currency);
		purchaseReturn.setEnUU(SystemSession.getUser().getEnterprise().getUu());
		purchaseReturn.setSendCode(this.pi_sendcode);
		purchaseReturn.setPayments(this.pi_payment);
		purchaseReturn.setRate(this.pi_rate);
		purchaseReturn.setRecorder(this.pi_inoutman);
		purchaseReturn.setRemark(this.pi_remark);
		purchaseReturn.setDate(this.pi_date1);
		purchaseReturn.setVendUU(this.pi_vendoruu);
		purchaseReturn.setSendStatus((short) Status.NOT_UPLOAD.value());
		if(! CollectionUtils.isEmpty(this.details)) {
			Set<PurchaseReturnItem> returnItems = new HashSet<PurchaseReturnItem>();
			for(PurchaseProdInOutDetail detail : this.details) {
				returnItems.add(detail.conventToReturn());
			}
			purchaseReturn.setReturnItems(returnItems);
		}
		return purchaseReturn;
	}
	
	public PurchaseBadIn conventToBadIn(){
		PurchaseBadIn purchaseBadIn = new PurchaseBadIn();
		purchaseBadIn.setCode(this.pi_inoutno);
		purchaseBadIn.setCurrency(this.pi_currency);
		purchaseBadIn.setEnUU(SystemSession.getUser().getEnterprise().getUu());
		purchaseBadIn.setSendCode(this.pi_sendcode);
		purchaseBadIn.setPayments(this.pi_payment);
		purchaseBadIn.setRate(this.pi_rate);
		purchaseBadIn.setRecorder(this.pi_inoutman);
		purchaseBadIn.setRemark(this.pi_remark);
		purchaseBadIn.setDate(this.pi_date1);
		purchaseBadIn.setVendUU(this.pi_vendoruu);
		purchaseBadIn.setSendStatus((short) Status.NOT_UPLOAD.value());
		if(! CollectionUtils.isEmpty(this.details)) {
			Set<PurchaseBadInItem> badInItems = new HashSet<PurchaseBadInItem>();
			for(PurchaseProdInOutDetail detail : this.details) {
				badInItems.add(detail.conventToBadIn());
			}
			purchaseBadIn.setBadInItems(badInItems);
		}
		return purchaseBadIn;
	}
	
	public PurchaseBadOut conventToBadOut(){
		PurchaseBadOut purchaseBadOut = new PurchaseBadOut();
		purchaseBadOut.setCode(this.pi_inoutno);
		purchaseBadOut.setCurrency(this.pi_currency);
		purchaseBadOut.setEnUU(SystemSession.getUser().getEnterprise().getUu());
		purchaseBadOut.setSendCode(this.pi_sendcode);
		purchaseBadOut.setPayments(this.pi_payment);
		purchaseBadOut.setRate(this.pi_rate);
		purchaseBadOut.setRecorder(this.pi_inoutman);
		purchaseBadOut.setRemark(this.pi_remark);
		purchaseBadOut.setDate(this.pi_date1);
		purchaseBadOut.setVendUU(this.pi_vendoruu);
		purchaseBadOut.setSendStatus((short) Status.NOT_UPLOAD.value());
		if(! CollectionUtils.isEmpty(this.details)) {
			Set<PurchaseBadOutItem> badOutItems = new HashSet<PurchaseBadOutItem>();
			for(PurchaseProdInOutDetail detail : this.details) {
				badOutItems.add(detail.conventToBadOut());
			}
			purchaseBadOut.setBadOutItems(badOutItems);
		}
		return purchaseBadOut;
	}
}
