Skip to content

Refactor invoices.py: Separate concerns and clean up route file #355

@nikhilb2

Description

@nikhilb2

Overview

The backend/src/api/routes/invoices.py file is over 1700 lines long and contains mixed concerns including route handlers, business logic, PDF generation, tax calculations, inventory management, and formatting utilities.

This is the main tracking issue for systematically refactoring and cleaning up the invoices route file.

Related Issues (Sub-tasks)

  1. Extract PDF template generation to separate module #349 - Extract PDF template generation to separate module
  2. Extract GST/tax calculation logic to service module #350 - Extract GST/tax calculation logic to service module
  3. Extract invoice payload processing to service module #351 - Extract invoice payload processing to service module
  4. Extract inventory management logic to service module #352 - Extract inventory management logic to service module
  5. Create formatters and validators utilities modules #353 - Create formatters and validators utilities modules
  6. Consolidate invoices.py and implement refactoring #354 - Consolidate invoices.py and implement refactoring

Goals

  • ✅ Reduce invoices.py to < 400 lines containing only route handlers
  • ✅ Extract PDF templates to dedicated module for maintainability
  • ✅ Move business logic to service/utility modules
  • ✅ Improve testability and code reuse
  • ✅ Improve code readability and maintainability

Current Structure Problems

  • HTML/CSS template code mixed with Python routes
  • No separation between concerns (routing, business logic, formatting)
  • Difficult to test business logic
  • Duplicate logic in some areas
  • Hard to find and modify specific functionality

Expected Final Structure

backend/src/
├── api/routes/invoices.py (route handlers only)
├── services/
│   ├── pdf_templates/
│   │   ├── invoice_template.py
│   │   ├── purchase_template.py
│   │   └── builders.py
│   └── invoice_processor.py
├── utils/
│   ├── formatters.py
│   └── validators.py
└── (existing services...)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions