142 lines
3.2 KiB
PHP
142 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
class Invoice extends Model
|
|
{
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $fillable = [
|
|
'user_id',
|
|
'customer_id',
|
|
'address_id',
|
|
'delivery_id',
|
|
'invoice_number',
|
|
'type',
|
|
'status',
|
|
'sum',
|
|
'tax',
|
|
'project_id',
|
|
'project_count',
|
|
'is_template',
|
|
'currency_code',
|
|
'due_date',
|
|
'cash_discount',
|
|
'cash_discount_date',
|
|
];
|
|
|
|
/**
|
|
* The attributes that are appended with attribute getters.
|
|
*
|
|
* @var string[]
|
|
*/
|
|
protected $appends = [
|
|
'created',
|
|
'number',
|
|
'localized_state'
|
|
];
|
|
|
|
/**
|
|
* Set the project_count variable automatically, if a project is related to this invoice.
|
|
*/
|
|
public static function boot(): void
|
|
{
|
|
parent::boot();
|
|
self::creating(function (Invoice $invoice) {
|
|
if (is_null($invoice->project_id)) {
|
|
return true;
|
|
}
|
|
if ($invoice->type != '326') {
|
|
return true;
|
|
}
|
|
$projectMax = Invoice::where('project_id', '=', $invoice->project_id)->max('project_count') + 1;
|
|
$invoice->project_count = $projectMax;
|
|
return true;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Get the invoice state as translated string.
|
|
*/
|
|
public function getLocalizedStateAttribute(): string
|
|
{
|
|
return __('invoice.state_' . $this->status);
|
|
}
|
|
|
|
/**
|
|
* Get the invoice number formatted
|
|
*/
|
|
public function getNumberAttribute(): string
|
|
{
|
|
return $this->created_at->format('Y') . '-' . str_pad($this->invoice_number, 5, '0', STR_PAD_LEFT);
|
|
}
|
|
|
|
/**
|
|
* Get the created_at attribute in local time format.
|
|
*/
|
|
public function getCreatedAttribute(): string
|
|
{
|
|
return $this->created_at->format('d.m.Y');
|
|
}
|
|
|
|
/**
|
|
* Get the user, that has created the invoice.
|
|
*/
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
/**
|
|
* Get the invoice's customer.
|
|
*/
|
|
public function customer(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Customer::class)->withTrashed();
|
|
}
|
|
|
|
/**
|
|
* Get the invoice's address.
|
|
*/
|
|
public function address(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Address::class)->withTrashed();
|
|
}
|
|
|
|
/**
|
|
* Get the invoice's delivery address.
|
|
*/
|
|
public function delivery(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Address::class)->withTrashed();
|
|
}
|
|
|
|
/**
|
|
* Get the items (invoice positions) of this invoice.
|
|
*/
|
|
public function items(): HasMany
|
|
{
|
|
return $this->hasMany(Invoiceitem::class);
|
|
}
|
|
|
|
/**
|
|
* Get the payments for the invoice
|
|
*/
|
|
public function payments(): HasMany
|
|
{
|
|
return $this->hasMany(Payment::class);
|
|
}
|
|
|
|
public function project(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Project::class);
|
|
}
|
|
}
|