com.nec.harvest.service.impl.ProfitByShopServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.nec.harvest.service.impl.ProfitByShopServiceImpl.java

Source

/*
 * Copyright(C) 2014
 * NEC Corporation All rights reserved.
 * 
 * No permission to use, copy, modify and distribute this software
 * and its documentation for any purpose is granted.
 * This software is provided under applicable license agreement only.
 */
package com.nec.harvest.service.impl;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.nec.core.exception.ObjectNotFoundException;
import com.nec.harvest.bean.mapping.ProfitMonthlyBean;
import com.nec.harvest.constant.Constants;
import com.nec.harvest.exception.ServiceException;
import com.nec.harvest.model.BudgetPerformance;
import com.nec.harvest.model.Tighten;
import com.nec.harvest.model.VJiseki;
import com.nec.harvest.service.ActualViewService;
import com.nec.harvest.service.BudgetPerformanceService;
import com.nec.harvest.service.ConsumptionTaxRateService;
import com.nec.harvest.service.InventoryService;
import com.nec.harvest.service.ProfitByShopService;
import com.nec.harvest.service.TightenService;
import com.nec.harvest.util.DateFormatUtil;
import com.nec.harvest.util.DateFormatUtil.DateFormat;
import com.nec.harvest.util.DateUtil;
import com.nec.harvest.util.RoundNumericUtil;

/**
 * {@link ProfitByShopService}
 * 
 * @author huonghv
 *
 */
public class ProfitByShopServiceImpl implements ProfitByShopService {

    private static final Logger logger = LoggerFactory.getLogger(ProfitByShopServiceImpl.class);

    private BudgetPerformanceService budgetPerformanceService;
    private InventoryService inventoryService;
    private TightenService tightenService;
    private ActualViewService actualViewService;
    private ConsumptionTaxRateService consumptionTaxRateService;

    private String[] kmkCodeJs = { Constants.DEFAULT_KMKCODEJ_K7111, Constants.DEFAULT_KMKCODEJ_K7521,
            Constants.DEFAULT_KMKCODEJ_K8110, Constants.DEFAULT_KMKCODEJ_K8210, Constants.DEFAULT_KMKCODEJ_K8310 };

    public ProfitByShopServiceImpl(BudgetPerformanceService budgetPerformanceService,
            InventoryService inventoryService, TightenService tightenService, ActualViewService actualViewService,
            ConsumptionTaxRateService consumptionTaxRateService) {
        this.budgetPerformanceService = budgetPerformanceService;
        this.inventoryService = inventoryService;
        this.tightenService = tightenService;
        this.actualViewService = actualViewService;
        this.consumptionTaxRateService = consumptionTaxRateService;
    }

    /** {@inheritDoc} */
    @Override
    public ProfitMonthlyBean aggregateByOrgCodeAndMonthly(String orgCode, Date monthly, Date businessDay)
            throws ServiceException {
        logger.info(
                "Starting aggregate profit by shop with given organization code, current month and bussiness day.");

        if (StringUtils.isEmpty(orgCode)) {
            throw new IllegalArgumentException("Aggregation profit by shop: Organization code must not be empty");
        }

        if (monthly == null) {
            throw new IllegalArgumentException("Aggregation profit by shop: monthly must not be null");
        }

        if (businessDay == null) {
            throw new IllegalArgumentException("Aggregation profit by shop: business day must not be null");
        }

        /* Get jiseki object by organization code and monthly */
        String strMonthly = DateFormatUtil.format(monthly, DateFormat.DATE_WITHOUT_DAY);

        VJiseki jisekis = null;
        try {
            jisekis = actualViewService.findByOrgCodeAndMonthly(orgCode, strMonthly); // [A]
        } catch (ObjectNotFoundException ex) {
            logger.warn(ex.getMessage());
            //
            jisekis = null;
        }

        // Get budget performance informations
        Date previousYear = DateUtil.monthsToSubtract(monthly, 12);
        String strPreviousYear = DateFormatUtil.format(previousYear, DateFormat.DATE_WITHOUT_DAY);

        Map<String, BudgetPerformance> mapPreviousYear = new HashMap<String, BudgetPerformance>();
        Map<String, BudgetPerformance> mapCurrentYear = new HashMap<String, BudgetPerformance>();

        try {
            mapPreviousYear = budgetPerformanceService.findByOrgCodeAndMonthAndKmkCodeJs(orgCode, strPreviousYear,
                    kmkCodeJs); // [B1]
        } catch (IllegalArgumentException ex) {
            logger.warn(ex.getMessage());
            //
            mapPreviousYear = null;
        }

        try {
            mapCurrentYear = budgetPerformanceService.findByOrgCodeAndMonthAndKmkCodeJs(orgCode, strMonthly,
                    kmkCodeJs); // [B2]
        } catch (IllegalArgumentException ex) {
            logger.warn(ex.getMessage());
            //
            mapCurrentYear = null;
        }

        // Inventory price previous month for No13
        Date previousMonth = DateUtil.monthsToSubtract(monthly, 1); // [C]
        Double inventoryPricePreviousMonth = getInventory(orgCode, previousMonth);

        // Inventory price current month for No22
        Double inventoryPriceCurrentMonth = getInventory(orgCode, monthly); // [D]

        /* Get and determine the mode to create the object ProfitByShopBean */
        Date tightenMonth = getTightenBusinessDay(businessDay);

        logger.info("current date " + monthly + " is " + (monthly.after(tightenMonth) ? " future " : " present ")
                + " of " + tightenMonth);

        // Get tax rate of current month
        double taxRate = 0;
        try {
            taxRate = consumptionTaxRateService.findActualTaxRateByDate(monthly);
            // taxRate = consumptionTaxRateService.findTaxRateByDate(monthly);
            // // old version, update 23/07/2014
        } catch (ObjectNotFoundException | IllegalArgumentException ex) {
            logger.warn(ex.getMessage());
            //
            taxRate = 0;
        }

        int restDay = DateUtil.getNumberOfDays(monthly, businessDay);
        int daysInMonth = DateUtil.getActualMaximumOfMonth(businessDay);

        logger.info(
                "Finished aggregation profit by shop with given organization code, current month and bussiness day.");
        //
        return calculateProfitMonthly(jisekis, mapPreviousYear, mapCurrentYear, inventoryPricePreviousMonth,
                inventoryPriceCurrentMonth, (tightenMonth == null ? true : !(monthly.after(tightenMonth))), taxRate,
                restDay, daysInMonth);
    }

    /**
     * 
     * @param jiseki
     * @param mapPreviousYearBudget
     * @param mapCurrentYearBudget
     * @param inventoryPricePreviousMonth
     * @param inventoryPriceCurrentMonth
     * @param mode
     * @param taxRate
     * @param restDay
     * @param daysInMonth
     * @return
     */
    private ProfitMonthlyBean calculateProfitMonthly(VJiseki jiseki,
            Map<String, BudgetPerformance> mapPreviousYearBudget,
            Map<String, BudgetPerformance> mapCurrentYearBudget, Double inventoryPricePreviousMonth,
            Double inventoryPriceCurrentMonth, boolean mode, double taxRate, int restDay, int daysInMonth) {

        final int HUNDRED = 100;
        final int THOUSAND = 1000;

        ProfitMonthlyBean profitMonthlyBean = new ProfitMonthlyBean();
        if (jiseki == null && mapPreviousYearBudget == null && mapCurrentYearBudget == null
                && inventoryPricePreviousMonth == null && inventoryPriceCurrentMonth == null) {
            return profitMonthlyBean;
        }

        //
        Double tmp = null;

        //      // No10 (No6)
        //      if (!mode && jiseki != null) {
        //         Double uriSkKG = jiseki.getUriSkKG();
        //         if (uriSkKG != null) {
        //            tmp = Math.floor((uriSkKG - Math
        //                  .floor((uriSkKG / (HUNDRED + taxRate)) * taxRate))
        //                  / THOUSAND); // integer
        //            tmp = (tmp == 0.0d ? null : tmp);
        //         }
        //      }
        //      profitMonthlyBean.setCurrentFoodIncome(tmp);

        // Modified by SONDN: Update SPEC 20141222
        // No10 (No6)
        if (!mode && jiseki != null) {
            Double uriSkKG = jiseki.getUriSkKG();
            Double ktSkKG = jiseki.getKtSkKG();
            Double idoSkKGU = jiseki.getIdoSkKGU();
            Double idoSkKGH = jiseki.getIdoSkKGH();
            if (uriSkKG != null || ktSkKG != null || idoSkKGU != null || idoSkKGH != null) {
                double step1 = uriSkKG == null ? 0
                        : uriSkKG - Math.floor((uriSkKG / (HUNDRED + taxRate)) * taxRate);
                double step2 = ktSkKG == null ? 0 : ktSkKG;
                if (restDay != -1) {
                    step2 = (step2 / daysInMonth) * restDay;
                }
                double step3 = idoSkKGU == null ? 0 : idoSkKGU;
                double step4 = idoSkKGH == null ? 0 : idoSkKGH;

                tmp = Math.floor((step1 + step2 + step3 - step4) / THOUSAND); // integer
                tmp = (tmp == 0.0d ? null : tmp);
            }
        }
        profitMonthlyBean.setCurrentFoodIncome(tmp);

        // No11 (No7)
        tmp = null;
        if (!mode && jiseki != null) {
            Double uriKrKG = jiseki.getUriKrKG();
            Double ktKrKG = jiseki.getKtKrKG();
            if (uriKrKG != null || ktKrKG != null) {
                double step1 = uriKrKG == null ? 0
                        : uriKrKG - Math.floor((uriKrKG / (HUNDRED + taxRate)) * taxRate);
                double step2 = ktKrKG == null ? 0 : ktKrKG;

                if (restDay != -1) {
                    step1 = (step1 / daysInMonth) * restDay;
                    step2 = (step2 / daysInMonth) * restDay;
                }

                tmp = Math.floor((step1 + step2) / THOUSAND); // integer
                tmp = (tmp == 0.0d ? null : tmp);
            }
        }
        profitMonthlyBean.setCurrentManageIncome(tmp);

        // No12 (No8)
        tmp = null;
        if (mapPreviousYearBudget != null && mapPreviousYearBudget.get(Constants.DEFAULT_KMKCODEJ_K7111) != null) {
            tmp = mapPreviousYearBudget.get(Constants.DEFAULT_KMKCODEJ_K7111).getJisekiKingaku();
            if (tmp != null) {
                // hungpd: amount tighten equals business day
                if (restDay != -1) {
                    tmp = (tmp / daysInMonth) * restDay;
                }
                // hungpd: end
                tmp = Math.floor(tmp / THOUSAND); // integer
                tmp = (tmp == 0.0d ? null : tmp);
            }
        }
        profitMonthlyBean.setPreviousYearIncomeTotal(tmp);

        // No13 (No9)
        tmp = null;
        if (mapCurrentYearBudget != null && mapCurrentYearBudget.get(Constants.DEFAULT_KMKCODEJ_K7111) != null) {
            tmp = mapCurrentYearBudget.get(Constants.DEFAULT_KMKCODEJ_K7111).getYosanKingaku();
            if (tmp != null) {
                // hungpd: amount tighten equals business day
                if (restDay != -1) {
                    tmp = (tmp / daysInMonth) * restDay;
                }
                // hungpd: end
                tmp = Math.floor(tmp / THOUSAND); // integer
                tmp = (tmp == 0.0d ? null : tmp);
            }
        }
        profitMonthlyBean.setExpectedYearIncomeTotal(tmp);

        // No14 (No10)
        tmp = null;
        if (mode && mapCurrentYearBudget != null
                && mapCurrentYearBudget.get(Constants.DEFAULT_KMKCODEJ_K7111) != null) {
            tmp = mapCurrentYearBudget.get(Constants.DEFAULT_KMKCODEJ_K7111).getJisekiKingaku();
            if (tmp != null) {
                Math.floor(tmp = tmp / THOUSAND); // integer
                tmp = (tmp == 0.0d ? null : tmp);
            }
        } else if (profitMonthlyBean.getCurrentFoodIncome() != null
                || profitMonthlyBean.getCurrentManageIncome() != null) { // check
            // No10
            // or
            // No11
            // not
            // null
            // No10 + No11;
            tmp = (profitMonthlyBean.getCurrentFoodIncome() == null ? 0 : profitMonthlyBean.getCurrentFoodIncome())
                    + (profitMonthlyBean.getCurrentManageIncome() == null ? 0
                            : profitMonthlyBean.getCurrentManageIncome());
            tmp = (tmp == 0.0d ? null : tmp);
        }
        profitMonthlyBean.setCurrentYearIncomeTotal(tmp);

        // No15 (No11)
        tmp = null;
        if (profitMonthlyBean.getCurrentYearIncomeTotal() != null
                && profitMonthlyBean.getPreviousYearIncomeTotal() != null
                && profitMonthlyBean.getPreviousYearIncomeTotal() != 0) { // check
            // No12
            // not
            // null
            // or
            // dif
            // 0
            // No14 / No12;
            tmp = RoundNumericUtil.round(
                    (profitMonthlyBean.getCurrentYearIncomeTotal() / profitMonthlyBean.getPreviousYearIncomeTotal())
                            * HUNDRED,
                    1);
            tmp = (tmp == 0.0d ? null : tmp);
        }
        profitMonthlyBean.setTotalPreviousYearIncomeRatio(tmp);

        // No16 (No12)
        tmp = null;
        if (profitMonthlyBean.getCurrentYearIncomeTotal() != null
                && profitMonthlyBean.getExpectedYearIncomeTotal() != null
                && profitMonthlyBean.getExpectedYearIncomeTotal() != 0) { // check
            // No13
            // not
            // null
            // or
            // dif
            // 0
            // No14 / No13;
            tmp = RoundNumericUtil.round(
                    (profitMonthlyBean.getCurrentYearIncomeTotal() / profitMonthlyBean.getExpectedYearIncomeTotal())
                            * HUNDRED,
                    1);
            tmp = (tmp == 0.0d ? null : tmp);
        }
        profitMonthlyBean.setTotalExpectedYearIncomeRatio(tmp);

        // No18 (No14) = No27
        tmp = null;
        if (mapPreviousYearBudget != null && mapPreviousYearBudget.get(Constants.DEFAULT_KMKCODEJ_K7521) != null) {
            tmp = mapPreviousYearBudget.get(Constants.DEFAULT_KMKCODEJ_K7521).getJisekiKingaku();
            if (tmp != null) {
                // hungpd: amount tighten equals business day
                if (restDay != -1) {
                    tmp = (tmp / daysInMonth) * restDay;
                }
                // hungpd: end
                tmp = Math.floor(tmp / THOUSAND); // integer
                tmp = (tmp == 0.0d ? null : tmp);
            }
        }
        profitMonthlyBean.setPreviousYearCostPrice(tmp);
        profitMonthlyBean.setPreviousYearCostPriceTotal(tmp); // No27

        // No19 (No15)
        tmp = null;
        profitMonthlyBean.setPreviousYearCostPriceRatio(tmp);

        // No20 (No16) = No29
        tmp = null;
        if (mapCurrentYearBudget != null && mapCurrentYearBudget.get(Constants.DEFAULT_KMKCODEJ_K7521) != null) {
            tmp = mapCurrentYearBudget.get(Constants.DEFAULT_KMKCODEJ_K7521).getYosanKingaku();
            tmp = (tmp == 0.0d ? null : tmp);
            if (tmp != null) {
                // hungpd: amount tighten equals business day
                if (restDay != -1) {
                    tmp = (tmp / daysInMonth) * restDay;
                }
                // hungpd: end
                tmp = Math.floor(tmp / THOUSAND); // integer
                tmp = (tmp == 0.0d ? null : tmp);
            }
        }
        profitMonthlyBean.setExpectedCostPrice(tmp);
        profitMonthlyBean.setExpectedCostPriceTotal(tmp); // No29

        // No21 (No17)
        tmp = null;
        profitMonthlyBean.setExpectedCostPriceRatio(tmp);

        // No22 (No18)
        tmp = null;
        if (mode && mapCurrentYearBudget != null
                && mapCurrentYearBudget.get(Constants.DEFAULT_KMKCODEJ_K7521) != null) {
            tmp = mapCurrentYearBudget.get(Constants.DEFAULT_KMKCODEJ_K7521).getJisekiKingaku();
            tmp = (tmp == null ? 0 : Math.floor(tmp / THOUSAND)); // integer
            tmp = (tmp == 0.0d ? null : tmp);
        } else if (jiseki != null) {
            if (jiseki.getKtSrKG() != null || jiseki.getKgcSrKG() != null || jiseki.getKgcSrKG() != null
                    || jiseki.getKnSrKG() != null || jiseki.getIdoSrKGU() != null || jiseki.getIdoSrKGH() != null) {
                double step1 = (jiseki.getKtSrKG() == null ? 0 : jiseki.getKtSrKG());
                if (restDay != -1) {
                    step1 = (step1 / daysInMonth) * restDay;
                }

                double step2 = (jiseki.getKgcSrKG() == null ? 0
                        : (jiseki.getKgcSrKG()
                                - Math.floor((jiseki.getKgcSrKG() / (HUNDRED + taxRate)) * taxRate)));

                tmp = Math.floor(((jiseki.getKnSrKG() == null ? 0 : jiseki.getKnSrKG()) + step1 + step2
                        + (jiseki.getIdoSrKGU() == null ? 0 : jiseki.getIdoSrKGU())
                        - (jiseki.getIdoSrKGH() == null ? 0 : jiseki.getIdoSrKGH())) / THOUSAND); // Integer
                tmp = (tmp == 0.0d ? null : tmp);
            }
        }
        profitMonthlyBean.setCurrentCostPrice(tmp);

        // No23 (No19)
        tmp = null;
        if (!mode && profitMonthlyBean.getCurrentCostPrice() != null
                && profitMonthlyBean.getCurrentFoodIncome() != null
                && profitMonthlyBean.getCurrentFoodIncome() != 0) { // check
            // No14 not
            // null or
            // dif 0
            // No22 / No10;
            tmp = RoundNumericUtil.round(
                    (profitMonthlyBean.getCurrentCostPrice() / profitMonthlyBean.getCurrentFoodIncome()) * HUNDRED,
                    1);
            tmp = (tmp == 0.0d ? null : tmp);
        }
        profitMonthlyBean.setCurrentCostPriceRatio(tmp);

        // No24 (No20)
        tmp = null;
        if (profitMonthlyBean.getCurrentCostPrice() != null
                && profitMonthlyBean.getPreviousYearCostPriceTotal() != null
                && profitMonthlyBean.getPreviousYearCostPriceTotal() != 0) { // check
            // No27
            // not
            // null
            // or
            // dif
            // 0
            // No22 / No27;
            tmp = RoundNumericUtil.round(
                    (profitMonthlyBean.getCurrentCostPrice() / profitMonthlyBean.getPreviousYearCostPriceTotal())
                            * HUNDRED,
                    1);
            tmp = (tmp == 0.0d ? null : tmp);
        }
        profitMonthlyBean.setPreviousCostPriceRatioTotal(tmp);

        // No25 (No21)
        tmp = null;
        if ((profitMonthlyBean.getCurrentCostPrice() != null)
                && ((profitMonthlyBean.getExpectedCostPriceTotal() != null)
                        && (profitMonthlyBean.getExpectedCostPriceTotal() != 0))) { // check No29 not
            // null or dif 0
            // No22 / No29;
            tmp = RoundNumericUtil
                    .round((profitMonthlyBean.getCurrentCostPrice() / profitMonthlyBean.getExpectedCostPriceTotal())
                            * HUNDRED, 1);
            tmp = (tmp == 0.0d ? null : tmp);
        }
        profitMonthlyBean.setTotalExpectedCostPriceRatio(tmp);

        // No26 (No22)
        tmp = null;
        if (!mode && inventoryPriceCurrentMonth != null) {
            tmp = inventoryPriceCurrentMonth / THOUSAND; // integer
            tmp = (tmp == 0.0d ? null : Double.parseDouble(String.valueOf(tmp.intValue())));
        }
        profitMonthlyBean.setCurrentInventoryCost(tmp);

        // No17 (No13)
        tmp = null;
        if (mode || profitMonthlyBean.getCurrentInventoryCost() == null || inventoryPricePreviousMonth == null) {
            tmp = null;
        } else {
            // update SPEC 01/09/2014
            if (restDay != -1 && (inventoryPriceCurrentMonth != null && inventoryPriceCurrentMonth == 0.0d)) {
                tmp = null;
            } else {
                tmp = inventoryPricePreviousMonth / 1000;
                tmp = (tmp == 0.0d ? null : Double.parseDouble(String.valueOf(tmp.intValue())));
            } // end

        }
        profitMonthlyBean.setPreviousInventoryCost(tmp);

        // No28 (No24)
        tmp = null;
        if (profitMonthlyBean.getPreviousYearCostPriceTotal() != null
                && profitMonthlyBean.getPreviousYearIncomeTotal() != null
                && profitMonthlyBean.getPreviousYearIncomeTotal() != 0) { // check
            // No12
            // not
            // null
            // or
            // dif
            // 0
            // No27 / No12;
            tmp = RoundNumericUtil.round((profitMonthlyBean.getPreviousYearCostPriceTotal()
                    / profitMonthlyBean.getPreviousYearIncomeTotal()) * HUNDRED, 1);
            tmp = (tmp == 0.0d ? null : tmp);
        }
        profitMonthlyBean.setPreviousYearCostPriceRatioTotal(tmp);

        // No30 (No26)
        tmp = null;
        if ((profitMonthlyBean.getExpectedCostPriceTotal() != null)
                && ((profitMonthlyBean.getExpectedYearIncomeTotal() != null)
                        && (profitMonthlyBean.getExpectedYearIncomeTotal() != 0))) { // check No13
            // not null or
            // dif 0
            // No29 / No13;
            tmp = RoundNumericUtil.round(
                    (profitMonthlyBean.getExpectedCostPriceTotal() / profitMonthlyBean.getExpectedYearIncomeTotal())
                            * HUNDRED,
                    1);

            tmp = (tmp == 0.0d ? null : tmp);
        }
        profitMonthlyBean.setExpectedCostPriceRatioTotal(tmp);

        // No31 (No27) = No17 + No22 - No26
        tmp = null;
        if (profitMonthlyBean.getPreviousInventoryCost() != null || profitMonthlyBean.getCurrentCostPrice() != null
                || profitMonthlyBean.getCurrentInventoryCost() != null) {
            tmp = (profitMonthlyBean.getPreviousInventoryCost() == null ? 0
                    : profitMonthlyBean.getPreviousInventoryCost())
                    + (profitMonthlyBean.getCurrentCostPrice() == null ? 0
                            : profitMonthlyBean.getCurrentCostPrice())
                    - (profitMonthlyBean.getCurrentInventoryCost() == null ? 0
                            : profitMonthlyBean.getCurrentInventoryCost());
            tmp = (tmp == 0.0d ? null : tmp);
        }
        profitMonthlyBean.setCurrentCostPriceTotal(tmp);

        // No32 (No28)
        tmp = null;
        if (profitMonthlyBean.getCurrentCostPriceTotal() != null
                && profitMonthlyBean.getCurrentYearIncomeTotal() != null
                && profitMonthlyBean.getCurrentYearIncomeTotal() != 0) { // check
            // No14
            // not
            // null
            // or
            // dif
            // 0
            // No31 / No14;
            tmp = RoundNumericUtil.round(
                    (profitMonthlyBean.getCurrentCostPriceTotal() / profitMonthlyBean.getCurrentYearIncomeTotal())
                            * HUNDRED,
                    1);
            tmp = (tmp == 0.0d ? null : tmp);
        }
        profitMonthlyBean.setCurrentCostPriceRatioTotal(tmp);

        // No33 (No29)
        tmp = null;
        if (profitMonthlyBean.getCurrentCostPriceTotal() != null
                && profitMonthlyBean.getPreviousYearCostPriceTotal() != null
                && profitMonthlyBean.getPreviousYearCostPriceTotal() != 0) { // check
            // No27
            // not
            // null
            // or
            // dif
            // 0
            // No31 / No27;
            tmp = RoundNumericUtil.round((profitMonthlyBean.getCurrentCostPriceTotal()
                    / profitMonthlyBean.getPreviousYearCostPriceTotal()) * HUNDRED, 1);
            tmp = (tmp == 0.0d ? null : tmp);
        }
        profitMonthlyBean.setTotalCostPriceRatio(tmp);

        // No34 (No30)
        tmp = null;
        if (profitMonthlyBean.getCurrentCostPriceTotal() != null
                && profitMonthlyBean.getExpectedCostPriceTotal() != null
                && profitMonthlyBean.getExpectedCostPriceTotal() != 0) { // check
            // No27
            // not
            // null
            // or
            // dif
            // 0
            // No31 / No29;
            tmp = RoundNumericUtil.round(
                    (profitMonthlyBean.getCurrentCostPriceTotal() / profitMonthlyBean.getExpectedCostPriceTotal())
                            * HUNDRED,
                    1);
            tmp = (tmp == 0.0d ? null : tmp);
        }
        profitMonthlyBean.setTotalExpCostPriceRatio(tmp);

        // No35 (No31)
        tmp = null;
        if (mapPreviousYearBudget != null && mapPreviousYearBudget.get(Constants.DEFAULT_KMKCODEJ_K8110) != null) {
            tmp = mapPreviousYearBudget.get(Constants.DEFAULT_KMKCODEJ_K8110).getJisekiKingaku();
            if (tmp != null) {
                // hungpd: amount tighten equals business day
                if (restDay != -1) {
                    tmp = (tmp / daysInMonth) * restDay;
                }
                // hungpd: end
                tmp = Math.floor(tmp / THOUSAND); // integer
                tmp = (tmp == 0.0d ? null : tmp);
            }
        }
        profitMonthlyBean.setPreviousYearHumanCost(tmp);

        // No36 (No32)
        tmp = null;
        if (profitMonthlyBean.getPreviousYearHumanCost() != null
                && profitMonthlyBean.getPreviousYearIncomeTotal() != null
                && profitMonthlyBean.getPreviousYearIncomeTotal() != 0) { // check
            // No12
            // not
            // null
            // or
            // dif
            // 0
            // No35 / No12;
            tmp = RoundNumericUtil.round(
                    (profitMonthlyBean.getPreviousYearHumanCost() / profitMonthlyBean.getPreviousYearIncomeTotal())
                            * HUNDRED,
                    1);
            tmp = (tmp == 0.0d ? null : tmp);
        }
        profitMonthlyBean.setPreviousYearHumanCostRatio(tmp);

        // No37 (No33)
        tmp = null;
        if (mapCurrentYearBudget != null && mapCurrentYearBudget.get(Constants.DEFAULT_KMKCODEJ_K8110) != null) {
            tmp = mapCurrentYearBudget.get(Constants.DEFAULT_KMKCODEJ_K8110).getYosanKingaku();
            if (tmp != null) {
                // hungpd: amount tighten equals business day
                if (restDay != -1) {
                    tmp = (tmp / daysInMonth) * restDay;
                }
                // hungpd: end
                tmp = Math.floor(tmp / THOUSAND); // integer
                tmp = (tmp == 0.0d ? null : tmp);
            }
        }
        profitMonthlyBean.setExpectedHumanCost(tmp);

        // No38 (No34)
        tmp = null;
        if (profitMonthlyBean.getExpectedHumanCost() != null
                && profitMonthlyBean.getExpectedYearIncomeTotal() != null
                && profitMonthlyBean.getExpectedYearIncomeTotal() != 0) { // check
            // No13
            // not
            // null
            // or
            // dif
            // 0
            // No37 / No13;
            tmp = RoundNumericUtil.round(
                    (profitMonthlyBean.getExpectedHumanCost() / profitMonthlyBean.getExpectedYearIncomeTotal())
                            * HUNDRED,
                    1);
            tmp = (tmp == 0.0d ? null : tmp);
        }
        profitMonthlyBean.setExpectedHumanCostRatio(tmp);

        // No39 (No35)
        tmp = null;
        if (mode && mapCurrentYearBudget != null
                && mapCurrentYearBudget.get(Constants.DEFAULT_KMKCODEJ_K8110) != null) {
            tmp = mapCurrentYearBudget.get(Constants.DEFAULT_KMKCODEJ_K8110).getJisekiKingaku();
            if (tmp != null) {
                tmp = Math.floor(tmp / THOUSAND); // integer
                tmp = (tmp == 0.0d ? null : tmp);
            }
        } else if (jiseki != null) {
            if (jiseki.getKtJkKG() != null || jiseki.getKgcJkKG() != null || jiseki.getJkJkKG() != null
                    || jiseki.getIdoJkKGU() != null || jiseki.getIdoJkKGH() != null || jiseki.getHelpJkKGU() != null
                    || jiseki.getHelpJkKGH() != null) {
                double step1 = (jiseki.getKtJkKG() == null ? 0 : jiseki.getKtJkKG());
                if (restDay != -1) {
                    step1 = (step1 / daysInMonth) * restDay;
                }

                double step2 = (jiseki.getKgcJkKG() == null ? 0
                        : (jiseki.getKgcJkKG()
                                - Math.floor((jiseki.getKgcJkKG() / (HUNDRED + taxRate)) * taxRate)));

                tmp = Math.floor(((jiseki.getJkJkKG() == null ? 0 : jiseki.getJkJkKG()) + step1 + step2
                        + (jiseki.getIdoJkKGU() == null ? 0 : jiseki.getIdoJkKGU())
                        - (jiseki.getIdoJkKGH() == null ? 0 : jiseki.getIdoJkKGH())
                        + (jiseki.getHelpJkKGU() == null ? 0 : jiseki.getHelpJkKGU())
                        - (jiseki.getHelpJkKGH() == null ? 0 : jiseki.getHelpJkKGH())) / THOUSAND); // Integer
                tmp = (tmp == 0.0d ? null : tmp);
            }
        }
        profitMonthlyBean.setCurrentHumanCost(tmp);

        // No40 (No36)
        tmp = null;
        if (profitMonthlyBean.getCurrentHumanCost() != null && profitMonthlyBean.getCurrentYearIncomeTotal() != null
                && profitMonthlyBean.getCurrentYearIncomeTotal() != 0) { // check
            // No14
            // not
            // null
            // or
            // dif
            // 0
            // No39 / No14;
            tmp = RoundNumericUtil
                    .round((profitMonthlyBean.getCurrentHumanCost() / profitMonthlyBean.getCurrentYearIncomeTotal())
                            * HUNDRED, 1);
            tmp = (tmp == 0.0d ? null : tmp);
        }
        profitMonthlyBean.setCurrentHumanCostRatio(tmp);

        // No41 (No37)
        tmp = null;
        if (profitMonthlyBean.getCurrentHumanCost() != null && profitMonthlyBean.getPreviousYearHumanCost() != null
                && profitMonthlyBean.getPreviousYearHumanCost() != 0) { // check
            // No35
            // not
            // null
            // or
            // dif 0
            // No39 / No35;
            tmp = RoundNumericUtil
                    .round((profitMonthlyBean.getCurrentHumanCost() / profitMonthlyBean.getPreviousYearHumanCost())
                            * HUNDRED, 1);
            tmp = (tmp == 0.0d ? null : tmp);
        }
        profitMonthlyBean.setTotalPreviousYearHumanCostRatio(tmp);

        // No42 (No38)
        tmp = null;
        if (profitMonthlyBean.getCurrentHumanCost() != null && profitMonthlyBean.getExpectedHumanCost() != null
                && profitMonthlyBean.getExpectedHumanCost() != 0) { // check
            // No37 not
            // null or
            // dif 0
            // No39 / No37;
            tmp = RoundNumericUtil.round(
                    (profitMonthlyBean.getCurrentHumanCost() / profitMonthlyBean.getExpectedHumanCost()) * HUNDRED,
                    1);
            tmp = (tmp == 0.0d ? null : tmp);
        }
        profitMonthlyBean.setTotalExpectedYearHumanCostRatio(tmp);

        // No43 (No39)
        tmp = null;
        if (mapPreviousYearBudget != null && mapPreviousYearBudget.get(Constants.DEFAULT_KMKCODEJ_K8210) != null) {
            tmp = mapPreviousYearBudget.get(Constants.DEFAULT_KMKCODEJ_K8210).getJisekiKingaku();
            if (tmp != null) {
                // hungpd: amount tighten equals business day
                if (restDay != -1) {
                    tmp = (tmp / daysInMonth) * restDay;
                }
                // hungpd: end
                tmp = Math.floor(tmp / THOUSAND); // integer
                tmp = (tmp == 0.0d ? null : tmp);
            }
        }
        profitMonthlyBean.setPreviousYearBudget(tmp);

        // No44 (No40)
        tmp = null;
        if (profitMonthlyBean.getPreviousYearBudget() != null
                && profitMonthlyBean.getPreviousYearIncomeTotal() != null
                && profitMonthlyBean.getPreviousYearIncomeTotal() != 0) { // check
            // No12
            // not
            // null
            // or
            // dif
            // 0
            // No43 / No12;
            tmp = RoundNumericUtil.round(
                    (profitMonthlyBean.getPreviousYearBudget() / profitMonthlyBean.getPreviousYearIncomeTotal())
                            * HUNDRED,
                    1);
            tmp = (tmp == 0.0d ? null : tmp);
        }
        profitMonthlyBean.setPreviousYearBudgetRatio(tmp);

        // No45 (No41)
        tmp = null;
        if (mapCurrentYearBudget != null && mapCurrentYearBudget.get(Constants.DEFAULT_KMKCODEJ_K8210) != null) {
            tmp = mapCurrentYearBudget.get(Constants.DEFAULT_KMKCODEJ_K8210).getYosanKingaku();
            if (tmp != null) {
                // hungpd: amount tighten equals business day
                if (restDay != -1) {
                    tmp = (tmp / daysInMonth) * restDay;
                }
                // hungpd: end
                tmp = Math.floor(tmp / THOUSAND); // integer            
                tmp = (tmp == 0.0d ? null : tmp);
            }
        }
        profitMonthlyBean.setExptectedBudget(tmp);

        // No46 (No42)
        tmp = null;
        if (profitMonthlyBean.getExptectedBudget() != null && profitMonthlyBean.getExpectedYearIncomeTotal() != null
                && profitMonthlyBean.getExpectedYearIncomeTotal() != 0) { // check
            // No13
            // not
            // null
            // or
            // dif
            // 0
            // No45 / No13;
            tmp = RoundNumericUtil
                    .round((profitMonthlyBean.getExptectedBudget() / profitMonthlyBean.getExpectedYearIncomeTotal())
                            * HUNDRED, 1);
            tmp = (tmp == 0.0d ? null : tmp);
        }
        profitMonthlyBean.setExptectedBudgetRatio(tmp);

        // No47 (No43)
        tmp = null;
        if (mode && mapCurrentYearBudget != null
                && mapCurrentYearBudget.get(Constants.DEFAULT_KMKCODEJ_K8210) != null) {
            tmp = mapCurrentYearBudget.get(Constants.DEFAULT_KMKCODEJ_K8210).getJisekiKingaku();
            if (tmp != null) {
                // hungpd: amount tighten equals business day
                if (restDay != -1) {
                    tmp = (tmp / daysInMonth) * restDay;
                }
                // hungpd: end
                tmp = Math.floor(tmp / THOUSAND);
                tmp = (tmp == 0.0d ? null : tmp);
            }
        } else if (jiseki != null) {
            if (jiseki.getKtKhKG() != null || jiseki.getKgcKhKG() != null || jiseki.getUriKhKG() != null
                    || jiseki.getKnKhKG() != null || jiseki.getIdoKhKGH() != null || jiseki.getIdoKhKGU() != null) {
                double step1 = (jiseki.getKtKhKG() == null ? 0 : jiseki.getKtKhKG());

                double step2 = (jiseki.getKgcKhKG() == null ? 0
                        : (jiseki.getKgcKhKG()
                                - Math.floor((jiseki.getKgcKhKG() / (HUNDRED + taxRate)) * taxRate)));

                double step3 = (jiseki.getUriKhKG() == null ? 0
                        : (jiseki.getUriKhKG()
                                - Math.floor((jiseki.getUriKhKG() / (HUNDRED + taxRate)) * taxRate)));

                if (restDay != -1) {
                    step1 = (step1 / daysInMonth) * restDay;
                    step3 = (step3 / daysInMonth) * restDay;
                }

                tmp = Math.floor((step1 + (jiseki.getKnKhKG() == null ? 0 : jiseki.getKnKhKG()) + step2 + step3
                        + (jiseki.getIdoKhKGU() == null ? 0 : jiseki.getIdoKhKGU())
                        - (jiseki.getIdoKhKGH() == null ? 0 : jiseki.getIdoKhKGH())) / THOUSAND);
                tmp = (tmp == 0.0d ? null : tmp);
            }
        }
        profitMonthlyBean.setCurrentBudget(tmp);

        // No 48 (No44)
        tmp = null;
        if (profitMonthlyBean.getCurrentBudget() != null && profitMonthlyBean.getCurrentYearIncomeTotal() != null
                && profitMonthlyBean.getCurrentYearIncomeTotal() != 0) { // check
            // No14
            // not
            // null
            // or
            // dif
            // 0
            // No47 / No14;
            tmp = RoundNumericUtil
                    .round((profitMonthlyBean.getCurrentBudget() / profitMonthlyBean.getCurrentYearIncomeTotal())
                            * HUNDRED, 1);
            tmp = (tmp == 0.0d ? null : tmp);
        }
        profitMonthlyBean.setCurrentBudgetRatio(tmp);

        // No49 (No45)
        tmp = null;
        if ((profitMonthlyBean.getCurrentBudget() != null) && ((profitMonthlyBean.getPreviousYearBudget() != null)
                && (profitMonthlyBean.getPreviousYearBudget() != 0))) { // check No43 not
            // null or dif 0
            // No47 / No43;
            tmp = RoundNumericUtil.round(
                    (profitMonthlyBean.getCurrentBudget() / profitMonthlyBean.getPreviousYearBudget()) * HUNDRED,
                    1);
            tmp = (tmp == 0.0d ? null : tmp);
        }
        profitMonthlyBean.setTotalPreviousYearBudgetRatio(tmp);

        // No50 (No46)
        tmp = null;
        if (profitMonthlyBean.getCurrentBudget() != null && profitMonthlyBean.getExptectedBudget() != null
                && profitMonthlyBean.getExptectedBudget() != 0) { // check No45
            // not null
            // or dif 0
            // No47 / No45;
            tmp = RoundNumericUtil.round(
                    (profitMonthlyBean.getCurrentBudget() / profitMonthlyBean.getExptectedBudget()) * HUNDRED, 1);
            tmp = (tmp == 0.0d ? null : tmp);
        }
        profitMonthlyBean.setTotalExpectedBudgetRatio(tmp);

        // No51 (No47)
        tmp = null;
        if (mapPreviousYearBudget != null && mapPreviousYearBudget.get(Constants.DEFAULT_KMKCODEJ_K8310) != null) {
            tmp = mapPreviousYearBudget.get(Constants.DEFAULT_KMKCODEJ_K8310).getJisekiKingaku();
            if (tmp != null) {
                // hungpd: amount tighten equals business day
                if (restDay != -1) {
                    tmp = (tmp / daysInMonth) * restDay;
                }
                // hungpd: end
                tmp = Math.floor(tmp / THOUSAND); // integer
                tmp = (tmp == 0.0d ? null : tmp);
            }
        }
        profitMonthlyBean.setPreviousYearPersonnelProfit(tmp);

        // No52 (No48)
        tmp = null;
        if (profitMonthlyBean.getPreviousYearPersonnelProfit() != null
                && profitMonthlyBean.getPreviousYearIncomeTotal() != null
                && profitMonthlyBean.getPreviousYearIncomeTotal() != 0) { // check
            // No12
            // not
            // null
            // or
            // dif
            // 0
            // No51 / No12;
            tmp = RoundNumericUtil.round((profitMonthlyBean.getPreviousYearPersonnelProfit()
                    / profitMonthlyBean.getPreviousYearIncomeTotal()) * HUNDRED, 1);
            tmp = (tmp == 0.0d ? null : tmp);
        }
        profitMonthlyBean.setPreviousYearPersonnelProfitRatio(tmp);

        // No53 (No49)
        tmp = null;
        if (mapCurrentYearBudget != null && mapCurrentYearBudget.get(Constants.DEFAULT_KMKCODEJ_K8310) != null) {
            tmp = mapCurrentYearBudget.get(Constants.DEFAULT_KMKCODEJ_K8310).getYosanKingaku();
            if (tmp != null) {
                // hungpd: amount tighten equals business day
                if (restDay != -1) {
                    tmp = (tmp / daysInMonth) * restDay;
                }
                // hungpd: end
                tmp = Math.floor(tmp / THOUSAND); // integer
                tmp = (tmp == 0.0d ? null : tmp);
            }
        }
        profitMonthlyBean.setExpectedPersonnelProfit(tmp);

        // No54 (No50)
        tmp = null;
        if (profitMonthlyBean.getExpectedPersonnelProfit() != null
                && profitMonthlyBean.getExpectedYearIncomeTotal() != null
                && profitMonthlyBean.getExpectedYearIncomeTotal() != 0) { // check
            // No13
            // not
            // null
            // or
            // dif
            // 0
            // No53 / No13;
            tmp = RoundNumericUtil.round((profitMonthlyBean.getExpectedPersonnelProfit()
                    / profitMonthlyBean.getExpectedYearIncomeTotal()) * HUNDRED, 1);
            tmp = (tmp == 0.0d ? null : tmp);
        }
        profitMonthlyBean.setExpectedPersonnelProfitRatio(tmp);

        // No55 (No51)
        tmp = null;
        if ((mode) && (mapCurrentYearBudget != null)
                && (mapCurrentYearBudget.get(Constants.DEFAULT_KMKCODEJ_K8310) != null)) {
            tmp = mapCurrentYearBudget.get(Constants.DEFAULT_KMKCODEJ_K8310).getJisekiKingaku();
            if (tmp != null) {
                tmp = Math.floor(tmp / THOUSAND); // integer
                tmp = (tmp == 0.0d ? null : tmp);
            }
        } else {
            // No55 = No14 - No31 - No39 - No47
            if (profitMonthlyBean.getCurrentYearIncomeTotal() != null
                    || profitMonthlyBean.getCurrentCostPriceTotal() != null
                    || profitMonthlyBean.getCurrentHumanCost() != null
                    || profitMonthlyBean.getCurrentBudget() != null) {

                tmp = (profitMonthlyBean.getCurrentYearIncomeTotal() == null ? 0
                        : profitMonthlyBean.getCurrentYearIncomeTotal())
                        - (profitMonthlyBean.getCurrentCostPriceTotal() == null ? 0
                                : profitMonthlyBean.getCurrentCostPriceTotal())
                        - (profitMonthlyBean.getCurrentHumanCost() == null ? 0
                                : profitMonthlyBean.getCurrentHumanCost())
                        - (profitMonthlyBean.getCurrentBudget() == null ? 0 : profitMonthlyBean.getCurrentBudget());
                tmp = (tmp == 0.0d ? null : tmp);
            }

        }
        profitMonthlyBean.setCurrentPersonnelProfit(tmp);

        // No56 (No52)
        tmp = null;
        if (profitMonthlyBean.getCurrentPersonnelProfit() != null
                && profitMonthlyBean.getCurrentYearIncomeTotal() != null
                && profitMonthlyBean.getCurrentYearIncomeTotal() != 0) {
            // No55 / No14;
            tmp = RoundNumericUtil.round(
                    (profitMonthlyBean.getCurrentPersonnelProfit() / profitMonthlyBean.getCurrentYearIncomeTotal())
                            * HUNDRED,
                    1);
            tmp = (tmp == 0.0d ? null : tmp);
        }
        profitMonthlyBean.setCurrentPersonnelProfitRatio(tmp);

        // No57 (No53)
        tmp = null;
        if (profitMonthlyBean.getCurrentPersonnelProfit() != null
                && profitMonthlyBean.getPreviousYearPersonnelProfit() != null
                && profitMonthlyBean.getPreviousYearPersonnelProfit() != 0) {
            // No55 / No51;
            tmp = RoundNumericUtil.round((profitMonthlyBean.getCurrentPersonnelProfit()
                    / profitMonthlyBean.getPreviousYearPersonnelProfit()) * HUNDRED, 1);
            tmp = (tmp == 0.0d ? null : tmp);
        }
        profitMonthlyBean.setTotalPreviousYearPersonnelProfitRatio(tmp);

        // No58 (No54)
        tmp = null;
        if (profitMonthlyBean.getCurrentPersonnelProfit() != null
                && profitMonthlyBean.getExpectedPersonnelProfit() != null
                && profitMonthlyBean.getExpectedPersonnelProfit() != 0) {
            // No55 / No53;
            tmp = RoundNumericUtil.round(
                    (profitMonthlyBean.getCurrentPersonnelProfit() / profitMonthlyBean.getExpectedPersonnelProfit())
                            * HUNDRED,
                    1);
            tmp = (tmp == 0.0d ? null : tmp);
        }
        profitMonthlyBean.setTotalExpectedPersonnelProfitRatio(tmp);
        return profitMonthlyBean;
    }

    /** {@inheritDoc} */
    @Override
    public Date checkAvailableByOrgCodeAndMothly(String orgCode, Date monthly, Date businessDay,
            boolean nextOrPrevious) throws ServiceException {
        logger.info("Check available next or previous month to aggregate profit by shop.");

        //
        if (StringUtils.isEmpty(orgCode)) {
            throw new IllegalArgumentException("Organization's code must not be null or empty");
        }

        if (monthly == null) {
            throw new IllegalArgumentException("Monthly must not be null");
        }

        if (businessDay == null) {
            throw new IllegalArgumentException("Business day must not be null");
        }

        // for the case processing month is the business one, disable the next
        // month
        String strBusinessMonth = DateFormatUtil.format(businessDay, DateFormat.DATE_WITHOUT_DAY);
        String strProcessingMonth = DateFormatUtil.format(monthly, DateFormat.DATE_WITHOUT_DAY);
        if (nextOrPrevious && strBusinessMonth.equals(strProcessingMonth)) {
            return null;
        }

        Date monthToCheck = nextOrPrevious ? DateUtil.monthsToAdd(monthly, 1)
                : DateUtil.monthsToSubtract(monthly, 1);
        String strMonthly = DateFormatUtil.format(monthToCheck, DateFormat.DATE_WITHOUT_DAY);
        Date tightenMonth = getTightenBusinessDay(businessDay);

        boolean isAvailable = monthToCheck.after(tightenMonth)
                ? actualViewService.checkAvailableByOrgCodeAndMonthly(orgCode, strMonthly)
                : budgetPerformanceService.checkAvailableByOrgCodeAndMonthly(orgCode, strMonthly);
        if (StringUtils.isNotEmpty(strMonthly) && isAvailable) {
            logger.info("Check available next or previous month to aggregate profit by shop have done.");
            //
            return monthToCheck;
        }

        logger.info("Check available next or previous month to aggregate profit by shop have done.");
        //
        return null;
    }

    /**
     * Get tighten business day
     * 
     * @param businessDay
     *            The business day
     * @return Tighten month
     */
    private Date getTightenBusinessDay(Date businessDay) {
        Date tightenMonth = null;
        try {
            Tighten tighten = tightenService.findByClassifyAndMonth(Constants.DEFAULT_KBN);
            tightenMonth = DateFormatUtil.parse(tighten.getGetSudo(), DateFormat.DATE_WITHOUT_DAY);
        } catch (ObjectNotFoundException ex) {
            //
            tightenMonth = DateUtil.monthsToSubtract(businessDay, 3);
        } catch (NullPointerException | IllegalArgumentException | ParseException ex) {
            //
            tightenMonth = null;
        }
        //
        return tightenMonth;
    }

    /**
     * Get shop's inventory of determine month
     * 
     * @param orgCode
     *            The organization (shop) code
     * @param month
     *            Month to get inventory
     * @return Inventory value return
     * @throws ServiceException
     *             Runtime error occur
     */
    private Double getInventory(String orgCode, Date month) throws ServiceException {
        try {
            String strMonth = DateFormatUtil.format(month, DateFormat.DATE_WITHOUT_DAY);
            //
            return inventoryService.calculateInventoryByOrgCodeAndMonthly(orgCode, strMonth);
        } catch (NullPointerException | IllegalArgumentException | ObjectNotFoundException ex) {
            return null;
        }
    }
}