package com.jwipc.finance.dto;


import cn.hutool.core.date.DateUtil;
import com.jwipc.finance.entity.AccountStatementDetail;
import com.jwipc.finance.entity.ReceiveVoucher;
import lombok.Data;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * <p>
 *     对账单保存明细实体类
 * </p>
 *
 * @author li.kui
 * @version 1.0
 * @since 2020/8/3
 */
@Data
public class StatementItemDTO {
    /**
     * 对账单明细表ID
     */
    private Long id;
    private Long receiveVoucherId;
    private Integer returnConfirmStatus;
    /**
     * 行号
     */
    private String itemNo;
    /**
     * 对账单ID
     */
    private Long asId;
    /**
     * 对账单号
     */
    private String asNo;
    /**
     * 凭证ID
     */
    private Long receiveId;
    /**
     * SC订单号
     */
    private String zlsdh;
    private String poType;
    private String poTypeName;
    /**
     * 凭证号
     */
    private String receiveCode;
    /**
     * 凭证行号
     */
    private String receiveRowNo;
    /**
     * 入库凭证标识 S:入库 H:退货
     */
    private String voucherFlag;
    /**
     * 出入库标识 格式:入库(101)
     */
    private String inOutFlag;
    /**
     * 入库数量
     */
    private BigDecimal receiveNum;
    /**
     * 入库日期
     */
    private Date receiveDate;
    private Date purPostDate;
    private String extendFiled1;
    /**
     * 入库凭证年度
     */
    private String receiveYear;
    /**
     * 入库凭证SAP客户端编码
     */
    private String sapClient;
    /**
     * 物料编码
     */
    private String materialCode;
    /**
     * 物料名称
     */
    private String materialName;
    /**
     * 规格
     */
    private String specs;
    /**
     * 物料单位
     */
    private String materialUnit;
    /**
     * 对账数量
     */
    private BigDecimal asQuantity;
    /**
     * 税率编码
     */
    private String taxCode;
    /**
     * 税率描述
     */
    private String taxDesc;
    /**
     * (税率编码)税率描述
     */
    private String taxCodeDesc;
    /**
     * 税率值(%)
     */
    private BigDecimal taxValue;
    /**
     * 未税单价
     */
    private BigDecimal notTaxPrice;
    /**
     * 含税单价
     */
    private BigDecimal taxPrice;
    /**
     * 未税金额
     */
    private BigDecimal notTaxAmount;
    /**
     * 含税金额
     */
    private BigDecimal taxAmount;
    private BigDecimal originalTaxPrice;
    private BigDecimal mvNum;
    private BigDecimal originalTaxAmount;

    /**
     * 差税额
     */
    private BigDecimal diffTaxAmount;
    /**
     * 开票完成标识 0:未开票 1:已开票
     */
    private Integer invoiceCompleteFlag;
    /**
     * 采购是否可编辑 0:否 1:是
     */
    private Integer editFlag;
    /**
     * SAP订单号
     */
    private String poCode;
    /**
     * 采购订单行号
     */
    private String poItemNo;
    /**
     * 采购组编码
     */
    private String pgCode;
    /**
     * 采购组名称
     */
    private String pgName;
    /**
     * 付款方式编码
     */
    private String paymentWayCode;
    /**
     * 付款方式名称
     */
    private String paymentWayDesc;
    /**
     * (付款方式编码)付款方式名称
     */
    private String paymentWayCodeDesc;
    /**
     * 付款条件编码
     */
    private String paymentTermCode;
    /**
     * 付款条件名称
     */
    private String paymentTermName;
    /**
     * (付款条件编码)付款条件名称
     */
    private String paymentTermCodeName;



    /**
     * 价格单位
     */
    private String priceUnit;
    /**
     * 对账差异含税单价
     */
    private BigDecimal asDiffPrice = BigDecimal.ZERO;
    /**
     * 对账差异含税金额
     */
    private BigDecimal asDiffAmount = BigDecimal.ZERO;


    // 对账时相关金额
    /**
     * 对账时订单未税金额 = 对账数量 * 订单未税单价
     */
    private BigDecimal asNtAmount = BigDecimal.ZERO;
    /**
     * 对账时订单含税金额 = 对账数量 * 订单含税单价
     */
    private BigDecimal asItAmount = BigDecimal.ZERO;
    /**
     * 对账时订单税金 = 对账时订单含税金额 - 对账时订单未税金额
     */
    private BigDecimal asDtAmount = BigDecimal.ZERO;


    // 采购审批相关字段
    /**
     * 采购审批-订单未税单价
     */
    private BigDecimal orderNotTaxPrice;

    /**
     * 订单原含税单价
     */
    private BigDecimal orderOriginalTaxPrice;

    /**
     * 其他折扣
     */
    private BigDecimal deductionRatio;


    /**
     * 采购审批-订单含税单价
     */
    private BigDecimal orderTaxPrice;
    /**
     * 采购审批-订单未税金额
     */
    private BigDecimal orderNotTaxAmount;
    /**
     * 采购审批-订单含税金额
     */
    private BigDecimal orderTaxAmount;
    /**
     * 采购订单-税额
     */
    private BigDecimal orderDiffTaxAmount;
    /**
     * 采购审批-订单税率值
     */
    private Integer orderTaxValue;
    /**
     * 采购审批-订单税率编码
     */
    private String orderTaxCode;
    /**
     * 采购审批-订单税率描述
     */
    private String orderTaxDesc;
    /**
     * (订单税率编码)订单税率描述
     */
    private String orderTaxCodeDesc;

    // 承运商对账字段
    /**
     * 材料商编码
     */
    private String materialSupplierCode;
    /**
     * 材料商名称
     */
    private String materialSupplierName;
    /**
     * 运费类型
     */
    private String freightType;


    // 集采订单相关信息
    /**
     * 集采SAP订单号
     */
    private String jcPoCode;
    /**
     * 集采采购订单行号
     */
    private String jcPoItemNo;
    /**
     * 集采工厂编码
     */
    private String jcFactoryCode;
    private String factoryCode;


    /**
     * 累计开票金额(申请开票金额 + 采购确认金额 + 财务过账金额)
     */
    private BigDecimal invoicedAmount = BigDecimal.ZERO;
    /**
     * 剩余可开票金额((入库数量-对账数量) * 订单含税单价)
     */
    private BigDecimal surplusAmount = BigDecimal.ZERO;
    /**
     * 剩余可用数量
     */
    private BigDecimal surplusNum = BigDecimal.ZERO;


    /**
     * 参考凭证号
     */
    private String cvCode;
    /**
     * 参考凭证行号
     */
    private String cvItemNo;
    /**
     * 参考凭证年度
     */
    private String cvYear;
    /**
     * SAP移动类型
     */
    private String sapMoveType;
    /**
     * 最低对账数量
     */
    private BigDecimal minStatementQuantity;

    private Integer deleteFlag;
    /**
     * 合同号
     */
    private String contractNo;

    private String purchaseUser;

    /**
     * 凭证日期
     */
    private LocalDateTime mvPostDate;
    private String mvPostDateStr;
    private String mvCode;
    private String deliveryOrderCode;
    private String middleClass;
    private String headUpText;
    private LocalDateTime paymentExpireDate;
    private String invoicedCode;


    /**
     * 报关公司编码
     */
    private String supplierCode2;

    /**
     * 报关公司名称
     */
    private String supplierName2;

    private String  subjectTypeCode;
    private String  rowTypeCode;
    private BigDecimal  sharedAmount;
    private BigDecimal summaryReconciliationAmount = BigDecimal.ZERO;
    /**
     * 计算对账时订单税金
     *
     * @return 结果
     */
//    public BigDecimal getAsDtAmount() {
//        return this.asItAmount.subtract(this.asNtAmount);
//    }

    /**
     * 计算对账时订单未税金额
     *
     * @return 结果
     */
//    public BigDecimal getAsNtAmount(){
//        BigDecimal tax = new BigDecimal(this.getOrderTaxValue()).divide(new BigDecimal("100"), 2, BigDecimal.ROUND_HALF_UP).add(BigDecimal.ONE);
//        return this.asItAmount.divide(tax, 2, BigDecimal.ROUND_HALF_UP);
//    }

    public String getReceiveDate(){
        if(null != this.receiveDate){
            return DateUtil.format(this.receiveDate, "yyyy/MM/dd");
        }
        return "";
    }

    public String getInOutFlag(){
        if(null != this.voucherFlag && null != this.sapMoveType){
            if("S".equals(this.voucherFlag)){
                return "<span style='color: green;'>入库(" + this.sapMoveType + ")</span>";
            }else {
                return "<span style='color: red;'>出库(" + this.sapMoveType + ")</span>";
            }
        }
        return "";
    }

    /**
     * 转换对账单明细数据
     *
     * @return 结果
     */
    public AccountStatementDetail toAsd(){
        AccountStatementDetail asd = new AccountStatementDetail();
        asd.setId(this.id);
        asd.setItemNo(this.itemNo);
        asd.setAsId(this.asId);
        asd.setAsNo(this.asNo);
        asd.setReceiveId(this.receiveId);
        asd.setReceiveCode(this.receiveCode);
        asd.setReceiveRowNo(this.receiveRowNo);
        asd.setReceiveNum(this.receiveNum);
        asd.setAsQuantity(this.asQuantity);
        asd.setNotTaxPrice(this.notTaxPrice);
        asd.setTaxPrice(this.taxPrice);
        asd.setNotTaxAmount(this.notTaxAmount);
        asd.setTaxAmount(this.taxAmount);
        asd.setDiffTaxAmount(this.diffTaxAmount);
        asd.setPriceUnit(this.priceUnit);
        asd.setTaxCode(this.taxCode);
        asd.setTaxValue(this.taxValue);
        asd.setAsDiffPrice(this.asDiffPrice);
        asd.setAsDiffAmount(this.asDiffAmount);
        asd.setEditFlag(this.editFlag);
        asd.setMinStatementQuantity(this.minStatementQuantity);
        asd.setSubjectTypeCode(this.subjectTypeCode);
        asd.setRowTypeCode(this.rowTypeCode);
        return asd;
    }


    /**
     * 确认对账单明细是否可以编辑时使用
     *
     * @return 结果
     */
    public ReceiveVoucher toRv(){
        ReceiveVoucher rv = new ReceiveVoucher();
        rv.setId(this.receiveId);
        rv.setMvCode(this.receiveCode);
        rv.setMvItemNo(this.receiveRowNo);
        rv.setMvNum(this.receiveNum);
        rv.setVoucherFlag(this.voucherFlag);
        rv.setCvCode(this.cvCode);
        rv.setCvItemNo(this.cvItemNo);
        rv.setCvYear(this.cvYear);
        rv.setSapMoveType(this.sapMoveType);
        return rv;
    }
}
