Order.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. }
  92. }
  93. cost(){
  94. return (this._quantity * this._pricePerUnit) / 100;
  95. }
  96. }
  97. /*
  98. Order Object
  99. id = id of order in the database
  100. name = name/id of order, if any
  101. date = Date Object for when the order was created
  102. taxes = User entered taxes associated with the order
  103. fees = User entered fees associated with the order
  104. ingredients = [{
  105. ingredient: Ingredient Object,
  106. quantity: quantity of ingredient sold,
  107. pricePerUnit: price of purchase (per base unit)
  108. }]
  109. parent = the merchant that it belongs to
  110. */
  111. class Order{
  112. constructor(id, name, date, taxes, fees, ingredients, parent){
  113. if(!this.isSanitaryString(name)){
  114. return false;
  115. }
  116. if(taxes < 0){
  117. return false;
  118. }
  119. this._id = id;
  120. this._name = name;
  121. this._date = new Date(date);
  122. this._taxes = taxes;
  123. this._fees = fees;
  124. this._ingredients = [];
  125. this._parent = parent;
  126. if(date > new Date()){
  127. return false;
  128. }
  129. for(let i = 0; i < ingredients.length; i++){
  130. for(let j = 0; j < merchant.ingredients.length; j++){
  131. if(merchant.ingredients[j].ingredient.id === ingredients[i].ingredient){
  132. this._ingredients.push(new OrderIngredient(
  133. merchant.ingredients[j].ingredient,
  134. ingredients[i].quantity,
  135. ingredients[i].pricePerUnit
  136. ));
  137. break;
  138. }
  139. }
  140. }
  141. this._parent.modules.ingredients.isPopulated = false;
  142. }
  143. get id(){
  144. return this._id;
  145. }
  146. get name(){
  147. return this._name;
  148. }
  149. get date(){
  150. return this._date;
  151. }
  152. get taxes(){
  153. return this._taxes / 100;
  154. }
  155. get fees(){
  156. return this._fees / 100;
  157. }
  158. get parent(){
  159. return this._parent;
  160. }
  161. get ingredients(){
  162. return this._ingredients;
  163. }
  164. getIngredientCost(){
  165. let sum = 0;
  166. for(let i = 0; i < this._ingredients.length; i++){
  167. sum += this._ingredients[i].cost();
  168. }
  169. return sum;
  170. }
  171. getTotalCost(){
  172. return (this.getIngredientCost() + this.taxes + this.fees);
  173. }
  174. isSanitaryString(str){
  175. let disallowed = ["\\", "<", ">", "$", "{", "}", "(", ")"];
  176. for(let i = 0; i < disallowed.length; i++){
  177. if(str.includes(disallowed[i])){
  178. return false;
  179. }
  180. }
  181. return true;
  182. }
  183. }
  184. module.exports = Order;