wip indelen leden

This commit is contained in:
Erik Hommel 2022-01-06 16:07:43 +00:00
parent ce4e2d0bb6
commit 5b6c7c6323
7 changed files with 261 additions and 2 deletions

View File

@ -20,7 +20,7 @@ class CRM_Bij1migratie_Activiteit {
WHERE cog.name = %1 AND cov.name = %2";
$this->_gereedActivityStatusId = (int) CRM_Core_DAO::singleValueQuery($query, [
1 => ["activity_status", "String"],
3 => ["Completed", "String"],
2 => ["Completed", "String"],
]);
}

View File

@ -147,4 +147,53 @@ class CRM_Bij1migratie_Contact {
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;
}
}

64
api/v3/Lid/Indelen.php Normal file
View File

@ -0,0 +1,64 @@
<?php
use CRM_Bij1migratie_ExtensionUtil as E;
/**
* Lid.Indelen API specification (optional)
* This is used for documentation and validation.
*
* @param array $spec description of fields supported by this API call
*/
function _civicrm_api3_lid_Indelen_spec(&$spec) {
$spec['max_id']['api_required'] = 1;
}
/**
* Lid.Indelen API
* Haal alle contacten die een bijdrage hebben en:
* - bekijk of ze het basisbedrag betaald hebben in 2021, zo ja zet in standaard leden en haal uit nieuwe leden
* - zo nee:
* - zet in ex-leden als ze helemaal geen betalingen in 2021 hebben
* - zet in groep nog in te delen leden als ze wel in 2021 betaald hebben
* - haal ze uit nieuwe leden
*
* @param array $params
*
* @return array
* API result descriptor
*
* @see civicrm_api3_create_success
*
* @throws API_Exception
*/
function civicrm_api3_lid_Indelen($params) {
$maxId = (int) $params['max_id'];
$vanDatum = new DateTime('2021-01-01');
$totDatum = new DateTime('2021-12-31');
$contact = new CRM_Bij1migratie_Contact();
$groep = new CRM_Bij1algemeen_Groep();
$groep->maakGroep([
'name' => "bij1_nog_in_te_delen",
'title' => "Nog in te delen na migratie",
'description' => "Deze groep bevat alle contacten die betaald hebben waarvan nog geen groepsindeling duidelijk is",
]);
$nogInTeDelenGroepId = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_group WHERE name = %1", [1 => ["bij1_nog_in_te_delen", "String"]]);
$returnValues = [];
$lid = CRM_Core_DAO::executeQuery("SELECT DISTINCT(contact_id) FROM civicrm_contribution WHERE contact_id <= %1 ORDER BY contact_id", [1 => [$maxId, "Integer"]]);
while ($lid->fetch()) {
if ($contact->heeftRecentBetaald((int) $lid->contact_id)) {
$groep->plaatsInGroep((int) $lid->contact_id, Civi::service('bij1Algemeen')->getNieuweLedenGroepId(), $contact->haalInschrijvingsDatum((int) $lid->contact_id));
$returnValues[] = "Contact met ID " . $lid->contact_id . " in nieuwe leden geplaatst.";
}
else {
if ($contact->heeftBasisBetaald((int) $lid->contact_id, $vanDatum, $totDatum)) {
$groep->plaatsInGroep((int) $lid->contact_id, Civi::service('bij1Algemeen')->getStandaardLedenGroepId(), $vanDatum);
$groep->verwijderUitGroep((int) $lid->contact_id, Civi::service('bij1Algemeen')->getNieuweLedenGroepId());
$returnValues[] = "Contact met ID " . $lid->contact_id . " in standaard leden geplaatst.";
}
else {
$groep->plaatsInGroep((int) $lid->contact_id, $nogInTeDelenGroepId);
$groep->verwijderUitGroep((int) $lid->contact_id, Civi::service('bij1Algemeen')->getNieuweLedenGroepId());
}
}
}
return civicrm_api3_create_success($returnValues, $params, 'Lid', 'Indelen');
}

View File

@ -1,6 +1,16 @@
<?php
use CRM_Bij1migratie_ExtensionUtil as E;
/**
* LidDatum.Fix API specification (optional)
* This is used for documentation and validation.
*
* @param array $spec description of fields supported by this API call
*/
function _civicrm_api3_lid_datum_Fix_spec(&$spec) {
$spec['max_id']['api_required'] = 1;
}
/**
* LidDatum.Fix API
* haal contacten met betalingen en check:
@ -22,7 +32,8 @@ function civicrm_api3_lid_datum_Fix($params) {
$returnValues = [];
$contact = new CRM_Bij1migratie_Contact();
$activiteit = new CRM_Bij1migratie_Activiteit();
$lid = CRM_Core_DAO::executeQuery("SELECT DISTINCT(contact_id) FROM civicrm_contribution");
$maxId = (int) $params['max_id'];
$lid = CRM_Core_DAO::executeQuery("SELECT DISTINCT(contact_id) FROM civicrm_contribution WHERE contact_id <= %1 ORDER BY contact_id", [1 => [$maxId, "Integer"]]);
while ($lid->fetch()) {
$inschrijvingsDatum = $contact->haalInschrijvingsDatum((int) $lid->contact_id);
if ($inschrijvingsDatum) {

18
phpunit.xml.dist Normal file
View File

@ -0,0 +1,18 @@
<?xml version="1.0"?>
<phpunit backupGlobals="false" backupStaticAttributes="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" bootstrap="tests/phpunit/bootstrap.php">
<testsuites>
<testsuite name="My Test Suite">
<directory>./tests/phpunit</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">./</directory>
</whitelist>
</filter>
<listeners>
<listener class="Civi\Test\CiviTestListener">
<arguments/>
</listener>
</listeners>
</phpunit>

View File

@ -0,0 +1,53 @@
<?php
use Civi\Test\HeadlessInterface;
use Civi\Test\HookInterface;
use Civi\Test\TransactionalInterface;
/**
* Lid.Indelen API Test Case
* This is a generic test class implemented with PHPUnit.
* @group headless
*/
class api_v3_Lid_IndelenTest 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('Lid', 'indelen', array('magicword' => 'sesame'));
$this->assertEquals('Twelve', $result['values'][12]['name']);
}
}

View File

@ -0,0 +1,64 @@
<?php
ini_set('memory_limit', '2G');
// phpcs:disable
eval(cv('php:boot --level=classloader', 'phpcode'));
// phpcs:enable
// Allow autoloading of PHPUnit helper classes in this extension.
$loader = new \Composer\Autoload\ClassLoader();
$loader->add('CRM_', __DIR__);
$loader->add('Civi\\', __DIR__);
$loader->add('api_', __DIR__);
$loader->add('api\\', __DIR__);
$loader->register();
/**
* Call the "cv" command.
*
* @param string $cmd
* The rest of the command to send.
* @param string $decode
* Ex: 'json' or 'phpcode'.
* @return mixed
* Response output (if the command executed normally).
* For 'raw' or 'phpcode', this will be a string. For 'json', it could be any JSON value.
* @throws \RuntimeException
* If the command terminates abnormally.
*/
function cv(string $cmd, string $decode = 'json') {
$cmd = 'cv ' . $cmd;
$descriptorSpec = [0 => ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => STDERR];
$oldOutput = getenv('CV_OUTPUT');
putenv('CV_OUTPUT=json');
// Execute `cv` in the original folder. This is a work-around for
// phpunit/codeception, which seem to manipulate PWD.
$cmd = sprintf('cd %s; %s', escapeshellarg(getenv('PWD')), $cmd);
$process = proc_open($cmd, $descriptorSpec, $pipes, __DIR__);
putenv("CV_OUTPUT=$oldOutput");
fclose($pipes[0]);
$result = stream_get_contents($pipes[1]);
fclose($pipes[1]);
if (proc_close($process) !== 0) {
throw new RuntimeException("Command failed ($cmd):\n$result");
}
switch ($decode) {
case 'raw':
return $result;
case 'phpcode':
// If the last output is /*PHPCODE*/, then we managed to complete execution.
if (substr(trim($result), 0, 12) !== '/*BEGINPHP*/' || substr(trim($result), -10) !== '/*ENDPHP*/') {
throw new \RuntimeException("Command failed ($cmd):\n$result");
}
return $result;
case 'json':
return json_decode($result, 1);
default:
throw new RuntimeException("Bad decoder format ($decode)");
}
}