## NOTE:   Instances of  kCols.datestore  may instead need to be kCols.main


###### ------------------------------------ ########
{
  kCols.scgroup <- c(kCols.datestore, "label_sc_group")
  
  DT.sc_splits <- DT.acc[, list(OA_gross_forDSOM = sum(gross)), keyby=kCols.scgroup]
  DT.sc_splits.anal <- DT.anal[date %ni% unique(DT.sc_splits$date), list(OA_gross_forDSOM = sum(gest, na.rm=TRUE)), keyby=kCols.scgroup]
  DT.sc_splits <-  rbind(DT.sc_splits, DT.sc_splits.anal)
  
  ## Make sure all label_sc_groups are represented per date-store and fill with 0's
  setkeyIfNot(DT.sc_splits, kCols.scgroup, verbose=FALSE)
  DT.sc_splits <- DT.sc_splits[setkey(DT.sc_splits[, unique(DT.sc_splits$label_sc_group), keyby=kCols.datestore])]
  DT.sc_splits[is.na(OA_gross_forDSOM), OA_gross_forDSOM := 0]
  
  ## Calculate the SC split per each
  DT.sc_splits[, sc_split := OA_gross_forDSOM / sum(OA_gross_forDSOM), by=kCols.datestore]
  
  ###### ------------------------------------ ########
  
  ## Split up the gross by label_sc_group
  DT.mgmtr_summary_tall <- rbindlist(lapply(unique(DT.sc_splits$label_sc_group), function(x) cbind(DT.mgmtr_summary_tall, label_sc_group = x)))
  
  
  kCols.for_gross_summing <- c(kCols.datestore, "music_vs_video")
  ## Take the average permonth of each SC group. Then Confirm all sum to a whole
  DT.avg_sc_splits <- 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.avg_sc_splits, kCols.SC_date, verbose=FALSE)
  stopifnot(sumsToOne(DT.avg_sc_splits, "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=kCols.datestore][, !any(V1)])
  
  ## Fill with monthly averages
  matchKey(DT.mgmtr_summary_tall, DT.avg_sc_splits, kCols.SC_date)
  DT.mgmtr_summary_tall[DT.avg_sc_splits, 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_sc_splat := gross * sc_split]
  DT.mgmtr_summary_tall[, gross_sc_splat := ifelse(label_sc_group=="Orchard", gross - sum(gross_sc_splat[label_sc_group != "Orchard"]),  gross_sc_splat), by=kCols.for_gross_summing]
  
  ## Confirm all sum
  stopifnot(DT.mgmtr_summary_tall[, sum(gross_sc_splat) - sum(unique(GL_gross_forDS), na.rm=TRUE), by=kCols.datestore][, abs(V1) < 1e-4])
  
  ## move the gross_x_splat over to gross, and drop the splat column
  DT.mgmtr_summary_tall[, gross := NULL]
  setnames(DT.mgmtr_summary_tall, "gross_sc_splat", "gross")


  DT.mgmtr_summary_tall
}
###### ------------------------------------ ########
  
  
