Order.js 6.0 KB

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