R/subset_by_date.R

Defines functions subset_by_date

Documented in subset_by_date

#' Subset daily_work by date
#'
#' Subset daily_work by date.
#'
#' @param mo Character string of month to bill for, from constant `month.abb`.
#' @param yr Numeric year to bill for.
#' @param up_to_day Last day of month to bill for (inclusive). Billing then includes dates from previous months with
#' `day > up_to_day`.
#' @inheritParams parse_daily_work
#' @importFrom lubridate %within%
#' @export

subset_by_date <- function(dw, mo, yr, up_to_day=25){
  stopifnot(!is.na(dw$date), colSums(!is.na(dw)) > 0)
  stopifnot(!is.na(mo), mo %in% month.abb, is.numeric(yr))

  # subset to new period
  mo.last.day <- lubridate::days_in_month(x = lubridate::dmy(paste(1, mo, yr)))
  end.date <- lubridate::dmy(paste(min(up_to_day, mo.last.day), mo, yr))

  if (up_to_day == 31){
    start.date <- lubridate::dmy(paste(1, mo, yr))
  } else {
    start.date <- end.date - months(1) + lubridate::days(1)
  }

  bill.interval <- lubridate::interval(start = start.date, end = end.date)

  dw <- dw[dw$date %within% bill.interval,]
  dw
}
jdreyf/jdcbill documentation built on Nov. 4, 2019, 2:35 p.m.