summary report download code commit.
This commit is contained in:
@@ -2,6 +2,12 @@ import os
|
|||||||
from AppCode.Config import DBConfig
|
from AppCode.Config import DBConfig
|
||||||
from AppCode.FileHandler import FileHandler
|
from AppCode.FileHandler import FileHandler
|
||||||
from werkzeug.utils import secure_filename
|
from werkzeug.utils import secure_filename
|
||||||
|
from flask import Flask, render_template, request, redirect, url_for, send_from_directory, abort, flash,send_file
|
||||||
|
import mysql.connector
|
||||||
|
import pandas as pd
|
||||||
|
import io
|
||||||
|
from AppCode.YearGet import YearGet
|
||||||
|
|
||||||
|
|
||||||
class DocumentHandler:
|
class DocumentHandler:
|
||||||
|
|
||||||
@@ -85,15 +91,129 @@ class DocumentHandler:
|
|||||||
# """, (filename, filepath, file.filename.rsplit('.', 1)[1], year, stage))
|
# """, (filename, filepath, file.filename.rsplit('.', 1)[1], year, stage))
|
||||||
|
|
||||||
|
|
||||||
cursor.callproc('InsertDocument', [
|
cursor.callproc('InsertDocument', [ filename, filepath, extension, year, stage ])
|
||||||
filename,
|
|
||||||
filepath,
|
|
||||||
extension,
|
|
||||||
year,
|
|
||||||
stage
|
|
||||||
])
|
|
||||||
|
|
||||||
connection.commit()
|
connection.commit()
|
||||||
cursor.close()
|
cursor.close()
|
||||||
connection.close()
|
connection.close()
|
||||||
# return redirect(url_for('view_documents'))
|
# return redirect(url_for('view_documents'))
|
||||||
|
|
||||||
|
|
||||||
|
# Summary report
|
||||||
|
def Summary_report(self, request):
|
||||||
|
|
||||||
|
dbconfig = DBConfig()
|
||||||
|
connection = dbconfig.get_db_connection()
|
||||||
|
|
||||||
|
year = request.args.get('year')
|
||||||
|
|
||||||
|
# if not year get all year in list.
|
||||||
|
if not year:
|
||||||
|
yearGetter = YearGet()
|
||||||
|
allYears = yearGetter.get_year_by_model("AllYearsInAllModel")
|
||||||
|
yearGetter.close()
|
||||||
|
|
||||||
|
return render_template(
|
||||||
|
'summary_reports.html',
|
||||||
|
years=allYears,
|
||||||
|
message="Please select a year to download."
|
||||||
|
)
|
||||||
|
|
||||||
|
# for excel
|
||||||
|
try:
|
||||||
|
stages = {
|
||||||
|
"ITR": "itr",
|
||||||
|
"AO": "ao",
|
||||||
|
"CIT": "cit",
|
||||||
|
"ITAT": "itat",
|
||||||
|
}
|
||||||
|
# stages = ['itr', 'ao', 'cit', 'itat']
|
||||||
|
stage_data = {}
|
||||||
|
|
||||||
|
for stage_name, table_name in stages.items():
|
||||||
|
cursor = connection.cursor(dictionary=True)
|
||||||
|
|
||||||
|
cursor.callproc("sp_get_stage_data", [table_name, year])
|
||||||
|
|
||||||
|
for result in cursor.stored_results():
|
||||||
|
rows = result.fetchall()
|
||||||
|
|
||||||
|
df = pd.DataFrame(rows) if rows else pd.DataFrame()
|
||||||
|
stage_data[stage_name] = df
|
||||||
|
|
||||||
|
cursor.close()
|
||||||
|
|
||||||
|
|
||||||
|
def safe_get(df, col):
|
||||||
|
return df[col].values[0] if col in df.columns and not df.empty else "-"
|
||||||
|
|
||||||
|
particulars = [
|
||||||
|
"Gross Total Income", "Add: Disallowance u/s 14A", "Add: Disallowance u/s 37", "GTI as per",
|
||||||
|
"Less: Deduction u/s 80IA", "Less: Deduction u/s 80G", "Net Taxable Income", "Tax @ 30%",
|
||||||
|
"Tax @ 18.5% on Book Profit", "Surcharge @ 12%", "Education Cess @ 3%", "Total Tax Payable",
|
||||||
|
"Less: MAT Credit", "Net Tax", "Add: Interest u/s 234C", "Total Tax",
|
||||||
|
"Advance Tax", "TDS", "TCS", "SAT", "Tax on Regular Assessment", "Refund"
|
||||||
|
]
|
||||||
|
|
||||||
|
columns = [
|
||||||
|
'gross_total_income', 'disallowance_14a', 'disallowance_37', 'gti',
|
||||||
|
'deduction_80ia', 'deduction_80g', 'net_taxable_income', 'tax_30',
|
||||||
|
'book_profit_tax', 'surcharge_12', 'education_cess', 'total_tax',
|
||||||
|
'mat_credit', 'net_tax', 'interest_234c', 'total_tax_payable',
|
||||||
|
'advance_tax', 'tds', 'tcs', 'sat', 'tax_regular', 'refund'
|
||||||
|
]
|
||||||
|
|
||||||
|
data = {
|
||||||
|
"Particulars": particulars,
|
||||||
|
"ITR": [safe_get(stage_data['ITR'], col) for col in columns],
|
||||||
|
"AO": [safe_get(stage_data['AO'], col) for col in columns],
|
||||||
|
"CIT": [safe_get(stage_data['CIT'], col) for col in columns],
|
||||||
|
"ITAT": [safe_get(stage_data['ITAT'], col) for col in columns],
|
||||||
|
}
|
||||||
|
|
||||||
|
df = pd.DataFrame(data)
|
||||||
|
|
||||||
|
# Export to Excel
|
||||||
|
output = io.BytesIO()
|
||||||
|
with pd.ExcelWriter(output, engine='xlsxwriter') as writer:
|
||||||
|
df.to_excel(writer, index=False, sheet_name=f'AY {year}')
|
||||||
|
workbook = writer.book
|
||||||
|
worksheet = writer.sheets[f'AY {year}']
|
||||||
|
|
||||||
|
header = workbook.add_format({
|
||||||
|
'bold': True,
|
||||||
|
'align': 'center',
|
||||||
|
'valign': 'middle',
|
||||||
|
'bg_color': '#007bff',
|
||||||
|
'font_color': 'white',
|
||||||
|
'border': 1
|
||||||
|
})
|
||||||
|
|
||||||
|
cell = workbook.add_format({
|
||||||
|
'border': 1,
|
||||||
|
'align': 'center',
|
||||||
|
'valign': 'middle'
|
||||||
|
})
|
||||||
|
|
||||||
|
# Apply formatting
|
||||||
|
for col_num, col_name in enumerate(df.columns):
|
||||||
|
worksheet.write(0, col_num, col_name, header)
|
||||||
|
max_len = max(df[col_name].astype(str).map(len).max(), len(col_name)) + 2
|
||||||
|
worksheet.set_column(col_num, col_num, max_len)
|
||||||
|
|
||||||
|
for row in range(1, len(df) + 1):
|
||||||
|
for col in range(len(df.columns)):
|
||||||
|
worksheet.write(row, col, df.iloc[row - 1, col], cell)
|
||||||
|
|
||||||
|
output.seek(0)
|
||||||
|
|
||||||
|
return send_file(
|
||||||
|
output,
|
||||||
|
download_name=f"Summary_Report_{year}.xlsx",
|
||||||
|
as_attachment=True,
|
||||||
|
mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
||||||
|
)
|
||||||
|
|
||||||
|
finally:
|
||||||
|
connection.close()
|
||||||
|
|
||||||
|
|||||||
@@ -10,3 +10,4 @@ class FileHandler:
|
|||||||
#Wheteher path exists
|
#Wheteher path exists
|
||||||
os.makedirs(FileHandler.UPLOAD_FOLDER, exist_ok=True)
|
os.makedirs(FileHandler.UPLOAD_FOLDER, exist_ok=True)
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -7,22 +7,33 @@ class YearGet:
|
|||||||
self.conn = DBConfig.get_db_connection()
|
self.conn = DBConfig.get_db_connection()
|
||||||
self.cursor = self.conn.cursor(dictionary=True)
|
self.cursor = self.conn.cursor(dictionary=True)
|
||||||
|
|
||||||
|
# get year fetch in perticular Model name.
|
||||||
def get_year_by_model(self, proc_name):
|
def get_year_by_model(self, proc_name):
|
||||||
try:
|
try:
|
||||||
self.cursor.callproc(proc_name)
|
self.cursor.callproc(proc_name)
|
||||||
|
|
||||||
years = []
|
years = []
|
||||||
|
|
||||||
for result in self.cursor.stored_results():
|
for result in self.cursor.stored_results():
|
||||||
rows = result.fetchall()
|
rows = result.fetchall()
|
||||||
years = [row["year"] for row in rows]
|
years = [row["year"] for row in rows]
|
||||||
|
|
||||||
|
print("-- years get --",years)
|
||||||
return years
|
return years
|
||||||
|
|
||||||
except mysql.connector.Error as e:
|
except mysql.connector.Error as e:
|
||||||
print("MySQL Error:", e)
|
print("MySQL Error:", e)
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
# def get_all_year_in_all_model(self):
|
||||||
|
# self.cursor.callproc("AllYearsInAllModel")
|
||||||
|
# years = []
|
||||||
|
# for result in self.cursor.stored_results():
|
||||||
|
# rows = result.fetchall()
|
||||||
|
# years = [row["year"] for row in rows]
|
||||||
|
# return years
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
self.cursor.close()
|
self.cursor.close()
|
||||||
self.conn.close()
|
self.conn.close()
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
209
main.py
209
main.py
@@ -850,8 +850,6 @@ def itat_report():
|
|||||||
# return render_template("itr_reports.html", years=years)
|
# return render_template("itr_reports.html", years=years)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# @app.route('/ao_report', methods=['GET'])
|
# @app.route('/ao_report', methods=['GET'])
|
||||||
# def ao_report():
|
# def ao_report():
|
||||||
# selected_year = request.args.get('year')
|
# selected_year = request.args.get('year')
|
||||||
@@ -972,7 +970,6 @@ def itat_report():
|
|||||||
# connection.close()
|
# connection.close()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# @app.route('/itr_report_download', methods=['GET'])
|
# @app.route('/itr_report_download', methods=['GET'])
|
||||||
# def itr_report_download():
|
# def itr_report_download():
|
||||||
# connection = pymysql.connect(**db_config)
|
# connection = pymysql.connect(**db_config)
|
||||||
@@ -1020,107 +1017,117 @@ def download_report(doc_id):
|
|||||||
return send_from_directory(directory='static/uploads', path=document['filename'], as_attachment=True)
|
return send_from_directory(directory='static/uploads', path=document['filename'], as_attachment=True)
|
||||||
|
|
||||||
|
|
||||||
|
# @app.route('/summary_report', methods=['GET'])
|
||||||
|
# def summary_report():
|
||||||
|
# year = request.args.get('year')
|
||||||
|
|
||||||
|
# if not year:
|
||||||
|
# connection = pymysql.connect(**db_config)
|
||||||
|
# try:
|
||||||
|
# years = set()
|
||||||
|
# for table in ['itr', 'ao', 'cit', 'itat']:
|
||||||
|
# df = pd.read_sql(f"SELECT DISTINCT year FROM {table}", connection)
|
||||||
|
# years.update(int(y) for y in df['year'].dropna().tolist())
|
||||||
|
|
||||||
|
# return render_template('summary_reports.html', years=sorted(years), message="Please select a year to download.")
|
||||||
|
|
||||||
|
# finally:
|
||||||
|
# connection.close()
|
||||||
|
|
||||||
|
# connection = pymysql.connect(**db_config)
|
||||||
|
# try:
|
||||||
|
# stages = ['itr', 'ao', 'cit', 'itat']
|
||||||
|
# stage_data = {}
|
||||||
|
|
||||||
|
# for stage in stages:
|
||||||
|
# query = f"SELECT * FROM {stage} WHERE year = %s"
|
||||||
|
# df = pd.read_sql(query, connection, params=[year])
|
||||||
|
# stage_data[stage.upper()] = df
|
||||||
|
|
||||||
|
# def safe_get(df, col):
|
||||||
|
# return df[col].values[0] if col in df.columns and not df.empty else '-'
|
||||||
|
|
||||||
|
|
||||||
|
# particulars = [
|
||||||
|
# "Gross Total Income", "Add: Disallowance u/s 14A", "Add: Disallowance u/s 37", "GTI as per",
|
||||||
|
# "Less: Deduction u/s 80IA", "Less: Deduction u/s 80G", "Net Taxable Income", "Tax @ 30%",
|
||||||
|
# "Tax @ 18.5% on Book Profit", "Surcharge @ 12%", "Education Cess @ 3%", "Total Tax Payable",
|
||||||
|
# "Less: MAT Credit", "Net Tax", "Add: Interest u/s 234C", "Total Tax",
|
||||||
|
# "Advance Tax", "TDS", "TCS", "SAT", "Tax on Regular Assessment", "Refund"
|
||||||
|
# ]
|
||||||
|
|
||||||
|
# columns = [
|
||||||
|
# 'gross_total_income', 'disallowance_14a', 'disallowance_37', 'gti',
|
||||||
|
# 'deduction_80ia', 'deduction_80g', 'net_taxable_income', 'tax_30',
|
||||||
|
# 'book_profit_tax', 'surcharge_12', 'education_cess', 'total_tax',
|
||||||
|
# 'mat_credit', 'net_tax', 'interest_234c', 'total_tax_payable',
|
||||||
|
# 'advance_tax', 'tds', 'tcs', 'sat', 'tax_regular', 'refund'
|
||||||
|
# ]
|
||||||
|
|
||||||
|
# data = {
|
||||||
|
# "Particulars": particulars,
|
||||||
|
# "ITR": [safe_get(stage_data['ITR'], col) for col in columns],
|
||||||
|
# "AO": [safe_get(stage_data['AO'], col) for col in columns],
|
||||||
|
# "CIT(A)": [safe_get(stage_data['CIT'], col) for col in columns],
|
||||||
|
# "ITAT": [safe_get(stage_data['ITAT'], col) for col in columns],
|
||||||
|
# }
|
||||||
|
|
||||||
|
# df = pd.DataFrame(data)
|
||||||
|
|
||||||
|
# # Export to Excel with formatting
|
||||||
|
# output = io.BytesIO()
|
||||||
|
# with pd.ExcelWriter(output, engine='xlsxwriter') as writer:
|
||||||
|
# df.to_excel(writer, index=False, sheet_name=f'AY {year}')
|
||||||
|
# workbook = writer.book
|
||||||
|
# worksheet = writer.sheets[f'AY {year}']
|
||||||
|
|
||||||
|
# # Format definitions
|
||||||
|
# header_format = workbook.add_format({
|
||||||
|
# 'bold': True,
|
||||||
|
# 'text_wrap': True,
|
||||||
|
# 'valign': 'middle',
|
||||||
|
# 'align': 'center',
|
||||||
|
# 'bg_color': '#007bff',
|
||||||
|
# 'font_color': 'white',
|
||||||
|
# 'border': 1
|
||||||
|
# })
|
||||||
|
|
||||||
|
# cell_format = workbook.add_format({
|
||||||
|
# 'border': 1,
|
||||||
|
# 'valign': 'top',
|
||||||
|
# 'align': 'center',
|
||||||
|
# })
|
||||||
|
|
||||||
|
# # Apply formats
|
||||||
|
# for col_num, value in enumerate(df.columns):
|
||||||
|
# worksheet.write(0, col_num, value, header_format)
|
||||||
|
# # Auto column width
|
||||||
|
# max_len = max(df[value].astype(str).map(len).max(), len(str(value))) + 2
|
||||||
|
# worksheet.set_column(col_num, col_num, max_len)
|
||||||
|
|
||||||
|
# # Format data rows
|
||||||
|
# for row_num in range(1, len(df) + 1):
|
||||||
|
# for col_num in range(len(df.columns)):
|
||||||
|
# worksheet.write(row_num, col_num, df.iloc[row_num - 1, col_num], cell_format)
|
||||||
|
|
||||||
|
# output.seek(0)
|
||||||
|
|
||||||
|
# return send_file(
|
||||||
|
# output,
|
||||||
|
# mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||||
|
# as_attachment=True,
|
||||||
|
# download_name=f"Summary_Report_{year}.xlsx"
|
||||||
|
# )
|
||||||
|
|
||||||
|
# finally:
|
||||||
|
# connection.close()
|
||||||
|
|
||||||
|
|
||||||
@app.route('/summary_report', methods=['GET'])
|
@app.route('/summary_report', methods=['GET'])
|
||||||
def summary_report():
|
def summary_report():
|
||||||
year = request.args.get('year')
|
docHandler = DocumentHandler()
|
||||||
|
return docHandler.Summary_report(request=request)
|
||||||
|
|
||||||
if not year:
|
|
||||||
connection = pymysql.connect(**db_config)
|
|
||||||
try:
|
|
||||||
years = set()
|
|
||||||
for table in ['itr', 'ao', 'cit', 'itat']:
|
|
||||||
df = pd.read_sql(f"SELECT DISTINCT year FROM {table}", connection)
|
|
||||||
years.update(int(y) for y in df['year'].dropna().tolist())
|
|
||||||
return render_template('summary_reports.html', years=sorted(years), message="Please select a year to download.")
|
|
||||||
finally:
|
|
||||||
connection.close()
|
|
||||||
|
|
||||||
connection = pymysql.connect(**db_config)
|
|
||||||
try:
|
|
||||||
stages = ['itr', 'ao', 'cit', 'itat']
|
|
||||||
stage_data = {}
|
|
||||||
|
|
||||||
for stage in stages:
|
|
||||||
query = f"SELECT * FROM {stage} WHERE year = %s"
|
|
||||||
df = pd.read_sql(query, connection, params=[year])
|
|
||||||
stage_data[stage.upper()] = df
|
|
||||||
|
|
||||||
def safe_get(df, col):
|
|
||||||
return df[col].values[0] if col in df.columns and not df.empty else '-'
|
|
||||||
|
|
||||||
particulars = [
|
|
||||||
"Gross Total Income", "Add: Disallowance u/s 14A", "Add: Disallowance u/s 37", "GTI as per",
|
|
||||||
"Less: Deduction u/s 80IA", "Less: Deduction u/s 80G", "Net Taxable Income", "Tax @ 30%",
|
|
||||||
"Tax @ 18.5% on Book Profit", "Surcharge @ 12%", "Education Cess @ 3%", "Total Tax Payable",
|
|
||||||
"Less: MAT Credit", "Net Tax", "Add: Interest u/s 234C", "Total Tax",
|
|
||||||
"Advance Tax", "TDS", "TCS", "SAT", "Tax on Regular Assessment", "Refund"
|
|
||||||
]
|
|
||||||
|
|
||||||
columns = [
|
|
||||||
'gross_total_income', 'disallowance_14a', 'disallowance_37', 'gti',
|
|
||||||
'deduction_80ia', 'deduction_80g', 'net_taxable_income', 'tax_30',
|
|
||||||
'book_profit_tax', 'surcharge_12', 'education_cess', 'total_tax',
|
|
||||||
'mat_credit', 'net_tax', 'interest_234c', 'total_tax_payable',
|
|
||||||
'advance_tax', 'tds', 'tcs', 'sat', 'tax_regular', 'refund'
|
|
||||||
]
|
|
||||||
|
|
||||||
data = {
|
|
||||||
"Particulars": particulars,
|
|
||||||
"ITR": [safe_get(stage_data['ITR'], col) for col in columns],
|
|
||||||
"AO": [safe_get(stage_data['AO'], col) for col in columns],
|
|
||||||
"CIT(A)": [safe_get(stage_data['CIT'], col) for col in columns],
|
|
||||||
"ITAT": [safe_get(stage_data['ITAT'], col) for col in columns],
|
|
||||||
}
|
|
||||||
|
|
||||||
df = pd.DataFrame(data)
|
|
||||||
|
|
||||||
# Export to Excel with formatting
|
|
||||||
output = io.BytesIO()
|
|
||||||
with pd.ExcelWriter(output, engine='xlsxwriter') as writer:
|
|
||||||
df.to_excel(writer, index=False, sheet_name=f'AY {year}')
|
|
||||||
workbook = writer.book
|
|
||||||
worksheet = writer.sheets[f'AY {year}']
|
|
||||||
|
|
||||||
# Format definitions
|
|
||||||
header_format = workbook.add_format({
|
|
||||||
'bold': True,
|
|
||||||
'text_wrap': True,
|
|
||||||
'valign': 'middle',
|
|
||||||
'align': 'center',
|
|
||||||
'bg_color': '#007bff',
|
|
||||||
'font_color': 'white',
|
|
||||||
'border': 1
|
|
||||||
})
|
|
||||||
|
|
||||||
cell_format = workbook.add_format({
|
|
||||||
'border': 1,
|
|
||||||
'valign': 'top',
|
|
||||||
'align': 'center',
|
|
||||||
})
|
|
||||||
|
|
||||||
# Apply formats
|
|
||||||
for col_num, value in enumerate(df.columns):
|
|
||||||
worksheet.write(0, col_num, value, header_format)
|
|
||||||
# Auto column width
|
|
||||||
max_len = max(df[value].astype(str).map(len).max(), len(str(value))) + 2
|
|
||||||
worksheet.set_column(col_num, col_num, max_len)
|
|
||||||
|
|
||||||
# Format data rows
|
|
||||||
for row_num in range(1, len(df) + 1):
|
|
||||||
for col_num in range(len(df.columns)):
|
|
||||||
worksheet.write(row_num, col_num, df.iloc[row_num - 1, col_num], cell_format)
|
|
||||||
|
|
||||||
output.seek(0)
|
|
||||||
|
|
||||||
return send_file(
|
|
||||||
output,
|
|
||||||
mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
||||||
as_attachment=True,
|
|
||||||
download_name=f"Summary_Report_{year}.xlsx"
|
|
||||||
)
|
|
||||||
|
|
||||||
finally:
|
|
||||||
connection.close()
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app.run(host='0.0.0.0', port=5003, debug=True)
|
app.run(host='0.0.0.0', port=5003, debug=True)
|
||||||
|
|||||||
Reference in New Issue
Block a user