Files
Kent-logistics-Laravel/app/Models/Invoice.php
2026-02-27 10:51:26 +05:30

113 lines
2.5 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Invoice extends Model
{
use HasFactory;
protected $fillable = [
'container_id',
'customer_id',
'mark_no',
'invoice_number',
'invoice_date',
'due_date',
'payment_method',
'reference_no',
'status',
'final_amount',
'gst_percent',
'gst_amount',
'final_amount_with_gst',
'customer_name',
'company_name',
'customer_email',
'customer_mobile',
'customer_address',
'pincode',
'pdf_path',
'notes',
];
/****************************
* Relationships
****************************/
public function items()
{
return $this->hasMany(InvoiceItem::class)->orderBy('id', 'ASC');
}
public function container()
{
return $this->belongsTo(Container::class);
}
public function customer()
{
return $this->belongsTo(User::class, 'customer_id');
}
public function installments()
{
return $this->hasMany(InvoiceInstallment::class);
}
// ✅ SINGLE, correct relation
public function chargeGroups()
{
return $this->hasMany(\App\Models\InvoiceChargeGroup::class, 'invoice_id');
}
/****************************
* Helper Functions
****************************/
public function calculateTotals()
{
$gst = ($this->final_amount * $this->gst_percent) / 100;
$this->gst_amount = $gst;
$this->final_amount_with_gst = $this->final_amount + $gst;
}
public function isOverdue()
{
return $this->status === 'pending' && now()->gt($this->due_date);
}
public function getShipment()
{
return null;
}
// ✅ Charge groups total accessor
public function getChargeGroupsTotalAttribute()
{
// relation already loaded असेल तर collection वरून sum होईल
return (float) $this->chargeGroups->sum('total_charge');
}
// ✅ Grand total accessor (items + GST + charge groups)
public function getGrandTotalWithChargesAttribute()
{
return (float) ($this->final_amount_with_gst ?? 0) + $this->charge_groups_total;
}
public function totalPaid()
{
return $this->installments->sum('amount');
}
public function remainingAmount()
{
return $this->grand_total_with_charges - $this->totalPaid();
}
}