379 lines
10 KiB
Python
379 lines
10 KiB
Python
import config
|
|
import mysql.connector
|
|
|
|
# ------------------- Helper -------------------
|
|
def clear_results(cursor):
|
|
for r in cursor.stored_results():
|
|
r.fetchall()
|
|
|
|
|
|
# ------------------- Get Village Id -------------------
|
|
def get_village_id(village_name):
|
|
connection = config.get_db_connection()
|
|
cursor = connection.cursor(dictionary=True)
|
|
|
|
cursor.callproc("GetVillageIdByName", (village_name,))
|
|
village_result = None
|
|
|
|
for rs in cursor.stored_results():
|
|
village_result = rs.fetchone()
|
|
|
|
cursor.close()
|
|
connection.close()
|
|
return village_result
|
|
|
|
|
|
# ------------------- Insert Invoice -------------------
|
|
def insert_invoice(data, village_id):
|
|
connection = config.get_db_connection()
|
|
cursor = connection.cursor(dictionary=True)
|
|
|
|
try:
|
|
# 1. Insert Invoice
|
|
cursor.callproc('InsertInvoice', [
|
|
data.get('pmc_no'),
|
|
village_id,
|
|
data.get('work_type'),
|
|
data.get('invoice_details'),
|
|
data.get('invoice_date'),
|
|
data.get('invoice_no'),
|
|
float(data.get('basic_amount') or 0),
|
|
float(data.get('debit_amount') or 0),
|
|
float(data.get('after_debit_amount') or 0),
|
|
float(data.get('amount') or 0),
|
|
float(data.get('gst_amount') or 0),
|
|
float(data.get('tds_amount') or 0),
|
|
float(data.get('sd_amount') or 0),
|
|
float(data.get('on_commission') or 0),
|
|
float(data.get('hydro_testing') or 0),
|
|
float(data.get('gst_sd_amount') or 0),
|
|
float(data.get('final_amount') or 0)
|
|
])
|
|
|
|
invoice_id = None
|
|
for result in cursor.stored_results():
|
|
row = result.fetchone()
|
|
if row:
|
|
invoice_id = row.get('invoice_id')
|
|
|
|
if not invoice_id:
|
|
raise Exception("Invoice ID not returned")
|
|
|
|
# 2. Insert Inpayment
|
|
cursor.callproc('InsertInpayment', [
|
|
data.get('pmc_no'),
|
|
village_id,
|
|
data.get('work_type'),
|
|
data.get('invoice_details'),
|
|
data.get('invoice_date'),
|
|
data.get('invoice_no'),
|
|
float(data.get('basic_amount') or 0),
|
|
float(data.get('debit_amount') or 0),
|
|
float(data.get('after_debit_amount') or 0),
|
|
float(data.get('amount') or 0),
|
|
float(data.get('gst_amount') or 0),
|
|
float(data.get('tds_amount') or 0),
|
|
float(data.get('sd_amount') or 0),
|
|
float(data.get('on_commission') or 0),
|
|
float(data.get('hydro_testing') or 0),
|
|
float(data.get('gst_sd_amount') or 0),
|
|
float(data.get('final_amount') or 0),
|
|
data.get('subcontractor_id')
|
|
])
|
|
clear_results(cursor)
|
|
|
|
connection.commit()
|
|
return invoice_id
|
|
|
|
except Exception as e:
|
|
connection.rollback()
|
|
raise e
|
|
|
|
finally:
|
|
cursor.close()
|
|
connection.close()
|
|
|
|
|
|
# ------------------- Assign Subcontractor -------------------
|
|
def assign_subcontractor(data, village_id):
|
|
connection = config.get_db_connection()
|
|
cursor = connection.cursor(dictionary=True)
|
|
|
|
try:
|
|
cursor.callproc('AssignSubcontractor', [
|
|
data.get('pmc_no'),
|
|
data.get('subcontractor_id'),
|
|
village_id
|
|
])
|
|
clear_results(cursor)
|
|
|
|
connection.commit()
|
|
|
|
except Exception as e:
|
|
connection.rollback()
|
|
raise e
|
|
|
|
finally:
|
|
cursor.close()
|
|
connection.close()
|
|
|
|
|
|
# ------------------- Insert Hold Types -------------------
|
|
def insert_hold_types(data, invoice_id):
|
|
connection = config.get_db_connection()
|
|
cursor = connection.cursor(dictionary=True)
|
|
|
|
try:
|
|
hold_types = data.getlist('hold_type[]')
|
|
hold_amounts = data.getlist('hold_amount[]')
|
|
|
|
for hold_type, hold_amount in zip(hold_types, hold_amounts):
|
|
if not hold_type:
|
|
continue
|
|
|
|
cursor.callproc('GetHoldTypeIdByName', [hold_type])
|
|
hold_type_result = None
|
|
|
|
for result in cursor.stored_results():
|
|
hold_type_result = result.fetchone()
|
|
|
|
if not hold_type_result:
|
|
cursor.callproc('InsertHoldType', [hold_type, 0])
|
|
cursor.execute("SELECT @_InsertHoldType_1")
|
|
hold_type_id = cursor.fetchone()[0]
|
|
else:
|
|
hold_type_id = hold_type_result['hold_type_id']
|
|
|
|
hold_amount = float(hold_amount or 0)
|
|
|
|
cursor.callproc('InsertInvoiceSubcontractorHold', [
|
|
data.get('subcontractor_id'),
|
|
invoice_id,
|
|
hold_type_id,
|
|
hold_amount
|
|
])
|
|
clear_results(cursor)
|
|
|
|
connection.commit()
|
|
|
|
except Exception as e:
|
|
connection.rollback()
|
|
raise e
|
|
|
|
finally:
|
|
cursor.close()
|
|
connection.close()
|
|
|
|
|
|
# ------------------- Get All Invoices -------------------
|
|
def get_all_invoice_details():
|
|
connection = config.get_db_connection()
|
|
cursor = connection.cursor(dictionary=True)
|
|
|
|
cursor.callproc('GetAllInvoiceDetails')
|
|
invoices = []
|
|
|
|
for result in cursor.stored_results():
|
|
invoices = result.fetchall()
|
|
|
|
cursor.close()
|
|
connection.close()
|
|
return invoices
|
|
|
|
|
|
# ------------------- Get All Villages -------------------
|
|
def get_all_villages():
|
|
connection = config.get_db_connection()
|
|
cursor = connection.cursor(dictionary=True)
|
|
|
|
cursor.callproc("GetAllVillages")
|
|
villages = []
|
|
|
|
for result in cursor.stored_results():
|
|
villages = result.fetchall()
|
|
|
|
cursor.close()
|
|
connection.close()
|
|
return villages
|
|
|
|
|
|
# ------------------- Search Contractors -------------------
|
|
def search_contractors(sub_query):
|
|
connection = config.get_db_connection()
|
|
cursor = connection.cursor(dictionary=True)
|
|
|
|
cursor.callproc('SearchContractorsByName', [sub_query])
|
|
results = []
|
|
|
|
for result in cursor.stored_results():
|
|
results = result.fetchall()
|
|
|
|
cursor.close()
|
|
connection.close()
|
|
return results
|
|
|
|
|
|
# ------------------- Get All Hold Types -------------------
|
|
def get_all_hold_types():
|
|
connection = config.get_db_connection()
|
|
cursor = connection.cursor(dictionary=True)
|
|
|
|
cursor.callproc("GetAllHoldTypes")
|
|
hold_types = []
|
|
|
|
for result in cursor.stored_results():
|
|
hold_types = result.fetchall()
|
|
|
|
cursor.close()
|
|
connection.close()
|
|
return hold_types
|
|
|
|
|
|
# ------------------- Get Invoice By Id -------------------
|
|
def get_invoice_by_id(invoice_id):
|
|
connection = config.get_db_connection()
|
|
cursor = connection.cursor(dictionary=True)
|
|
|
|
cursor.callproc('GetInvoiceDetailsById', [invoice_id])
|
|
invoice = None
|
|
|
|
for result in cursor.stored_results():
|
|
invoice = result.fetchone()
|
|
|
|
cursor.callproc('GetHoldAmountsByInvoiceId', [invoice_id])
|
|
hold_amounts = []
|
|
|
|
for result in cursor.stored_results():
|
|
hold_amounts = result.fetchall()
|
|
|
|
if invoice:
|
|
invoice["hold_amounts"] = hold_amounts
|
|
|
|
cursor.close()
|
|
connection.close()
|
|
return invoice
|
|
|
|
|
|
# ------------------- Update Invoice -------------------
|
|
def update_invoice(data, invoice_id):
|
|
connection = config.get_db_connection()
|
|
cursor = connection.cursor(dictionary=True)
|
|
|
|
try:
|
|
cursor.callproc("GetVillageIdByName", (data.get('village'),))
|
|
village = None
|
|
|
|
for rs in cursor.stored_results():
|
|
village = rs.fetchone()
|
|
|
|
village_id = village['Village_Id']
|
|
|
|
numeric = [
|
|
float(data.get('basic_amount') or 0),
|
|
float(data.get('debit_amount') or 0),
|
|
float(data.get('after_debit_amount') or 0),
|
|
float(data.get('amount') or 0),
|
|
float(data.get('gst_amount') or 0),
|
|
float(data.get('tds_amount') or 0),
|
|
float(data.get('sd_amount') or 0),
|
|
float(data.get('on_commission') or 0),
|
|
float(data.get('hydro_testing') or 0),
|
|
float(data.get('gst_sd_amount') or 0),
|
|
float(data.get('final_amount') or 0),
|
|
]
|
|
|
|
cursor.callproc('UpdateInvoice', [
|
|
data.get('pmc_no'),
|
|
village_id,
|
|
data.get('work_type'),
|
|
data.get('invoice_details'),
|
|
data.get('invoice_date'),
|
|
data.get('invoice_no'),
|
|
*numeric,
|
|
invoice_id
|
|
])
|
|
clear_results(cursor)
|
|
|
|
connection.commit()
|
|
|
|
except Exception as e:
|
|
connection.rollback()
|
|
raise e
|
|
|
|
finally:
|
|
cursor.close()
|
|
connection.close()
|
|
|
|
|
|
# ------------------- Update Inpayment -------------------
|
|
def update_inpayment(data):
|
|
connection = config.get_db_connection()
|
|
cursor = connection.cursor(dictionary=True)
|
|
|
|
try:
|
|
numeric = [
|
|
float(data.get('basic_amount') or 0),
|
|
float(data.get('debit_amount') or 0),
|
|
float(data.get('after_debit_amount') or 0),
|
|
float(data.get('amount') or 0),
|
|
float(data.get('gst_amount') or 0),
|
|
float(data.get('tds_amount') or 0),
|
|
float(data.get('sd_amount') or 0),
|
|
float(data.get('on_commission') or 0),
|
|
float(data.get('hydro_testing') or 0),
|
|
float(data.get('gst_sd_amount') or 0),
|
|
float(data.get('final_amount') or 0),
|
|
]
|
|
|
|
cursor.callproc('UpdateInpayment', [
|
|
data.get('work_type'),
|
|
data.get('invoice_details'),
|
|
data.get('invoice_date'),
|
|
*numeric,
|
|
data.get('pmc_no'),
|
|
data.get('invoice_no')
|
|
])
|
|
clear_results(cursor)
|
|
|
|
connection.commit()
|
|
|
|
except Exception as e:
|
|
connection.rollback()
|
|
raise e
|
|
|
|
finally:
|
|
cursor.close()
|
|
connection.close()
|
|
|
|
|
|
# ------------------- Delete Invoice -------------------
|
|
def delete_invoice_data(invoice_id, user_id):
|
|
connection = config.get_db_connection()
|
|
cursor = connection.cursor(dictionary=True)
|
|
|
|
try:
|
|
cursor.callproc('GetInvoicePMCById', [invoice_id])
|
|
|
|
record = {}
|
|
for result in cursor.stored_results():
|
|
record = result.fetchone() or {}
|
|
if not record:
|
|
raise Exception("Invoice not found")
|
|
|
|
cursor.callproc("DeleteInvoice", (invoice_id,))
|
|
clear_results(cursor)
|
|
|
|
cursor.callproc(
|
|
'DeleteInpaymentByPMCInvoice',
|
|
[record['PMC_No'], record['invoice_no']]
|
|
)
|
|
|
|
connection.commit()
|
|
|
|
except Exception as e:
|
|
connection.rollback()
|
|
raise e
|
|
|
|
finally:
|
|
cursor.close()
|
|
connection.close() |