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"],)
)
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()