76 lines
2.4 KiB
PHP
76 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace Modules\Maintenance\Services;
|
|
|
|
use App\Business;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Modules\Maintenance\Models\Equipment;
|
|
use Modules\Maintenance\Models\SparePart;
|
|
|
|
class MaintenanceIntegrationBridgeService
|
|
{
|
|
public function syncSparePartFromProduct(int $businessId, int $productId, ?int $variationId = null): ?SparePart
|
|
{
|
|
if (! Schema::hasTable('products') || ! Schema::hasTable('maintenance_spare_parts')) {
|
|
return null;
|
|
}
|
|
|
|
$product = \App\Product::where('business_id', $businessId)->find($productId);
|
|
if (! $product) {
|
|
return null;
|
|
}
|
|
|
|
return SparePart::updateOrCreate(
|
|
['business_id' => $businessId, 'product_id' => $productId],
|
|
[
|
|
'part_number' => $product->sku ?? ('PRD-'.$productId),
|
|
'name' => $product->name,
|
|
'variation_id' => $variationId,
|
|
'unit' => $product->unit->short_name ?? 'عدد',
|
|
]
|
|
);
|
|
}
|
|
|
|
public function linkLineTemplateEquipment(int $lineTemplateId, int $equipmentId): bool
|
|
{
|
|
if (! class_exists(\Modules\IndustrialEngineering\Models\LineTemplate::class)) {
|
|
return false;
|
|
}
|
|
|
|
$line = \Modules\IndustrialEngineering\Models\LineTemplate::find($lineTemplateId);
|
|
if (! $line) {
|
|
return false;
|
|
}
|
|
|
|
$line->update(['maintenance_equipment_id' => $equipmentId]);
|
|
|
|
return true;
|
|
}
|
|
|
|
public function notifyManufacturingDowntime(Business $business, Equipment $equipment, float $hours): void
|
|
{
|
|
if (! class_exists(\Modules\Manufacturing\Entities\MfgProduction::class)) {
|
|
return;
|
|
}
|
|
|
|
app(MaintenanceNotificationService::class)->notify(
|
|
$business,
|
|
'production_downtime',
|
|
'توقف تولید',
|
|
"تجهیز «{$equipment->name}» به مدت {$hours} ساعت متوقف شد — برنامه تولید را بررسی کنید.",
|
|
null,
|
|
['equipment_id' => $equipment->id, 'downtime_hours' => $hours]
|
|
);
|
|
|
|
if (Schema::hasTable('maintenance_activity_logs')) {
|
|
MaintenanceActivityService::log(
|
|
$business,
|
|
'manufacturing_bridge',
|
|
'equipment',
|
|
$equipment->id,
|
|
"هشدار توقف تولید برای تجهیز {$equipment->name}"
|
|
);
|
|
}
|
|
}
|
|
}
|