More Tax Rates
This commit is contained in:
parent
9efde6fa34
commit
a9dc78736c
@ -4,6 +4,7 @@
|
||||
|
||||
use App\Filament\Admin\Resources\InvoiceResource;
|
||||
use App\Models\Invoice;
|
||||
use App\Models\TaxRate;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
@ -13,6 +14,9 @@ class CreateInvoice extends CreateRecord
|
||||
|
||||
protected function handleRecordCreation(array $data): Model
|
||||
{
|
||||
$data['pst_rate'] = TaxRate::where('name', 'PST')->value('value');
|
||||
$data['gst_rate'] = TaxRate::where('name', 'GST')->value('value');
|
||||
|
||||
$invoice = Invoice::create($data);
|
||||
$invoice->calculateTotals();
|
||||
|
||||
|
@ -7,11 +7,22 @@
|
||||
use Filament\Actions;
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class EditInvoice extends EditRecord
|
||||
{
|
||||
protected static string $resource = InvoiceResource::class;
|
||||
|
||||
protected function handleRecordUpdate(Model $record, array $data): Model
|
||||
{
|
||||
$invoice = Invoice::find($record->id);
|
||||
|
||||
$invoice->update($data);
|
||||
$invoice->calculateTotals();
|
||||
|
||||
return $invoice;
|
||||
}
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
|
@ -32,6 +32,8 @@ class Invoice extends Model
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'has_gst' => 'boolean',
|
||||
'has_pst' => 'boolean',
|
||||
'date' => 'datetime',
|
||||
'total' => 'decimal:2',
|
||||
'subtotal' => 'decimal:2',
|
||||
@ -42,47 +44,42 @@ public static function boot(): void
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
static::created(function ($model) {
|
||||
static::created(function (Invoice $model) {
|
||||
$model->attributes['internal_id'] = 'TN4'.str_pad($model->id, 4, '0', STR_PAD_LEFT);
|
||||
$model->attributes['pst_rate'] = TaxRate::where('name', 'PST')->value('value');
|
||||
$model->attributes['gst_rate'] = TaxRate::where('name', 'GST')->value('value');
|
||||
$model->save();
|
||||
});
|
||||
}
|
||||
|
||||
public function setHasPstAttribute(bool $value): void
|
||||
{
|
||||
$this->attributes['has_pst'] = $value;
|
||||
$this->save();
|
||||
|
||||
$this->calculateTotals();
|
||||
}
|
||||
|
||||
public function setHasGstAttribute(bool $value): void
|
||||
{
|
||||
$this->attributes['has_gst'] = $value;
|
||||
$this->save();
|
||||
|
||||
$this->calculateTotals();
|
||||
}
|
||||
|
||||
public function calculateTotals(): void
|
||||
{
|
||||
$subtotal = 0;
|
||||
if (empty($this->orders)) {
|
||||
$this->subtotal = 0;
|
||||
$this->gst_amount = 0;
|
||||
$this->pst_amount = 0;
|
||||
$this->total = 0;
|
||||
$this->save();
|
||||
|
||||
// $gst_amount = ($this->gst_rate / 100) * $this->gst ?? 0;
|
||||
// $pst_amount = ($this->pst_rate / 100) * $this->pst ?? 0;
|
||||
|
||||
foreach ($this->orders as $order) {
|
||||
$subtotal += $order->total_service_price;
|
||||
return;
|
||||
}
|
||||
|
||||
$subtotal = $this->orders->sum(fn (Order $order) => $order->total_service_price);
|
||||
$this->subtotal = $subtotal;
|
||||
// $this->total = $subtotal + $gst_amount + $pst_amount;
|
||||
|
||||
$gstAmount = $this->calculateTaxAmount($subtotal, $this->gst_rate, $this->has_gst);
|
||||
$pstAmount = $this->calculateTaxAmount($subtotal, $this->pst_rate, $this->has_pst);
|
||||
|
||||
$this->gst_amount = $gstAmount;
|
||||
$this->pst_amount = $pstAmount;
|
||||
$this->total = $subtotal + $gstAmount + $pstAmount;
|
||||
|
||||
$this->save();
|
||||
}
|
||||
|
||||
private function calculateTaxAmount(float $amount, float $rate, ?bool $apply): float
|
||||
{
|
||||
return $apply ? $amount * ($rate / 100) : 0;
|
||||
}
|
||||
|
||||
public function setStatus(InvoiceStatus $status): void
|
||||
{
|
||||
if ($this->status !== $status) {
|
||||
@ -91,24 +88,6 @@ public function setStatus(InvoiceStatus $status): void
|
||||
}
|
||||
}
|
||||
|
||||
// public function getGstAmountAttribute(): float
|
||||
// {
|
||||
// if ($this->gst) {
|
||||
// return round($this->subtotal * ($this->gst_rate / 100), 2);
|
||||
// }
|
||||
//
|
||||
// return 0.00;
|
||||
// }
|
||||
//
|
||||
// public function getPstAmountAttribute(): float
|
||||
// {
|
||||
// if ($this->pst) {
|
||||
// return round($this->subtotal * ($this->pst_rate / 100), 2);
|
||||
// }
|
||||
//
|
||||
// return 0.00;
|
||||
// }
|
||||
|
||||
public function orders(): HasMany
|
||||
{
|
||||
return $this->HasMany(Order::class);
|
||||
|
@ -1,49 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Observers;
|
||||
|
||||
use App\Models\Invoice;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class InvoiceObserver
|
||||
{
|
||||
/**
|
||||
* Handle the Invoice "created" event.
|
||||
*/
|
||||
public function created(Invoice $invoice): void
|
||||
{
|
||||
Log::debug('Invoice created');
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the Invoice "updated" event.
|
||||
*/
|
||||
public function updated(Invoice $invoice): void
|
||||
{
|
||||
\Log::debug('Invoice updated!');
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the Invoice "deleted" event.
|
||||
*/
|
||||
public function deleted(Invoice $invoice): void
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the Invoice "restored" event.
|
||||
*/
|
||||
public function restored(Invoice $invoice): void
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the Invoice "force deleted" event.
|
||||
*/
|
||||
public function forceDeleted(Invoice $invoice): void
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
@ -21,8 +21,8 @@ public function definition(): array
|
||||
'created_at' => Carbon::now()->subDays(rand(1, 30)),
|
||||
'pst_rate' => TaxRate::where('name', 'PST')->value('value'),
|
||||
'gst_rate' => TaxRate::where('name', 'GST')->value('value'),
|
||||
// 'gst' => true,
|
||||
// 'pst' => $this->faker->boolean(40),
|
||||
'has_gst' => true,
|
||||
'has_pst' => $this->faker->boolean(40),
|
||||
'date' => Carbon::now()->subDays(rand(1, 60)),
|
||||
'status' => $this->faker->randomElement(InvoiceStatus::cases())->value,
|
||||
'customer_id' => $customer->id,
|
||||
|
Loading…
x
Reference in New Issue
Block a user