private void processInventory() {
    LocalDate crc = SystemDao.getCrc();
    if (isStockedOut) {
      Demand d = getDemand(SystemDao.getCrc());
      if (crc.getDayOfWeek() == DateTimeConstants.SUNDAY) {
        d = getDemand(SystemDao.getReviewCycleStartDate());
      }
      int targetInv = (int) d.getRcAvgDemand() - inventory;

      setInventory(targetInv > 0 ? targetInv : (int) d.getRcAvgDemand());
      isStockedOut = false;
    }

    if (crc.getDayOfWeek() == DateTimeConstants.SUNDAY) {
      rcBopInv = inventory;
    }
    // daily inventory updates
    updateEpEopInv();
    epEopInv = inventory;
    rcInvOut += epInvOut;
    rcInvIn += epInvIn;
    ProductInventory p = getInventory(SystemDao.getReviewCycleStartDate());
    double beginOfPeriodEpAvgInv = 0;
    if (p != null) {
      beginOfPeriodEpAvgInv = p.getEpAvgInv();
    }
    double weight_5 = SystemDao.getWeight_5();
    epAvgInv =
        weight_5 * ((epEopInv >= 0) ? epEopInv : 0) + (((1 - weight_5) * beginOfPeriodEpAvgInv));
    epAvgInv = Math.max(0, epAvgInv);
  }
  private String inventoryToString() {
    StringBuffer epAvgInvBf = new StringBuffer();
    epAvgInvBf.append("epAvgInv|");

    StringBuffer epEopInvBf = new StringBuffer();
    epEopInvBf.append("epEopInv|");

    StringBuffer epInvOutBf = new StringBuffer();
    epInvOutBf.append("epInvOut|");

    StringBuffer epInvInBf = new StringBuffer();
    epInvInBf.append("epInvIn|");

    StringBuffer rcBopInvBf = new StringBuffer();
    rcBopInvBf.append("rcBopInv|");

    StringBuffer rcInvInBf = new StringBuffer();
    rcInvInBf.append("rcInvIn|");

    StringBuffer rcInvOutBf = new StringBuffer();
    rcInvOutBf.append("rcInvOut|");

    StringBuffer actualInvBf = new StringBuffer();
    actualInvBf.append("actualInv|");

    Iterator<Years> yrItr = inventoryMap.keySet().iterator();
    Map<LocalDate, ProductInventory> currInvMap;
    LocalDate currDate;
    Years currYr;
    ProductInventory currInv;
    // Populate Sale buffers
    while (yrItr.hasNext()) {
      currYr = yrItr.next();
      currInvMap = inventoryMap.get(currYr);
      Iterator<LocalDate> itr = currInvMap.keySet().iterator();
      while (itr.hasNext()) {
        currDate = itr.next();
        currInv = currInvMap.get(currDate);
        // epAvgInv
        epAvgInvBf.append(currDate);
        epAvgInvBf.append(":");
        epAvgInvBf.append(currInv.getEpAvgInv());
        epAvgInvBf.append("|");
        // epEopInv
        epEopInvBf.append(currDate);
        epEopInvBf.append(":");
        epEopInvBf.append(currInv.getEpEopInv());
        epEopInvBf.append("|");
        // epInvOut
        epInvOutBf.append(currDate);
        epInvOutBf.append(":");
        epInvOutBf.append(currInv.getEpInvOut());
        epInvOutBf.append("|");
        // epInvIn
        epInvInBf.append(currDate);
        epInvInBf.append(":");
        epInvInBf.append(currInv.getEpInvIn());
        epInvInBf.append("|");

        // rcBopInv
        rcBopInvBf.append(currDate);
        rcBopInvBf.append(":");
        rcBopInvBf.append(currInv.getRcBopInv());
        rcBopInvBf.append("|");
        // rcInvInBf
        rcInvInBf.append(currDate);
        rcInvInBf.append(":");
        rcInvInBf.append(currInv.getRcInvIn());
        rcInvInBf.append("|");
        // rcInvOut
        rcInvOutBf.append(currDate);
        rcInvOutBf.append(":");
        rcInvOutBf.append(currInv.getRcInvOut());
        rcInvOutBf.append("|");
        // ActualInv
        actualInvBf.append(currDate);
        actualInvBf.append(":");
        actualInvBf.append(currInv.getInventory());
        actualInvBf.append("|");
      }
    }
    epAvgInvBf.append(";\n");
    epEopInvBf.append(";\n");
    epInvOutBf.append(";\n");
    epInvInBf.append(";\n");
    actualInvBf.append(";\n");

    rcBopInvBf.append(";\n");
    rcInvInBf.append(";\n");
    rcInvOutBf.append(";\n");

    StringBuffer retBf = new StringBuffer();
    retBf.append(epAvgInvBf.toString());
    retBf.append(epEopInvBf.toString());
    retBf.append(epInvOutBf.toString());
    retBf.append(epInvInBf.toString());
    retBf.append(actualInvBf.toString());
    retBf.append(rcBopInvBf.toString());
    retBf.append(rcInvInBf.toString());
    retBf.append(rcInvOutBf.toString());
    retBf.append("\n");

    return retBf.toString();
  }
  /** Outlier filtering has been done prior to processing daily metrics * */
  private void processDailyMetrics() {
    LocalDate crc = SystemDao.getCrc();
    double lostSales = getLostSales();

    rcSalesActual = rcSalesActual + epSalesActual;
    // rcSales is outlier filtered sales and has been calculated in outlier processing
    rcSales = rcSales + epSales;

    Years yr = Years.years(crc.getYear());
    Map<LocalDate, Sales> currSalesMap = salesMap.get(yr);
    if (currSalesMap == null) {
      currSalesMap = new TreeMap<LocalDate, Sales>();
    }
    Sales s = currSalesMap.get(crc);
    if (s == null) {
      s = new Sales();
    }
    s.setEpSalesActual(epSalesActual);
    s.setEpSales(epSales);
    s.setRcSales(rcSales);
    s.setRcSalesActual(rcSalesActual);
    s.setLostSales(lostSales);

    currSalesMap.put(crc, s);
    salesMap.put(yr, currSalesMap);

    /** * Daily Demand Calculations **** */
    // daily demand
    epDemand = getEpDemand();
    // demand = sales + lostsales
    // outlier filtered sales used for rcDemand
    rcDemand = rcSales + lostSales;
    rcDemandActual += (epSalesActual + lostSales);

    Map<LocalDate, Demand> currDemandMap = demandMap.get(yr);
    if (currDemandMap == null) {
      currDemandMap = new TreeMap<LocalDate, Demand>();
    }
    Demand d = currDemandMap.get(crc);
    if (d == null) {
      d = new Demand();
    }
    d.setEpDemand(epDemand);
    d.setRcDemand(rcDemand);
    d.setRcDemandActual(rcDemandActual);
    currDemandMap.put(crc, d);
    demandMap.put(yr, currDemandMap);

    /** **** Daily Inventory Calculations ****** */
    processInventory();

    if (epSales > rcMaxSales) {
      rcMaxSales = epSales;
      weekSinceMaxSales = 0;
    }

    if (epEopInv > demoStock) {
      daysSinceWalk = daysSinceWalk + 1;
    } else {
      daysSinceWalk = 0;
    }

    if (epSalesActual == 0) {
      daysSinceSale = daysSinceSale + 1;
    } else {
      daysSinceSale = 0;
    }

    Map<LocalDate, ProductInventory> currInventoryMap = inventoryMap.get(yr);
    if (currInventoryMap == null) {
      currInventoryMap = new TreeMap<LocalDate, ProductInventory>();
    }
    ProductInventory inv = currInventoryMap.get(crc);
    if (inv == null) {
      inv = new ProductInventory();
    }
    inv.setEpAvgInv(epAvgInv);
    inv.setEpEopInv(epEopInv);
    inv.setEpInvOut(epInvOut);
    inv.setEpInvIn(epInvIn);
    inv.setRcBopInv(rcBopInv);
    inv.setRcBopInv(rcBopInv);
    inv.setRcInvIn(rcInvIn);
    inv.setRcInvOut(rcInvOut);
    inv.setInventory(inventory);

    currInventoryMap.put(crc, inv);
    inventoryMap.put(yr, currInventoryMap);

    if (statusCd == STATUS_CD.LEARNING) {
      performDailyLearning();
    }
    resetEpAccumulators();
  }