Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,6 +16,10 @@ from reportlab.lib.units import inch
|
|
| 16 |
import tempfile
|
| 17 |
from analytics_tab import AnalyticsTab
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
# Google Sheets Setup
|
| 20 |
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
|
| 21 |
|
|
@@ -229,10 +233,13 @@ def calculate_interest(principal, start_date, end_date):
|
|
| 229 |
def create_pdf_report(farmer_name, df, total_lent, total_returned, interest_accrued, remaining_balance):
|
| 230 |
"""Generate PDF report of transaction history"""
|
| 231 |
try:
|
| 232 |
-
# Create
|
| 233 |
pdf_filename = f"{farmer_name.replace(' ', '_')}_transactions_with_interest.pdf"
|
| 234 |
-
|
| 235 |
-
|
|
|
|
|
|
|
|
|
|
| 236 |
|
| 237 |
doc = SimpleDocTemplate(pdf_path, pagesize=letter, rightMargin=30, leftMargin=30, topMargin=30, bottomMargin=18)
|
| 238 |
|
|
@@ -311,10 +318,18 @@ def create_pdf_report(farmer_name, df, total_lent, total_returned, interest_accr
|
|
| 311 |
# Build PDF
|
| 312 |
doc.build(elements)
|
| 313 |
|
| 314 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 315 |
except Exception as e:
|
| 316 |
print(f"Error creating PDF: {str(e)}")
|
| 317 |
-
|
|
|
|
|
|
|
| 318 |
|
| 319 |
def get_farmer_report(farmer_name):
|
| 320 |
"""Generate complete report for a farmer with interest calculations"""
|
|
@@ -465,9 +480,9 @@ def get_farmer_report(farmer_name):
|
|
| 465 |
)
|
| 466 |
|
| 467 |
# Generate PDF
|
| 468 |
-
pdf_path
|
| 469 |
|
| 470 |
-
return detailed_df, pdf_path, summary_html, fig_bank, fig_pie, remaining_balance,
|
| 471 |
|
| 472 |
except Exception as e:
|
| 473 |
return None, None, f"❌ Error generating report: {str(e)}", None, None, None, None
|
|
|
|
| 16 |
import tempfile
|
| 17 |
from analytics_tab import AnalyticsTab
|
| 18 |
|
| 19 |
+
# Create downloads directory if it doesn't exist
|
| 20 |
+
DOWNLOADS_DIR = os.path.join(os.getcwd(), "downloads")
|
| 21 |
+
os.makedirs(DOWNLOADS_DIR, exist_ok=True)
|
| 22 |
+
|
| 23 |
# Google Sheets Setup
|
| 24 |
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
|
| 25 |
|
|
|
|
| 233 |
def create_pdf_report(farmer_name, df, total_lent, total_returned, interest_accrued, remaining_balance):
|
| 234 |
"""Generate PDF report of transaction history"""
|
| 235 |
try:
|
| 236 |
+
# Create PDF in persistent downloads directory
|
| 237 |
pdf_filename = f"{farmer_name.replace(' ', '_')}_transactions_with_interest.pdf"
|
| 238 |
+
pdf_path = os.path.join(DOWNLOADS_DIR, pdf_filename)
|
| 239 |
+
|
| 240 |
+
# Remove old file if it exists
|
| 241 |
+
if os.path.exists(pdf_path):
|
| 242 |
+
os.remove(pdf_path)
|
| 243 |
|
| 244 |
doc = SimpleDocTemplate(pdf_path, pagesize=letter, rightMargin=30, leftMargin=30, topMargin=30, bottomMargin=18)
|
| 245 |
|
|
|
|
| 318 |
# Build PDF
|
| 319 |
doc.build(elements)
|
| 320 |
|
| 321 |
+
# Verify file was created
|
| 322 |
+
if os.path.exists(pdf_path):
|
| 323 |
+
print(f"PDF created successfully at: {pdf_path}")
|
| 324 |
+
return pdf_path
|
| 325 |
+
else:
|
| 326 |
+
print("PDF file was not created")
|
| 327 |
+
return None
|
| 328 |
except Exception as e:
|
| 329 |
print(f"Error creating PDF: {str(e)}")
|
| 330 |
+
import traceback
|
| 331 |
+
traceback.print_exc()
|
| 332 |
+
return None
|
| 333 |
|
| 334 |
def get_farmer_report(farmer_name):
|
| 335 |
"""Generate complete report for a farmer with interest calculations"""
|
|
|
|
| 480 |
)
|
| 481 |
|
| 482 |
# Generate PDF
|
| 483 |
+
pdf_path = create_pdf_report(farmer_name, detailed_df, total_lent, total_returned, interest_accrued, remaining_balance)
|
| 484 |
|
| 485 |
+
return detailed_df, pdf_path, summary_html, fig_bank, fig_pie, remaining_balance, None
|
| 486 |
|
| 487 |
except Exception as e:
|
| 488 |
return None, None, f"❌ Error generating report: {str(e)}", None, None, None, None
|