orderCalculator.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. let orderCalculator = {
  2. display: function(){
  3. let calculatorItems = document.getElementById("calculatorItemsBody");
  4. let template = document.getElementById("calculatorItem").content.children[0];
  5. let calculations = this.predict();
  6. while(calculatorItems.children.length > 0){
  7. calculatorItems.removeChild(calculatorItems.firstChild);
  8. }
  9. for(let i = 0; i < calculations.length; i++){
  10. let outputString = `${calculations[i].output.toFixed(2)} ${calculations[i].ingredient.unit.toUpperCase()}`;
  11. let item = template.cloneNode(true);
  12. item.children[0].innerText = calculations[i].ingredient.name,
  13. item.children[1].innerText = outputString;
  14. calculatorItems.appendChild(item);
  15. }
  16. },
  17. predict: function(){
  18. let now = new Date();
  19. let yesterday = new Date();
  20. yesterday.setHours(0, 0, 0, 0);
  21. let monthAgo = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 30);
  22. let weekAgo = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 7);
  23. let calculations = [];
  24. let month = merchant.getIngredientsSold(monthAgo, yesterday);
  25. let week = merchant.getIngredientsSold(weekAgo, yesterday);
  26. let weights = {
  27. month: 0.33,
  28. week: 0.67
  29. }
  30. for(let i = 0; i < month.length; i++){
  31. for(let j = 0; j < week.length; j++){
  32. if(month[i].ingredient.id === week[j].ingredient.id){
  33. let monthAverage = (month[i].quantity / 30) * weights.month;
  34. let weekAverage = (week[i].quantity / 7) * weights.week;
  35. let calc = {
  36. ingredient: month[i].ingredient,
  37. output: monthAverage + weekAverage
  38. };
  39. calculations.push(calc);
  40. }
  41. }
  42. }
  43. return calculations;
  44. }
  45. }
  46. module.exports = orderCalculator;