# 01b RED Splits for Spotify.r

colsToPull.red_split <- c("accounting_month", "label_sc_group", "transac_type")
colsToAgg  <- c("units", "gross")

## NOTE: We are missing data for March 2013 to Oct 2013 ... but for accounting??
DT.RED_Split_Spotify <- 
  runQry(makeQry(
      tbl = "accounting"
    , schema = "bi"
    , colsToPull = colsToPull.red_split
    , colsToAgg = colsToAgg
    , dateCol = "accounting_month"
    , minDate = "2013-11-01"
    , where = list(store_name = "Spotify")
    , limit = NULL
    ))

DT.RED_Split_Spotify[label_sc_group %ni% c("Orchard", "RED"), label_sc_group := "OSC"]
setkeyIfNot(DT.RED_Split_Spotify, colsToPull.red_split)
DT.RED_Split_Spotify <- aggregateDT(DT.RED_Split_Spotify)

DT.RED_Split_Spotify[, GPU := gross / units]

{
  jesusForData(DT.RED_Split_Spotify)
  saveImageTo()
  .cb()
}

&&&  CONTINUE ON LOCAL MACHINE   &&&&&&&


.g(); loadFromJesus("DT.RED_Split_Spotify", over=TRUE)

# DT.RED_Split_Spotify[, units_perc := units / sumn(units), by=accounting_month]
# DT.RED_Split_Spotify[, gross_perc := gross / sumn(gross), by=accounting_month]


"%% percent of revenue, for each transac_type"

GPU by transac_type


#7a0d20
(colors.sc <- getDict("colors.sc"))

P.SPotify_GPU_with_OSC_Split <- 
  ggLinegraph(DT.RED_Split_Spotify[transac_type != "Download Tracks"]
            , x="accounting_month", y="GPU", color="label_sc_group"
            , ylab = "Gross Revenue per Unit"
            , y_relativetext = .7
            , thickness = 0.75
            , linetype="transac_type", yscale="dollar"
            , dict.colors = colors.sc
            )

P.SPotify_GPU_with_OSC_Split_FACETED <- P.SPotify_GPU_with_OSC_Split + facet_grid("transac_type ~ .", scales="free")

printToPDF(list(P.SPotify_GPU_with_OSC_Split, P.SPotify_GPU_with_OSC_Split_FACETED), f.name="SPotify_GPU_with_OSC_Split.pdf", paginate=FALSE, ncol=1)

ggplot(DT.RED_Split_Spotify[transac_type != "Download Tracks"], aes(x = accounting_month, y=GPU, color=label_sc_group)) + geom_line(aes(linetype = transac_type)) + geom_point() + dollar.y(units="ones") 

P.GPU <- ggplot(DT.RED_Split_Spotify[transac_type != "Download Tracks"], aes(x = accounting_month, y=GPU, color=label_sc_group)) + geom_line(aes(linetype = transac_type)) + geom_point() + dollar.y(units="ones") 
