Просмотр исходного кода

Add code to only display current data for the current month

Lee Morgan 6 лет назад
Родитель
Сommit
f9333a0405
3 измененных файлов с 19 добавлено и 3 удалено
  1. 1 1
      controllers/transactionData.js
  2. 11 0
      views/inventoryPage/data.js
  3. 7 2
      views/inventoryPage/inventory.ejs

+ 1 - 1
controllers/transactionData.js

@@ -12,7 +12,7 @@ module.exports = {
         let firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
         let lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
 
-        Transaction.find({merchant: req.session.user})
+        Transaction.find({merchant: req.session.user, date: {$gte: firstDay, $lt: lastDay}})
             .then((transactions)=>{
                 return res.json(transactions);
             })

+ 11 - 0
views/inventoryPage/data.js

@@ -21,12 +21,19 @@ window.dataObj = {
             });
         }
 
+        console.log(transactions);
+
         //Populate number of recipes sold
+        let revenueTotal = 0;
+        let recipeTotal = 0;
         for(let transaction of transactions){
             for(let transactionRecipe of transaction.recipes){
                 for(let recipeCounter of recipes){
                     if(transactionRecipe === recipeCounter.id){
                         recipeCounter.quantity++;
+                        recipeTotal++;
+                        revenueTotal += (recipeCounter.price * recipeCounter.quantity);
+                        break;
                     }
                 }
             }
@@ -95,6 +102,10 @@ window.dataObj = {
             revenue.innerText = `$${recipe.quantity * recipe.price}`;
             row.appendChild(revenue);
         }
+
+        //Populate totals
+        document.querySelector("#revenueTotal").innerText = `$${revenueTotal.toFixed(2)}`;
+        document.querySelector("#soldTotal").innerText = recipeTotal;
     },
 
     populatePurchases: function(purchases){

+ 7 - 2
views/inventoryPage/inventory.ejs

@@ -77,6 +77,11 @@
                 <div id="recipesData" class="dataTable">
                     <h3>Recipes</h3>
 
+                    <div>
+                        <p>Total Revenue: <span id="revenueTotal"></span></p>
+                        <p>Total sold: <span id="soldTotal"></span></p>
+                    </div>
+
                     <table>
                         <thead>
                             <tr>
@@ -103,8 +108,6 @@
                         <tbody></tbody>
                     </table>
                 </div>
-
-                <!-- Totals -->
             </div>
         </div>
 
@@ -291,6 +294,7 @@
                     }
                 })
                 .catch((err)=>{
+                    console.log(err);
                     banner.createError("Error: unable to render sales data");
                 });
 
@@ -303,6 +307,7 @@
                     }
                 })
                 .catch((err)=>{
+                    console.log(err);
                     banner.createError("Error: unable to render purchases data");
                 })
         </script>