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 return mat_rows, utilization_rows
finally: finally:
self.cursor.close() self.cursor.close()
self.cursor.close()
# Save Mat credit data single row # Save Mat credit data single row
@staticmethod @staticmethod
def save_single(data): def save_single(data):
conn = DBConfig.get_db_connection() conn = DBConfig.get_db_connection()
cur = conn.cursor() cur = conn.cursor(dictionary=True)
try: try:
cur.execute(
"SELECT id FROM mat_credit WHERE financial_year=%s", cur.callproc("SaveOrUpdateMatCredit",(
(data["financial_year"],) 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() conn.commit()
except Exception: except Exception as e:
conn.rollback() conn.rollback()
raise raise e
finally: finally:
cur.close() cur.close()
conn.close() conn.close()
# save all Mat credit data # save all Mat credit data
@staticmethod # @staticmethod
def save_bulk(rows): # def save_bulk(rows):
conn = DBConfig.get_db_connection() # conn = DBConfig.get_db_connection()
cur = conn.cursor() # cur = conn.cursor()
skipped = [] # skipped = []
try: # try:
for row in rows: # for row in rows:
cur.execute( # cur.execute(
"SELECT id FROM mat_credit WHERE financial_year=%s", # "SELECT id FROM mat_credit WHERE financial_year=%s",
(row["financial_year"],) # (row["financial_year"],)
) # )
if cur.fetchone(): # if cur.fetchone():
skipped.append(row["financial_year"]) # skipped.append(row["financial_year"])
continue # continue
cur.execute(""" # cur.execute("""
INSERT INTO mat_credit (financial_year, mat_credit, balance) # INSERT INTO mat_credit (financial_year, mat_credit, balance)
VALUES (%s,%s,%s) # VALUES (%s,%s,%s)
""", (row["financial_year"], row["mat_credit"], row["balance"])) # """, (row["financial_year"], row["mat_credit"], row["balance"]))
mat_id = cur.lastrowid # mat_id = cur.lastrowid
for u in row["utilization"]: # for u in row["utilization"]:
cur.execute(""" # cur.execute("""
INSERT INTO mat_utilization # INSERT INTO mat_utilization
(mat_credit_id, utilized_year, utilized_amount) # (mat_credit_id, utilized_year, utilized_amount)
VALUES (%s,%s,%s) # VALUES (%s,%s,%s)
""", (mat_id, u["year"], u["amount"])) # """, (mat_id, u["year"], u["amount"]))
conn.commit() # conn.commit()
return skipped # return skipped
except Exception: # except Exception:
conn.rollback() # conn.rollback()
raise # raise
finally: # finally:
cur.close() # cur.close()
conn.close() # conn.close()
# CLOSE CONNECTION # CLOSE CONNECTION
def close(self): def close(self):
self.cursor.close() self.cursor.close()
self.conn.close() self.conn.close()

24
main.py
View File

@@ -461,9 +461,10 @@ def summary_report():
@app.route("/mat_credit", methods=["GET"]) @app.route("/mat_credit", methods=["GET"])
@auth.login_required @auth.login_required
def mat_credit(): def mat_credit():
mat = MatCreditHandler() mat = MatCreditHandler()
try:
mat_rows, utilization_rows = mat.fetch_all() mat_rows, utilization_rows = mat.fetch_all()
finally:
mat.close() mat.close()
utilization_map = {} utilization_map = {}
@@ -492,17 +493,20 @@ def save_mat_row():
return jsonify({"message": "Row saved successfully"}) return jsonify({"message": "Row saved successfully"})
except Exception as e: except Exception as e:
return jsonify({"error": str(e)}), 500 return jsonify({"error": str(e)}), 500
finally:
mat.close()
# save mat credit bulk data # save mat credit bulk data
@app.route("/save_mat_all", methods=["POST"]) # @app.route("/save_mat_all", methods=["POST"])
@auth.login_required # @auth.login_required
def save_mat_all(): # def save_mat_all():
mat= MatCreditHandler() # mat= MatCreditHandler()
try: # try:
skipped = mat.save_bulk(request.json) # skipped = mat.save_bulk(request.json)
return jsonify({"message": "Saved successfully", "skipped": skipped}) # return jsonify({"message": "Saved successfully", "skipped": skipped})
except Exception as e: # except Exception as e:
return jsonify({"error": str(e)}), 500 # return jsonify({"error": str(e)}), 500
# run server # run server