Office Automation Hub: Bank Statement Analyser
Table of Contents
- Overview, Goals, and Architecture Summary
- Domain Model and Glossary
- Capabilities and Module Intents
- Data Model and Contracts
- End-to-End Workflows and State Models
- Inter-module Interfaces and Integration Patterns
- NFRs: Performance, Reliability, Security, Compliance
- Configuration and Environment Matrix
- Install, Deploy, Operate (Runbooks)
- Testing Strategy and Coverage
- User Guides and Troubleshooting
- Governance, Versioning, and Release Notes
- Appendices
1. Overview, Goals, and Architecture Summary
1.1 Overview
The Bank Statement Analyser is a modular office automation tool designed to ingest, process, and categorize financial transactions from a standard bank statement format. It leverages a rule-based engine combined with optional AI-powered analysis to classify transactions, identify high-value items, and generate insightful summaries and visualizations. The system is designed to significantly reduce manual effort in accounting, auditing, and financial review processes.
1.2 Goals
- Automation: Automate the tedious process of manually categorizing bank statement entries.
- Standardization: Enforce a consistent categorization and tax treatment methodology using a centralized rules engine.
- Insight Generation: Provide high-level insights through automated summaries, dashboards, and high-value transaction reports.
- Efficiency: Accelerate workflows for accountants and auditors, such as ITR preparation, audit investigations, and suspense account reconciliation.
- Intelligence: Integrate modern AI capabilities (via Google Gemini) to provide intelligent suggestions for uncategorized "Suspense" transactions.
- Extensibility: Allow for client-specific rules and optional processing modules (e.g., Self-Improviser) to be added without altering the core logic.
1.3 Architecture Summary
The application follows a modular, three-tier architecture:
- Presentation Layer (Frontend): A web-based user interface (bank_analyzer_index.html, bank_analyzer_results.html) built with HTML, Tailwind CSS, and JavaScript. It allows users to upload files, set parameters, and view the analysis results, including interactive charts.
- Business Logic Layer (Backend): A Python-based processing engine, orchestrated by the main script bankv14.py. This layer contains the core logic for rule application, data manipulation, and coordination between various processing modules.
- Data Processing Modules (Libraries): A collection of specialized Python scripts, each responsible for a specific task:
- gemini_analyzer.py: Interfaces with the Google Gemini API for AI-based analysis.
- high_value_detector.py: Filters for high-value transactions.
- excel_dashboard_util.py: Generates graphical dashboards in the output Excel file.
- excel_styling.py: Applies professional formatting to Excel outputs.
- self_improviser.py: (Inferred) Analyzes suspense items to suggest new rules.
The system is designed to be run within a web server environment (e.g., Flask), where the frontend triggers the backend processing and renders the results.
2. Domain Model and Glossary
- Bank Statement: The primary input file (XLSX) containing transactional data with standard columns like Date, Transaction Remarks, Withdrawal Amount, and Deposit Amount.
- Rules File: An Excel workbook (Permanant_Bank.xlsx) that acts as the knowledge base for the system.
- Permanent Rules: A set of global categorization rules defined in the "Permanant" sheet of the Rules File, applicable to all clients.
- Client Rules: A set of client-specific rules defined in a dedicated sheet (named after the client) in the Rules File. These rules take precedence over Permanent Rules.
- Keyword: A specific text string within a rule used to match against the Transaction Remarks of a bank entry.
- Category: A high-level classification for a transaction (e.g., PERSONAL, BUSINESS, INVESTMENT). This is the primary output of the rule engine.
- Tax Treatment: A more granular classification related to Indian tax regulations (e.g., SALARY, CAPITAL GAINS, TELEPHONE EXP).
- Suspense: The default category assigned to any transaction that does not match any existing rule. These are the primary targets for review and AI analysis.
- High-Value Transaction (HVT): A transaction whose withdrawal or deposit amount exceeds a user-defined threshold.
- Self-Improviser: An optional module that analyzes recurring Suspense transactions to recommend new, permanent rules, thus improving the system's accuracy over time.
3. Capabilities and Module Intents
- bankv14.py (Core Orchestrator):
- Intent: To manage the entire analysis workflow from start to finish.
- Capabilities: Loads data, sequences the execution of processing modules, handles errors, aggregates results, and prepares the final output for both the Excel file and the web UI.
- Rule-Based Categorization Engine (within bankv14.py):
- Intent: To apply business logic for transaction classification.
- Capabilities: Reads Permanent and Client rules, iterates through transactions, and applies category/tax treatment based on keyword matching. Implements a client-over-permanent priority system.
- gemini_analyzer.py (AI Analysis Module):
- Intent: To provide intelligent, context-aware suggestions for ambiguous transactions.
- Capabilities: Securely connects to the Google Gemini API, constructs a precise prompt for financial analysis, parses the structured JSON response, and handles API-specific errors like rate limiting with an exponential backoff strategy.
- high_value_detector.py (HVT Module):
- Intent: To flag transactions that require special attention due to their monetary value.
- Capabilities: A configurable class that filters a DataFrame based on user-provided withdrawal and deposit thresholds.
- excel_dashboard_util.py (Reporting Module):
- Intent: To create a user-friendly, visual summary of the analysis.
- Capabilities: Uses openpyxl to dynamically generate a "Dashboard" sheet within the output Excel file, containing a monthly trend line chart, a category-wise pie chart, and a tax-wise bar chart.
- excel_styling.py (Formatting Module):
- Intent: To ensure the final Excel output is professional, readable, and consistently formatted.
- Capabilities: Applies styles (fonts, colors, borders, alignment) and adjusts column widths for all data tables in the output file.
4. Data Model and Contracts
4.1 Input Data Contracts
- Bank Statement (bank_file):
- Format: .xlsx
- Required Columns: Transaction Remarks, Withdrawal Amount (INR ), Deposit Amount (INR ), and a valid date column (e.g., Date, TXN Date). The column names must be exact.
- Rules File (rules_file):
- Format: .xlsx
- Sheet "Permanant":
- Columns: Keyword, Category, Tax Treatment
- Sheet [client_name]:
- Columns: Keyword, Category, Tax Treatment
4.2 Output Data Contracts
- Analyzed Excel File ([client_name]_analyzed_[timestamp].xlsx):
- Format: .xlsx
- Sheet "Dashboard": Contains charts for visual analysis.
- Sheet "Analyzed_Statement": The original bank statement with two new columns: Category and Tax Treatment.
- Sheet "Category_Summary": Aggregated sums of withdrawals and deposits, grouped by Category.
- Sheet "Tax_Summary": Aggregated sums of withdrawals and deposits, grouped by Tax Treatment.
- Sheet "Suspense": A subset of "Analyzed_Statement" containing only transactions categorized as Suspense. If the Gemini Analyzer is run, this sheet includes additional columns: AI Suggestive Category, Justification, Category (AI), Tax Treatment (AI).
- Sheet "High_Value_Transactions": (Optional) A list of high-value suspense transactions.
- Web UI Data (JSON):
- The backend returns a JSON object to the bank_analyzer_results.html template containing keys like category_json, tax_json, detailed_trend_json, and full_transactions_json to populate the interactive charts and modal dialogues.
5. End-to-End Workflows and State Models
Workflow: Standard Analysis
- START: User navigates to bank_analyzer_index.html.
- User Input: User selects a client, enters the client name for rules, uploads the bank statement, and clicks "Process Statement".
- Request: The browser POSTs the form data to the web server.
- Orchestration: The server calls the process_bank_statement function in bankv14.py.
- Processing:
- The function loads the rules and the bank statement.
- It applies the rule-based categorization engine.
- It generates summaries (Category, Tax).
- It writes all dataframes to a new Excel file.
- It applies styling (excel_styling.py).
- It generates the dashboard (excel_dashboard_util.py).
- It prepares JSON data for the UI.
- Response: The function returns a dictionary with the status, file paths, and JSON data.
- Render: The server renders the bank_analyzer_results.html template, passing the result dictionary.
- Display: The user sees a results page with interactive charts and download links.
- END.
State Model: Transaction
A transaction can be in one of two states:
- Uncategorized (Initial State): Default state upon ingestion.
- Categorized (Terminal State): The state after a rule has been successfully matched and applied.
Workflow: AI-Enhanced Analysis
This follows the standard workflow, but with an additional step after rule-based categorization:
- 5a. AI Analysis: If the "Analyze Suspense with Gemini AI" checkbox is ticked, the orchestrator iterates through all remaining Suspense transactions and sends them to gemini_analyzer.py for processing. The AI's suggestions are appended to the "Suspense" data.
6. Inter-module Interfaces and Integration Patterns
- Orchestrator-Module Pattern: bankv14.py acts as the central orchestrator that calls other modules.
- bankv14.py -> gemini_analyzer.py:
- Interface: analyze_single_transaction(transaction: dict) -> dict
- Integration: bankv14.py calls this function in a loop for each suspense row. The function is synchronous and includes a time.sleep() to manage rate limits internally.
- bankv14.py -> high_value_detector.py:
- Interface: HighValueTransactionDetector(thresholds).process_transactions(df: DataFrame) -> DataFrame
- Integration: bankv14.py instantiates the detector class with user-defined thresholds and calls its processing method.
- bankv14.py -> excel_dashboard_util.py:
- Interface: create_excel_dashboard(file_path: str)
- Integration: Called after the main Excel file is created. It modifies the file in place by adding a new sheet.
- bankv14.py -> excel_styling.py:
- Interface: apply_excel_styling(workbook: Workbook, ...)
- Integration: Called after data is written to the Excel file but before it's saved, allowing it to apply formatting to the openpyxl workbook object directly.
7. NFRs: Performance, Reliability, Security, Compliance
- Performance:
- The primary bottleneck is the optional Gemini AI analysis due to the per-transaction API calls and the intentional delay (time.sleep).
- Standard processing is fast, limited mainly by Pandas DataFrame operations and file I/O. Performance will degrade linearly with the number of rows in the bank statement.
- Reliability:
- The core processing logic is wrapped in a global try...except block, ensuring that any single failure during processing is caught, logged, and reported to the user without crashing the server.
- The Gemini module includes its own retry logic for transient API errors (rate limiting), enhancing its robustness.
- Security:
- The Google Gemini API key is handled securely using environment variables (os.getenv("GOOGLE_API_KEY")), preventing it from being hardcoded in the source.
- File uploads should be handled by the web server with appropriate security measures (e.g., file type validation, size limits, storing in a non-executable directory).
- Compliance:
- The tool processes financial data, so data privacy and handling are critical. All processing is done server-side, and the data should be managed according to the relevant privacy policies (e.g., GDPR, local data protection laws).
8. Configuration and Environment Matrix
- Runtime Environment: Python 3.8+.
- Key Libraries: pandas, openpyxl, google-generativeai.
- Environment Variables:
- GOOGLE_API_KEY: Required for the Gemini AI analysis module. The application will function without it, but the AI feature will be disabled.
- File-based Configuration:
- rules_excel_path_config: The file path to the Permanant_Bank.xlsx rules file.
- output_dir_config: The directory where analyzed output files are saved.
- log_dir_config: The directory where log files are stored.
9. Install, Deploy, Operate (Runbooks)
Installation
- Clone the repository.
- Install Python dependencies: pip install pandas openpyxl google-generativeai
- Set the environment variable GOOGLE_API_KEY with your API key.
Deployment
- Integrate the bankv14.py script into a web framework like Flask.
- Create a route (e.g., /process_bank_statement) that accepts POST requests from the bank_analyzer_index.html form.
- Configure the paths for rules, outputs, and logs within the web application's configuration.
- Serve the HTML files (index and results) from the appropriate routes.
Operation
- Ensure the web server is running.
- Ensure the Permanant_Bank.xlsx file is accessible at the configured path.
- Monitor the log_dir_config directory for logs to diagnose any processing failures.
10. Testing Strategy and Coverage
- Unit Testing: Each Python module should have dedicated unit tests.
- test_high_value_detector.py: Test with various thresholds, including zero and none.
- test_gemini_analyzer.py: Mock the genai API call to test the prompt construction and JSON parsing logic. Test the retry mechanism.
- Test the core rule engine with sample data to ensure client rules override permanent rules and that keywords are matched correctly.
- Integration Testing: Test the end-to-end workflow by simulating a web request with sample input files and verifying the contents of the generated Excel file.
- User Acceptance Testing (UAT): Users should test the web interface, uploading various real-world bank statements and rules files to validate the accuracy of the output and the usability of the interface.
11. User Guides and Troubleshooting
User Guide
- Navigate to the main page.
- Download Templates: First, download the "Rules Template" and "Bank Statement Format" to understand the required data structure.
- Prepare Files:
- Populate the Permanant_Bank.xlsx file with your rules. For specific clients, add new sheets with the client's name.
- Ensure your bank statement Excel file matches the column headers from the downloaded format.
- Upload Rules: Use the "Upload Rules" form to upload your modified Permanant_Bank.xlsx.
- Process Statement:
- In the "Processing Engine" form, select the client's official name for the report header.
- Enter the client's short name that exactly matches the sheet name in your rules file.
- Upload the client's bank statement.
- (Optional) Set high-value thresholds and enable the AI Analyzer or Self-Improviser.
- Click "Process Statement".
- Review Results: The results page will appear with charts and download links for the detailed Excel analysis and log file.
Troubleshooting
- Error: "Rules file not found": Check the server configuration to ensure the path to Permanant_Bank.xlsx is correct.
- Incorrect Categorization:
- Verify the client_name_for_rules input exactly matches the sheet name in the rules file.
- Check for typos or extra spaces in your keywords.
- AI Analyzer Not Working: Ensure the GOOGLE_API_KEY environment variable is correctly set in the server environment where the script is running.
- Charts Not Appearing: Make sure the date column in the bank statement is correctly formatted and has a recognized header (Date, TXN Date, etc.).
12. Governance, Versioning, and Release Notes
- Versioning: The system should follow Semantic Versioning (e.g., v1.4.0).
- Governance: Changes to the core logic or data contracts must be reviewed and documented. New rules can be added to the Permanant_Bank.xlsx file by authorized users without requiring a code change.
- Release Notes (v1.4):
- Feature: Integrated Gemini AI for intelligent analysis of suspense transactions.
- Feature: Added a web-based results page with interactive charts and a transaction detail modal.
- Improvement: Refactored the core script for better error handling and logging.
- Improvement: Added an automated Excel dashboard generation module.
13. Appendices
- Appendix A: API Schema Reference
- Gemini API Request (Prompt):
- You are an expert accounting assistant...
- Analyze the following bank transaction...
- Provide your answer as a single, valid, minified JSON object with 4 keys: "AI Suggestive Category", "Justification", "Category", and "Tax treatment"...
- --- TRANSACTION TO ANALYZE ---
- Type: {transaction['type']}, Narration: "{transaction['narration']}"
-
- Gemini API Response (Expected JSON):
- {
- "AI Suggestive Category": "Example Fuel Expense",
- "Justification": "The narration contains the word 'PETRO', indicating a fuel purchase.",
- "Category": "BUSINESS",
- "Tax treatment": "TRANSPORT"
- }
-