60 lines
1.6 KiB
PHP
60 lines
1.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class InvoiceSettings extends Model
|
|
{
|
|
protected $table = 'invoice_settings';
|
|
|
|
protected $fillable = [
|
|
'invoice_prefix',
|
|
'invoice_next_number',
|
|
'header_name',
|
|
'header_title',
|
|
'header_logo',
|
|
'accent_color',
|
|
'default_tax_rate',
|
|
'default_currency',
|
|
'default_service_description',
|
|
'default_bank_name',
|
|
'default_branch_name',
|
|
'default_bank_account',
|
|
'default_bank_holder',
|
|
'default_notes',
|
|
'signature_image',
|
|
'signer_name',
|
|
'signer_title',
|
|
];
|
|
|
|
protected $casts = [
|
|
'invoice_next_number' => 'integer',
|
|
'default_tax_rate' => 'decimal:2',
|
|
];
|
|
|
|
/** Always return the single settings row, creating it if absent. */
|
|
public static function instance(): self
|
|
{
|
|
return static::firstOrCreate([], [
|
|
'invoice_prefix' => 'INV',
|
|
'invoice_next_number' => 1,
|
|
'accent_color' => '#FFB7C5',
|
|
'default_tax_rate' => 11,
|
|
'default_currency' => 'IDR',
|
|
]);
|
|
}
|
|
|
|
/** Generate the next invoice number and increment the counter. */
|
|
public static function generateNumber(): string
|
|
{
|
|
$settings = static::instance();
|
|
$prefix = rtrim($settings->invoice_prefix, '-');
|
|
$number = str_pad((string) $settings->invoice_next_number, 3, '0', STR_PAD_LEFT);
|
|
$settings->increment('invoice_next_number');
|
|
return $prefix . '-' . $number;
|
|
}
|
|
}
|