- Introduced StockMutation model and service to handle stock changes. - Created migration for stock_mutations table. - Implemented stock mutation recording in various services (CuttingService, OrderService, PurchaseService, RestockService, RetailStockService). - Added StockHistoryController to manage stock history views. - Developed frontend components for displaying stock history and actions. - Updated routes to include stock history access with appropriate permissions. - Enhanced ProductVariant and RawMaterialPrice models to support stock mutations. - Added RowHistoryAction button for accessing stock history in product and raw material tables.
100 lines
2.5 KiB
PHP
100 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Models\Concerns\HasModuleMedia;
|
|
use App\Models\Concerns\InteractsWithActivityLog;
|
|
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 Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Spatie\MediaLibrary\HasMedia;
|
|
|
|
#[Guarded(['id'])]
|
|
#[Appends(['stock_formatted', 'reject_stock_formatted', 'retail_stock_formatted'])]
|
|
class ProductVariant extends Model implements HasMedia
|
|
{
|
|
// 1. Use Trait
|
|
use HasFactory, HasModuleMedia, InteractsWithActivityLog, SoftDeletes;
|
|
|
|
private const MIN_STOCK = 5;
|
|
|
|
// 2. Casting
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'reject_stock' => 'integer',
|
|
'stock' => 'integer',
|
|
'retail_stock' => 'integer',
|
|
];
|
|
}
|
|
|
|
// 4. Attribute
|
|
public function rejectStockFormatted(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => number_format($this->reject_stock, 0, ',', '.'),
|
|
);
|
|
}
|
|
|
|
public function stockFormatted(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => number_format($this->stock, 0, ',', '.'),
|
|
);
|
|
}
|
|
|
|
public function retailStockFormatted(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => number_format($this->retail_stock, 0, ',', '.'),
|
|
);
|
|
}
|
|
|
|
// 5. Other Methods
|
|
public static function mediaModuleName(): string
|
|
{
|
|
return 'product';
|
|
}
|
|
|
|
public static function minStock(): int
|
|
{
|
|
return self::MIN_STOCK;
|
|
}
|
|
|
|
public function registerMediaCollections(): void
|
|
{
|
|
$this->addMediaCollection('images');
|
|
}
|
|
|
|
// 6. Relation
|
|
public function cuttingResults(): HasMany
|
|
{
|
|
return $this->hasMany(CuttingResult::class);
|
|
}
|
|
|
|
public function orderItems(): HasMany
|
|
{
|
|
return $this->hasMany(OrderItem::class);
|
|
}
|
|
|
|
public function prices(): HasMany
|
|
{
|
|
return $this->hasMany(ProductPrice::class, 'variant_id');
|
|
}
|
|
|
|
public function product(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Product::class)->withTrashed();
|
|
}
|
|
|
|
public function stockMutations(): \Illuminate\Database\Eloquent\Relations\MorphMany
|
|
{
|
|
return $this->morphMany(StockMutation::class, 'stockable');
|
|
}
|
|
}
|