2026-01-07 16:26:59 +05:30
|
|
|
from AppCode.Config import DBConfig
|
|
|
|
|
import mysql.connector
|
|
|
|
|
|
|
|
|
|
class MatCreditHandler:
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
2026-01-07 17:22:18 +05:30
|
|
|
# db = DBConfig()
|
|
|
|
|
self.conn = DBConfig.get_db_connection()
|
2026-01-07 16:26:59 +05:30
|
|
|
self.cursor = self.conn.cursor(dictionary=True)
|
|
|
|
|
|
2026-01-07 17:22:18 +05:30
|
|
|
# get all Mat credit data
|
|
|
|
|
def fetch_all(self):
|
2026-01-07 16:26:59 +05:30
|
|
|
try:
|
2026-01-07 17:22:18 +05:30
|
|
|
self.cursor.callproc("GetMatCedit")
|
|
|
|
|
result_sets = self.cursor.stored_results()
|
|
|
|
|
mat_rows = next(result_sets).fetchall()
|
|
|
|
|
utilization_rows = next(result_sets).fetchall()
|
2026-01-07 16:26:59 +05:30
|
|
|
|
2026-01-07 17:22:18 +05:30
|
|
|
return mat_rows, utilization_rows
|
2026-01-07 16:26:59 +05:30
|
|
|
finally:
|
2026-01-07 17:22:18 +05:30
|
|
|
self.cursor.close()
|
2026-01-07 16:26:59 +05:30
|
|
|
|
2026-01-07 17:22:18 +05:30
|
|
|
# Save Mat credit data single row
|
2026-01-07 16:26:59 +05:30
|
|
|
@staticmethod
|
|
|
|
|
def save_single(data):
|
|
|
|
|
conn = DBConfig.get_db_connection()
|
2026-01-22 01:36:21 +05:30
|
|
|
cur = conn.cursor(dictionary=True)
|
2026-01-07 16:26:59 +05:30
|
|
|
try:
|
|
|
|
|
|
2026-01-22 01:36:21 +05:30
|
|
|
cur.callproc("SaveOrUpdateMatCredit",(
|
|
|
|
|
data["financial_year"],
|
|
|
|
|
data["mat_credit"],
|
|
|
|
|
data["balance"]
|
|
|
|
|
))
|
2026-01-07 16:26:59 +05:30
|
|
|
|
2026-01-22 01:36:21 +05:30
|
|
|
result = next(cur.stored_results()).fetchone()
|
|
|
|
|
mat_id = result["mat_id"]
|
2026-01-07 16:26:59 +05:30
|
|
|
|
2026-01-22 01:36:21 +05:30
|
|
|
if not mat_id:
|
|
|
|
|
raise Exception("mat_id not returned from procedure")
|
2026-01-07 16:26:59 +05:30
|
|
|
|
2026-01-22 01:36:21 +05:30
|
|
|
for u in data.get("utilization", []):
|
|
|
|
|
cur.callproc(
|
|
|
|
|
"InsertMatUtilization",
|
|
|
|
|
(mat_id, u["year"], u["amount"])
|
2026-01-07 16:26:59 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
conn.commit()
|
|
|
|
|
|
2026-01-22 01:36:21 +05:30
|
|
|
except Exception as e:
|
2026-01-07 16:26:59 +05:30
|
|
|
conn.rollback()
|
2026-01-22 01:36:21 +05:30
|
|
|
raise e
|
2026-01-07 16:26:59 +05:30
|
|
|
|
|
|
|
|
finally:
|
|
|
|
|
cur.close()
|
2026-01-07 17:22:18 +05:30
|
|
|
conn.close()
|
2026-01-22 01:36:21 +05:30
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
|
|
# 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()
|
2026-01-07 17:22:18 +05:30
|
|
|
|
|
|
|
|
# CLOSE CONNECTION
|
|
|
|
|
def close(self):
|
|
|
|
|
self.cursor.close()
|
|
|
|
|
self.conn.close()
|
2026-01-22 01:36:21 +05:30
|
|
|
|
|
|
|
|
|