etl <- function(sales.dir = getwd()){
all.csv <- list.files(path = sales.dir, pattern = "\\.csv$")
if(length(all.csv)==0L) return(FALSE) # no new files
joinkey <- c("prod_code","month_code")
sales <- rbindlist(lapply(all.csv, fread))[
][, month_code := substr(time_code,1L,6L)
][, .SD,, joinkey]
prices <- setDT(dbGetQuery(DBIconn, "SELECT * FROM prices"),
key = joinkey)
sales[prices, value := quantity * price]
sqlSave(RODBCconn, sales, "sales", append=TRUE, rownames=FALSE)
file.rename(all.csv, paste(all.csv,paste0("loaded",as.character(Sys.time(),"%Y%m%d%H%M%S")), sep="."))
}
etl()
# disconnect from database if not using it anymore
dbDisconnect(DBIconn)
odbcClose(RODBCconn)