belongsTo(ProductPlatform::class, 'product_platform_id'); } public function parentTemplate() { return $this->belongsTo(LineTemplate::class, 'parent_template_id'); } public function childTemplates() { return $this->hasMany(LineTemplate::class, 'parent_template_id')->orderBy('sort_order'); } public function revisions() { return $this->hasMany(LineRevision::class, 'line_template_id'); } public function latestRevision() { return $this->hasOne(LineRevision::class, 'line_template_id')->latestOfMany('id'); } /** * Latest MBOM for this production line (legacy alias: bom). */ public function bom() { return $this->hasOneThrough( Bom::class, LineRevision::class, 'line_template_id', 'line_revision_id', 'id', 'id' )->where('ie_boms.bom_type', 'mbom'); } public function maintenanceEquipment() { return $this->belongsTo(\Modules\Maintenance\Models\Equipment::class, 'maintenance_equipment_id'); } public function scopeProductionLines($query) { return $query->where('line_type', 'production_line')->whereNull('parent_template_id'); } public function effectiveCode(): string { return $this->template_code ?: ($this->auto_code ?? ''); } }