Skip to content

Automated Tax Calculations (Part 1: Basic) #67

Description

@desoga10

Description

Add a simple tax calculation system that automatically computes taxes on invoices based on a single configurable tax rate.

Why This Matters

Users currently must manually calculate taxes, which is error-prone and time-consuming. Basic automated tax calculation ensures accuracy and saves time.

Features

Tax Configuration

  • Tax Rate Setting: Single tax rate per user (e.g., 20%)
  • Tax Name: Customizable name (e.g., "VAT", "GST", "Sales Tax")
  • Enable/Disable: Option to turn tax calculation on/off
  • Default Application: Tax applies to all new invoices automatically

Tax Calculation

  • Tax Exclusive (default): Subtotal + Tax = Total
  • Automatic Calculation: Updates in real-time as items are added
  • Clear Display:
Subtotal:        $1,000.00
VAT (20%):       $  200.00
─────────────────────────
Total:           $1,200.00

Tax Settings

  • Location: Settings page → Tax Configuration
  • Simple form: Tax Name, Tax Rate (%), Enable Tax
  • Save and apply to all future invoices

Database Schema

-- Add tax fields to invoices table
ALTER TABLE invoices
ADD COLUMN tax_rate DECIMAL(5,2) DEFAULT 0, -- e.g., 20.00 for 20%
ADD COLUMN tax_amount DECIMAL(10,2) DEFAULT 0;

-- Optional: Store user's default tax setting
ALTER TABLE user_settings
ADD COLUMN default_tax_rate DECIMAL(5,2) DEFAULT 0,
ADD COLUMN default_tax_name VARCHAR(50) DEFAULT 'Tax',
ADD COLUMN tax_enabled BOOLEAN DEFAULT false;

User Flow

Setup

  1. Go to Settings → Tax
  2. Enable tax calculation (toggle)
  3. Enter tax name (e.g., "VAT")
  4. Enter tax rate (e.g., 20)
  5. Save settings

Creating Invoice

  1. Create new invoice
  2. Add line items
  3. System automatically calculates tax based on user's default rate
  4. View subtotal, tax amount, and total
  5. Tax appears on saved invoice and PDF

Tax Calculation Logic

function calculateInvoiceTax(subtotal: number, taxRate: number) {
  const tax = subtotal * (taxRate / 100);
  const total = subtotal + tax;
  
  return {
    subtotal: subtotal,
    tax: parseFloat(tax.toFixed(2)), // Round to 2 decimals
    total: parseFloat(total.toFixed(2))
  };
}

Technical Implementation

Auto-calculation: Update tax when:

  • Items are added/removed
  • Item quantities/rates change
  • Invoice is created

Settings Service:

interface TaxSettings {
  taxEnabled: boolean;
  taxRate: number;
  taxName: string;
}

Invoice Display:

  • Show tax row only if tax is enabled
  • Display tax name and percentage
  • Round all amounts to 2 decimal places

Design Considerations

  • Clear indication when tax is enabled
  • Display tax percentage in invoice (e.g., "VAT (20%)")
  • Tax appears in invoice list, detail view, and PDF
  • Simple on/off toggle for tax
  • Validation: tax rate between 0-100%

Acceptance Criteria

  • Integrate Tax settings in Account Settings Page
  • User can enable/disable tax
  • User can set tax name and rate
  • Tax auto-calculates on new invoices
  • Subtotal, tax, and total display correctly
  • Tax shows in invoice detail view
  • Tax appears in PDF exports
  • Settings persist across sessions
  • Validation prevents invalid rates (0-100%)
  • Existing invoices not affected by default

Edge Cases

  • Zero tax rate (0%)
  • Tax disabled (don't show tax row)
  • Rounding issues (always 2 decimals)
  • Negative amounts (validation)
  • Very large amounts (handle precision)

Testing Checklist

  • Enable tax with 20% rate
  • Create invoice, verify tax calculates
  • Disable tax, verify tax disappears
  • Change tax rate, verify new invoices use new rate
  • Verify PDF includes tax
  • Test with different currencies
  • Test rounding with various amounts

Priority

High - Core feature for many businesses

Estimated Complexity

Low-Medium - Simple calculation, basic settings UI

Next Steps

After this is complete, see Part 2 for advanced features like per-item tax, tax-exempt items, and multiple tax rates.

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions