/** * Save presets and create/update account/reference */ function postProcess() { $values = $this->exportValues(); $was_created = FALSE; // save presets if (!empty($values['reference_type'])) { CRM_Core_BAO_Setting::setItem($values['reference_type'], 'CiviBanking', 'account.default_reference_id'); } if (!empty($values['country'])) { CRM_Core_BAO_Setting::setItem($values['country'], 'CiviBanking', 'account.default_country'); } // create bank account $ba_id = $values['ba_id']; if (empty($ba_id)) { $bank_account = civicrm_api3('BankingAccount', 'create', array('contact_id' => $values['contact_id'], 'data_parsed' => '{}')); $was_created = TRUE; $ba_id = $bank_account['id']; } // update bank account data $bank_data_attributes = array('bic' => 'BIC', 'bank_name' => 'name', 'country' => 'country'); $bank_bao = new CRM_Banking_BAO_BankAccount(); $bank_bao->get('id', $ba_id); $bank_data = $bank_bao->getDataParsed(); foreach ($bank_data_attributes as $form_attribute => $bank_data_attribute) { if (empty($values[$form_attribute])) { unset($bank_data[$bank_data_attribute]); } else { $bank_data[$bank_data_attribute] = $values[$form_attribute]; } } $bank_bao->setDataParsed($bank_data); $bank_bao->save(); // update/create bank reference $reference_update = array('reference' => $values['reference'], 'reference_type_id' => $values['reference_type'], 'ba_id' => $ba_id); if (!empty($values['reference_id'])) { $reference_update['id'] = $values['reference_id']; } civicrm_api3('BankingAccountReference', 'create', $reference_update); if ($was_created) { CRM_Core_Session::setStatus(ts("Bank account '%1' was created.", $values['reference']), ts('Success')); } else { CRM_Core_Session::setStatus(ts("Bank account '%1' was updated.", $values['reference']), ts('Success')); } // return to accounts tab CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid=2&selectedChild=bank_accounts")); parent::postProcess(); }
/** * Will execute any dedupe/merge requests specified via the REQUEST params */ function executeRequests($duplicates) { $refs_fixed = 0; $accounts_fixed = 0; $errors_ecountered = 0; $account_reflist = array(); $reflist = array(); // ========================= // MERGE DUPLICATE ACCOUNTS // ========================= if (!empty($_REQUEST['fixdupe'])) { if ($_REQUEST['fixdupe'] == 'all') { foreach ($duplicates['account'] as $reference => $info) { if ((int) $info['reference_id']) { $account_reflist[] = (int) $info['reference_id']; } } } else { $parm_list = explode(',', $_REQUEST['fixdupe']); foreach ($parm_list as $reference_id) { if ((int) $reference_id) { $account_reflist[] = (int) $reference_id; } } } // perform the changes foreach ($account_reflist as $reference_id) { // MERGE ACCOUNTS // first, find the account IDs to merge $bank_account_ids = array(); $sql = "SELECT ba_id \n FROM civicrm_bank_account_reference \n WHERE reference = (SELECT reference FROM civicrm_bank_account_reference WHERE id={$reference_id})\n ORDER BY ba_id ASC;"; // use the oldest (lowest ID) as merge target $bank_account_query = CRM_Core_DAO::executeQuery($sql); while ($bank_account_query->fetch()) { $bank_account_ids[] = $bank_account_query->ba_id; } if (count($bank_account_ids) < 2) { // we need at least two bank accounts to merge... $errors_ecountered += 1; continue; } // now: load the first bank account... $main_ba = new CRM_Banking_BAO_BankAccount(); $main_ba->get('id', $bank_account_ids[0]); $main_data_parsed = $main_ba->getDataParsed(); // ...and try to merge the others into it $merge_failed = FALSE; for ($i = 1; $i < count($bank_account_ids); $i++) { $merge_ba = new CRM_Banking_BAO_BankAccount(); $merge_ba->get('id', $bank_account_ids[$i]); // merge created_date if (isset($merge_ba->created_date) && $merge_ba->created_date < $main_ba->created_date) { $main_ba->created_date = $merge_ba->created_date; } // merge description/data_raw $replace_attributes = array('description', 'data_raw'); foreach ($replace_attributes as $attribute) { if (!empty($merge_ba->{$attribute})) { if (empty($main_ba->{$attribute})) { // main_ba.$attribute not set, just overwrite $main_ba->{$attribute} = $merge_ba->{$attribute}; } else { // main_ba.$attribute set, check if identical if ($main_ba->{$attribute} != $merge_ba->{$attribute}) { if (CIVIBANKING_MERGE_ACCOUNTS_CHECK_STRICT) { $merge_failed = TRUE; break; } } } } } if ($merge_failed) { break; } // merge data_parsed $merge_data_parsed = $merge_ba->getDataParsed(); foreach ($merge_data_parsed as $key => $value) { if (empty($main_data_parsed[$key])) { $main_data_parsed[$key] = $value; } else { if ($main_data_parsed[$key] != $merge_data_parsed[$key]) { if (CIVIBANKING_MERGE_ACCOUNTS_CHECK_STRICT) { $merge_failed = TRUE; break; } } } } if ($merge_failed) { break; } } // MERGE NEXT ACCOUNT (for same target) if ($merge_failed) { $errors_ecountered += 1; } else { // SAVE THE MERGED OBJECT $main_ba->setDataParsed($main_data_parsed); $main_ba->modified_date = date('YmdHis'); $main_ba->save(); // DELETE THE OTHER, NOW OBSOLETE ACCOUNTS $target_id = $bank_account_ids[0]; unset($bank_account_ids[0]); $delete_ids = implode(',', $bank_account_ids); CRM_Core_DAO::singleValueQuery("UPDATE civicrm_bank_account_reference SET ba_id={$target_id} WHERE ba_id IN ({$delete_ids});"); CRM_Core_DAO::singleValueQuery("UPDATE civicrm_bank_tx SET ba_id={$target_id} WHERE ba_id IN ({$delete_ids});"); CRM_Core_DAO::singleValueQuery("UPDATE civicrm_bank_tx SET party_ba_id={$target_id} WHERE party_ba_id IN ({$delete_ids});"); CRM_Core_DAO::singleValueQuery("DELETE FROM civicrm_bank_account WHERE id IN ({$delete_ids});"); $accounts_fixed += 1; // finally, add the reference_ids to duplicate reference list, // in order to delete the resulting duplicate references $reflist[] = $reference_id; } } if ($errors_ecountered) { CRM_Core_Session::setStatus(ts("%1 errors were encountered when trying to merge duplicate bank accounts, %2/%3 bank accounts were successfully merged.", array(1 => $errors_ecountered, 2 => $accounts_fixed, 3 => $errors_ecountered + $accounts_fixed)), ts('Errors encountered'), 'warn'); $errors_ecountered = 0; } else { CRM_Core_Session::setStatus(ts("%1 duplicate bank accounts successfully merged.", array(1 => $accounts_fixed)), ts('Success'), 'info'); } } // ============================ // DELETE DUPLICATE REFERENCES. // ============================ if (!empty($_REQUEST['fixref']) || !empty($reflist)) { // They should be identical and can be safely removed if (!empty($_REQUEST['fixref'])) { if ($_REQUEST['fixref'] == 'all') { foreach ($duplicates['reference'] as $reference => $info) { if ((int) $info['reference_id']) { $reflist[] = (int) $info['reference_id']; } } } else { $parm_list = explode(',', $_REQUEST['fixref']); foreach ($parm_list as $reference_id) { if ((int) $reference_id) { $reflist[] = (int) $reference_id; } } } } // perform the changes foreach ($reflist as $reference_id) { $sql = "SELECT ref_delete.id AS reference_id\n FROM civicrm_bank_account_reference ref_delete\n LEFT JOIN civicrm_bank_account_reference ref_keep ON ref_keep.id={$reference_id}\n WHERE ref_keep.reference = ref_delete.reference\n AND ref_keep.ba_id = ref_delete.ba_id\n AND ref_keep.reference_type_id = ref_delete.reference_type_id\n AND ref_keep.id != ref_delete.id;"; $reference_to_delete = CRM_Core_DAO::executeQuery($sql); while ($reference_to_delete->fetch()) { $result = civicrm_api('BankingAccountReference', 'delete', array('id' => $reference_to_delete->reference_id, 'version' => 3)); if (empty($result['is_error'])) { $refs_fixed += 1; } else { error_log("org.project60.banking.dedupe: Error while deleting dupe reference: " . $result['error_message']); $errors_ecountered += 1; } } } if ($errors_ecountered) { CRM_Core_Session::setStatus(ts("%1 errors were encountered when trying to delete duplicate references. %2/%3 references were successfully deleted.", array(1 => $errors_ecountered, 2 => $refs_fixed, 3 => $errors_ecountered + $refs_fixed)), ts('Errors encountered'), 'warn'); $errors_ecountered = 0; } else { CRM_Core_Session::setStatus(ts("%1 duplicate references successfully deleted.", array(1 => $refs_fixed)), ts('Success'), 'info'); } } return $refs_fixed + $accounts_fixed; }
/** * Will store the donor's account data * * @todo move to post processors */ function storeAccountWithContact($btx, $contact_id) { // find all reference types $reference_type_group = array('name' => 'civicrm_banking.reference_types'); $reference_types = array(); CRM_Core_OptionValue::getValues($reference_type_group, $reference_types); // gather the information $data = $btx->getDataParsed(); $references = array(); foreach ($reference_types as $reference_type) { $field_name = '_party_' . $reference_type['name']; if (!empty($data[$field_name])) { $references[$reference_type['id']] = $data[$field_name]; } } // if we don't have references, there's nothing we can do... if (empty($references)) { return; } // gather account info $account_info = array(); if (!empty($data['_party_BIC'])) { $account_info['BIC'] = $data['_party_BIC']; } if (!empty($data['_party_IBAN'])) { $account_info['country'] = substr($data['_party_IBAN'], 0, 2); } // copy all entries that start with _party_ba_ into the account info foreach ($data as $key => $value) { if ('_party_ba_' == substr($key, 0, 10)) { if (!empty($value)) { $new_key = substr($key, 10); $account_info[$new_key] = $value; } } } // find all referenced bank accounts $bank_accounts = array(); $contact_bank_account_id = NULL; $contact_bank_account_created = false; $reference2instances = array(); foreach ($references as $reference_type => $reference) { $reference2instances[$reference] = array(); $query = array('version' => 3, 'reference' => $reference, 'reference_type_id' => $reference_type); $existing = civicrm_api('BankingAccountReference', 'get', $query); if (empty($existing['is_error'])) { foreach ($existing['values'] as $account_reference) { array_push($reference2instances[$reference], $account_reference); if (!isset($bank_accounts[$account_reference['ba_id']])) { // load the bank account $ba_bao = new CRM_Banking_BAO_BankAccount(); $ba_bao->get('id', $account_reference['ba_id']); $bank_accounts[$account_reference['ba_id']] = $ba_bao; } // consider this bank account to be ours if the contact id matches if (!$contact_bank_account_id && $ba_bao->contact_id == $contact_id) { $contact_bank_account_id = $ba_bao->id; } } } } // create new account if it does not yet exist if (!$contact_bank_account_id) { $ba_bao = new CRM_Banking_BAO_BankAccount(); $ba_bao->contact_id = $contact_id; $ba_bao->description = ts("created by CiviBanking"); $ba_bao->created_date = date('YmdHis'); $ba_bao->modified_date = date('YmdHis'); $ba_bao->data_raw = NULL; $ba_bao->data_parsed = "{}"; $ba_bao->save(); $contact_bank_account_id = $ba_bao->id; $bank_accounts[$contact_bank_account_id] = $ba_bao; $contact_bank_account_created = true; } // update bank account data $ba_bao = $bank_accounts[$contact_bank_account_id]; $ba_data = $ba_bao->getDataParsed(); foreach ($account_info as $key => $value) { $ba_data[$key] = $value; } $ba_bao->setDataParsed($ba_data); $ba_bao->save(); // create references (warn if exists for another contact) foreach ($references as $reference_type => $reference) { // check the existing $reference_already_there = false; foreach ($reference2instances[$reference] as $reference_instance) { if ($reference_instance['ba_id'] == $contact_bank_account_id) { // there is already a reference for 'our' bank account $reference_already_there = true; break; } } if (!$reference_already_there) { // there was no reference to 'our' bank account -> create! $query = array('version' => 3, 'reference' => $reference, 'reference_type_id' => $reference_type, 'ba_id' => $contact_bank_account_id); $result = civicrm_api('BankingAccountReference', 'create', $query); if (!empty($result['is_error'])) { CRM_Core_Session::setStatus(ts("Couldn't create reference. Error was: '%1'", array(1 => $result['error_message'])), ts('Error'), 'alert'); } } } // finally, create some feedback if ($contact_bank_account_created) { if (count($bank_accounts) > 1) { // there are mutiple acccounts referenced by this $message = ts("The account information of this contact was saved, but it is also used by the following contacts:<br/><ul>%s</ul>"); $contacts = ""; foreach ($bank_accounts as $ba_id => $ba_bao) { if ($ba_id == $contact_bank_account_id) { continue; } $contact = civicrm_api('Contact', 'getsingle', array('version' => 3, 'id' => $ba_bao->contact_id)); if (empty($contact['is_error'])) { $url = CRM_Utils_System::url('civicrm/contact/view', 'cid=' . $ba_bao->contact_id); $contacts .= "<li><a href='{$url}'>" . $contact['display_name'] . "</a></li>"; } } CRM_Core_Session::setStatus(sprintf($message, $contacts), ts('Warning'), 'warn'); } else { CRM_Core_Session::setStatus(ts("The account information of this contact was saved."), ts('Account saved'), 'info'); } } }
public function getOrCreateBankAccount(&$coda_tx) { $refs = array(); if (isset($coda_tx->iban) && !empty($coda_tx->iban)) { $refs['iban'] = $coda_tx->iban; } if (isset($coda_tx->bban) && !empty($coda_tx->bban)) { $refs['bban'] = $coda_tx->bban; } if (!array_key_exists('iban', $refs) || !array_key_exists('bban', $refs)) { //todo take the main ba } foreach ($refs as $type => $ref) { $bank_account_refs = array(); $breftypeid = $this->_ba_ref_types[$type]['value']; $options = array('reference_type_id' => $breftypeid, 'reference' => $ref, 'version' => 3); $result = civicrm_api('banking_account_reference', 'get', $options); if ($result['count'] != 0) { $bank_account_refs[$type] = $result['values'][$result['id']]; } } /* if(isset($coda_tx->bic) && !empty($coda_tx->bic)){ $refs['bic'] = $coda_tx->bic; } */ if (empty($bank_account_refs)) { $bank_account = new CRM_Banking_BAO_BankAccount(); $bank_account->description = $coda_tx->name; $data_raw = array('name' => $coda_tx->name, 'info_msg' => $coda_tx->info_message); $data_parsed = array('name' => $coda_tx->name, 'street_address' => trim($coda_tx->streetname . ' ' . $coda_tx->streetnumber), 'postal_code' => $coda_tx->postal_code, 'city' => $coda_tx->city, 'country_code' => $coda_tx->country_code, 'bic' => $coda_tx->bic); $bank_account->created_date = date('YmdHis'); $bank_account->modified_date = date('YmdHis'); $bank_account->data_raw = json_encode($data_raw); $bank_account->data_parsed = json_encode($data_parsed); $bank_account->save(); $ma = new CRM_Banking_Helpers_MatchAddress($bank_account); if ($ma->findAddress()) { $ma->updateDataParsed(); } } else { $ba_ref = each($bank_account_refs); $result = civicrm_api('banking_account', 'get', array('id' => $ba_ref['id'], 'version' => 3)); $bank_account = (object) $result['values'][$result['id']]; } foreach ($refs as $type => $ref) { if (!array_key_exists($type, $bank_account_refs)) { $bank_account_ref = new CRM_Banking_BAO_BankAccountReference(); $bank_account_ref->reference = $coda_tx->{$type}; $bank_account_ref->reference_type_id = $this->_ba_ref_types[$type]['value']; $bank_account_ref->ba_id = $bank_account->id; $bank_account_ref->save(); } } return $bank_account->id; }