## GL_pt2_e Create DT.ancillary_with_forecasts.r

## This file adds forecast to ancillary lines, 
##  rbinds them back with DT.ancillary_from_rev_summary
##  and sets column names, ready to be rbound into DT.cleaned


## FORECAST future values for ancillary
{
  method_used_for_forecasting <- "all"
  forecasts_manual_from_rev_summary.ll <- emptylist(dates_not_in_GL, nm=as.character(dates_not_in_GL))

  ## We need to create forecasts FOR EACH DATE in dates_not_in_GL
  for (date.target in dates_not_in_GL) {

    date.target <- as.Date(date.target)

    ## We will test which is the better forecast
    forecasts_manual_from_rev_summary.ll[[as.character(date.target)]] <- 
                mkForecast( DT=DT.ancillary_from_rev_summary ## GL_firstDate_forS will be NA when  !(id_is_in_GL)
                          , valueCol="reported_gross"
                          , byCols=kCols.smry  # "date" will be removed in function
                          , date_diff="month"
                          , date.target=date.target
                          , months.to.test=4
                          , method=method_used_for_forecasting
                          , verbose=FALSE
                          , fail.on.duplicates=FALSE
                          )

      ## bank the forecast method
      ## Also, Apply the same method to all future dates, after the first test
      method_used_for_forecasting <- attr(forecasts_manual_from_rev_summary.ll[[as.character(date.target)]], "method")
  } #// end for loop

  verboseMsg(verbose, "The method_used_for_forecasting is '", method_used_for_forecasting, "'", time=FALSE, sep="")

  ## Flatten the list  (each element of the list is one forecasted date)
  DT.forecasts_manual_from_rev_summary <- rbindlist(forecasts_manual_from_rev_summary.ll)

  ## recall that DT.ancillary_from_rev_summary comes from the overall SUMMARY sheet
  ##   so it will not have a gross_last_modified date  Nevertheless add one for forecasted values
  DT.forecasts_manual_from_rev_summary[, gross_last_modified := Sys.Date()]

  setnames(DT.forecasts_manual_from_rev_summary, "forecastedValue", "reported_gross")
}

## CREATE DT.ancillary_with_forecasts  and clean
{
  ## Rbind, with filler
  DT.ancillary_with_forecasts <- rbind(DT.ancillary_from_rev_summary, DT.forecasts_manual_from_rev_summary, use.names=TRUE, fill=TRUE)

  ## clean gross_last_modified to be a date
  DT.ancillary_with_forecasts[, gross_last_modified := as.Date(gross_last_modified)]

  ## clean column names
  setnames(DT.ancillary_with_forecasts, "reported_gross", colName_being_adjusted)
}


warning ("Dont forget to add the CIs for DT.ancillary_with_forecasts")