Exemple #1
0
/**
 * ContributionRecur.LinkEspadon API
 * Specific AIVL API for migration purposes
 *
 * Selects all contacts from mandates with Migratie 2015 and then:
 * - if contact has more than 1 recurring with same campaign, log and do nothing
 * - if contact has 1 recurring contribution with campaign, find all contributions where:
 *   - financial_type_id = 4
 *   - contact_id is correct
 *   - is_test = 0
 *   - campaign_id = campaign of recurring
 *   - receive_date >= start date recurring
 *   and update the recurring id in the contributions
 *
 * @author Erik Hommel (CiviCooP) <*****@*****.**>
 * @param array $params
 * @return array API result descriptor
 * @see civicrm_api3_create_success
 * @see civicrm_api3_create_error
 * @throws API_Exception
 */
function civicrm_api3_contribution_recur_linkespadon($params)
{
    $logger = $logger = new CRM_MigrateLogger("link_contrib_log");
    $returnValues = array();
    $querySDD = "SELECT r.contact_id, r.id AS recur_id, r.start_date, r.campaign_id\n    FROM civicrm_sdd_mandate s LEFT JOIN civicrm_contribution_recur r ON s.entity_id = r.id\n    WHERE s.source = %1 AND entity_table = %2";
    $paramsSDD = array(1 => array("Migratie 2015", "String"), 2 => array("civicrm_contribution_recur", "String"));
    $daoSDD = CRM_Core_DAO::executeQuery($querySDD, $paramsSDD);
    while ($daoSDD->fetch()) {
        if (_canRecurBeProcessed($daoSDD) == TRUE) {
            $contribQuery = "UPDATE civicrm_contribution SET contribution_recur_id = %1 WHERE contact_id = %2\n        AND financial_type_id = %3 AND campaign_id = %4 AND is_test = %5 AND receive_date >= %6";
            $contribParams = array(1 => array($daoSDD->recur_id, "Integer"), 2 => array($daoSDD->contact_id, "Integer"), 3 => array(4, "Integer"), 4 => array($daoSDD->campaign_id, "Integer"), 5 => array(0, "Integer"), 6 => array(date("Ymd", strtotime($daoSDD->start_date)), "Date"));
            CRM_Core_DAO::executeQuery($contribQuery, $contribParams);
        } else {
            $logger->logMessage("Waarschuwing", "Contact " . $daoSDD->contact_id . " heeft meer dan 1 recurring contribution met dezelfde campaign, niet verwerkt!");
        }
    }
    return civicrm_api3_create_success($returnValues, $params, 'ContributionRecur', 'LinkEspadon');
}
Exemple #2
0
/**
 * ContributionRecur.FixAmount API
 *
 * @param array $params
 * @return array API result descriptor
 * @see civicrm_api3_create_success
 * @see civicrm_api3_create_error
 * @throws API_Exception
 */
function civicrm_api3_contribution_recur_fixamount($params)
{
    $logger = $logger = new CRM_MigrateLogger("fix_amount_log");
    $returnValues = array();
    $querySDD = "SELECT r.contact_id, r.id AS recur_id, s.reference, c.external_identifier\n    FROM civicrm_sdd_mandate s LEFT JOIN civicrm_contribution_recur r ON s.entity_id = r.id\n    JOIN civicrm_contact c ON r.contact_id = c.id WHERE s.source = %1 AND s.entity_table = %2";
    $paramsSDD = array(1 => array("Migratie 2015", "String"), 2 => array("civicrm_contribution_recur", "String"));
    $daoSDD = CRM_Core_DAO::executeQuery($querySDD, $paramsSDD);
    while ($daoSDD->fetch()) {
        $newAmount = _shouldAmountBeChanged($daoSDD);
        if ($newAmount) {
            $updateRecur = "UPDATE civicrm_contribution_recur SET amount = %1 WHERE id = %2";
            $paramsRecur = array(1 => array($newAmount, "Money"), 2 => array($daoSDD->recur_id, "Integer"));
            CRM_Core_DAO::executeQuery($updateRecur, $paramsRecur);
            $logger->logMessage("INFO", "Bedrag in recurring " . $daoSDD->recur_id . " op " . $newAmount . " gezet (mandaat " . $daoSDD->reference . " en externe Id " . $daoSDD->external_identifier . ")");
        }
    }
    return civicrm_api3_create_success($returnValues, $params, 'ContributionRecur', 'FixAmount');
}
Exemple #3
0
/**
 * Espadon.Migrate API
 * Amnesty International Flanders Migrate Recurring Contribution/SDD Mandate from Espadon
 *
 * @author Erik Hommel (CiviCooP) <*****@*****.**>
 * @param array $params
 * @return array API result descriptor
 * @see civicrm_api3_create_success
 * @see civicrm_api3_create_error
 * @throws API_Exception
 */
function civicrm_api3_espadon_migrate($params)
{
    $logger = new CRM_MigrateLogger();
    $errors = 0;
    $warnings = 0;
    $processed = 0;
    $migrated = 0;
    if (isset($params['limit'])) {
        $limit = $params['limit'];
    } else {
        $limit = 2500;
    }
    $queryEspadon = "SELECT * FROM espadon_data WHERE Statuut_detail = %1 AND Frequentie != %2 AND processed = %3 LIMIT " . $limit;
    $paramsEspadon = array(1 => array("Lopend", "String"), 2 => array("OOFF", "String"), 3 => array(0, "Integer"));
    $daoEspadon = CRM_Core_DAO::executeQuery($queryEspadon, $paramsEspadon);
    while ($daoEspadon->fetch()) {
        _setEspadonProcessed($daoEspadon->espadon_id);
        $processed++;
        if (empty($daoEspadon->DatumeersteBetaling) && empty($datumLaatsteBetaling)) {
            $logger->logMessage("Info", "Espadon mandaat " . $daoEspadon->Mandate . " overgeslagen omdat datum eerste betaling EN datum laatste betaling leeg");
        } else {
            $daoEspadon->campaignId = _getCampaignIdWithBron($daoEspadon->Bron, $logger, $warnings);
            $daoEspadon->Mandate = _correctMandate($daoEspadon->Mandate);
            $daoEspadon->Iban = _correctIban($daoEspadon->Iban);
            $daoEspadon->contactId = _getContactIdWithExternalId($daoEspadon->ExterneId, $logger, $warnings);
            if (!empty($daoEspadon->contactId)) {
                _processEspadonRecord($daoEspadon, $logger, $warnings, $errors);
                $migrated++;
            } else {
                $daoEspadon->contactId = _getContactIdWithMandate($daoEspadon->Mandate, $logger, $errors);
                if (!empty($daoEspadon->contactId)) {
                    _processEspadonRecord($daoEspadon, $logger, $warnings, $errors);
                    $migrated++;
                }
            }
        }
    }
    $returnValues = array("Migratie klaar, " . $processed . " records uit Espadon verwerkt met " . $errors . " fouten, " . $warnings . " waarschuwingen en " . $migrated . " records gemigreerd");
    return civicrm_api3_create_success($returnValues, $params, 'Espadon', 'Migrate');
}
Exemple #4
0
function civicrm_api3_contribution_migrate($params)
{
    $logger = new CRM_MigrateLogger();
    $migrated = array();
    if (!array_key_exists('option.limit', $params)) {
        $logger->logMessage('Warning', 'option.limit defaults to 1');
        $params["option.limit"] = 1;
    } else {
        $logger->logMessage('Warning', 'option.limit set to ' . $params['option.limit']);
    }
    $contributionQuery = "SELECT c.contact_id, c.id, c.total_amount AS amount, c.campaign_id, p.id AS pledge_id,\n    p.status_id,p.frequency_unit, p.frequency_interval, p.frequency_day,p.start_date, p.create_date,\n    p.acknowledge_date,p.end_date,p.cancel_date, m.bankname_pledge_7 AS bank, m.mandate_3 AS mandate,\n    iban_pledge_5 AS iban,bic_pledge_6 AS bic\n    FROM civicrm_contribution AS c\n    LEFT JOIN civicrm_pledge AS p ON p.campaign_id=c.campaign_id AND  p.contact_id = c.contact_id\n    LEFT JOIN civicrm_campaign ON civicrm_campaign.id=c.campaign_id\n    LEFT JOIN civicrm_value_sepa_direct_debit_2 AS m ON m.entity_id=p.id\n    WHERE c.financial_type_id=4 AND p.original_installment_amount = c.total_amount AND c.total_amount > 0\n      AND contribution_recur_id IS NULL AND c.campaign_id IS NOT NULL ORDER BY receive_date DESC\n    LIMIT %1";
    $contributionParams = array(1 => array($params['option.limit'], 'Integer'));
    $c = CRM_Core_DAO::executeQuery($contributionQuery, $contributionParams);
    while ($c->fetch()) {
        if ($c->frequency_unit == "quarter") {
            $logger->logMessage("Warning", "Skipping quarterly pledge " . $c->pledge_id . " for contact " . $c->contact_id . " with mandate " . $c->mandate);
            continue;
        }
        if (in_array($c->pledge_id, $migrated)) {
            continue;
            //this contribution is part of a pledge that has already been migrated in this batch
        }
        $logger->logMessage("Info", "Processing pledge " . $c->pledge_id . " for contact " . $c->contact_id . " with mandate " . $c->mandate);
        $migrated[] = $c->pledge_id;
        $recurParams = array("contact_id" => $c->contact_id, "amount" => $c->amount, "currency" => "EUR", "contribution_status_id" => $c->status_id, "frequency_interval" => $c->frequency_interval, "frequency_unit" => $c->frequency_unit, "cycle_day" => $c->frequency_day, "start_date" => $c->start_date, "create_date" => $c->create_date, "end_date" => $c->end_date, "cancel_date" => $c->cancel_date, "financial_type_id" => 4, "payment_instrument_id" => (int) CRM_Core_OptionGroup::getValue('payment_instrument', 'RCUR', 'name'), "status" => 'RCUR', "campaign_id" => $c->campaign_id, "sequential" => 0);
        $cr = civicrm_api3("ContributionRecur", "create", $recurParams);
        $logger->logMessage("Info", "Created recurring contribution " . $cr['id'] . " for pledge " . $c->pledge_id . " and contact_id " . $c->contact_id);
        /*
         * validate IBAN and BIC
         */
        $verifyIBAN = CRM_Sepa_Logic_Verification::verifyIBAN($c->iban);
        if ($verifyIBAN) {
            $logger->logError("IBAN is invalid, emptied mandate IBAN and BIC", $c->contact_id, $c->pledge_id, $cr['id'], $c->campaign_id, "IBAN: " . $c->iban);
            $c->iban = "";
            $c->bic = "";
            $reference = "INVIBAN" . $c->mandate . ": " . $cr["id"];
        } else {
            $verifyBIC = CRM_Sepa_Logic_Verification::verifyBIC($c->bic);
            if ($verifyBIC == "BIC is not correct") {
                // try lookup with Iban
                try {
                    $result = civicrm_api3('Bic', 'getfromiban', array('iban' => $c->iban));
                    $logger->logError("BIC was invalid, replaced with look up of BIC", $c->contact_id, $c->pledge_id, $cr['id'], $c->campaign_id, "BIC: " . $c->bic . " with IBAN: " . $c->iban);
                    $c->bic = $result['bic'];
                    $reference = $c->mandate;
                } catch (CiviCRM_API3_Exception $ex) {
                    $logger->logError("BIC is invalid and lookup failed, emptied BIC", $c->contact_id, $c->pledge_id, $cr['id'], $c->campaign_id, "BIC: " . $c->bic . " with IBAN: " . $c->iban);
                    $c->bic = "";
                    $reference = "INVBIC" . $c->mandate . ": " . $cr["id"];
                }
            }
        }
        /*
         * add check on length of reference
         */
        if (strlen($reference) > 35) {
            $logger->logError("Mandate was more than 35 characters, will be truncated to 35", $c->contact_id, $c->pledge_id, $cr['id'], $c->campaign_id, "Mandate was " . $reference . " and will be " . substr($reference, 0, 35));
            $reference = substr($reference, 0, 35);
        }
        $mandate = array("contact_id" => $c->contact_id, "entity_table" => "civicrm_contribution_recur", "entity_id" => $cr["id"], "source" => "pledge:" . $c->pledge_id, "creditor_id" => 2, "type" => "RCUR", "status" => "RCUR", "creation_date" => $c->create_date, "validation_date" => $c->start_date, "iban" => $c->iban, "bic" => $c->bic, "reference" => $reference, "bank" => $c->bank, "sequential" => 0);
        try {
            $r = civicrm_api3("SepaMandate", "create", $mandate);
            $logger->logMessage("Info", "Added mandate " . $reference . " for contact " . $c->contact_id . " recurring contribution " . $cr['id'] . " and pledge " . $c->pledge_id);
        } catch (Exception $e) {
            $mandate = array("contact_id" => $c->contact_id, "entity_table" => "civicrm_contribution_recur", "entity_id" => $cr["id"], "source" => "pledge:" . $c->pledge_id, "creditor_id" => 2, "type" => "RCUR", "status" => "RCUR", "creation_date" => $c->create_date, "validation_date" => $c->start_date, "iban" => $c->iban, "bic" => $c->bic, "bank" => $c->bank, "sequential" => 0);
            if (empty($c->iban)) {
                $mandate['reference'] = "DUP " . $reference;
            } else {
                $mandate['reference'] = "DUP" . $reference . ": " . $cr["id"];
            }
            $logger->logError("Creating duplicate mandate", $c->contact_id, $c->pledge_id, $cr['id'], $c->campaign_id, "Original mandate is " . $c->mandate . ", duplicate mandate is " . $mandate['reference']);
            $r = civicrm_api3("SepaMandate", "create", $mandate);
        }
        $updateQuery = "UPDATE civicrm_contribution SET contribution_recur_id = %1\n      WHERE contact_id = %2 AND financial_type_id = %3 AND total_amount = %4";
        $updateParams = array(1 => array($cr['id'], 'Integer'), 2 => array($c->contact_id, 'Integer'), 3 => array(4, 'Integer'), 4 => array($c->amount, 'Money'));
        $t = CRM_Core_DAO::executeQuery($updateQuery, $updateParams);
        $logger->logMessage("Info", "migrated contributions/pledge for " . $c->contact_id);
    }
}