
if (FALSE) 
{
  .g()
  source("~rsaporta/git/misc/rscripts/utils/Orchard_Specific/AcctBIandProd_are_synced.r")
  # AcctBIandProd_are_synced(colsToPull="transac_typeid", minDate="2014-11-01", verbose=TRUE)
  # AcctBIandProd_are_synced(colsToPull="countryid", minDate="2014-11-01", verbose=TRUE)
  (AcctBIandProd_are_really_synced(sink=TRUE, minDate.complete='2011-01-01'))
  debug(AcctBIandProd_are_synced)
  (AcctBIandProd_are_synced(cluster.in=4))
}

## TODO:  Add a column check



AcctBIandProd_are_synced <- function(cluster.in=9, cluster.out=9, colsToPull="storeid", minDate="2014-09-01", maxDate=NULL, storesToCheck=c(1, 7, 187, 286), wh=getSnowflakeWH(), dbname=getSnowflakeDB(), schema=getSnowflakeSchema(), snowflake_inuse=getOption("snowflake_inuse"), verbose=FALSE) {
## This function checks that bi.accounting and production.fact_sales are in sync
## And returns TRUE/FALSE
## 
## Useful for deteremining whether or not to run an update on bi.accounting

  cat("Checking if production.fact_sales is in sync with bi.accounting...  ")

  browser(expr=inDebugMode("AcctBIandProd_are_synced"), text="in AcctBIandProd_are_synced at the top before making the queries")

  whereIn <- if (!is.null(storesToCheck)) list(storeid=storesToCheck) else NULL

  colsToPull.sales <- colsToPull
  colsToPull.bi    <- colsToPull

  colsToPull.sales <- gsub("transac_typeid", "transactiontypeid", colsToPull.sales)
  colsToPull.bi    <- gsub("transactiontypeid", "transac_typeid", colsToPull.bi)

  colsToPull.sales <- gsub("country_code", "countryid", colsToPull.sales)
  colsToPull.bi    <- gsub("countryid", "country_code", colsToPull.bi)

  colsToPull.sales <- gsub("date", "accountingperiodid", colsToPull.sales)
  colsToPull.bi    <- gsub("date", "accounting_month",   colsToPull.bi)


  ## Cols whose value to actually check
  colsToAgg.sales <- c(units="sales", "gross")
  colsToAgg.bi    <- c(units="units", "gross")

  ## Queries
  Q.sales <- makeQry(tbl="fact_sales", schema="production", aggFunc="SUM", colsToPull=colsToPull.sales, colsToAgg=colsToAgg.sales, minDate=minDate, maxDate=maxDate, whereIn=whereIn, dateCol="accountingperiodid")
  Q.bi    <- makeQry(tbl="accounting", schema="bi",         aggFunc="SUM", colsToPull=colsToPull.bi,    colsToAgg=colsToAgg.bi,    minDate=minDate, maxDate=maxDate, whereIn=whereIn, dateCol="accounting_month")


  ## Execute Queries
  DT.sales <- runQry(Q.sales, cluster=cluster.in,  key=NULL, wh=wh, dbname=dbname, snowflake_inuse=snowflake_inuse, verbose=verbose)
  DT.bi    <- runQry(Q.bi,    cluster=cluster.out, key=NULL, wh=wh, dbname=dbname, snowflake_inuse=snowflake_inuse, verbose=verbose)

  ## Clean up meta data from fact_sales
  ## Country
  if (any(grepl("country", colsToPull))) {
     addCountry.byCode_ (DT=DT.sales, country_codeCol="countryid", colsToBring="country_code")
     DT.sales[, countryid := NULL]
  }
 
  ## date
  addDateCols.periodid_(DT.sales, showWarnings=FALSE, dropPeriodCols=TRUE)

  if ("accountingdate" %in% names(DT.sales))
    setcolorderpt(DT.sales, "accountingdate")
  if ("accounting_month" %in% names(DT.bi))
    setcolorderpt(DT.bi, "accounting_month")

  ## key cols and set key
  kCols.sales <- c(setdiff(names(DT.sales), colsToAgg.sales))
  kCols.bi    <- c(setdiff(names(DT.bi),    colsToAgg.bi))
  setkeyIfNot(DT.sales, kCols.sales, verbose=FALSE)
  setkeyIfNot(DT.bi,    kCols.bi, verbose=FALSE)

  ## Check the row count
  if (nrow(DT.sales) != nrow(DT.bi))
    warning("DT.sales (", nrow(DT.sales) , ") & DT.bi (", nrow(DT.bi), ") have differing number of rows")

  ret <- all(eq <- DT.sales == DT.bi)
  message(sprintf("  [  %s  ]", ifelse(ret, "OK", "Needs Updating")))

  if (!ret) {
    inds <- sort(unique(c(1:150, 300:320, 800:900, 1500:1550, ((-200):(-1)+nrow(DT.sales)))))
    inds <- inds[inds < nrow(DT.sales) & inds > 0]
    print(formnumb(DT.sales[DT.bi][inds], round=0), nrows=1000)
  }

  browser(expr=inDebugMode("AcctBIandProd_are_synced", "AcctBIandProd_are_synced_bottom"), text="in AcctBIandProd_are_synced at the top before making the queries")

  return(ret)
}



AcctBIandProd_are_really_synced <- function(cluster.in=cluster.out, cluster.out=9, minDate=prevMonth(monthFloor(today()), 4), storesToCheck=NULL, sink=TRUE , minDate.complete, wh=getSnowflakeWH(), dbname=getSnowflakeDB(), schema=getSnowflakeSchema(), snowflake_inuse=getOption("snowflake_inuse"), verbose=FALSE) {
  
  force (cluster.out)
  force (snowflake_inuse)

  if (getProjName() == "") {
    setScience("dbconcile", subl=FALSE, load=FALSE)
    message("using projName = 'dbconcole' --- but remember that these functions live in utils/'Orchard Specific'")
  }

  stopifnot(
      qCanRead(tbl="fact_sales", schema="production", cluster=cluster.in,  wh=wh, dbname=dbname, snowflake_inuse=snowflake_inuse, verbose=FALSE)
    , qCanRead(tbl="accounting", schema="bi",         cluster=cluster.out, wh=wh, dbname=dbname, snowflake_inuse=snowflake_inuse, verbose=FALSE)
  )

  if (isTRUE(sink)) {
    sinkOn(zarchive.old.files=TRUE, split=FALSE)
    on.exit(sinkOff())
  }

  colsToPull <- c("storeid", "transactiontypeid", "countryid", "labelid")

  ## check in phases
  if (missing(minDate.complete)) {
    message("Finding the minDate.")
    minDate.complete <- qMaxDate("accounting", schema="bi", min=TRUE, cluster=cluster.out, wh=wh, dbname=dbname, snowflake_inuse=snowflake_inuse, verbose=FALSE)
  }

  message("Using minDate.complete for the simple queries: '", minDate.complete, "'")

  for (col in colsToPull) {
    if (col %in% c("storeid", "transactiontypeid"))
      next
    if (!AcctBIandProd_are_synced(colsToPull=c(col, "date"), minDate=minDate.complete, storesToCheck=storesToCheck, cluster.in=cluster.in, cluster.out=cluster.out, wh=wh, dbname=dbname, snowflake_inuse=snowflake_inuse, verbose=TRUE)) {
      message("FAILED at simple ", col, " check")
      return(FALSE)
    }
    cat("\n")
  }


  lastMonth <- prevMonth(monthFloor(today()), 1)
  if (lastMonth <= minDate)
    lastMonth <- NULL

  ## Check more deeply up until lastMonth
  if (!AcctBIandProd_are_synced(colsToPull=c(colsToPull, "date"), minDate=minDate, maxDate=lastMonth, storesToCheck=storesToCheck, cluster.in=cluster.in, cluster.out=cluster.out, wh=wh, dbname=dbname, snowflake_inuse=snowflake_inuse, verbose=TRUE)) {
    message("FAILED at full check with minDate=", minDate, " and maxDate=",lastMonth, " ")
    return(FALSE)
  }
  ## Check more deeply from lastMonth to no-cap
  if (!AcctBIandProd_are_synced(colsToPull=c(colsToPull, "date"), minDate=lastMonth, maxDate=NULL, storesToCheck=storesToCheck, cluster.in=cluster.in, cluster.out=cluster.out, wh=wh, dbname=dbname, snowflake_inuse=snowflake_inuse, verbose=TRUE)) {
    message("FAILED at full check with minDate=", minDate, " and maxDate=NULL ")
    return(FALSE)
  }

  message("Succes!!  bi.accounting and fact_sales are in sync!")
  return(TRUE)
}

