girishwangikar commited on
Commit
78873fa
·
verified ·
1 Parent(s): 6158ef0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -16
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
- balance = 0
 
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
- # Calculate interest on outstanding balance since last transaction
473
- if balance < 0 and last_date:
474
- interest = calculate_interest(abs(balance), last_date, current_date, interest_rate)
475
- balance -= interest
 
 
476
  transactions_with_interest.append({
477
  'Date': current_date,
478
  'Description': 'Interest Accrued',
479
  'Bank Account': '-',
480
  'Amount': -interest,
481
- 'Balance': balance
482
  })
483
-
484
- # Add transaction
485
- balance += amount
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 balance < 0 and last_date:
504
- interest = calculate_interest(abs(balance), last_date, end_date_str, interest_rate)
505
- balance -= interest
 
 
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': 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 = abs(remaining_balance) - (total_lent - total_returned)
 
 
 
 
 
 
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"""