high_sales <- trees %>%
  #mutate(Sales_Price = coalesce(as.numeric(Sales_Price), 0)) %>%
  mutate(Sales_Price = as.numeric(Sales_Price)) %>%
  select(Sales_Price) %>%
  count(Sales_Price) %>%
  mutate(Label = ifelse(Sales_Price >= 4500, "High Sale", "Low Sales")) %>%
  arrange(desc(Sales_Price))
## Warning: Problem with `mutate()` input `Sales_Price`.
## i NAs introduced by coercion
## i Input `Sales_Price` is `as.numeric(Sales_Price)`.
## Warning in mask$eval_all_mutate(dots[[i]]): NAs introduced by coercion
#view(high_sales)

high_sales
## # A tibble: 2,788 x 3
##    Sales_Price     n Label    
##          <dbl> <int> <chr>    
##  1      45000      1 High Sale
##  2      17760      1 High Sale
##  3      16334.     1 High Sale
##  4      14438.     1 High Sale
##  5      14392.     1 High Sale
##  6      10978.     1 High Sale
##  7      10745      1 High Sale
##  8      10581      1 High Sale
##  9       8835      1 High Sale
## 10       8607.     1 High Sale
## # … with 2,778 more rows
lostMoney <- cleanedtreesdf2 %>%
  filter(Amount < 0) %>%
  arrange((Amount))

lostMoneyItems <- lostMoney %>%
  count(Item) %>%
  arrange(desc(n))
lostMoneyItems
## # A tibble: 112 x 2
##    Item                     n
##    <chr>                <int>
##  1 Replacement Discount  1048
##  2 Customer Discount      227
##  3 Invoice Balancer       137
##  4 Misc. Labor             44
##  5 Installation            39
##  6 Donation                30
##  7 Extended Warranty       20
##  8 Tree Staking            11
##  9 Restaking Discount       9
## 10 Fertilization            7
## # … with 102 more rows
lostMoneyReplacements <- lostMoney %>%
  filter(Item == "Replacement Discount")

money_lost <- sum(lostMoney$Amount)
money_lost_replacement <- sum(lostMoneyReplacements$Amount)

cat("Total money lost: $", money_lost, "\n")
## Total money lost: $ -852663.1
cat("Total money lost from replacemnt: $", money_lost_replacement, "\n")
## Total money lost from replacemnt: $ -614929.9
gainedMoney <- cleanedtreesdf2 %>%
  filter(Amount > 0) %>%
  arrange((Amount))
gainedMoney
## # A tibble: 19,887 x 10
##       X1 Type  Date  Num   Ship_To_Address2 Ship_Zip Item    Qty Sales_Price
##    <dbl> <chr> <chr> <chr> <chr>            <chr>    <chr> <dbl> <chr>      
##  1    50 Invo… 8/5/… 9858  E Cobblestone C… 46725    Invo…     1 0.01       
##  2   764 Invo… 4/12… 1025  <NA>             <NA>     Invo…     1 0.01       
##  3   842 Invo… 4/15… 1039  <NA>             <NA>     Invo…     1 0.01       
##  4   858 Invo… 4/12… 1024  <NA>             <NA>     Invo…     1 0.01       
##  5  1194 Invo… 5/5/… 1117  <NA>             <NA>     Invo…     1 0.01       
##  6  1904 Invo… 9/20… 1362  <NA>             <NA>     Invo…     1 0.01       
##  7  1924 Invo… 5/26… 2474  <NA>             <NA>     Invo…     1 0.01       
##  8  2028 Invo… 7/12… 1283  <NA>             <NA>     Invo…     1 0.01       
##  9  2052 Invo… 8/2/… 1297  <NA>             <NA>     Invo…     1 0.01       
## 10  2262 Invo… 6/24… 1856  <NA>             <NA>     Invo…     1 0.01       
## # … with 19,877 more rows, and 1 more variable: Amount <dbl>
gainedMoneyItems <- gainedMoney %>%
  count(Item) %>%
  arrange(desc(n))
gainedMoneyItems
## # A tibble: 1,298 x 2
##    Item                                             n
##    <chr>                                        <int>
##  1 Installation                                  2761
##  2 Extended Warranty                             1330
##  3 Tree Staking                                  1239
##  4 Trip Charge                                    654
##  5 Fertilization                                  473
##  6 Trees:Arborvitae:6' Emerald Green Arborvitae   411
##  7 Delivery Fee                                   383
##  8 Trees:Spruce:6' Norway Spruce                  378
##  9 Trees:Spruce:6' White Spruce                   264
## 10 Misc. Labor                                    226
## # … with 1,288 more rows
#trees_sold <- gainedMoneyItems %>%
#  filter(str_detect(trees_sold$Item, "Trees:"))
#trees_sold

gained_money <- sum(gainedMoney$Amount)

cat("Total Gross Income: $", gained_money, "\n")
## Total Gross Income: $ 10778956
count_dates <- cleanedtreesdf2 %>%
  mutate(Date = as.Date(Date, format="%m/%d/%Y")) %>%
  mutate(year = lubridate::year(Date)) %>%
  count(year) %>%
  mutate(Label = paste("Sales")) %>%
  arrange(desc(year))
  

count_dates
## # A tibble: 10 x 3
##     year     n Label
##    <dbl> <int> <chr>
##  1  2023  1820 Sales
##  2  2022   360 Sales
##  3  2021  3919 Sales
##  4  2020  4239 Sales
##  5  2019  2978 Sales
##  6  2018  2116 Sales
##  7  2017  1960 Sales
##  8  2016  1929 Sales
##  9  2015  1721 Sales
## 10  2014  1060 Sales
#View(count_dates)

graph_dates <- cleanedtreesdf2 %>%
  ggplot(aes(x= Date, y = Qty, froup = 1, colour = Amount)) +
  geom_line() +
  labs(title = "Sale amount each day", 
      colour = "Date")

graph_dates
## Warning: Removed 111 row(s) containing missing values (geom_path).

count_years <- count_dates %>%
  filter(year >= 2014 & year <= 2023) %>%
  ggplot(aes(x = year, y = n)) +
  geom_line(stat = "identity") +
  labs(title = "Number of tree sales each year",
       x = "Year",
       y = "Sales")

print(count_years)

total_sales_no_address <- 0
total_sales_with_address <- 0

for (i in seq_along(cleanedtreesdf2$Ship_To_Address2)) {
  if (is.na(cleanedtreesdf2$Ship_To_Address2[i])) {
    #cat("Adress for sale ", cleanedtreesdf2$Num[i], " is blank", "\n") <- prints each individual sale WITHOUT address
    total_sales_no_address <- total_sales_no_address + 1
  } else {
    total_sales_with_address <- total_sales_with_address + 1
  }
}

cat("Total orders WITH address: ", total_sales_with_address, "\n")
## Total orders WITH address:  751
cat("Total orders WITHOUT address: ", total_sales_no_address, "\n")
## Total orders WITHOUT address:  21351