2021-12-09 16:41:13 +00:00
|
|
|
<?php
|
|
|
|
use CRM_Bij1migratie_ExtensionUtil as E;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verwerking van migratie
|
|
|
|
*/
|
|
|
|
class CRM_Bij1migratie_Migratie {
|
|
|
|
/**
|
|
|
|
* Method om data te migreren
|
|
|
|
*
|
|
|
|
* @param string $migratieType
|
|
|
|
* @param string $tableName
|
|
|
|
* @throws
|
|
|
|
*/
|
|
|
|
public static function migreer(string $migratieType, string $tableName) {
|
|
|
|
set_time_limit(0);
|
|
|
|
$table = new CRM_Bij1migratie_ImportTable();
|
|
|
|
$data = $table->getAllRows($migratieType, $tableName);
|
|
|
|
$migratieDatum = new DateTime('now');
|
|
|
|
if ($data) {
|
|
|
|
foreach ($data as $rij) {
|
|
|
|
if (!empty($rij)) {
|
|
|
|
try {
|
2021-12-22 15:53:48 +00:00
|
|
|
switch ($migratieType) {
|
|
|
|
case "A":
|
|
|
|
$contact = new CRM_Bij1migratie_Contact();
|
|
|
|
$rij['locatietype'] = Civi::service('bij1Algemeen')->getDefaultLocationTypeId();
|
|
|
|
// eerst beoordelen of het contact gevonden kan worden met Mollie Customer Id
|
|
|
|
$contactId = $contact->haalContactIdMetMollieCustomerId($rij);
|
|
|
|
// als niks gevonden gebruik Form Processor om contact te vinden of te maken
|
|
|
|
if (!$contactId) {
|
|
|
|
$contactId = $contact->haalOfMaak($rij, $migratieDatum);
|
|
|
|
}
|
|
|
|
if ($contactId) {
|
|
|
|
$rij['contact_id'] = $contactId;
|
|
|
|
// verwerk de rest (email, adres, telefoon, notities, groepen, lidnummer, inschrijfdatum via Form Processor
|
|
|
|
$result = civicrm_api3('FormProcessor', 'bij1_airtable_migratie', $rij);
|
|
|
|
// voeg bankrekening toe indien van toepassing
|
|
|
|
if (isset($rij['iban']) && !empty($rij['iban'])) {
|
|
|
|
self::maakBankrekening($migratieDatum, $contactId, $rij['iban']);
|
|
|
|
}
|
|
|
|
// voeg identifier toe indien nodig
|
|
|
|
if (isset($rij['mollie_customer_id']) && !empty($rij['mollie_customer_id'])) {
|
|
|
|
self::maakMollieCustomerIdIdentifier($contactId, $rij['mollie_customer_id']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "B":
|
|
|
|
if (!self::bestaatBetaling($rij)) {
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "P":
|
|
|
|
$rij['locatietype'] = Civi::service('bij1Algemeen')->getDefaultLocationTypeId();
|
|
|
|
civicrm_api3('FormProcessor', 'bij1_contacten_migratie', $rij);
|
2021-12-09 16:41:13 +00:00
|
|
|
\Civi\Api4\MigratieLog::create()
|
2021-12-22 15:53:48 +00:00
|
|
|
->addValue('mollie_customer_id', $rij['mollie_customer_id'])
|
2021-12-09 16:41:13 +00:00
|
|
|
->addValue('migratiedatum', $migratieDatum->format("y-m-d"))
|
2021-12-22 15:53:48 +00:00
|
|
|
->addValue('type_migratie', $migratieType)
|
|
|
|
->addValue('type_melding', 'succes')
|
|
|
|
->addValue('melding', 'Gemigreerd')
|
2021-12-09 16:41:13 +00:00
|
|
|
->execute();
|
2021-12-22 15:53:48 +00:00
|
|
|
break;
|
2021-12-09 16:41:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (CiviCRM_API3_Exception $ex) {
|
|
|
|
\Civi\Api4\MigratieLog::create()
|
|
|
|
->addValue('mollie_customer_id', $rij['mollie_customer_id'])
|
|
|
|
->addValue('migratiedatum', $migratieDatum->format("y-m-d"))
|
2021-12-22 15:53:48 +00:00
|
|
|
->addValue('type_migratie', $migratieType)
|
|
|
|
->addValue('type_melding', 'fout')
|
2021-12-09 16:41:13 +00:00
|
|
|
->addValue('melding', 'FOUT, melding van FormProcessor API call: ' . $ex->getMessage())
|
|
|
|
->execute();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-22 15:53:48 +00:00
|
|
|
/**
|
|
|
|
* Method om mollie customer id als identifier toe te voegen als die nog niet bestaat (airtable)
|
|
|
|
*
|
|
|
|
* @param int $contactId
|
|
|
|
* @param string $mollieCustomerId
|
|
|
|
*/
|
|
|
|
public static function maakMollieCustomerIdIdentifier(int $contactId, string $mollieCustomerId) {
|
|
|
|
if (!empty($contactId) && !empty($mollieCustomerId)) {
|
|
|
|
$query = "SELECT COUNT(*) FROM civicrm_value_contact_id_history WHERE identifier_type = %1 AND entity_id = %2 AND identifier = %3";
|
|
|
|
$queryParams = [
|
|
|
|
1 => [Civi::service('bij1Algemeen')->getMollieCustomerIdIdentityType(), "String"],
|
|
|
|
2 => [$contactId, "Integer"],
|
|
|
|
3 => [$mollieCustomerId, "String"],
|
|
|
|
];
|
|
|
|
$count = CRM_Core_DAO::singleValueQuery($query, $queryParams);
|
|
|
|
if ($count == 0) {
|
|
|
|
$insert = "INSERT INTO civicrm_value_contact_id_history (entity_id, identifier_type, identifier) VALUES(%2, %1, %3)";
|
|
|
|
CRM_Core_DAO::executeQuery($insert, $queryParams);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-09 16:41:13 +00:00
|
|
|
/**
|
|
|
|
* Method om te beoordelen of gift al bestaat voor contact
|
|
|
|
*
|
|
|
|
* @param $betaling
|
|
|
|
* @return bool
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public static function bestaatBetaling($betaling) {
|
|
|
|
$verplichten = ["contact_id", "donatiebedrag", "donatiedatum"];
|
|
|
|
foreach ($verplichten as $verplicht) {
|
|
|
|
if (!isset($gift[$verplicht]) || empty($gift[$verplicht])) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
$receiveDate = new DateTime($gift['donatiedatum']);
|
|
|
|
$query = "SELECT COUNT(*) FROM civicrm_contribution WHERE contact_id = %1 AND receive_date = %2 AND total_amount = %3";
|
|
|
|
$count = CRM_Core_DAO::singleValueQuery($query, [
|
|
|
|
1 => [(int) $gift['contact_id'], "Integer"],
|
|
|
|
2 => [$receiveDate->format("Y-m-d"), "String"],
|
|
|
|
3 => [$gift['donatiebedrag'], "String"],
|
|
|
|
]);
|
|
|
|
if ($count > 0) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method om te bepalen of de persoon al bestaat
|
|
|
|
*
|
|
|
|
* @param int $ssaskiaNummer
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function bestaatPersoon(int $ssaskiaNummer) {
|
|
|
|
$query = "SELECT COUNT(*) FROM civicrm_value_pax_contact_data WHERE ssaskia_nummer = %1";
|
|
|
|
$count = CRM_Core_DAO::singleValueQuery($query, [1 => [$ssaskiaNummer, "Integer"]]);
|
|
|
|
if ($count > 0) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method om migratie groep aan te maken
|
|
|
|
*/
|
|
|
|
public function maakMigratieGroepen() {
|
|
|
|
$query = "SELECT COUNT(*) FROM civicrm_group WHERE name = %1";
|
|
|
|
$count = CRM_Core_DAO::singleValueQuery($query, [1 => ["pax_migratie_groep", "String"]]);
|
|
|
|
if ($count == 0) {
|
|
|
|
try {
|
|
|
|
\Civi\Api4\Group::create()
|
|
|
|
->addValue('name', 'pax_migratie_groep')
|
|
|
|
->addValue('title', 'CiviCRM Migratie')
|
|
|
|
->addValue('description', 'Groep met personen en organisaties die tijdens de migratie aangemaakt werden')
|
|
|
|
->addValue('is_active', TRUE)
|
|
|
|
->addValue('is_reserved', TRUE)
|
|
|
|
->execute();
|
|
|
|
}
|
|
|
|
catch (API_Exception $ex) {
|
|
|
|
Civi::log()->error(E::ts("Kon geen groep voor migratie aanmaken, fout van API4 Group create: ") . $ex->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$count = CRM_Core_DAO::singleValueQuery($query, [1 => ["pax_dm_mag", "String"]]);
|
|
|
|
if ($count == 0) {
|
|
|
|
try {
|
|
|
|
\Civi\Api4\Group::create()
|
|
|
|
->addValue('name', 'pax_dm_mag')
|
|
|
|
->addValue('title', 'DM Mag')
|
|
|
|
->addValue('description', 'Groep van personen die in sSaskia de vlag DM mag aan hadden staan')
|
|
|
|
->addValue('is_active', TRUE)
|
|
|
|
->addValue('is_reserved', TRUE)
|
|
|
|
->execute();
|
|
|
|
}
|
|
|
|
catch (API_Exception $ex) {
|
|
|
|
Civi::log()->error(E::ts("Kon geen groep voor dm mag aanmaken, fout van API4 Group create: ") . $ex->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$count = CRM_Core_DAO::singleValueQuery($query, [1 => ["pax_post_mag", "String"]]);
|
|
|
|
if ($count == 0) {
|
|
|
|
try {
|
|
|
|
\Civi\Api4\Group::create()
|
|
|
|
->addValue('name', 'pax_post_mag')
|
|
|
|
->addValue('title', 'Post Mag')
|
|
|
|
->addValue('description', 'Groep van personen die in sSaskia de vlag post mag aan hadden staan')
|
|
|
|
->addValue('is_active', TRUE)
|
|
|
|
->addValue('is_reserved', TRUE)
|
|
|
|
->execute();
|
|
|
|
}
|
|
|
|
catch (API_Exception $ex) {
|
|
|
|
Civi::log()->error(E::ts("Kon geen groep voor post mag aanmaken, fout van API4 Group create: ") . $ex->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method om bankrekening toe te voegen
|
|
|
|
*
|
|
|
|
* @param DateTime $migratieDatum
|
2021-12-22 15:53:48 +00:00
|
|
|
* @param int $contactId
|
|
|
|
* @param string $iban
|
2021-12-09 16:41:13 +00:00
|
|
|
* @throws API_Exception
|
|
|
|
*/
|
2021-12-22 15:53:48 +00:00
|
|
|
public static function maakBankrekening(DateTime $migratieDatum, int $contactId, string $iban) {
|
|
|
|
if (!empty($iban) && !empty($contactId)) {
|
|
|
|
$iban = trim($iban);
|
2021-12-09 16:41:13 +00:00
|
|
|
// alleen als rekeningnummer nog niet bestaat voor de donor
|
|
|
|
if (!self::bestaatBankrekening($contactId, $iban)) {
|
|
|
|
try {
|
|
|
|
$rekening = civicrm_api3('BankingAccount', 'create', [
|
|
|
|
'contact_id' => $contactId,
|
2021-12-22 15:53:48 +00:00
|
|
|
'description' => 'rekening vanuit migratie airtable',
|
2021-12-09 16:41:13 +00:00
|
|
|
'created_date' => date('YmdHis'),
|
|
|
|
'data_raw' => '{}',
|
|
|
|
]);
|
|
|
|
// add a reference
|
|
|
|
civicrm_api3('BankingAccountReference', 'create', [
|
2021-12-22 15:53:48 +00:00
|
|
|
'reference' => $iban,
|
|
|
|
'reference_type_id' => Civi::service('bij1Algemeen')->getIbanAccountReference(),
|
2021-12-09 16:41:13 +00:00
|
|
|
'ba_id' => $rekening['id'],
|
|
|
|
]);
|
|
|
|
} catch (CiviCRM_API3_Exception $ex) {
|
|
|
|
\Civi\Api4\MigratieLog::create()
|
2021-12-22 15:53:48 +00:00
|
|
|
->addValue('mollie_customer_id', $contactId)
|
2021-12-09 16:41:13 +00:00
|
|
|
->addValue('migratiedatum', $migratieDatum->format("y-m-d"))
|
2021-12-22 15:53:48 +00:00
|
|
|
->addValue('type_migratie', 'A')
|
|
|
|
->addValue('type_melding', 'fout')
|
|
|
|
->addValue('melding', 'FOUT, geen bankrekening aan kunnen maken voor contact ID ' . $contactId . ' met IBAN ' . $iban)
|
2021-12-09 16:41:13 +00:00
|
|
|
->execute();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method om te bekijken of het rekening nummer al bestaat voor het contact
|
|
|
|
*
|
|
|
|
* @param int $contactId
|
|
|
|
* @param string $rekening
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function bestaatBankrekening(int $contactId, string $rekening) {
|
|
|
|
$query = "SELECT COUNT(*)
|
|
|
|
FROM civicrm_bank_account cba
|
|
|
|
JOIN civicrm_bank_account_reference cbar ON cba.id = cbar.ba_id
|
|
|
|
WHERE cba.contact_id = %1 AND cbar.reference = %2";
|
|
|
|
$count = CRM_Core_DAO::singleValueQuery($query, [
|
|
|
|
1 => [$contactId, "Integer"],
|
|
|
|
2 => [$rekening, "String"],
|
|
|
|
]);
|
|
|
|
if ($count > 0) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|