---
title: "Untitled"
author: "Matt"
date: "April 7, 2016"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

Should use this document for actual operations and keep actual functions in another document for the public [GitHub repo](https://github.com/mattbk/ledgerr). Shiny will be set up at https://www.shinyapps.io/admin/#/dashboard.

```{r}
# Use https://github.com/maxconway/gsheet
library(gsheet)

# Pull Google sheet locations from another file (journal1, journal2, etc.)
source("config.R")

# Combine bank accounts
journal<-rbind(journal1,journal2)

## Clean up
# Date is date
journal$Date<-as.Date(journal$Date,format="%m/%d/%Y")
# Replace empty classes and accounrs
journal$Class[journal$Class==""]<-c("unclassified")
journal$Account[journal$Account==""]<-c("unclassified")
# Class, Accounts are factors
journal$Class<-as.factor(journal$Class)
journal$Account<-as.factor(journal$Account)
# Empty class should be NA
# Debit and Credit are numeric, without commas
journal$Debit<-as.numeric(sub(",","",journal$Debit))
journal$Credit<-as.numeric(sub(",","",journal$Credit))

## Temp function location
# TODO learn where to store functions
########## net() function, takes journal, class name(s), and date range as arguments
net<-function(journ,classselect="all",accselect="all",dates="all"){
  if("all" %in% classselect) {
    classselect<-levels(journ$Class)
  }
  if("all" %in% accselect) {
    accselect<-levels(journ$Account)
  }  
  if("all" %in% dates) {
    dates<-c(min(journ$Date),max(journ$Date))
  }
  net<-sum(journ$Credit[journ$Class %in% classselect & journ$Account %in% accselect & journ$Date>=dates[1] & journ$Date<=dates[2]],
           na.rm=T)-sum(journ$Debit[journ$Class %in% classselect & journ$Account %in% accselect & journ$Date>=dates[1] & journ$Date<=dates[2]],
                        na.rm=T)
  return(net)
}
##########

########## Net table by class
net.class<-function(journ,classselect="all",dates="all",total=F) {
# List of classes
  if("all" %in% classselect) {
    classselect<-levels(journ$Class)
  }
# Set up data frame
net.class.out<-data.frame(Class=character(),
                        Net=numeric(),
                        stringsAsFactors=FALSE)
# Loop through classes, calculate net, and add to data frame
for (i in 1:length(classselect)) {
  net.class.out[i,] <- c(classselect[i],
                       net(journ,
                           classselect=classselect[i],
                           dates="all"))
  }
# Add total net row
if(total) {
  net.class.out[nrow(net.class.out)+1,]<-c("Total",net(journal))
  }
return(net.class.out)
}
##########

#work in progress

########## Net table by account
net.acc<-function(journ,accselect="all",dates="all",total=F) {
# List of classes
  if("all" %in% accselect) {
    accselect<-unique(journ$Account)
  }
# Set up data frame
net.acc.out<-data.frame(Account=character(),
                        Net=numeric(),
                        stringsAsFactors=FALSE)
# Loop through accounts, calculate net, and add to data frame
for (i in 1:length(accselect)) {
  net.acc.out[i,] <- c(as.character(accselect[i]),
                       net(journ,
                           accselect=accselect[i],
                           dates=dates))
}
# Sort by account name
net.acc.out<-net.acc.out[order(Account) , ]
# Add total net row
if(total) {
  net.acc.out[nrow(net.acc.out)+1,]<-c("Total",net(journal))
  }
return(net.acc.out)
}
##########





# See other reports in QB and Ledger

```