130 lines
3.0 KiB
PHP
130 lines
3.0 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',
|
|
// totals from charge groups
|
|
'charge_groups_total',
|
|
'grand_total_with_charges',
|
|
'tax_type',
|
|
'cgst_percent',
|
|
'sgst_percent',
|
|
'igst_percent',
|
|
];
|
|
|
|
/****************************
|
|
* 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);
|
|
}
|
|
|
|
public function chargeGroups()
|
|
{
|
|
return $this->hasMany(InvoiceChargeGroup::class, 'invoice_id');
|
|
}
|
|
|
|
/****************************
|
|
* Helper Functions
|
|
****************************/
|
|
|
|
// (Items based calculateTotals वापरणार नाहीस तरी ठेवू शकतोस)
|
|
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 base total (WITHOUT GST)
|
|
public function getChargeGroupsTotalAttribute()
|
|
{
|
|
// base = total_charge sum
|
|
return (float) $this->chargeGroups->sum('total_charge');
|
|
}
|
|
|
|
// ✅ Grand total: Charge groups base + GST (items ignore)
|
|
public function getGrandTotalWithChargesAttribute()
|
|
{
|
|
$base = (float) ($this->charge_groups_total ?? 0);
|
|
$gst = (float) ($this->gst_amount ?? 0);
|
|
|
|
return $base + $gst;
|
|
}
|
|
|
|
public function totalPaid(): float
|
|
{
|
|
return (float) $this->installments()->sum('amount');
|
|
}
|
|
|
|
public function remainingAmount(): float
|
|
{
|
|
$grand = (float) $this->grand_total_with_charges;
|
|
$paid = (float) $this->totalPaid();
|
|
|
|
return max(0, $grand - $paid);
|
|
}
|
|
|
|
public function isLockedForEdit(): bool
|
|
{
|
|
return $this->status === 'paid';
|
|
}
|
|
|
|
|
|
}
|