ingredientDetails.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. let ingredientDetails = {
  2. ingredient: {},
  3. dailyUse: 0,
  4. display: function(ingredient){
  5. this.ingredient = ingredient;
  6. if(this.ingredient.ingredient.specialUnit === "bottle"){
  7. try{
  8. let label = document.getElementById("ingredientUnitSizeLabel");
  9. label.parentElement.removeChild(label);
  10. let border = document.getElementById("bottleBorder");
  11. border.parentElement.removeChild(border);
  12. }catch(err){}
  13. }
  14. document.getElementById("ingredientDetailsCategory").innerText = ingredient.ingredient.category;
  15. let categoryInput = document.getElementById("detailsCategoryInput");
  16. categoryInput.value = "";
  17. categoryInput.placeholder = ingredient.ingredient.category;
  18. document.getElementById("ingredientDetailsName").innerText = ingredient.ingredient.name;
  19. let nameInput = document.getElementById("ingredientDetailsNameIn");
  20. nameInput.value = "";
  21. nameInput.placeholder = ingredient.ingredient.name;
  22. //Display the stock quantity (and the edit input) based on the unit type
  23. let stockInput = document.getElementById("ingredientInput");
  24. let stockDisplay = document.getElementById("ingredientStock");
  25. stockInput.value = "";
  26. if(ingredient.ingredient.specialUnit === "bottle"){
  27. let quantity = ingredient.ingredient.convert(ingredient.quantity);
  28. stockDisplay.innerText = `${quantity.toFixed(2)} ${ingredient.ingredient.unit.toUpperCase()}`;
  29. stockDisplay.innerText = `${(ingredient.quantity / ingredient.ingredient.unitSize).toFixed(2)} BOTTLES`;
  30. stockInput.placeholder = quantity.toFixed(2);
  31. }else{
  32. stockDisplay.innerText = `${ingredient.ingredient.convert(ingredient.quantity).toFixed(2)} ${ingredient.ingredient.unit.toUpperCase()}`;
  33. stockInput.placeholder = ingredient.ingredient.convert(ingredient.quantity).toFixed(2);
  34. }
  35. let quantities = [];
  36. let now = new Date();
  37. for(let i = 1; i < 31; i++){
  38. let endDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i)
  39. let startDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i - 1);
  40. let indices = controller.transactionIndices(merchant.transactions, startDay, endDay);
  41. if(indices === false){
  42. quantities.push(0);
  43. }else{
  44. quantities.push(merchant.singleIngredientSold(indices, ingredient));
  45. }
  46. }
  47. let sum = 0;
  48. for(let i = 0; i < quantities.length; i++){
  49. sum += quantities[i];
  50. }
  51. let dailyUse = sum / quantities.length;
  52. document.getElementById("dailyUse").innerText = `${ingredient.ingredient.convert(dailyUse).toFixed(2)} ${ingredient.ingredient.unit.toUpperCase()}`;
  53. let ul = document.getElementById("ingredientRecipeList");
  54. let recipes = merchant.getRecipesForIngredient(ingredient.ingredient);
  55. while(ul.children.length > 0){
  56. ul.removeChild(ul.firstChild);
  57. }
  58. for(let i = 0; i < recipes.length; i++){
  59. let li = document.createElement("li");
  60. li.innerText = recipes[i].name;
  61. li.onclick = ()=>{
  62. controller.openStrand("recipeBook");
  63. controller.openSidebar("recipeDetails", recipes[i]);
  64. }
  65. ul.appendChild(li);
  66. }
  67. let ingredientButtons = document.getElementById("ingredientButtons");
  68. let units = [];
  69. if(this.ingredient.ingredient.unitType !== "other"){
  70. units = merchant.units[this.ingredient.ingredient.unitType];
  71. }
  72. while(ingredientButtons.children.length > 0){
  73. ingredientButtons.removeChild(ingredientButtons.firstChild);
  74. }
  75. for(let i = 0; i < units.length; i++){
  76. let button = document.createElement("button");
  77. button.classList.add("unitButton");
  78. button.innerText = units[i].toUpperCase();
  79. button.onclick = ()=>{this.changeUnit(button, units[i])};
  80. ingredientButtons.appendChild(button);
  81. if(units[i] === this.ingredient.ingredient.unit){
  82. button.classList.add("unitActive");
  83. }
  84. }
  85. let add = document.querySelectorAll(".editAdd");
  86. let remove = document.querySelectorAll(".editRemove");
  87. for(let i = 0; i < add.length; i++){
  88. add[i].style.display = "none";
  89. }
  90. for(let i = 0; i < remove.length; i++){
  91. remove[i].style.display = "block";
  92. }
  93. document.getElementById("editSubmitButton").onclick = ()=>{this.editSubmit()};
  94. document.getElementById("editCancelButton").onclick = ()=>{this.display(this.ingredient)};
  95. document.getElementById("editIngBtn").onclick = ()=>{this.edit()};
  96. document.getElementById("removeIngBtn").onclick = ()=>{this.remove()};
  97. },
  98. remove: function(){
  99. for(let i = 0; i < merchant.recipes.length; i++){
  100. for(let j = 0; j < merchant.recipes[i].ingredients.length; j++){
  101. if(this.ingredient.ingredient === merchant.recipes[i].ingredients[j].ingredient){
  102. banner.createError("MUST REMOVE INGREDIENT FROM ALL RECIPES BEFORE REMOVING FROM INVENTORY");
  103. return;
  104. }
  105. }
  106. }
  107. let loader = document.getElementById("loaderContainer");
  108. loader.style.display = "flex";
  109. fetch(`/ingredients/remove/${this.ingredient.ingredient.id}`, {
  110. method: "DELETE",
  111. })
  112. .then((response) => response.json())
  113. .then((response)=>{
  114. if(typeof(response) === "string"){
  115. banner.createError(response);
  116. }else{
  117. banner.createNotification("INGREDIENT REMOVED");
  118. merchant.editIngredients([this.ingredient], true);
  119. }
  120. })
  121. .catch((err)=>{})
  122. .finally(()=>{
  123. loader.style.display = "none";
  124. });
  125. },
  126. edit: function(){
  127. let add = document.querySelectorAll(".editAdd");
  128. let remove = document.querySelectorAll(".editRemove");
  129. for(let i = 0; i < add.length; i++){
  130. add[i].style.display = "flex";
  131. }
  132. for(let i = 0; i < remove.length; i++){
  133. remove[i].style.display = "none";
  134. }
  135. if(this.ingredient.ingredient.specialUnit === "bottle"){
  136. const mainDiv = document.getElementById("ingredientDetails");
  137. const displayUnits = document.getElementById("displayUnitLabel");
  138. let label = document.createElement("label");
  139. label.innerText = `BOTTLE SIZE (${this.ingredient.ingredient.unit.toUpperCase()})`;
  140. label.id = "ingredientUnitSizeLabel";
  141. mainDiv.insertBefore(label, displayUnits);
  142. const convQuant = this.ingredient.ingredient.convert(this.ingredient.ingredient.unitSize);
  143. let input = document.createElement("input");
  144. input.type = "number";
  145. input.value = convQuant.toFixed(2);
  146. input.id = "ingredientUnitSizeIn";
  147. input.min = "0";
  148. input.step = "0.01";
  149. label.appendChild(input);
  150. let border = document.createElement("div");
  151. border.classList.add("lineBorder");
  152. border.id="bottleBorder";
  153. mainDiv.insertBefore(border, displayUnits);
  154. }
  155. },
  156. editSubmit: function(){
  157. //Update the ingredient unit depending on the type of unit
  158. let ingredientButtons = document.querySelectorAll(".unitButton");
  159. for(let i = 0; i < ingredientButtons.length; i++){
  160. if(ingredientButtons[i].classList.contains("unitActive")){
  161. const unit = ingredientButtons[i].innerText.toLowerCase();
  162. this.ingredient.ingredient.unit = unit;
  163. break;
  164. }
  165. }
  166. //Update the ingredient quantity depending on the type of unit
  167. const quantityElem = document.getElementById("ingredientInput");
  168. if(quantityElem.value !== ""){
  169. if(this.ingredient.ingredient.specialUnit === "bottle"){
  170. let quantInMain = controller.convertToMain(
  171. this.ingredient.ingredient.unit,
  172. quantityElem.value * this.ingredient.ingredient.unitSize
  173. );
  174. this.ingredient.quantity = quantInMain / this.ingredient.ingredient.unitSize;
  175. }else{
  176. this.ingredient.quantity = controller.convertToMain(
  177. this.ingredient.ingredient.unit,
  178. Number(quantityElem.value)
  179. );
  180. }
  181. }
  182. const category = document.getElementById("detailsCategoryInput");
  183. this.ingredient.ingredient.category = (category.value === "") ? this.ingredient.ingredient.category : category.value;
  184. const name = document.getElementById("ingredientDetailsNameIn");
  185. this.ingredient.ingredient.name = (name.value === "") ? this.ingredient.ingredient.name : name.value;
  186. let data = {
  187. id: this.ingredient.ingredient.id,
  188. name: this.ingredient.ingredient.name,
  189. quantity: this.ingredient.quantity,
  190. category: this.ingredient.ingredient.category,
  191. defaultUnit: this.ingredient.ingredient.unit
  192. };
  193. if(this.ingredient.ingredient.specialUnit === "bottle"){
  194. const unitSize = document.getElementById("ingredientUnitSizeIn").value;
  195. this.ingredient.ingredient.unitSize = controller.convertToMain(this.ingredient.ingredient.unit, unitSize);
  196. data.unitSize = this.ingredient.ingredient.unitSize;
  197. }
  198. let loader = document.getElementById("loaderContainer");
  199. loader.style.display = "flex";
  200. fetch("/ingredients/update", {
  201. method: "PUT",
  202. headers: {
  203. "Content-Type": "application/json;charset=utf-8"
  204. },
  205. body: JSON.stringify(data)
  206. })
  207. .then((response) => response.json())
  208. .then((response)=>{
  209. if(typeof(response) === "string"){
  210. banner.createError(response);
  211. }else{
  212. merchant.editIngredients([this.ingredient]);
  213. this.display(this.ingredient);
  214. banner.createNotification("INGREDIENT UPDATED");
  215. }
  216. })
  217. .catch((err)=>{
  218. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  219. })
  220. .finally(()=>{
  221. loader.style.display = "none";
  222. });
  223. },
  224. changeUnit: function(newActive, unit){
  225. let ingredientButtons = document.querySelectorAll(".unitButton");
  226. for(let i = 0; i < ingredientButtons.length; i++){
  227. ingredientButtons[i].classList.remove("unitActive");
  228. }
  229. newActive.classList.add("unitActive");
  230. },
  231. changeUnitDefault: function(){
  232. let loader = document.getElementById("loaderContainer");
  233. loader.style.display = "flex";
  234. let id = this.ingredient.ingredient.id;
  235. let unit = this.ingredient.ingredient.unit;
  236. fetch(`/merchant/ingredients/update/${id}/${unit}`, {
  237. method: "put",
  238. headers: {
  239. "Content-Type": "application/json;charset=utf-8"
  240. },
  241. })
  242. .then((response)=>{
  243. if(typeof(response) === "string"){
  244. banner.createError(response);
  245. }else{
  246. banner.createNotification("INGREDIENT DEFAULT UNIT UPDATED");
  247. }
  248. })
  249. .catch((err)=>{
  250. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  251. })
  252. .finally(()=>{
  253. loader.style.display = "none";
  254. });
  255. }
  256. }
  257. module.exports = ingredientDetails;