CluPilotCloud/app/Models/ExportTarget.php

63 lines
1.7 KiB
PHP

<?php
namespace App\Models;
use App\Models\Concerns\HasUuid;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
/**
* One place a copy of every invoice is written to.
*
* As many as somebody wants, because the reason for having a second is that
* the first can fail — an office NAS is unreachable while the office router
* reboots, and an off-site box is unreachable while somebody else's data centre
* has a bad morning.
*/
class ExportTarget extends Model
{
use HasUuid;
public const LOCAL = 'local';
public const SFTP = 'sftp';
/** One folder per year — an archive somebody keeps. */
public const BY_YEAR = 'year';
/** One folder per day — a handover point somebody collects from. */
public const BY_DAY = 'day';
protected $fillable = [
'name', 'driver', 'path', 'layout', 'keep_days', 'host', 'port', 'username', 'secret_key', 'active',
];
protected function casts(): array
{
return ['active' => 'boolean', 'port' => 'integer', 'keep_days' => 'integer'];
}
public function exports(): HasMany
{
return $this->hasMany(InvoiceExport::class);
}
/** The folder an invoice issued on this date belongs in. */
public function folderFor(?\Illuminate\Support\Carbon $issuedOn): string
{
$date = $issuedOn ?? now();
return $this->layout === self::BY_DAY
? $date->format('Y-m-d')
: $date->format('Y');
}
/** Human-readable, for a list and for an error message. */
public function describe(): string
{
return $this->driver === self::SFTP
? $this->username.'@'.$this->host.':'.$this->path
: $this->path;
}
}