File size: 32,910 Bytes
72fd761
 
 
 
 
 
 
 
 
 
96d612d
 
 
 
 
de49b67
586c913
8585c15
72fd761
4dad372
 
 
 
72fd761
 
 
3b6fd8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72fd761
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b6fd8b
 
 
 
 
 
 
 
72fd761
 
 
 
3b6fd8b
72fd761
 
 
 
 
3b6fd8b
72fd761
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b6fd8b
72fd761
3b6fd8b
72fd761
 
 
 
 
3b6fd8b
72fd761
 
 
 
 
3b6fd8b
72fd761
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b6fd8b
72fd761
3b6fd8b
72fd761
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b6fd8b
 
 
72fd761
 
 
 
 
 
 
 
 
 
3b6fd8b
72fd761
3b6fd8b
72fd761
 
 
3b6fd8b
 
72fd761
 
 
 
 
3b6fd8b
 
72fd761
 
 
 
 
96d612d
 
406a71e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b6fd8b
 
 
 
96d612d
406a71e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b6fd8b
406a71e
 
 
 
3b6fd8b
406a71e
 
3b6fd8b
 
406a71e
 
 
3b6fd8b
 
 
406a71e
 
 
 
 
 
 
 
 
3b6fd8b
 
 
 
 
 
 
 
 
 
406a71e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96d612d
3b6fd8b
c38d5c7
72fd761
 
122b3ad
72fd761
 
 
3b6fd8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72fd761
 
 
 
 
122b3ad
72fd761
 
 
 
 
 
122b3ad
72fd761
 
 
3b6fd8b
 
 
 
 
 
 
72fd761
 
 
78873fa
 
72fd761
 
 
78873fa
 
72fd761
 
78873fa
72fd761
 
 
78873fa
 
 
 
 
 
 
72fd761
 
 
 
 
78873fa
72fd761
78873fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72fd761
8585c15
 
 
 
72fd761
8585c15
 
 
 
 
 
 
4faeacc
72fd761
8585c15
72fd761
3b6fd8b
78873fa
 
 
 
 
72fd761
3b6fd8b
 
72fd761
 
78873fa
72fd761
78873fa
72fd761
 
 
 
3b6fd8b
 
 
 
 
 
72fd761
5251dbe
 
 
 
 
78873fa
 
 
 
 
 
 
96d612d
c38d5c7
96d612d
 
 
3b6fd8b
96d612d
 
 
3b6fd8b
96d612d
 
 
3b6fd8b
96d612d
 
 
3b6fd8b
96d612d
 
 
3b6fd8b
96d612d
 
 
72fd761
 
 
 
 
 
 
 
 
 
96d612d
3b6fd8b
96d612d
72fd761
 
 
 
 
96d612d
 
72fd761
 
 
 
 
96d612d
 
3b6fd8b
72fd761
 
 
96d612d
 
72fd761
 
3b6fd8b
406a71e
 
3b6fd8b
72fd761
 
c38d5c7
 
 
122b3ad
 
586c913
 
122b3ad
586c913
72fd761
 
 
1cd5f96
6158ef0
8585c15
 
586c913
72fd761
 
 
3b6fd8b
72fd761
 
 
 
 
96d612d
3b6fd8b
72fd761
 
3b6fd8b
 
 
 
72fd761
 
3b6fd8b
 
 
96d612d
72fd761
 
 
 
 
 
 
 
 
 
96d612d
72fd761
96d612d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72fd761
 
3b6fd8b
 
 
 
 
 
 
 
 
 
72fd761
 
 
3b6fd8b
72fd761
 
 
 
 
3b6fd8b
72fd761
 
 
 
 
 
 
 
 
 
96d612d
72fd761
3b6fd8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72fd761
586c913
 
 
72fd761
c38d5c7
96d612d
72fd761
c38d5c7
406a71e
 
96d612d
 
 
 
72fd761
 
 
 
96d612d
 
 
72fd761
96d612d
72fd761
 
 
 
 
3b6fd8b
 
 
 
 
72fd761
 
3b6fd8b
406a71e
72fd761
406a71e
586c913
 
 
406a71e
586c913
3b6fd8b
586c913
1cd5f96
 
8585c15
 
 
72fd761
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
import gradio as gr
import pandas as pd
import gspread
from google.oauth2.service_account import Credentials
import json
import os
from datetime import datetime, timedelta
import plotly.graph_objects as go
import plotly.express as px
from io import BytesIO
from reportlab.lib import colors
from reportlab.lib.pagesizes import letter, A4
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph, Spacer
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.units import inch
import tempfile
from analytics_tab import AnalyticsTab
from delete_tab import DeleteTab

# Create downloads directory if it doesn't exist
DOWNLOADS_DIR = os.path.join(os.getcwd(), "downloads")
os.makedirs(DOWNLOADS_DIR, exist_ok=True)

# Google Sheets Setup
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']

def format_indian_currency(amount):
    """Format number in Indian currency format (1,00,000)"""
    try:
        amount = float(amount)
        s = f"{abs(amount):.2f}"
        integer_part, decimal_part = s.split('.')
        
        # Indian numbering system
        if len(integer_part) <= 3:
            formatted = integer_part
        else:
            last_three = integer_part[-3:]
            remaining = integer_part[:-3]
            
            # Add commas every 2 digits for the remaining part
            formatted_remaining = ""
            for i, digit in enumerate(reversed(remaining)):
                if i > 0 and i % 2 == 0:
                    formatted_remaining = "," + formatted_remaining
                formatted_remaining = digit + formatted_remaining
            
            formatted = formatted_remaining + "," + last_three
        
        result = f"{formatted}.{decimal_part}"
        return f"-{result}" if amount < 0 else result
    except:
        return str(amount)

def parse_date(date_str):
    """Parse date in DD-MM-YY format to YYYY-MM-DD"""
    try:
        # Try DD-MM-YY format first
        parts = date_str.strip().split('-')
        if len(parts) == 3:
            day, month, year = parts
            # Handle 2-digit year
            if len(year) == 2:
                year = f"20{year}"
            return f"{year}-{month.zfill(2)}-{day.zfill(2)}"
    except:
        pass
    return date_str

def format_date_display(date_str):
    """Format date from YYYY-MM-DD to DD-MM-YY for display"""
    try:
        date_obj = datetime.strptime(date_str, "%Y-%m-%d")
        return date_obj.strftime("%d-%m-%y")
    except:
        return date_str

def get_google_sheets_client():
    """Initialize Google Sheets client"""
    try:
        # Get credentials from environment variable
        creds_json = os.environ.get('GOOGLE_SHEETS_CREDENTIALS')
        if not creds_json:
            raise ValueError("GOOGLE_SHEETS_CREDENTIALS not found in environment")
        
        creds_dict = json.loads(creds_json)
        creds = Credentials.from_service_account_info(creds_dict, scopes=SCOPES)
        client = gspread.authorize(creds)
        return client
    except Exception as e:
        raise Exception(f"Failed to initialize Google Sheets client: {str(e)}")

def get_sheet():
    """Get the Google Sheet"""
    client = get_google_sheets_client()
    sheet_id = os.environ.get('SHEET_ID')
    if not sheet_id:
        raise ValueError("SHEET_ID not found in environment")
    return client.open_by_key(sheet_id)

def initialize_sheets():
    """Initialize sheets with headers if empty"""
    try:
        spreadsheet = get_sheet()
        
        # Initialize Transactions sheet
        try:
            trans_sheet = spreadsheet.worksheet("Transactions")
        except:
            trans_sheet = spreadsheet.add_worksheet(title="Transactions", rows="1000", cols="10")
        
        if not trans_sheet.row_values(1):
            trans_sheet.update('A1:F1', [['Date', 'Farmer Name', 'Bank Account', 'Amount', 'Type', 'Timestamp']])
        
        # Initialize Metadata sheet
        try:
            meta_sheet = spreadsheet.worksheet("Metadata")
        except:
            meta_sheet = spreadsheet.add_worksheet(title="Metadata", rows="1000", cols="10")
        
        if not meta_sheet.row_values(1):
            meta_sheet.update('A1:B1', [['Type', 'Value']])
            meta_sheet.update('A2:B2', [['farmer', '']])
            meta_sheet.update('A3:B3', [['bank', '']])
        
        return True
    except Exception as e:
        print(f"Error initializing sheets: {str(e)}")
        return False

def get_farmers():
    """Get list of registered farmers"""
    try:
        spreadsheet = get_sheet()
        meta_sheet = spreadsheet.worksheet("Metadata")
        data = meta_sheet.get_all_values()
        
        farmers = []
        for row in data:
            if len(row) >= 2 and row[0] == 'farmer' and row[1]:
                farmers.extend([f.strip() for f in row[1].split(',') if f.strip()])
        
        return sorted(list(set(farmers)))
    except Exception as e:
        print(f"Error getting farmers: {str(e)}")
        return []

def get_banks():
    """Get list of registered bank accounts"""
    try:
        spreadsheet = get_sheet()
        meta_sheet = spreadsheet.worksheet("Metadata")
        data = meta_sheet.get_all_values()
        
        banks = []
        for row in data:
            if len(row) >= 2 and row[0] == 'bank' and row[1]:
                banks.extend([b.strip() for b in row[1].split(',') if b.strip()])
        
        return sorted(list(set(banks)))
    except Exception as e:
        print(f"Error getting banks: {str(e)}")
        return []

def refresh_farmers():
    """Refresh farmer dropdown"""
    return gr.Dropdown(choices=get_farmers())

def refresh_banks():
    """Refresh bank dropdown"""
    return gr.Dropdown(choices=get_banks())

def register_farmer(farmer_name):
    """Register a new farmer"""
    try:
        if not farmer_name or not farmer_name.strip():
            return "❌ Please enter a farmer name"
        
        farmer_name = farmer_name.strip()
        farmers = get_farmers()
        
        if farmer_name in farmers:
            return f"❌ Farmer '{farmer_name}' is already registered"
        
        spreadsheet = get_sheet()
        meta_sheet = spreadsheet.worksheet("Metadata")
        data = meta_sheet.get_all_values()
        
        # Find the farmer row
        farmer_row = None
        for idx, row in enumerate(data):
            if len(row) >= 1 and row[0] == 'farmer':
                farmer_row = idx + 1
                break
        
        if farmer_row:
            current_farmers = data[farmer_row - 1][1] if len(data[farmer_row - 1]) > 1 else ""
            new_farmers = f"{current_farmers},{farmer_name}" if current_farmers else farmer_name
            meta_sheet.update_cell(farmer_row, 2, new_farmers)
        
        return f"βœ… Farmer '{farmer_name}' registered successfully!"
    except Exception as e:
        return f"❌ Error registering farmer: {str(e)}"

def register_bank(bank_name):
    """Register a new bank account"""
    try:
        if not bank_name or not bank_name.strip():
            return "❌ Please enter a bank account name"
        
        bank_name = bank_name.strip()
        banks = get_banks()
        
        if bank_name in banks:
            return f"❌ Bank account '{bank_name}' is already registered"
        
        spreadsheet = get_sheet()
        meta_sheet = spreadsheet.worksheet("Metadata")
        data = meta_sheet.get_all_values()
        
        # Find the bank row
        bank_row = None
        for idx, row in enumerate(data):
            if len(row) >= 1 and row[0] == 'bank':
                bank_row = idx + 1
                break
        
        if bank_row:
            current_banks = data[bank_row - 1][1] if len(data[bank_row - 1]) > 1 else ""
            new_banks = f"{current_banks},{bank_name}" if current_banks else bank_name
            meta_sheet.update_cell(bank_row, 2, new_banks)
        
        return f"βœ… Bank account '{bank_name}' registered successfully!"
    except Exception as e:
        return f"❌ Error registering bank: {str(e)}"

def add_transaction(date, farmer_name, bank_account, amount, trans_type):
    """Add a new transaction"""
    try:
        if not all([date, farmer_name, bank_account, amount, trans_type]):
            return "❌ Please fill in all fields"
        
        farmer_name = farmer_name.strip()
        bank_account = bank_account.strip()
        
        # Check if farmer exists
        farmers = get_farmers()
        if farmer_name not in farmers:
            return f"❌ Farmer '{farmer_name}' not found. Please register the farmer first or check spelling."
        
        # Check if bank exists
        banks = get_banks()
        if bank_account not in banks:
            return f"❌ Bank account '{bank_account}' not found. Please register the bank first."
        
        try:
            amount = float(amount)
        except:
            return "❌ Amount must be a valid number"
        
        # Parse and convert date
        formatted_date = parse_date(date)
        
        # Format amount based on type
        if trans_type == "Outgoing (Lent to Farmer)":
            amount = -abs(amount)
        else:  # Incoming
            amount = abs(amount)
        
        spreadsheet = get_sheet()
        trans_sheet = spreadsheet.worksheet("Transactions")
        
        timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        trans_sheet.append_row([formatted_date, farmer_name, bank_account, amount, trans_type, timestamp])
        
        return f"βœ… Transaction added successfully! Amount: β‚Ή{format_indian_currency(amount)}"
    except Exception as e:
        return f"❌ Error adding transaction: {str(e)}"

def calculate_interest(principal, start_date, end_date, interest_rate=2.0):
    """Calculate monthly interest (default 2%) prorated daily"""
    try:
        start = datetime.strptime(start_date, "%Y-%m-%d")
        end = datetime.strptime(end_date, "%Y-%m-%d")
        days = (end - start).days
        
        # interest_rate% per month = interest_rate/30 per day
        daily_rate = (interest_rate / 100) / 30
        interest = principal * daily_rate * days
        return interest
    except:
        return 0

def create_pdf_report(farmer_name, df, total_lent, total_returned, interest_accrued, remaining_balance):
    """Generate PDF report of transaction history"""
    try:
        # Create PDF with custom name in current directory
        pdf_filename = f"{farmer_name.replace(' ', '_')}_transactions_with_interest.pdf"
        
        # Remove old file if exists
        if os.path.exists(pdf_filename):
            os.remove(pdf_filename)
        
        doc = SimpleDocTemplate(pdf_filename, pagesize=letter, rightMargin=30, leftMargin=30, topMargin=30, bottomMargin=18)
        
        elements = []
        styles = getSampleStyleSheet()
        
        # Title
        title_style = ParagraphStyle(
            'CustomTitle',
            parent=styles['Heading1'],
            fontSize=24,
            textColor=colors.HexColor('#1f77b4'),
            spaceAfter=30,
            alignment=1
        )
        elements.append(Paragraph(f"Transaction History: {farmer_name}", title_style))
        elements.append(Spacer(1, 12))
        
        # Summary statistics
        summary_data = [
            ['Metric', 'Amount (β‚Ή)'],
            ['Total Lent', format_indian_currency(total_lent)],
            ['Total Returned', format_indian_currency(total_returned)],
            ['Interest Accrued', format_indian_currency(interest_accrued)],
            ['Current Balance', f'{format_indian_currency(abs(remaining_balance))} {"(Farmer owes you)" if remaining_balance < 0 else "(You owe farmer)"}']
        ]
        
        summary_table = Table(summary_data, colWidths=[3*inch, 3*inch])
        summary_table.setStyle(TableStyle([
            ('BACKGROUND', (0, 0), (-1, 0), colors.HexColor('#1f77b4')),
            ('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
            ('ALIGN', (0, 0), (-1, -1), 'CENTER'),
            ('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
            ('FONTSIZE', (0, 0), (-1, 0), 14),
            ('BOTTOMPADDING', (0, 0), (-1, 0), 12),
            ('BACKGROUND', (0, 1), (-1, -1), colors.beige),
            ('GRID', (0, 0), (-1, -1), 1, colors.black),
            ('FONTNAME', (0, 1), (-1, -1), 'Helvetica'),
            ('FONTSIZE', (0, 1), (-1, -1), 12),
            ('ROWBACKGROUNDS', (0, 1), (-1, -1), [colors.white, colors.lightgrey]),
        ]))
        elements.append(summary_table)
        elements.append(Spacer(1, 20))
        
        # Transaction history title
        elements.append(Paragraph("Detailed Transaction History", styles['Heading2']))
        elements.append(Spacer(1, 12))
        
        # Transaction table with highlighted interest rows
        trans_data = [['Date', 'Description', 'Bank Account', 'Amount (β‚Ή)', 'Balance (β‚Ή)']]
        
        for _, row in df.iterrows():
            trans_data.append([
                format_date_display(str(row['Date'])),
                str(row['Description']),
                str(row['Bank Account']),
                format_indian_currency(row['Amount']),
                format_indian_currency(row['Balance'])
            ])
        
        trans_table = Table(trans_data, colWidths=[1.2*inch, 1.8*inch, 1.5*inch, 1.2*inch, 1.2*inch])
        
        # Base style
        table_style = [
            ('BACKGROUND', (0, 0), (-1, 0), colors.HexColor('#2ca02c')),
            ('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
            ('ALIGN', (0, 0), (-1, -1), 'CENTER'),
            ('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
            ('FONTSIZE', (0, 0), (-1, 0), 10),
            ('BOTTOMPADDING', (0, 0), (-1, 0), 12),
            ('GRID', (0, 0), (-1, -1), 1, colors.black),
            ('FONTNAME', (0, 1), (-1, -1), 'Helvetica'),
            ('FONTSIZE', (0, 1), (-1, -1), 8),
        ]
        
        # Highlight interest rows in red
        for idx, row in df.iterrows():
            row_num = idx + 1  # +1 because header is row 0
            if 'Interest Accrued' in str(row['Description']):
                table_style.append(('BACKGROUND', (0, row_num), (-1, row_num), colors.Color(1, 0.8, 0.8)))
                table_style.append(('TEXTCOLOR', (0, row_num), (-1, row_num), colors.red))
        
        trans_table.setStyle(TableStyle(table_style))
        elements.append(trans_table)
        
        # Build PDF
        doc.build(elements)
        
        # Verify file was created and get its absolute path
        abs_path = os.path.abspath(pdf_filename)
        if os.path.exists(abs_path):
            file_size = os.path.getsize(abs_path)
            print(f"PDF created successfully: {abs_path}, Size: {file_size} bytes")
            return abs_path
        else:
            print(f"PDF file not found at: {abs_path}")
            return None
            
    except Exception as e:
        print(f"Error creating PDF: {str(e)}")
        import traceback
        traceback.print_exc()
        return None

def get_farmer_report(farmer_name, end_date=None, interest_rate=None):
    """Generate complete report for a farmer with interest calculations and PDF"""
    try:
        if not farmer_name or not farmer_name.strip():
            return None, None, "❌ Please enter a farmer name", None, None
        
        farmer_name = farmer_name.strip()
        
        # Set default interest rate if not provided
        if interest_rate is None or interest_rate == "":
            interest_rate = 2.0
        else:
            try:
                interest_rate = float(interest_rate)
            except:
                return None, None, "❌ Interest rate must be a valid number", None, None
        
        # Set end date
        if end_date and end_date.strip():
            try:
                end_date_parsed = parse_date(end_date)
                end_date_obj = datetime.strptime(end_date_parsed, "%Y-%m-%d")
            except:
                return None, None, "❌ Invalid date format. Please use DD-MM-YY", None, None
        else:
            end_date_obj = datetime.now()
        
        end_date_str = end_date_obj.strftime("%Y-%m-%d")
        
        spreadsheet = get_sheet()
        trans_sheet = spreadsheet.worksheet("Transactions")
        data = trans_sheet.get_all_values()
        
        if len(data) <= 1:
            return None, None, f"❌ No transactions found for farmer '{farmer_name}'", None, None
        
        # Create DataFrame
        df = pd.DataFrame(data[1:], columns=data[0])
        df = df[df['Farmer Name'] == farmer_name]
        
        if df.empty:
            return None, None, f"❌ No transactions found for farmer '{farmer_name}'", None, None
        
        df['Amount'] = pd.to_numeric(df['Amount'], errors='coerce')
        df['Date'] = pd.to_datetime(df['Date'], errors='coerce')
        
        # Filter transactions up to end_date
        df = df[df['Date'] <= end_date_obj]
        
        if df.empty:
            return None, None, f"❌ No transactions found for farmer '{farmer_name}' up to {format_date_display(end_date_str)}", None, None
        
        df = df.sort_values('Date')
        
        # Calculate running balance with interest
        principal_due = 0        # actual loan outstanding
        total_interest = 0       # accumulated interest
        total_lent = 0
        total_returned = 0
        last_date = None
        surplus_to_farmer = 0    # if farmer paid extra

        
        transactions_with_interest = []

        for idx, row in df.iterrows():
            amount = row['Amount']
            current_date = row['Date'].strftime("%Y-%m-%d")
        
            # 1) Apply interest ONLY on principal_due (only when farmer owes us)
            if principal_due > 0 and last_date:
                interest = calculate_interest(principal_due, last_date, current_date, interest_rate)
                total_interest += interest
                principal_due += interest
        
                transactions_with_interest.append({
                    'Date': current_date,
                    'Description': 'Interest Accrued',
                    'Bank Account': '-',
                    'Amount': -interest,
                    'Balance': -principal_due
                })
        
            # 2) Process transaction
            if amount < 0:
                # We lent money
                lend = abs(amount)
                principal_due += lend
                total_lent += lend
        
            else:
                # Farmer paid money
                payment = amount
                total_returned += payment
        
                if payment <= principal_due:
                    principal_due -= payment
                else:
                    surplus_to_farmer += (payment - principal_due)
                    principal_due = 0
        
            # Record transaction
            transactions_with_interest.append({
                'Date': current_date,
                'Description': row['Type'],
                'Bank Account': row['Bank Account'],
                'Amount': amount,
                'Balance': -principal_due if principal_due > 0 else surplus_to_farmer
            })
        
            last_date = current_date

            
            # if amount < 0:
            #     total_lent += abs(amount)
            # else:
            #     total_returned += amount
            
            # transactions_with_interest.append({
            #     'Date': current_date,
            #     'Description': row['Type'],
            #     'Bank Account': row['Bank Account'],
            #     'Amount': amount,
            #     'Balance': -principal_due if principal_due > 0 else surplus_to_farmer
            # })

            
            # last_date = current_date
        
        # Calculate interest up to end date
        if principal_due > 0 and last_date:
            interest = calculate_interest(principal_due, last_date, end_date_str, interest_rate)
            total_interest += interest
            principal_due += interest
        
            transactions_with_interest.append({
                'Date': end_date_str,
                'Description': f'Interest Accrued (to {format_date_display(end_date_str)})',
                'Bank Account': '-',
                'Amount': -interest,
                'Balance': -principal_due
            })

        
        # Create detailed transaction DataFrame
        detailed_df = pd.DataFrame(transactions_with_interest)
        
        # Format amounts in detailed_df for display
        detailed_df_display = detailed_df.copy()
        detailed_df_display['Date'] = detailed_df_display['Date'].apply(format_date_display)
        detailed_df_display['Amount'] = detailed_df_display['Amount'].apply(format_indian_currency)
        detailed_df_display['Balance'] = detailed_df_display['Balance'].apply(format_indian_currency)
        
        # Summary statistics
        remaining_balance = (
            -principal_due if principal_due > 0
            else surplus_to_farmer
        )

        interest_accrued = total_interest

        remaining_balance = (
            -principal_due if principal_due > 0 
            else surplus_to_farmer
        )

        
        # Create summary HTML
        summary_html = f"""
        <div style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); padding: 30px; border-radius: 15px; margin: 20px 0;">
            <h2 style="color: white; text-align: center; margin-bottom: 25px; font-size: 28px;">πŸ“Š Summary for {farmer_name}</h2>
            <p style="color: white; text-align: center; margin-bottom: 15px; font-size: 16px;">Report calculated up to: {format_date_display(end_date_str)} | Interest Rate: {interest_rate}% per month</p>
            <table style="width: 100%; border-collapse: separate; border-spacing: 0 15px;">
                <tr style="background-color: rgba(255,255,255,0.95); border-radius: 10px;">
                    <td style="padding: 20px; font-size: 18px; font-weight: bold; border-radius: 10px 0 0 10px; color: #333;">πŸ’° Total Lent</td>
                    <td style="padding: 20px; font-size: 24px; font-weight: bold; text-align: right; border-radius: 0 10px 10px 0; color: #e74c3c;">β‚Ή{format_indian_currency(total_lent)}</td>
                </tr>
                <tr style="background-color: rgba(255,255,255,0.95); border-radius: 10px;">
                    <td style="padding: 20px; font-size: 18px; font-weight: bold; border-radius: 10px 0 0 10px; color: #333;">πŸ’΅ Total Returned</td>
                    <td style="padding: 20px; font-size: 24px; font-weight: bold; text-align: right; border-radius: 0 10px 10px 0; color: #27ae60;">β‚Ή{format_indian_currency(total_returned)}</td>
                </tr>
                <tr style="background-color: rgba(255,255,255,0.95); border-radius: 10px;">
                    <td style="padding: 20px; font-size: 18px; font-weight: bold; border-radius: 10px 0 0 10px; color: #333;">πŸ“ˆ Interest Accrued</td>
                    <td style="padding: 20px; font-size: 24px; font-weight: bold; text-align: right; border-radius: 0 10px 10px 0; color: #f39c12;">β‚Ή{format_indian_currency(interest_accrued)}</td>
                </tr>
                <tr style="background-color: rgba(255,255,255,0.95); border-radius: 10px;">
                    <td style="padding: 20px; font-size: 18px; font-weight: bold; border-radius: 10px 0 0 10px; color: #333;">πŸ”’ Current Balance</td>
                    <td style="padding: 20px; font-size: 24px; font-weight: bold; text-align: right; border-radius: 0 10px 10px 0; color: {'#e74c3c' if remaining_balance < 0 else '#27ae60'};">β‚Ή{format_indian_currency(abs(remaining_balance))}<br><span style="font-size: 14px; color: #666;">{'(Farmer owes you)' if remaining_balance < 0 else '(You owe farmer)'}</span></td>
                </tr>
            </table>
        </div>
        """
        
        # Create visualizations
        bank_dist = df.groupby('Bank Account')['Amount'].apply(lambda x: abs(x[x < 0].sum())).reset_index()
        bank_dist.columns = ['Bank Account', 'Amount Lent']
        
        fig_bank = go.Figure(go.Bar(
            x=bank_dist['Amount Lent'],
            y=bank_dist['Bank Account'],
            orientation='h',
            marker_color='lightblue',
            text=bank_dist['Amount Lent'].apply(lambda x: f'β‚Ή{format_indian_currency(x)}'),
            textposition='auto'
        ))
        fig_bank.update_layout(
            title=f"Money Lent to {farmer_name} by Bank Account",
            xaxis_title="Amount (β‚Ή)",
            yaxis_title="Bank Account",
            height=350,
            font=dict(size=12)
        )
        
        fig_pie = go.Figure(data=[go.Pie(
            labels=['Returned', 'Outstanding (with interest)'],
            values=[total_returned, abs(remaining_balance)],
            marker_colors=['#27ae60', '#e74c3c'],
            textinfo='label+percent+value',
            texttemplate='%{label}<br>β‚Ή' + format_indian_currency(total_returned) + '<br>(%{percent})'
        )])
        fig_pie.update_layout(
            title=f"Payment Status for {farmer_name}",
            height=350,
            font=dict(size=12)
        )
        
        # Generate PDF with original numeric values
        pdf_path = create_pdf_report(farmer_name, detailed_df, total_lent, total_returned, interest_accrued, remaining_balance)
        
        return detailed_df_display, pdf_path, summary_html, fig_bank, fig_pie
    
    except Exception as e:
        import traceback
        traceback.print_exc()
        return None, None, f"❌ Error generating report: {str(e)}", None, None


def clear_farmer_report():
    """Clear all report outputs"""
    return None, None, "", None, None
    
# Initialize sheets on startup
initialize_sheets()

# Initialize Analytics Tab
analytics = AnalyticsTab(get_sheet, get_farmers, format_indian_currency, format_date_display)
delete_tab = DeleteTab(get_sheet, get_farmers, format_indian_currency, format_date_display)


# Gradio Interface
with gr.Blocks(title="Farmer Ledger Tracker", theme=gr.themes.Soft()) as app:
    gr.Markdown("# 🌾 Farmer Ledger Tracker")
    gr.Markdown("Track loans to farmers with customizable monthly interest (default 2%, prorated daily)")
    
    with gr.Tabs():
        # Tab 1: Add Transactions
        with gr.Tab("πŸ“ Add Transaction"):
            
            gr.Markdown("## Add New Transaction")
            gr.Markdown("Enter transaction details below (Date format: DD-MM-YY):")
            
            with gr.Row():
                trans_date = gr.Textbox(label="Date (DD-MM-YY)", placeholder="15-01-24", scale=1)
                with gr.Row():
                    trans_farmer = gr.Dropdown(label="Farmer Name", choices=[], allow_custom_value=True, scale=9)
                    refresh_farmer_btn_tab1 = gr.Button("πŸ”„", scale=1, size="sm")
            
            with gr.Row():
                with gr.Row():
                    trans_bank = gr.Dropdown(label="Bank Account", choices=[], allow_custom_value=True, scale=9)
                    refresh_bank_btn_tab1 = gr.Button("πŸ”„", scale=1, size="sm")
                trans_amount = gr.Number(label="Amount (β‚Ή)", value=0, scale=1)
            
            trans_type = gr.Radio(
                label="Transaction Type",
                choices=["Outgoing (Lent to Farmer)", "Incoming (Farmer Paid Back)"],
                value="Outgoing (Lent to Farmer)"
            )
            
            add_trans_btn = gr.Button("Add Transaction", variant="primary", size="lg")
            trans_status = gr.Textbox(label="Status", interactive=False)
            
            gr.Markdown("---")
            
            # Register entities section - smaller and less prominent
            with gr.Accordion("βš™οΈ Register New Farmer or Bank Account", open=False):
                gr.Markdown("### Register New Entities")
                gr.Markdown("*Use this section to add new farmers or bank accounts to the system*")
                
                with gr.Row():
                    with gr.Column():
                        new_farmer_input = gr.Textbox(label="New Farmer Name", placeholder="Enter farmer name", scale=1)
                        register_farmer_btn = gr.Button("Register Farmer", size="sm")
                        farmer_status = gr.Textbox(label="Status", interactive=False, scale=1)
                    
                    with gr.Column():
                        new_bank_input = gr.Textbox(label="New Bank Account", placeholder="Enter bank name", scale=1)
                        register_bank_btn = gr.Button("Register Bank Account", size="sm")
                        bank_status = gr.Textbox(label="Status", interactive=False, scale=1)
            
            # Event handlers for Tab 1
            refresh_farmer_btn_tab1.click(
                fn=refresh_farmers,
                outputs=[trans_farmer]
            )
            
            refresh_bank_btn_tab1.click(
                fn=refresh_banks,
                outputs=[trans_bank]
            )
            
            register_farmer_btn.click(
                fn=register_farmer,
                inputs=[new_farmer_input],
                outputs=[farmer_status]
            )
            
            register_bank_btn.click(
                fn=register_bank,
                inputs=[new_bank_input],
                outputs=[bank_status]
            )
            
            add_trans_btn.click(
                fn=add_transaction,
                inputs=[trans_date, trans_farmer, trans_bank, trans_amount, trans_type],
                outputs=[trans_status]
            )
        
        # Tab 2: View Reports
        with gr.Tab("πŸ“Š Farmer Reports"):
            gr.Markdown("## Generate Farmer Report")
            
            with gr.Row():
                report_farmer = gr.Dropdown(
                    label="Select Farmer",
                    choices=[],
                    allow_custom_value=True,
                    scale=9
                )
                refresh_farmer_btn_tab2 = gr.Button("πŸ”„", scale=1, size="sm")
            
            gr.Markdown("### Optional: Customize Report Parameters")
            gr.Markdown("*Leave blank to use current date and 2% default interest rate*")
            
            with gr.Row():
                report_end_date = gr.Textbox(
                    label="Calculate up to Date (DD-MM-YY)",
                    placeholder="Leave blank for today",
                    scale=1
                )
                report_interest_rate = gr.Number(
                    label="Interest Rate (% per month)",
                    value=2.0,
                    placeholder="2.0",
                    scale=1
                )
            
            with gr.Row():
                generate_report_btn = gr.Button("Generate Report", variant="primary", size="lg")
                clear_report_btn = gr.Button("Clear Report", variant="secondary", size="lg")
            
            # Summary section
            report_summary = gr.HTML()
            
            # Download PDF
            with gr.Row():
                pdf_download = gr.File(label="πŸ“„ Download Transaction History PDF", interactive=False)
            
            gr.Markdown("---")
            
            # Charts
            with gr.Row():
                bank_chart = gr.Plot(label="Distribution by Bank Account")
                pie_chart = gr.Plot(label="Payment Status")
            
            gr.Markdown("---")
            
            # Transaction table
            report_df = gr.Dataframe(
                label="Detailed Transaction History (with Interest Calculations)",
                interactive=False,
                wrap=True
            )
            
            # Event handlers for Tab 2
            refresh_farmer_btn_tab2.click(
                fn=refresh_farmers,
                outputs=[report_farmer]
            )
            
            generate_report_btn.click(
                fn=get_farmer_report,
                inputs=[report_farmer, report_end_date, report_interest_rate],
                outputs=[report_df, pdf_download, report_summary, bank_chart, pie_chart]
            )
            
            clear_report_btn.click(
                fn=clear_farmer_report,
                inputs=[],
                outputs=[report_df, pdf_download, report_summary, bank_chart, pie_chart]
            )
        
        # Tab 3: Analytics & Insights
        analytics.create_tab()

        delete_tab.create_tab()


if __name__ == "__main__":
    app.launch()