Order.js 5.3 KB

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