@@ -7,9 +7,6 @@ create_calendar <- S7::new_generic("create_calendar","x")
77
88calculate <- S7 :: new_generic(" calculate" ," x" )
99
10-
11-
12-
1310# ' Create Calendar Table
1411# ' @name create_calendar
1512# ' @param x ti object
@@ -28,69 +25,35 @@ calculate <- S7::new_generic("calculate","x")
2825# ' @keywords internal
2926S7 :: method(create_calendar ,ti ) <- function (x ){
3027
31- # 1. Determine the Anchor Start Date --------------------------------------
32- # For 5-5-4 calendars, we must anchor to the fiscal start (Sunday closest to Feb 1).
33- # For standard calendars, we use the natural data minimum.
34-
35- # 2. Summarize Raw Data ---------------------------------------------------
36- # Aggregate the source data to the target time unit (day, month, etc.)
37- # before building the scaffold.
38-
3928
4029 summary_dbi <- x @ datum @ data | >
4130 dplyr :: ungroup() | >
4231 make_db_tbl() | >
4332 dplyr :: mutate(
44- date = lubridate :: floor_date(!! x @ datum @ date_quo , unit = !! x @ time_unit @ value ,week_start = 1 ),
33+ date = lubridate :: floor_date(!! x @ datum @ date_quo , unit = !! x @ time_unit @ value ,week_start = 7 ),
4534 time_unit = !! x @ time_unit @ value
4635 ) | >
4736 dplyr :: summarise(
4837 !! x @ value @ value_vec : = sum(!! x @ value @ value_quo , na.rm = TRUE ),
4938 .by = c(date , !!! x @ datum @ group_quo )
5039 )
5140
52-
53- if (x @ datum @ calendar_type != " standard" ) {
54- min_year <- lubridate :: year(x @ datum @ min_date )
55- start_date <- closest_sunday_feb1(min_year )
56-
57- # Ensure the anchor doesn't start after our actual data
58- if (min_year < lubridate :: year(start_date )) {
59- start_date <- closest_sunday_feb1(min_year - 1 )
60- }
61- } else {
62- start_date <- x @ datum @ min_date
63- }
64-
65-
66-
67- # 3. Define "Active Life" Bounds ------------------------------------------
68- # Optimization: Calculate the first and last activity per group.
69- # This prevents generating thousands of 'zero' rows for products that
70- # didn't exist yet or were discontinued.
71-
7241 active_bounds <- summary_dbi | >
7342 dplyr :: summarise(
7443 min_g = min(date , na.rm = TRUE ),
7544 max_g = max(date , na.rm = TRUE ),
7645 .by = c(!!! x @ datum @ group_quo )
7746 )
7847
79- # 4. Generate Master Date Sequence ---------------------------------------
80- # Create a single-column table of all possible dates in the range.
8148
8249 master_dates <- seq_date_sql(
83- start_date = start_date ,
50+ start_date = x @ datum @ min_date ,
8451 end_date = x @ datum @ max_date ,
8552 calendar_type = x @ datum @ calendar_type ,
8653 time_unit = x @ time_unit @ value ,
8754 .con = dbplyr :: remote_con(x @ datum @ data )
8855 )
8956
90- # 5. Build the Scaffolding ------------------------------------------------
91- # If groups exist, expand the calendar. We use an inner join to the bounds
92- # to constrain the cross-join to only the "Active Life" of each group.
93-
9457 if (x @ datum @ group_indicator ) {
9558
9659 calendar_dbi <- master_dates | >
@@ -107,11 +70,6 @@ S7::method(create_calendar,ti) <- function(x){
10770 calendar_dbi <- master_dates
10871 }
10972
110- # 6. Final Join & Gap Filling ---------------------------------------------
111- # Combine the scaffold with actual data. Dates with no records are
112- # flagged and filled with 0 to ensure continuous time intelligence.
113-
114-
11573
11674 full_dbi <-
11775 calendar_dbi | >
@@ -124,6 +82,7 @@ S7::method(create_calendar,ti) <- function(x){
12482 !! x @ value @ value_vec : = dplyr :: coalesce(!! x @ value @ value_quo , 0 )
12583 )
12684
85+
12786 return (full_dbi )
12887
12988
@@ -327,7 +286,6 @@ S7::method(print,segment_abc) <- function(x,...){
327286 )
328287 )
329288
330-
331289 cli :: cat_line(" " )
332290
333291 print_actions_steps(x )
0 commit comments