title: Calculate Balance in Date range - PouchDB
body: <p>i am working on small billing application<br>
  i want to calculate the opening balance for the date range</p>
  <p>i have following fields in docs of pouch db data base</p>
  <p>date:2021-08-01 <br>
  sale:800<br>
  invest:500<br>
  expense: 100<br></p>
  <p>i want to calculate the opening balance by date range using pouchdb</p>
  <p>opening balance will be previous date closing balance</p>
  <p>calculation will be -&gt; <strong>closing = ( opening+investment+sale ) - exp</strong></p>
  <p>This table is example to calculate the balance for each day</p>
  <pre><code>date        opening     sale        invest     exp    closing
  2020-03-01  0           100.00      10.00      0      90.00
  2020-03-02  90.00       50.00       20.00      0      120.00     
  2020-03-02  120.0       100.00       0.00      0      220.00
  2020-03-03  220.00      200.00      20.00      0      400.00  
  2020-03-04  400.00      110.00      10.00      10     110.00
  </code></pre>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>// onchange function
  function getday(){
  
      // ---------------------------------------------opening---------------------------------------------
      var opening_balance = 0
      function daysale(n){
      
          daybook.find({
              selector: {
                  
                  Date:n ,
              
              }
          }).then(function(result) {  
              
             
          
              //chenking the data is present in  the selected date 
              if(result.docs.length&gt;0){
  
                  result.docs.forEach((element, ) =&gt; {
  
                      saletotal = (isNaN(element.TotalAmount) ? 0: parseInt(element.TotalAmount));
                      Totalinvest = (isNaN(element.TotalInvestment) ? 0: parseInt(element.TotalInvestment));
                      Totalexp = (isNaN(element.TotalExpenses) ? 0: parseInt(element.TotalExpenses));
  
                      opening_balance+=saletotal+Totalinvest-Totalexp
  
                  
                      document.getElementById('open-balance').value=opening_balance
  
                      
                  });
              } 
              // if there is no data on particular date subtracting one  day from  date from date and calling the 
              //get day function with yesterday value as parameter to check if the data is present in previos date
              else{
  
                  let d = new Date(n);
                  d.setDate(d.getDate() - 1);
                  var daybeforedate = d.toISOString().split('T')[0];
                  
                  //recursion function to calculate the previous date if there is no data in particular date
                  daysale(daybeforedate)
  
              }
              
          });
  
      }
      // get date value and minus one day 
      var  TodayDate = document.getElementById('daybook-firstdate').value;
      let d = new Date(TodayDate);
      d.setDate(d.getDate() - 1);
      var yesterdaydate = d.toISOString().split('T')[0];
     
      // function call for current date
      daysale(yesterdaydate)
      
      }</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>input{
  display:block;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;div id="daybookhandle"&gt;
         &lt;label&gt;From&lt;input type="date" name="daybook" id="daybook-firstdate" onchange="getday()" &gt;&lt;/label&gt;
         &lt;label&gt;To&lt;input type="date" name="daybook" id="daybook-firstdate" onchange="getday()" &gt;&lt;/label&gt;
         
         &lt;label&gt;Opening Balance:&lt;/label&gt;
          &lt;input type="text" name="balnce" id="open-balance"   &gt;
  
         &lt;label &gt;Investment : &lt;/label&gt;
          &lt;input type="text" name="ivest" id="invest-ment"&gt;
  
         &lt;label &gt;Expenses : &lt;/label&gt;
          &lt;input type="text" name="Exp" id="Expense_Total"&gt;
  
        &lt;label &gt;Total Sales : &lt;/label&gt;
        &lt;input type="text" name="sale" id="Sales_total"&gt;
  
         &lt;label &gt;Closing Balance :&lt;/label&gt;
          &lt;input type="text" name="cbalance" id="closing_balance"&gt;
     &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>
  <p>any one help me with a code
  if there is any more doubt please feel free to ask</p>