All Kent Code Updated

This commit is contained in:
Utkarsh Khedkar
2026-02-27 10:51:26 +05:30
parent 338425535e
commit e188780329
38 changed files with 9288 additions and 5775 deletions

View File

@@ -9,41 +9,29 @@ class Invoice extends Model
{
use HasFactory;
protected $fillable = [
'order_id',
'customer_id',
'mark_no',
'invoice_number',
'invoice_date',
'due_date',
'payment_method',
'reference_no',
'status',
'final_amount', // without tax
'tax_type', // gst / igst
'gst_percent', // only used for gst UI input
'cgst_percent',
'sgst_percent',
'igst_percent',
'gst_amount', // total tax amount
'final_amount_with_gst',
'customer_name',
'company_name',
'customer_email',
'customer_mobile',
'customer_address',
'pincode',
'pdf_path',
'notes',
];
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
@@ -54,9 +42,9 @@ class Invoice extends Model
return $this->hasMany(InvoiceItem::class)->orderBy('id', 'ASC');
}
public function order()
public function container()
{
return $this->belongsTo(Order::class);
return $this->belongsTo(Container::class);
}
public function customer()
@@ -64,19 +52,28 @@ class Invoice extends Model
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
****************************/
// Auto calculate GST fields (you can call this in controller before saving)
public function calculateTotals()
{
$gst = ($this->final_amount * $this->gst_percent) / 100;
$this->gst_amount = $gst;
$this->gst_amount = $gst;
$this->final_amount_with_gst = $this->final_amount + $gst;
}
// Check overdue status condition
public function isOverdue()
{
return $this->status === 'pending' && now()->gt($this->due_date);
@@ -84,27 +81,31 @@ class Invoice extends Model
public function getShipment()
{
return $this->order?->shipments?->first();
return null;
}
public function installments()
// ✅ 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->hasMany(InvoiceInstallment::class);
return $this->installments->sum('amount');
}
// App\Models\Invoice.php
public function totalPaid()
{
return $this->installments()->sum('amount');
}
public function remainingAmount()
{
return max(
($this->final_amount_with_gst ?? 0) - $this->totalPaid(),
0
);
return $this->grand_total_with_charges - $this->totalPaid();
}