Order.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. const ingredients = require("../strands/ingredients.js");
  2. class OrderIngredient{
  3. constructor(ingredient, quantity, pricePerUnit){
  4. this._ingredient = ingredient;
  5. this._quantity = quantity;
  6. this._pricePerUnit = pricePerUnit;
  7. }
  8. get ingredient(){
  9. return this._ingredient;
  10. }
  11. get quantity(){
  12. switch(this._ingredient.unit){
  13. case "kg": return this._quantity / 1000;
  14. case "oz": return this._quantity / 28.3495;
  15. case "lb": return this._quantity / 453.5924;
  16. case "ml": return this._quantity * 1000;
  17. case "tsp": return this._quantity * 202.8842;
  18. case "tbsp": return this._quantity * 67.6278;
  19. case "ozfl": return this._quantity * 33.8141;
  20. case "cup": return this._quantity * 4.1667;
  21. case "pt": return this._quantity * 2.1134;
  22. case "qt": return this._quantity * 1.0567;
  23. case "gal": return this._quantity / 3.7854;
  24. case "mm": return this._quantity * 1000;
  25. case "cm": return this._quantity * 100;
  26. case "in": return this._quantity * 39.3701;
  27. case "ft": return this._quantity * 3.2808;
  28. default: return this._quantity;
  29. }
  30. }
  31. updateQuantity(quantity){
  32. this._quantity += this.convertToBase(quantity);
  33. }
  34. convertToBase(quantity){
  35. switch(this._ingredient.unit){
  36. case "g": return quantity;
  37. case "kg": return quantity * 1000;
  38. case "oz": return quantity * 28.3495;
  39. case "lb": return quantity * 453.5924;
  40. case "ml": return quantity / 1000;
  41. case "l": return quantity;
  42. case "tsp": return quantity / 202.8842;
  43. case "tbsp": return quantity / 67.6278;
  44. case "ozfl": return quantity / 33.8141;
  45. case "cup": return quantity / 4.1667;
  46. case "pt": return quantity / 2.1134;
  47. case "qt": return quantity / 1.0567;
  48. case "gal": return quantity * 3.7854;
  49. case "mm": return quantity / 1000;
  50. case "cm": return quantity / 100;
  51. case "m": return quantity;
  52. case "in": return quantity / 39.3701;
  53. case "ft": return quantity / 3.2808;
  54. default: return quantity;
  55. }
  56. }
  57. get pricePerUnit(){
  58. switch(this._ingredient.unit){
  59. case "g": return this._pricePerUnit / 100;
  60. case "kg": return (this._pricePerUnit * 1000) / 100;
  61. case "oz": return (this._pricePerUnit * 28.3495) / 100;
  62. case "lb": return (this._pricePerUnit * 453.5924) / 100;
  63. case "ml": return (this._pricePerUnit / 1000) / 100;
  64. case "l": return this._pricePerUnit / 100;
  65. case "tsp": return (this._pricePerUnit / 202.8842) / 100;
  66. case "tbsp": return (this._pricePerUnit / 67.6278) / 100;
  67. case "ozfl": return (this._pricePerUnit / 33.8141) / 100;
  68. case "cup": return (this._pricePerUnit / 4.1667) / 100;
  69. case "pt": return (this._pricePerUnit / 2.1134) / 100;
  70. case "qt": return (this._pricePerUnit / 1.0567) / 100;
  71. case "gal": return (this._pricePerUnit * 3.7854) / 100;
  72. case "mm": return (this._pricePerUnit / 1000) / 100;
  73. case "cm": return (this._pricePerUnit / 100) / 100;
  74. case "m": return this._pricePerUnit / 100;
  75. case "in": return (this._pricePerUnit / 39.3701) / 100;
  76. case "ft": return (this._pricePerUnit / 3.2808) / 100;
  77. default: return this._pricePerUnit / 100;
  78. }
  79. }
  80. cost(){
  81. return (this._quantity * this._pricePerUnit) / 100;
  82. }
  83. }
  84. /*
  85. Order Object
  86. id = id of order in the database
  87. name = name/id of order, if any
  88. date = Date Object for when the order was created
  89. taxes = User entered taxes associated with the order
  90. fees = User entered fees associated with the order
  91. ingredients = [{
  92. ingredient: Ingredient Object,
  93. quantity: quantity of ingredient sold,
  94. pricePerUnit: price of purchase (per base unit)
  95. }]
  96. parent = the merchant that it belongs to
  97. */
  98. class Order{
  99. constructor(id, name, date, taxes, fees, ingredients, parent){
  100. this._id = id;
  101. this._name = name;
  102. this._date = new Date(date);
  103. this._taxes = taxes;
  104. this._fees = fees;
  105. this._ingredients = [];
  106. this._parent = parent;
  107. for(let i = 0; i < ingredients.length; i++){
  108. for(let j = 0; j < merchant.ingredients.length; j++){
  109. if(merchant.ingredients[j].ingredient.id === ingredients[i].ingredient){
  110. let thing = new OrderIngredient(
  111. merchant.ingredients[j].ingredient,
  112. ingredients[i].quantity,
  113. ingredients[i].pricePerUnit
  114. );
  115. this._ingredients.push(thing);
  116. break;
  117. }
  118. }
  119. }
  120. ingredients.isPopulated = false;
  121. }
  122. get id(){
  123. return this._id;
  124. }
  125. get name(){
  126. return this._name;
  127. }
  128. get date(){
  129. return this._date;
  130. }
  131. get taxes(){
  132. return this._taxes / 100;
  133. }
  134. get fees(){
  135. return this._fees / 100;
  136. }
  137. get parent(){
  138. return this._parent;
  139. }
  140. get ingredients(){
  141. return this._ingredients;
  142. }
  143. getIngredientCost(){
  144. let sum = 0;
  145. for(let i = 0; i < this._ingredients.length; i++){
  146. sum += this._ingredients[i].cost();
  147. }
  148. return sum;
  149. }
  150. getTotalCost(){
  151. return (this.getIngredientCost() + this.taxes + this.fees);
  152. }
  153. }
  154. module.exports = Order;