package cn.palmte.work.bean;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class FinalBean {

    //========================收入表================================

    /**
     * 设备类决算总额
     */
    private BigDecimal incomeDeviceFinalTotal;

    /**
     * 工程类决算总额
     */
    private BigDecimal incomeEngineerFinalTotal;

    /**
     * 服务类决算总额
     */
    private BigDecimal incomeServiceFinalTotal;

    /**
     * 获取所有收入决算总额
     *
     * @return
     */
    public BigDecimal getIncomeTotal() {
        BigDecimal incomeDeviceFinalTotal = getIncomeDeviceFinalTotal();
        BigDecimal incomeEngineerFinalTotal = getIncomeEngineerFinalTotal();
        BigDecimal incomeServiceFinalTotal = getIncomeServiceFinalTotal();
        if (null == incomeDeviceFinalTotal || null == incomeEngineerFinalTotal || null == incomeServiceFinalTotal) {
            return new BigDecimal(0);
        }
        return incomeDeviceFinalTotal.add(incomeEngineerFinalTotal).add(incomeServiceFinalTotal);
    }

    //========================成本表================================

    /**
     * 设备采购成本决算总额
     */
    private BigDecimal costPurchaseDeviceFinalTotal;

    /**
     * 施工采购成本决算总额
     */
    private BigDecimal costPurchaseBuildFinalTotal;

    /**
     * 服务采购成本决算总额
     */
    private BigDecimal costPurchaseServiceFinalTotal;

    /**
     * 其他采购成本决算总额
     */
    private BigDecimal costPurchaseOtherFinalTotal;

    /**
     * 项目管理成本决算总额
     */
    private BigDecimal costProjectManageFinalTotal;

    /**
     * 其他成本决算总额
     */
    private BigDecimal costOtherFinalTotal;

    /**
     * 获取所有成本决算总额
     *
     * @return
     */
    public BigDecimal getCostTotal() {
        BigDecimal costPurchaseDeviceFinalTotal = getCostPurchaseDeviceFinalTotal();
        BigDecimal costPurchaseBuildFinalTotal = getCostPurchaseBuildFinalTotal();
        BigDecimal costPurchaseServiceFinalTotal = getCostPurchaseServiceFinalTotal();
        BigDecimal costPurchaseOtherFinalTotal = getCostPurchaseOtherFinalTotal();
        BigDecimal costProjectManageFinalTotal = getCostProjectManageFinalTotal();
        BigDecimal costOtherFinalTotal = getCostOtherFinalTotal();

        if (null == costPurchaseDeviceFinalTotal || null == costPurchaseBuildFinalTotal || null == costPurchaseServiceFinalTotal
                || null == costPurchaseOtherFinalTotal || null == costProjectManageFinalTotal) {
            return new BigDecimal(0);
        }

        return costPurchaseDeviceFinalTotal.add(costPurchaseBuildFinalTotal).add(costPurchaseServiceFinalTotal)
                .add(costPurchaseOtherFinalTotal).add(costProjectManageFinalTotal).add(costOtherFinalTotal);
    }


    //========================管理表================================

    /**
     * 财务费用决算总额
     */
    private BigDecimal costExpropriationFinalTotal;

    /**
     * 公司管理费用决算总额
     */
    private BigDecimal costCompanyManageFinalTotal;

    /**
     * 所得税费用决算总额
     */
    private BigDecimal costIncomeTaxFinalTotal;

    /**
     * 获取所有管理成本决算总额
     *
     * @return
     */
    public BigDecimal getCostManageTotal() {
        BigDecimal costExpropriationFinalTotal = getCostExpropriationFinalTotal();
        BigDecimal costCompanyManageFinalTotal = getCostCompanyManageFinalTotal();
        BigDecimal costIncomeTaxFinalTotal = getCostIncomeTaxFinalTotal();

        if (null == costExpropriationFinalTotal || null == costCompanyManageFinalTotal || null == costIncomeTaxFinalTotal) {
            return new BigDecimal(0);
        }

        return costExpropriationFinalTotal.add(costCompanyManageFinalTotal).add(costIncomeTaxFinalTotal);
    }


    //========================利润率计算表================================

    /**
     * 项目毛利决算总额
     */
    private BigDecimal grossProfitFinalTotal;

    public BigDecimal getGrossProfitFinalTotal() {
        BigDecimal incomeTotal = getIncomeTotal();
        BigDecimal costTotal = getCostTotal();
        BigDecimal costExpropriationFinalTotal = getCostExpropriationFinalTotal();

        if (null == incomeTotal || null == costTotal || null == costExpropriationFinalTotal) {
            return null;
        }
        return incomeTotal.subtract(costTotal).subtract(costExpropriationFinalTotal);
    }

    public void setGrossProfitFinalTotal(BigDecimal grossProfitFinalTotal) {
        this.grossProfitFinalTotal = grossProfitFinalTotal;
    }

    /**
     * 项目毛利利润率
     */
    private BigDecimal grossProfitProfitMargin;

    public BigDecimal getGrossProfitProfitMargin() {
        BigDecimal grossProfitFinalTotal = getGrossProfitFinalTotal();
        BigDecimal incomeTotal = getIncomeTotal();

        if(null == grossProfitFinalTotal || null == incomeTotal){
            return null;
        }
        if (incomeTotal.compareTo(BigDecimal.valueOf(0.01)) < 0 && incomeTotal.compareTo(BigDecimal.valueOf(-0.01)) > 0) {
            incomeTotal = BigDecimal.ONE;
        }
        return grossProfitFinalTotal.divide(incomeTotal,4, RoundingMode.HALF_UP).multiply(new BigDecimal(100));
    }

    public void setGrossProfitProfitMargin(BigDecimal grossProfitProfitMargin) {
        this.grossProfitProfitMargin = grossProfitProfitMargin;
    }

    /**
     * 项目贡献利润决算总额
     */
    private BigDecimal contributionMarginFinalTotal;

    public BigDecimal getContributionMarginFinalTotal() {
        BigDecimal grossProfitFinalTotal = getGrossProfitFinalTotal();
        BigDecimal costCompanyManageFinalTotal = getCostCompanyManageFinalTotal();

        if (null == grossProfitFinalTotal || null == costCompanyManageFinalTotal) {
            return null;
        }
        return grossProfitFinalTotal.subtract(costCompanyManageFinalTotal);
    }

    public void setContributionMarginFinalTotal(BigDecimal contributionMarginFinalTotal) {
        this.contributionMarginFinalTotal = contributionMarginFinalTotal;
    }

    /**
     * 项目贡献利润利润率
     */
    private BigDecimal contributionMarginProfitMargin;

    public BigDecimal getContributionMarginProfitMargin() {
        BigDecimal contributionMarginFinalTotal = getContributionMarginFinalTotal();
        BigDecimal incomeTotal = getIncomeTotal();

        if(null == contributionMarginFinalTotal || null == incomeTotal){
            return null;
        }
        if (incomeTotal.compareTo(BigDecimal.valueOf(0.01)) < 0 && incomeTotal.compareTo(BigDecimal.valueOf(-0.01)) > 0) {
            incomeTotal = BigDecimal.ONE;
        }
        return contributionMarginFinalTotal.divide(incomeTotal,4, RoundingMode.HALF_UP).multiply(new BigDecimal(100));
    }

    public void setContributionMarginProfitMargin(BigDecimal contributionMarginProfitMargin) {
        this.contributionMarginProfitMargin = contributionMarginProfitMargin;
    }

    /**
     * 项目净利润决算总额
     */
    private BigDecimal netMarginFinalTotal;

    public BigDecimal getNetMarginFinalTotal() {
        BigDecimal contributionMarginFinalTotal = getContributionMarginFinalTotal();
        BigDecimal costIncomeTaxFinalTotal = getCostIncomeTaxFinalTotal();
        if (null == contributionMarginFinalTotal || null == costIncomeTaxFinalTotal) {
            return null;
        }
        return contributionMarginFinalTotal.subtract(costIncomeTaxFinalTotal);
    }

    public void setNetMarginFinalTotal(BigDecimal netMarginFinalTotal) {
        this.netMarginFinalTotal = netMarginFinalTotal;
    }

    /**
     * 项目净利润利润率
     */
    private BigDecimal netMarginProfitMargin;

    public BigDecimal getNetMarginProfitMargin() {
        BigDecimal netMarginFinalTotal = getNetMarginFinalTotal();
        BigDecimal incomeTotal = getIncomeTotal();

        if(null == netMarginFinalTotal || null == incomeTotal){
            return null;
        }
        if (incomeTotal.compareTo(BigDecimal.valueOf(0.01)) < 0 && incomeTotal.compareTo(BigDecimal.valueOf(-0.01)) > 0) {
            incomeTotal = BigDecimal.ONE;
        }
        return netMarginFinalTotal.divide(incomeTotal,4, RoundingMode.HALF_UP).multiply(new BigDecimal(100));
    }

    public void setNetMarginProfitMargin(BigDecimal netMarginProfitMargin) {
        this.netMarginProfitMargin = netMarginProfitMargin;
    }

    //========================结算现金流量表================================

    /**
     * a销售商品、提供劳务收到的现金
     */
    private BigDecimal saleIncomeCash;
    /**
     * b收到的税费返还
     */
    private BigDecimal taxReturn;
    /**
     * c其他与经营活动有关的现金【保证金收入】
     */
    private BigDecimal earnestMoneyIncome;
    /**
     * d购买商品、接受劳务支付的现金【设备支出+工程支出】
     */
    private BigDecimal purchaseCost;
    /**
     * e支付的各项税费
     */
    private BigDecimal taxCost;
    /**
     * f支付其他与经营活动有关的现金
     */
    private BigDecimal earnestMoneyCost;
    /**
     * g经营活动产生的现金流量净额
     * g=a+c+b-d-f-e
     */
    private BigDecimal netCashFlow;

    public BigDecimal getNetCashFlow() {

        BigDecimal saleIncomeCash = getSaleIncomeCash();
        BigDecimal taxReturn = getTaxReturn();
        BigDecimal earnestMoneyIncome = getEarnestMoneyIncome();
        BigDecimal purchaseCost = getPurchaseCost();
        BigDecimal taxCost = getTaxCost();
        BigDecimal earnestMoneyCost = getEarnestMoneyCost();

        if (null == saleIncomeCash || null == taxReturn || null == earnestMoneyIncome ||
                null == purchaseCost || null == taxCost || null == earnestMoneyCost) {
            return null;
        }

        return saleIncomeCash
                .add(taxReturn)
                .add(earnestMoneyIncome)
                .subtract(purchaseCost)
                .subtract(taxCost)
                .subtract(earnestMoneyCost);
    }

    public void setNetCashFlow(BigDecimal netCashFlow) {
        this.netCashFlow = netCashFlow;
    }

    /**
     * h投资活动现金流入
     */
    private BigDecimal cashInflowFromInvestingActivities;
    /**
     * i投资活动现金流出
     */
    private BigDecimal cashOutflowFromInvestingActivities;
    /**
     * j投资活动产生的现金流量净额
     */
    private BigDecimal netCashFromInvestingActivities;
    /**
     * k融资资金流入【垫资计划】
     */
    private BigDecimal financingCapitalInflow;
    /**
     * l融资资金流出【还款计划】
     */
    private BigDecimal financingCapitalOutflow;
    /**
     * m筹资活动产生的现金流量净额
     * m=k-l
     */
    private BigDecimal financingCapitalCashflow;

    public BigDecimal getFinancingCapitalCashflow() {
        BigDecimal financingCapitalInflow = getFinancingCapitalInflow();
        BigDecimal financingCapitalOutflow = getFinancingCapitalOutflow();

        if (null == financingCapitalInflow || null == financingCapitalOutflow) {
            return null;
        }
        return financingCapitalInflow.subtract(financingCapitalOutflow);
    }

    public void setFinancingCapitalCashflow(BigDecimal financingCapitalCashflow) {
        this.financingCapitalCashflow = financingCapitalCashflow;
    }

    /**
     * n货币资金净增加额
     * n=g+j+m
     */
    private BigDecimal netIncreaseMonetaryFunds;

    public BigDecimal getNetIncreaseMonetaryFunds() {
        BigDecimal netCashFlow = getNetCashFlow();
        BigDecimal netCashFromInvestingActivities = getNetCashFromInvestingActivities();
        BigDecimal financingCapitalCashflow = getFinancingCapitalCashflow();

        if (null == netCashFlow || null == netCashFromInvestingActivities || null == financingCapitalCashflow) {
            return null;
        }
        return netCashFlow
                .add(netCashFromInvestingActivities)
                .add(financingCapitalCashflow);
    }

    public void setNetIncreaseMonetaryFunds(BigDecimal netIncreaseMonetaryFunds) {
        this.netIncreaseMonetaryFunds = netIncreaseMonetaryFunds;
    }

    /**
     * 获取所有现金流量决算总额
     *
     * @return
     */
    public BigDecimal getCashFluxTotal() {
        BigDecimal saleIncomeCash = getSaleIncomeCash();
        BigDecimal taxReturn = getTaxReturn();
        BigDecimal earnestMoneyIncome = getEarnestMoneyIncome();
        BigDecimal purchaseCost = getPurchaseCost();
        BigDecimal taxCost = getTaxCost();
        BigDecimal earnestMoneyCost = getEarnestMoneyCost();
        BigDecimal netCashFlow = getNetCashFlow();
        BigDecimal cashInflowFromInvestingActivities = getCashInflowFromInvestingActivities();
        BigDecimal cashOutflowFromInvestingActivities = getCashOutflowFromInvestingActivities();
        BigDecimal netCashFromInvestingActivities = getNetCashFromInvestingActivities();
        BigDecimal financingCapitalInflow = getFinancingCapitalInflow();
        BigDecimal financingCapitalOutflow = getFinancingCapitalOutflow();
        BigDecimal financingCapitalCashflow = getFinancingCapitalCashflow();
        BigDecimal netIncreaseMonetaryFunds = getNetIncreaseMonetaryFunds();

        if (null == saleIncomeCash || null == taxReturn || null == earnestMoneyIncome || null == purchaseCost || null == taxCost
                || null == earnestMoneyCost || null == netCashFlow || null == cashInflowFromInvestingActivities ||
                null == cashOutflowFromInvestingActivities || null == netCashFromInvestingActivities ||
                null == financingCapitalInflow || null == financingCapitalOutflow || null == financingCapitalCashflow || null == netIncreaseMonetaryFunds) {
            return new BigDecimal(0);
        }

        return saleIncomeCash.add(taxReturn).add(earnestMoneyIncome).add(purchaseCost).add(taxCost).add(earnestMoneyCost)
                .add(netCashFlow).add(cashInflowFromInvestingActivities).add(cashOutflowFromInvestingActivities).add(netCashFromInvestingActivities)
                .add(financingCapitalInflow).add(financingCapitalOutflow).add(financingCapitalCashflow).add(netIncreaseMonetaryFunds);
    }


    public BigDecimal getIncomeDeviceFinalTotal() {
        return incomeDeviceFinalTotal;
    }

    public void setIncomeDeviceFinalTotal(BigDecimal incomeDeviceFinalTotal) {
        this.incomeDeviceFinalTotal = incomeDeviceFinalTotal;
    }

    public BigDecimal getIncomeEngineerFinalTotal() {
        return incomeEngineerFinalTotal;
    }

    public void setIncomeEngineerFinalTotal(BigDecimal incomeEngineerFinalTotal) {
        this.incomeEngineerFinalTotal = incomeEngineerFinalTotal;
    }

    public BigDecimal getIncomeServiceFinalTotal() {
        return incomeServiceFinalTotal;
    }

    public void setIncomeServiceFinalTotal(BigDecimal incomeServiceFinalTotal) {
        this.incomeServiceFinalTotal = incomeServiceFinalTotal;
    }

    public BigDecimal getCostPurchaseDeviceFinalTotal() {
        return costPurchaseDeviceFinalTotal;
    }

    public void setCostPurchaseDeviceFinalTotal(BigDecimal costPurchaseDeviceFinalTotal) {
        this.costPurchaseDeviceFinalTotal = costPurchaseDeviceFinalTotal;
    }

    public BigDecimal getCostPurchaseBuildFinalTotal() {
        return costPurchaseBuildFinalTotal;
    }

    public void setCostPurchaseBuildFinalTotal(BigDecimal costPurchaseBuildFinalTotal) {
        this.costPurchaseBuildFinalTotal = costPurchaseBuildFinalTotal;
    }

    public BigDecimal getCostPurchaseServiceFinalTotal() {
        return costPurchaseServiceFinalTotal;
    }

    public void setCostPurchaseServiceFinalTotal(BigDecimal costPurchaseServiceFinalTotal) {
        this.costPurchaseServiceFinalTotal = costPurchaseServiceFinalTotal;
    }

    public BigDecimal getCostPurchaseOtherFinalTotal() {
        return costPurchaseOtherFinalTotal;
    }

    public void setCostPurchaseOtherFinalTotal(BigDecimal costPurchaseOtherFinalTotal) {
        this.costPurchaseOtherFinalTotal = costPurchaseOtherFinalTotal;
    }

    public BigDecimal getCostProjectManageFinalTotal() {
        return costProjectManageFinalTotal;
    }

    public void setCostProjectManageFinalTotal(BigDecimal costProjectManageFinalTotal) {
        this.costProjectManageFinalTotal = costProjectManageFinalTotal;
    }

    public BigDecimal getCostOtherFinalTotal() {
        return (costOtherFinalTotal == null)?BigDecimal.ZERO:costOtherFinalTotal;
    }

    public void setCostOtherFinalTotal(BigDecimal costOtherFinalTotal) {
        this.costOtherFinalTotal = costOtherFinalTotal;
    }

    public BigDecimal getCostExpropriationFinalTotal() {
        return costExpropriationFinalTotal;
    }

    public void setCostExpropriationFinalTotal(BigDecimal costExpropriationFinalTotal) {
        this.costExpropriationFinalTotal = costExpropriationFinalTotal;
    }

    public BigDecimal getCostCompanyManageFinalTotal() {
        return costCompanyManageFinalTotal;
    }

    public void setCostCompanyManageFinalTotal(BigDecimal costCompanyManageFinalTotal) {
        this.costCompanyManageFinalTotal = costCompanyManageFinalTotal;
    }

    public BigDecimal getCostIncomeTaxFinalTotal() {
        return costIncomeTaxFinalTotal;
    }

    public void setCostIncomeTaxFinalTotal(BigDecimal costIncomeTaxFinalTotal) {
        this.costIncomeTaxFinalTotal = costIncomeTaxFinalTotal;
    }

    public BigDecimal getSaleIncomeCash() {
        return saleIncomeCash;
    }

    public void setSaleIncomeCash(BigDecimal saleIncomeCash) {
        this.saleIncomeCash = saleIncomeCash;
    }

    public BigDecimal getTaxReturn() {
        return taxReturn;
    }

    public void setTaxReturn(BigDecimal taxReturn) {
        this.taxReturn = taxReturn;
    }

    public BigDecimal getEarnestMoneyIncome() {
        return earnestMoneyIncome;
    }

    public void setEarnestMoneyIncome(BigDecimal earnestMoneyIncome) {
        this.earnestMoneyIncome = earnestMoneyIncome;
    }

    public BigDecimal getPurchaseCost() {
        return purchaseCost;
    }

    public void setPurchaseCost(BigDecimal purchaseCost) {
        this.purchaseCost = purchaseCost;
    }

    public BigDecimal getTaxCost() {
        return taxCost;
    }

    public void setTaxCost(BigDecimal taxCost) {
        this.taxCost = taxCost;
    }

    public BigDecimal getEarnestMoneyCost() {
        return earnestMoneyCost;
    }

    public void setEarnestMoneyCost(BigDecimal earnestMoneyCost) {
        this.earnestMoneyCost = earnestMoneyCost;
    }

    public BigDecimal getCashInflowFromInvestingActivities() {
        return cashInflowFromInvestingActivities;
    }

    public void setCashInflowFromInvestingActivities(BigDecimal cashInflowFromInvestingActivities) {
        this.cashInflowFromInvestingActivities = cashInflowFromInvestingActivities;
    }

    public BigDecimal getCashOutflowFromInvestingActivities() {
        return cashOutflowFromInvestingActivities;
    }

    public void setCashOutflowFromInvestingActivities(BigDecimal cashOutflowFromInvestingActivities) {
        this.cashOutflowFromInvestingActivities = cashOutflowFromInvestingActivities;
    }

    public BigDecimal getNetCashFromInvestingActivities() {
        return netCashFromInvestingActivities;
    }

    public void setNetCashFromInvestingActivities(BigDecimal netCashFromInvestingActivities) {
        this.netCashFromInvestingActivities = netCashFromInvestingActivities;
    }

    public BigDecimal getFinancingCapitalInflow() {
        return financingCapitalInflow;
    }

    public void setFinancingCapitalInflow(BigDecimal financingCapitalInflow) {
        this.financingCapitalInflow = financingCapitalInflow;
    }

    public BigDecimal getFinancingCapitalOutflow() {
        return financingCapitalOutflow;
    }

    public void setFinancingCapitalOutflow(BigDecimal financingCapitalOutflow) {
        this.financingCapitalOutflow = financingCapitalOutflow;
    }


}
