0, 'message' => "Customer name is required."], JSON_THROW_ON_ERROR); exit; } if (!isset($_REQUEST['customer_id']) || empty(trim($_REQUEST['customer_id']))) { echo json_encode(['status' => 0, 'message' => "Customer is required."], JSON_THROW_ON_ERROR); exit; } // Sanitize customer name input $customer_name = $db->escape(trim($_REQUEST['customer_name'])); $customer_id = $db->escape(trim($_REQUEST['customer_id'])); // Start transaction $db->transBegin(); // Fetch files for the customer from `customer_upload_ltms` $query = $db->connect()->query("SELECT * FROM customer_upload_ltms WHERE fullname = '$customer_name'"); // Check if any files were found if($query->num_rows > 0) { // Loop through the found files and process each one while($row = $query->fetch_assoc()) { $file_name = $row['filename']; $source_path = '../../dist/img/customer/ltms-customer-upload/' . $file_name; $destination_path = '../../dist/img/customer/ltms/' . $file_name; // Adjust destination as needed $fullName = $row['fullname']; $ref_code = $row['reference']; $created_by = $_SESSION['user']['id']; $created_at = date('Y-m-d H:i:s'); if (file_exists($source_path)) { // Attempt to copy the file to the destination directory if (copy($source_path, $destination_path)) { // Update the status in `customer_upload_ltms` $db->connect()->query("UPDATE customer_upload_ltms SET status = 2 WHERE filename = " . $row['filename']); $is_valid = 1; $is_active = 1; $db->connect()->query("INSERT INTO customer_ltms (file, customer_id, created_at, is_valid, is_active, created_by) VALUES ('$file_name', '$customer_id', '$created_at', '$is_valid', '$is_active', '$created_by')"); } else { // Rollback the transaction if file copy fails $db->transRollback(); echo json_encode(['status' => 0, 'message' => "Failed to attach file."], JSON_THROW_ON_ERROR); exit; } }else{ $db->connect()->query("INSERT INTO ltms_logs (fullname, ref_code, source, created_at, created_by) VALUES ('$fullName', '$ref_code', '$source_path', '$created_at', '$created_by')"); //no file exist $db->connect()->query("UPDATE customer_upload_ltms SET status = 3 WHERE filename = " . $row['filename']); } } // Commit the transaction after all files are processed $db->transCommit(); echo json_encode(['status' => 1, 'message' => "Files attached successfully."], JSON_THROW_ON_ERROR); } else { // If no files found for the customer echo json_encode(['status' => 0, 'message' => "No files found for the specified customer."], JSON_THROW_ON_ERROR); } } else { echo json_encode(['status' => 0, 'message' => "Invalid API key."], JSON_THROW_ON_ERROR); } ?>