ingredientDetails.js 12 KB

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