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

import java.util.Date;
import java.util.Objects;

import org.apache.commons.collections.CollectionUtils;

import com.uas.platform.b2b.model.PurchaseOrderChange;
import com.uas.platform.b2b.model.PurchaseOrderChangeItem;
import com.uas.platform.b2b.support.SystemSession;
import com.uas.platform.core.model.Constant;
import com.uas.platform.core.model.Status;
import com.uas.platform.core.util.NumberUtil;

/**
 * 数据传送到移动端的轻量级实体类 <br>
 * 收到的客户采购变更单
 * 
 * @author yingp
 *
 */
public class PurchaseChange {

	/**
	 * ID
	 */
	private long pc_id;

	/**
	 * 流水号
	 */
	private String pc_code;
	/**
	 * 订单号
	 */
	private String pc_purccode;
	/**
	 * 日期
	 */
	private Date pc_date;
	/**
	 * 原付款方式
	 */
	private String pc_payments;
	/**
	 * 新付款方式
	 */
	private String pc_newpayments;
	/**
	 * 原币种
	 */
	private String pc_currency;
	/**
	 * 新币种
	 */
	private String pc_newcurrency;
	/**
	 * 原汇率
	 */
	private Float pc_rate;
	/**
	 * 新汇率
	 */
	private Float pc_newrate;
	/**
	 * 备注
	 */
	private String pc_remark;
	/**
	 * 金额
	 */
	private Double pc_total;
	/**
	 * 是否已同意
	 */
	private String pc_agreed;
	/**
	 * 客户企业名称
	 */
	private String customerName;
	/**
	 * 供应商企业名称
	 */
	private String vendName;

	public String getPc_code() {
		return pc_code;
	}

	public void setPc_code(String pc_code) {
		this.pc_code = pc_code;
	}

	public String getPc_purccode() {
		return pc_purccode;
	}

	public void setPc_purccode(String pc_purccode) {
		this.pc_purccode = pc_purccode;
	}

	public Date getPc_date() {
		return pc_date;
	}

	public void setPc_date(Date pc_date) {
		this.pc_date = pc_date;
	}

	public String getPc_payments() {
		return pc_payments;
	}

	public void setPc_payments(String pc_payments) {
		this.pc_payments = pc_payments;
	}

	public String getPc_newpayments() {
		return pc_newpayments;
	}

	public void setPc_newpayments(String pc_newpayments) {
		this.pc_newpayments = pc_newpayments;
	}

	public String getPc_currency() {
		return pc_currency;
	}

	public void setPc_currency(String pc_currency) {
		this.pc_currency = pc_currency;
	}

	public String getPc_newcurrency() {
		return pc_newcurrency;
	}

	public void setPc_newcurrency(String pc_newcurrency) {
		this.pc_newcurrency = pc_newcurrency;
	}

	public Float getPc_rate() {
		return pc_rate;
	}

	public void setPc_rate(Float pc_rate) {
		this.pc_rate = pc_rate;
	}

	public Float getPc_newrate() {
		return pc_newrate;
	}

	public void setPc_newrate(Float pc_newrate) {
		this.pc_newrate = pc_newrate;
	}

	public String getPc_remark() {
		return pc_remark;
	}

	public void setPc_remark(String pc_remark) {
		this.pc_remark = pc_remark;
	}

	public Double getPc_total() {
		return pc_total;
	}

	public void setPc_total(Double pc_total) {
		this.pc_total = pc_total;
	}

	public String getPc_agreed() {
		return pc_agreed;
	}

	public void setPc_agreed(String pc_agreed) {
		this.pc_agreed = pc_agreed;
	}

	public long getPc_id() {
		return pc_id;
	}

	public void setPc_id(long pc_id) {
		this.pc_id = pc_id;
	}

	public String getCustomerName() {
		return customerName;
	}

	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}

	public String getVendName() {
		return vendName;
	}

	public void setVendName(String vendName) {
		this.vendName = vendName;
	}

	public PurchaseChange() {
	}

	/**
	 * 转为移动端的变更单
	 * 
	 * @param orderChange
	 */
	public PurchaseChange(PurchaseOrderChange orderChange) {
		if (orderChange.getAgreed() != null && orderChange.getAgreed() == Constant.YES) {
            this.pc_agreed = Status.AGREED.getPhrase();
        }
		this.pc_id = orderChange.getId();
		this.pc_code = orderChange.getCode();
		this.pc_currency = orderChange.getOldCurrency();
		this.pc_date = orderChange.getDate();
		this.pc_newcurrency = orderChange.getNewCurrency();
		this.pc_newpayments = orderChange.getNewPayments();
		this.pc_newrate = orderChange.getNewRate();
		this.pc_payments = orderChange.getOldPayments();
		this.pc_purccode = orderChange.getOrderCode();
		this.pc_rate = orderChange.getOldRate();
		this.pc_remark = orderChange.getRemark();
		this.customerName = orderChange.getEnterprise().getEnName();
		this.pc_total = 0.0;
		if (!CollectionUtils.isEmpty(orderChange.getOrderChangeItems())) {
			for (PurchaseOrderChangeItem item : orderChange.getOrderChangeItems()) {
				this.pc_total += item.getNewPrice() * item.getNewQty();
			}
			this.pc_total = NumberUtil.formatDouble(this.pc_total, 2);
		}
		// 取到多个企业的单据，需要切换供应商名字
		if(!Objects.equals(orderChange.getVendUU(), SystemSession.getUser().getEnterprise().getUu())) {
			SystemSession.getUser().setCurrentEnterprise(orderChange.getVendUU());
		}
		this.vendName = SystemSession.getUser().getEnterprise().getEnName();
	}

}
