update itr and ao from and auto save mat ceadit and utility

This commit is contained in:
2026-02-06 15:11:58 +05:30
parent a0f4568ba2
commit a9af2cde8a
43 changed files with 825 additions and 327 deletions

View File

@@ -1,15 +1,18 @@
from AppCode.Config import DBConfig
class MatCreditHandler:
def __init__(self):
# db = DBConfig()
self.conn = DBConfig.get_db_connection()
self.cursor = self.conn.cursor(dictionary=True)
# get all Mat credit data
# --------------------------------------------------
# FETCH ALL MAT CREDIT + UTILIZATION (For UI Display)
# --------------------------------------------------
def fetch_all(self):
try:
self.cursor.callproc("GetMatCedit")
result_sets = self.cursor.stored_results()
mat_rows = next(result_sets).fetchall()
@@ -18,30 +21,78 @@ class MatCreditHandler:
return mat_rows, utilization_rows
finally:
self.cursor.close()
self.conn.close()
# Save Mat credit data single row
# --------------------------------------------------
# SAVE / UPDATE SINGLE MAT ROW (FROM MANUAL UI)
# --------------------------------------------------
@staticmethod
def save_single(data):
conn = DBConfig.get_db_connection()
cur = conn.cursor(dictionary=True)
try:
# Save / Update MAT Credit
cur.callproc(
"SaveOrUpdateMatCredit",
(
data["financial_year"],
data["mat_credit"],
data["balance"],
data.get("remarks", "")
)
)
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")
mat_id = None
for result in cur.stored_results():
mat_id = result.fetchone()["mat_id"]
# Save utilization rows
for u in data.get("utilization", []):
if float(u["amount"]) > 0:
cur.callproc(
"InsertMatUtilization",
(mat_id, u["year"], u["amount"])
)
conn.commit()
except Exception as e:
conn.rollback()
raise e
finally:
cur.close()
conn.close()
# --------------------------------------------------
# AUTO SAVE MAT FROM ITR (MAIN LOGIC)
# --------------------------------------------------
@staticmethod
def save_from_itr(year, mat_created, mat_utilized, remarks="Auto from"):
conn = DBConfig.get_db_connection()
cur = conn.cursor(dictionary=True)
try:
mat_created = float(mat_created or 0)
mat_utilized = float(mat_utilized or 0)
balance = mat_created - mat_utilized
# Save / Update MAT Credit
cur.callproc(
"SaveOrUpdateMatCredit",
(year, mat_created, balance, remarks)
)
mat_id = None
for result in cur.stored_results():
mat_id = result.fetchone()["mat_id"]
# Save utilization only if used
if mat_utilized > 0:
cur.callproc(
"InsertMatUtilization",
(mat_id, u["year"], u["amount"])
(mat_id, year, mat_utilized)
)
conn.commit()
@@ -49,56 +100,29 @@ class MatCreditHandler:
except Exception as e:
conn.rollback()
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 = []
# 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
# --------------------------------------------------
# DELETE MAT CREDIT SAFELY (OPTIONAL)
# --------------------------------------------------
def delete_by_year(self, financial_year):
try:
self.cursor.execute(
"DELETE FROM mat_credit WHERE financial_year=%s",
(financial_year,)
)
self.conn.commit()
finally:
self.cursor.close()
self.conn.close()
# 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
# 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
# except Exception:
# conn.rollback()
# raise
# finally:
# cur.close()
# conn.close()
# CLOSE CONNECTION
# --------------------------------------------------
# CLOSE CONNECTION (MANUAL USE)
# --------------------------------------------------
def close(self):
self.cursor.close()
self.conn.close()
if self.cursor:
self.cursor.close()
if self.conn:
self.conn.close()