chnages of store proce. use and code optimize

This commit is contained in:
2026-01-22 01:36:21 +05:30
parent 7bdcee0656
commit 2a9a29d4cd
2 changed files with 71 additions and 83 deletions

View File

@@ -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"],)
cur.callproc("SaveOrUpdateMatCredit",(
data["financial_year"],
data["mat_credit"],
data["balance"]
))
result = next(cur.stored_results()).fetchone()
mat_id = result["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"])
)
row = cur.fetchone()
if row:
mat_id = row[0]
cur.execute("""
UPDATE mat_credit
SET mat_credit=%s, balance=%s
WHERE id=%s
""", (data["mat_credit"], data["balance"], mat_id))
cur.execute(
"DELETE FROM mat_utilization WHERE mat_credit_id=%s",
(mat_id,)
)
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()

28
main.py
View File

@@ -461,9 +461,10 @@ def summary_report():
@app.route("/mat_credit", methods=["GET"])
@auth.login_required
def mat_credit():
mat= MatCreditHandler()
mat = MatCreditHandler()
try:
mat_rows, utilization_rows = mat.fetch_all()
finally:
mat.close()
utilization_map = {}
@@ -486,23 +487,26 @@ def mat_credit():
@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