170 lines
4.2 KiB
PHP
170 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\User;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
class DatevExport extends Model
|
|
{
|
|
use SoftDeletes;
|
|
|
|
protected $table = 'datev_exports';
|
|
|
|
// Status-Konstanten
|
|
const STATUS_DRAFT = 0;
|
|
|
|
const STATUS_GENERATED = 1;
|
|
|
|
const STATUS_DOWNLOADED = 2;
|
|
|
|
const STATUS_LOCKED = 3;
|
|
|
|
const STATUS_LABELS = [
|
|
self::STATUS_DRAFT => 'Entwurf',
|
|
self::STATUS_GENERATED => 'Generiert',
|
|
self::STATUS_DOWNLOADED => 'Heruntergeladen',
|
|
self::STATUS_LOCKED => 'Gesperrt',
|
|
];
|
|
|
|
const STATUS_BADGES = [
|
|
self::STATUS_DRAFT => 'secondary',
|
|
self::STATUS_GENERATED => 'info',
|
|
self::STATUS_DOWNLOADED => 'success',
|
|
self::STATUS_LOCKED => 'dark',
|
|
];
|
|
|
|
protected $fillable = [
|
|
'period_from',
|
|
'period_to',
|
|
'month',
|
|
'year',
|
|
'status',
|
|
'berater_nr',
|
|
'mandant_nr',
|
|
'invoice_count',
|
|
'credit_count',
|
|
'cancellation_count',
|
|
'total_revenue',
|
|
'total_commissions',
|
|
'filename',
|
|
'file_path',
|
|
'file_hash',
|
|
'created_by',
|
|
'warning_count',
|
|
'error_count',
|
|
'validation_summary',
|
|
];
|
|
|
|
protected $casts = [
|
|
'period_from' => 'date',
|
|
'period_to' => 'date',
|
|
'total_revenue' => 'decimal:2',
|
|
'total_commissions' => 'decimal:2',
|
|
'validation_summary' => 'array',
|
|
];
|
|
|
|
/*
|
|
|--------------------------------------------------------------------------
|
|
| Relationships
|
|
|--------------------------------------------------------------------------
|
|
*/
|
|
|
|
public function lines()
|
|
{
|
|
return $this->hasMany(DatevExportLine::class, 'datev_export_id');
|
|
}
|
|
|
|
public function creator()
|
|
{
|
|
return $this->belongsTo(User::class, 'created_by');
|
|
}
|
|
|
|
/*
|
|
|--------------------------------------------------------------------------
|
|
| Scopes
|
|
|--------------------------------------------------------------------------
|
|
*/
|
|
|
|
public function scopeForPeriod($query, int $month, int $year)
|
|
{
|
|
return $query->where('month', $month)->where('year', $year);
|
|
}
|
|
|
|
public function scopeGenerated($query)
|
|
{
|
|
return $query->where('status', '>=', self::STATUS_GENERATED);
|
|
}
|
|
|
|
public function scopeLocked($query)
|
|
{
|
|
return $query->where('status', self::STATUS_LOCKED);
|
|
}
|
|
|
|
/*
|
|
|--------------------------------------------------------------------------
|
|
| Accessors
|
|
|--------------------------------------------------------------------------
|
|
*/
|
|
|
|
public function getStatusLabelAttribute(): string
|
|
{
|
|
return self::STATUS_LABELS[$this->status] ?? '-';
|
|
}
|
|
|
|
public function getStatusBadgeAttribute(): string
|
|
{
|
|
$badge = self::STATUS_BADGES[$this->status] ?? 'secondary';
|
|
|
|
return '<span class="badge badge-'.$badge.'">'.$this->status_label.'</span>';
|
|
}
|
|
|
|
public function getPeriodLabelAttribute(): string
|
|
{
|
|
return str_pad($this->month, 2, '0', STR_PAD_LEFT).'/'.$this->year;
|
|
}
|
|
|
|
public function getTotalLinesAttribute(): int
|
|
{
|
|
return $this->invoice_count + $this->credit_count + $this->cancellation_count;
|
|
}
|
|
|
|
/*
|
|
|--------------------------------------------------------------------------
|
|
| Methods
|
|
|--------------------------------------------------------------------------
|
|
*/
|
|
|
|
public function isLocked(): bool
|
|
{
|
|
return $this->status === self::STATUS_LOCKED;
|
|
}
|
|
|
|
public function isGenerated(): bool
|
|
{
|
|
return $this->status >= self::STATUS_GENERATED;
|
|
}
|
|
|
|
public function markAsDownloaded(): void
|
|
{
|
|
if ($this->status === self::STATUS_GENERATED) {
|
|
$this->update(['status' => self::STATUS_DOWNLOADED]);
|
|
}
|
|
}
|
|
|
|
public function lock(): void
|
|
{
|
|
$this->update(['status' => self::STATUS_LOCKED]);
|
|
}
|
|
|
|
public function getStoragePath(): string
|
|
{
|
|
return config('datev.storage_path').'/'.$this->year.'/'.str_pad($this->month, 2, '0', STR_PAD_LEFT);
|
|
}
|
|
|
|
public function getFullFilePath(): string
|
|
{
|
|
return $this->getStoragePath().'/'.$this->filename;
|
|
}
|
|
}
|