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

Related Controllers

---

๐Ÿ—„๏ธ Database Tables

Primary Tables (Direct Operations)

Table NamePurposeKey Columns
**premiumautomatic**Automatic premium configurationsid, totalinstallment, presenterDir, presenterPer, remainAmount, installmentNos, installmentMonths, installmentDays, clientId, userid, date
**premiuminstallments**Generated installment schedulesid, premiumauto_id, installment_date, installment_amount, payment_number
**premiumclient**Premium client masterid, client_id, userid, date
**premium**Individual premium paymentsid, date, value, premiumclientId, payed, rest, conditions
### Reference Tables

Table NamePurposeKey Columns
**client**Customer informationclientid, clientname, typeclientid
**typeclient**Client type definitionstypeId, typeName
**user**System usersuserid, username
**youtubelink**Tutorial videosyoutubelinkid, title, url
### Table Structure Details

premiumautomatic:

---

๐Ÿ”‘ 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:

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

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
START: Create Automatic Premium
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
1Input Calculation Parameters
- Total installment amount
- Down payment (fixed amount or percentage)
- Number of installments
- Payment frequency (months/days)
- Starting date
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
2Calculate Down Payment
IF presenterPer > 0:
โ”‚ โ””โ”€โ†’ Down Payment = (totalinstallment * presenterPer) / 100โ”‚
ELSE:
โ”‚ โ””โ”€โ†’ Down Payment = presenterDir โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
3Calculate Installment Details
- Remaining Amount = Total - Down Payment
- Generate payment dates based on frequency
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
4Create Database Records
- Insert premiumautomatic configuration
- Create premiuminstallments schedule
- Generate premium records for payments
- Link all records together
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
5Generate Payment Schedule
FOR EACH installment (1 to N):
โ”‚
โ†’ Calculate payment date
โ”‚ (start date + (installment# * frequency))
โ”‚
โ†’ Create installment record
โ”‚
โ”‚ โ””โ”€โ†’ Create premium payment record โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

---

๐ŸŒ URL Routes & Actions

URL ParameterFunction CalledDescriptionAuthentication
`do=` (empty)DefaultDisplay add automatic premium formRequired
`do=add``add()`Create automatic premium scheduleRequired
`do=show``show()`List all automatic premiumsRequired
`do=details``details()`View automatic premium detailsRequired
`do=edit``edit()`Display edit formRequired
`do=update``update()`Update automatic premiumRequired
`do=delete``delete()`Delete automatic premiumRequired
`do=updatefromreport``updatefromreport()`Update from report viewRequired
`do=deletefromRemport``deletefromRemport()`Delete from report viewRequired
### Required Parameters

Create Automatic Premium (do=add):

---

๐Ÿงฎ 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

Input Validation Concerns

Financial calculations require validation:

---

๐Ÿ“Š 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

---

Documented By: AI Assistant

Review Status: โš ๏ธ Needs validation and precision handling

Next Review: After financial validation implemented