d2d.emegrendeles.hu/app/Services/PricelistFileProcessService.php
2026-03-07 06:31:34 +01:00

187 lines
6.5 KiB
PHP

<?php
namespace App\Services;
use App\Enums\PricelistWorkflowStep;
use App\Models\PricelistFile;
use App\Enums\PricelistFileStatusEnum;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class PricelistFileProcessService
{
public function __construct(
protected PriceListService $priceListService
) {}
/**
* Elindítja az árlista feldolgozás kezdeti láncát (Pre-processing -> Validation)
*/
public function dispatchInitialChain(PricelistFile $pricelistFile): void
{
Bus::chain([
PricelistWorkflowStep::Preprocessing->getJob($pricelistFile),
PricelistWorkflowStep::Validation->getJob($pricelistFile),
])->dispatch();
}
/**
* Elindítja a végrehajtási jobot (Approval után)
*/
public function dispatchExecutionJob(PricelistFile $pricelistFile): void
{
$job = PricelistWorkflowStep::Execution->getJob($pricelistFile);
if ($job) {
dispatch($job);
}
}
public function updateStepStatus(
PricelistFile $pricelistFile,
PricelistWorkflowStep $stepEnum,
string $status,
?string $message = null,
?int $percentage = 0
): void {
$effectiveMessage = $message ?? $stepEnum->label();
// Frissítjük a modellt az adatbázisból, hogy a legfrissebb workflow_steps-szel dolgozzunk
if ($pricelistFile->exists) {
$pricelistFile->refresh();
}
$steps = $pricelistFile->workflow_steps ?? [];
$found = false;
foreach ($steps as &$step) {
if ($step['name'] === $stepEnum->value) {
$step['status'] = $status;
$step['message'] = $effectiveMessage;
$found = true;
break;
}
}
unset($step);
// Ha valamiért nem találtuk meg a lépést (pl. régi rekord), adjuk hozzá a listához
if (!$found) {
$steps[] = [
'name' => $stepEnum->value,
'label' => $stepEnum->label(),
'status' => $status,
'message' => $effectiveMessage,
];
}
$updateData = [
'workflow_steps' => $steps,
'processing_current_step' => ($message && $message !== $stepEnum->label())
? $stepEnum->label() . ': ' . $message
: $effectiveMessage,
'processing_current_step_percentage' => $percentage,
];
if ($status === 'inprogress') {
if ($stepEnum === PricelistWorkflowStep::Approval) {
$updateData['status'] = PricelistFileStatusEnum::waiting_for_approval;
$updateData['processing_current_step'] = null;
$updateData['processing_current_step_percentage'] = null;
} else {
$updateData['status'] = PricelistFileStatusEnum::inprogress;
}
} elseif ($status === 'failed') {
$updateData['status'] = PricelistFileStatusEnum::fail;
}
$pricelistFile->update($updateData);
}
/**
* Előfeldolgozás (Pre-processing) - Adatok ellenőrzése és beolvasása
*
* @param PricelistFile $pricelistFile
* @return bool
*/
public function preProcess(PricelistFile $pricelistFile): bool
{
$this->updateStepStatus($pricelistFile, PricelistWorkflowStep::Preprocessing, 'inprogress', 'Adatok ellenőrzése...', 10);
try {
// TODO: Itt hívjuk majd meg a PriceListService-t a fájl beolvasásához
// $data = $this->priceListService->readFromFile($pricelistFile->path);
// $results = $this->priceListService->importPriceListCheck($pricelistFile->supplier_id, $data);
sleep(15); // Szimuláció
$this->updateStepStatus($pricelistFile, PricelistWorkflowStep::Preprocessing, 'completed', 'Adatok ellenőrizve.', 100);
return true;
} catch (\Exception $e) {
Log::error('Pricelist pre-process error: ' . $e->getMessage());
$this->updateStepStatus($pricelistFile, PricelistWorkflowStep::Preprocessing, 'failed', $e->getMessage());
throw $e;
}
}
/**
* Validálás (Validation) - Üzleti szabályok ellenőrzése
*
* @param PricelistFile $pricelistFile
* @return bool
*/
public function validate(PricelistFile $pricelistFile): bool
{
sleep(15); // Szimuláció
$this->updateStepStatus($pricelistFile, PricelistWorkflowStep::Validation, 'inprogress', 'Üzleti szabályok ellenőrzése...', 60);
try {
// TODO: Validációs logika
sleep(15); // Szimuláció
$this->updateStepStatus($pricelistFile, PricelistWorkflowStep::Validation, 'completed', 'Validálás sikeres.', 100);
sleep(15); // Szimuláció
// A validálás végén átadjuk a lépést a jóváhagyásnak (Approval)
$this->updateStepStatus($pricelistFile, PricelistWorkflowStep::Approval, 'inprogress');
return true;
} catch (\Exception $e) {
Log::error('Pricelist validation error: ' . $e->getMessage());
$this->updateStepStatus($pricelistFile, PricelistWorkflowStep::Validation, 'failed', $e->getMessage());
throw $e;
}
}
/**
* Végrehajtás (Execution) - Módosítások tényleges alkalmazása tranzakcióban
*
* @param PricelistFile $pricelistFile
* @return bool
*/
public function execute(PricelistFile $pricelistFile): bool
{
$this->updateStepStatus($pricelistFile, PricelistWorkflowStep::Execution, 'inprogress', 'Árlista frissítése...', 0);
DB::beginTransaction();
try {
// TODO: Tényleges importálás végrehajtása
// $this->priceListService->importPriceList(...);
sleep(1); // Szimuláció
$this->updateStepStatus($pricelistFile, PricelistWorkflowStep::Execution, 'completed', 'Sikeresen befejezve.', 100);
DB::commit();
$pricelistFile->update([
'status' => PricelistFileStatusEnum::done,
]);
return true;
} catch (\Exception $e) {
DB::rollBack();
Log::error('Pricelist execution error: ' . $e->getMessage());
$this->updateStepStatus($pricelistFile, PricelistWorkflowStep::Execution, 'failed', $e->getMessage());
return false;
}
}
}