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

320 lines
9.9 KiB
PHP

<?php
/**
* Created by Reliese Model.
*/
namespace App\Models;
use App\User;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* Class UserCredit
*
* @property int $id
* @property int $user_id
* @property int|null $month
* @property int|null $year
* @property Carbon|null $date
* @property string|null $full_number
* @property int|null $number
* @property float|null $net
* @property float|null $tax_rate
* @property float|null $tax
* @property float|null $total
* @property string|null $filename
* @property string|null $dir
* @property string|null $disk
* @property array|null $infos
* @property bool $paid_out
* @property Carbon|null $paid_out_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 User $user
* @property Collection|UserCreditItem[] $user_credit_items
* @property bool $taxable
* @property-read int|null $user_credit_items_count
*
* @method static \Illuminate\Database\Eloquent\Builder|UserCredit newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|UserCredit newQuery()
* @method static \Illuminate\Database\Query\Builder|UserCredit onlyTrashed()
* @method static \Illuminate\Database\Eloquent\Builder|UserCredit query()
* @method static \Illuminate\Database\Eloquent\Builder|UserCredit whereCancellation($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserCredit whereCancellationDate($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserCredit whereCancellationId($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserCredit whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserCredit whereDate($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserCredit whereDeletedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserCredit whereDir($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserCredit whereDisk($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserCredit whereFilename($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserCredit whereFullNumber($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserCredit whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserCredit whereInfos($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserCredit whereMonth($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserCredit whereNet($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserCredit whereNumber($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserCredit wherePaidOut($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserCredit wherePaidOutDate($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserCredit whereStatus($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserCredit whereTax($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserCredit whereTaxRate($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserCredit whereTaxable($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserCredit whereTotal($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserCredit whereUpdatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserCredit whereUserId($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserCredit whereYear($value)
* @method static \Illuminate\Database\Query\Builder|UserCredit withTrashed()
* @method static \Illuminate\Database\Query\Builder|UserCredit withoutTrashed()
*
* @mixin \Eloquent
*/
class UserCredit extends Model
{
use SoftDeletes;
protected $table = 'user_credits';
protected $casts = [
'user_id' => 'int',
'month' => 'int',
'year' => 'int',
'number' => 'int',
'net' => 'float',
'tax_rate' => 'float',
'tax' => 'float',
'taxable' => 'int',
'total' => 'float',
'paid_out' => 'bool',
'cancellation' => 'bool',
'cancellation_id' => 'int',
'status' => 'int',
'infos' => 'array',
];
protected $dates = [
'date',
'paid_out_date',
'cancellation_date',
];
protected $fillable = [
'user_id',
'month',
'year',
'date',
'full_number',
'number',
'net',
'tax_rate',
'tax',
'total',
'taxable',
'filename',
'dir',
'disk',
'infos',
'paid_out',
'paid_out_date',
'cancellation',
'cancellation_id',
'cancellation_date',
'status',
];
public static $statusTypes = [
0 => 'open',
1 => 'paid',
2 => 'check',
10 => 'cancelled',
];
public static $statusColors = [
0 => 'warning',
1 => 'success',
2 => 'secondary',
10 => 'danger',
];
public static $taxableTypes = [
0 => '',
1 => 'umsatzsteuerpflichtig / DE', // payment.to_sales_tax_de
2 => 'nicht umsatzsteuerpflichtig / DE', // payment.not_to_sales_tax_de
3 => 'nicht umsatzsteuerpflichtig / Ausland', // payment.not_to_sales_tax_foreign
];
public function user()
{
return $this->belongsTo(User::class);
}
public function user_credit_items()
{
return $this->hasMany(UserCreditItem::class);
}
public function isCredit()
{
return $this->filename ? true : false;
}
public function getDateAttribute($value)
{
return $value ? Carbon::parse($value)->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 getFormattedTax()
{
return formatNumber($this->attributes['tax']);
}
public function getFormattedNet()
{
return formatNumber($this->attributes['net']);
}
public function getFormattedTotal()
{
return formatNumber($this->attributes['total']);
}
public function getStatusType()
{
// trans('payment.cancelled')
return isset(self::$statusTypes[$this->status]) ? __('payment.'.self::$statusTypes[$this->status]) : '';
}
public function getStatusColor()
{
return isset(self::$statusColors[$this->status]) ? self::$statusColors[$this->status] : 'default';
}
public static function getTransStatusType()
{
$ret = [];
foreach (self::$statusTypes as $key => $val) {
$ret[$key] = trans('payment.'.$val);
}
return $ret;
}
public function getDownloadPath($full = false)
{
if (! $full) {
return $this->dir.$this->filename;
}
return \Storage::disk($this->disk)->path($this->dir.$this->filename);
}
/**
* Gibt den Download-Pfad für die lokalisierte Gutschrift 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 lokalisierten Dateinamen für die Gutschrift 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 Gutschrift 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);
}
}