358 lines
8.8 KiB
Markdown
358 lines
8.8 KiB
Markdown
# Development Guide: Bank Transactions & Payment Recording
|
|
|
|
Complete implementation of bank transaction CSV import and AR invoice payment recording for the ERP platform.
|
|
|
|
## Quick Links
|
|
|
|
### Documentation
|
|
- **API_ENDPOINTS.md** - Complete API reference with examples
|
|
- **IMPLEMENTATION_SUMMARY.md** - Technical architecture and implementation details
|
|
- **EXAMPLE_CSVs.md** - Sample CSV files and testing guide
|
|
- **FRONTEND_INTEGRATION_GUIDE.md** - Frontend code examples and patterns
|
|
|
|
### Code Files
|
|
- **API: Bank Transaction Import** - `/src/app/api/bank-transactions/import/route.ts`
|
|
- **API: CSV Parser** - `/src/app/api/bank-transactions/parse-csv/route.ts`
|
|
- **UI: AR Invoice Detail** - `/src/app/(dashboard)/finance/ar/invoice/[invoiceId]/page.tsx`
|
|
- **UI: AR List Page** - `/src/app/(dashboard)/finance/ar/page.tsx`
|
|
|
|
## What Was Built
|
|
|
|
### Task 1: Bank Transaction CSV Import Endpoint
|
|
|
|
Two new API endpoints for importing bank transactions from CSV files:
|
|
|
|
1. **POST `/api/bank-transactions/parse-csv`**
|
|
- Accept CSV file upload
|
|
- Parse and validate
|
|
- Return preview of transactions
|
|
- No external dependencies (pure TypeScript CSV parser)
|
|
|
|
2. **POST `/api/bank-transactions/import`**
|
|
- Bulk import validated transactions
|
|
- Atomic operation (all or nothing)
|
|
- Verify bank account ownership
|
|
- Return count of imported transactions
|
|
|
|
**Features:**
|
|
- Supports 2 CSV formats (Amount or Debit/Credit columns)
|
|
- Handles quoted fields with commas
|
|
- Handles escaped quotes
|
|
- Auto-detects CSV format
|
|
- Comprehensive validation
|
|
- Organization isolation
|
|
|
|
### Task 2: AR Invoice Payment Recording
|
|
|
|
Enhanced AR invoice detail page with payment recording capability:
|
|
|
|
1. **Payment Recording Modal**
|
|
- Form for entering payment details
|
|
- Payment amount (validated > 0)
|
|
- Payment date (date picker)
|
|
- Payment method (dropdown with 7 options)
|
|
- Error handling and loading states
|
|
|
|
2. **Integration with Existing Payment API**
|
|
- Uses existing `/api/payments` endpoint
|
|
- Automatically updates invoice balances
|
|
- Updates invoice status (PAID or PARTIALLY_PAID)
|
|
- Creates payment allocations
|
|
|
|
3. **AR List Page Integration**
|
|
- "Record Payment" action in context menu
|
|
- Navigates to invoice detail with auto-opening modal
|
|
- Direct payment entry from list
|
|
|
|
**Features:**
|
|
- Modal auto-opens with query parameter `?action=payment`
|
|
- Form validation on client and server
|
|
- Error display with helpful messages
|
|
- Loading state during submission
|
|
- Auto-refresh after successful payment
|
|
- Maintains invoice context
|
|
|
|
## Architecture
|
|
|
|
### Authentication & Multi-Tenancy
|
|
- All endpoints use `getOrganization()` for tenant isolation
|
|
- Bank account ownership verified before import
|
|
- Organization ID included in all queries
|
|
|
|
### Error Handling
|
|
- Standard HTTP status codes
|
|
- Descriptive error messages
|
|
- Input validation on both client and server
|
|
- Proper exception handling with try-catch
|
|
|
|
### Data Flow
|
|
|
|
#### CSV Import
|
|
```
|
|
User uploads CSV
|
|
↓
|
|
Parse endpoint validates and extracts transactions
|
|
↓
|
|
UI displays preview of parsed transactions
|
|
↓
|
|
User confirms import
|
|
↓
|
|
Import endpoint validates and bulk inserts
|
|
↓
|
|
Success response with transaction count
|
|
↓
|
|
Transactions appear in bank account
|
|
```
|
|
|
|
#### Payment Recording
|
|
```
|
|
User clicks "Record Payment" button
|
|
↓
|
|
Modal form opens
|
|
↓
|
|
User enters payment details
|
|
↓
|
|
User submits form
|
|
↓
|
|
Payment endpoint creates payment record
|
|
↓
|
|
Invoice balances updated
|
|
↓
|
|
Invoice status updated
|
|
↓
|
|
Modal closes, page refreshes
|
|
```
|
|
|
|
## API Reference
|
|
|
|
### Parse CSV
|
|
```bash
|
|
POST /api/bank-transactions/parse-csv
|
|
Content-Type: multipart/form-data
|
|
|
|
file: [CSV file]
|
|
```
|
|
|
|
Response:
|
|
```json
|
|
{
|
|
"success": true,
|
|
"count": 100,
|
|
"transactions": [
|
|
{
|
|
"transactionDate": "2024-01-15",
|
|
"description": "Office Supplies",
|
|
"transactionType": "DEBIT",
|
|
"amount": 150.50
|
|
}
|
|
]
|
|
}
|
|
```
|
|
|
|
### Import Transactions
|
|
```bash
|
|
POST /api/bank-transactions/import
|
|
Content-Type: application/json
|
|
|
|
{
|
|
"bankAccountId": "uuid",
|
|
"transactions": [...]
|
|
}
|
|
```
|
|
|
|
Response:
|
|
```json
|
|
{
|
|
"success": true,
|
|
"imported": 100,
|
|
"message": "Successfully imported 100 transaction(s)"
|
|
}
|
|
```
|
|
|
|
### Record Payment
|
|
```bash
|
|
POST /api/payments
|
|
Content-Type: application/json
|
|
|
|
{
|
|
"paymentDate": "2024-01-20",
|
|
"paymentMethod": "ACH",
|
|
"paymentAmount": 1500.00,
|
|
"customerId": "uuid",
|
|
"invoiceId": "uuid",
|
|
"paymentAllocations": [...]
|
|
}
|
|
```
|
|
|
|
Response: Created payment object with payment number and allocations
|
|
|
|
## Development Workflow
|
|
|
|
### 1. Testing CSV Import
|
|
|
|
**Prepare Test Data:**
|
|
1. Create or download CSV file (see EXAMPLE_CSVs.md)
|
|
2. Ensure correct format with Date, Description, Amount columns
|
|
3. Verify bank account exists in system
|
|
|
|
**Test Parse Endpoint:**
|
|
```bash
|
|
curl -X POST http://localhost:3000/api/bank-transactions/parse-csv \
|
|
-F "file=@test.csv"
|
|
```
|
|
|
|
**Test Import Endpoint:**
|
|
```bash
|
|
curl -X POST http://localhost:3000/api/bank-transactions/import \
|
|
-H "Content-Type: application/json" \
|
|
-d '{
|
|
"bankAccountId": "your-account-id",
|
|
"transactions": [... parsed transactions ...]
|
|
}'
|
|
```
|
|
|
|
### 2. Testing Payment Recording
|
|
|
|
**Manual Testing:**
|
|
1. Log into application
|
|
2. Navigate to AR invoices
|
|
3. Click on any open invoice
|
|
4. Click "Record Payment" button
|
|
5. Enter payment details
|
|
6. Submit form
|
|
7. Verify invoice status updates
|
|
|
|
**Test with Query Parameter:**
|
|
```
|
|
/finance/ar/invoice/{invoiceId}?action=payment
|
|
```
|
|
Should auto-open payment modal.
|
|
|
|
### 3. Frontend Integration
|
|
|
|
Build the CSV import UI using FRONTEND_INTEGRATION_GUIDE.md as reference:
|
|
- File upload component
|
|
- CSV preview table
|
|
- Bank account selector
|
|
- Import confirmation
|
|
|
|
## CSV Format Examples
|
|
|
|
### Format 1: Date, Description, Amount
|
|
```csv
|
|
Date,Description,Amount
|
|
2024-01-15,Office Supplies,150.50
|
|
2024-01-16,Client Payment,2500.00
|
|
```
|
|
|
|
### Format 2: Date, Description, Debit, Credit
|
|
```csv
|
|
Date,Description,Debit,Credit
|
|
2024-01-15,Office Supplies,150.50,0
|
|
2024-01-16,Client Payment,0,2500.00
|
|
```
|
|
|
|
### Format 3: With Quoted Descriptions
|
|
```csv
|
|
Date,Description,Amount
|
|
2024-01-15,"Office Supplies, Inc., Springfield",150.50
|
|
2024-01-16,"Client Payment, Invoice #INV-123",2500.00
|
|
```
|
|
|
|
See EXAMPLE_CSVs.md for more examples and bank-specific formats.
|
|
|
|
## Common Issues & Troubleshooting
|
|
|
|
### CSV Not Parsing
|
|
- Check column headers (Date, Description required)
|
|
- Verify file is valid CSV format
|
|
- Check for special characters in descriptions
|
|
- Ensure amounts are numeric without currency symbols
|
|
|
|
### Import Fails
|
|
- Verify bank account ID is correct
|
|
- Check organization ownership
|
|
- Validate transaction types are DEBIT or CREDIT
|
|
- Ensure amounts are positive numbers
|
|
|
|
### Payment Not Recording
|
|
- Verify customer exists
|
|
- Verify invoice exists and belongs to customer
|
|
- Check invoice status (some statuses may be readonly)
|
|
- Verify payment amount is greater than 0
|
|
|
|
### Modal Not Opening
|
|
- Check URL has `?action=payment` query parameter
|
|
- Verify invoice detail page loads successfully
|
|
- Check browser console for JavaScript errors
|
|
- Clear browser cache and reload
|
|
|
|
## Performance Considerations
|
|
|
|
### CSV Import
|
|
- Supports files up to 10MB (50,000+ transactions)
|
|
- Bulk import in single database operation
|
|
- No row-by-row processing overhead
|
|
- Consider pagination for preview if 1000+ transactions
|
|
|
|
### Payment Recording
|
|
- Modal form lightweight component
|
|
- Single API call per payment
|
|
- Invoice refetch may take few seconds
|
|
- Fallback to page refresh if needed
|
|
|
|
## Security Notes
|
|
|
|
### Authentication
|
|
- All endpoints require valid session
|
|
- Organization isolation enforced
|
|
- Bank account ownership verified
|
|
|
|
### Input Validation
|
|
- Transaction amounts must be positive
|
|
- Transaction types must be valid enum
|
|
- Bank account must belong to organization
|
|
- Customer and invoice must exist and match organization
|
|
|
|
### Data Protection
|
|
- CSV data validated before import
|
|
- Transactions created with status PENDING
|
|
- Organization ID required for all queries
|
|
- Proper HTTP status codes for errors
|
|
|
|
## Future Enhancements
|
|
|
|
1. **CSV Column Mapping**
|
|
- Allow users to map custom CSV columns
|
|
- Save mappings for future imports
|
|
|
|
2. **Batch Payment Import**
|
|
- Import payments from CSV
|
|
- Auto-match to invoices
|
|
- Reconciliation matching
|
|
|
|
3. **Payment Verification**
|
|
- Confirm payment details (routing numbers, etc.)
|
|
- Store payment references
|
|
- Payment reconciliation workflow
|
|
|
|
4. **Webhook Support**
|
|
- Receive payment notifications
|
|
- Auto-create payment records
|
|
- Real-time reconciliation
|
|
|
|
5. **Advanced Reporting**
|
|
- Import history and audit trail
|
|
- Failed transaction log
|
|
- Reconciliation reports
|
|
|
|
## Support & Questions
|
|
|
|
For questions about implementation, refer to:
|
|
1. **API_ENDPOINTS.md** - API specifications
|
|
2. **IMPLEMENTATION_SUMMARY.md** - Technical details
|
|
3. **FRONTEND_INTEGRATION_GUIDE.md** - Code examples
|
|
4. **EXAMPLE_CSVs.md** - CSV format guide
|
|
|
|
All code follows existing ERP platform patterns and conventions.
|