Invoice factory and listing
This commit is contained in:
parent
b3e44efd13
commit
125f82e382
|
|
@ -4,12 +4,32 @@ namespace App\Models;
|
||||||
|
|
||||||
use App\Casts\MoneyCast;
|
use App\Casts\MoneyCast;
|
||||||
use App\Enums\InvoiceStatus;
|
use App\Enums\InvoiceStatus;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
|
|
||||||
class Invoice extends Model
|
class Invoice extends Model
|
||||||
{
|
{
|
||||||
|
use HasFactory;
|
||||||
|
public static function booted(): void
|
||||||
|
{
|
||||||
|
static::creating(function (Invoice $invoice) {
|
||||||
|
$invoice->invoice_number ??= static::generateInvoiceNumber();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function generateInvoiceNumber(): string
|
||||||
|
{
|
||||||
|
$prefix = date('y').'-';
|
||||||
|
|
||||||
|
do {
|
||||||
|
$number = $prefix.str_pad(random_int(0, 99999), 5, '0', STR_PAD_LEFT);
|
||||||
|
} while (static::where('invoice_number', $number)->exists());
|
||||||
|
|
||||||
|
return $number;
|
||||||
|
}
|
||||||
|
|
||||||
protected $fillable = [
|
protected $fillable = [
|
||||||
'invoice_number',
|
'invoice_number',
|
||||||
'client_id',
|
'client_id',
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,29 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Factories;
|
||||||
|
|
||||||
|
use App\Enums\InvoiceStatus;
|
||||||
|
use App\Models\Client;
|
||||||
|
use App\Models\Invoice;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
|
use Illuminate\Support\Carbon;
|
||||||
|
|
||||||
|
class InvoiceFactory extends Factory
|
||||||
|
{
|
||||||
|
protected $model = Invoice::class;
|
||||||
|
|
||||||
|
public function definition(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'status' => InvoiceStatus::DRAFT,
|
||||||
|
'invoice_date' => Carbon::now(),
|
||||||
|
'due_date' => Carbon::now()->addDays(30),
|
||||||
|
'notes' => $this->faker->word(),
|
||||||
|
'internal_notes' => $this->faker->word(),
|
||||||
|
'created_at' => Carbon::now(),
|
||||||
|
'updated_at' => Carbon::now(),
|
||||||
|
|
||||||
|
'client_id' => Client::factory()->withContact(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -72,12 +72,12 @@ new class extends Component {
|
||||||
<flux:table.rows>
|
<flux:table.rows>
|
||||||
@foreach($this->invoices as $invoice)
|
@foreach($this->invoices as $invoice)
|
||||||
<flux:table.row :key="$invoice->id">
|
<flux:table.row :key="$invoice->id">
|
||||||
<flux:table.cell>{{ $invoice->number }}</flux:table.cell>
|
<flux:table.cell>{{ $invoice->invoice_number }}</flux:table.cell>
|
||||||
<flux:table.cell>{{ $invoice->client->abbreviation }}</flux:table.cell>
|
<flux:table.cell>{{ $invoice->client->abbreviation }}</flux:table.cell>
|
||||||
<flux:table.cell>{{ $invoice->status->value }}</flux:table.cell>
|
<flux:table.cell>{{ $invoice->status->value }}</flux:table.cell>
|
||||||
<flux:table.cell>{{ $invoice->invoice_date }}</flux:table.cell>
|
<flux:table.cell>{{ $invoice->invoice_date?->format('m/d/Y') }}</flux:table.cell>
|
||||||
<flux:table.cell>{{ $invoice->sent_at }}</flux:table.cell>
|
<flux:table.cell>{{ $invoice->sent_at?->format('m/d/Y') }}</flux:table.cell>
|
||||||
<flux:table.cell>{{ $invoice->due_date }}</flux:table.cell>
|
<flux:table.cell>{{ $invoice->due_date?->format('m/d/Y') }}</flux:table.cell>
|
||||||
<flux:table.cell>{{ formatMoney($invoice->total) }}</flux:table.cell>
|
<flux:table.cell>{{ formatMoney($invoice->total) }}</flux:table.cell>
|
||||||
|
|
||||||
</flux:table.row>
|
</flux:table.row>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue