Created
July 25, 2022 13:50
-
-
Save DumahX/394a7887e0870fa8527a38af3e86d675 to your computer and use it in GitHub Desktop.
Remove duplicate corporate accounts from the mpca_corporate_accounts table.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action('init', function() { | |
$users = MeprUser::all(); | |
$ca_obj_ids = array(); | |
$duplicates = array(); | |
foreach($users as $user) { | |
$corporate_accounts = MPCA_Corporate_Account::get_all_by_user_id($user->ID); | |
if(!empty($corporate_accounts)) { | |
foreach($corporate_accounts as $ca_rec) { | |
$corporate_account = new MPCA_Corporate_Account(); | |
$corporate_account->load_from_array($ca_rec); | |
// If this is a duplicate corporate account with no sub accounts used. Add it as a duplicate. | |
if(in_array($corporate_account->obj_id, $ca_obj_ids) && $corporate_account->num_sub_accounts_used() <= 0) { | |
$duplicates[] = $corporate_account; | |
continue; | |
} | |
$ca_obj_ids[] = $corporate_account->obj_id; | |
} | |
} | |
} | |
// If there were duplicates, remove them as a corporate account. | |
if(!empty($duplicates)) { | |
foreach($duplicates as $duplicate) { | |
$duplicate->destroy(); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment