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

350 lines
11 KiB
PHP

<?php
/**
* Created by Reliese Model.
*/
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* Class UserInvoice
*
* @property int $id
* @property int $shopping_order_id
* @property int|null $month
* @property int|null $year
* @property Carbon|null $date
* @property string|null $full_number
* @property int|null $number
* @property string|null $file
* @property string|null $infos
* @property bool $paid
* @property Carbon|null $paid_date
* @property bool $cancellation
* @property int|null $cancellation_id
* @property Carbon|null $cancellation_date
* @property int $status
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
* @property string|null $deleted_at
* @property ShoppingOrder $shopping_order
* @property string|null $filename
* @property string|null $dir
* @property string|null $delivery_filename
* @property string|null $delivery_dir
* @property string|null $disk
*
* @method static \Illuminate\Database\Eloquent\Builder|UserInvoice newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|UserInvoice newQuery()
* @method static \Illuminate\Database\Query\Builder|UserInvoice onlyTrashed()
* @method static \Illuminate\Database\Eloquent\Builder|UserInvoice query()
* @method static \Illuminate\Database\Eloquent\Builder|UserInvoice whereCancellation($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserInvoice whereCancellationDate($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserInvoice whereCancellationId($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserInvoice whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserInvoice whereDate($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserInvoice whereDeletedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserInvoice whereDeliveryDir($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserInvoice whereDeliveryFilename($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserInvoice whereDir($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserInvoice whereDisk($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserInvoice whereFilename($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserInvoice whereFullNumber($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserInvoice whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserInvoice whereInfos($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserInvoice whereMonth($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserInvoice whereNumber($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserInvoice wherePaid($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserInvoice wherePaidDate($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserInvoice whereShoppingOrderId($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserInvoice whereStatus($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserInvoice whereUpdatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserInvoice whereYear($value)
* @method static \Illuminate\Database\Query\Builder|UserInvoice withTrashed()
* @method static \Illuminate\Database\Query\Builder|UserInvoice withoutTrashed()
*
* @mixin \Eloquent
*/
class UserInvoice extends Model
{
use SoftDeletes;
protected $table = 'user_invoices';
protected $casts = [
'shopping_order_id' => 'int',
'month' => 'int',
'year' => 'int',
'number' => 'int',
'paid' => 'bool',
'cancellation' => 'bool',
'cancellation_id' => 'int',
'status' => 'int',
'infos' => 'array',
];
protected $dates = [
'date',
'paid_date',
'cancellation_date',
];
protected $fillable = [
'shopping_order_id',
'month',
'year',
'date',
'full_number',
'number',
'filename',
'dir',
'delivery_filename',
'delivery_dir',
'disk',
'infos',
'paid',
'paid_date',
'cancellation',
'cancellation_id',
'cancellation_date',
'status',
];
public static $monthNames = [
1 => 'Januar',
2 => 'Februar',
3 => 'März',
4 => 'April',
5 => 'Mai',
6 => 'Juni',
7 => 'Juli',
8 => 'August',
9 => 'September',
10 => 'Oktober',
11 => 'November',
12 => 'Dezember',
];
public static $statusTypes = [
0 => '-',
1 => 'Bestellung',
2 => 'Shop',
11 => 'storniert B.',
12 => 'storniert Shop',
];
public static $statusColors = [
0 => 'warning',
1 => 'success',
2 => 'secondary',
11 => 'danger',
12 => 'danger',
];
public function shopping_order()
{
return $this->belongsTo(ShoppingOrder::class);
}
public function getDateAttribute($value)
{
return $this->attributes['date'] ? Carbon::parse($this->attributes['date'])->format(\Util::formatDateDB()) : '';
}
public function setDateAttribute($value)
{
$this->attributes['date'] = isset($value) ? (new Carbon($value))->format('Y-m-d') : null;
}
public function getDateRaw()
{
return isset($this->attributes['date']) ? $this->attributes['date'] : null;
}
public function getPaidDateAttribute($value)
{
return $this->attributes['paid_date'] ? Carbon::parse($this->attributes['paid_date'])->format(\Util::formatDateDB()) : '';
}
public function setPaidDateAttribute($value)
{
$this->attributes['paid_date'] = isset($value) ? (new Carbon($value))->format('Y-m-d') : null;
}
public function getPaidDateRaw()
{
return isset($this->attributes['paid_date']) ? $this->attributes['paid_date'] : null;
}
public function getCancellationDateAttribute($value)
{
return $this->attributes['cancellation_date'] ? Carbon::parse($this->attributes['cancellation_date'])->format(\Util::formatDateDB()) : '';
}
public function setCancellationDateAttribute($value)
{
$this->attributes['cancellation_date'] = isset($value) ? (new Carbon($value))->format('Y-m-d') : null;
}
public function getCancellationDateRaw()
{
return isset($this->attributes['cancellation_date']) ? $this->attributes['cancellation_date'] : null;
}
public static function getMonthName($month)
{
return isset(self::$monthNames[$month]) ? self::$monthNames[$month] : $month;
}
public function getStatusType()
{
return isset(self::$statusTypes[$this->status]) ? self::$statusTypes[$this->status] : '';
}
public function getStatusColor()
{
return isset(self::$statusColors[$this->status]) ? self::$statusColors[$this->status] : 'default';
}
public function getDownloadPath($full = false)
{
if (! $full) {
return $this->dir.$this->filename;
}
return \Storage::disk($this->disk)->path($this->dir.$this->filename);
}
public function getDownloadPathDelivery($full = false)
{
if (! $full) {
return $this->delivery_dir.$this->delivery_filename;
}
return \Storage::disk($this->disk)->path($this->delivery_dir.$this->delivery_filename);
}
/**
* Gibt den Download-Pfad für die lokalisierte Rechnung zurück.
* Bei 'de' oder nicht vorhandener Locale-Version wird das Original zurückgegeben.
*
* @param string|null $locale Sprachcode (de, en, es)
* @param bool $full Vollständiger Dateisystempfad oder relativer Pfad
* @return string
*/
public function getDownloadPathLocale($locale = null, $full = false)
{
// Bei Deutsch oder keiner Angabe: Original zurückgeben
if (! $locale || $locale === 'de') {
return $this->getDownloadPath($full);
}
// Dateiname mit Locale-Suffix
$filename = str_replace('.pdf', '-'.$locale.'.pdf', $this->filename);
$path = $this->dir.$filename;
// Prüfen ob Locale-Version existiert, sonst Fallback auf DE
if (! \Storage::disk($this->disk)->exists($path)) {
return $this->getDownloadPath($full);
}
return $full ? \Storage::disk($this->disk)->path($path) : $path;
}
/**
* Gibt den Download-Pfad für den lokalisierten Lieferschein zurück.
* Bei 'de' oder nicht vorhandener Locale-Version wird das Original zurückgegeben.
*
* @param string|null $locale Sprachcode (de, en, es)
* @param bool $full Vollständiger Dateisystempfad oder relativer Pfad
* @return string
*/
public function getDownloadPathDeliveryLocale($locale = null, $full = false)
{
// Bei Deutsch oder keiner Angabe: Original zurückgeben
if (! $locale || $locale === 'de') {
return $this->getDownloadPathDelivery($full);
}
// Dateiname mit Locale-Suffix
$filename = str_replace('.pdf', '-'.$locale.'.pdf', $this->delivery_filename);
$path = $this->delivery_dir.$filename;
// Prüfen ob Locale-Version existiert, sonst Fallback auf DE
if (! \Storage::disk($this->disk)->exists($path)) {
return $this->getDownloadPathDelivery($full);
}
return $full ? \Storage::disk($this->disk)->path($path) : $path;
}
/**
* Gibt den lokalisierten Dateinamen für die Rechnung zurück.
*
* @param string|null $locale
* @return string
*/
public function getFilenameLocale($locale = null)
{
if (! $locale || $locale === 'de') {
return $this->filename;
}
$filename = str_replace('.pdf', '-'.$locale.'.pdf', $this->filename);
$path = $this->dir.$filename;
// Fallback auf Original wenn nicht vorhanden
if (! \Storage::disk($this->disk)->exists($path)) {
return $this->filename;
}
return $filename;
}
/**
* Gibt alle verfügbaren lokalisierten Versionen der Rechnung zurück (außer DE).
*
* @return array Array mit Sprachcodes, z.B. ['en', 'es']
*/
public function getAvailableLocales(): array
{
$availableTemplates = config('localization.availableTemplates', ['de']);
$locales = [];
foreach ($availableTemplates as $locale) {
if ($locale === 'de') {
continue;
}
$filename = str_replace('.pdf', '-'.$locale.'.pdf', $this->filename);
$path = $this->dir.$filename;
if (\Storage::disk($this->disk)->exists($path)) {
$locales[] = $locale;
}
}
return $locales;
}
/**
* Prüft ob eine lokalisierte Version für die angegebene Sprache existiert.
*/
public function hasLocale(string $locale): bool
{
if ($locale === 'de') {
return true;
}
$filename = str_replace('.pdf', '-'.$locale.'.pdf', $this->filename);
$path = $this->dir.$filename;
return \Storage::disk($this->disk)->exists($path);
}
}