-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
659 lines (530 loc) · 21.7 KB
/
app.py
File metadata and controls
659 lines (530 loc) · 21.7 KB
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
import os
import tempfile
import threading
import datetime
from flask import Flask, request, jsonify, send_file, render_template
from flask_cors import CORS
import io
import json
import uuid
# Import custom modules
from database import DatabaseClient
from ocr_processor import OCRProcessor
from gst_classifier import GSTClassifier
from report_generator import ReportGenerator
from trend_analyzer import TrendAnalyzer
from ai_processor import AIProcessor
# For batch processing
batch_jobs = {}
# Create Flask app
app = Flask(__name__,
static_url_path='',
static_folder='static',
template_folder='templates')
CORS(app)
# Global instances
db = DatabaseClient()
ocr_processor = OCRProcessor()
gst_classifier = GSTClassifier()
report_generator = ReportGenerator()
trend_analyzer = TrendAnalyzer(db)
ai_processor = AIProcessor()
# Routes
@app.route('/')
def index():
return render_template('index.html')
@app.route('/api/invoices', methods=['GET'])
def get_invoices():
try:
invoices = db.get_invoices()
return jsonify(invoices)
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/api/invoice/<invoice_id>', methods=['GET'])
def get_invoice(invoice_id):
try:
invoice = db.get_invoice(invoice_id)
items = db.get_items_by_invoice(invoice_id)
if not invoice:
return jsonify({"error": "Invoice not found"}), 404
return jsonify({
"invoice": invoice,
"items": items
})
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/api/gst-slabs', methods=['GET'])
def get_gst_slabs():
try:
gst_slabs = db.get_gst_slabs()
return jsonify(gst_slabs)
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/api/process-invoice', methods=['POST'])
def process_invoice():
if 'file' not in request.files:
return jsonify({"error": "No file part"}), 400
file = request.files['file']
if file.filename == '':
return jsonify({"error": "No selected file"}), 400
try:
# Save uploaded file temporarily
with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(file.filename)[1]) as tmp:
file.save(tmp.name)
temp_file_path = tmp.name
# Extract text using OCR
extracted_text = ocr_processor.process_file(temp_file_path)
if not extracted_text:
return jsonify({"error": "No text could be extracted from the invoice"}), 400
# Extract structured data
items_data = ocr_processor.extract_items(extracted_text)
if not items_data:
return jsonify({"error": "Could not identify item details in the invoice"}), 400
# Classify items into GST slabs
classified_items = gst_classifier.classify_items(items_data)
# Save to database
invoice_id = db.insert_invoice(
file_name=file.filename,
file_type=file.content_type,
raw_text=extracted_text
)
if not invoice_id:
return jsonify({"error": "Failed to save invoice to database"}), 500
# Save classified items
db.insert_items(invoice_id, classified_items)
# Calculate GST breakdown
gst_breakdown = {}
for item in classified_items:
gst_rate = item.get("gst_rate", 0)
if gst_rate not in gst_breakdown:
gst_breakdown[gst_rate] = {
"taxable_amount": 0,
"tax_amount": 0
}
taxable_amount = item.get("total", 0)
tax_amount = taxable_amount * (gst_rate / 100)
gst_breakdown[gst_rate]["taxable_amount"] += taxable_amount
gst_breakdown[gst_rate]["tax_amount"] += tax_amount
# Clean up the temporary file
os.unlink(temp_file_path)
return jsonify({
"success": True,
"invoice_id": invoice_id,
"items": classified_items,
"gst_breakdown": gst_breakdown
})
except Exception as e:
# Clean up in case of error
if 'temp_file_path' in locals():
os.unlink(temp_file_path)
return jsonify({"error": str(e)}), 500
@app.route('/api/update-item', methods=['POST'])
def update_item():
try:
item_data = request.json
if not item_data or "id" not in item_data:
return jsonify({"error": "Invalid item data"}), 400
success = db.update_item(item_data)
if not success:
return jsonify({"error": "Failed to update item"}), 500
return jsonify({"success": True})
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/api/reports/pdf/<invoice_id>', methods=['GET'])
def generate_pdf_report(invoice_id):
try:
invoice = db.get_invoice(invoice_id)
if not invoice:
return jsonify({"error": "Invoice not found"}), 404
items = db.get_items_by_invoice(invoice_id)
if not items:
return jsonify({"error": "No items found for this invoice"}), 404
# Calculate GST breakdown
gst_breakdown = {}
for item in items:
gst_rate = item.get("gst_rate", 0)
if gst_rate not in gst_breakdown:
gst_breakdown[gst_rate] = {
"taxable_amount": 0,
"tax_amount": 0
}
taxable_amount = item.get("total", 0)
tax_amount = taxable_amount * (gst_rate / 100)
gst_breakdown[gst_rate]["taxable_amount"] += taxable_amount
gst_breakdown[gst_rate]["tax_amount"] += tax_amount
# Generate PDF report
pdf_report = report_generator.generate_pdf_report(invoice_id, items, gst_breakdown)
# Return PDF as file attachment
return send_file(
io.BytesIO(pdf_report),
mimetype='application/pdf',
as_attachment=True,
download_name=f"invoice_{invoice_id}_report.pdf"
)
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/api/reports/json/<invoice_id>', methods=['GET'])
def generate_json_report(invoice_id):
try:
invoice = db.get_invoice(invoice_id)
if not invoice:
return jsonify({"error": "Invoice not found"}), 404
items = db.get_items_by_invoice(invoice_id)
if not items:
return jsonify({"error": "No items found for this invoice"}), 404
# Calculate GST breakdown
gst_breakdown = {}
for item in items:
gst_rate = item.get("gst_rate", 0)
if gst_rate not in gst_breakdown:
gst_breakdown[gst_rate] = {
"taxable_amount": 0,
"tax_amount": 0
}
taxable_amount = item.get("total", 0)
tax_amount = taxable_amount * (gst_rate / 100)
gst_breakdown[gst_rate]["taxable_amount"] += taxable_amount
gst_breakdown[gst_rate]["tax_amount"] += tax_amount
# Generate JSON report
json_report = report_generator.generate_json_report(invoice_id, items, gst_breakdown)
# Return JSON report
return send_file(
io.BytesIO(json_report.encode('utf-8')),
mimetype='application/json',
as_attachment=True,
download_name=f"invoice_{invoice_id}_report.json"
)
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/api/reports/gstr1', methods=['POST'])
def generate_gstr1_report():
try:
data = request.json
if not data or "start_date" not in data or "end_date" not in data:
return jsonify({"error": "Start date and end date are required"}), 400
start_date = data["start_date"]
end_date = data["end_date"]
# Fetch all invoices
invoices = db.get_invoices()
# Filter invoices by date range
filtered_invoices = [
inv for inv in invoices
if start_date <= inv["created_at"] <= end_date
]
if not filtered_invoices:
return jsonify({"error": "No invoices found in the selected date range"}), 404
# Get items for filtered invoices
filtered_items = []
for invoice in filtered_invoices:
items = db.get_items_by_invoice(invoice["id"])
filtered_items.extend(items)
# Generate GSTR-1 compatible report
gstr1_report = report_generator.generate_gstr1_report(filtered_invoices, filtered_items)
# Return CSV report
return send_file(
io.BytesIO(gstr1_report.encode('utf-8')),
mimetype='text/csv',
as_attachment=True,
download_name=f"GSTR1_report_{start_date}_to_{end_date}.csv"
)
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/api/batch/process', methods=['POST'])
def batch_process_invoices():
if 'files' not in request.files:
return jsonify({"error": "No files part"}), 400
files = request.files.getlist('files')
if not files or len(files) == 0 or files[0].filename == '':
return jsonify({"error": "No files selected"}), 400
# Create a batch job
batch_id = str(uuid.uuid4())
file_names = [file.filename for file in files]
# Save files temporarily
temp_files = []
for file in files:
with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(file.filename)[1]) as tmp:
file.save(tmp.name)
temp_files.append({
'path': tmp.name,
'name': file.filename,
'content_type': file.content_type
})
# Create batch job entry
batch_jobs[batch_id] = {
'id': batch_id,
'status': 'processing',
'total_files': len(files),
'processed_files': 0,
'successful_files': 0,
'failed_files': 0,
'started_at': str(datetime.datetime.now()),
'completed_at': None,
'results': [],
'files': file_names
}
# Start processing in background
thread = threading.Thread(
target=_process_batch,
args=(batch_id, temp_files)
)
thread.daemon = True # Daemon threads are killed when the main program exits
thread.start()
return jsonify({
"success": True,
"batch_id": batch_id,
"message": f"Processing {len(files)} files in the background",
"total_files": len(files)
})
def _process_batch(batch_id, temp_files):
"""Background task to process multiple invoice files"""
batch_info = batch_jobs[batch_id]
try:
for file_info in temp_files:
try:
# Extract text using OCR
extracted_text = ocr_processor.process_file(file_info['path'])
if not extracted_text:
batch_info['failed_files'] += 1
batch_info['results'].append({
'file_name': file_info['name'],
'success': False,
'error': "No text could be extracted from the invoice"
})
continue
# Extract structured data
items_data = ocr_processor.extract_items(extracted_text)
if not items_data:
batch_info['failed_files'] += 1
batch_info['results'].append({
'file_name': file_info['name'],
'success': False,
'error': "Could not identify item details in the invoice"
})
continue
# Classify items into GST slabs
classified_items = gst_classifier.classify_items(items_data)
# Save to database
invoice_id = db.insert_invoice(
file_name=file_info['name'],
file_type=file_info['content_type'],
raw_text=extracted_text
)
if not invoice_id:
batch_info['failed_files'] += 1
batch_info['results'].append({
'file_name': file_info['name'],
'success': False,
'error': "Failed to save invoice to database"
})
continue
# Save classified items
db.insert_items(invoice_id, classified_items)
# Calculate GST breakdown
gst_breakdown = {}
for item in classified_items:
gst_rate = item.get("gst_rate", 0)
if gst_rate not in gst_breakdown:
gst_breakdown[gst_rate] = {
"taxable_amount": 0,
"tax_amount": 0
}
taxable_amount = item.get("total", 0)
tax_amount = taxable_amount * (gst_rate / 100)
gst_breakdown[gst_rate]["taxable_amount"] += taxable_amount
gst_breakdown[gst_rate]["tax_amount"] += tax_amount
# Record success
batch_info['successful_files'] += 1
batch_info['results'].append({
'file_name': file_info['name'],
'success': True,
'invoice_id': invoice_id,
'items_count': len(classified_items)
})
except Exception as e:
# Record failure
batch_info['failed_files'] += 1
batch_info['results'].append({
'file_name': file_info['name'],
'success': False,
'error': str(e)
})
finally:
# Cleanup temp file
try:
os.unlink(file_info['path'])
except:
pass
# Update progress
batch_info['processed_files'] += 1
# Mark batch as completed
batch_info['status'] = 'completed'
batch_info['completed_at'] = str(datetime.datetime.now())
except Exception as e:
# Mark batch as failed
batch_info['status'] = 'failed'
batch_info['error'] = str(e)
batch_info['completed_at'] = str(datetime.datetime.now())
# Clean up any remaining temp files
for file_info in temp_files:
try:
if os.path.exists(file_info['path']):
os.unlink(file_info['path'])
except:
pass
@app.route('/api/batch/status/<batch_id>', methods=['GET'])
def get_batch_status(batch_id):
if batch_id not in batch_jobs:
return jsonify({"error": "Batch job not found"}), 404
return jsonify(batch_jobs[batch_id])
@app.route('/api/batch/list', methods=['GET'])
def list_batch_jobs():
# Return basic info about all batch jobs
batch_summaries = []
for job_id, job_info in batch_jobs.items():
batch_summaries.append({
'id': job_id,
'status': job_info['status'],
'total_files': job_info['total_files'],
'processed_files': job_info['processed_files'],
'successful_files': job_info['successful_files'],
'failed_files': job_info['failed_files'],
'started_at': job_info['started_at'],
'completed_at': job_info['completed_at']
})
return jsonify(batch_summaries)
@app.route('/api/gst-statistics', methods=['GET'])
def get_gst_statistics():
try:
# Get time range filter parameters
start_date = request.args.get('start_date')
end_date = request.args.get('end_date')
# Fetch all invoices
invoices = db.get_invoices()
if not invoices:
# Return empty stats instead of 404 for better UI handling
return jsonify({
"tax_by_slab": {},
"total_tax": 0,
"total_taxable": 0,
"invoice_count": 0
})
# Apply date filtering if provided
if start_date and end_date:
invoices = [
inv for inv in invoices
if start_date <= inv["created_at"] <= end_date
]
# Get all items across all invoices
all_items = []
for invoice in invoices:
items = db.get_items_by_invoice(invoice["id"])
all_items.extend(items)
if not all_items:
# Return empty stats instead of 404 for better UI handling
return jsonify({
"tax_by_slab": {},
"total_tax": 0,
"total_taxable": 0,
"invoice_count": len(invoices)
})
# Calculate total tax by slab
tax_by_slab = {}
for item in all_items:
slab = item.get("gst_rate", 0)
if slab not in tax_by_slab:
tax_by_slab[slab] = {
"taxable_amount": 0,
"tax_amount": 0,
"item_count": 0
}
taxable_amount = item.get("total", 0)
tax_amount = taxable_amount * (slab / 100)
tax_by_slab[slab]["taxable_amount"] += taxable_amount
tax_by_slab[slab]["tax_amount"] += tax_amount
tax_by_slab[slab]["item_count"] += 1
# Calculate total tax collected
total_tax = sum(slab_data["tax_amount"] for slab_data in tax_by_slab.values())
total_taxable = sum(slab_data["taxable_amount"] for slab_data in tax_by_slab.values())
return jsonify({
"tax_by_slab": tax_by_slab,
"total_tax": total_tax,
"total_taxable": total_taxable,
"invoice_count": len(invoices),
"item_count": len(all_items)
})
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/api/trend-analysis', methods=['GET'])
def get_trend_analysis():
try:
# Get filter parameters
start_date = request.args.get('start_date')
end_date = request.args.get('end_date')
group_by = request.args.get('group_by', 'month') # Default to monthly grouping
# Validate group_by parameter
valid_group_by = ['day', 'week', 'month', 'quarter']
if group_by not in valid_group_by:
return jsonify({"error": f"Invalid group_by parameter. Must be one of: {', '.join(valid_group_by)}"}), 400
# Run trend analysis
trend_data = trend_analyzer.analyze_historical_trends(
start_date=start_date,
end_date=end_date,
group_by=group_by
)
return jsonify(trend_data)
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/api/slab-distribution', methods=['GET'])
def get_slab_distribution():
try:
# Get filter parameters
start_date = request.args.get('start_date')
end_date = request.args.get('end_date')
# Run trend analysis with focus on slab distribution
trend_data = trend_analyzer.analyze_historical_trends(
start_date=start_date,
end_date=end_date
)
# Extract just the slab distribution part
return jsonify(trend_data.get("slab_distribution", []))
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/api/top-hsn-codes', methods=['GET'])
def get_top_hsn_codes():
try:
# Get filter parameters
start_date = request.args.get('start_date')
end_date = request.args.get('end_date')
limit = request.args.get('limit', 10)
try:
limit = int(limit)
except ValueError:
limit = 10
# Run trend analysis
trend_data = trend_analyzer.analyze_historical_trends(
start_date=start_date,
end_date=end_date
)
# Extract just the top HSN codes part
return jsonify(trend_data.get("top_hsn_codes", [])[:limit])
except Exception as e:
return jsonify({"error": str(e)}), 500
# Chatbot endpoint
@app.route('/api/chatbot', methods=['POST'])
def chatbot():
try:
data = request.json
if not data or 'message' not in data:
return jsonify({'error': 'No message provided'}), 400
user_message = data['message']
# Get chat history context if available
chat_history = data.get('history', [])
# Use AI processor to get response
response = ai_processor.get_chatbot_response(user_message, chat_history)
return jsonify({
'response': response
})
except Exception as e:
return jsonify({'error': str(e)}), 500
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)