131 lines
4.7 KiB
PHP
131 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace Modules\IndustrialEngineering\Services;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
use Modules\IndustrialEngineering\Models\Bom;
|
|
use Modules\IndustrialEngineering\Models\ItemMaster;
|
|
use Modules\IndustrialEngineering\Models\LineRevision;
|
|
use Modules\IndustrialEngineering\Models\LineTemplate;
|
|
use Modules\IndustrialEngineering\Models\ProductPlatform;
|
|
use Modules\IndustrialEngineering\Support\CmmsAvailability;
|
|
|
|
/**
|
|
* Establishes cross-module links automatically at creation time.
|
|
*/
|
|
class AutoIntegrationService
|
|
{
|
|
public function __construct(
|
|
protected ErpItemLinkService $erpItemLink,
|
|
protected IntegrationBridgeService $integrationBridge,
|
|
protected CodeGeneratorService $codeGenerator
|
|
) {
|
|
}
|
|
|
|
public function afterProductCreated(ProductPlatform $platform): array
|
|
{
|
|
$messages = [];
|
|
$messages[] = ['type' => 'success', 'text' => __('industrialengineering::lang.auto_product_ready')];
|
|
|
|
return $messages;
|
|
}
|
|
|
|
public function afterLineCreated(LineTemplate $line, LineRevision $revision, Bom $mbom): array
|
|
{
|
|
$messages = [];
|
|
|
|
try {
|
|
if (CmmsAvailability::isReady() && ! $line->maintenance_equipment_id) {
|
|
$sync = $this->integrationBridge->syncLineBomToMaintenance($line, $mbom);
|
|
$messages[] = ['type' => 'success', 'text' => __('industrialengineering::lang.cmms_synced_parts', [
|
|
'code' => $line->effectiveCode(),
|
|
'count' => $sync['parts_count'] ?? 0,
|
|
])];
|
|
}
|
|
} catch (\Throwable $e) {
|
|
Log::warning('IE auto CMMS on line create: '.$e->getMessage());
|
|
$messages[] = ['type' => 'warning', 'text' => __('industrialengineering::lang.auto_cmms_skipped')];
|
|
}
|
|
|
|
if (! CmmsAvailability::isReady()) {
|
|
$messages[] = ['type' => 'warning', 'text' => __('industrialengineering::lang.cmms_not_installed')];
|
|
}
|
|
|
|
$messages[] = ['type' => 'info', 'text' => __('industrialengineering::lang.auto_mbom_revision_created')];
|
|
|
|
return $messages;
|
|
}
|
|
|
|
public function afterItemCreated(ItemMaster $item, array $input = []): array
|
|
{
|
|
$messages = [];
|
|
|
|
if (! empty($input['variation_id']) || ! empty($input['product_id'])) {
|
|
$this->erpItemLink->linkItem($item, $input);
|
|
$messages[] = ['type' => 'success', 'text' => __('industrialengineering::lang.auto_erp_linked')];
|
|
|
|
return $messages;
|
|
}
|
|
|
|
$match = $this->erpItemLink->autoMatchProduct($item);
|
|
if ($match['linked']) {
|
|
$messages[] = ['type' => 'success', 'text' => $match['message']];
|
|
|
|
return $messages;
|
|
}
|
|
|
|
if (in_array($item->item_type, ['part', 'consumable', 'tool'], true)) {
|
|
$provision = app(ConnectionResolutionService::class)->provisionErpProduct($item);
|
|
if ($provision['action'] === 'created') {
|
|
$messages[] = ['type' => 'success', 'text' => $provision['message']];
|
|
} else {
|
|
$messages[] = ['type' => 'warning', 'text' => $provision['message'] ?? $match['message']];
|
|
}
|
|
|
|
return $messages;
|
|
}
|
|
|
|
$messages[] = ['type' => 'warning', 'text' => $match['message']];
|
|
|
|
return $messages;
|
|
}
|
|
|
|
public function afterBomNodeCreated(ItemMaster $item, array $input = []): array
|
|
{
|
|
return $this->afterItemCreated($item, $input);
|
|
}
|
|
|
|
public function ensureLineMbom(LineTemplate $line): array
|
|
{
|
|
$revision = $line->revisions()->latest('id')->first();
|
|
|
|
if (! $revision) {
|
|
$revision = LineRevision::create([
|
|
'business_id' => $line->business_id,
|
|
'line_template_id' => $line->id,
|
|
'revision_code' => 'A',
|
|
'auto_code' => $this->codeGenerator->suggest('revision', $line->business_id),
|
|
'name' => $line->name,
|
|
'status' => 'draft',
|
|
]);
|
|
}
|
|
|
|
$mbom = $revision->boms()->where('bom_type', 'mbom')->first();
|
|
if (! $mbom) {
|
|
$bomCodes = $this->codeGenerator->resolveCode('bom', $line->business_id, null, new Bom(), 'bom_code');
|
|
$mbom = Bom::create(array_merge($bomCodes, [
|
|
'business_id' => $line->business_id,
|
|
'line_revision_id' => $revision->id,
|
|
'bom_type' => 'mbom',
|
|
'name' => $line->name.' — MBOM',
|
|
'revision_code' => $revision->revision_code,
|
|
'status' => 'draft',
|
|
]));
|
|
|
|
return ['revision' => $revision, 'mbom' => $mbom, 'created' => true];
|
|
}
|
|
|
|
return ['revision' => $revision, 'mbom' => $mbom, 'created' => false];
|
|
}
|
|
}
|