2025-12-30 15:03:19 +08:00
|
|
|
<?php
|
|
|
|
|
|
2026-01-26 10:37:47 +08:00
|
|
|
namespace App\Modules\Inventory\Models;
|
2025-12-30 15:03:19 +08:00
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
2026-01-16 17:36:37 +08:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
|
use Spatie\Activitylog\Traits\LogsActivity;
|
|
|
|
|
use Spatie\Activitylog\LogOptions;
|
2026-01-26 10:37:47 +08:00
|
|
|
use App\Modules\Procurement\Models\Vendor; // Cross-module dependency (Procurement)
|
2025-12-30 15:03:19 +08:00
|
|
|
|
|
|
|
|
class Product extends Model
|
|
|
|
|
{
|
2026-01-16 17:36:37 +08:00
|
|
|
use HasFactory, LogsActivity, SoftDeletes;
|
2025-12-30 15:03:19 +08:00
|
|
|
|
|
|
|
|
protected $fillable = [
|
|
|
|
|
'code',
|
|
|
|
|
'name',
|
|
|
|
|
'category_id',
|
|
|
|
|
'brand',
|
|
|
|
|
'specification',
|
2026-01-08 11:52:25 +08:00
|
|
|
'base_unit_id',
|
|
|
|
|
'large_unit_id',
|
2025-12-30 15:03:19 +08:00
|
|
|
'conversion_rate',
|
2026-01-08 11:52:25 +08:00
|
|
|
'purchase_unit_id',
|
2025-12-30 15:03:19 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
protected $casts = [
|
|
|
|
|
'conversion_rate' => 'decimal:4',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the category that owns the product.
|
|
|
|
|
*/
|
|
|
|
|
public function category(): BelongsTo
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(Category::class);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-08 11:52:25 +08:00
|
|
|
public function baseUnit(): BelongsTo
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(Unit::class, 'base_unit_id');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function largeUnit(): BelongsTo
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(Unit::class, 'large_unit_id');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function purchaseUnit(): BelongsTo
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(Unit::class, 'purchase_unit_id');
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-30 15:03:19 +08:00
|
|
|
public function vendors(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsToMany(Vendor::class)->withPivot('last_price')->withTimestamps();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function inventories(): \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(Inventory::class);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-16 17:36:37 +08:00
|
|
|
public function transactions(): HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(InventoryTransaction::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getActivitylogOptions(): LogOptions
|
|
|
|
|
{
|
|
|
|
|
return LogOptions::defaults()
|
|
|
|
|
->logAll()
|
|
|
|
|
->logOnlyDirty()
|
|
|
|
|
->dontSubmitEmptyLogs();
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-19 11:47:10 +08:00
|
|
|
public function tapActivity(\Spatie\Activitylog\Contracts\Activity $activity, string $eventName)
|
|
|
|
|
{
|
|
|
|
|
$properties = $activity->properties;
|
|
|
|
|
$attributes = $properties['attributes'] ?? [];
|
2026-01-19 15:32:41 +08:00
|
|
|
$snapshot = $properties['snapshot'] ?? [];
|
2026-01-19 11:47:10 +08:00
|
|
|
|
|
|
|
|
// Handle Category Name Snapshot
|
|
|
|
|
if (isset($attributes['category_id'])) {
|
|
|
|
|
$category = Category::find($attributes['category_id']);
|
2026-01-19 15:32:41 +08:00
|
|
|
$snapshot['category_name'] = $category ? $category->name : null;
|
2026-01-19 11:47:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handle Unit Name Snapshots
|
|
|
|
|
$unitFields = ['base_unit_id', 'large_unit_id', 'purchase_unit_id'];
|
|
|
|
|
foreach ($unitFields as $field) {
|
|
|
|
|
if (isset($attributes[$field])) {
|
|
|
|
|
$unit = Unit::find($attributes[$field]);
|
|
|
|
|
$nameKey = str_replace('_id', '_name', $field);
|
2026-01-19 15:32:41 +08:00
|
|
|
$snapshot[$nameKey] = $unit ? $unit->name : null;
|
2026-01-19 11:47:10 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Always snapshot self name for context (so logs always show "Cola")
|
2026-01-19 15:32:41 +08:00
|
|
|
$snapshot['name'] = $this->name;
|
2026-01-19 11:47:10 +08:00
|
|
|
|
|
|
|
|
$properties['attributes'] = $attributes;
|
2026-01-19 15:32:41 +08:00
|
|
|
$properties['snapshot'] = $snapshot;
|
2026-01-19 11:47:10 +08:00
|
|
|
$activity->properties = $properties;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-30 15:03:19 +08:00
|
|
|
public function warehouses(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsToMany(Warehouse::class, 'inventories')
|
|
|
|
|
->withPivot(['quantity', 'safety_stock', 'location'])
|
|
|
|
|
->withTimestamps();
|
|
|
|
|
}
|
|
|
|
|
}
|