## This file is not necessary.  Was useful for debugging and confirming that things are executing correctly. 
## It would go before creating  DT.acc_v_anal


# ------------------------- # 
if (FALSE && "Confiming that changeAndAggregate() works correctly")
{   
  ## CONFIRM THAT changeAndAggregate() IS WORKING CORRECTLY
  DT.anals  <- combineAnalWithAErr(DT.totalpaidunits.anal, DT.totalpaidunits.aErr, fromErr=FALSE)
  DT.anals2 <- changeAndAggregate(DT.anals, colsToAgg=colNamesFromVector(colsToAgg.anal), colsToDrop=c("download_activity_date"), changeFunc=changeFunc.anals544, browseOnFail=TRUE)



  ANAL  <- DT.totalpaidunits.anal[storeid %in% stores_confirm & download_activity_date >= mnD_confirm & download_activity_date <= EndOfMonth(mxD_confirm), list(DT='DT.totalpaidunits.anal', units=sumn(paidunits)), keyby=list(storeid)]
  AERR  <- DT.totalpaidunits.aErr[storeid %in% stores_confirm & download_activity_date >= mnD_confirm & download_activity_date <= EndOfMonth(mxD_confirm), list(DT='DT.totalpaidunits.aErr', units=sumn(paidunits)), keyby=list(storeid)]
  COMB  <- DT.anals[storeid %in% stores_confirm & download_activity_date >= mnD_confirm & download_activity_date <= EndOfMonth(mxD_confirm), list(DT='DT.anals', units=sumn(paidunits)), keyby=list(storeid)]
  COMB2 <- DT.anals2[storeid %in% stores_confirm][ date >= mnD_confirm & date <= EndOfMonth(mxD_confirm), list(DT='DT.anals2', units=sumn(paidunits)), keyby=list(storeid)]

  # ----------------------------------------------------------------------------------------
  # (RIGHT)
  #   CORRECTION ON THE FOLLOWING NOTE:  
  #       544 Implies 364 Days per year.   
  #       If you look at the diff of whats missing for iTunes, it is just about 1/365 of the total
  # ----------------------------------------------------------------------------------------
  # (WRONG) NOTE TO SELF:  There should be no loss of units from COMB to COMB2 (ie, before and after running changeAndAggregate) --  
  # (WRONG)                 The only place that this may happen is when doing a month-wise comparison (ie, the 544 might shift units)
  # (WRONG)                 However, during a whole year, there should be no loss
  # (WRONG)                 Also true if you take a three month 544 interval -- ie by quarter
  # ----------------------------------------------------------------------------------------
  {
    cat("\n---------------- \n")
    cat("COMB same as COMB2:\n")
    print(c(setNames(nm=stores_confirm, obj=COMB$units == COMB2$units), diff_in_iTunes=as.character(COMB[storeid==1,units]-COMB2[storeid==1,units])))
    cat("\nBefore ChangeFunc: \n")
    print(formnumb(merge(COMB,  merge(ANAL, AERR, all=TRUE, suffix=c(".anal", ".aErr")), all=TRUE)[, DIFF := units - (units.anal + units.aErr)], round=FALSE)); cat("DT.anals has ", formnumb(nrow(DT.anals)), "rows", fill=TRUE)
    cat("\nAfter ChangeFunc: \n")
    print(formnumb(merge(COMB2, merge(ANAL, AERR, all=TRUE, suffix=c(".anal", ".aErr")), all=TRUE)[, DIFF := units - (units.anal + units.aErr)], round=FALSE)); cat("DT.anals2 has ", formnumb(nrow(DT.anals2)), "rows", fill=TRUE)
    cat("\n---------------- \n")
  }

  ## TODO:  Detect whether or not the diff column in the merge is $0 (except itunes anal which should be about 1/365 give or take)
} 


## I used these when the above failed.  However, I believe it failed due to the missing  EndOfMonth()  around mxD_confirm. 
## 
## Leaving it here in case fails again. 
## 
##
# DT.anals [storeid == 7 ][monthFloor(download_activity_date) == "2014-11-01"][, sum(paidunits), by=transid]
# DT.anals2[storeid == 7 ][monthFloor(date) == "2014-11-01" ][, sum(paidunits), by=transid ]
# 
# .dd <- as.Date("2014-06-01")
# .dd <- as.Date("2012-01-01")
# .dd <- mnD_confirm
# 
# monthCeiling("2014-12-10")
# formnumb(DT.anals [storeid == 7 ][(download_activity_date) >= .dd & download_activity_date <= monthCeiling(mxD_confirm)-1][, sum(paidunits), by=transid])
# formnumb(DT.anals2[storeid == 7 ][(date) >= .dd & date <= monthCeiling(mxD_confirm)-1 ][, sum(paidunits), by=transid ])
# 
# DT.anals[storeid %in% 7 ][ download_activity_date >= mnD_confirm & download_activity_date <= mxD_confirm, list(DT='DT.anals', units=sumn(paidunits)), keyby=list(storeid)]
# DT.anals [storeid == 7 ][(download_activity_date) >= .dd & download_activity_date <= mxD_confirm][, sum(paidunits), by=transid]
# DT.anals2 [storeid == 7 ][(date) >= .dd & date <= mxD_confirm][, sum(paidunits), by=transid]
# 
# DT.anals [storeid == 7 ][(download_activity_date) >= .dd & download_activity_date <= mxD_confirm][, sum(paidunits), by=transid][, sum(V1)]
# DT.anals2 [storeid == 7 ][(date) >= .dd & date <= mxD_confirm][, sum(paidunits), by=transid][, sum(V1)]
# 
# 
