chnages of store proce. use and code optimize
This commit is contained in:
@@ -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",
|
|
||||||
(data["financial_year"],)
|
|
||||||
)
|
|
||||||
row = cur.fetchone()
|
|
||||||
|
|
||||||
if row:
|
cur.callproc("SaveOrUpdateMatCredit",(
|
||||||
mat_id = row[0]
|
data["financial_year"],
|
||||||
|
data["mat_credit"],
|
||||||
|
data["balance"]
|
||||||
|
))
|
||||||
|
|
||||||
cur.execute("""
|
result = next(cur.stored_results()).fetchone()
|
||||||
UPDATE mat_credit
|
mat_id = result["mat_id"]
|
||||||
SET mat_credit=%s, balance=%s
|
|
||||||
WHERE id=%s
|
|
||||||
""", (data["mat_credit"], data["balance"], mat_id))
|
|
||||||
|
|
||||||
cur.execute(
|
if not mat_id:
|
||||||
"DELETE FROM mat_utilization WHERE mat_credit_id=%s",
|
raise Exception("mat_id not returned from procedure")
|
||||||
(mat_id,)
|
|
||||||
|
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()
|
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()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
34
main.py
34
main.py
@@ -461,10 +461,11 @@ 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()
|
||||||
mat.close()
|
finally:
|
||||||
|
mat.close()
|
||||||
|
|
||||||
utilization_map = {}
|
utilization_map = {}
|
||||||
all_years = set()
|
all_years = set()
|
||||||
@@ -482,27 +483,30 @@ def mat_credit():
|
|||||||
added_years=sorted(all_years)
|
added_years=sorted(all_years)
|
||||||
)
|
)
|
||||||
|
|
||||||
# save mat credit row data
|
# save mat credit row data
|
||||||
@app.route("/save_mat_row", methods=["POST"])
|
@app.route("/save_mat_row", methods=["POST"])
|
||||||
@auth.login_required
|
@auth.login_required
|
||||||
def save_mat_row():
|
def save_mat_row():
|
||||||
mat= MatCreditHandler()
|
mat = MatCreditHandler()
|
||||||
try:
|
try:
|
||||||
mat.save_single(request.json)
|
mat.save_single(request.json)
|
||||||
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
|
||||||
|
|||||||
Reference in New Issue
Block a user