---
title: "Analysis"
format: html
editor: visual
---

## Quarto

```{r}
knitr::opts_chunk$set(echo = TRUE)
library('httr')
library('data.table')
library('dplyr') 
library(lubridate)
library(highcharter)
```

```{r}
user=keyring::key_list("odata.dev.shiny")[1,2]

pass=keyring::key_get("odata.dev.shiny", "SHINYAPP_REQUEST")

url<-"https://my355441.crm.ondemand.com/sap/c4c/odata/v1/c4codataapi/OpportunityCollection?$filter=CreationDate%20ge%20datetime'2022-01-01T00:00:00'%20and%20CreationDate%20le%20datetime'2023-07-25T00:00:00'%20and%20SalesOrganisationID%20eq%20'NL01'"
dt.get<-GET(url, authenticate(user, pass, type = "basic"))

dt.list<-content(dt.get, as="parsed")
dt.list$d$`__next`
dat<-data.table::rbindlist(dt.list$d$results,use.names=TRUE, fill=TRUE, idcol="UID")

sort(colnames(dat))
df<-unique(dat[, list(ID, ProcessingTypeCodeText, ProspectPartyID, Name, PrimaryContactPartyID,
           PriorityCodeText, OriginTypeCodeText, LifeCycleStatusCodeText, ExternalUserStatusCodeText, SalesCycleCodeText, SalesCyclePhaseCodeText, ProbabilityPercent, ExpectedRevenueAmount, ExpectedRevenueAmountCurrencyCode,
           SalesOrganisationID, SalesTerritoryID, MainEmployeeResponsiblePartyID,
           PhaseProgressEvaluationStatusCodeText, MainEmployeeResponsiblePartyName,
           SalesUnityPartyName, ProspectPartyName, SalesOrganisationName, SalesTerritoryName, CreationDate, Zoppstartdate_KUT, Zopptype_KUTText, Zproductpillar_KUT, Zinterproject_KUT, Zoppclosedate_KUT,
Zclassification_KUTText, Zprojectcountry_KUTText, Zprojectpostalcode_KUT, Zprojetcity_KUT, Zprojetcity_KUT)])


filename<-paste0(tempdir(), "/", "OppData", ".csv")

write.csv(df, file = filename)

odata.opp.get<-function(user=keyring::key_list("odata.dev.shiny")[1,2], pass=keyring::key_get("odata.dev.shiny", "SHINYAPP_REQUEST"), url="https://my355441.crm.ondemand.com/sap/c4c/odata/v1/c4codataapi/OpportunityCollection?$filter=CreationDate%20ge%20datetime'2022-01-01T00:00:00'%20and%20CreationDate%20le%20datetime'2023-07-25T00:00:00'%20and%20SalesOrganisationID%20eq%20'NL01'"){

  dt.get<-GET(url, authenticate(user, pass, type = "basic"))
  
  if(dt.get$status_code != 200){
    Sys.sleep(5)
  }
  dt.list<-content(dt.get, as="parsed")
  print("parsed")
  dat<-data.table::rbindlist(dt.list$d$results,use.names=TRUE, fill=TRUE, idcol="UID")
  dt.df<-unique(dat[, list(ID, ProcessingTypeCodeText, ProspectPartyID, Name, PrimaryContactPartyID,
           PriorityCodeText, OriginTypeCodeText, LifeCycleStatusCodeText, ExternalUserStatusCodeText, SalesCycleCodeText, SalesCyclePhaseCodeText, ProbabilityPercent, ExpectedRevenueAmount, ExpectedRevenueAmountCurrencyCode,
           SalesOrganisationID, SalesTerritoryID, MainEmployeeResponsiblePartyID,
           PhaseProgressEvaluationStatusCodeText, MainEmployeeResponsiblePartyName,
           SalesUnityPartyName, ProspectPartyName, SalesOrganisationName, SalesTerritoryName, CreationDate, Zoppstartdate_KUT, Zopptype_KUTText, Zproductpillar_KUT, Zinterproject_KUT, Zoppclosedate_KUT,
Zclassification_KUTText, Zprojectcountry_KUTText, Zprojectpostalcode_KUT, Zprojetcity_KUT)])
  rm(dat)
  print("writing file")
  filename<-paste0(tempdir(), "/", "OppData", ".csv")
  data.table::fwrite(dt.df,filename, append = TRUE, row.names = FALSE)
  print("Done")
  print(dt.list$d$`__next`)
  print(names(dt.list$d))
  if(!c("__next") %in% names(dt.list$d)){
    r.df<-read.csv(filename) |>
             mutate(CreationDate=as_datetime(as.numeric(gsub("\\D", "", CreationDate))/1000))
    file.remove(filename)
    return(r.df)
    
  } else {
    return(
      odata.opp.get(url=dt.list$d$`__next`,user=keyring::key_list("odata.dev.shiny")[1,2], pass=keyring::key_get("odata.dev.shiny", "SHINYAPP_REQUEST"))
    )
  }
  
}

opp.dt<-odata.opp.get()
colnames(opp.dt)

unique(opp.dt)
```


```{r}
opp.dt |> 
  mutate(cre_date=floor_date(CreationDate, unit="weeks")) |> 
  count(cre_date) |> # SalesTerritoryID, 
# PhaseProgressEvaluationStatusCodeText, 
# MainEmployeeResponsiblePartyName, SalesTerritoryName, Zopptype_KUTText, Zprojetcity_KUT
  gg
```


```{r}
opp.dt[opp.dt$SalesTerritoryName=="AT_09 - Kärnten",]
```


