Skip to content

Commit cba98fc

Browse files
committed
Refactor billing into modular architecture
- Split billing.js (2215 lines) into 3 focused modules: - billing.js (1041 lines): Core coordinator for mode switching, drafts, auto-save, edit bill - purchase.js (654 lines): Purchase operations - weights, items, totals, save to purchases collection - retail-sale.js (648 lines): Retail sale operations - weights, items, totals, save to retailSales collection - Add window.app.purchase and window.app.retailSale exports in main.js - BillingManager delegates to child managers for backward compatibility - All 103 tests passing - No breaking changes to existing functionality
1 parent d7b403f commit cba98fc

5 files changed

Lines changed: 1812 additions & 1554 deletions

File tree

www/js/README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ www/js/
2121
│ └── firestore-service.js # Firestore CRUD operations
2222
├── modules/
2323
│ ├── analytics.js # Usage analytics and charts
24-
│ ├── billing.js # Purchase bill creation (weights, labour)
24+
│ ├── billing.js # Core billing coordinator (mode switching, drafts, auto-save)
25+
│ ├── purchase.js # Purchase bill creation (weights, rates, labor)
26+
│ ├── retail-sale.js # Retail sale creation (weights, rates, payment)
27+
│ ├── wholesale-sales.js # Wholesale sales (sales tab)
2528
│ ├── cash-management.js # Session-based cash tracking
2629
│ ├── datefilter.js # Date range filtering
2730
│ ├── finance.js # Financial overview and profit/loss
@@ -30,7 +33,6 @@ www/js/
3033
│ ├── miscellaneous.js # Expenses and withdrawals
3134
│ ├── outstanding.js # Outstanding payments tracking
3235
│ ├── reports.js # Reports generation
33-
│ ├── sales.js # Wholesale sales management
3436
│ ├── settings.js # App settings, audit logs, storage stats
3537
│ ├── stock.js # Stock tracking and adjustments
3638
│ └── users.js # User management and roles
@@ -69,9 +71,10 @@ www/js/
6971

7072
| Module | Description |
7173
|--------|-------------|
72-
| `billing.js` | Retail sales (billing tab) with weights, multiple rates, labour charges |
73-
| `sales.js` | Wholesale sales (sales tab) with stock deduction |
74-
| `retail-sales.js` | Retail point-of-sale |
74+
| `billing.js` | Core billing coordinator - mode switching, item dropdowns, drafts, auto-save, edit bill |
75+
| `purchase.js` | Purchase operations - weights, bill items, totals, payment, save to Firebase `purchases` collection |
76+
| `retail-sale.js` | Retail sale operations - weights, sale items, totals, payment, save to Firebase `retailSales` collection |
77+
| `wholesale-sales.js` | Wholesale sales (sales tab) with stock deduction, saves to Firebase `wholesaleSales` collection |
7578
| `items.js` | Item CRUD, purchase/sale/wholesale rates |
7679
| `stock.js` | Stock levels, adjustments, history |
7780
| `history.js` | Bill history, view/edit/delete bills |

www/js/main.js

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { AuthManager } from './auth/authentication.js';
77
import { FirebaseService } from './firebase/firestore-service.js';
88
import { ItemsManager } from './modules/items.js';
99
import { PrinterService, printerManager } from './services/printer.js';
10-
import { BillingManager } from './modules/billing.js';
10+
import { BillingManager, PurchaseManager, RetailSaleManager } from './modules/billing.js';
1111
import { StockManager } from './modules/stock.js';
1212
import { WholesaleSalesManager } from './modules/wholesale-sales.js';
1313
import { HistoryManager } from './modules/history.js';
@@ -129,6 +129,9 @@ async function loadUserDataAndInitialize() {
129129
// Calculate stock
130130
AppState.stock = await FirebaseService.calculateStock();
131131

132+
// Initialize billing manager (connects purchase and retail-sale modules)
133+
BillingManager.init();
134+
132135
// Set up real-time listeners
133136
FirebaseService.setupRealtimeListeners();
134137

@@ -365,6 +368,49 @@ window.app = {
365368
checkAutoSave: () => BillingManager.checkAutoSave()
366369
},
367370

371+
// Purchase (direct access to PurchaseManager)
372+
purchase: {
373+
addWeight: (autoAdd) => PurchaseManager.addWeight(autoAdd),
374+
renderWeights: () => PurchaseManager.renderWeights(),
375+
removeWeight: (idx) => PurchaseManager.removeWeight(idx),
376+
clearWeights: () => PurchaseManager.clearWeights(),
377+
addToBill: (autoAdd) => PurchaseManager.addToBill(autoAdd),
378+
renderBill: () => PurchaseManager.renderBill(),
379+
deleteBillItem: (idx) => PurchaseManager.deleteBillItem(idx),
380+
editBillItem: (idx) => PurchaseManager.editBillItem(idx),
381+
updateTotals: (heavy) => PurchaseManager.updateTotals(heavy),
382+
updatePaymentTotal: () => PurchaseManager.updatePaymentTotal(),
383+
fillPayableAmount: (type) => PurchaseManager.fillPayableAmount(type),
384+
saveBillToHistory: () => PurchaseManager.saveBillToHistory(),
385+
shareWhatsApp: () => PurchaseManager.shareWhatsApp(),
386+
getBillItems: () => PurchaseManager.getBillItems(),
387+
getWeights: () => PurchaseManager.getWeights(),
388+
clearBill: () => PurchaseManager.clearBill()
389+
},
390+
391+
// Retail Sale (direct access to RetailSaleManager)
392+
retailSale: {
393+
addSaleWeight: (autoAdd) => RetailSaleManager.addSaleWeight(autoAdd),
394+
renderSaleWeights: () => RetailSaleManager.renderSaleWeights(),
395+
removeSaleWeight: (idx) => RetailSaleManager.removeSaleWeight(idx),
396+
clearSaleWeights: () => RetailSaleManager.clearSaleWeights(),
397+
addToSalesBill: (autoAdd) => RetailSaleManager.addToSalesBill(autoAdd),
398+
renderSalesBill: () => RetailSaleManager.renderSalesBill(),
399+
removeSaleItem: (idx) => RetailSaleManager.removeSaleItem(idx),
400+
editSaleItem: (idx) => RetailSaleManager.editSaleItem(idx),
401+
updateSaleTotals: () => RetailSaleManager.updateSaleTotals(),
402+
updateSaleRunningTotal: () => RetailSaleManager.updateSaleRunningTotal(),
403+
updateSalePaymentTotal: () => RetailSaleManager.updateSalePaymentTotal(),
404+
fillReceivableAmount: (type) => RetailSaleManager.fillReceivableAmount(type),
405+
completeSale: () => RetailSaleManager.completeSale(),
406+
shareSaleWhatsApp: () => RetailSaleManager.shareSaleWhatsApp(),
407+
printSale: () => RetailSaleManager.printSale(),
408+
pickSaleContact: () => RetailSaleManager.pickSaleContact(),
409+
getSaleItems: () => RetailSaleManager.getSaleItems(),
410+
getSaleWeights: () => RetailSaleManager.getSaleWeights(),
411+
clearSale: () => RetailSaleManager.clearSale()
412+
},
413+
368414
// Printer
369415
printer: {
370416
scan: () => PrinterService.scanDevices(),

0 commit comments

Comments
 (0)