Files
citrus-cms/app/Services/RegisterCreator/PlaceholderReplacer.php
T
2026-04-28 21:14:25 +03:00

101 lines
2.8 KiB
PHP

<?php
namespace App\Services\RegisterCreator;
use Illuminate\Support\Facades\Log;
class PlaceholderReplacer
{
/**
* Prepare replacements array from weld log data
*/
public function prepareReplacements(array $weldLogData, array $additionalData = []): array
{
$replacements = [];
// Add project name
$replacements['{project_name}'] = setting('project_name_ru') ?? '';
// Add all weld log fields
foreach ($weldLogData as $column => $value) {
$replacements['{' . $column . '}'] = $value ?? '';
}
// Add additional data (title placeholders, etc.)
foreach ($additionalData as $key => $value) {
if (!str_starts_with($key, '{')) {
$key = '{' . $key . '}';
}
$replacements[$key] = $value ?? '';
}
Log::debug('Placeholders prepared', [
'count' => count($replacements),
'keys' => array_keys($replacements)
]);
return $replacements;
}
/**
* Get latest test date from weld log
*/
public function getLatestDate(array $weldLogData, string $registerColumnBased): string
{
try {
$latestDateQuery = db("weld_logs")
->where($registerColumnBased, $weldLogData[$registerColumnBased])
->select(
\DB::raw("GREATEST(
IFNULL(vt_test_date, '0000-00-00'),
IFNULL(rt_test_date, '0000-00-00'),
IFNULL(ut_test_date, '0000-00-00'),
IFNULL(pt_test_date, '0000-00-00'),
IFNULL(mt_test_date, '0000-00-00'),
IFNULL(pmi_test_date, '0000-00-00'),
IFNULL(ht_test_date, '0000-00-00'),
IFNULL(pwht_test_date, '0000-00-00'),
IFNULL(ferrite_test_date, '0000-00-00')
) AS latest_date")
)
->first();
// If query returns result, use latest date, otherwise use welding_date
$latestDate = ($latestDateQuery && $latestDateQuery->latest_date != '0000-00-00')
? $latestDateQuery->latest_date
: ($weldLogData['welding_date'] ?? now()->format('Y-m-d'));
Log::debug('Latest date calculated', [
'line' => $weldLogData[$registerColumnBased],
'date' => $latestDate
]);
return $latestDate;
} catch (\Throwable $th) {
Log::error('Error calculating latest date', [
'error' => $th->getMessage()
]);
return $weldLogData['welding_date'] ?? now()->format('Y-m-d');
}
}
}