home.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. window.homeStrandObj = {
  2. isPopulated: false,
  3. display: function(){
  4. if(!this.isPopulated){
  5. let today = new Date();
  6. let firstOfMonth = new Date(today.getFullYear(), today.getMonth(), 1);
  7. let firstOfLastMonth = new Date(today.getFullYear(), today.getMonth() - 1, 1);
  8. let lastMonthtoDay = new Date(new Date().setMonth(today.getMonth() - 1));
  9. let revenueThisMonth = this.calculateRevenue(dateIndices(firstOfMonth));
  10. let revenueLastmonthToDay = this.calculateRevenue(dateIndices(firstOfLastMonth, lastMonthtoDay));
  11. document.querySelector("#revenue").innerText = `$${revenueThisMonth.toLocaleString("en")}`;
  12. let revenueChange = ((revenueThisMonth - revenueLastmonthToDay) / revenueLastmonthToDay) * 100;
  13. let img = "";
  14. if(revenueChange >= 0){
  15. img = "/shared/images/upArrow.png";
  16. }else{
  17. img = "/shared/images/downArrow.png";
  18. }
  19. document.querySelector("#revenueChange p").innerText = `${Math.abs(revenueChange).toFixed(2)}% vs last month`;
  20. document.querySelector("#revenueChange img").src = img;
  21. this.isPopulated = true;
  22. }
  23. },
  24. calculateRevenue: function(indices){
  25. let total = 0;
  26. for(let i = indices[0]; i <= indices[1]; i++){
  27. for(let recipe of transactions[i].recipes){
  28. for(let merchRecipe of merchant.recipes){
  29. if(recipe.recipe === merchRecipe._id){
  30. total += recipe.quantity * merchRecipe.price;
  31. }
  32. }
  33. }
  34. }
  35. return total / 100;
  36. }
  37. }