Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -458,31 +458,63 @@ def get_farmer_report(farmer_name, end_date=None, interest_rate=None):
|
|
| 458 |
df = df.sort_values('Date')
|
| 459 |
|
| 460 |
# Calculate running balance with interest
|
| 461 |
-
|
|
|
|
| 462 |
total_lent = 0
|
| 463 |
total_returned = 0
|
| 464 |
last_date = None
|
|
|
|
|
|
|
| 465 |
|
| 466 |
transactions_with_interest = []
|
| 467 |
-
|
| 468 |
for idx, row in df.iterrows():
|
| 469 |
amount = row['Amount']
|
| 470 |
current_date = row['Date'].strftime("%Y-%m-%d")
|
| 471 |
-
|
| 472 |
-
#
|
| 473 |
-
if
|
| 474 |
-
interest = calculate_interest(
|
| 475 |
-
|
|
|
|
|
|
|
| 476 |
transactions_with_interest.append({
|
| 477 |
'Date': current_date,
|
| 478 |
'Description': 'Interest Accrued',
|
| 479 |
'Bank Account': '-',
|
| 480 |
'Amount': -interest,
|
| 481 |
-
'Balance':
|
| 482 |
})
|
| 483 |
-
|
| 484 |
-
#
|
| 485 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 486 |
|
| 487 |
if amount < 0:
|
| 488 |
total_lent += abs(amount)
|
|
@@ -500,16 +532,19 @@ def get_farmer_report(farmer_name, end_date=None, interest_rate=None):
|
|
| 500 |
last_date = current_date
|
| 501 |
|
| 502 |
# Calculate interest up to end date
|
| 503 |
-
if
|
| 504 |
-
interest = calculate_interest(
|
| 505 |
-
|
|
|
|
|
|
|
| 506 |
transactions_with_interest.append({
|
| 507 |
'Date': end_date_str,
|
| 508 |
'Description': f'Interest Accrued (to {format_date_display(end_date_str)})',
|
| 509 |
'Bank Account': '-',
|
| 510 |
'Amount': -interest,
|
| 511 |
-
'Balance':
|
| 512 |
})
|
|
|
|
| 513 |
|
| 514 |
# Create detailed transaction DataFrame
|
| 515 |
detailed_df = pd.DataFrame(transactions_with_interest)
|
|
@@ -522,7 +557,13 @@ def get_farmer_report(farmer_name, end_date=None, interest_rate=None):
|
|
| 522 |
|
| 523 |
# Summary statistics
|
| 524 |
remaining_balance = balance
|
| 525 |
-
interest_accrued =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 526 |
|
| 527 |
# Create summary HTML
|
| 528 |
summary_html = f"""
|
|
|
|
| 458 |
df = df.sort_values('Date')
|
| 459 |
|
| 460 |
# Calculate running balance with interest
|
| 461 |
+
principal_due = 0 # actual loan outstanding
|
| 462 |
+
total_interest = 0 # accumulated interest
|
| 463 |
total_lent = 0
|
| 464 |
total_returned = 0
|
| 465 |
last_date = None
|
| 466 |
+
surplus_to_farmer = 0 # if farmer paid extra
|
| 467 |
+
|
| 468 |
|
| 469 |
transactions_with_interest = []
|
| 470 |
+
|
| 471 |
for idx, row in df.iterrows():
|
| 472 |
amount = row['Amount']
|
| 473 |
current_date = row['Date'].strftime("%Y-%m-%d")
|
| 474 |
+
|
| 475 |
+
# 1) Apply interest ONLY on principal_due (only when farmer owes us)
|
| 476 |
+
if principal_due > 0 and last_date:
|
| 477 |
+
interest = calculate_interest(principal_due, last_date, current_date, interest_rate)
|
| 478 |
+
total_interest += interest
|
| 479 |
+
principal_due += interest
|
| 480 |
+
|
| 481 |
transactions_with_interest.append({
|
| 482 |
'Date': current_date,
|
| 483 |
'Description': 'Interest Accrued',
|
| 484 |
'Bank Account': '-',
|
| 485 |
'Amount': -interest,
|
| 486 |
+
'Balance': -principal_due
|
| 487 |
})
|
| 488 |
+
|
| 489 |
+
# 2) Process transaction
|
| 490 |
+
if amount < 0:
|
| 491 |
+
# We lent money
|
| 492 |
+
lend = abs(amount)
|
| 493 |
+
principal_due += lend
|
| 494 |
+
total_lent += lend
|
| 495 |
+
|
| 496 |
+
else:
|
| 497 |
+
# Farmer paid money
|
| 498 |
+
payment = amount
|
| 499 |
+
total_returned += payment
|
| 500 |
+
|
| 501 |
+
if payment <= principal_due:
|
| 502 |
+
principal_due -= payment
|
| 503 |
+
else:
|
| 504 |
+
surplus_to_farmer += (payment - principal_due)
|
| 505 |
+
principal_due = 0
|
| 506 |
+
|
| 507 |
+
# Record transaction
|
| 508 |
+
transactions_with_interest.append({
|
| 509 |
+
'Date': current_date,
|
| 510 |
+
'Description': row['Type'],
|
| 511 |
+
'Bank Account': row['Bank Account'],
|
| 512 |
+
'Amount': amount,
|
| 513 |
+
'Balance': -principal_due if principal_due > 0 else surplus_to_farmer
|
| 514 |
+
})
|
| 515 |
+
|
| 516 |
+
last_date = current_date
|
| 517 |
+
|
| 518 |
|
| 519 |
if amount < 0:
|
| 520 |
total_lent += abs(amount)
|
|
|
|
| 532 |
last_date = current_date
|
| 533 |
|
| 534 |
# Calculate interest up to end date
|
| 535 |
+
if principal_due > 0 and last_date:
|
| 536 |
+
interest = calculate_interest(principal_due, last_date, end_date_str, interest_rate)
|
| 537 |
+
total_interest += interest
|
| 538 |
+
principal_due += interest
|
| 539 |
+
|
| 540 |
transactions_with_interest.append({
|
| 541 |
'Date': end_date_str,
|
| 542 |
'Description': f'Interest Accrued (to {format_date_display(end_date_str)})',
|
| 543 |
'Bank Account': '-',
|
| 544 |
'Amount': -interest,
|
| 545 |
+
'Balance': -principal_due
|
| 546 |
})
|
| 547 |
+
|
| 548 |
|
| 549 |
# Create detailed transaction DataFrame
|
| 550 |
detailed_df = pd.DataFrame(transactions_with_interest)
|
|
|
|
| 557 |
|
| 558 |
# Summary statistics
|
| 559 |
remaining_balance = balance
|
| 560 |
+
interest_accrued = total_interest
|
| 561 |
+
|
| 562 |
+
remaining_balance = (
|
| 563 |
+
-principal_due if principal_due > 0
|
| 564 |
+
else surplus_to_farmer
|
| 565 |
+
)
|
| 566 |
+
|
| 567 |
|
| 568 |
# Create summary HTML
|
| 569 |
summary_html = f"""
|