From a9dc78736c7a230f59718d4691d9962e7a25b11d Mon Sep 17 00:00:00 2001 From: Nisse Lommerde Date: Thu, 16 Jan 2025 17:37:32 -0500 Subject: [PATCH] More Tax Rates --- .../InvoiceResource/Pages/CreateInvoice.php | 4 ++ .../InvoiceResource/Pages/EditInvoice.php | 11 +++ app/Models/Invoice.php | 67 +++++++------------ app/Observers/InvoiceObserver.php | 49 -------------- database/factories/InvoiceFactory.php | 10 +-- 5 files changed, 43 insertions(+), 98 deletions(-) delete mode 100644 app/Observers/InvoiceObserver.php diff --git a/app/Filament/Admin/Resources/InvoiceResource/Pages/CreateInvoice.php b/app/Filament/Admin/Resources/InvoiceResource/Pages/CreateInvoice.php index 7a2280a..a16fbfe 100644 --- a/app/Filament/Admin/Resources/InvoiceResource/Pages/CreateInvoice.php +++ b/app/Filament/Admin/Resources/InvoiceResource/Pages/CreateInvoice.php @@ -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(); diff --git a/app/Filament/Admin/Resources/InvoiceResource/Pages/EditInvoice.php b/app/Filament/Admin/Resources/InvoiceResource/Pages/EditInvoice.php index 4c39b9d..843e583 100644 --- a/app/Filament/Admin/Resources/InvoiceResource/Pages/EditInvoice.php +++ b/app/Filament/Admin/Resources/InvoiceResource/Pages/EditInvoice.php @@ -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 [ diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index e5c88d2..fbc61c4 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -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); diff --git a/app/Observers/InvoiceObserver.php b/app/Observers/InvoiceObserver.php deleted file mode 100644 index 21cdd7a..0000000 --- a/app/Observers/InvoiceObserver.php +++ /dev/null @@ -1,49 +0,0 @@ -shuffle()->first(); return [ - '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), + 'created_at' => Carbon::now()->subDays(rand(1, 30)), + 'pst_rate' => TaxRate::where('name', 'PST')->value('value'), + 'gst_rate' => TaxRate::where('name', 'GST')->value('value'), + '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,