Files
CRM_Scripts_NVKH/code-current/archive/script-bereken-nascholingstotalen-oud1.js
T
2026-08-28 11:29:00 +02:00

356 lines
19 KiB
JavaScript

//exec last execution: Fri Jan 20 2023 15:56:21 GMT+0100 (Midden-Europese standaardtijd)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Initialisatie
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
log('warning', 'Doorrekenen voor contact ', $contact_id);
$na_reg_dt = record\attribute('Contact', $contact_id, 'datumRegistratie');
$na_reg_jr = datetime\year($na_reg_dt);
$na_methode_1_max_jaar = configGet('berekening-methode-1-tot-en-met');
$na_methode_1_max_surplus = configGet('berekening-methode-1-max-surplus');
$na_style_rubric = configGet('nascholingstotaal-rapport-jaar-rubric-style');
$na_style_dagen = configGet('nascholingstotaal-rapport-jaar-dagen-style');
$na_style_dagen_inspr = configGet('nascholingstotaal-rapport-jaar-dagen-style-inspr');
$na_style_nstd_goed = configGet('nascholingstotaal-rapport-jaar-nstd-goed-style');
$na_style_nstd_goed_report = configGet('nascholingstotaal-rapport-jaar-nstd-goed-report-style');
$na_style_nstd_tekort = configGet('nascholingstotaal-rapport-jaar-nstd-tekort-style');
$na_style_nstd_tekort_report = configGet('nascholingstotaal-rapport-jaar-nstd-tekort-report-style');
$na_style_nstd_nvt = configGet('nascholingstotaal-rapport-jaar-nstd-nvt-style');
$na_style_bepaald = configGet('nascholingstotaal-rapport-jaar-nstd-bepaald-style');
$na_style_bepaald_report = configGet('nascholingstotaal-rapport-jaar-nstd-bepaald-report-style');
$na_style_onbepaald = configGet('nascholingstotaal-rapport-jaar-nstd-onbepaald-style');
$na_style_onbepaald_report = configGet('nascholingstotaal-rapport-jaar-nstd-onbepaald-report-style');
$na_style_kop = configGet('nascholingstotaal-rapport-jaar-kop-style');
$na_style_probleem = configGet('nascholingstotaal-rapport-probleem-style');
$na_style_waarschuwing = configGet('nascholingstotaal-rapport-waarschuwing-style');
$na_style_sub_rubric = configGet('nascholingstotaal-rapport-sub-rubriek-style');
$na_nascholing_onvoldoende = false;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Haal eerst alle nascholingstotalen voor een contact op, in volgorde van jaar.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
$na_geen_status_vanaf_jaar = datetime\year(datetime\now()) - 1;
$na_in_calc_key = string\concatenate('nascholingstotalen-in-berekening-voor', $contact_id);
global\set($na_in_calc_key, true);
$na_rows = record\fetchManyHash('Nascholingstotaal',
'id, jaar, dagen, dagenMedisch, dagenHomeopathie, dagenTotaal, medischSaldo, homeopathieSaldo, dagenSaldo, saldoNaCorrectie, dagenWerkgroep, dagenVrijstelling, status, berekeningsUitleg, startSurplus, dagenFormeleNorm, homeopathieFormeleNorm, medischFormeleNorm, maxMeenemen, examenjaar, handmatig, surplus, surplusHomeopathie, surplusMedisch',
'jaar', 'asc',
'contactId=', $contact_id,
);
$na_n = array\length($na_rows);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Initialiseer om te beginnen alle surplussen op de saldo's van een jaar.
// Daarna gaan we de zaak verdelen.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
$na_i = 0;
while($na_i < $na_n,
$na_row = array\at($na_rows, $na_i);
$na_surplus = hash\get($na_row, 'dagenSaldo');
$na_surplus_med = hash\get($na_row, 'medischSaldo');
$na_surplus_hom = hash\get($na_row, 'homeopathieSaldo');
ifThen($na_i == 0,
$na_start_surplus = hash\get($na_row, 'startSurplus');
$surplus = $surplus + $na_start_surplus;
);
$na_row = hash\set($na_row, 'surplus', 0);
$na_row = hash\set($na_row, 'surplusMedisch', 0);
$na_row = hash\set($na_row, 'surplusHomeopathie', 0);
$na_rows = array\set($na_rows, $na_i, $na_row);
$na_i = $na_i + 1;
);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Pas de eenvoudige berekeningsmethode toe. Eerst tellen we gewoon door, zonder te corrigeren
// Behalve dan dat we maximaal de norm aan dagen meenemen naar het volgende jaar.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
$na_i = 0;
while($na_i < $na_n,
$na_uitleg = '';
$na_row = array\at($na_rows, $na_i);
ifThenElse($na_i > 0,
$na_row_prev = array\at($na_rows, $na_i - 1);
,
$na_row_prev = null;
);
$na_id = hash\get($na_row, 'id');
$na_jaar = hash\get($na_row, 'jaar');
$na_surplus_tot = hash\get($na_row, 'surplus');
$na_surplus_med = hash\get($na_row, 'surplusMedisch');
$na_surplus_hom = hash\get($na_row, 'surplusHomeopathie');
$na_max_meenemen = hash\get($na_row, 'maxMeenemen');
log('warning', 'na_max_meenemen=', $na_max_meenemen);
$do_adding = true;
ifThenElse($na_i == 0,
log('warning', '1e jaar');
log('warning', 'jaar=', $na_jaar, ', surplus_hom=', $na_surplus_hom, ', surplus_med=', $na_surplus_med, ', surplus_tot=', $na_surplus_tot);
,
// Bereken het opvolgende jaar
// Initialiseer de situatie van vorig jaar en dit jaar.
$na_surplus_hom_prev = hash\get($na_row_prev, 'surplusHomeopathie');
$na_surplus_med_prev = hash\get($na_row_prev, 'surplusMedisch');
$na_surplus_tot_prev = hash\get($na_row_prev, 'surplus');
log('warning', 'previous: jaar=', $na_jaar - 1, ', surplus_hom=', $na_surplus_hom_prev, ', surplus_med=', $na_surplus_med_prev, ', surplus_tot=', $na_surplus_tot_prev);
log('warning', 'current : jaar=', $na_jaar, ', surplus_hom=', $na_surplus_hom, ', surplus_med=', $na_surplus_med, ', surplus_tot=', $na_surplus_tot);
// Check eerst of er nog tekorten van het voorgaande jaar zijn om op te lossen.
// Stel dat er voldoende saldo is in het huidige jaar en een tekort in het voorgaande jaar,
// dan gaan we dat van het huidige jaar afsnoepen en terugbrengen naar het laatste voorgaande
// jaar dat een tekort heeft. Nu is het zo, dat dit tekort cumulatief is voor alle voorgaande
// jaren met tekorten. We gaan dan terug naar het 1e jaar met een tekort voor een gegeven
// soort en fixen het daar. Daarna zetten we na_i terug naar dat jaar + 1 en rekenen
// opnieuw door.
ifThen($na_surplus_hom_prev < 0 || $na_surplus_med_prev < 0 || $na_surplus_tot_prev < 0,
// Is er überhaupt iets aan te vullen?
ifThen($na_surplus_tot > 0,
// Bepaal tot waar het tekort voor medisch ligt
// Als de medische surplus > 0.
ifThenElse($na_surplus_med > 0 && $na_surplus_med_prev < 0,
// Corrigeer medisch
$na_j = $na_i - 1;
$na_row_j = array\at($na_rows, $na_j);
$na_surplus_med_j = hash\get($na_row_j, 'surplusMedisch');
while($na_surplus_med_j < 0 && $na_j >= 0
$na_j = $na_j - 1;
ifThen($na_j > 0,
$na_row_j = array\at($na_rows, $na_j);
$na_surplus_med_j = hash\get($na_row_j, 'surplusMedisch');
);
);
$na_j = $na_j + 1;
$na_row_j = array\at($na_rows, $na_j);
$na_surplus_med_j = hash\get($na_row_j, 'surplusMedisch');
$na_surplus_j = hash\get($na_row_j, 'surplus');
$na_max_corectie = -1 * $na_surplus_med_j;
ifThen($na_max_correctie > $na_surplus_med, $na_max_correctie = $na_surplus_med);
ifThen($na_max_correctie > $na_surplus_tot, $na_max_correctie = $na_surplus_tot);
$na_surplus_med_j = $na_surplus_med_j + $na_max_correctie;
$na_surplus_j = $na_surplus_j + $na_max_correctie;
$na_suplus_med = $na_suplus_med - $na_max_correctie;
$na_surplus_tot = $na_surplus_tot - $na_max_correctie;
$na_row_j = hash\set($na_row_j, 'surplusMedisch', $na_surplus_med_j);
$na_row_j = hash\set($na_row_j, 'surplus', $na_surplus_j);
$na_rows = array\set($na_rows, $na_j, $na_row_j);
$do_adding = false;
,
ifThenElse($na_surplus_hom > 0 && $na_surplus_hom_prev < 0,
// Corrigeer homeopathie
$na_j = $na_i - 1;
$na_row_j = array\at($na_rows, $na_j);
$na_surplus_hom_j = hash\get($na_row_j, 'surplusHomeopathie');
while($na_surplus_hom_j < 0 && $na_j >= 0
$na_j = $na_j - 1;
ifThen($na_j > 0,
$na_row_j = array\at($na_rows, $na_j);
$na_surplus_hom_j = hash\get($na_row_j, 'surplusHomeopathie');
);
);
$na_j = $na_j + 1;
$na_row_j = array\at($na_rows, $na_j);
$na_surplus_hom_j = hash\get($na_row_j, 'surplusHomeopathie');
$na_surplus_j = hash\get($na_row_j, 'surplus');
$na_max_corectie = -1 * $na_surplus_hom_j;
ifThen($na_max_correctie > $na_surplus_hom, $na_max_correctie = $na_surplus_hom);
ifThen($na_max_correctie > $na_surplus_tot, $na_max_correctie = $na_surplus_tot);
$na_surplus_hom_j = $na_surplus_hom_j + $na_max_correctie;
$na_surplus_j = $na_surplus_j + $na_max_correctie;
$na_suplus_hom = $na_suplus_hom - $na_max_correctie;
$na_surplus_tot = $na_surplus_tot - $na_max_correctie;
$na_row_j = hash\set($na_row_j, 'surplusHomeopathie', $na_surplus_hom_j);
$na_row_j = hash\set($na_row_j, 'surplus', $na_surplus_j);
$na_rows = array\set($na_rows, $na_j, $na_row_j);
$do_adding = false;
,
// Corrigeer overig indien nodig
ifThen($na_surplus_tot > 0 && $na_surplus_tot_prev < 0
$na_j = $na_i - 1;
$na_row_j = array\at($na_rows, $na_j);
$na_surplus_j = hash\get($na_row_j, 'surplus');
while($na_surplus_j < 0 && $na_j >= 0
$na_j = $na_j - 1;
ifThen($na_j > 0,
$na_row_j = array\at($na_rows, $na_j);
$na_surplus_j = hash\get($na_row_j, 'surplus');
);
);
$na_j = $na_j + 1;
$na_row_j = array\at($na_rows, $na_j);
$na_surplus_j = hash\get($na_row_j, 'surplus');
$na_max_corectie = -1 * $na_surplus_j;
ifThen($na_max_correctie > $na_surplus_tot, $na_max_correctie = $na_surplus_tot);
$na_surplus_j = $na_surplus_j + $na_max_correctie;
$na_surplus_tot = $na_surplus_tot - $na_max_correctie;
$na_row_j = hash\set($na_row_j, 'surplus', $na_surplus_j);
$na_rows = array\set($na_rows, $na_j, $na_row_j);
$do_adding = false;
),
);
);
);
);
ifThen($do_adding,
// Pak maximaal de maxMeenemen norm aan dagen over van het voorgaande jaar, als er voldoende dagen zijn.
ifThen($na_surplus_tot_prev > 0,
log('warning', 'Surplus meenemen voor zover mogelijk, max_meenemen=', $na_max_meenemen);
// Oke, we kunnen überhaupt dagen meenemen.
$na_max_mee = $na_max_meenemen;
// Verdeel de dagen tussen medisch en homeopatisch, tenzij de
// voorgaande surplussen niet voldoende zijn, vul dan verder aantal
// met overig
while($na_surplus_tot_prev > 0 && $na_max_mee > 0,
// Zo lang huidig medisch surplus < 0 en previous medisch surplus > 0, kies voor medisch
// Anders, zo lang huidig homeopathie surplus < 0 en previous homeopathie surplus > 0, kies voor homeopathie
// Anders, zo lang previous homeopathie surplus / medisch surplus > 0, kies voor med, hom alternerend.
// Anders, zo lang previous homeopathie surplus > 0, kies voor homeopathie
// Anders, zo lang previous medisch surplus > 0, kies voor medisch
// Anders, kies voor overig
ifThenElse($na_surplus_med < 0 && $na_surplus_med_prev > 0,
$na_hom_med_overig = 'med';
,
ifThenElse($na_surplus_hom < 0 && $na_surplus_hom_prev > 0,
$na_hom_med_overig = 'hom';
,
ifThenElse($na_surplus_hom_prev > 0 && $na_surplus_med_prev > 0,
ifThenElse($na_hom_med_overig == 'med',
$na_hom_med_overig = 'hom';
,
$na_hom_med_overig = 'med';
);
,
ifThenElse($na_surplus_hom_prev > 0,
$na_hom_med_overig = 'hom';
,
ifThenElse($na_surplus_med_prev > 0,
$na_hom_med_overig = 'med';
,
$na_hom_med_overig = 'overig';
);
);
);
);
);
log('warning', 'na_hom_med_overig=', $na_hom_med_overig, ', surplus_tot_prev=', $na_surplus_tot_prev, ', na_max_mee=', $na_max_mee);
ifThen($na_hom_med_overig == 'med',
ifThen($na_surplus_med_prev > 0,
$na_take = 1;
ifThen($na_surplus_med_prev < $na_take, $na_take = $na_surplus_med_prev);
ifThen($na_surplus_tot_prev < $na_take, $na_take = $na_surplus_tot_prev);
$na_surplus_med = $na_surplus_med + $na_take;
$na_surplus_med_prev = $na_surplus_med_prev - $na_take;
$na_surplus_tot = $na_surplus_tot + $na_take;
$na_surplus_tot_prev = $na_surplus_tot_prev - $na_take;
$na_max_mee = $na_max_mee - $na_take;
);
);
ifThen($na_hom_med_overig == 'hom',
ifThen($na_surplus_hom_prev > 0,
$na_take = 1;
ifThen($na_surplus_hom_prev < $na_take, $na_take = $na_surplus_hom_prev);
ifThen($na_surplus_tot_prev < $na_take, $na_take = $na_surplus_tot_prev);
$na_surplus_hom = $na_surplus_hom + $na_take;
$na_surplus_hom_prev = $na_surplus_hom_prev - $na_take;
$na_surplus_tot = $na_surplus_tot + $na_take;
$na_surplus_tot_prev = $na_surplus_tot_prev - $na_take;
$na_max_mee = $na_max_mee - $na_take;
);
);
ifThen($na_hom_med_overig == 'overig',
// We weten nu al dat na_surplus_tot_prev > 0
$na_take = 1;
ifThen($na_surplus_tot_prev < $na_take, $na_take = $na_surplus_tot_prev);
$na_surplus_tot = $na_surplus_tot + $na_take;
$na_surplus_tot_prev = $na_surplus_tot_prev - $na_take;
$na_max_mee = $na_max_mee - $na_take;
);
);
);
log('warning', 'previous: jaar=', $na_jaar - 1, ', surplus_hom=', $na_surplus_hom_prev, ', surplus_med=', $na_surplus_med_prev, ', surplus_tot=', $na_surplus_tot_prev);
log('warning', 'current : jaar=', $na_jaar, ', surplus_hom=', $na_surplus_hom, ', surplus_med=', $na_surplus_med, ', surplus_tot=', $na_surplus_tot);
$na_row_prev = hash\set($na_row_prev, 'surplusHomeopathie', $na_surplus_hom_prev);
$na_row_prev = hash\set($na_row_prev, 'surplusMedisch', $na_surplus_med_prev);
$na_row_prev = hash\set($na_row_prev, 'surplus', $na_surplus_tot_prev);
$na_rows = array\set($na_rows, $na_i - 1, $na_row_prev);
);
);
$na_row = hash\set($na_row, 'surplusHomeopathie', $na_surplus_hom);
$na_row = hash\set($na_row, 'surplusMedisch', $na_surplus_med);
$na_row = hash\set($na_row, 'surplus', $na_surplus_tot);
$na_rows = array\set($na_rows, $na_i, $na_row);
ifThenElse($do_adding,
$na_i = $na_i + 1;
,
$na_i = $na_j;
);
);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Zet al die totalen weer in de database
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
$na_i = 0;
while($na_i < $na_n,
$na_row = array\at($na_rows, $na_i);
$na_id = hash\get($na_row, 'id');
$na_surplus = hash\get($na_row, 'surplus');
$na_surplus_hom = hash\get($na_row, 'surplusHomeopathie');
$na_surplus_med = hash\get($na_row, 'surplusMedisch');
$na_dashboard = '';
$na_berekeningsuitleg = '';
record\update('Nascholingstotaal', $na_id,
'surplus', $na_surplus,
'surplusMedisch', $na_surplus_med,
'surplusHomeopathie', $na_surplus_hom,
'dashboard', $na_dashboard,
'berekeningsuitleg', $na_report
);
$na_i = $na_i + 1;
);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Klaar.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
global\unset($na_in_calc_key, true);