optimize and add new service of get report and download report.
This commit is contained in:
160
model/Report.py
160
model/Report.py
@@ -1,11 +1,11 @@
|
||||
import config
|
||||
from datetime import datetime
|
||||
from flask import send_file
|
||||
import os
|
||||
import openpyxl
|
||||
from openpyxl.styles import Font, PatternFill
|
||||
|
||||
from model.FolderAndFile import FolderAndFile
|
||||
from services.Generalservice import GeneralUse
|
||||
|
||||
class ReportHelper:
|
||||
isSuccess = False
|
||||
@@ -17,30 +17,6 @@ class ReportHelper:
|
||||
self.resultMessage = ""
|
||||
self.data = []
|
||||
|
||||
@staticmethod
|
||||
def execute_sp(cursor, proc_name, params=[], fetch_one=False):
|
||||
cursor.callproc(proc_name, params)
|
||||
return (
|
||||
ReportHelper.fetch_one_result(cursor)
|
||||
if fetch_one else
|
||||
ReportHelper.fetch_all_results(cursor)
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def fetch_all_results(cursor):
|
||||
data = []
|
||||
for result in cursor.stored_results():
|
||||
data = result.fetchall()
|
||||
return data
|
||||
|
||||
|
||||
@staticmethod
|
||||
def fetch_one_result(cursor):
|
||||
data = None
|
||||
for result in cursor.stored_results():
|
||||
data = result.fetchone()
|
||||
return data
|
||||
|
||||
|
||||
@staticmethod
|
||||
def search_contractor(request):
|
||||
@@ -60,10 +36,7 @@ class ReportHelper:
|
||||
cursor = connection.cursor(dictionary=True)
|
||||
|
||||
try:
|
||||
data = ReportHelper.execute_sp(
|
||||
cursor,
|
||||
"search_contractor_info",
|
||||
[
|
||||
data = GeneralUse.execute_sp(cursor,"search_contractor_info",[
|
||||
subcontractor_name or None,
|
||||
pmc_no or None,
|
||||
state or None,
|
||||
@@ -72,8 +45,7 @@ class ReportHelper:
|
||||
village or None,
|
||||
year_from or None,
|
||||
year_to or None
|
||||
]
|
||||
)
|
||||
])
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error in search_contractor: {e}")
|
||||
@@ -85,69 +57,6 @@ class ReportHelper:
|
||||
|
||||
return data
|
||||
|
||||
|
||||
@staticmethod
|
||||
def get_contractor_report(contractor_id):
|
||||
connection = config.get_db_connection()
|
||||
cursor = connection.cursor(dictionary=True, buffered=True)
|
||||
|
||||
try:
|
||||
# Contractor Info (only one fetch)
|
||||
contInfo = ReportHelper.execute_sp(cursor, 'GetContractorInfo', [contractor_id], True)
|
||||
# Hold Types
|
||||
hold_types = ReportHelper.execute_sp(cursor, 'GetContractorHoldTypes', [contractor_id])
|
||||
# Invoices
|
||||
invoices = ReportHelper.execute_sp(cursor, 'GetContractorInvoices', [contractor_id])
|
||||
# GST Release
|
||||
gst_rel = ReportHelper.execute_sp(cursor, 'GetGSTRelease', [contractor_id])
|
||||
# Hold Release
|
||||
hold_release = ReportHelper.execute_sp(cursor, 'GetHoldRelease', [contractor_id])
|
||||
# Credit Note
|
||||
credit_note = ReportHelper.execute_sp(cursor, 'GetCreditNote', [contractor_id])
|
||||
# Payments
|
||||
payments = ReportHelper.execute_sp(cursor, 'GetPayments', [contractor_id])
|
||||
|
||||
# Totals
|
||||
total = {
|
||||
"sum_invo_basic_amt": float(sum(row['Basic_Amount'] or 0 for row in invoices)),
|
||||
"sum_invo_debit_amt": float(sum(row['Debit_Amount'] or 0 for row in invoices)),
|
||||
"sum_invo_after_debit_amt": float(sum(row['After_Debit_Amount'] or 0 for row in invoices)),
|
||||
"sum_invo_amt": float(sum(row['Amount'] or 0 for row in invoices)),
|
||||
"sum_invo_gst_amt": float(sum(row['GST_Amount'] or 0 for row in invoices)),
|
||||
"sum_invo_tds_amt": float(sum(row['TDS_Amount'] or 0 for row in invoices)),
|
||||
"sum_invo_ds_amt": float(sum(row['SD_Amount'] or 0 for row in invoices)),
|
||||
"sum_invo_on_commission": float(sum(row['On_Commission'] or 0 for row in invoices)),
|
||||
"sum_invo_hydro_test": float(sum(row['Hydro_Testing'] or 0 for row in invoices)),
|
||||
"sum_invo_gst_sd_amt": float(sum(row['GST_SD_Amount'] or 0 for row in invoices)),
|
||||
"sum_invo_final_amt": float(sum(row['Final_Amount'] or 0 for row in invoices)),
|
||||
"sum_invo_hold_amt": float(sum(row['hold_amount'] or 0 for row in invoices)),
|
||||
|
||||
"sum_gst_basic_amt": float(sum(row['basic_amount'] or 0 for row in gst_rel)),
|
||||
"sum_gst_final_amt": float(sum(row['final_amount'] or 0 for row in gst_rel)),
|
||||
|
||||
"sum_pay_payment_amt": float(sum(row['Payment_Amount'] or 0 for row in payments)),
|
||||
"sum_pay_tds_payment_amt": float(sum(row['TDS_Payment_Amount'] or 0 for row in payments)),
|
||||
"sum_pay_total_amt": float(sum(row['Total_amount'] or 0 for row in payments))
|
||||
}
|
||||
|
||||
current_date = datetime.now().strftime('%Y-%m-%d')
|
||||
|
||||
finally:
|
||||
cursor.close()
|
||||
connection.close()
|
||||
|
||||
return {
|
||||
"contInfo": contInfo,
|
||||
"invoices": invoices,
|
||||
"hold_types": hold_types,
|
||||
"gst_rel": gst_rel,
|
||||
"payments": payments,
|
||||
"credit_note": credit_note,
|
||||
"hold_release": hold_release,
|
||||
"total": total,
|
||||
"current_date": current_date
|
||||
}
|
||||
|
||||
|
||||
@staticmethod
|
||||
def get_contractor_info(contractor_id):
|
||||
@@ -156,10 +65,11 @@ class ReportHelper:
|
||||
return contractor.contInfo if contractor.contInfo else None
|
||||
|
||||
|
||||
# call this method for excel formate written
|
||||
@staticmethod
|
||||
def generate_excel(contractor_id, contInfo, invoices, hold_types, hold_data,
|
||||
credit_note_map, gst_release_map, output_file):
|
||||
|
||||
|
||||
workbook = openpyxl.Workbook()
|
||||
sheet = workbook.active
|
||||
sheet.title = "Contractor Report"
|
||||
@@ -196,8 +106,7 @@ class ReportHelper:
|
||||
inv["invoice_no"].replace(" ", "") if inv["invoice_no"] else ""
|
||||
if inv["invoice_no"] not in (None, "", 0)
|
||||
else ""
|
||||
)
|
||||
|
||||
)
|
||||
key = (pmc_no)
|
||||
|
||||
# Yellow separator
|
||||
@@ -284,7 +193,13 @@ class ReportHelper:
|
||||
|
||||
cn_row += [""] * len(hold_headers)
|
||||
|
||||
cn_row += [cn.get("Final_Amount", ""),"","",cn.get("Total_Amount", ""),cn.get("UTR", "")]
|
||||
cn_row += [
|
||||
cn.get("Final_Amount", ""),
|
||||
"",
|
||||
"",
|
||||
cn.get("Total_Amount", ""),
|
||||
cn.get("UTR", "")
|
||||
]
|
||||
|
||||
sheet.append(cn_row)
|
||||
|
||||
@@ -294,53 +209,4 @@ class ReportHelper:
|
||||
workbook.save(output_file)
|
||||
|
||||
|
||||
@staticmethod
|
||||
def create_contractor_report(contractor_id):
|
||||
fileName = f"Contractor_Report_{contractor_id}.xlsx"
|
||||
output_file = FolderAndFile.get_download_path(filename=fileName)
|
||||
|
||||
# Fetch Data
|
||||
contInfo = ReportHelper.get_contractor_info(contractor_id)
|
||||
if not contInfo:
|
||||
return None, "No contractor found"
|
||||
|
||||
connection = config.get_db_connection()
|
||||
cursor = connection.cursor(dictionary=True, buffered=True)
|
||||
|
||||
hold_types = ReportHelper.execute_sp(cursor, 'HoldTypesByContractorId', [contractor_id])
|
||||
invoices = ReportHelper.execute_sp(cursor, 'FetchInvoicesByContractor', [contractor_id])
|
||||
hold_amounts = ReportHelper.execute_sp(cursor, 'HoldAmountsByContractorId', [contractor_id])
|
||||
hold_data = {}
|
||||
for h in hold_amounts:
|
||||
hold_data.setdefault(h['Invoice_Id'], {})[h['hold_type_id']] = h['hold_amount']
|
||||
|
||||
|
||||
# -------- Credit Note MAP --------
|
||||
credit_note_raw = ReportHelper.execute_sp(cursor, 'GetCreditNotesByContractor', [contractor_id])
|
||||
credit_note_map = {}
|
||||
for cn in credit_note_raw:
|
||||
key = (
|
||||
str(cn['PMC_No']).strip()
|
||||
)
|
||||
credit_note_map.setdefault(key, []).append(cn)
|
||||
|
||||
# -------- GST MAP --------
|
||||
gst_release_raw = ReportHelper.execute_sp(cursor, 'GstReleasesByContractorId', [contractor_id])
|
||||
gst_release_map = {}
|
||||
for gr in gst_release_raw:
|
||||
key = (
|
||||
str(gr['PMC_No']).strip()
|
||||
)
|
||||
gst_release_map.setdefault(key, []).append(gr)
|
||||
|
||||
# Generate Excel
|
||||
ReportHelper.generate_excel(
|
||||
contractor_id, contInfo, invoices, hold_types, hold_data,
|
||||
credit_note_map, gst_release_map, output_file
|
||||
)
|
||||
|
||||
return output_file, None
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user