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;
|
|
|
|
|
|
2025-11-25 13:14:53 +05:30
|
|
|
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',
|
|
|
|
|
];
|
|
|
|
|
|
2025-11-17 10:33:11 +05:30
|
|
|
|
|
|
|
|
/****************************
|
|
|
|
|
* Relationships
|
|
|
|
|
****************************/
|
|
|
|
|
|
|
|
|
|
public function items()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(InvoiceItem::class)->orderBy('id', 'ASC');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function order()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(Order::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function customer()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(User::class, 'customer_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->final_amount_with_gst = $this->final_amount + $gst;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check overdue status condition
|
|
|
|
|
public function isOverdue()
|
|
|
|
|
{
|
|
|
|
|
return $this->status === 'pending' && now()->gt($this->due_date);
|
|
|
|
|
}
|
2025-11-25 13:14:53 +05:30
|
|
|
|
|
|
|
|
public function getShipment()
|
|
|
|
|
{
|
|
|
|
|
return $this->order?->shipments?->first();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function installments()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(InvoiceInstallment::class);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-22 19:22:01 +05:30
|
|
|
// 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
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-25 13:14:53 +05:30
|
|
|
|
2025-11-17 10:33:11 +05:30
|
|
|
}
|