diff --git a/AppCode/MatCreditHandler.py b/AppCode/MatCreditHandler.py index 076ea5e..2fa0a9b 100644 --- a/AppCode/MatCreditHandler.py +++ b/AppCode/MatCreditHandler.py @@ -19,103 +19,87 @@ class MatCreditHandler: return mat_rows, utilization_rows finally: self.cursor.close() - self.cursor.close() # Save Mat credit data single row @staticmethod def save_single(data): conn = DBConfig.get_db_connection() - cur = conn.cursor() - + cur = conn.cursor(dictionary=True) try: - cur.execute( - "SELECT id FROM mat_credit WHERE financial_year=%s", - (data["financial_year"],) - ) - row = cur.fetchone() - if row: - mat_id = row[0] + cur.callproc("SaveOrUpdateMatCredit",( + data["financial_year"], + data["mat_credit"], + data["balance"] + )) - cur.execute(""" - UPDATE mat_credit - SET mat_credit=%s, balance=%s - WHERE id=%s - """, (data["mat_credit"], data["balance"], mat_id)) + result = next(cur.stored_results()).fetchone() + mat_id = result["mat_id"] - cur.execute( - "DELETE FROM mat_utilization WHERE mat_credit_id=%s", - (mat_id,) + if not mat_id: + raise Exception("mat_id not returned from procedure") + + for u in data.get("utilization", []): + cur.callproc( + "InsertMatUtilization", + (mat_id, u["year"], u["amount"]) ) - else: - cur.execute(""" - INSERT INTO mat_credit (financial_year, mat_credit, balance) - VALUES (%s,%s,%s) - """, (data["financial_year"], data["mat_credit"], data["balance"])) - - mat_id = cur.lastrowid - - for u in data["utilization"]: - cur.execute(""" - INSERT INTO mat_utilization - (mat_credit_id, utilized_year, utilized_amount) - VALUES (%s,%s,%s) - """, (mat_id, u["year"], u["amount"])) conn.commit() - except Exception: + except Exception as e: conn.rollback() - raise + raise e finally: cur.close() conn.close() - # save all Mat credit data - @staticmethod - def save_bulk(rows): - conn = DBConfig.get_db_connection() - cur = conn.cursor() - skipped = [] + # @staticmethod + # def save_bulk(rows): + # conn = DBConfig.get_db_connection() + # cur = conn.cursor() + # skipped = [] - try: - for row in rows: - cur.execute( - "SELECT id FROM mat_credit WHERE financial_year=%s", - (row["financial_year"],) - ) - if cur.fetchone(): - skipped.append(row["financial_year"]) - continue + # try: + # for row in rows: + # cur.execute( + # "SELECT id FROM mat_credit WHERE financial_year=%s", + # (row["financial_year"],) + # ) + # if cur.fetchone(): + # skipped.append(row["financial_year"]) + # continue - cur.execute(""" - INSERT INTO mat_credit (financial_year, mat_credit, balance) - VALUES (%s,%s,%s) - """, (row["financial_year"], row["mat_credit"], row["balance"])) + # cur.execute(""" + # INSERT INTO mat_credit (financial_year, mat_credit, balance) + # VALUES (%s,%s,%s) + # """, (row["financial_year"], row["mat_credit"], row["balance"])) - mat_id = cur.lastrowid + # mat_id = cur.lastrowid - for u in row["utilization"]: - cur.execute(""" - INSERT INTO mat_utilization - (mat_credit_id, utilized_year, utilized_amount) - VALUES (%s,%s,%s) - """, (mat_id, u["year"], u["amount"])) + # for u in row["utilization"]: + # cur.execute(""" + # INSERT INTO mat_utilization + # (mat_credit_id, utilized_year, utilized_amount) + # VALUES (%s,%s,%s) + # """, (mat_id, u["year"], u["amount"])) - conn.commit() - return skipped + # conn.commit() + # return skipped - except Exception: - conn.rollback() - raise + # except Exception: + # conn.rollback() + # raise - finally: - cur.close() - conn.close() + # finally: + # cur.close() + # conn.close() # CLOSE CONNECTION def close(self): self.cursor.close() self.conn.close() + + diff --git a/main.py b/main.py index 8b0eddd..a0c5e77 100644 --- a/main.py +++ b/main.py @@ -461,10 +461,11 @@ def summary_report(): @app.route("/mat_credit", methods=["GET"]) @auth.login_required def mat_credit(): - - mat= MatCreditHandler() - mat_rows, utilization_rows = mat.fetch_all() - mat.close() + mat = MatCreditHandler() + try: + mat_rows, utilization_rows = mat.fetch_all() + finally: + mat.close() utilization_map = {} all_years = set() @@ -482,27 +483,30 @@ def mat_credit(): added_years=sorted(all_years) ) -# save mat credit row data +# save mat credit row data @app.route("/save_mat_row", methods=["POST"]) @auth.login_required def save_mat_row(): - mat= MatCreditHandler() + mat = MatCreditHandler() try: mat.save_single(request.json) return jsonify({"message": "Row saved successfully"}) except Exception as e: return jsonify({"error": str(e)}), 500 + finally: + mat.close() + # save mat credit bulk data -@app.route("/save_mat_all", methods=["POST"]) -@auth.login_required -def save_mat_all(): - mat= MatCreditHandler() - try: - skipped = mat.save_bulk(request.json) - return jsonify({"message": "Saved successfully", "skipped": skipped}) - except Exception as e: - return jsonify({"error": str(e)}), 500 +# @app.route("/save_mat_all", methods=["POST"]) +# @auth.login_required +# def save_mat_all(): +# mat= MatCreditHandler() +# try: +# skipped = mat.save_bulk(request.json) +# return jsonify({"message": "Saved successfully", "skipped": skipped}) +# except Exception as e: +# return jsonify({"error": str(e)}), 500 # run server