Car Economics & Invoicing
This document explains revenue/expense tracking, vehicle balances, invoicing, and payments.
Scope
- Revenue recognition on orders, extra services, and insurances
- Vehicle-level economics (profits/expenses, purchases, balances)
- Invoices, invoice series/numbering, and PDF generation
- Payments lifecycle and Stripe integration touchpoints
- Calculators and totals
Data model (Prisma)
invoicesandinvoices_series(invoice header, numbering series, type)paymentsandpayment_methods(capture status, type, method, stage)vehicle_balances(per-vehicle PROFIT/EXPENSE with categories)orderssnapshot totals (totalProfit,expenses, etc.)
See prisma/schema.prisma:
- Key enums:
InvoiceType,PaymentStage,PaymentStatus,PaymentType,PaymentMethod,PaymentVariant,VehicleBalanceType,VehicleBalanceCategory. - Important fields (selection):
orders.totalProfit,orders.expenses,orders.paymentInstructions,orders.paymentVariantpayments.stage,payments.type,payments.method,payments.status,paymentIntentIdvehicle_balances.type,vehicle_balances.category, amount and meta
Services
services/tenant/InvoiceService.ts: CRUD and business rules for invoicesservices/tenant/InvoiceSerieService.ts: series management and last/next numberingservices/tenant/PaymentService.ts: creation, capture/refund hooks, invariantsservices/tenant/VehicleBalanceService.ts: track per-vehicle expenses/profitsservices/tenant/VehiclePurchaseService.ts: purchases/depreciation inputsservices/tenant/CalculationService.ts: assist with totals where applicable
Services are exposed via services/index.ts and injected into GraphQL resolvers through context.services.
GraphQL API
- Schema files:
server/graphql/schema/*.graphqlcover invoices, payments, and related types. - Resolvers (selection):
- Invoices:
- Query:
server/graphql/resolvers/query/Invoice/{invoices,invoice,lastInvoiceNumber}.resolver.js - Mutation:
server/graphql/resolvers/mutation/Invoice/{createInvoice,updateInvoice,deleteInvoice}.resolver.js
- Query:
- Payments:
- Mutations under
server/graphql/resolvers/mutation/stripe/*integrate with Stripe flows - Other payment-related resolvers under
server/graphql/resolvers/mutation/addPayment.resolver.js, etc.
- Mutations under
- Invoices:
Calculators and totals
lib/calculators.js: pricing, totals, and economics helpers used by order flows and invoice/payment computations.- Inputs: base tariffs, seasonal matrices, extra services, insurance, kilometers, delivery, taxes
- Outputs: totals, profits, expenses, and odometer-based calculations
Invoicing flow
- Order finalized (check-in/out) → totals computed (
lib/calculators.js). - Create invoice via GraphQL mutation →
InvoiceServicepersists header, lines, tenant/company context. - Invoice series/numbering:
InvoiceSerieServicereturns series code and next number per tenant/company.- Query
lastInvoiceNumberused for UI previews and validations.
- PDF generation:
- Components under
components/PDFDocuments/Invoice/*render data from fetchers (e.g.,fetchInvoiceData.js). - Document downloaded/emailed to the client; see
docs/rebuild/11-pdf-and-documents.md.
- Components under
Payments lifecycle
- Payment variants on orders (
PaymentVariant):noPayments,instantPayment, etc. - Stages/statuses are tracked in
payments:PaymentStage: initiation → confirmation → settlementPaymentStatus: pending/succeeded/failed/refunded
- Stripe integration: see
docs/rebuild/09-integrations.md(webhooks and resolvers). Payments mutate order/invoice states and persistpaymentIntentIdwhere applicable.
Vehicle economics
vehicle_balancescaptures bothPROFITandEXPENSEentries per vehicle, categorized (fuel, maintenance, insurance, purchase, other).VehicleBalanceServiceandVehiclePurchaseServiceprovide creation/query/update routines.- Reporting resolvers (tests reference):
_tests_/vehicleExpenses.resolver.test.js,_tests_/vehicleProfits.resolver.test.js,_tests_/vehicleStatsPrisma.resolver.test.jsdemonstrate expected aggregations and error handling.
UI overview
- Invoice pages and tables under
components/InvoicesPage/*(including electronic billing helpers undercomponents/InvoicesPage/electronicBilling/*). - Billing and payments in order views:
components/OrderView/BillingTabOrderView/*and related. - Client and payments UX integrate with Stripe components and payment link flows.
Ops and constraints
- Multi-store support (Fauna→Postgres migration) controlled via flags; services/read paths must respect flags.
- Numbering and series must be unique per tenant/company; see resolvers that guard uniqueness.
- PDFs: ensure correct locale, currency, and tax rules from settings.
- Webhooks: keep idempotent; reconcile payment state with Stripe events.
Testing
- Unit tests around expenses/profits/stats under
_tests_/*. - Invoice/Payment resolver tests under
_tests_andserver/graphql/resolvers/*/*.test.{js,ts}. - See
docs/rebuild/14-testing-and-quality.mdfor running the suite.