Skip to content
Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

291 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Billing Solutions - Smart Billing Application

A comprehensive offline-first Progressive Web App (PWA) for small businesses to manage products, create GST invoices, track customers, generate reports, and share invoices via WhatsApp.

πŸ“‹ Table of Contents


πŸ—οΈ Architecture Overview

High-Level Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    Client (Browser)                         β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”‚
β”‚  β”‚   React UI   β”‚  β”‚  IndexedDB   β”‚  β”‚  Service     β”‚     β”‚
β”‚  β”‚  Components  β”‚  β”‚   (Dexie)    β”‚  β”‚  Worker      β”‚     β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                            β”‚
                            β”‚ HTTPS
                            β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    Next.js Server                           β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”‚
β”‚  β”‚   API Routes β”‚  β”‚  Middleware  β”‚  β”‚   Supabase   β”‚     β”‚
β”‚  β”‚              β”‚  β”‚   (Auth)     β”‚  β”‚   Client     β”‚     β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                            β”‚
                            β”‚
                            β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚              External Services                              β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”‚
β”‚  β”‚   Supabase   β”‚  β”‚   Firebase   β”‚  β”‚   WhatsApp   β”‚     β”‚
β”‚  β”‚   (Backend)  β”‚  β”‚   (Admin)    β”‚  β”‚   Web API    β”‚     β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Architecture Principles

  1. Offline-First: All data is stored locally in IndexedDB, enabling full functionality without internet
  2. Progressive Web App: Installable, works offline, uses service workers
  3. Dual Database Mode: Supports both IndexedDB (offline) and Supabase (online sync)
  4. Secure Session Management: Cryptographic signatures prevent session tampering
  5. License-Based Access: Device-bound licensing system

πŸ› οΈ Technology Stack

Frontend

  • Framework: Next.js 16 (App Router)
  • UI Library: React 19
  • Styling: Tailwind CSS 4
  • Components: Radix UI, shadcn/ui
  • State Management: React Query (TanStack Query)
  • Forms: React Hook Form + Zod validation

Backend & Database

  • Primary Database: IndexedDB (Dexie.js) - Client-side
  • Cloud Database: Supabase (PostgreSQL)
  • Authentication: Supabase Auth + Custom offline auth
  • File Storage: Supabase Storage

PDF Generation

  • Library: jsPDF + jsPDF-AutoTable
  • Format: Invoice slip PDFs for WhatsApp sharing

Other Libraries

  • Excel: xlsx (for Excel mode)
  • Cryptography: crypto-js (HMAC signatures)
  • Charts: Recharts
  • Date: date-fns

πŸ”„ Application Flow

1. Application Startup Flow

User Opens App
    β”‚
    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  LicenseGuard   β”‚ β†’ Check License Validity
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    β”‚
    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   AuthGuard     β”‚ β†’ Check Session Validity
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    β”‚
    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Service Worker β”‚ β†’ Register for Offline Support
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    β”‚
    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Main App UI    β”‚ β†’ Render Dashboard/Login
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

2. Authentication Flow

Admin/User Login

User Enters Credentials
    β”‚
    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Supabase Auth (Online) β”‚
β”‚  OR                      β”‚
β”‚  Offline Auth (IndexedDB)β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    β”‚
    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Create Secure Session β”‚
β”‚  - Generate HMAC        β”‚
β”‚  - Set Expiry Time      β”‚
β”‚  - Store in IndexedDB   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    β”‚
    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Redirect to Dashboard  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Employee Login

Employee Enters Credentials
    β”‚
    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Validate Employee ID    β”‚
β”‚  & Password (IndexedDB) β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    β”‚
    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Create Employee Sessionβ”‚
β”‚  - Store in localStorageβ”‚
β”‚  - Link to Store        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    β”‚
    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Redirect to Dashboard   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

3. Invoice Creation Flow

User Creates Invoice
    β”‚
    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Fill Invoice Form      β”‚
β”‚  - Select Customer      β”‚
β”‚  - Add Products/Items   β”‚
β”‚  - Calculate Totals     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    β”‚
    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Save to IndexedDB      β”‚
β”‚  - Invoice Header       β”‚
β”‚  - Invoice Items        β”‚
β”‚  - Update Stock         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    β”‚
    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Queue for Sync         β”‚
β”‚  (If Supabase Mode)     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    β”‚
    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Generate PDF (Optional)β”‚
β”‚  Share on WhatsApp      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

4. PDF Sharing Flow

Share PDF Button (Generic Share)

User Clicks "Share PDF"
    β”‚
    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Fetch Invoice Data     β”‚
β”‚  from IndexedDB         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    β”‚
    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Generate PDF Blob      β”‚
β”‚  (jsPDF + AutoTable)    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    β”‚
    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Web Share API          β”‚
β”‚  - Create File Object   β”‚
β”‚  - Open Share Dialog    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    β”‚
    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  User Selects App       β”‚
β”‚  (Email, WhatsApp, etc.)β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

WhatsApp Share Button

User Clicks "Share on WhatsApp"
    β”‚
    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Check Internet         β”‚
β”‚  Connection             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    β”‚
    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Generate WhatsApp      β”‚
β”‚  Message (Formatted)    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    β”‚
    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Generate Slip PDF      β”‚
β”‚  (Compact Format)       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    β”‚
    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Download PDF           β”‚
β”‚  Open WhatsApp Web      β”‚
β”‚  (wa.me/?text=...)      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    β”‚
    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  User Attaches PDF      β”‚
β”‚  Manually in WhatsApp   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

5. Logout Flow

User Clicks Logout
    β”‚
    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Clear IndexedDB Sessionβ”‚
β”‚  - Delete auth_session  β”‚
β”‚  - Verify Signature     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    β”‚
    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Clear localStorage     β”‚
β”‚  - employeeSession      β”‚
β”‚  - offlineAdminSession  β”‚
β”‚  - authType             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    β”‚
    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Supabase SignOut       β”‚
β”‚  (If Online)            β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    β”‚
    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Redirect to Login      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ”’ Security Implementation

Session Security (Enhanced)

The application implements multiple layers of security to prevent session tampering:

1. Cryptographic Signatures (HMAC-SHA256)

Implementation: lib/utils/auth-session.ts

  • Every session is signed with HMAC-SHA256 using a secret key
  • Signature includes: userId, email, role, storeId, issuedAt, expiresAt
  • Any modification to session data invalidates the signature
  • Signature is verified on every session read
// Session Structure
{
  id: "current_session",
  userId: "user-123",
  email: "user@example.com",
  role: "admin",
  storeId: "store-456",
  issuedAt: 1234567890,
  expiresAt: 1234654290,
  signature: "hmac-sha256-hash", // Prevents tampering
  lastValidated: 1234567890,
  validationCount: 1
}

2. Server-Side Time Validation

Implementation: app/api/time/route.ts

  • Server provides authoritative timestamp
  • Client compares server time vs client time
  • Detects time manipulation (>5 minute difference)
  • Falls back to client time if server unavailable (offline mode)

3. Multiple Validation Layers

  • Signature Validation: Detects data tampering
  • Time Validation: Detects clock manipulation
  • Expiry Check: Uses server time when available
  • Anomaly Detection: Tracks validation count for suspicious patterns

4. Security Features

βœ… Prevents:

  • Modifying expiresAt timestamp in IndexedDB
  • Changing system time to extend sessions
  • Tampering with session data (userId, email, role)
  • Replay attacks (signature changes on each validation)

βœ… Detects:

  • Invalid signatures β†’ Auto-logout
  • Time manipulation β†’ Warning logged
  • Suspicious validation patterns β†’ Alert

5. Vulnerabilities Fixed

Vulnerability Previous State Fixed State
IndexedDB Tampering ❌ No protection βœ… HMAC signatures
Time Manipulation ❌ Client time only βœ… Server time validation
Session Replay ❌ No detection βœ… Signature validation
Data Integrity ❌ No checks βœ… Cryptographic verification

πŸ“„ PDF Sharing & WhatsApp Integration

PDF Generation

Files Involved:

  • lib/utils/invoice-slip-pdf.ts - Invoice slip PDF (compact format for WhatsApp)
  • lib/utils/invoice-pdf.ts - Full invoice PDF (A4 format)
  • lib/utils/pdf-generator.ts - Shared InvoiceData interface

PDF Generation Flow:

  1. Data Collection:

    • Invoice header (number, date, customer)
    • Invoice items (description, quantity, price, GST)
    • Totals (subtotal, CGST, SGST, IGST, total)
    • Business information (name, GSTIN, address)
  2. PDF Creation:

    • Uses jsPDF library
    • AutoTable plugin for item tables
    • Custom styling (colors, fonts, layout)
    • Slip format: 80mm x 210mm (receipt-like, for WhatsApp)
    • Invoice format: A4 size (full invoice)
  3. Output:

    • Returns Blob object
    • Can be downloaded or shared

Share PDF Button

Location: app/(dashboard)/invoices/[id]/page-client.tsx

Flow:

  1. Fetches invoice data from IndexedDB (no Supabase dependency)
  2. Generates PDF using generateInvoiceSlipPDF()
  3. Creates File object from PDF Blob
  4. Uses Web Share API (navigator.share())
  5. User selects sharing app (Email, WhatsApp, etc.)

Features:

  • βœ… Works offline (uses IndexedDB only)
  • βœ… No Supabase dependency
  • βœ… Generic share (works with any app)
  • βœ… Fallback to download if share not supported

WhatsApp Share Button

Location: components/features/invoices/whatsapp-share-button.tsx

Flow:

  1. Checks internet connection (required)
  2. Generates formatted WhatsApp message
  3. Generates invoice slip PDF
  4. Downloads PDF automatically
  5. Opens WhatsApp Web (https://wa.me/?text=...)
  6. User manually attaches downloaded PDF

Message Format:

πŸ“‹ *Invoice Receipt*

πŸͺ *Store Name*

━━━━━━━━━━━━━━━━━━━━
πŸ“„ Invoice #INV-001
πŸ“… Date: 01/01/2024
━━━━━━━━━━━━━━━━━━━━

*Items:*
1. Product Name
   Qty: 2 Γ— β‚Ή100.00 = β‚Ή200.00

━━━━━━━━━━━━━━━━━━━━
πŸ’° *Total: β‚Ή200.00*
━━━━━━━━━━━━━━━━━━━━

πŸ“± View full invoice:
https://app.com/i/invoice-id

Thank you for your business! πŸ™

Features:

  • βœ… Formatted message with emojis
  • βœ… Auto-downloads PDF
  • βœ… Opens WhatsApp Web directly
  • βœ… Includes invoice link

Save & Share on WhatsApp (Invoice Form)

Location: components/features/invoices/invoice-form.tsx

Flow:

  1. User fills invoice form
  2. Clicks "Save & Share on WhatsApp"
  3. Saves invoice to IndexedDB
  4. Generates PDF
  5. Opens WhatsApp with message
  6. Downloads PDF for attachment

πŸ” Authentication & Session Management

Session Storage

IndexedDB (Primary)

  • Table: auth_session
  • Structure: See lib/db/dexie.ts - AuthSession interface
  • Security: HMAC signatures prevent tampering

localStorage (Secondary)

  • employeeSession - Employee login data
  • offlineAdminSession - Offline admin session
  • authType - "employee" or "admin"
  • currentStoreId - Selected store ID

Session Validation

Component: components/auth-guard.tsx

Checks:

  1. IndexedDB session exists and valid
  2. Signature verification
  3. Expiry check (server time)
  4. Employee session (if applicable)
  5. Offline admin session (if applicable)

Frequency: Every 5 seconds (periodic check)

Session Expiry

  • Default Duration: 24 hours (86400000 ms)
  • Configurable: NEXT_PUBLIC_SESSION_DURATION_MS env variable
  • Validation: Server time when available, client time as fallback
  • Auto-logout: On expiry, redirects to /auth/session-expired

Logout Implementation

Files:

  • app/auth/login/page.tsx - Login page logout
  • components/layout/sidebar.tsx - Sidebar logout button
  • components/layout/header.tsx - Header logout menu

Process:

  1. Clear IndexedDB session (clearAuthSession())
  2. Clear localStorage (employeeSession, offlineAdminSession, etc.)
  3. Supabase signOut (if online)
  4. Redirect to login

πŸ’Ύ Database Architecture

IndexedDB (Dexie)

Database Name: BillingDatabase

Tables:

  • products - Product catalog
  • customers - Customer information
  • invoices - Invoice headers
  • invoice_items - Invoice line items
  • employees - Employee data
  • stores - Store information
  • settings - Application settings
  • auth_session - Authentication sessions
  • sync_queue - Sync queue for Supabase
  • license - License information
  • inventory - Inventory tracking
  • attendance - Employee attendance
  • sales_header - Sales transactions
  • sales_items - Sales line items

Schema Version: 4 (with auth_session support)

Supabase (PostgreSQL)

Tables: See types/database.types.ts

Sync Strategy:

  • Offline-first: All writes go to IndexedDB first
  • Background sync: Sync queue processes changes
  • Conflict resolution: Last-write-wins
  • Sync frequency: Every 30 seconds (when online)

πŸ“ File Structure

billing-solutions/
β”œβ”€β”€ app/                          # Next.js App Router
β”‚   β”œβ”€β”€ (dashboard)/             # Dashboard routes (protected)
β”‚   β”‚   β”œβ”€β”€ invoices/            # Invoice management
β”‚   β”‚   β”‚   β”œβ”€β”€ [id]/           # Invoice detail page
β”‚   β”‚   β”‚   β”‚   └── page-client.tsx  # Share PDF button
β”‚   β”‚   β”‚   └── new/             # Create invoice
β”‚   β”‚   β”œβ”€β”€ products/            # Product management
β”‚   β”‚   β”œβ”€β”€ customers/           # Customer management
β”‚   β”‚   └── ...
β”‚   β”œβ”€β”€ auth/                    # Authentication routes
β”‚   β”‚   β”œβ”€β”€ login/               # Login page
β”‚   β”‚   β”œβ”€β”€ employee-login/     # Employee login
β”‚   β”‚   └── session-expired/     # Session expired page
β”‚   β”œβ”€β”€ api/                     # API routes
β”‚   β”‚   β”œβ”€β”€ time/                # Server time endpoint
β”‚   β”‚   └── invoices/            # Invoice API
β”‚   └── layout.tsx               # Root layout
β”œβ”€β”€ components/
β”‚   β”œβ”€β”€ auth-guard.tsx           # Authentication guard
β”‚   β”œβ”€β”€ license-guard.tsx        # License validation
β”‚   β”œβ”€β”€ features/
β”‚   β”‚   └── invoices/
β”‚   β”‚       β”œβ”€β”€ invoice-form.tsx        # Invoice creation form
β”‚   β”‚       β”œβ”€β”€ invoice-actions.tsx      # Invoice actions menu
β”‚   β”‚       └── whatsapp-share-button.tsx  # WhatsApp share
β”‚   └── layout/
β”‚       β”œβ”€β”€ header.tsx           # App header
β”‚       └── sidebar.tsx          # Sidebar navigation
β”œβ”€β”€ lib/
β”‚   β”œβ”€β”€ db/
β”‚   β”‚   └── dexie.ts            # IndexedDB schema
β”‚   β”œβ”€β”€ utils/
β”‚   β”‚   β”œβ”€β”€ auth-session.ts     # Secure session management
β”‚   β”‚   β”œβ”€β”€ invoice-slip-pdf.ts # PDF slip generation
β”‚   β”‚   β”œβ”€β”€ invoice-pdf.ts # PDF invoice generation
β”‚   β”‚   β”œβ”€β”€ whatsapp-bill.ts    # WhatsApp integration
β”‚   β”‚   └── invoice-pdf-sync.ts # PDF sync utilities
β”‚   β”œβ”€β”€ supabase/               # Supabase clients
β”‚   └── hooks/                  # Custom React hooks
└── public/                      # Static assets
    β”œβ”€β”€ manifest.json           # PWA manifest
    └── sw.js                   # Service worker

✨ Key Features

1. Invoice Management

  • Create GST/non-GST invoices
  • Multiple tax calculations (CGST, SGST, IGST)
  • Discount support
  • Print/Download PDF
  • Share via WhatsApp

2. Product Management

  • Product catalog with categories
  • Stock tracking
  • HSN code support
  • GST rates per product

3. Customer Management

  • Customer database
  • GSTIN tracking
  • Contact information
  • Purchase history

4. Employee Management

  • Employee login system
  • Store-based access
  • Attendance tracking
  • Role-based permissions

5. Reports & Analytics

  • Sales reports
  • Inventory reports
  • Tax reports
  • Dashboard analytics

6. Offline Support

  • Full offline functionality
  • Background sync
  • Service worker caching
  • IndexedDB storage

πŸ›‘οΈ Security Features

Implemented Security Measures

  1. Session Security

    • βœ… HMAC-SHA256 signatures
    • βœ… Server time validation
    • βœ… Signature verification on every read
    • βœ… Auto-logout on tampering detection
  2. License Protection

    • βœ… Device-bound licensing
    • βœ… MAC address binding
    • βœ… Expiry validation
    • βœ… Revocation support
  3. Data Integrity

    • βœ… Cryptographic signatures
    • βœ… Validation on read/write
    • βœ… Anomaly detection
  4. Authentication

    • βœ… Secure password hashing
    • βœ… Session expiry enforcement
    • βœ… Multi-factor validation

Security Best Practices

  • βœ… Never trust client-side data alone
  • βœ… Always verify signatures
  • βœ… Use server time when available
  • βœ… Log security events
  • βœ… Auto-logout on suspicious activity

πŸš€ Development Setup

Prerequisites

  • Node.js 18+
  • npm or yarn
  • Supabase account (optional, for cloud sync)

Installation

# Clone repository
git clone <repository-url>
cd billing-solutions

# Install dependencies
npm install

# Set up environment variables
cp .env.example .env.local
# Edit .env.local with your configuration

# Run development server
npm run dev

Environment Variables

# Supabase (Optional)
NEXT_PUBLIC_SUPABASE_URL=your-supabase-url
NEXT_PUBLIC_SUPABASE_ANON_KEY=your-anon-key

# Session Configuration
NEXT_PUBLIC_SESSION_DURATION_MS=86400000  # 24 hours
NEXT_PUBLIC_SESSION_SECRET=your-secret-key  # Change in production!

# Other
NEXT_PUBLIC_APP_URL=http://localhost:3000

Build

# Production build
npm run build

# Start production server
npm start

πŸ“ Environment Variables

Required

  • NEXT_PUBLIC_SESSION_SECRET - Secret key for HMAC signatures (MUST be changed in production)

Optional

  • NEXT_PUBLIC_SUPABASE_URL - Supabase project URL
  • NEXT_PUBLIC_SUPABASE_ANON_KEY - Supabase anonymous key
  • NEXT_PUBLIC_SESSION_DURATION_MS - Session duration in milliseconds (default: 86400000)
  • NEXT_PUBLIC_APP_URL - Application URL

πŸ”§ Configuration

Session Duration

Edit NEXT_PUBLIC_SESSION_DURATION_MS in .env.local:

# 1 hour
NEXT_PUBLIC_SESSION_DURATION_MS=3600000

# 12 hours
NEXT_PUBLIC_SESSION_DURATION_MS=43200000

# 24 hours (default)
NEXT_PUBLIC_SESSION_DURATION_MS=86400000

Database Mode

The app supports two modes:

  • IndexedDB Mode: Fully offline, no Supabase
  • Supabase Mode: Online sync with Supabase

Mode is determined automatically based on Supabase configuration.


πŸ“š Additional Documentation


🀝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Test thoroughly
  5. Submit a pull request

πŸ“„ License

Proprietary - All rights reserved


πŸ†˜ Support

For issues or questions:

  • Create an issue in the repository
  • Contact the development team

Last Updated: 2024 Version: 0.1.0

About

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages