| Path : /var/www/html/thb_loan_system/app/Models/ |
|
B-Con CMD Config cPanel C-Rdp D-Log Info Jump Mass Ransom Symlink vHost Zone-H |
| Current File : //var/www/html/thb_loan_system/app/Models/Loan.php |
<?php
namespace App\Models;
use App\Builders\LoanBuilder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Loan extends Model
{
use SoftDeletes;
protected $table = "loans";
public function charges()
{
return $this->hasMany(LoanCharge::class, 'loan_id', 'id');
}
public function schedules()
{
return $this->hasMany(LoanSchedule::class, 'loan_id', 'id')->orderBy('due_date', 'asc');
}
public function latest_schedule()
{
return $this->hasOne(LoanSchedule::class)->latest('due_date')->where("description", "Repayment");
}
public function comments()
{
return $this->hasMany(LoanComment::class, 'loan_id', 'id')->orderBy('created_at', 'desc');;
}
public function transactions()
{
return $this->hasMany(LoanTransaction::class, 'loan_id', 'id')->orderBy('date', 'asc');;
}
public function payments()
{
return $this->hasMany(LoanRepayment::class, 'loan_id', 'id')->orderBy('collection_date', 'asc');;
}
public function collateral()
{
return $this->hasMany(Collateral::class, 'loan_id', 'id');
}
public function guarantors()
{
return $this->hasMany(LoanGuarantor::class, 'loan_id', 'id');
}
public function borrower()
{
return $this->hasOne(Borrower::class, 'id', 'borrower_id');
}
public function loan_product()
{
return $this->hasOne(LoanProduct::class, 'id', 'loan_product_id');
}
public function loan_repayment_method()
{
return $this->hasOne(LoanRepaymentMethod::class, 'id', 'repayment_method_id');
}
public function loan_disbursed_by()
{
return $this->hasOne(LoanDisbursedBy::class, 'id', 'loan_disbursed_by_id');
}
public function loan_officer()
{
return $this->hasOne(User::class, 'id', 'loan_officer_id');
}
public function loan_paid_items()
{
$principal = $fees = $penalty = $interest_waived = $interest = 0;
if (!empty($this->loan_product)) {
$payments = $this->transactions_sum_payment;
$interest_waived = $this->transactions_sum_waived;
foreach ($this->schedules as $schedule) {
//$schedules have not yet been covered
if ($payments > 0) {
//try to allocate the remaining payment to the respective elements
$repayment_order = unserialize($this->loan_product->repayment_order);
foreach ($repayment_order as $order) {
if ($order == 'interest') {
if ($payments > ($schedule->interest - $schedule->interest_waived)) {
$interest = $interest + $schedule->interest - $schedule->interest_waived;
$payments = $payments - $schedule->interest - $schedule->interest_waived;
} else {
$interest = $interest + $payments;
$payments = 0;
}
}
if ($order == 'penalty') {
if ($payments > $schedule->penalty) {
$penalty = $penalty + $schedule->penalty;
$payments = $payments - $schedule->penalty;
} else {
$penalty = $penalty + $payments;
$payments = 0;
}
}
if ($order == 'fees') {
if ($payments > $schedule->fees) {
$fees = $fees + $schedule->fees;
$payments = $payments - $schedule->fees;
} else {
$fees = $fees + $payments;
$payments = 0;
}
}
if ($order == 'principal') {
if ($payments > $schedule->principal) {
$principal = $principal + $schedule->principal;
$payments = $payments - $schedule->principal;
} else {
$principal = $principal + $payments;
$payments = 0;
}
}
}
} else {
break;
}
}
}
return $principal + $interest + $interest_waived + $fees + $penalty;
}
public function loan_paid_items2()
{
$allocation = [];
$principal = $fees = $penalty = $interest_waived = $interest = 0;
if (!empty($this->loan_product)) {
$payments = $this->transactions_sum_payment;
$interest_waived = $this->transactions_sum_waived;
foreach ($this->schedules as $schedule) {
//$schedules have not yet been covered
if ($payments > 0) {
//try to allocate the remaining payment to the respective elements
$repayment_order = unserialize($this->loan_product->repayment_order);
foreach ($repayment_order as $order) {
if ($order == 'interest') {
if ($payments > ($schedule->interest - $schedule->interest_waived)) {
$interest = $interest + $schedule->interest - $schedule->interest_waived;
$payments = $payments - $schedule->interest - $schedule->interest_waived;
} else {
$interest = $interest + $payments;
$payments = 0;
}
}
if ($order == 'penalty') {
if ($payments > $schedule->penalty) {
$penalty = $penalty + $schedule->penalty;
$payments = $payments - $schedule->penalty;
} else {
$penalty = $penalty + $payments;
$payments = 0;
}
}
if ($order == 'fees') {
if ($payments > $schedule->fees) {
$fees = $fees + $schedule->fees;
$payments = $payments - $schedule->fees;
} else {
$fees = $fees + $payments;
$payments = 0;
}
}
if ($order == 'principal') {
if ($payments > $schedule->principal) {
$principal = $principal + $schedule->principal;
$payments = $payments - $schedule->principal;
} else {
$principal = $principal + $payments;
$payments = 0;
}
}
}
} else {
break;
}
}
}
$allocation["principal"] = $principal;
$allocation["interest"] = $interest;
$allocation["interest_waived"] = $interest_waived;
$allocation["fees"] = $fees;
$allocation["penalty"] = $penalty;
return $allocation;
}
public function loan_due_items2()
{
$allocation = [];
$principal = 0;
$fees = 0;
$penalty = 0;
$interest = 0;
foreach ($this->schedules as $schedule) {
$interest = $interest + $schedule->interest;
$penalty = $penalty + $schedule->penalty;
$fees = $fees + $schedule->fees;
$principal = $principal + $schedule->principal;
}
$allocation["principal"] = $principal;
$allocation["interest"] = $interest;
$allocation["fees"] = $fees;
$allocation["penalty"] = $penalty;
return $allocation;
}
public static function query(): LoanBuilder
{
return parent::query(); // TODO: Change the autogenerated stub
}
public function newEloquentBuilder($query): LoanBuilder
{
return new LoanBuilder($query);
}
}