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

187 lines
5.3 KiB
PHP

<?php
namespace App\Models;
use App\Services\Util;
use App\User;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
class UserAboItemHistory extends Model
{
protected $table = 'user_abo_item_histories';
const ACTION_INITIAL = 'initial';
const ACTION_ADDED = 'added';
const ACTION_REMOVED = 'removed';
const ACTION_QTY_CHANGED = 'qty_changed';
const ACTION_COMP_CHANGED = 'comp_product_changed';
const ACTION_COMP_ADDED = 'comp_added';
const ACTION_COMP_REMOVED = 'comp_removed';
const ACTION_ROLLBACK = 'rollback';
const CHANNEL_ADMIN = 'admin';
const CHANNEL_USER_ME = 'user_me';
const CHANNEL_USER_OT = 'user_ot';
const CHANNEL_PORTAL = 'portal';
const CHANNEL_SYSTEM = 'system';
public static $actionLabels = [
'initial' => 'abo_history.action_initial',
'added' => 'abo_history.action_added',
'removed' => 'abo_history.action_removed',
'qty_changed' => 'abo_history.action_qty_changed',
'comp_product_changed' => 'abo_history.action_comp_changed',
'comp_added' => 'abo_history.action_comp_added',
'comp_removed' => 'abo_history.action_comp_removed',
'rollback' => 'abo_history.action_rollback',
];
public static $actionColors = [
'initial' => 'info',
'added' => 'success',
'removed' => 'danger',
'qty_changed' => 'warning',
'comp_product_changed' => 'primary',
'comp_added' => 'success',
'comp_removed' => 'danger',
'rollback' => 'dark',
];
public static $channelLabels = [
'admin' => 'abo_history.channel_admin',
'user_me' => 'abo_history.channel_user_me',
'user_ot' => 'abo_history.channel_user_ot',
'portal' => 'abo_history.channel_portal',
'system' => 'abo_history.channel_system',
];
public static $channelColors = [
'admin' => 'danger',
'user_me' => 'warning',
'user_ot' => 'info',
'portal' => 'secondary',
'system' => 'default',
];
protected $casts = [
'user_abo_id' => 'int',
'user_abo_item_id' => 'int',
'product_id' => 'int',
'unit_price' => 'float',
'total_price' => 'float',
'qty_before' => 'int',
'qty_after' => 'int',
'old_product_id' => 'int',
'comp' => 'int',
'changed_by_user_id' => 'int',
'is_initial' => 'bool',
];
protected $fillable = [
'user_abo_id',
'user_abo_item_id',
'product_id',
'action',
'product_name',
'product_number',
'unit_price',
'total_price',
'qty_before',
'qty_after',
'old_product_id',
'old_product_name',
'comp',
'changed_by_user_id',
'changed_by_name',
'channel',
'batch_id',
'is_initial',
];
public function user_abo()
{
return $this->belongsTo(UserAbo::class);
}
public function product()
{
return $this->belongsTo(Product::class);
}
public function old_product()
{
return $this->belongsTo(Product::class, 'old_product_id');
}
public function changed_by()
{
return $this->belongsTo(User::class, 'changed_by_user_id');
}
public function getActionBadge()
{
$label = isset(self::$actionLabels[$this->action]) ? __(self::$actionLabels[$this->action]) : $this->action;
$color = self::$actionColors[$this->action] ?? 'secondary';
return '<span class="badge badge-'.$color.'">'.$label.'</span>';
}
public function getChannelBadge()
{
$label = isset(self::$channelLabels[$this->channel]) ? __(self::$channelLabels[$this->channel]) : $this->channel;
$color = self::$channelColors[$this->channel] ?? 'secondary';
return '<span class="badge badge-outline-'.$color.'">'.$label.'</span>';
}
public function getChangeDescription()
{
switch ($this->action) {
case self::ACTION_INITIAL:
return __('abo_history.desc_initial', ['qty' => $this->qty_after]);
case self::ACTION_ADDED:
return __('abo_history.desc_added', ['qty' => $this->qty_after]);
case self::ACTION_REMOVED:
return __('abo_history.desc_removed');
case self::ACTION_QTY_CHANGED:
return __('abo_history.desc_qty_changed', ['from' => $this->qty_before, 'to' => $this->qty_after]);
case self::ACTION_COMP_CHANGED:
return __('abo_history.desc_comp_changed', ['old' => $this->old_product_name]);
case self::ACTION_COMP_ADDED:
return __('abo_history.desc_comp_added');
case self::ACTION_COMP_REMOVED:
return __('abo_history.desc_comp_removed');
case self::ACTION_ROLLBACK:
return __('abo_history.desc_rollback');
default:
return '';
}
}
public function getFormattedUnitPrice()
{
return Util::formatNumber($this->unit_price);
}
public function getFormattedTotalPrice()
{
return Util::formatNumber($this->total_price);
}
public function getFormattedDate()
{
return Carbon::parse($this->created_at)->format('d.m.Y H:i');
}
}