From 132e9f135e9442743b380ca36f960742655a838a Mon Sep 17 00:00:00 2001 From: ErikHommel Date: Thu, 27 Jan 2022 16:11:28 +0100 Subject: [PATCH] genereer lidnummers --- CRM/Bij1migratie/Lidnummer.php | 88 +++++++++++++++++++ CRM/Bij1migratie/Upgrader.php | 37 ++++++++ api/v3/LidNummer/Genereer.php | 20 +++++ .../phpunit/api/v3/LidNummer/GenereerTest.php | 53 +++++++++++ 4 files changed, 198 insertions(+) create mode 100644 CRM/Bij1migratie/Lidnummer.php create mode 100644 api/v3/LidNummer/Genereer.php create mode 100644 tests/phpunit/api/v3/LidNummer/GenereerTest.php diff --git a/CRM/Bij1migratie/Lidnummer.php b/CRM/Bij1migratie/Lidnummer.php new file mode 100644 index 0000000..14ece51 --- /dev/null +++ b/CRM/Bij1migratie/Lidnummer.php @@ -0,0 +1,88 @@ + + * @date 27 Jan 2022 + * @license AGPL-3.0 + */ +class CRM_Bij1migratie_Lidnummer { + + /** + * Method om lidnummers te genereren + * + * @return array + */ + public function genereer() { + $sylvanaId = $this->vindSylvana(); + // selecteer iedereen met een inschrijfdatum (behalve Sylvana) + if ($sylvanaId) { + $this->bewaar($sylvanaId, 1); + $query = "SELECT entity_id AS contact_id + FROM civicrm_value_airtable_data + WHERE entity_id != %1 AND inschrijvingsdatum IS NOT NULL + ORDER BY inschrijvingsdatum"; + $contact = CRM_Core_DAO::executeQuery($query, [1 => [$sylvanaId, "Integer"]]); + } + else { + $query = "SELECT entity_id AS contact_id + FROM civicrm_value_airtable_data + WHERE inschrijvingsdatum IS NOT NULL ORDER BY inschrijvingsdatum"; + $contact = CRM_Core_DAO::executeQuery($query); + } + $maxLid = (int) Civi::settings()->get('bij1_laatste_lidnummer'); + while ($contact->fetch()) { + $maxLid++; + $this->bewaar((int) $contact->contact_id, $maxLid); + } + Civi::settings()->set("bij1_laatste_lidnummer", $maxLid); + } + + /** + * Method om contact ID van Sylvana Simons te vinden + * + * @return int|FALSE + */ + private function vindSylvana() { + try { + $contacts = \Civi\Api4\Contact::get(FALSE) + ->addSelect('id') + ->addJoin('Email AS email', 'INNER', ['id', '=', 'email.contact_id']) + ->addWhere('contact_type', '=', 'Individual') + ->addWhere('first_name', '=', 'Sylvana') + ->addWhere('last_name', '=', 'Simons') + ->addWhere('email.email', '=', 'sylvana@bij1.org') + ->execute(); + $contact = $contacts->first(); + if (isset($contact['id'])) { + return (int) $contact['id']; + } + } + catch (API_Exception $ex) { + Civi::log()->warning(E::ts("Kon geen contact ID voor Sylvana Simons vinden, er wordt geen lidnummer 1 aan haar verstrekt.")); + } + return FALSE; + } + + /** + * Method om lidnummer te bewaren bij contact + */ + public function bewaar(int $contactId, int $lidNummer) { + $countQuery = "SELECT COUNT(*) FROM civicrm_value_lid_data WHERE entity_id = %1"; + $count = CRM_Core_DAO::singleValueQuery($countQuery, [1 => [$contactId, "Integer"]]); + if ($count == 0) { + $bijwerken = "INSERT INTO civicrm_value_lid_data (entity_id, lidnummer) VALUES(%1, %2)"; + } + else { + $bijwerken = "UPDATE civicrm_value_lid_data SET lidnummer = %2 WHERE entity_id = %1"; + } + $bijwerkenParams = [ + 1 => [$contactId, "Integer"], + 2 => [$lidNummer, "Integer"], + ]; + CRM_Core_DAO::executeQuery($bijwerken, $bijwerkenParams); + } + +} diff --git a/CRM/Bij1migratie/Upgrader.php b/CRM/Bij1migratie/Upgrader.php index 7ccba77..8e4298d 100644 --- a/CRM/Bij1migratie/Upgrader.php +++ b/CRM/Bij1migratie/Upgrader.php @@ -21,6 +21,43 @@ class CRM_Bij1migratie_Upgrader extends CRM_Bij1migratie_Upgrader_Base { 'description' => "Deze groep bevat alle contacten aangemaakt tijdens de migratie naar CiviCRM", ]); } + $this->geplandeTaakLidnummers(); + } + + /** + * Upgrade 1000: geplande taak voor het genereren van lidnummers + * + * @return TRUE on success + * @throws Exception + */ + public function upgrade_1000() { + $this->ctx->log->info('Applying update 1000 - geplande taak voor het genereren van lidnummers'); + $this->geplandeTaakLidnummers(); + return TRUE; + } + + /** + * Method om geplande taak voor het genereren van lidnummers toe te voegen + */ + public function geplandeTaakLidnummers() { + $countQuery = "SELECT * FROM civicrm_job WHERE api_entity = %1 AND api_action = %2"; + $count = CRM_Core_DAO::singleValueQuery($countQuery, [ + 1 => ["Lidnummer", "String"], + 2 => ["genereer", "String"], + ]); + if ($count == 0) { + $insert = "INSERT INTO civicrm_job (domain_id, run_frequency, name, description, api_entity, api_action, is_active) + VALUES(%1, %2, %3, %4, %5, %6, %7)"; + CRM_Core_DAO::executeQuery($insert, [ + 1 => [1, "Integer"], + 2 => ["Yearly", "String"], + 3 => ["BIJ1 Genereer lidnummers gelijk na migratie", "String"], + 4 => ["Deze taak genereert nieuwe lidnummers op basis van inschrijvingsdatum", "String"], + 5 => ["Lidnummer", "String"], + 6 => ["genereer", "String"], + 7 => [0, "Integer"], + ]); + } } /** diff --git a/api/v3/LidNummer/Genereer.php b/api/v3/LidNummer/Genereer.php new file mode 100644 index 0000000..a406189 --- /dev/null +++ b/api/v3/LidNummer/Genereer.php @@ -0,0 +1,20 @@ +genereer(); + return civicrm_api3_create_success([], $params, 'LidNummer', 'Genereer'); +} diff --git a/tests/phpunit/api/v3/LidNummer/GenereerTest.php b/tests/phpunit/api/v3/LidNummer/GenereerTest.php new file mode 100644 index 0000000..0288862 --- /dev/null +++ b/tests/phpunit/api/v3/LidNummer/GenereerTest.php @@ -0,0 +1,53 @@ +installMe(__DIR__) + ->apply(); + } + + /** + * The setup() method is executed before the test is executed (optional). + */ + public function setUp() { + parent::setUp(); + } + + /** + * The tearDown() method is executed after the test was executed (optional) + * This can be used for cleanup. + */ + public function tearDown() { + parent::tearDown(); + } + + /** + * Simple example test case. + * + * Note how the function name begins with the word "test". + */ + public function testApiExample() { + $result = civicrm_api3('LidNummer', 'genereer', array('magicword' => 'sesame')); + $this->assertEquals('Twelve', $result['values'][12]['name']); + } + +}