ifnex/04_Laravel/app/Models/ShipmentChecklist.php
Kazem Alghasi b9b83ffecf feat(shipment): implement checklist templating and automated instantiation
Introduce a template-based system for shipment checklists to automate
the creation of required inspection items upon shipment approval.

- Add `ShipmentChecklistTemplate` model and migration to define reusable
  checklist structures.
- Extend `ShipmentChecklist` schema to support item titles, requirement
  flags, sorting, and attachment paths.
- Implement `instantiateChecklist` in `ShipmentReviewService` to
  automatically generate checklist items from active templates based on
  shipment direction (import/export).
- Refactor `ShipmentChecklistResource` and `ChecklistsRelationManager`
  in Filament to support the new data structure and improved UI.
- Update `ShipmentChecklist` model with helper methods for completion
  tracking and improved relationship handling.
2026-09-30 18:35:25 +03:30

84 lines
2.3 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Facades\Auth;
class ShipmentChecklist extends Model
{
use HasFactory;
protected $fillable = [
'shipment_id',
'user_id', // (deprecated) نگه‌داری‌شده برای backward-compat
'status', // (deprecated) — از item_title استفاده شود
'item_title',
'is_required',
'sort_order',
'is_completed',
'completed_by',
'notes',
'attachment',
'completed_at',
];
protected $casts = [
'is_required' => 'boolean',
'is_completed' => 'boolean',
'sort_order' => 'integer',
'completed_at' => 'datetime',
];
// ─── Relationships ───────────────────────────────
public function shipment(): BelongsTo
{
return $this->belongsTo(Shipment::class);
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class, 'user_id');
}
public function completedBy(): BelongsTo
{
return $this->belongsTo(User::class, 'completed_by');
}
// ─── Scopes ───────────────────────────────────────
public function scopeIncomplete($query)
{
return $query->where('is_completed', false);
}
public function scopeRequired($query)
{
return $query->where('is_required', true);
}
// ─── Helpers ─────────────────────────────────────
/**
* تکمیل یک آیتم چک‌لیست توسط کارمند.
*/
public function complete(?int $userId = null, ?string $note = null): void
{
$data = [
'is_completed' => true,
'completed_by' => $userId ?? Auth::id(),
'completed_at' => now(),
];
if ($note) {
$existing = $this->notes ? $this->notes . "\n\n" : '';
$data['notes'] = $existing . '[' . now()->format('Y-m-d H:i') . '] ' . $note;
}
$this->update($data);
}
}