orderDetails.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. let orderDetails = {
  2. display: function(order){
  3. document.getElementById("removeOrderBtn").onclick = ()=>{this.remove(order)};
  4. document.getElementById("orderDetailName").innerText = order.name;
  5. document.getElementById("orderDetailDate").innerText = order.date.toLocaleDateString("en-US");
  6. document.getElementById("orderDetailTime").innerText = order.date.toLocaleTimeString("en-US");
  7. let ingredientList = document.getElementById("orderIngredients");
  8. while(ingredientList.children.length > 0){
  9. ingredientList.removeChild(ingredientList.firstChild);
  10. }
  11. let template = document.getElementById("orderIngredient").content.children[0];
  12. let grandTotal = 0;
  13. for(let i = 0; i < order.ingredients.length; i++){
  14. let ingredientDiv = template.cloneNode(true);
  15. let price = order.ingredients[i].pricePerUnit * order.ingredients[i].quantity;
  16. grandTotal += price;
  17. const ingredient = order.ingredients[i].ingredient;
  18. ingredientDiv.children[0].innerText = order.ingredients[i].ingredient.name;
  19. ingredientDiv.children[2].innerText = `$${(price / 100).toFixed(2)}`;
  20. const ingredientDisplay = ingredientDiv.children[1];
  21. if(ingredient.specialUnit === "bottle"){
  22. const quantSold = order.ingredients[i].quantity / ingredient.unitSize;
  23. const ppu = (order.ingredients[i].pricePerUnit * order.ingredients[i].quantity) / quantSold;
  24. ingredientDisplay.innerText = `${quantSold.toFixed(0)} bottles x $${(ppu / 100).toFixed(2)}`;
  25. }else{
  26. const convertedQuantity = ingredient.convert(order.ingredients[i].quantity);
  27. const convertedPrice = controller.reconvertPrice(order.ingredients[i].ingredient.unitType, order.ingredients[i].ingredient.unit, order.ingredients[i].pricePerUnit);
  28. ingredientDisplay.innerText = `${convertedQuantity.toFixed(2)} ${ingredient.unit.toUpperCase()} x $${(convertedPrice / 100).toFixed(2)}`;
  29. }
  30. ingredientList.appendChild(ingredientDiv);
  31. }
  32. document.querySelector("#orderTotalPrice p").innerText = `$${(grandTotal / 100).toFixed(2)}`;
  33. },
  34. remove: function(order){
  35. let loader = document.getElementById("loaderContainer");
  36. loader.style.display = "flex";
  37. fetch(`/order/${order.id}`, {
  38. method: "DELETE",
  39. headers: {
  40. "Content-Type": "application/json;charset=utf-8"
  41. }
  42. })
  43. .then((response) => response.json())
  44. .then((response)=>{
  45. if(typeof(response) === "string"){
  46. banner.createError(response);
  47. }else{
  48. merchant.editOrders([order], true);
  49. banner.createNotification("ORDER REMOVED");
  50. }
  51. })
  52. .catch((err)=>{
  53. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  54. })
  55. .finally(()=>{
  56. loader.style.display = "none";
  57. });
  58. }
  59. }
  60. module.exports = orderDetails;