Files
citrus-cms/resources/views/cron/weld_logs-delete-non-matching-test-pack-statuses.blade.php
2026-04-28 21:15:09 +03:00

161 lines
6.6 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
try {
// ============================================================
// SECTION 1: Clean empty/null test_package_number records
// ============================================================
// Delete test_packages with empty or null test_package_number
$emptyTestPackages = DB::table('test_packages')
->where(function($query) {
$query->where('test_package_number', '')
->orWhereNull('test_package_number');
})
->where(function($query) {
$query->where('is_manual', 0)
->orWhereNull('is_manual');
})
->whereNull('test_date')
->whereNull('reinstatement_date')
->whereNull('cleaning_blowing_drying_date')
->delete();
Log::info("Deleted empty test_package_number records", [
'deleted_count' => $emptyTestPackages,
'table' => 'test_packages'
]);
// Delete test_pack_base_statuses with empty or null fields
$emptyBaseStatuses = DB::table('test_pack_base_statuses')
->where(function($query) {
$query->where('test_package_no', '')
->orWhereNull('test_package_no')
->orWhere('drawing_no', '')
->orWhereNull('drawing_no');
})
->where(function($query) {
$query->where('is_manual', 0)
->orWhereNull('is_manual');
})
->delete();
Log::info("Deleted empty field records", [
'deleted_count' => $emptyBaseStatuses,
'table' => 'test_pack_base_statuses'
]);
// ============================================================
// SECTION 2: Delete non-matching records (not in weld_logs)
// ============================================================
// Find test_package_no not in weld_logs
$nonMatchingTestPackages = DB::table('test_packages as tp')
->leftJoin('weld_logs as wl', function($join) {
$join->on('tp.test_package_number', '=', 'wl.test_package_no')
->whereNotNull('wl.test_package_no')
->where('wl.test_package_no', '!=', ''); // Exclude empty strings
})
->whereNull('wl.test_package_no')
->whereNotNull('tp.test_package_number') // Ensure test_package_number is not null
->where('tp.test_package_number', '!=', '') // Exclude empty test_package_number
->where(function($query) {
$query->where('tp.is_manual', 0)
->orWhereNull('tp.is_manual');
})
->whereNull('tp.test_date')
->whereNull('tp.reinstatement_date')
->whereNull('tp.cleaning_blowing_drying_date')
->pluck('tp.test_package_number')
->toArray();
// Filter out null and empty values from the array
$nonMatchingTestPackages = array_filter($nonMatchingTestPackages, function($value) {
return !empty($value) && $value !== null && trim($value) !== '';
});
// Aşama 2: Bulunan test_package_no'ları sil
if (!empty($nonMatchingTestPackages)) {
Log::info("Deleting non-matching test packages", [
'count' => count($nonMatchingTestPackages),
'packages' => array_slice($nonMatchingTestPackages, 0, 5) // Log first 5 for debugging
]);
$TPresult = DB::table('test_packages')
->whereIn('test_package_number', $nonMatchingTestPackages)
->delete();
Log::info("Test packages deleted", ['deleted_count' => $TPresult]);
} else {
$TPresult = 0;
Log::debug("No non-matching test packages found to delete");
}
// Aşama 1: test_pack_base_statuses için weld_logs'da olmayan kayıtları bul
$nonMatchingBaseStatuses = DB::table('test_pack_base_statuses as tpbs')
->leftJoin('weld_logs as wl', function($join) {
$join->on('tpbs.drawing_no', '=', 'wl.iso_number')
->on('tpbs.test_package_no', '=', 'wl.test_package_no')
->whereNotNull('wl.test_package_no')
->where('wl.test_package_no', '!=', ''); // Exclude empty strings
})
->leftJoin('test_packages as tp', 'tpbs.test_package_no', '=', 'tp.test_package_number')
->whereNull('wl.test_package_no')
->whereNotNull('tpbs.test_package_no') // Ensure test_package_no is not null
->where('tpbs.test_package_no', '!=', '') // Exclude empty test_package_no
->whereNotNull('tpbs.drawing_no') // Ensure drawing_no is not null
->where('tpbs.drawing_no', '!=', '') // Exclude empty drawing_no
->where(function($query) {
$query->where('tpbs.is_manual', 0)
->orWhereNull('tpbs.is_manual');
})
->where(function($query) {
$query->whereNull('tp.test_date')
->whereNull('tp.reinstatement_date')
->whereNull('tp.cleaning_blowing_drying_date');
})
->pluck('tpbs.id')
->toArray();
// Filter out null values from the array
$nonMatchingBaseStatuses = array_filter($nonMatchingBaseStatuses, function($value) {
return $value !== null && is_numeric($value) && $value > 0;
});
// Aşama 2: Bulunan kayıtları sil
if (!empty($nonMatchingBaseStatuses)) {
Log::info("Deleting non-matching test pack base statuses", [
'count' => count($nonMatchingBaseStatuses),
'ids' => array_slice($nonMatchingBaseStatuses, 0, 5) // Log first 5 for debugging
]);
$TPISOresult = DB::table('test_pack_base_statuses')
->whereIn('id', $nonMatchingBaseStatuses)
->delete();
Log::info("Test pack base statuses deleted", ['deleted_count' => $TPISOresult]);
} else {
$TPISOresult = 0;
Log::debug("No non-matching test pack base statuses found to delete");
}
Log::info("Test pack cleanup completed successfully", [
'empty_test_packages_deleted' => $emptyTestPackages ?? 0,
'empty_base_statuses_deleted' => $emptyBaseStatuses ?? 0,
'non_matching_test_packages_deleted' => $TPresult ?? 0,
'non_matching_base_statuses_deleted' => $TPISOresult ?? 0,
'total_deleted' => ($emptyTestPackages ?? 0) + ($emptyBaseStatuses ?? 0) + ($TPresult ?? 0) + ($TPISOresult ?? 0)
]);
} catch (\Throwable $th) {
Log::error("Error in weld_logs-delete-non-matching-test-pack-statuses", [
'error' => $th->getMessage(),
'file' => $th->getFile(),
'line' => $th->getLine(),
'trace' => $th->getTraceAsString()
]);
// Don't throw - this is a cleanup operation and should not break the main flow
}
?>