224 lines
7.6 KiB
PHP
224 lines
7.6 KiB
PHP
<?php
|
|
use CRM_Bij1migratie_ExtensionUtil as E;
|
|
|
|
/**
|
|
* Class voor BIJ1 migratie contact verwerking
|
|
*
|
|
* @author Erik Hommel (CiviCooP) <erik.hommel@civicoop.org>
|
|
* @date 21 dec 2021
|
|
* @license AGPL-3.0
|
|
*/
|
|
class CRM_Bij1migratie_Contact {
|
|
|
|
/**
|
|
* Method om contact ID op te halen met mollie customer id
|
|
*
|
|
* @param string $mollieCustomerId
|
|
* @return false|string
|
|
*/
|
|
public function haalContactIdMetMollieCustomerId(string $mollieCustomerId) {
|
|
if (!empty($mollieCustomerId)) {
|
|
$identifierType = Civi::service('bij1Algemeen')->getMollieCustomerIdIdentityType();
|
|
if ($identifierType) {
|
|
$query = "SELECT entity_id FROM civicrm_value_contact_id_history WHERE identifier_type = %1 AND identifier = %2";
|
|
$contactId = CRM_Core_DAO::singleValueQuery($query, [
|
|
1 => [$identifierType, "String"],
|
|
2 => [$mollieCustomerId, "String"],
|
|
]);
|
|
if ($contactId) {
|
|
return (int) $contactId;
|
|
}
|
|
}
|
|
}
|
|
return FALSE;
|
|
}
|
|
|
|
/**
|
|
* Method om contact met email op te zoeken
|
|
*
|
|
* @param $email
|
|
* @return false|int
|
|
* @throws API_Exception
|
|
*/
|
|
public function haalContactIdMetEmail($email) {
|
|
if (!empty($email)) {
|
|
$contactId = NULL;
|
|
$count = CRM_Core_DAO::singleValueQuery("SELECT COUNT(DISTINCT(contact_id)) FROM civicrm_email WHERE email = %1", [1 => [$email, "String"]]);
|
|
if ($count > 1) {
|
|
$datum = new DateTime();
|
|
\Civi\Api4\MigratieLog::create()
|
|
->addValue('mollie_customer_id', "n.v.t.")
|
|
->addValue('migratiedatum', $datum->format("y-m-d"))
|
|
->addValue('type_migratie', "B")
|
|
->addValue('type_melding', 'waarschuwing')
|
|
->addValue('melding', 'Waarschuwing, meerdere contacten met email: ' . $email . ', betaling aan eerste toegekend. Nakijken!')
|
|
->execute();
|
|
$query = "SELECT contact_id FROM civicrm_email WHERE email = %1 LIMIT 1";
|
|
$contactId = CRM_Core_DAO::singleValueQuery($query, [1 => [$email, "String"]]);
|
|
}
|
|
if ($count == 1) {
|
|
$query = "SELECT contact_id FROM civicrm_email WHERE email = %1";
|
|
$contactId = CRM_Core_DAO::singleValueQuery($query, [1 => [$email, "String"]]);
|
|
}
|
|
if ($contactId) {
|
|
return (int) $contactId;
|
|
}
|
|
}
|
|
return FALSE;
|
|
}
|
|
|
|
/**
|
|
* Method om klant te halen of te maken via de contacten migratie form processor
|
|
*
|
|
* @param array $data
|
|
* @param DateTime $migratieDatum
|
|
* @return false|int
|
|
* @throws API_Exception
|
|
* @throws \Civi\API\Exception\UnauthorizedException
|
|
*/
|
|
public function haalOfMaak(array $data, DateTime $migratieDatum) {
|
|
try {
|
|
$result = civicrm_api3('FormProcessor', 'bij1_contacten_migratie', $data);
|
|
if (isset($result['action'][2]['output']['contact_id'])) {
|
|
return (int) $result['action'][2]['output']['contact_id'];
|
|
}
|
|
}
|
|
catch (CiviCRM_API3_Exception $ex) {
|
|
\Civi\Api4\MigratieLog::create()
|
|
->addValue('mollie_customer_id', $data['mollie_customer_id'])
|
|
->addValue('migratiedatum', $migratieDatum->format("y-m-d"))
|
|
->addValue('type_migratie', "A")
|
|
->addValue('type_melding', 'fout')
|
|
->addValue('melding', 'FOUT, melding van FormProcessor in CRM_Bij1migratie_Contact method haalOfMaak : ' . $ex->getMessage())
|
|
->execute();
|
|
}
|
|
return FALSE;
|
|
}
|
|
|
|
/**
|
|
* Method om de inschrijvingsdatum van een contact te bepalen. Dat is als ingevuld de inschrijvingsdatum uit
|
|
* airtable, anders de eerste betaling die we ontvangen hebben
|
|
*
|
|
* @param int $contactId
|
|
* @return DateTime|false
|
|
* @throws Exception
|
|
*/
|
|
public function haalInschrijvingsDatum(int $contactId) {
|
|
if ($contactId) {
|
|
$query = "SELECT inschrijvingsdatum FROM civicrm_value_airtable_data WHERE entity_id = %1";
|
|
$airTableDatum = CRM_Core_DAO::singleValueQuery($query, [1 => [$contactId, "Integer"]]);
|
|
if ($airTableDatum) {
|
|
$inschrijvingsDatum = new DateTime($airTableDatum);
|
|
return $inschrijvingsDatum;
|
|
}
|
|
else {
|
|
$query = "SELECT MIN(receive_date) FROM civicrm_contribution WHERE contact_id = %1";
|
|
$bijdrageDatum = CRM_Core_DAO::singleValueQuery($query, [1 => [$contactId, "Integer"]]);
|
|
if ($bijdrageDatum) {
|
|
$inschrijvingsDatum = new DateTime($bijdrageDatum);
|
|
return $inschrijvingsDatum;
|
|
}
|
|
}
|
|
}
|
|
return FALSE;
|
|
}
|
|
|
|
/**
|
|
* Probeer frequentie te bepalen aan de hand van de herkomst van de bijdrage
|
|
*
|
|
* @param int $contactId
|
|
* @return string|null
|
|
*/
|
|
public function haalFrequentie(int $contactId) {
|
|
// kijk eerst of er ergens in de omschrijving van de bijdrage iets te vinden is
|
|
$bijdrage = CRM_Core_DAO::executeQuery('SELECT source FROM civicrm_contribution WHERE contact_id = %1', [1 => [$contactId, "Integer"]]);
|
|
while ($bijdrage->fetch()) {
|
|
$herkomst = strtolower($bijdrage->source);
|
|
if (strpos($herkomst, "3-maandelijks") !== FALSE) {
|
|
return "3-maandelijks";
|
|
}
|
|
if (strpos($herkomst, "jaarlijks") !== FALSE) {
|
|
return "jaarlijks";
|
|
}
|
|
if (strpos($herkomst, "maandelijks") !== FALSE) {
|
|
return "maandelijks";
|
|
}
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
/**
|
|
* Method om vast te stellen of het contact het basisbedrag in de periode betaald heeft
|
|
*
|
|
* @param int $contactId
|
|
* @param DateTime $vanDatum
|
|
* @param DateTime $totDatum
|
|
* @return bool
|
|
*/
|
|
public function heeftBasisBetaald(int $contactId, DateTime $vanDatum, DateTime $totDatum) {
|
|
if (!empty($contactId) && !empty($vanDatum) && !empty($totDatum)) {
|
|
$query = "SELECT SUM(total_amount) FROM civicrm_contribution
|
|
WHERE contact_id = %1 AND receive_date BETWEEN %2 AND %3 AND contribution_status_id = %4";
|
|
$betaald = CRM_Core_DAO::singleValueQuery($query, [
|
|
1 => [$contactId, "Integer"],
|
|
2 => [$vanDatum->format('Y-m-d'), "String"],
|
|
3 => [$totDatum->format('Y-m-d'), "String"],
|
|
4 => [Civi::service('bij1Migratie')->getCompletedContributionStatusId(), "Integer"],
|
|
]);
|
|
if ($betaald) {
|
|
if ($betaald >= Civi::settings()->get('bij1_basisbedrag')) {
|
|
return TRUE;
|
|
}
|
|
}
|
|
}
|
|
return FALSE;
|
|
}
|
|
|
|
/**
|
|
* Method om te bekijken of eerste betaling na 1 oktober was ivm groep nieuwe leden
|
|
*
|
|
* @param int $contactId
|
|
* @return bool
|
|
* @throws Exception
|
|
*/
|
|
public function heeftRecentBetaald(int $contactId) {
|
|
if (!empty($contactId)) {
|
|
$peilDatum = new DateTime('2021-10-01');
|
|
$query = "SELECT MIN(receive_date) FROM civicrm_contribution WHERE contact_id = %1";
|
|
$eersteBetaling = CRM_Core_DAO::singleValueQuery($query, [1 => [$contactId, "Integer"]]);
|
|
if ($eersteBetaling) {
|
|
$eersteDatum = new DateTime($eersteBetaling);
|
|
if ($eersteDatum >= $peilDatum) {
|
|
return TRUE;
|
|
}
|
|
}
|
|
}
|
|
return FALSE;
|
|
}
|
|
|
|
/**
|
|
* Method om te bekijken of 3 betalingen mislukt
|
|
*
|
|
* @param int $contactId
|
|
* @param DateTime $vanDatum
|
|
* @param DateTime $totDatum
|
|
* @return bool
|
|
*/
|
|
public function heeftMislukt(int $contactId, DateTime $vanDatum, DateTime $totDatum) {
|
|
if (!empty($contactId)) {
|
|
$query = "SELECT COUNT(*) FROM civicrm_contribution WHERE contact_id = %1 AND contribution_status_id = %2 AND receive_date BETWEEN %3 AND %4";
|
|
$aantalMislukt = CRM_Core_DAO::singleValueQuery($query, [
|
|
1 => [$contactId, "Integer"],
|
|
2 => [Civi::service('bij1Migratie')->getFailedContributionStatusId(), "Integer"],
|
|
3 => [$vanDatum->format("Y-m-d"), "String"],
|
|
4 => [$totDatum->format("Y-m-d"), "String"],
|
|
]);
|
|
if ($aantalMislukt >= 3) {
|
|
return TRUE;
|
|
}
|
|
}
|
|
return FALSE;
|
|
}
|
|
|
|
}
|