R/Classes/ReconClass.R

library(tidyverse)

path <- getwd()
isRunningInShiny <- Sys.getenv('SHINY_PORT') != ""

if(endsWith(path,'R') | isRunningInShiny){
  source('Classes/MintClass.R')
  source('Classes/ExpensesClass.R')
} else {
  source('R/Classes/MintClass.R')
  source('R/Classes/ExpensesClass.R')
}


setClass("Recon", slots =c(mint = "Mint",
                          expenses = "Expenses"),
         prototype=list(
           mint = NULL,
           expenses = NULL
         ))
#Creator
Recon = function(mint,expenses) {
  new("Recon",mint=mint,expenses=expenses)
}


setGeneric(name="reconExpenses",
           def=function(object,tranWeek,mintWeek,showReconciled)
           {
             standardGeneric("reconExpenses")
           }
)

#' Join mint Data to Reconcile
#' @param object Recon object
#' @param tranWeek No of weeks of Expenses transaction to take into account
#' @param mintWeek No of weeks of Mint transaction to take into account
#' @param showReconciled Show expenses that have already been reconciled?
#' @return data frame
#' @export
#' @examples
setMethod(f="reconExpenses",
          signature=c("Recon","character","character","logical"),
          definition = function(object,tranWeek,mintWeek,showReconciled) {

sumTran <- if(showReconciled)
                      subsetSummary(object@expenses,tranWeek)
              else
                filter( subsetSummary(object@expenses,tranWeek),
                          Reconciled != "Yes"  | is.na(Reconciled))

clnMint <- subsetTransactions(object@mint,mintWeek)

#Need to convert to character becouse, double comparison is not possible.
mutTran <- mutate(sumTran,chrSmAmount = as.character(smAmount))
mutMint <- mutate(clnMint,chrclnAmount = as.character(clnAmount))


joinDs <- left_join(mutTran,
                  mutMint,
                  by = c( "Store" = "Establishment", "chrSmAmount" = "chrclnAmount" ))


#arrange fields in required order
arrDs <- arrange(joinDs, desc(TransactionDate))


#rename fields
rnDs <- rename(arrDs,Reconciled = Reconciled.x)

#select required fields
selDs <- select(rnDs,
              TransactionDate,
              Store,
              ReceiptNo,
              smAmount,
              trBilledDateStr,
              Reconciled)

#make sure schema is correct
checkSchema(selDs,reconTranSchema)

selDs

})


setGeneric(name="reconMint",
           def=function(object,tranWeek,mintWeek,showReconciled)
           {
             standardGeneric("reconMint")
           }
)


#' Join transaction & Mint Data to Reconcile
#' @param object Recon object
#' @param tranWeek No of weeks of Expenses transaction to take into account
#' @param mintWeek No of weeks of Mint transaction to take into account
#' @param showReconciled Show expenses that have already been reconciled?
#' @return data frame
#' @export
#' @examples
setMethod(f="reconMint",
          signature=c("Recon","character","character","logical"),
          definition = function(object,tranWeek,mintWeek,showReconciled) {

  sumTran <- subsetSummary(object@expenses,tranWeek)

  clnMint <- if(showReconciled)
                subsetTransactions(object@mint,mintWeek)
              else
                filter( subsetTransactions(object@mint,mintWeek),
                        Reconciled != "Yes" | is.na(Reconciled))

  #Need to convert to character becouse, double comparison is not possible.
  mutTran <- mutate(sumTran,chrSmAmount = as.character(smAmount))
  mutMint <- mutate(clnMint,chrclnAmount = as.character(clnAmount))


  joinDs <- left_join(mutMint,
                      mutTran,
                      by = c( "Establishment" = "Store", "chrclnAmount" = "chrSmAmount" ))

  #arrange fields in required order
  arrDs <- arrange(joinDs, desc(clnBilledDate))

  #rename Reconciled fields. Due to join, some field are appended with .x
  renDs <- rename(arrDs,Reconciled=Reconciled.x)

  #select required fields
  selDs <- select(renDs,
                  trBilledDateStr,
                  Establishment,
                  Category,
                  clnAmount,
                  TransactionDate,
                  ReceiptNo,
                  Reconciled)

  #make sure schema is correct
  checkSchema(selDs,reconMintSchema)

  selDs

})
ravi9884/PersonalFinance documentation built on May 4, 2019, 6:38 p.m.