orderCalculator.js 1.9 KB

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