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


###### ------------------------------------ ########
{
  if (!exists("DT.bak"))
    DT.bak <- copy(DT.mgmtr_summary_tall)

  kCols.compgroup <- c(kCols.datestore, "release_is_compilation")

  DT.comp_splits <- DT.acc[, list(OA_gross_forDSOM = sum(gross)), keyby=kCols.compgroup]

  DT.comp_splits.anal <- DT.anal[date %ni% unique(DT.comp_splits$date), list(OA_gross_forDSOM = sum(gest, na.rm=TRUE)), keyby=kCols.compgroup]
  DT.comp_splits <-  rbind(DT.comp_splits, DT.comp_splits.anal)

  ## Make sure all release_is_compilations are represented per date-store and fill with 0's
  setkeyIfNot(DT.comp_splits, kCols.compgroup, verbose=FALSE)
  DT.comp_splits <- DT.comp_splits[setkey(DT.comp_splits[, unique(DT.comp_splits$release_is_compilation), keyby=kCols.datestore])]
  DT.comp_splits[is.na(OA_gross_forDSOM), OA_gross_forDSOM := 0]

  ## Calculate the SC split per each
  DT.comp_splits[, comp_split := OA_gross_forDSOM / sum(OA_gross_forDSOM), by=kCols.datestore]

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

  ## Split up the gross by release_is_compilation
  stopifnot(is.null(DT.mgmtr_summary_tall$release_is_compilation))
  DT.mgmtr_summary_tall <- rbindlist(lapply(unique(DT.comp_splits$release_is_compilation), function(x) 
                                      ## logic:  It cannot be a compilation and NOT be (Orchard AND Music)
                                cbind(DT.mgmtr_summary_tall, release_is_compilation = x)[!(release_is_compilation & !(label_sc_group == "Orchard" & music_vs_video == "Music"))] 
                            ))

  kCols.for_gross_summing <- c(kCols.datestore,  "label_sc_group", "music_vs_video", "release_product_type", "musicbucket")
  ## Take the average permonth of each SC group. Then Confirm all sum to a whole
  DT.avg_comp_splits <- DT.comp_splits[, sum(OA_gross_forDSOM), keyby=c("date",  "release_is_compilation")][, list(release_is_compilation, avg_comp_split = V1 / sum(V1)), by=date]
  setkeyIfNot(DT.avg_comp_splits, c("date", "release_is_compilation"), verbose=FALSE)
  stopifnot(sumsToOne(DT.avg_comp_splits, "avg_comp_split", by="date"))

  ## set those groups that have only one unique "release_is_compilation" value to a prelim split percentage of 100%. (To avoid monthly avverages)
  setkeyIfNot(DT.mgmtr_summary_tall, kCols.for_gross_summing, verbose=FALSE)
  DT.mgmtr_summary_tall[DT.mgmtr_summary_tall[, lunique(release_is_compilation), by=kCols.for_gross_summing][(V1 == 1)], comp_split := 1]



  ## match the keys to add the splits
  ## Load in the splits
  matchKey(DT.mgmtr_summary_tall, DT.comp_splits, key(DT.comp_splits))
  DT.mgmtr_summary_tall[DT.comp_splits, comp_split := ifelse(is.na(comp_split), i.comp_split, comp_split)]

  ## Manually view on screen
  DT.mgmtr_summary_tall[!is.na(comp_split)] [, lunique(release_is_compilation), by=kCols.for_gross_summing][(V1 == 1)]
  DT.mgmtr_summary_tall[is.na(comp_split)] [, lunique(release_is_compilation), by=kCols.for_gross_summing][(V1 != 1)]

  ## Fill with monthly averages
  matchKey(DT.mgmtr_summary_tall, DT.avg_comp_splits, c("date", "release_is_compilation"))
  DT.mgmtr_summary_tall[DT.avg_comp_splits, comp_split := ifelse(is.na(comp_split), round(i.avg_comp_split, 4), comp_split)]


  ## There should not be any comp_split 's 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(comp_split) & !all(is.na(comp_split)), by=kCols.datestore][, !any(V1)])


          ## ****************** ##
  ## There should not be any compilations in non-orchard, non-music, non-download. Check that all others are "1" cor comp_split
  stopifnot(sumsToOne(DT.mgmtr_summary_tall, col="comp_split", by=kCols.for_gross_summing), fail=TRUE)
  stopifnot(DT.mgmtr_summary_tall[!(label_sc_group == "Orchard" & music_vs_video == "Music" & musicbucket %in% c("Download", "zUncategorized Store"))][, comp_split == 1])

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


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

  ## Confirm all sum
  stopifnot(DT.mgmtr_summary_tall[, sum(gross_comp_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_comp_splat", "gross")


  ## clear the backup
  rm(DT.bak)

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

