Files
Kent-logistics-Laravel/app/Models/Invoice.php

130 lines
3.0 KiB
PHP
Raw Normal View History

2025-11-17 10:33:11 +05:30
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Invoice extends Model
{
use HasFactory;
2026-02-27 10:51:26 +05:30
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',
2026-03-11 20:02:43 +05:30
// totals from charge groups
'charge_groups_total',
'grand_total_with_charges',
'tax_type',
'cgst_percent',
'sgst_percent',
'igst_percent',
2026-02-27 10:51:26 +05:30
];
2025-11-17 10:33:11 +05:30
/****************************
* Relationships
****************************/
public function items()
{
return $this->hasMany(InvoiceItem::class)->orderBy('id', 'ASC');
}
2026-02-27 10:51:26 +05:30
public function container()
2025-11-17 10:33:11 +05:30
{
2026-02-27 10:51:26 +05:30
return $this->belongsTo(Container::class);
2025-11-17 10:33:11 +05:30
}
public function customer()
{
return $this->belongsTo(User::class, 'customer_id');
}
2026-02-27 10:51:26 +05:30
public function installments()
{
return $this->hasMany(InvoiceInstallment::class);
}
public function chargeGroups()
{
2026-03-11 20:02:43 +05:30
return $this->hasMany(InvoiceChargeGroup::class, 'invoice_id');
2026-02-27 10:51:26 +05:30
}
2025-11-17 10:33:11 +05:30
/****************************
* Helper Functions
****************************/
2026-03-11 20:02:43 +05:30
// (Items based calculateTotals वापरणार नाहीस तरी ठेवू शकतोस)
2025-11-17 10:33:11 +05:30
public function calculateTotals()
{
$gst = ($this->final_amount * $this->gst_percent) / 100;
2026-02-27 10:51:26 +05:30
$this->gst_amount = $gst;
2025-11-17 10:33:11 +05:30
$this->final_amount_with_gst = $this->final_amount + $gst;
}
public function isOverdue()
{
return $this->status === 'pending' && now()->gt($this->due_date);
}
2025-11-25 13:14:53 +05:30
public function getShipment()
{
2026-02-27 10:51:26 +05:30
return null;
2025-11-25 13:14:53 +05:30
}
2026-03-11 20:02:43 +05:30
// ✅ Charge groups base total (WITHOUT GST)
2026-02-27 10:51:26 +05:30
public function getChargeGroupsTotalAttribute()
{
2026-03-11 20:02:43 +05:30
// base = total_charge sum
2026-02-27 10:51:26 +05:30
return (float) $this->chargeGroups->sum('total_charge');
}
2025-11-25 13:14:53 +05:30
2026-03-11 20:02:43 +05:30
// ✅ Grand total: Charge groups base + GST (items ignore)
2026-02-27 10:51:26 +05:30
public function getGrandTotalWithChargesAttribute()
{
2026-03-11 20:02:43 +05:30
$base = (float) ($this->charge_groups_total ?? 0);
$gst = (float) ($this->gst_amount ?? 0);
2025-12-22 19:22:01 +05:30
2026-03-11 20:02:43 +05:30
return $base + $gst;
}
2026-02-27 10:51:26 +05:30
2026-03-11 20:02:43 +05:30
public function totalPaid(): float
{
return (float) $this->installments()->sum('amount');
}
2025-12-22 19:22:01 +05:30
2026-03-11 20:02:43 +05:30
public function remainingAmount(): float
{
$grand = (float) $this->grand_total_with_charges;
$paid = (float) $this->totalPaid();
2025-11-25 13:14:53 +05:30
2026-03-11 20:02:43 +05:30
return max(0, $grand - $paid);
}
2026-03-12 18:11:43 +05:30
public function isLockedForEdit(): bool
{
return $this->status === 'paid';
}
2025-11-17 10:33:11 +05:30
}