From: Agnibho Mondal <mondal@agnibho.com>
Date: Tue, 21 Jan 2025 20:03:40 +0000 (+0530)
Subject: Date range in report
X-Git-Url: https://code.agnibho.com/repo?a=commitdiff_plain;h=8a3f202291fca18fe1ce09088f7644d008662390;p=librevax.git

Date range in report
---

diff --git a/librevax.py b/librevax.py
index ba2bf1c..56bf9e3 100644
--- a/librevax.py
+++ b/librevax.py
@@ -824,9 +824,19 @@ def report(mid):
     if((ret:=problem())!="go"):
         return ret
     try:
-        date=request.args.get("date", datetime.now().strftime("%Y-%m-%d"))
+
+        def get_valid_date(date_str):
+            today=datetime.now().strftime("%Y-%m-%d")
+            try:
+                return datetime.strptime(date_str, "%Y-%m-%d").strftime("%Y-%m-%d")
+            except (ValueError, TypeError):
+                return today
+
+        fromDate=get_valid_date(request.args.get("fromDate"))
+        toDate=get_valid_date(request.args.get("toDate"))
+
         cursor=get_db().cursor()
-        (ok, data)=vaccination.list_by_date(cursor, date)
+        (ok, data)=vaccination.list_by_date(cursor, fromDate, toDate)
         if(not ok):
             raise Exception(data)
         (ok, center)=multicenter.read(cursor, g.mid)
@@ -840,7 +850,7 @@ def report(mid):
                 count[i["vaccine"]]=count[i["vaccine"]]+i["dosage"]
             except KeyError:
                 count[i["vaccine"]]=i["dosage"]
-        return render_template("report.html", data=data, count=count, individual=individual, center=center, date=date)
+        return render_template("report.html", data=data, count=count, individual=individual, center=center, fromDate=fromDate, toDate=toDate)
     except Exception as e:
         raise(e)
         return render_template("error.html", data=e)
diff --git a/templates/report.html b/templates/report.html
index 911f5d5..5e72738 100644
--- a/templates/report.html
+++ b/templates/report.html
@@ -14,16 +14,19 @@ You should have received a copy of the GNU General Public License along with Lib
 <div class="content report">
   <form>
     <div class="mb-2 row">
-      <div class="col-sm-9">
-        <input name="date" type="date" class="form-control">
+      <div class="col-sm-4">
+        <input name="fromDate" type="date" class="form-control" title="From this date">
       </div>
-      <div class="col-sm-3 d-grid">
+      <div class="col-sm-4">
+        <input name="toDate" type="date" class="form-control" title="To this date">
+      </div>
+      <div class="col-sm-2 d-grid">
         <button type="submit" class="btn btn-primary">Report</button>
       </div>
     </div>
   </form>
   <table class="table">
-    <tr><td>{{config["TITLE"]}} / {{center["center"]}}</td><td>Vaccination Report: {{date}}</td></tr>
+    <tr><td>{{config["TITLE"]}} / {{center["center"]}}</td><td>Vaccination Report: {{toDate}} to {{fromDate}}</td></tr>
   </table>
   <table class="table table-bordered">
     {% for i in data %}
diff --git a/vaccination.py b/vaccination.py
index a92f3cc..a219baf 100644
--- a/vaccination.py
+++ b/vaccination.py
@@ -75,9 +75,9 @@ def list_by_inventory(cursor, iid):
     except Exception as e:
         return (False, e)
 
-def list_by_date(cursor, date):
+def list_by_date(cursor, fromDate, toDate):
     try:
-        result=cursor.execute("SELECT * FROM vaccination LEFT JOIN inventory ON vaccination.iid=inventory.iid LEFT JOIN patients ON vaccination.pid=patients.pid WHERE date LIKE ? ORDER BY vaccination.iid", (date+"%",))
+        result=cursor.execute("SELECT * FROM vaccination LEFT JOIN inventory ON vaccination.iid=inventory.iid LEFT JOIN patients ON vaccination.pid=patients.pid WHERE date BETWEEN ? AND ? ORDER BY vaccination.iid", (fromDate, toDate))
         if(cursor.rowcount==0):
             return (False, "Record not found.")
         else: