- Added activity logging functionality to multiple models including CuttingMaterial, Employee, Invoice, and more. - Implemented `getActivitylogOptions` method to configure logging behavior. - Updated composer dependencies to include `spatie/laravel-activitylog`. - Created a custom Activity model extending Spatie's Activity model. - Added configuration file for activity logging settings.
86 lines
2.3 KiB
PHP
86 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Attributes\Appends;
|
|
use Illuminate\Database\Eloquent\Attributes\Guarded;
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Spatie\Activitylog\Support\LogOptions;
|
|
use Spatie\Activitylog\Models\Concerns\LogsActivity;
|
|
|
|
#[Appends(['formatted_quantity', 'formatted_retail_stock_before', 'formatted_retail_stock_after', 'formatted_stock_before', 'formatted_stock_after'])]
|
|
#[Guarded(['id'])]
|
|
class RetailStockHistory extends Model
|
|
{
|
|
use HasFactory, LogsActivity;
|
|
|
|
public $timestamps = false;
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'quantity' => 'integer',
|
|
'stock_before' => 'integer',
|
|
'retail_stock_before' => 'integer',
|
|
'stock_after' => 'integer',
|
|
'retail_stock_after' => 'integer',
|
|
];
|
|
}
|
|
|
|
public function getActivitylogOptions(): LogOptions
|
|
{
|
|
return LogOptions::defaults()
|
|
->logAll()
|
|
->logOnlyDirty()
|
|
->dontLogEmptyChanges();
|
|
}
|
|
|
|
protected function formattedQuantity(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => number_format($this->quantity, 0, ',', '.'),
|
|
);
|
|
}
|
|
|
|
protected function formattedRetailStockBefore(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => number_format($this->retail_stock_before, 0, ',', '.'),
|
|
);
|
|
}
|
|
|
|
protected function formattedRetailStockAfter(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => number_format($this->retail_stock_after, 0, ',', '.'),
|
|
);
|
|
}
|
|
|
|
protected function formattedStockBefore(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => number_format($this->stock_before, 0, ',', '.'),
|
|
);
|
|
}
|
|
|
|
protected function formattedStockAfter(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => number_format($this->stock_after, 0, ',', '.'),
|
|
);
|
|
}
|
|
|
|
public function productVariant(): BelongsTo
|
|
{
|
|
return $this->belongsTo(ProductVariant::class);
|
|
}
|
|
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
}
|