Order.js 4.8 KB

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