iamvimukthi / loancalculator
A simple Laravel package for calculating loans, including EMI, interest, and total payments. Perfect for financial apps with easy integration and customizable options.
Installs: 5
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:package
README
A Laravel package for calculating loan details, including EMI (Equated Monthly Installment), Total Cost, Total Interest, and generating a detailed Payment Schedule. This package is ideal for financial applications requiring loan calculations.
Features
- Calculate EMI, Total Cost, and Total Interest for a loan.
- Generate a Payment Schedule with a month-by-month breakdown of payments.
- Accepts flexible loan terms in both years and months.
- Easy integration with Laravel applications.
Installation
Step 1: Install via Composer
Run the installation command:
composer require iamvimukthi/loancalculator:1.0.0
Step 2: (Optional) Publish Configuration
To publish the configuration file (if required):
php artisan vendor:publish --provider="Iamvimukthi\Loancalculator\LoanCalculatorService"
Usage
1. Calculate Loan Details
Use the calculateLoanDetails
method to calculate the EMI, total cost, and total interest for a loan.
use Iamvimukthi\Loancalculator\LoanCalculator;
$loanAmount = 500000; // Principal amount
$annualInterestRate = 5; // Annual interest rate (percentage)
$loanTermYears = 5; // Loan term in years
$loanTermMonths = 6; // Additional months
$loanDetails = LoanCalculator::calculateLoanDetails($loanAmount, $annualInterestRate, $loanTermYears, $loanTermMonths);
echo "Monthly EMI: " . number_format($loanDetails['EMI'], 2) . "\n";
echo "Total Cost of Loan: " . number_format($loanDetails['TotalCost'], 2) . "\n";
echo "Total Interest: " . number_format($loanDetails['TotalInterest'], 2) . "\n";
Output Example:
Monthly EMI: 8,680.78
Total Cost of Loan: 572,931.50
Total Interest: 72,931.50
2. Generate Payment Schedule
Use the generatePaymentSchedule
method to create a detailed payment schedule for the loan.
$schedule = LoanCalculator::generatePaymentSchedule($loanAmount, $annualInterestRate, $loanTermYears, $loanTermMonths);
foreach ($schedule as $payment) {
echo "Month-Year: {$payment['MonthYear']}, Principal: " . number_format($payment['PrincipalPayment'], 2) . ", Interest: " . number_format($payment['InterestPayment'], 2) . ", Total: " . number_format($payment['TotalPayment'], 2) . ", Balance: " . number_format($payment['Balance'], 2) . "\n";
}
Output Example:
Month-Year: Dec-2024, Principal: 6,597.45, Interest: 2,083.33, Total: 8,680.78, Balance: 493,402.55
Month-Year: Jan-2025, Principal: 6,624.94, Interest: 2,055.84, Total: 8,680.78, Balance: 486,777.62
...
Methods
calculateLoanDetails($loanAmount, $annualInterestRate, $loanTermYears, $loanTermMonths = 0)
- Description: Calculates EMI, total cost, and total interest for the given loan.
- Parameters:
$loanAmount
(float): Principal loan amount.$annualInterestRate
(float): Annual interest rate (percentage).$loanTermYears
(int): Loan term in years.$loanTermMonths
(int): Additional months to add to the loan term (default: 0).
- Returns: Array containing:
'EMI'
: Monthly installment.'TotalCost'
: Total loan cost (principal + interest).'TotalInterest'
: Total interest paid.
generatePaymentSchedule($loanAmount, $annualInterestRate, $loanTermYears, $loanTermMonths = 0)
- Description: Generates a detailed payment schedule for the loan.
- Parameters:
$loanAmount
(float): Principal loan amount.$annualInterestRate
(float): Annual interest rate (percentage).$loanTermYears
(int): Loan term in years.$loanTermMonths
(int): Additional months to add to the loan term (default: 0).
- Returns: Array containing:
'MonthYear'
: Month and year of the payment.'PrincipalPayment'
: Principal portion of the payment.'InterestPayment'
: Interest portion of the payment.'TotalPayment'
: Total payment (EMI).'Balance'
: Remaining loan balance.
Testing
Run unit tests for the package using PHPUnit to ensure calculations are accurate.
php artisan test
Example test case:
use Iamvimukthi\Loancalculator\LoanCalculator;
class LoanCalculatorTest extends TestCase
{
public function testCalculateLoanDetails()
{
$loanDetails = LoanCalculator::calculateLoanDetails(500000, 5, 5, 6);
$this->assertEquals(9432.26, $loanDetails['EMI']);
$this->assertEquals(566136.45, $loanDetails['TotalCost']);
$this->assertEquals(66136.45, $loanDetails['TotalInterest']);
}
}
License
This package is licensed under the MIT License.
Contributing
Contributions are welcome! Please follow these steps to contribute:
- Fork the repository.
- Create a new branch (
git checkout -b feature/YourFeature
). - Commit your changes (
git commit -m 'Add some feature'
). - Push to the branch (
git push origin feature/YourFeature
). - Open a Pull Request.
Contact
For any issues or suggestions, please create an issue on the GitHub Issues page.