]> Softwares of Agnibho - librevax.git/commitdiff
Added transactions
authorAgnibho Mondal <mondal@agnibho.com>
Tue, 5 Mar 2024 17:39:12 +0000 (23:09 +0530)
committerAgnibho Mondal <mondal@agnibho.com>
Tue, 5 Mar 2024 17:39:12 +0000 (23:09 +0530)
librevax.py
templates/inventory-view.html
templates/transaction-edit.html [new file with mode: 0644]
templates/transaction-view.html [new file with mode: 0644]
transaction.py

index 3c109dee2078def61faf9e0061a8670170b06bd7..26dd90d6e7f6a258958cd9dfb216e272142d92b3 100644 (file)
@@ -309,6 +309,80 @@ def inventory_edit(mid=None, iid=None):
         raise(e)
         return render_template("error.html", data=e)
 
+@app.get("/transaction/<action>")
+@app.get("/transaction/<action>/<id>")
+def transaction_view(action, id=None):
+    if((ret:=problem())!="go"):
+        return ret
+    try:
+        cursor=get_db().cursor()
+        if(action=="list"):
+            if(id is not None):
+                (ok, data)=transaction.list(cursor, id)
+                if(not ok):
+                    raise Exception(data)
+                (ok, item)=inventory.read(cursor, id)
+                if(not ok):
+                    raise Exception(item)
+                return render_template("transaction-view.html", data=data, item=item, action=action)
+            else:
+                raise Exception("Invalid parameter")
+        elif(action=="view"):
+            if(id is not None):
+                (ok, data)=transaction.read(cursor, id)
+                if(not ok):
+                    raise Exception(data)
+                (ok, item)=inventory.read(cursor, data["iid"])
+                if(not ok):
+                    raise Exception(item)
+                return render_template("transaction-view.html", data=data, action=action, item=item)
+            else:
+                raise Exception("Invalid parameter")
+        elif(action=="edit"):
+            if(id is not None):
+                (ok, data)=transaction.read(cursor, id)
+                if(not ok):
+                    raise Exception(data)
+                (ok, item)=inventory.read(cursor, data["iid"])
+                if(not ok):
+                    raise Exception(item)
+                return render_template("transaction-edit.html", data=data, iid=data["iid"], action=action, item=item)
+            else:
+                raise Exception("Invalid parameter")
+        elif(action=="new"):
+            if(id is not None):
+                (ok, item)=inventory.read(cursor, id)
+                if(not ok):
+                    raise Exception(item)
+                data={"date":datetime.now().strftime("%Y-%m-%dT%H:%M:%S")}
+                return render_template("transaction-edit.html", data=data, iid=id, action=action, item=item)
+            else:
+                raise Exception("Invalid parameter")
+    except Exception as e:
+        raise(e)
+        return render_template("error.html", data=e)
+
+@app.post("/transaction/new/<iid>")
+@app.post("/transaction/edit/<tid>")
+def transaction_edit(iid=None, tid=None):
+    if((ret:=problem())!="go"):
+        return ret
+    try:
+        cursor=get_db().cursor()
+        if(tid is None):
+            (ok, data)=transaction.create(cursor, request.form)
+            if(not ok):
+                raise Exception(data)
+            tid=data
+        else:
+            (ok, data)=transaction.update(cursor, tid, request.form)
+            if(not ok):
+                raise Exception(data)
+        return redirect("/transaction/view/"+str(tid))
+    except Exception as e:
+        raise(e)
+        return render_template("error.html", data=e)
+
 @app.get("/patient/<action>")
 @app.get("/patient/<action>/<pid>")
 def patient_view(action, pid=None):
index bada27f2fde11b4381a5fdf0580a0a8c8f4767ba..3741dceb053dc6f462ad289b93c1022e88ab04f6 100644 (file)
@@ -41,6 +41,7 @@ You should have received a copy of the GNU General Public License along with Lib
     <tr><th>Available</th><td>{% if data["available"] %}Yes{% else %}No{% endif %}</td></tr>
   </table>
   <a href="/inventory/edit/{{data["iid"]}}" class="btn btn-primary">Edit</a>
+  <a href="/transaction/list/{{data["iid"]}}" class="btn btn-warning">Transactions</a>
   <a href="/delete/inventory/{{data["iid"]}}" class="btn btn-danger delete-link">Delete</a>
 
   {% endif %}
diff --git a/templates/transaction-edit.html b/templates/transaction-edit.html
new file mode 100644 (file)
index 0000000..36e386f
--- /dev/null
@@ -0,0 +1,50 @@
+<!--
+LibreVax
+Copyright (C) 2024 Dr. Agnibho Mondal
+This file is part of LibreVax.
+LibreVax is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
+LibreVax is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+You should have received a copy of the GNU General Public License along with LibreVax. If not, see <https://www.gnu.org/licenses/>.
+-->
+
+{% set title="Edit Transaction Record" %}
+{% extends "base.html" %}
+
+{% block content %}
+<div class="content">
+  <table class="table">
+    <tr><td>Item</td><td>{{item["vaccine"]}}</td></tr>
+  </table>
+  <form method="post">
+    <input type="hidden" name="iid" value="{{iid}}">
+    <div class="mb-2 row">
+      <label class="form-label col-sm-3">Action</label>
+      <div class="col-sm-9">
+        <select name="action" class="form-select">
+          <option>IN</option>
+          <option>OUT</option>
+        </select>
+      </div>
+    </div>
+    <div class="mb-2 row">
+      <label class="form-label col-sm-3">Date</label>
+      <div class="col-sm-9">
+        <input name="date" type="datetime-local" required class="form-control" value="{{data["date"]}}">
+      </div>
+    </div>
+    <div class="mb-2 row">
+      <label class="form-label col-sm-3">Amount</label>
+      <div class="col-sm-9">
+        <input name="target" type="number" required class="form-control" value="{{data["target"]}}">
+      </div>
+    </div>
+    <div class="mb-2 row">
+      <label class="form-label col-sm-3">Memo</label>
+      <div class="col-sm-9">
+        <textarea name="memo" placeholder="Memo" class="form-control">{{data["memo"]}}</textarea>
+      </div>
+    </div>
+    <button type="submit" class="btn btn-primary">Save</button>
+  </form>
+</div>
+{% endblock %}
diff --git a/templates/transaction-view.html b/templates/transaction-view.html
new file mode 100644 (file)
index 0000000..7805617
--- /dev/null
@@ -0,0 +1,51 @@
+<!--
+LibreVax
+Copyright (C) 2024 Dr. Agnibho Mondal
+This file is part of LibreVax.
+LibreVax is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
+LibreVax is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+You should have received a copy of the GNU General Public License along with LibreVax. If not, see <https://www.gnu.org/licenses/>.
+-->
+
+{% set title="Transaction Record" %}
+{% extends "base.html" %}
+
+{% block content %}
+
+<div class="content">
+  {% if action=="list" %}
+
+  <!-- LIST -->
+
+  <div class="row">
+    <div class="col-sm-10"><h2>{{item["vaccine"]}}</h2></div>
+    <div class="col-sm-2 d-grid"><a href="/transaction/new/{{item["iid"]}}" class="btn btn-outline-primary">Add Transaction</a></div>
+  </div>
+  <hr>
+  <table class="table">
+    <tr><th>Date</th><th>Type</th><th>Amount</th></tr>
+    {% for rec in data %}
+    <tr><td><a href="/transaction/view/{{rec["tid"]}}">{{rec["date"]|format_date}}</a></td><td><a href="/transaction/view/{{rec["tid"]}}">{{rec["action"]}}</a></td><td><a href="/transaction/view/{{rec["tid"]}}">{{rec["target"]}}</a></td></tr>
+    {% endfor %}
+  </table>
+
+  {% endif %}
+
+  {% if action=="view" %}
+
+  <!-- VIEW -->
+  <table class="table">
+    <tr><th>Item</th><td><a href="/inventory/view/{{item["iid"]}}">{{item["vaccine"]}} ({{item["batch"]}})</a></td></tr>
+    <tr><th>Type</th><td>{{data["action"]}}</td></tr>
+    <tr><th>Date</th><td>{{data["date"]|format_date}}</td></tr>
+    <tr><th>Amount</th><td>{{data["target"]}}</td></tr>
+    <tr><th>Memo</th><td>{{data["memo"]}}</td></tr>
+  </table>
+  <a href="/transaction/edit/{{data["tid"]}}" class="btn btn-primary">Edit</a>
+  <a href="/delete/transaction/{{data["tid"]}}" class="btn btn-danger delete-link">Delete</a>
+  </table>
+
+  {% endif %}
+</div>
+
+{% endblock %}
index 63e913cfb61b14fe0e6a72951b736cecfd8d53a3..95d6a7f9969cfab0a505c006910950581951c0e9 100644 (file)
@@ -47,7 +47,7 @@ def delete(cursor, tid):
 
 def list(cursor, iid):
     try:
-        result=cursor.execute("SELECT * FROM advice WHERE iid=?", (iid,))
+        result=cursor.execute("SELECT * FROM transactions WHERE iid=?", (iid,))
         if(cursor.rowcount==0):
             return (False, "Record not found.")
         else: