setScience("!Misc", load=FALSE, subl=FALSE, create=TRUE)

dateCol <- "accounting_month"
minDate.full <- as.Date("2013-08-01")
countries_Using <- c("ES", "FI", "FR", "GB", "NL", "NO", "NZ", "SE", "US", "AU", "BE", "CH", "DE", "DK", "IT", "PL", "PT", "MX", "AR", "TR", "CL", "CO", "PE", "BR", "PH")
colsToPull <- c(dateCol, "labelid", "label_name", "label_owner", "label_sc_group", "label_is_dthree", country_of_label="label_country", "label_priority", "releaseid", "release_name", "release_sale_start_date", "release_releasedate", "release_priority", "release_genre", country_of_transaction="country_code", "transac_type_abbr")
colsToAgg <- c("units", "gross")
tbl <- "accounting"
schema <- "bi"
storeids <- 286

ultMax <- qMaxDate(tbl, schema=schema)
dates <- seq(minDate.full, ultMax+31, by="+1 month") 

for (i in seq(length(dates)-1))
{
  minDate <- dates[[i]]
  maxDate <- dates[[i+1]] - 1

  Q.ankit <- makeQry(colsToPull=colsToPull, colsToAgg=colsToAgg, aggFunc="sum", tbl=tbl, schema=schema, minDate=minDate, maxDate=maxDate, dateCol=dateCol, whereIn=list(storeid = storeids, country_code=countries_Using))
  DT.ankit <- runQry(Q.ankit, allow.large=TRUE)
  info <- sprintf("from_%s_to_%s", minDate, maxDate)

  f.out <- out.p(sprintf("Spotify_Data_for_ankit__%s", info), ext="csv")
  print(f.out)
  write.table(DT.ankit, file=f.out, append=TRUE, sep="|", row.names=FALSE, col.names=TRUE)

  jesusForData(DT.ankit, info=info)
}


tos <- c(`Ankit Sheth`="asheth@liquidhub.com", `Antoine Wilson`="awilson@theorchard.com", `Prashant Bahadur` = "prashant@theorchard.com")
ccs <- c(`Rick Saporta`="rsaporta@theorchard.com")

body <- sprintf(
"Hi Ankit, 

Attached is the Spotify data broken down.  The only column which may not be self-evident is 'label_sc_group' which indicates which supply chain group, if any, a particular label belongs to.  Options are 'RED', 'Allegro' or 'SelectO'. If not one of these three, the value will default to 'Orchard'.  In contrast, label_owner and label_is_dthree are broader categories that include other entities beyond the aforementioned.

For your records, the query used is shown below

----------------------------------------------------------
%s
----------------------------------------------------------

"


