220 lines
7.2 KiB
PHP
220 lines
7.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services\WeldLogTriggers\Triggers;
|
|
|
|
use App\Services\WeldLogTriggers\Base\BaseTrigger;
|
|
use App\Helpers\TransactionHelper;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
/**
|
|
* Handovers Sync Trigger
|
|
*
|
|
* Syncs data from WeldLogs to Handovers table
|
|
* Groups by line_number and creates/updates handover records
|
|
*/
|
|
class HandoversSyncTrigger extends BaseTrigger
|
|
{
|
|
public function getName(): string
|
|
{
|
|
return 'Handovers Sync';
|
|
}
|
|
|
|
public function getOrder(): int
|
|
{
|
|
return 12;
|
|
}
|
|
|
|
public function getDependentFields(): array
|
|
{
|
|
return [
|
|
|
|
];
|
|
}
|
|
|
|
protected function process($data, $beforeData, array $context): array
|
|
{
|
|
try {
|
|
// Get current weld log data
|
|
$weldLog = db("weld_logs")->where("id", $data->id)->first();
|
|
|
|
if (!$weldLog) {
|
|
return ['success' => false, 'reason' => 'weld_log_not_found'];
|
|
}
|
|
|
|
// Skip processing if essential fields are missing
|
|
if (empty($weldLog->line_number)) {
|
|
return ['success' => false, 'reason' => 'line_number_empty'];
|
|
}
|
|
|
|
// Get all weld logs with the same line_number
|
|
$allWeldLogs = db("weld_logs")
|
|
->where("line_number", $weldLog->line_number)
|
|
->orderBy('id', 'ASC') // Deadlock prevention
|
|
->get();
|
|
|
|
// Process each weld log
|
|
$handoversCreated = 0;
|
|
$handoversUpdated = 0;
|
|
|
|
// Group by line - handovers are stored by line
|
|
$uniqueLines = [];
|
|
foreach ($allWeldLogs as $currentWeldLog) {
|
|
$lineKey = $currentWeldLog->line_number;
|
|
if (!isset($uniqueLines[$lineKey])) {
|
|
$uniqueLines[$lineKey] = $currentWeldLog;
|
|
}
|
|
}
|
|
|
|
// Process Handovers in chunks with TransactionHelper
|
|
TransactionHelper::chunkTransaction(
|
|
collect($uniqueLines),
|
|
function ($handoverChunk) use (&$handoversCreated, &$handoversUpdated) {
|
|
foreach ($handoverChunk as $currentWeldLog) {
|
|
$this->processHandoverRecord($currentWeldLog, $handoversCreated, $handoversUpdated);
|
|
}
|
|
return $handoverChunk->count();
|
|
},
|
|
10, // Chunk size
|
|
10000 // 10ms delay
|
|
);
|
|
|
|
} catch (\Throwable $th) {
|
|
Log::error("Handovers sync error: " . $th->getMessage());
|
|
|
|
// Log detailed error for debugging
|
|
Log::error("Error details: ", [
|
|
'weldLogId' => $data->id,
|
|
'exception' => get_class($th),
|
|
'file' => $th->getFile(),
|
|
'line' => $th->getLine(),
|
|
'trace' => $th->getTraceAsString()
|
|
]);
|
|
|
|
throw $th;
|
|
}
|
|
|
|
return [
|
|
'success' => true,
|
|
'created' => $handoversCreated,
|
|
'updated' => $handoversUpdated
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Process a single handover record
|
|
*/
|
|
protected function processHandoverRecord($currentWeldLog, &$createdCount, &$updatedCount)
|
|
{
|
|
// Check for required field
|
|
if (empty($currentWeldLog->line_number)) {
|
|
return;
|
|
}
|
|
|
|
// Get line list data (for additional information)
|
|
$lineList = db("line_lists")
|
|
->where('line_no', $currentWeldLog->line_number)
|
|
->first();
|
|
|
|
// Handover data
|
|
$handoverData = [
|
|
// Mapping: weld_logs -> handovers
|
|
'project' => $currentWeldLog->line_number, // line_number -> location (zone instead)
|
|
'work_type' => 'ТРУБКА', // piping_type -> work_type
|
|
'object' => $currentWeldLog->project ?? '', // project -> object
|
|
'location' => $currentWeldLog->design_area ?? '', // design_area -> project
|
|
|
|
// Status information
|
|
'status1' => 'In Progress',
|
|
'updated_at' => now()
|
|
];
|
|
|
|
// Where condition - location (line_number) and object (project) for unique record
|
|
$whereCondition = [
|
|
'project' => $currentWeldLog->line_number,
|
|
];
|
|
|
|
// Check if existing record exists
|
|
$existingRecord = db("handovers")
|
|
->where($whereCondition)
|
|
->first();
|
|
|
|
// Calculate volumes logic (mirrors hand-over.blade.php)
|
|
$collected = 0;
|
|
$notCollected = 1;
|
|
|
|
if ($existingRecord) {
|
|
$collected = (float) $existingRecord->collected_volumes;
|
|
// If existing record has specific value, use it, but check the "default to 1 if 0" rule
|
|
$notCollected = (float) $existingRecord->not_collected_volumes;
|
|
}
|
|
|
|
// Logic: if not_collected is 0 or empty, make it 1
|
|
if (empty($notCollected) || $notCollected == 0) {
|
|
$notCollected = 1;
|
|
}
|
|
|
|
$totalVolumes = $collected + $notCollected;
|
|
|
|
$handoverData['not_collected_volumes'] = $notCollected;
|
|
$handoverData['total_volumes'] = $totalVolumes;
|
|
|
|
// Calculate id_status logic
|
|
$handoverControlCount = (int) setting('handover_control_count', 3);
|
|
$idStatus = '';
|
|
$allAgreed = true;
|
|
$hasController = false;
|
|
|
|
for ($i = 1; $i <= $handoverControlCount; $i++) {
|
|
$statusField = "control{$i}_id_status";
|
|
$controllerField = "control{$i}_controller";
|
|
|
|
$statusValue = $existingRecord ? $existingRecord->$statusField : null;
|
|
$controllerValue = $existingRecord ? $existingRecord->$controllerField : null;
|
|
|
|
if ($controllerValue) {
|
|
$hasController = true;
|
|
|
|
if ($statusValue == 'Comment / замечание') {
|
|
$idStatus = 'Comment / замечание';
|
|
$allAgreed = false;
|
|
break;
|
|
}
|
|
if ($statusValue == 'Revision / Редакция') {
|
|
$idStatus = 'Revision / Редакция';
|
|
$allAgreed = false;
|
|
break;
|
|
}
|
|
|
|
if ($statusValue != 'Agreed / подписано') {
|
|
$allAgreed = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($idStatus === '') {
|
|
if ($allAgreed && $hasController) {
|
|
$archiveValue = $existingRecord ? $existingRecord->archive : null;
|
|
$idStatus = $archiveValue ? $archiveValue : 'Agreed / подписано';
|
|
} else {
|
|
$idStatus = 'Wait - Not Prepared';
|
|
}
|
|
}
|
|
|
|
$handoverData['id_status'] = $idStatus;
|
|
|
|
if ($existingRecord) {
|
|
// Update existing record
|
|
db("handovers")
|
|
->where('id', $existingRecord->id)
|
|
->update($handoverData);
|
|
$updatedCount++;
|
|
} else {
|
|
// Create new record
|
|
$handoverData['created_at'] = now();
|
|
db("handovers")->insert($handoverData);
|
|
$createdCount++;
|
|
}
|
|
}
|
|
}
|
|
|