srcOther <- function(new.src, old.src=getProjName(), path=srcDir) {
  pat  <- paste0("/", old.src, "(/|$)")
  repl <- paste0("/", new.src, "\\1")
  gsub(pat, repl, path)
}

loadFromJesus(objToLoad="DT.stores", src=srcOther("MGMTReport"))

-----



Will create TWO summary tables. 

mgmtr_summary_tall  := summary of monthly numbers, tall. This one will be "immutable" in that it will track all historical values for budget and gest for any given month. 
mgmtr_summary       := summary of monthly numbers, wide. This one will select the lastest estimates/budgets for any given month (ie, where modifiedDate = (select max(modifieddate) group by groupings) )
                       This will also have gross and gross_yago and budget all side by side. 


mgmtr_summary_tall
c("accounting_month, store, music_vs_video, musicbucket, videobucket, ancillaryline, gross, budget, gross_is_estimate, gross_last_modified, budget_last_modified")

mgmtr_summary
c("accounting_month, store, music_vs_video, musicbucket, videobucket, ancillaryline, gross, gross_yago, budget, budget_yago, gross_is_estimate, gross_yago_is_estimate, gross_last_modified, budget_last_modified, budget_yago_last_modified")

      [,1]                  
 [1,] "accounting_month"    
 [2,] "store"
 [3,] "music_vs_video"       
 [4,] "musicbucket"         
 [5,] "videobucket"         
 [6,] "gross"               
 [7,] "budget"              
 [8,] "gross_is_estimate"   
 [9,] "gross_last_modified" 
[10,] "budget_last_modified"


kCols.acc <- c("")
DT.acc
date
storeid
release_is_compilation

label_sc_group

stream_vs_download
store_name

store_musicbucket
store_videobucket

music_vs_video_by_product
stream_vs_download
music_vs_video_by_transac

gross
units
source

DT.acc[, table(stream_vs_download, store_musicbucket)]








# GIVEN THESE FOUR
DT.acc
DT.anal
DT.GL.summaries
DT.sc_splits
DT.stores


## Begin creating the TALL
DT.mgmtr_summary_tall <- {
  DT.GL.summaries[, list(  date                   = accounting_month
                         , storeid                = suppressWarnings(as.integer(storeid))
                         , storeid_char           = storeid
                         , store_name             = store_name
                         , music_vs_video         = Music_vs_Video
                         , musicbucket            = NA_character_
                         , videobucket            = NA_character_
                         , is_compilation         = FALSE
                         # , label_sc_group         = NA_character_
                         , ancillaryline             = NA_character_
                         , gross                  = Total_ThisMonth
                         , budget                 = NA_real_
                         , gross_is_estimate      = FALSE
                         , gross_last_modified    = last_modified
                         , budget_last_modified   = as.POSIXct(NA_real_, origin=.origin.utc)
                         , store_is_in_analytics  = NA
                         , store_is_in_accounting = NA
                         )
                  ]
}

## THESE ARE MISSING.  ADDRESS
DT.anal[store_name %ni% DT.mgmtr_summary_tall$store_name]



###### ------------------------------------ ########


## Split up the gross by label_sc_group
DT.mgmtr_summary_tall <- rbindlist(lapply(unique(DT.acc$label_sc_group), function(x) cbind(DT.mgmtr_summary_tall, label_sc_group = x)))


kCols.for_gross_summing <- c("date", "storeid", "store_name", "music_vs_video")
## Take the average permonth of each SC group. Then Confirm all sum to a whole
DT.avgsplits <- DT.sc_splits[, sum(OA_gross_forDSOM), keyby=c("date",  "label_sc_group")][, list(label_sc_group, avg_sc_split = V1 / sum(V1)), by=date]
setkeyIfNot(DT.avgsplits, kCols.SC_date, verbose=FALSE)
stopifnot(sumsToOne(DT.avgsplits, "avg_sc_split", by="date"))


## match the keys to add the splits
## Load in the splits
matchKey(DT.mgmtr_summary_tall, DT.sc_splits, key(DT.sc_splits))
DT.mgmtr_summary_tall[DT.sc_splits, sc_split := i.sc_split]

## There should not be any sc_splits that are NA other than those wher ethe whole store-month are NA (meaning they were not in OA, and thus we will take the monthly average)
##  Note that if there are some offenders here (meaning only some NA per store-month) then when we fill in with averages
##        those NAs will be given a value, which will in turn throw off the total Gross
stopifnot(DT.mgmtr_summary_tall[, is.na(sc_split) & !all(is.na(sc_split)), by=c("date", "storeid")][, !any(V1)])

## Fill with monthly averages
matchKey(DT.mgmtr_summary_tall, DT.avgsplits, kCols.SC_date)
DT.mgmtr_summary_tall[DT.avgsplits, sc_split := ifelse(is.na(sc_split), round(i.avg_sc_split, 4), sc_split)]

## There should not be any NA's in sc_split
stopifnot(DT.mgmtr_summary_tall[, !is.na(sc_split)])


## Next, multiply the gross by the sc_split. 
##  Take back any rounding error and assign it to "Orchard"
DT.mgmtr_summary_tall[, gross_splat := gross * sc_split]
DT.mgmtr_summary_tall[, gross_splat := ifelse(label_sc_group=="Orchard", gross - sum(gross_splat[label_sc_group != "Orchard"]),  gross_splat), by=kCols.for_gross_summing]

## Confirm all sum
stopifnot(DT.mgmtr_summary_tall[, sum(gross_splat) - sum(unique(gross), na.rm=TRUE), by=c("date", "storeid", "store_name")][, abs(V1) < 1e-4])




