Files
citrus-cms/app/Http/Controllers/DeleteTrigger/test_pack_base_statuses.php
T
2026-04-28 21:14:25 +03:00

42 lines
1.9 KiB
PHP

<?php
// Get the test_pack_base_status that is being deleted
$testPackBaseStatus = db("test_pack_base_statuses")->where("id", $request['key'])->first();
if ($testPackBaseStatus) {
try {
$testPackageNo = $testPackBaseStatus->test_package_no;
// Check if there are any remaining test_pack_base_statuses records with the same test_package_no
$remainingCount = db("test_pack_base_statuses")
->where("test_package_no", $testPackageNo)
->where("id", "!=", $request['key']) // Exclude the current record being deleted
->count();
// Only delete test_packages if no other test_pack_base_statuses records exist for this test_package_no
if ($remainingCount == 0) {
// Check if there are any weld_logs records with this test_package_no
$weldLogsCount = db("weld_logs")
->where("test_package_no", $testPackageNo)
->count();
if ($weldLogsCount > 0) {
dump("Cannot delete test_packages. Found $weldLogsCount weld_logs records for test_package_no: $testPackageNo");
} else {
$deletedCount = db("test_packages")
->where("test_package_number", $testPackageNo)
->delete();
dump("No remaining test_pack_base_statuses for test_package_no: $testPackageNo. Deleted $deletedCount test_packages records.");
}
} else {
dump("Found $remainingCount remaining test_pack_base_statuses for test_package_no: $testPackageNo. Keeping test_packages records.");
}
} catch (\Throwable $th) {
dump("Error processing test_packages deletion: " . $th->getMessage());
Log::error("DeleteTrigger test_pack_base_statuses error: " . $th->getMessage());
}
} else {
dump("Test pack base status not found with id: " . $request['key']);
}
?>