genereer lidnummers
This commit is contained in:
parent
e083872465
commit
132e9f135e
|
@ -0,0 +1,88 @@
|
|||
<?php
|
||||
use CRM_Bij1migratie_ExtensionUtil as E;
|
||||
|
||||
/**
|
||||
* Class voor BIJ1 migratie genereer lidnummer
|
||||
*
|
||||
* @author Erik Hommel (CiviCooP) <erik.hommel@civicoop.org>
|
||||
* @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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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"],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
use CRM_Bij1migratie_ExtensionUtil as E;
|
||||
|
||||
/**
|
||||
* LidNummer.Genereer API (eenmalig API na migratie om lidnummer te genereren op basis van inschrijfdatum)
|
||||
*
|
||||
* @param array $params
|
||||
*
|
||||
* @return array
|
||||
* API result descriptor
|
||||
*
|
||||
* @see civicrm_api3_create_success
|
||||
*
|
||||
* @throws API_Exception
|
||||
*/
|
||||
function civicrm_api3_lid_nummer_Genereer($params) {
|
||||
$lidnummer = new CRM_Bij1migratie_Lidnummer();
|
||||
$lidnummer->genereer();
|
||||
return civicrm_api3_create_success([], $params, 'LidNummer', 'Genereer');
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
<?php
|
||||
|
||||
use Civi\Test\HeadlessInterface;
|
||||
use Civi\Test\HookInterface;
|
||||
use Civi\Test\TransactionalInterface;
|
||||
|
||||
/**
|
||||
* LidNummer.Genereer API Test Case
|
||||
* This is a generic test class implemented with PHPUnit.
|
||||
* @group headless
|
||||
*/
|
||||
class api_v3_LidNummer_GenereerTest extends \PHPUnit\Framework\TestCase implements HeadlessInterface, HookInterface, TransactionalInterface {
|
||||
use \Civi\Test\Api3TestTrait;
|
||||
|
||||
/**
|
||||
* Set up for headless tests.
|
||||
*
|
||||
* Civi\Test has many helpers, like install(), uninstall(), sql(), and sqlFile().
|
||||
*
|
||||
* See: https://docs.civicrm.org/dev/en/latest/testing/phpunit/#civitest
|
||||
*/
|
||||
public function setUpHeadless() {
|
||||
return \Civi\Test::headless()
|
||||
->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']);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue