mivita/app/Models/DatevExportLine.php
2026-02-20 17:55:06 +01:00

100 lines
2.5 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class DatevExportLine extends Model
{
protected $table = 'datev_export_lines';
// Source Types
const SOURCE_INVOICE = 'invoice';
const SOURCE_CREDIT = 'credit';
const SOURCE_CANCELLATION = 'cancellation';
protected $fillable = [
'datev_export_id',
'source_type',
'source_id',
'line_number',
'amount_gross',
'soll_haben',
'konto',
'gegenkonto',
'bu_schluessel',
'belegdatum',
'belegfeld1',
'buchungstext',
'eu_ustid',
'eu_land',
'row_csv',
];
protected $casts = [
'amount_gross' => 'decimal:2',
'belegdatum' => 'date',
];
/*
|--------------------------------------------------------------------------
| Relationships
|--------------------------------------------------------------------------
*/
public function datev_export()
{
return $this->belongsTo(DatevExport::class, 'datev_export_id');
}
/*
|--------------------------------------------------------------------------
| Scopes
|--------------------------------------------------------------------------
*/
public function scopeInvoices($query)
{
return $query->where('source_type', self::SOURCE_INVOICE);
}
public function scopeCredits($query)
{
return $query->where('source_type', self::SOURCE_CREDIT);
}
public function scopeCancellations($query)
{
return $query->where('source_type', self::SOURCE_CANCELLATION);
}
/*
|--------------------------------------------------------------------------
| Accessors
|--------------------------------------------------------------------------
*/
public function getSourceTypeLabelAttribute(): string
{
return match ($this->source_type) {
self::SOURCE_INVOICE => 'Rechnung',
self::SOURCE_CREDIT => 'Gutschrift',
self::SOURCE_CANCELLATION => 'Storno',
default => $this->source_type,
};
}
public function getFormattedAmountAttribute(): string
{
$prefix = $this->soll_haben === 'S' ? '-' : '';
return $prefix.number_format(abs($this->amount_gross), 2, ',', '.').' €';
}
public function getFormattedBelegdatumAttribute(): string
{
return $this->belegdatum ? $this->belegdatum->format('d.m.Y') : '-';
}
}