|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
|
|
|
|
|
|
|
|
class Invoice extends Model
|
|
|
|
{
|
|
|
|
use HasFactory;
|
|
|
|
|
|
|
|
protected $fillable = [
|
|
|
|
'customer_id',
|
|
|
|
'subtotal',
|
|
|
|
'total',
|
|
|
|
'gst',
|
|
|
|
'pst',
|
|
|
|
'paid',
|
|
|
|
'date',
|
|
|
|
];
|
|
|
|
|
|
|
|
protected $appends = [
|
|
|
|
'internal_id',
|
|
|
|
'gst_amount',
|
|
|
|
'pst_amount',
|
|
|
|
];
|
|
|
|
|
|
|
|
protected $casts = [
|
|
|
|
'total' => 'decimal:2',
|
|
|
|
'subtotal' => 'decimal:2',
|
|
|
|
];
|
|
|
|
|
|
|
|
public function getInternalIdAttribute(): string
|
|
|
|
{
|
|
|
|
$po = str_pad(strval($this->id), 4, '0', STR_PAD_LEFT);
|
|
|
|
$year = date('y');
|
|
|
|
|
|
|
|
return 'TN-IN-'.$year.'-'.$po;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function calculateTotals(): void
|
|
|
|
{
|
|
|
|
$subtotal = 0;
|
|
|
|
|
|
|
|
foreach ($this->orders as $order) {
|
|
|
|
$subtotal += $order->total_service_price;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->subtotal = $subtotal;
|
|
|
|
$this->total = $subtotal + $this->gst_amount + $this->pst_amount;
|
|
|
|
|
|
|
|
$this->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getGstAmountAttribute(): float
|
|
|
|
{
|
|
|
|
if ($this->gst) {
|
|
|
|
return number_format($this->subtotal * 0.05, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPstAmountAttribute(): float
|
|
|
|
{
|
|
|
|
if ($this->pst) {
|
|
|
|
return number_format($this->subtotal * 0.07, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function orders(): HasMany
|
|
|
|
{
|
|
|
|
return $this->HasMany(Order::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function customer(): BelongsTo
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Customer::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function productServices(): HasManyThrough
|
|
|
|
{
|
|
|
|
return $this->hasManyThrough(ProductService::class, Order::class);
|
|
|
|
}
|
|
|
|
}
|