phantomas34 commited on
Commit
d1fce9e
·
1 Parent(s): fe8a793

feat: Revenue Intelligence tab — Phase 2 executed contract value by PM, GC concentration, project pipeline

Browse files
Files changed (2) hide show
  1. server.R +300 -0
  2. ui.R +127 -2
server.R CHANGED
@@ -1769,4 +1769,304 @@ function(input, output, session) {
1769
  ggplotly(p, tooltip = "text")
1770
  })
1771
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1772
  }
 
1769
  ggplotly(p, tooltip = "text")
1770
  })
1771
 
1772
+ # ── DATA FETCH ────────────────────────────────────────────────────────────────
1773
+
1774
+ get_revenue_data <- reactive({
1775
+ input$refresh
1776
+
1777
+ if (!dbExistsTable(db_pool, "pm___project_file")) return(data.frame())
1778
+
1779
+ dbGetQuery(db_pool, "
1780
+ SELECT
1781
+ pm.pmprojectid,
1782
+ pm.projectid,
1783
+ pm.projectname,
1784
+ pm.projectmanager,
1785
+ pm.projectdatecreated,
1786
+ pm.finalcompletiondate,
1787
+ pm.substantialcompletiondate,
1788
+ pm.billingcompletepercent,
1789
+ pm.billingstatus,
1790
+ pf.contractor,
1791
+ pf.contractamt,
1792
+ pf.contractdate,
1793
+ -- Normalise Scott W. Hutchings to match estimators table
1794
+ CASE
1795
+ WHEN TRIM(pm.projectmanager) = 'Scott W. Hutchings' THEN 'Scott Hutchings'
1796
+ ELSE TRIM(pm.projectmanager)
1797
+ END AS manager_clean
1798
+ FROM pm___project_file pm
1799
+ JOIN project_file pf
1800
+ ON pm.projectid::text = pf.projectid::text
1801
+ WHERE pm.projectmanager IS NOT NULL
1802
+ AND pm.projectmanager != ''
1803
+ AND pm.projectmanager != 'Unassigned Project'
1804
+ AND pf.contractamt > 0
1805
+ ORDER BY pm.projectdatecreated DESC
1806
+ ")
1807
+ })
1808
+
1809
+
1810
+ # ── POPULATE FILTERS ──────────────────────────────────────────────────────────
1811
+
1812
+ observe({
1813
+ df <- get_revenue_data()
1814
+ if (nrow(df) == 0) return()
1815
+
1816
+ managers <- sort(unique(df$manager_clean))
1817
+ contractors <- sort(unique(df$contractor[!is.na(df$contractor) & df$contractor != ""]))
1818
+
1819
+ updatePickerInput(session, "ri_manager", choices = managers)
1820
+ updatePickerInput(session, "ri_contractor", choices = contractors)
1821
+
1822
+ min_dt <- as.Date(min(df$projectdatecreated, na.rm = TRUE))
1823
+ max_dt <- as.Date(max(df$projectdatecreated, na.rm = TRUE))
1824
+ updateDateRangeInput(session, "ri_date_range", start = min_dt, end = max_dt)
1825
+ })
1826
+
1827
+
1828
+ # ── FILTERED DATA ─────────────────────────────────────────────────────────────
1829
+
1830
+ ri_filtered <- reactive({
1831
+ df <- get_revenue_data()
1832
+ if (nrow(df) == 0) return(df)
1833
+
1834
+ req(input$ri_date_range)
1835
+ df <- df %>%
1836
+ filter(as.Date(projectdatecreated) >= input$ri_date_range[1],
1837
+ as.Date(projectdatecreated) <= input$ri_date_range[2])
1838
+
1839
+ if (!is.null(input$ri_manager) && length(input$ri_manager) > 0)
1840
+ df <- df %>% filter(manager_clean %in% input$ri_manager)
1841
+
1842
+ if (!is.null(input$ri_contractor) && length(input$ri_contractor) > 0)
1843
+ df <- df %>% filter(contractor %in% input$ri_contractor)
1844
+
1845
+ df
1846
+ })
1847
+
1848
+
1849
+ # ── KPIs ─────────────────────────────────────────────────────────────────────
1850
+
1851
+ output$ri_kpi_total_value <- renderValueBox({
1852
+ df <- ri_filtered()
1853
+ val <- sum(df$contractamt, na.rm = TRUE)
1854
+ valueBox(
1855
+ paste0("$", format(round(val / 1e6, 2), big.mark = ","), "M"),
1856
+ "Total Contract Value",
1857
+ icon = icon("dollar-sign"),
1858
+ color = "green"
1859
+ )
1860
+ })
1861
+
1862
+ output$ri_kpi_total_projects <- renderValueBox({
1863
+ df <- ri_filtered()
1864
+ valueBox(
1865
+ comma(nrow(df)),
1866
+ "Executed Projects",
1867
+ icon = icon("building"),
1868
+ color = "blue"
1869
+ )
1870
+ })
1871
+
1872
+ output$ri_kpi_completed <- renderValueBox({
1873
+ df <- ri_filtered()
1874
+ n <- sum(!is.na(df$finalcompletiondate))
1875
+ valueBox(
1876
+ comma(n),
1877
+ "Completed Projects",
1878
+ icon = icon("check-circle"),
1879
+ color = "yellow"
1880
+ )
1881
+ })
1882
+
1883
+ output$ri_kpi_avg_contract <- renderValueBox({
1884
+ df <- ri_filtered()
1885
+ avg <- mean(df$contractamt, na.rm = TRUE)
1886
+ valueBox(
1887
+ paste0("$", format(round(avg / 1e3, 1), big.mark = ","), "K"),
1888
+ "Avg Contract Value",
1889
+ icon = icon("chart-line"),
1890
+ color = "purple"
1891
+ )
1892
+ })
1893
+
1894
+
1895
+ # ── EXECUTED REVENUE CHART ────────────────────────────────────────────────────
1896
+
1897
+ output$ri_revenue_chart <- renderPlotly({
1898
+ df <- ri_filtered()
1899
+ validate(need(nrow(df) > 0, "No project data for selected filters."))
1900
+
1901
+ agg <- df %>%
1902
+ group_by(manager_clean) %>%
1903
+ summarise(
1904
+ total_value = sum(contractamt, na.rm = TRUE),
1905
+ n_projects = n(),
1906
+ .groups = "drop"
1907
+ ) %>%
1908
+ arrange(desc(total_value)) %>%
1909
+ mutate(manager_clean = fct_reorder(manager_clean, total_value))
1910
+
1911
+ p <- ggplot(agg, aes(
1912
+ x = manager_clean,
1913
+ y = total_value,
1914
+ text = paste0(
1915
+ "<b>", manager_clean, "</b><br>",
1916
+ "Contract Value: $", format(round(total_value), big.mark = ","), "<br>",
1917
+ "Projects: ", n_projects
1918
+ )
1919
+ )) +
1920
+ geom_col(fill = "#1a5276", width = 0.65) +
1921
+ coord_flip() +
1922
+ scale_y_continuous(
1923
+ labels = function(x) paste0("$", format(x / 1e6, big.mark = ","), "M")
1924
+ ) +
1925
+ labs(x = "", y = "Total Contract Value") +
1926
+ theme_minimal() +
1927
+ theme(panel.grid.major.y = element_blank(),
1928
+ axis.text.y = element_text(size = 10))
1929
+
1930
+ ggplotly(p, tooltip = "text")
1931
+ })
1932
+
1933
+
1934
+ # ── PROJECT COMPLETION PIPELINE ───────────────────────────────────────────────
1935
+
1936
+ output$ri_pipeline_chart <- renderPlotly({
1937
+ df <- ri_filtered()
1938
+ validate(need(nrow(df) > 0, "No project data for selected filters."))
1939
+
1940
+ metric <- input$ri_pipeline_metric
1941
+
1942
+ agg <- df %>%
1943
+ mutate(status = ifelse(!is.na(finalcompletiondate), "Completed", "In Progress")) %>%
1944
+ group_by(manager_clean, status) %>%
1945
+ summarise(
1946
+ count = n(),
1947
+ value = sum(contractamt, na.rm = TRUE),
1948
+ .groups = "drop"
1949
+ )
1950
+
1951
+ y_col <- if (metric == "count") "count" else "value"
1952
+ y_label <- if (metric == "count") "Projects" else "Contract Value ($)"
1953
+
1954
+ p <- ggplot(agg, aes(
1955
+ x = manager_clean,
1956
+ y = .data[[y_col]],
1957
+ fill = status,
1958
+ text = paste0(
1959
+ "<b>", manager_clean, "</b><br>",
1960
+ status, "<br>",
1961
+ if (metric == "count")
1962
+ paste0("Projects: ", count)
1963
+ else
1964
+ paste0("Value: $", format(round(value), big.mark = ","))
1965
+ )
1966
+ )) +
1967
+ geom_col(position = "stack", width = 0.65) +
1968
+ coord_flip() +
1969
+ scale_fill_manual(
1970
+ values = c("Completed" = "#1e8449", "In Progress" = "#85c1e9"),
1971
+ name = ""
1972
+ ) +
1973
+ scale_y_continuous(
1974
+ labels = if (metric == "value")
1975
+ function(x) paste0("$", format(x / 1e6, big.mark = ","), "M")
1976
+ else
1977
+ comma_format()
1978
+ ) +
1979
+ labs(x = "", y = y_label) +
1980
+ theme_minimal() +
1981
+ theme(panel.grid.major.y = element_blank(),
1982
+ legend.position = "bottom",
1983
+ axis.text.y = element_text(size = 10))
1984
+
1985
+ ggplotly(p, tooltip = "text")
1986
+ })
1987
+
1988
+
1989
+ # ── GC REVENUE CONCENTRATION ──────────────────────────────────────────────────
1990
+
1991
+ output$ri_gc_chart <- renderPlotly({
1992
+ df <- ri_filtered()
1993
+ validate(need(nrow(df) > 0, "No project data for selected filters."))
1994
+
1995
+ top_n <- input$ri_top_gc
1996
+
1997
+ agg <- df %>%
1998
+ filter(!is.na(contractor), contractor != "") %>%
1999
+ group_by(contractor) %>%
2000
+ summarise(
2001
+ total_value = sum(contractamt, na.rm = TRUE),
2002
+ n_projects = n(),
2003
+ n_managers = n_distinct(manager_clean),
2004
+ .groups = "drop"
2005
+ ) %>%
2006
+ arrange(desc(total_value)) %>%
2007
+ slice_head(n = top_n) %>%
2008
+ mutate(contractor = fct_reorder(contractor, total_value))
2009
+
2010
+ p <- ggplot(agg, aes(
2011
+ x = contractor,
2012
+ y = total_value,
2013
+ text = paste0(
2014
+ "<b>", contractor, "</b><br>",
2015
+ "Contract Value: $", format(round(total_value), big.mark = ","), "<br>",
2016
+ "Projects: ", n_projects, "<br>",
2017
+ "Managers: ", n_managers
2018
+ )
2019
+ )) +
2020
+ geom_col(fill = "#2e86c1", width = 0.65) +
2021
+ coord_flip() +
2022
+ scale_y_continuous(
2023
+ labels = function(x) paste0("$", format(x / 1e6, big.mark = ","), "M")
2024
+ ) +
2025
+ labs(x = "", y = "Total Contract Value") +
2026
+ theme_minimal() +
2027
+ theme(panel.grid.major.y = element_blank(),
2028
+ axis.text.y = element_text(size = 9))
2029
+
2030
+ ggplotly(p, tooltip = "text")
2031
+ })
2032
+
2033
+
2034
+ # ── PROJECT DETAIL TABLE ──────────────────────────────────────────────────────
2035
+
2036
+ output$ri_project_table <- renderDataTable({
2037
+ df <- ri_filtered()
2038
+ validate(need(nrow(df) > 0, "No project data for selected filters."))
2039
+
2040
+ df %>%
2041
+ arrange(desc(contractamt)) %>%
2042
+ mutate(
2043
+ Manager = manager_clean,
2044
+ Project = projectname,
2045
+ GC = contractor,
2046
+ Contract = paste0("$", format(round(contractamt), big.mark = ",")),
2047
+ Created = format(as.Date(projectdatecreated), "%b %Y"),
2048
+ Completed = ifelse(
2049
+ !is.na(finalcompletiondate),
2050
+ format(as.Date(finalcompletiondate), "%b %Y"),
2051
+ "In Progress"
2052
+ )
2053
+ ) %>%
2054
+ select(Manager, Project, GC, Contract, Created, Completed) %>%
2055
+ datatable(
2056
+ options = list(
2057
+ pageLength = 10,
2058
+ scrollX = TRUE,
2059
+ dom = "ftp",
2060
+ order = list(list(3, "desc"))
2061
+ ),
2062
+ rownames = FALSE
2063
+ ) %>%
2064
+ formatStyle(
2065
+ "Completed",
2066
+ color = styleEqual(
2067
+ c("In Progress"),
2068
+ c("#2e86c1")
2069
+ )
2070
+ )
2071
+ })
2072
  }
ui.R CHANGED
@@ -15,6 +15,7 @@ dashboardPage(
15
  menuItem("Intervention Analysis", tabName = "its", icon = icon("flask")),
16
  menuItem("Data Management", tabName = "admin", icon = icon("database")),
17
  menuItem("Client Intelligence", tabName = "client_intel", icon = icon("handshake")),
 
18
  hr(),
19
  div(style = "padding:10px;",
20
  h5("Filter Controls",
@@ -561,8 +562,132 @@ dashboardPage(
561
  )
562
  )
563
  )
564
-
565
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
566
  )
567
  )
568
  )
 
15
  menuItem("Intervention Analysis", tabName = "its", icon = icon("flask")),
16
  menuItem("Data Management", tabName = "admin", icon = icon("database")),
17
  menuItem("Client Intelligence", tabName = "client_intel", icon = icon("handshake")),
18
+ menuItem("Revenue Intelligence", tabName = "revenue_intel", icon = icon("dollar-sign")),
19
  hr(),
20
  div(style = "padding:10px;",
21
  h5("Filter Controls",
 
562
  )
563
  )
564
  )
565
+ ,
566
+ tabItem(tabName = "revenue_intel",
567
+
568
+ # ── HEADER ─────────────────────────────────────────────────────────────────
569
+ fluidRow(
570
+ box(
571
+ width = 12, status = "info", solidHeader = FALSE,
572
+ div(style = "padding:4px 0;",
573
+ p(style = "margin:0; font-size:13px; color:#2c3e50;",
574
+ icon("flask"), " ",
575
+ tags$b("Phase 2 — Revenue Intelligence. "),
576
+ "Powered by executed project data from the IMS PM system. Shows contract
577
+ value, project completion, and GC revenue concentration by project manager.
578
+ Source: ", tags$code("pm___project_file"), " joined to ",
579
+ tags$code("project_file"), "."
580
+ )
581
+ )
582
+ )
583
+ ),
584
+
585
+ # ── KPI ROW ────────────────────────────────────────────────────────────────
586
+ fluidRow(
587
+ valueBoxOutput("ri_kpi_total_value", width = 3),
588
+ valueBoxOutput("ri_kpi_total_projects", width = 3),
589
+ valueBoxOutput("ri_kpi_completed", width = 3),
590
+ valueBoxOutput("ri_kpi_avg_contract", width = 3)
591
+ ),
592
+
593
+ # ── FILTERS ────────────────────────────────────────────────────────────────
594
+ fluidRow(
595
+ box(
596
+ width = 12, status = "primary", solidHeader = TRUE, title = "Filters",
597
+ fluidRow(
598
+ column(4,
599
+ pickerInput(
600
+ "ri_manager", "Project Manager",
601
+ choices = NULL, multiple = TRUE,
602
+ options = list(
603
+ `actions-box` = TRUE,
604
+ `live-search` = TRUE,
605
+ `selected-text-format` = "count > 2",
606
+ `none-selected-text` = "All Managers"
607
+ )
608
+ )
609
+ ),
610
+ column(4,
611
+ dateRangeInput(
612
+ "ri_date_range", "Project Created Date",
613
+ start = as.Date("2010-01-01"),
614
+ end = Sys.Date(),
615
+ separator = " to "
616
+ )
617
+ ),
618
+ column(4,
619
+ pickerInput(
620
+ "ri_contractor", "Contractor (GC)",
621
+ choices = NULL, multiple = TRUE,
622
+ options = list(
623
+ `actions-box` = TRUE,
624
+ `live-search` = TRUE,
625
+ `none-selected-text` = "All Contractors"
626
+ )
627
+ )
628
+ )
629
+ )
630
+ )
631
+ ),
632
+
633
+ # ── ROW 1: EXECUTED REVENUE + PROJECT PIPELINE ─────────────────────────────
634
+ fluidRow(
635
+ box(
636
+ title = "Executed Contract Value by Project Manager",
637
+ status = "primary", solidHeader = TRUE, width = 6,
638
+ plotlyOutput("ri_revenue_chart", height = "380px"),
639
+ p(class = "text-muted", style = "font-size:11px;",
640
+ "Total contract value of executed projects per manager.
641
+ Source: pm___project_file joined to project_file on projectid.")
642
+ ),
643
+ box(
644
+ title = "Project Completion Pipeline",
645
+ status = "primary", solidHeader = TRUE, width = 6,
646
+ fluidRow(
647
+ column(6,
648
+ radioGroupButtons(
649
+ "ri_pipeline_metric", NULL,
650
+ choices = c("Count" = "count", "Contract Value" = "value"),
651
+ selected = "count",
652
+ status = "default", size = "xs", justified = TRUE
653
+ )
654
+ )
655
+ ),
656
+ plotlyOutput("ri_pipeline_chart", height = "380px"),
657
+ p(class = "text-muted", style = "font-size:11px;",
658
+ "Completed vs in-progress projects per manager.
659
+ Toggle to see by count or by contract value.")
660
+ )
661
+ ),
662
+
663
+ # ── ROW 2: GC REVENUE CONCENTRATION + PROJECT DETAIL TABLE ─────────────────
664
+ fluidRow(
665
+ box(
666
+ title = "GC Revenue Concentration",
667
+ status = "primary", solidHeader = TRUE, width = 5,
668
+ fluidRow(
669
+ column(6,
670
+ sliderInput(
671
+ "ri_top_gc", "Show Top N GCs",
672
+ min = 5, max = 20, value = 10, step = 5, ticks = FALSE
673
+ )
674
+ )
675
+ ),
676
+ plotlyOutput("ri_gc_chart", height = "400px"),
677
+ p(class = "text-muted", style = "font-size:11px;",
678
+ "Which GCs generate the most executed contract value.
679
+ Reveals revenue concentration risk per contractor relationship.")
680
+ ),
681
+ box(
682
+ title = "Executed Project Detail",
683
+ status = "primary", solidHeader = TRUE, width = 7,
684
+ dataTableOutput("ri_project_table"),
685
+ p(class = "text-muted", style = "font-size:11px; margin-top:6px;",
686
+ "Full project list with contract values, completion dates, and GC.
687
+ Sortable and searchable.")
688
+ )
689
+ )
690
+ )
691
  )
692
  )
693
  )