---
title: Endoscopies
runtime: shiny
output:
  html_document:
    toc: true
    toc_float: true
---

```{r setup, message = F, warning = F, echo = F}
library(tidyverse)
library(DT)
library(knitr)
library(magrittr)
library(stringr)

opts_chunk$set(message = F,
               echo = F,
               warning = F)
options(DT.options = list(paging = F,
                          searching = F,
                          info = F,
                          dom = "Bfrtip",
                          buttons = c("copy", "excel")),
        shiny.maxRequestSize = 100*1024^2)
```

# {.tabset}

## Chargement des données

Les fichiers doivent être fournis au format CSV avec les paramètres suivants :

* séparateur de champs = ,
* guillemets = "
* encodage = UTF-8
* séparateur de décimales = ,

### Endoscopies en externe

Le fichier doit être issu de la [requête suivante](https://livenne.chu-nancy.fr/shiny_files/endoscopies/endoscopies_ext.wid).

### Endoscopies en hospitalisation

Le fichier doit être issu de la [requête suivante](https://livenne.chu-nancy.fr/shiny_files/endoscopies/endoscopies_hos.wid).

```{r data}
if (!dir.exists("/var/www/html/shiny_files/endoscopies"))
  dir.create("/var/www/html/shiny_files/endoscopies")

file.copy("endoscopies_ext.wid", "/var/www/html/shiny_files/endoscopies", overwrite = T) -> null
file.copy("endoscopies_hos.wid", "/var/www/html/shiny_files/endoscopies", overwrite = T) -> null

inputPanel(
  fileInput("ext", "Endoscopies externes", accept = "text/csv"),
  fileInput("hos", "Endoscopies hospitalisations", accept = "text/csv"),
  downloadButton("report", "Télecharger le rapport")
)

read_csv2("actes.csv") -> liste_actes

endoscopies <- reactive(
  {
    req(input$ext)

    read_csv(input$ext$datapath, locale = locale(decimal_mark = ",")) %>%
      setNames(c("Venue", "Intervention", "Acte", "Tarif", "Acte_libelle", "Service", "Type_venue")) %>%
      left_join(liste_actes %>% select(Code, Appareil), by = c("Acte" = "Code")) %>%
      mutate_if(is.character, factor) %>%
      filter(Tarif != 0) %>%
      distinct
  }
)

endoscopies_ext <- reactive(
  {
    req(endoscopies())

    endoscopies() %>%
      filter(Type_venue != 2)
  })

endoscopies_hos <- reactive(
  {
    req(input$hos)

    read_csv(input$hos$datapath, locale = locale(decimal_mark = ",")) %>%
      setNames(c("RSS", "Durée", "GHM", "GHM_libelle", "Valorisation")) %>%
      distinct %>%
      mutate(Type_duree = `Durée` %>% cut(c(-Inf, 1.5, Inf), labels = c("0 ou 1 j", "1+ j")))
  })

```

```{r report}
output$report <- downloadHandler(filename = "endoscopies.html",
                                content = function(file)
                                {
                                  rmarkdown::render("endoscopies.rmd", output_file = file, params = list(liste_actes = liste_actes, endoscopies = endoscopies(), endoscopies_ext = endoscopies_ext(), endoscopies_hos = endoscopies_hos()))
                                })
```

## Rapport

### Codes utilisés {.tabset}

#### ORL
```{r liste_orl}
liste_actes %>%
  filter(Appareil == "ORL") %>%
  select(- Appareil) %>%
  datatable(rownames = F, options = list(paging = T, searching = T))
```

#### Digestif
```{r liste_dig}
liste_actes %>%
  filter(Appareil == "Digestif") %>%
  select(- Appareil) %>%
  datatable(rownames = F, options = list(paging = T, searching = T))
```

#### Respiratoire
```{r liste_respi}
liste_actes %>%
  filter(Appareil == "Respiratoire") %>%
  select(- Appareil) %>%
  datatable(rownames = F, options = list(paging = T, searching = T))
```

#### Génito-urinaire
```{r liste_uro}
liste_actes %>%
  filter(Appareil == "Genito-urinaire") %>%
  select(- Appareil) %>%
  datatable(rownames = F, options = list(paging = T, searching = T))
```

### Global

#### Total
```{r global_total}
renderDataTable(
  {
    endoscopies() %>%
      summarise(Venues = n_distinct(Venue)) %>%
      datatable(rownames = F,
                extensions = "Buttons")
  })
```

#### Par service exécutant
```{r global_par_service}
renderDataTable(
  {
    endoscopies() %>%
      group_by(Service) %>%
      summarise(Venues = n_distinct(Venue)) %>%
      ungroup %>%
      arrange(desc(Venues)) %>%
      datatable(rownames = F,
                extensions = "Buttons")
  })
```

### Externe

#### Total
```{r ext_total}
renderDataTable(
  {
    endoscopies_ext() %>%
      summarise(Venues = n_distinct(Venue),
                Valorisation = sum(Tarif)) %>%
      datatable(rownames = F,
                extensions = "Buttons") %>%
      formatCurrency(2, currency = "€", dec.mark = ",", mark = " ", before = F)
  })
```

#### Par appareil
```{r ext_par_appareil}
renderDataTable(
  {
    endoscopies_ext() %>%
      group_by(Appareil) %>%
      summarise(Venues = n_distinct(Venue),
                Valorisation = sum(Tarif)) %>%
      arrange(desc(Valorisation)) %>%
      datatable(rownames = F,
                extensions = "Buttons") %>%
      formatCurrency(3, currency = "€", dec.mark = ",", mark = " ", before = F)
  })
```

#### Par acte
```{r ext_par_acte}
renderDataTable(
  {
    endoscopies_ext() %>%
      group_by(Acte, Acte_libelle) %>%
      summarise(Venues = n_distinct(Venue),
                Valorisation = sum(Tarif)) %>%
      ungroup %>%
      arrange(desc(Valorisation)) %>%
      datatable(options = list(paging = T, searching = T),
                colnames = c("Libelle" = "Acte_libelle"),
                rownames = F,
                extensions = "Buttons") %>%
      formatCurrency(4, currency = "€", dec.mark = ",", mark = " ", before = F)
  })
```

#### Par service exécutant
```{r ext_par_service}
renderDataTable(
  {
    endoscopies_ext() %>%
      group_by(Service) %>%
      summarise(Venues = n_distinct(Venue),
                Valorisation = sum(Tarif)) %>%
      ungroup %>%
      arrange(desc(Valorisation)) %>%
      datatable(rownames = F,
                extensions = "Buttons") %>%
      formatCurrency(3, currency = "€", dec.mark = ",", mark = " ", before = F)
  })
```

#### Par service exécutant et par acte
```{r ext_par_acte_et_service}
renderDataTable(
  {
    endoscopies_ext() %>%
      group_by(Service, Acte, Acte_libelle) %>%
      summarise(Venues = n_distinct(Venue),
                Valorisation = sum(Tarif)) %>%
      arrange(Service, desc(Valorisation)) %>%
      ungroup %>%
      datatable(options = list(paging = T, searching = T),
                colnames = c("Libelle" = "Acte_libelle"),
                rownames = F,
                extensions = "Buttons",
                filter = "top") %>%
      formatCurrency(5, currency = "€", dec.mark = ",", mark = " ", before = F)
  })
```

### Hospitalisation

#### Total
```{r total_hos}
renderDataTable(
  {
    endoscopies_hos() %>%
      summarise(RSS = n(),
                Valorisation = sum(Valorisation)) %>%
      datatable(rownames = F,
                extensions = "Buttons") %>%
      formatCurrency(2, currency = "€", dec.mark = ",", mark = " ", before = F)
  })
```

#### Par durée d'hospitalisation
```{r hos_par_durée}
renderDataTable(
  {
    endoscopies_hos() %>%
      group_by(Type_duree) %>%
      summarise(RSS = n(),
                Valorisation = sum(Valorisation)) %>%
      datatable(rownames = F,
                colnames = c("Durée" = "Type_duree"),
                extensions = "Buttons") %>%
      formatCurrency(3, currency = "€", dec.mark = ",", mark = " ", before = F)
  })
```

#### Par GHM {.tabset}

##### 0 ou 1j
```{r ghm_hos_0j}
renderDataTable(
  {
    endoscopies_hos() %>%
      filter(Type_duree == "0 ou 1 j") %>%
      group_by(GHM, GHM_libelle) %>%
      summarise(RSS = n(),
                Valorisation = sum(Valorisation)) %>%
      ungroup %>%
      arrange(desc(RSS)) %>%
      datatable(rownames = F,
                colnames = c("Libelle GHM" = "GHM_libelle"),
                options = list(paging = T),
                extensions = "Buttons") %>%
      formatCurrency(4, currency = "€", dec.mark = ",", mark = " ", before = F) %>%
      formatRound(5)
  })
```

##### 1+ j
```{r ghm_hos_1j}
renderDataTable(
  {
    endoscopies_hos() %>%
      filter(Type_duree == "1+ j") %>%
      group_by(GHM, GHM_libelle) %>%
      summarise(RSS = n(),
                Valorisation = sum(Valorisation)) %>%
      ungroup %>%
      arrange(desc(RSS)) %>%
      datatable(rownames = F,
                colnames = c("Libelle GHM" = "GHM_libelle"),
                options = list(paging = T),
                extensions = "Buttons") %>%
      formatCurrency(4, currency = "€", dec.mark = ",", mark = " ", before = F) %>%
      formatRound(5)
  })
```
