Invoice model

This commit is contained in:
Matt Young 2026-01-27 21:59:59 -06:00
parent 6c4deaa298
commit 67de82a525
4 changed files with 61 additions and 2 deletions

29
app/Casts/MoneyCast.php Normal file
View File

@ -0,0 +1,29 @@
<?php
namespace App\Casts;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Database\Eloquent\Model;
class MoneyCast implements CastsAttributes
{
/**
* Cast the given value.
*
* @param array<string, mixed> $attributes
*/
public function get(Model $model, string $key, mixed $value, array $attributes): float
{
return $value / 100;
}
/**
* Prepare the given value for storage.
*
* @param array<string, mixed> $attributes
*/
public function set(Model $model, string $key, mixed $value, array $attributes): int
{
return (int) round($value * 100);
}
}

View File

@ -0,0 +1,11 @@
<?php
namespace App\Enums;
enum InvoiceStatus: string
{
case DRAFT = 'draft';
case POSTED = 'posted';
case VOID = 'void';
case PAID = 'paid';
}

View File

@ -2,9 +2,22 @@
namespace App\Models;
use App\Casts\MoneyCast;
use App\Enums\InvoiceStatus;
use Illuminate\Database\Eloquent\Model;
class Invoice extends Model
{
//
protected $casts = [
'total' => MoneyCast::class,
'status' => InvoiceStatus::class,
'invoice_date' => 'date',
'due_date' => 'date',
'date_sent' => 'date',
];
public function formattedTotal(): string
{
return '$'.number_format($this->total, 2);
}
}

View File

@ -16,7 +16,13 @@ return new class extends Migration
$table->id();
$table->string('invoice_number')->unique();
$table->foreignIdFor(Client::class);
$table->json('lines');
$table->string('status')->default('draft');
$table->date('invoice_date')->nullable();
$table->date('date_sent')->nullable();
$table->date('due_date')->nullable();
$table->integer('total');
$table->text('notes')->nullable();
$table->text('internal_notes')->nullable();
$table->timestamps();
});
}