Order.js 6.2 KB

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