Skip to content

SaaS Invoice Processing Automation - Frappe App for AI-powered invoice extraction and accounting system integration

Notifications You must be signed in to change notification settings

chinmaybhatk/invoice_processing_saas

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 

Repository files navigation

🚀 Invoice Processing SaaS - Complete Frappe Implementation

DEPLOYMENT READY!

Your complete SaaS invoice processing solution is ready for deployment. All DocTypes, API endpoints, and n8n integration points have been implemented.

📊 What's Been Built

Core DocTypes Created:

  1. SaaS Customer - Complete customer management with quotas, billing, API keys
  2. Subscription Plan - Flexible pricing tiers with features and limits
  3. Processing Job - Full job tracking with AI extraction results
  4. Drive Integration - Google Drive OAuth and folder monitoring
  5. Accounting Integration - Multi-system support (QuickBooks, Xero, etc.)
  6. Usage Tracking - Automated billing and usage analytics

n8n API Endpoints Ready:

  • /api/method/invoice_processing_saas.api.n8n_integration.lookup_user_by_folder
  • /api/method/invoice_processing_saas.api.n8n_integration.create_processing_job
  • /api/method/invoice_processing_saas.api.n8n_integration.update_job_status
  • /api/method/invoice_processing_saas.api.n8n_integration.store_processing_result
  • /api/method/invoice_processing_saas.api.n8n_integration.update_usage_tracking

🔧 Installation Steps

Step 1: Install the Frappe App

# 1. Get the app from GitHub
bench get-app https://github.com/chinmaybhatk/invoice_processing_saas

# 2. Install on your site
bench --site your-site-name install-app invoice_processing_saas

# 3. Run database migrations  
bench --site your-site-name migrate

# 4. Build assets
bench build --app invoice_processing_saas

Step 2: Create Default Data

# Create default subscription plans
bench --site your-site-name execute invoice_processing_saas.setup.create_default_plans

# Or create manually via UI:
# - Go to: Subscription Plan > New
# - Create: Starter ($29/month, 100 invoices)
# - Create: Professional ($99/month, 500 invoices)  
# - Create: Enterprise ($299/month, 2000 invoices)

🔗 n8n Integration Guide

Required Changes to Your n8n Workflow

Your existing n8n workflow needs minimal updates - just change the API endpoint URLs:

1. Update "Lookup User Configuration" Node

Current:

URL: {{ $vars.SAAS_API_ENDPOINT }}/api/users/lookup-by-folder

New:

URL: https://your-frappe-site.com/api/method/invoice_processing_saas.api.n8n_integration.lookup_user_by_folder
Method: POST
Body: {"folder_id": "{{ $json.parents[0].id }}"}

2. Add "Create Processing Job" Node

Add after "Analyze File Complexity":

URL: https://your-frappe-site.com/api/method/invoice_processing_saas.api.n8n_integration.create_processing_job
Method: POST  
Body: {
  "customer_id": "{{ $json.user_config.customer_id }}",
  "file_name": "{{ $json.file_info.name }}",
  "file_id": "{{ $json.file_info.id }}",
  "file_size": "{{ $json.file_info.size }}",
  "complexity_score": "{{ $json.file_analysis.complexity }}",
  "recommended_engine": "{{ $json.file_analysis.recommended_engine }}"
}

3. Update "Store Processing Result" Node

Current:

URL: {{ $vars.SAAS_API_ENDPOINT }}/api/accounting/import

New:

URL: https://your-frappe-site.com/api/method/invoice_processing_saas.api.n8n_integration.store_processing_result
Method: POST
Body: {
  "job_id": "{{ $json.job_id }}",
  "extracted_data": "{{ $json.standardized_data }}",
  "validation_status": "{{ $json.processing_metadata.validated ? 'Valid' : 'Invalid' }}",
  "confidence_score": "{{ $json.processing_metadata.confidence_score }}"
}

Environment Variables

Update your n8n environment:

# Replace current SAAS_API_ENDPOINT
FRAPPE_BASE_URL=https://your-frappe-site.com

# Keep existing AI keys
MASTER_OPENAI_KEY=your-openai-key
MASTER_AZURE_KEY=your-azure-key
AZURE_ENDPOINT=your-azure-endpoint

👥 Customer Onboarding Flow

1. Web Form Signup (Next Implementation Phase)

Customer visits: https://your-frappe-site.com/signup
├── Company details
├── Google Drive folder URL  
├── Subscription plan selection
├── Accounting system choice
└── Creates: SaaS Customer + Drive Integration + Accounting Integration

2. Automatic Setup

Frappe Backend:
├── Generates API keys for n8n
├── Validates Drive access
├── Sets up usage tracking  
├── Sends welcome email
└── Activates monitoring

3. Customer Dashboard (Next Implementation Phase)

Portal at: https://your-frappe-site.com/dashboard
├── Real-time processing status
├── Usage vs quota tracking
├── Integration health status
├── Recent job history
└── Billing information

📈 Key Features Implemented

🎯 Customer Management

  • Automated API key generation for n8n integration
  • Subscription plan management with usage limits
  • Trial period handling and billing automation
  • Customer portal user creation and permissions

🔄 Processing Pipeline

  • Job tracking from creation to completion
  • AI engine routing (OpenAI vs Azure) based on complexity
  • Comprehensive data validation and error handling
  • Automatic customer notifications on completion/failure

💳 Usage & Billing

  • Real-time usage tracking with quota enforcement
  • Automated overage calculation and billing
  • Monthly usage summaries and analytics
  • Invoice generation and payment tracking

🔧 Integration Management

  • Google Drive OAuth authentication and folder monitoring
  • Multi-accounting system support (QuickBooks, Xero, etc.)
  • Connection health monitoring and auto-retry
  • Integration status dashboards

📊 Analytics & Reporting

  • Processing success rates and performance metrics
  • Engine usage breakdown (OpenAI vs Azure)
  • Customer usage trends and forecasting
  • System health monitoring and alerting

🎯 What Happens Next

Phase 1: Deploy Core System ✅ READY NOW

# Deploy immediately - all backend functionality is complete
bench get-app https://github.com/chinmaybhatk/invoice_processing_saas
bench --site your-site install-app invoice_processing_saas
bench migrate

Phase 2: Update n8n Workflow (10 minutes)

  • Update 3 API endpoint URLs in your n8n workflow
  • Test with existing customer data
  • Verify end-to-end processing pipeline

Phase 3: Customer Portal (Optional - can be built later)

  • Web form for customer signup
  • Dashboard pages for customer self-service
  • Billing and subscription management pages

🧪 Testing Your Integration

1. Test User Lookup API

curl -X POST https://your-site.com/api/method/invoice_processing_saas.api.n8n_integration.lookup_user_by_folder \
  -H "Content-Type: application/json" \
  -d '{"folder_id": "test-folder-id"}'

2. Create Test Customer

# In Frappe console: bench --site your-site console
import frappe

# Create subscription plan
plan = frappe.get_doc({
    "doctype": "Subscription Plan",
    "plan_name": "Starter",
    "plan_code": "starter", 
    "monthly_price": 29,
    "processing_limit": 100,
    "is_active": 1
})
plan.insert()

# Create test customer
customer = frappe.get_doc({
    "doctype": "SaaS Customer",
    "customer_name": "Test Company",
    "email": "test@company.com",
    "subscription_plan": "starter",
    "subscription_status": "Active",
    "created_by_webform": 1
})
customer.insert()

# Create drive integration
drive = frappe.get_doc({
    "doctype": "Drive Integration", 
    "customer": customer.name,
    "drive_folder_id": "test-folder-123",
    "integration_status": "Active",
    "setup_completed": 1
})
drive.insert()

print(f"Test customer created: {customer.name}")
print(f"API Key: {customer.api_key}")

3. Test Complete Workflow

  1. Upload a test invoice to the Google Drive folder
  2. Monitor n8n workflow execution
  3. Check Frappe for created Processing Job
  4. Verify usage tracking updates
  5. Confirm customer notifications

🔒 Security & Production Setup

API Security

# In hooks.py - already configured
api_call_limits = {
    "invoice_processing_saas.api.n8n_integration.create_job": {
        "limit": 100,  # 100 calls per hour
        "window": 3600
    }
}

Database Indexes

-- Automatically created by DocTypes
CREATE INDEX idx_customer_email ON `tabSaaS Customer` (email);
CREATE INDEX idx_folder_id ON `tabDrive Integration` (drive_folder_id); 
CREATE INDEX idx_job_customer ON `tabProcessing Job` (customer);
CREATE INDEX idx_usage_customer_month ON `tabUsage Tracking` (customer, month);

Scheduled Jobs

# Already configured in hooks.py
scheduler_events = {
    "daily": [
        "invoice_processing_saas.tasks.daily.update_usage_tracking",
        "invoice_processing_saas.tasks.daily.send_usage_summaries"
    ],
    "*/15 * * * *": [  # Every 15 minutes
        "invoice_processing_saas.tasks.frequent.check_integration_health"
    ]
}

💡 Business Model Ready

Pricing Tiers (Customizable)

🥉 Starter: $29/month
├── 100 invoices/month
├── Basic integrations  
├── Email support
└── 90-day data retention

🥈 Professional: $99/month  
├── 500 invoices/month
├── All integrations
├── Priority support
├── API access
└── 1-year data retention

🥇 Enterprise: $299/month
├── 2000 invoices/month
├── Custom integrations
├── Dedicated support
├── Advanced analytics
└── Unlimited data retention

Revenue Streams

  • 💰 Monthly Subscriptions - Recurring revenue from plans
  • 📈 Overage Charges - $0.50 per invoice over limit
  • 🔧 Setup Fees - One-time integration setup
  • 🏢 Enterprise Features - Custom pricing for large customers

📞 Support & Troubleshooting

Common Issues

1. "Customer not found" in n8n:

# Check if Drive Integration exists
frappe.db.get_value("Drive Integration", {"drive_folder_id": "your-folder-id"}, "customer")

2. "Quota exceeded" errors:

# Check customer usage
customer = frappe.get_doc("SaaS Customer", "CUST-001")
print(f"Usage: {customer.current_usage}/{customer.usage_limit}")

3. "Integration status error":

# Reset integration status
integration = frappe.get_doc("Drive Integration", "DRV-001")  
integration.integration_status = "Active"
integration.save()

Monitoring Dashboard Queries

-- Active customers
SELECT COUNT(*) FROM `tabSaaS Customer` WHERE subscription_status = 'Active';

-- Monthly processing volume
SELECT SUM(processed_count) FROM `tabUsage Tracking` WHERE month = '2025-01';

-- Revenue this month  
SELECT SUM(total_charges) FROM `tabUsage Tracking` WHERE month = '2025-01';

-- Integration health
SELECT integration_status, COUNT(*) FROM `tabDrive Integration` GROUP BY integration_status;

🎉 Ready to Launch!

Your Invoice Processing SaaS is production-ready with:

Complete backend infrastructure
n8n workflow integration
Customer management system
Automated billing and usage tracking
Multi-tenant architecture
API endpoints for all operations
Security and rate limiting
Error handling and monitoring

Next Steps:

  1. Deploy the Frappe app (10 minutes)
  2. Update your n8n workflow URLs (5 minutes)
  3. Create your first test customer (2 minutes)
  4. Start processing invoices! 🚀

Repository: https://github.com/chinmaybhatk/invoice_processing_saas

The foundation is solid - you can now onboard customers, process their invoices automatically, and scale your business! 🎯

About

SaaS Invoice Processing Automation - Frappe App for AI-powered invoice extraction and accounting system integration

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages