@@ -3623,7 +3623,13 @@ function lotw_update_batch($records)
36233623
36243624 // Step 1: Build WHERE conditions to find all matching QSOs and their current upload status
36253625 $ match_conditions = array ();
3626- foreach ($ records as $ idx => $ record ) {
3626+ $ primary_keys = array ();
3627+ foreach ($ records as $ record ) {
3628+ if (!empty ($ record ['primary_key ' ])) {
3629+ $ primary_keys [] = (int )$ record ['primary_key ' ];
3630+ continue ;
3631+ }
3632+
36273633 $ match_conditions [] = sprintf (
36283634 "(date_format(COL_TIME_ON, '%%Y-%%m-%%d %%H:%%i') = '%s' AND COL_CALL = '%s' AND COL_BAND = '%s' AND COL_STATION_CALLSIGN = '%s') " ,
36293635 $ this ->db ->escape_str ($ record ['datetime ' ]),
@@ -3632,6 +3638,19 @@ function lotw_update_batch($records)
36323638 $ this ->db ->escape_str ($ record ['station_callsign ' ])
36333639 );
36343640 }
3641+
3642+ $ where_parts = array ();
3643+ if (!empty ($ primary_keys )) {
3644+ $ where_parts [] = 'COL_PRIMARY_KEY IN ( ' . implode (', ' , array_unique ($ primary_keys )) . ') ' ;
3645+ }
3646+ if (!empty ($ match_conditions )) {
3647+ $ where_parts [] = '( ' . implode (' OR ' , $ match_conditions ) . ') ' ;
3648+ }
3649+
3650+ if (empty ($ where_parts )) {
3651+ log_message ('warning ' , 'LoTW batch update: No usable keys provided ' );
3652+ return array ('updated ' => 0 , 'gridsquare_updated ' => 0 , 'errors ' => 0 );
3653+ }
36353654
36363655 // Step 2: Get all matching QSOs with their current upload status and gridsquare
36373656 $ sql = "SELECT COL_PRIMARY_KEY,
@@ -3644,7 +3663,7 @@ function lotw_update_batch($records)
36443663 station_profile.station_id
36453664 FROM {$ table_name }
36463665 LEFT JOIN station_profile ON {$ table_name }.station_id = station_profile.station_id
3647- WHERE ( " . implode (' OR ' , $ match_conditions ) . " ) " ;
3666+ WHERE " . implode (' OR ' , $ where_parts ) ;
36483667
36493668 $ query = $ this ->db ->query ($ sql );
36503669
@@ -3655,7 +3674,9 @@ function lotw_update_batch($records)
36553674
36563675 // Step 3: Build lookup map and batch update array
36573676 $ qso_map = array ();
3677+ $ qso_map_by_id = array ();
36583678 foreach ($ query ->result () as $ qso ) {
3679+ $ qso_map_by_id [$ qso ->COL_PRIMARY_KEY ] = $ qso ;
36593680 $ key = $ qso ->fmt_time . '| ' . $ qso ->COL_CALL . '| ' . $ qso ->COL_BAND . '| ' . $ qso ->COL_STATION_CALLSIGN ;
36603681 $ qso_map [$ key ] = $ qso ;
36613682 }
@@ -3664,14 +3685,20 @@ function lotw_update_batch($records)
36643685 $ gridsquare_updates = array ();
36653686
36663687 foreach ($ records as $ record ) {
3667- $ key = $ record ['datetime ' ] . '| ' . $ record ['callsign ' ] . '| ' . $ record ['band ' ] . '| ' . $ record ['station_callsign ' ];
3668-
3669- if (!isset ($ qso_map [$ key ])) {
3688+ $ qso = null ;
3689+ if (!empty ($ record ['primary_key ' ]) && isset ($ qso_map_by_id [(int )$ record ['primary_key ' ]])) {
3690+ $ qso = $ qso_map_by_id [(int )$ record ['primary_key ' ]];
3691+ } else {
3692+ $ key = $ record ['datetime ' ] . '| ' . $ record ['callsign ' ] . '| ' . $ record ['band ' ] . '| ' . $ record ['station_callsign ' ];
3693+ if (isset ($ qso_map [$ key ])) {
3694+ $ qso = $ qso_map [$ key ];
3695+ }
3696+ }
3697+
3698+ if ($ qso === null ) {
36703699 continue ; // QSO not found in database
36713700 }
36723701
3673- $ qso = $ qso_map [$ key ];
3674-
36753702 // Skip if already updated with this exact status
36763703 if ($ qso ->COL_LOTW_QSL_RCVD == $ record ['qsl_status ' ]) {
36773704 continue ;
0 commit comments