package com.jwipc.finance.dto;

import com.jwipc.finance.entity.FinPaymentConfirmLog;
import com.jwipc.sys.dto.BusinessAttachmentDTO;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;

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

/**
 * <p>
 *     预付款申请数据保存实体类
 * </p>
 *
 * @author li.kui
 * @version 1.0
 * @since 2020/9/28
 */
@Data
public class PaymentAdvanceApplySaveDto {
    /**
     * 保存类型 1:新增 2:编辑
     */
    private Integer saveType;

    /**
     * 变更单审批状态；0-审批中  1-审批通过  2-审批驳回
     */
    private Integer changeStatus;
    /**
     * 冻结状态 0-暂存；1-冻结 2-解冻
     */
    private Integer freezeStatus;
    private Integer urgentFlag;
    private BigDecimal refundAmount;
    /**
     * 签署附件
     */
    private List<BusinessAttachmentDTO> businessAttachmentDTOList;
    /**
     * 是否推送OA 0:否 1:是
     */
    private Integer isPushOa;
    private String feePayer;
    private Integer advanceType;
    private Integer poSatisfyFlag;
    /**
     * 物理主键
     */
    private Long id;
    /**
     * 预付款申请单号
     */
    private String apApplyNo;
    /**
     * 付款委托书附件
     */
    private String paymentFiles;
    /**
     * 申请付款金额
     */
    private BigDecimal applyPaymentAmount;
    /**
     * 付款公司代码
     */
    private String companyCode;
    /**
     * 付款公司名称
     */
    private String companyName;
    /**
     * 工厂代码
     */
    private String factoryCode;
    /**
     * 工厂名称
     */
    private String factoryName;
    /**
     * 供应商编码
     */
    private String supplierCode;
    /**
     * 供应商名称
     */
    private String supplierName;
    /**
     * 供应商编码
     */
    private String paymentSupplierCode;
    /**
     * 供应商名称
     */
    private String paymentSupplierName;
    private Integer collectionFlag;
    /**
     * 供应商银行代码/供应商开户行代码/供应商联行号
     */
    private String supBankCode;
    /**
     * 供应商银行名称/供应商开户行名称
     */
    private String supBankName;
    /**
     * 供应商银行账号
     */
    private String supBankAccount;
    /**
     * 供应商银行代码
     */
    private String bankCode;
    /**
     * 供应商银行国际代码
     */
    private String bankInterCode;
    /**
     * 币种
     */
    private String currencyCode;
    /**
     * 付款方式编码
     */
    private String paymentWayCode;
    /**
     * 付款方式描述
     */
    private String paymentWayDesc;
    /**
     * 付款方式(期限)编码
     */
    private String paymentTermCode;
    /**
     * 付款方式(期限)描述
     */
    private String paymentTermDesc;
    /**
     * 项目经理
     */
    private String oaProjectManager;
    /**
     * 项目经理编码
     */
    private String oaProjectManagerCode;

    private String oaSubKey;
    private String oaDepKey;
    private String oaYear;
    private String oaDemandDep;
    private String oaAleradyPayMoney;
    private String oaPlantArea;
    private String oaContract;
    private BigDecimal budInnerMoney;
    private BigDecimal budOutMoney;
    private String oaDeviceName;
    private String oaSpecs;
    private String oaUnit;
    private BigDecimal oaNum;
    private BigDecimal oaQuaeterBud;
    private BigDecimal oaBudInnerMoney;
    private BigDecimal oaBudOutMoney;
    private BigDecimal oaBalance;
    private String oaUrgencyDegree;
    private String oaSubName;
    private String oaCode;
    private String oaDepName;
    private String oaQuarter;
    private String oaType;
    private BigDecimal oaContractMoney;
    private BigDecimal oaTransferMoney;
    private String oaProjectName;
    private String oaChildProjectName;
    private String files;
    /**
     * 申请备注
     */
    private String applyRemark;
    /**
     * 设备验收人code
     */
    private String  oaDevicePersonCode;
    /**
     * 设备验收人
     */
    private String oaDevicePerson;
    /**
     * 固定资产code
     */
    private String oaFixedAssetsCode;
    /**
     * 承兑金额
     */
    private BigDecimal oaAcceptanceMoney;
    /**
     * 维修内容
     */
    private String oaRepairContent;
    /**
     * 会计主管
     */
    private String oaAccountantCharge;
    /**
     * 会计主管code
     */
    private String oaAccountantChargeCode;
    /**
     * 经办人
     */
    private String oaHandler;
    /**
     * 经办人code
     */
    private String oaHandlerCode;
    /**
     * 付款用途
     */
    private String paymentPurpose;
    /**
     * 付款用途描述
     */
    private String paymentPurposeDesc;
    /**
     * 费用归属
     */
    private String oaCostAttribution;
    /**
     * 合同号
     */
    private String contractNo;
    /**
     * 流程类型
     */
    private Integer processId;
    /**
     * 研发项目
     */
    private String rdProject;
    /**
     * 用途
     */
    private String purpose;
    /**
     * 借款金额
     */
    private BigDecimal oaBorrowMoney;

    private Integer isOa;

    private Byte isSafeProductionCost;

    private String oaProcessCode;

    private String adviceFjid;
    /**
     * 租聘费类别 0：员工、经理、总监房租 1：公司日常组织
     */
    private String oaHireType;
    /**
     * 年度预算代码
     */
    private String oaYearBudget;
    /**
     * 申请人工号
     */
    private String oaApplyUserCode;
    /**
     * 申请人名称
     */
    private String oaApplyUserName;

    /**
     * 创建时间
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

    /**
     * 费用部门编码
     */
    private String costDeptCode;

    /**
     * 费用部门名称
     */
    private String costDeptName;


    /**
     * 使用人编码
     */
    private String useCode;

    /**
     * 使用人名称
     */
    private String useName;


    /**
     * 主管BP经理编码
     */
    private String BPCode;

    /**
     * 主管BP经理名称
     */
    private String BPName;

    /**
     * 费用科目
     */
    private String expenseAccount;

    /**
     * 费用科目名称
     */
    private String expenseAccountName;
    /**
     * OA项目
     */
    private String oaXm;
    /**
     * OA业务科目
     */
    private String oaYwkm;

    /**
     * llkzj
     */
    private String llkzj;

    /**
     * 申请明细
     */
    private List<ApplyDetail> itemList;


    /**
     * 编辑时删除的明细ID
     */
    private List<Long> delItemIdList;

    /**
     * 是否提前付款；0-否；1-是
     */
    private Integer advancePaymentFlag;

    /**
     * 付款计划金额，如果没有则为0
     */
    private BigDecimal payPlanMoney =  BigDecimal.ZERO;

    /**
     * 承兑期限
     */
    private String acceptanceMethod;

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

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

    /**
     * 实际应付金额
     */
    private BigDecimal actualityPayMoney;
    private String businessType;
    /**
     * 付款公司
     */
    private String paymentCompanyCode;

    /**
     * 收款单位
     */
    private String paymentCompanyName;

    /**
     * 收款地址
     */
    private String bankAddress;
    /**
     * 逾期款项
     */
    private BigDecimal overdueAmount;
    /**
     * 到期款项
     */
    private BigDecimal dueAmount;
    /**
     * 应付账款余额
     */
    private BigDecimal accountsPayableBalance;
    /**
     * 暂估余额
     */
    private BigDecimal provisionalBalance;

    /**
     * 警告日志
     */
    private List<FinPaymentConfirmLog> logDetailList;
    /**
     * 申请明细
     */
    @Data
    public static class ApplyDetail {
        /**
         * 物理主键
         */
        private Long id;
        /**
         * 预付款申请ID
         */
        private Long appId;
        /**
         * 预付款申请单号
         */
        private String appNo;
        /**
         * 采购订单号
         */
        private String poCode;
        /**
         * 订单行号
         */
        private String poItemNo;
        /**
         * 订单数量
         */
        private BigDecimal poNum;
        /**
         * 物料编码
         */
        private String materialCode;
        /**
         * 物料描述
         */
        private String materialDesc;
        /**
         * 单位
         */
        private String materialUnit;
        /**
         * 税率代码
         */
        private String taxCode;
        /**
         * 税率描述
         */
        private String taxDesc;
        /**
         * 税率值(0.13)
         */
        private BigDecimal taxValue;
        /**
         * 未税单价
         */
        private BigDecimal ntPrice;
        /**
         * 含税单价
         */
        private BigDecimal itPrice;
        /**
         * 未税金额
         */
        private BigDecimal ntAmount;
        /**
         * 含税金额
         */
        private BigDecimal itAmount;
        /**
         * 税额
         */
        private BigDecimal dtAmount;
        /**
         * 本次申请付款金额
         */
        private BigDecimal currApAmount;
        /**
         * 行备注
         */
        private String itemRemark;
        /**
         * 询源采购组编码
         */
        private String seekPgCode;
        /**
         * 询源采购组名称
         */
        private String seekPgName;
        /**
         * 订单行工厂编码
         */
        private String factoryCode;
        /**
         * 付款方式编码
         */
        private String paymentWayCode;
        /**
         * 付款方式描述
         */
        private String paymentWayDesc;
        /**
         * 付款条件编码
         */
        private String paymentConditionCode;
        /**
         * 付款条件描述
         */
        private String paymentConditionDesc;
        /**
         * 预算内金额
         */
        private BigDecimal budInnerMoney;
        /**
         * 预算外金额
         */
        private BigDecimal budOutMoney;

        private String contractNo;

        private String contractName;


        /**
         * 邮件发送标识
         */
        private Integer sendEmailCollectionFlag;

        /**
         * 预付款 付款到期日
         */
        @DateTimeFormat(pattern = "yyyy-MM-dd")
        private LocalDateTime paymentExpireDate;
        @DateTimeFormat(pattern = "yyyy-MM-dd")
        private LocalDateTime expectationPaymentDate;

        /**
         * 折扣金额
         */
        private BigDecimal discountMoney;
        private BigDecimal alreadyAccountAmount;
        private BigDecimal storeAmount;


        /**
         * 数据转化表结构 方便存储
         *
         * @return 结果
         */
        public PaymentAdvanceApplyDetailBaseDTO toPaaDetail(){
            PaymentAdvanceApplyDetailBaseDTO detail = new PaymentAdvanceApplyDetailBaseDTO();
            detail.setId(this.id);
            detail.setAppId(this.appId);
            detail.setAlreadyAccountAmount(this.alreadyAccountAmount);
            detail.setStoreAmount(this.storeAmount);
            detail.setAppNo(this.appNo);
            detail.setPoCode(this.poCode);
            detail.setPoItemNo(this.poItemNo);
            detail.setPoNum(this.poNum);
            detail.setMaterialCode(this.materialCode);
            detail.setMaterialDesc(this.materialDesc);
            detail.setMaterialUnit(this.materialUnit);
            detail.setTaxCode(this.taxCode);
            detail.setTaxDesc(this.taxDesc);
            detail.setTaxValue(this.taxValue);
            detail.setNtPrice(this.ntPrice);
            detail.setItPrice(this.itPrice);
            detail.setNtAmount(this.ntAmount);
            detail.setItAmount(this.itAmount);
            detail.setDtAmount(this.dtAmount);
            detail.setBudInnerMoney(this.budInnerMoney);
            detail.setBudOutMoney(this.budOutMoney);
            detail.setItemRemark(this.itemRemark);
            detail.setSeekPgCode(this.seekPgCode);
            detail.setSeekPgName(this.seekPgName);
            detail.setFactoryCode(this.factoryCode);
            detail.setPaymentWayCode(this.paymentWayCode);
            detail.setPaymentWayDesc(this.paymentWayDesc);
            detail.setPaymentConditionCode(this.paymentConditionCode);
            detail.setPaymentConditionDesc(this.paymentConditionDesc);
            detail.setContractName(this.contractName);
            detail.setPaymentExpireDate(this.paymentExpireDate);
            detail.setExpectationPaymentDate(this.expectationPaymentDate);
            detail.setContractNo(this.contractNo);
            detail.setDiscountMoney(this.discountMoney);

            // 数据主键 采购订单号_订单行号
            String dataKey = this.poCode + "_" + this.poItemNo;
            detail.setDataKey(dataKey);
            return detail;
        }

    }


    /**
     * 数据转化为表结构 方便存储
     *
     * @return 结果
     */
    public PaymentAdvanceApplyBaseDTO toPaa(){
        PaymentAdvanceApplyBaseDTO apply = new PaymentAdvanceApplyBaseDTO();
        apply.setId(this.id);
        apply.setApApplyNo(this.apApplyNo);
        apply.setRefundAmount(this.refundAmount);
        apply.setCompanyCode(this.companyCode);
        apply.setCompanyName(this.companyName);
        apply.setUrgentFlag(this.urgentFlag);
        apply.setFactoryCode(this.factoryCode);
        apply.setPoSatisfyFlag(this.poSatisfyFlag);
        apply.setFactoryName(this.factoryName);
        apply.setAdvanceType(this.advanceType);
        apply.setFeePayer(this.feePayer);
        apply.setSupplierCode(this.supplierCode);
        apply.setSupplierName(this.supplierName);
        apply.setPaymentSupplierCode(this.paymentSupplierCode);
        apply.setPaymentSupplierName(this.paymentSupplierName);
        apply.setCollectionFlag(this.collectionFlag);
        apply.setSupBankCode(this.supBankCode);
        apply.setSupBankName(this.supBankName);
        apply.setSupBankAccount(this.supBankAccount);
        apply.setBankCode(this.bankCode);
        apply.setBankInterCode(this.bankInterCode);
        apply.setCurrencyCode(this.currencyCode);
        apply.setPaymentFiles(this.paymentFiles);
        apply.setPaymentWayCode(this.paymentWayCode);
        apply.setPaymentWayDesc(this.paymentWayDesc);
        apply.setActualityPayMoney(this.actualityPayMoney);
        apply.setOaSubKey(this.oaSubKey);
        apply.setOaDepKey(this.oaDepKey);
        apply.setOaYear(this.oaYear);
        apply.setOaDemandDep(this.oaDemandDep);
        if(StringUtils.isNotEmpty(this.oaAleradyPayMoney)){
            apply.setOaAleradyPayMoney(new BigDecimal(this.oaAleradyPayMoney));
        }
        apply.setOaPlantArea(this.oaPlantArea);
        apply.setOaContract(this.oaContract);
        apply.setBudInnerMoney(this.budInnerMoney);
        apply.setBudOutMoney(this.budOutMoney);
        apply.setOaDeviceName(this.oaDeviceName);
        apply.setOaSpecs(this.oaSpecs);
        apply.setOaUnit(this.oaUnit);
        apply.setOaNum(this.oaNum);
        apply.setOaQuaeterBud(this.oaQuaeterBud);
        apply.setOaBudOutMoney(this.oaBudOutMoney);
        apply.setOaBudInnerMoney(this.oaBudInnerMoney);
        apply.setOaBalance(this.oaBalance);
        apply.setOaUrgencyDegree(this.oaUrgencyDegree);
        apply.setOaSubName(this.oaSubName);
        apply.setOaCode(this.oaCode);
        apply.setOaDepName(this.oaDepName);
        apply.setOaQuarter(this.oaQuarter);
        apply.setOaType(this.oaType);
        apply.setApplyRemark(this.applyRemark);
        apply.setOaDevicePersonCode(this.oaDevicePersonCode);
        apply.setOaDevicePerson(this.oaDevicePerson);
        apply.setOaFixedAssetsCode(this.oaFixedAssetsCode);
        apply.setOaAcceptanceMoney(this.oaAcceptanceMoney);
        apply.setOaRepairContent(this.oaRepairContent);
        apply.setOaAccountantCharge(this.oaAccountantCharge);
        apply.setOaAccountantChargeCode(this.oaAccountantChargeCode);
        apply.setOaHandler(this.oaHandler);
        apply.setOaHandlerCode(this.oaHandlerCode);
        apply.setOaCostAttribution(this.oaCostAttribution);
        apply.setContractNo(this.contractNo);
        apply.setProcessId(this.processId);
        apply.setRdProject(this.rdProject);
        apply.setPurpose(this.purpose);
        apply.setOaContractMoney(this.oaContractMoney);
        apply.setOaTransferMoney(this.oaTransferMoney);
        apply.setOaBorrowMoney(this.oaBorrowMoney);
        apply.setIsOa(this.isOa);
        apply.setIsSafeProductionCost(this.isSafeProductionCost);
        apply.setOaProcessCode(this.oaProcessCode);
        apply.setOaProjectName(this.oaProjectName);
        apply.setOaChildProjectName(this.oaChildProjectName);
        apply.setOaHireType(this.oaHireType);
        apply.setOaYearBudget(this.oaYearBudget);
        apply.setOaApplyUserCode(this.oaApplyUserCode);
        apply.setOaApplyUserName(this.oaApplyUserName);
        apply.setOaProjectManager(this.oaProjectManager);
        apply.setOaProjectManagerCode(this.oaProjectManagerCode);
        apply.setCostDeptCode(this.costDeptCode);
        apply.setCostDeptName(this.costDeptName);
        apply.setUseCode(this.useCode);
        apply.setUseName(this.useName);
        apply.setBpCode(this.BPCode);
        apply.setBpName(this.BPName);
        apply.setCreateTime(this.createTime);
        apply.setExpenseAccount(this.expenseAccount);
        apply.setExpenseAccountName(this.expenseAccountName);
        apply.setOaXm(this.oaXm);
        apply.setOaYwkm(this.oaYwkm);
        apply.setLlkzj(this.llkzj);
        apply.setFiles(this.files);
        apply.setAdvancePaymentFlag(this.advancePaymentFlag);
        apply.setAcceptanceMethod(this.acceptanceMethod);
        apply.setFaSupplier(this.faSupplier);
        apply.setFaSupplierName(this.faSupplierName);
        apply.setPayPlanMoney(this.payPlanMoney);
        apply.setPaymentPurpose(this.paymentPurpose);
        apply.setPaymentPurposeDesc(this.paymentPurposeDesc);
        apply.setBusinessType(this.businessType);
        apply.setPaymentCompanyCode(this.paymentCompanyCode);
        apply.setPaymentCompanyName(this.paymentCompanyName);
        apply.setBankAddress(this.bankAddress);
        apply.setOverdueAmount(this.overdueAmount);
        apply.setDueAmount(this.dueAmount);
        apply.setAccountsPayableBalance(this.accountsPayableBalance);
        apply.setProvisionalBalance(this.provisionalBalance);
        return apply;
    }
}
