Premiumautomatic Documentation
Premium Automatic Controller Documentation
File: /controllers/premiumautomaticController.php
Purpose: Manages automated premium installment schedule generation and calculations
Last Updated: December 20, 2024
Total Functions: 6+ (complex controller)
Lines of Code: ~503
---
๐ Overview
The Premium Automatic Controller handles the automated generation and management of premium installment schedules. Unlike manual premium creation, this controller provides sophisticated algorithms for calculating installment amounts, dates, and payment schedules based on parameters like down payment percentages, interest rates, and payment frequencies. It's designed for complex installment plans with automatic calculation capabilities.
Primary Functions
- โ Automated premium schedule generation
- โ Down payment and installment calculations
- โ Interest rate and percentage-based calculations
- โ Flexible payment frequency options (monthly, weekly, etc.)
- โ Installment date generation algorithms
- โ Edit and update automatic premium schedules
- โ Delete automatic premium records
- โ Integration with manual premium system
Related Controllers
- โข premiumController.php - Manual premium management
- โข premiumReportController.php - Premium reporting
- โข allpremiumReportController.php - All premium reports
- โข clientController.php - Client management
---
๐๏ธ Database Tables
Primary Tables (Direct Operations)
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **premiumautomatic** | Automatic premium configurations | id, totalinstallment, presenterDir, presenterPer, remainAmount, installmentNos, installmentMonths, installmentDays, clientId, userid, date | |
| **premiuminstallments** | Generated installment schedules | id, premiumauto_id, installment_date, installment_amount, payment_number | |
| **premiumclient** | Premium client master | id, client_id, userid, date | |
| **premium** | Individual premium payments | id, date, value, premiumclientId, payed, rest, conditions |
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **client** | Customer information | clientid, clientname, typeclientid | |
| **typeclient** | Client type definitions | typeId, typeName | |
| **user** | System users | userid, username | |
| **youtubelink** | Tutorial videos | youtubelinkid, title, url |
premiumautomatic:
- โข
id- Primary key - โข
totalinstallment- Total installment amount - โข
presenterDir- Down payment amount (fixed) - โข
presenterPer- Down payment percentage - โข
remainAmount- Remaining amount after down payment - โข
installmentNos- Number of installments - โข
installmentMonths- Payment frequency in months - โข
installmentDays- Payment frequency in days - โข
clientId- Reference to client - โข
userid- Creator user ID - โข
date- Creation date
---
๐ Key Functions
1. Default Action - Add Automatic Premium Form
Location: Lines 107-115
Purpose: Display form for creating automated premium schedules
Process Flow:
1. Check authentication
2. Load all clients for selection
3. Display automatic premium creation form
4. Form includes calculation parameters (amounts, percentages, frequencies)
---
2. add() - Create Automatic Premium Schedule
Purpose: Generate automated premium schedule with calculations
Key Features:
- โข Down payment calculations (fixed amount or percentage)
- โข Automatic installment amount calculations
- โข Date generation based on frequency parameters
- โข Integration with manual premium system
Expected Process:
1. Extract form parameters (totals, percentages, frequencies)
2. Calculate down payment amount
3. Calculate remaining balance
4. Generate installment schedule with dates
5. Create premium client master record
6. Generate individual installment records
---
3. show() - List Automatic Premiums
Location: Lines 128-140
Purpose: Display all automatic premium configurations
Process Flow:
1. Query all automatic premium records
2. Enrich data with user and client information
3. Display via template with tutorial videos
4. Provide links to edit/delete operations
---
4. details() - Automatic Premium Details
Location: Lines 141-185
Purpose: Show detailed view of specific automatic premium with calculations
Process Flow:
1. Load automatic premium master data
2. Load associated premium payments
3. Load generated installment schedule
4. Calculate totals and unpaid amounts
5. Process client type information
6. Display comprehensive details view
Total Calculation:
foreach ($alldataclient as $item) {
if ($item->conditions == 0 && $item->payed == 0) {
$total += $item->value - $item->rest;
}
}
---
5. edit() - Edit Automatic Premium
Location: Lines 201-242
Purpose: Load automatic premium for editing with installment data
Process Flow:
1. Load automatic premium configuration
2. Load generated installment dates/amounts
3. Load client information with types
4. Display edit form with current values
5. Allow modification of calculation parameters
---
6. update() - Update Automatic Premium
Location: Lines 243-253
Purpose: Update automatic premium configuration and regenerate schedule
Expected Process:
1. Update automatic premium master record
2. Recalculate installments based on new parameters
3. Delete old installment schedule
4. Generate new installment schedule
5. Update related premium records
---
๐ Workflows
Workflow 1: Create Automatic Premium Schedule
---
๐ URL Routes & Actions
| URL Parameter | Function Called | Description | Authentication | |
|---|---|---|---|---|
| `do=` (empty) | Default | Display add automatic premium form | Required | |
| `do=add` | `add()` | Create automatic premium schedule | Required | |
| `do=show` | `show()` | List all automatic premiums | Required | |
| `do=details` | `details()` | View automatic premium details | Required | |
| `do=edit` | `edit()` | Display edit form | Required | |
| `do=update` | `update()` | Update automatic premium | Required | |
| `do=delete` | `delete()` | Delete automatic premium | Required | |
| `do=updatefromreport` | `updatefromreport()` | Update from report view | Required | |
| `do=deletefromRemport` | `deletefromRemport()` | Delete from report view | Required |
Create Automatic Premium (do=add):
- โข
clientID- Selected client ID - โข
totalinstallment- Total premium amount - โข
presenterDir- Down payment fixed amount - โข
presenterPer- Down payment percentage - โข
installmentNos- Number of installments - โข
installmentMonths- Payment frequency in months - โข
installmentDays- Payment frequency in days - โข
startDate- First payment date
---
๐งฎ Calculation Methods
Down Payment Calculation
// If percentage specified, calculate from total
if ($presenterPer > 0) {
$downPayment = ($totalinstallment * $presenterPer) / 100;
} else {
$downPayment = $presenterDir; // Use fixed amount
}
Remaining Balance Calculation
$remainAmount = $totalinstallment - $downPayment;
Individual Installment Amount
$installmentAmount = $remainAmount / $installmentNos;
Payment Date Generation
// For each installment
for ($i = 1; $i <= $installmentNos; $i++) {
if ($installmentMonths > 0) {
$paymentDate = date('Y-m-d', strtotime('+' . ($i * $installmentMonths) . ' months', strtotime($startDate)));
} else {
$paymentDate = date('Y-m-d', strtotime('+' . ($i * $installmentDays) . ' days', strtotime($startDate)));
}
}
---
๐ Security & Permissions
Security Features
- โข Authentication required for all operations
- โข Session-based user tracking
- โข Exception handling for all operations
Input Validation Concerns
Financial calculations require validation:
- โข Ensure positive amounts for totals and payments
- โข Validate percentage ranges (0-100%)
- โข Verify installment numbers > 0
- โข Check date validity
---
๐ Performance Considerations
Calculation Complexity
1. Mathematical Operations: Multiple calculations for each installment
2. Date Generation: Iterative date calculations for schedules
3. Database Operations: Multiple table insertions for complete schedules
Optimization Opportunities
1. Batch Insertions: Insert multiple installments in single transaction
2. Calculation Caching: Store calculated values to avoid recalculation
3. Date Optimization: Use efficient date arithmetic libraries
---
๐ Common Issues & Troubleshooting
1. Floating Point Precision
Issue: Installment calculations may have rounding errors
Example: $100 / 3 = $33.33 leaves $0.01 remainder
Fix: Implement proper rounding and remainder handling
2. Date Calculation Edge Cases
Issue: Month-based calculations may skip dates
Example: January 31 + 1 month = March 3 (skips February 29/28)
Fix: Use proper date libraries with month-end handling
3. Validation Missing
Issue: No validation for calculation parameters
Risk: Division by zero, negative amounts, invalid dates
---
๐งช Testing Scenarios
Test Case 1: Percentage Down Payment
1. Set total amount = $1000
2. Set down payment percentage = 20%
3. Set 10 installments
4. Verify down payment = $200
5. Verify installment amount = $80 each
6. Check payment schedule generation
Test Case 2: Fixed Down Payment
1. Set total amount = $1000
2. Set fixed down payment = $150
3. Set 12 monthly installments
4. Verify remaining = $850
5. Verify installment amount = $70.83
6. Test rounding handling
Test Case 3: Date Generation
1. Set start date = 2024-01-15
2. Set monthly frequency
3. Set 6 installments
4. Verify dates: 2024-02-15, 2024-03-15, etc.
5. Test month-end edge cases
---
๐ Related Documentation
- โข CLAUDE.md - PHP 8.2 migration guide
- โข premiumController.md - Manual premium management
- โข Financial Calculations Guide - Precision and rounding
- โข Date Arithmetic Best Practices - Handling date calculations
- โข Transaction Management - Database consistency
---
Documented By: AI Assistant
Review Status: โ ๏ธ Needs validation and precision handling
Next Review: After financial validation implemented