Order.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. class OrderIngredient{
  2. constructor(ingredient, quantity, pricePerUnit){
  3. this.ingredient = merchant.getIngredient(ingredient).ingredient;
  4. this._quantity = quantity;
  5. this._pricePerUnit = pricePerUnit;
  6. }
  7. get quantity(){
  8. switch(this.ingredient.unit){
  9. case "kg": return this._quantity / 1000;
  10. case "oz": return this._quantity / 28.3495;
  11. case "lb": return this._quantity / 453.5924;
  12. case "ml": return this._quantity * 1000;
  13. case "tsp": return this._quantity * 202.8842;
  14. case "tbsp": return this._quantity * 67.6278;
  15. case "ozfl": return this._quantity * 33.8141;
  16. case "cup": return this._quantity * 4.1667;
  17. case "pt": return this._quantity * 2.1134;
  18. case "qt": return this._quantity * 1.0567;
  19. case "gal": return this._quantity / 3.7854;
  20. case "mm": return this._quantity * 1000;
  21. case "cm": return this._quantity * 100;
  22. case "in": return this._quantity * 39.3701;
  23. case "ft": return this._quantity * 3.2808;
  24. case "bottle": return this._quantity * this.ingredient.convert.toBottle;
  25. default: return this._quantity;
  26. }
  27. }
  28. updateQuantity(quantity){
  29. quantity *= controller.unitMultiplier(unit, controller.getBaseUnit(unit))
  30. switch(controller.getUnitType(this.ingredient.unit)){
  31. case "mass": quantity /= this.ingredient.convert.toMass; break;
  32. case "volume": quantity /= this.ingredient.convert.toVolume; break;
  33. case "length": quantity /= this.ingredient.convert.toLength; break;
  34. }
  35. this._quantity += quantity;
  36. }
  37. get pricePerUnit(){
  38. switch(this.ingredient.unit){
  39. case "g": return this._pricePerUnit / 100;
  40. case "kg": return (this._pricePerUnit * 1000) / 100;
  41. case "oz": return (this._pricePerUnit * 28.3495) / 100;
  42. case "lb": return (this._pricePerUnit * 453.5924) / 100;
  43. case "ml": return (this._pricePerUnit / 1000) / 100;
  44. case "l": return this._pricePerUnit / 100;
  45. case "tsp": return (this._pricePerUnit / 202.8842) / 100;
  46. case "tbsp": return (this._pricePerUnit / 67.6278) / 100;
  47. case "ozfl": return (this._pricePerUnit / 33.8141) / 100;
  48. case "cup": return (this._pricePerUnit / 4.1667) / 100;
  49. case "pt": return (this._pricePerUnit / 2.1134) / 100;
  50. case "qt": return (this._pricePerUnit / 1.0567) / 100;
  51. case "gal": return (this._pricePerUnit * 3.7854) / 100;
  52. case "mm": return (this._pricePerUnit / 1000) / 100;
  53. case "cm": return (this._pricePerUnit / 100) / 100;
  54. case "m": return this._pricePerUnit / 100;
  55. case "in": return (this._pricePerUnit / 39.3701) / 100;
  56. case "ft": return (this._pricePerUnit / 3.2808) / 100;
  57. case "bottle": return (this._pricePerUnit / this.ingredient.convert.toBottle) / 100;
  58. default: return this._pricePerUnit / 100;
  59. }
  60. }
  61. get pricePerBaseUnit(){
  62. return this._pricePerUnit;
  63. }
  64. cost(){
  65. return (this._quantity * this._pricePerUnit) / 100;
  66. }
  67. }
  68. /*
  69. Order Object
  70. id = id of order in the database
  71. name = name/id of order, if any
  72. date = Date Object for when the order was created
  73. taxes = User entered taxes associated with the order
  74. fees = User entered fees associated with the order
  75. ingredients = [{
  76. ingredient: Ingredient ID,
  77. quantity: quantity of ingredient sold,
  78. pricePerUnit: price of purchase (per base unit)
  79. }]
  80. parent = the merchant that it belongs to
  81. */
  82. class Order{
  83. constructor(id, name, date, taxes, fees, ingredients, parent){
  84. this.id = id;
  85. this.name = name;
  86. this.date = new Date(date);
  87. this._taxes = taxes;
  88. this._fees = fees;
  89. this.ingredients = [];
  90. this.parent = parent;
  91. for(let i = 0; i < ingredients.length; i++){
  92. this.ingredients.push(new OrderIngredient(
  93. ingredients[i].ingredient,
  94. ingredients[i].quantity,
  95. ingredients[i].pricePerUnit
  96. ));
  97. }
  98. }
  99. get taxes(){
  100. return this._taxes / 100;
  101. }
  102. get fees(){
  103. return this._fees / 100;
  104. }
  105. get ingredients(){
  106. return this.ingredients;
  107. }
  108. getIngredientCost(){
  109. let sum = 0;
  110. for(let i = 0; i < this.ingredients.length; i++){
  111. sum += this.ingredients[i].cost();
  112. }
  113. return sum;
  114. }
  115. getTotalCost(){
  116. return (this.getIngredientCost() + this.taxes + this.fees);
  117. }
  118. }
  119. module.exports = Order;