-
Notifications
You must be signed in to change notification settings - Fork 0
/
Winbooks.php
606 lines (555 loc) · 23.9 KB
/
Winbooks.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
<?php
/*
be.chiro.civi.winbooksexport - export contribution batches to Winbooks.
Copyright (C) 2016 Chirojeugd-Vlaanderen vzw
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2014
* $Id$
*
*/
/*
* @see http://wiki.civicrm.org/confluence/display/CRM/CiviAccounts+Specifications+-++Batches#CiviAccountsSpecifications-Batches-%C2%A0Overviewofimplementation
*/
class CRM_Financial_BAO_ExportFormat_Winbooks extends CRM_Financial_BAO_ExportFormat {
/**
* class constructor
*/
function __construct() {
parent::__construct();
}
/**
* @param $exportParams
*/
function export($exportParams) {
parent::export($exportParams);
self::assign('csf', $this->_exportParams['csf']);
self::assign('act', $this->_exportParams['act']);
self::assign('ant', $this->_exportParams['ant']);
$this->output();
}
/**
* @param null $fileName
*/
public function output($fileName = NULL) {
$tplFiles = $this->getHookedTemplateFileNames();
foreach ($tplFiles as $tplFile) {
$out[] = self::getTemplate()->fetch($tplFile);
}
$fileNames = $this->putFile($out);
foreach ($fileNames as $fileName) {
self::createActivityExport($this->_batchIds, $fileName);
}
}
/**
* @param $out array three strings to save as act, ant and csf.
*
* @return array file names of act, ant and csf file.
*/
function putFile($out) {
$config = CRM_Core_Config::singleton();
// Winbooks heeft drie bestanden nodig.
// Met specifieke naamgeving.
//act.txt (bevat transacties; in ons geval allemaal verkoopfacturen)
//ant.txt (bevat de info over de analytische plannen m.b.t. de facturen)
//csf.txt (bevat de gegevens van te importeren klanten)
$fileName[] = $config->uploadDir . 'act.txt';
$this->_downloadFile[] = $config->customFileUploadDir . CRM_Utils_File::cleanFileName(basename($fileName[0]));
$buffer = fopen($fileName[0], 'w');
fwrite($buffer, $out[0]);
fclose($buffer);
$fileName[] = $config->uploadDir . 'ant.txt';
$this->_downloadFile[] = $config->customFileUploadDir . CRM_Utils_File::cleanFileName(basename($fileName[1]));
$buffer = fopen($fileName[1], 'w');
fwrite($buffer, $out[1]);
fclose($buffer);
$fileName[] = $config->uploadDir . 'csf.txt';
$this->_downloadFile[] = $config->customFileUploadDir . CRM_Utils_File::cleanFileName(basename($fileName[2]));
$buffer = fopen($fileName[2], 'w');
fwrite($buffer, $out[2]);
fclose($buffer);
return $fileName;
}
/**
* Abuse generateExportQuery to generate the 3 queries for Winbooks export.
*
* @param int $batchId
* @return array
*/
function generateExportQuery($batchId) {
self::prepareWinbooksExport($batchId);
$result = array();
$result['csf'] = self::generateCSFQuery($batchId);
$result['act'] = self::generateACTQuery($batchId);
$result['ant'] = self::generateANTQuery($batchId);
return $result;
}
/**
* @param int $batchId
*
* @return CRM_Core_DAO
*/
static function generateCSFQuery($batchId) {
// Query voor CSF bestand
// Deel 1 voor groepsfacturen
// Deel 2 is voor personen
$results = array();
$params = array('name'=>'relationship_lid_van_customfields');
CRM_Core_BAO_CustomGroup::retrieve( $params, $results);
$relationship_lid_van_customfields_tabel = $results['table_name'];
$params = array('name'=>'relationship_lid_van_customfields_functie');
CRM_Core_BAO_CustomField::retrieve( $params, $results);
$relationship_lid_van_customfields_functie_kolom = $results['column_name'];
$sql = "SELECT DISTINCT
eb.id as batch_id,
UPPER(contact_from.external_identifier) as referentie,
CONCAT_WS(' ', contact_from.last_name, contact_from.first_name) as naam1,
'' as naam2,
contact_from_adres.street_address as adres1,
contact_from_tel.phone as tel,
'' as bankrekening,
contact_from_adres.city as woonplaats,
contact_from_adres.postal_code as postcode,
contact_from_email.email as email,
'' as categorie
FROM civicrm_entity_batch eb
LEFT JOIN civicrm_financial_trxn ft ON (eb.entity_id = ft.id AND eb.entity_table = 'civicrm_financial_trxn')
LEFT JOIN civicrm_entity_financial_trxn eft ON (eft.financial_trxn_id = ft.id AND eft.entity_table = 'civicrm_contribution')
LEFT JOIN civicrm_contribution cont ON cont.id = eft.entity_id
LEFT JOIN civicrm_contact contact_from ON contact_from.id = cont.contact_id
LEFT JOIN civicrm_address contact_from_adres ON contact_from_adres.contact_id = contact_from.id and contact_from_adres.is_billing = 1
LEFT JOIN civicrm_phone contact_from_tel ON contact_from_tel.contact_id = contact_from.id and contact_from_tel.is_primary = 1
LEFT JOIN civicrm_email contact_from_email ON contact_from_email.contact_id = contact_from.id and contact_from_email.is_primary = 1
WHERE eb.batch_id = ( %1 ) and contact_from.contact_type = 'Individual'
UNION
SELECT DISTINCT
eb.id as batch_id,
UPPER(contact_from.external_identifier) as referentie,
contact_from.organization_name as naam1,
"
// HACK ALERT! : de naam van de financieel verantwoordelijke zit bij ons
// in supplemental_address_1.
."supplemental_address_1 as naam2,
cba.street_address as adres1,
contact_fin_verantw_tel.phone as tel,
'' as bankrekening,
cba.city as woonplaats,
cba.postal_code as postcode,
contact_fin_verantw_email.email as email,
'C' as categorie
FROM civicrm_entity_batch eb
LEFT JOIN civicrm_financial_trxn ft ON (eb.entity_id = ft.id AND eb.entity_table = 'civicrm_financial_trxn')
LEFT JOIN civicrm_entity_financial_trxn eft ON (eft.financial_trxn_id = ft.id AND eft.entity_table = 'civicrm_contribution')
LEFT JOIN civicrm_contribution cont ON cont.id = eft.entity_id
LEFT JOIN civicrm_contact contact_from ON contact_from.id = cont.contact_id
# Zoektocht naar financieel verantwoordelijk
LEFT JOIN civicrm_relationship contact_relation
ON contact_from.id = contact_relation.contact_id_b AND contact_relation.is_active = 1
AND contact_relation.relationship_type_id = " . CHIRO_RELATIONSHIP_LID_VAN . "
LEFT JOIN $relationship_lid_van_customfields_tabel extra_lid_informatie ON contact_relation.id = extra_lid_informatie.entity_id
LEFT JOIN civicrm_contact contact_fin_verantw ON contact_fin_verantw.id = contact_relation.contact_id_a and extra_lid_informatie.$relationship_lid_van_customfields_functie_kolom like '%FI%'
LEFT JOIN civicrm_address cba ON cba.contact_id = contact_from.id and cba.is_billing = 1
LEFT JOIN civicrm_phone contact_fin_verantw_tel ON contact_fin_verantw_tel.contact_id = contact_fin_verantw.id and contact_fin_verantw_tel.is_primary = 1
LEFT JOIN civicrm_email contact_fin_verantw_email ON contact_fin_verantw_email.contact_id = contact_fin_verantw.id and contact_fin_verantw_email.is_primary = 1
WHERE eb.batch_id = ( %1 ) and contact_from.contact_type = 'Organization'";
$params = array(1 => array($batchId, 'String'));
$dao = CRM_Core_DAO::executeQuery($sql, $params);
return $dao;
}
/**
* @param $batchId
*
* @return Object
*/
static function generateACTQuery($batchId) {
// Query voor ACT bestand
// Lap. Hier wordt entity_batch aan contribution via financial_trxn en
// entity_financial_trxn. In Batch.php (exportFinancialBatch) werd vroeger een
// entity_batch rechtstreeks aan een contribution gekoppeld. Dat veroorzaakte
// issue #5347.
// Ik gok dat dit juist is, en pas de code in Batch.php aan.
// Vermoedelijk worden transacties overgezet naar het boekhoudpakket,
// en is het mogelijk dat er meerdere transacties zijn voor 1
// contributie.
$sql = "SELECT eb.id as batch_id,
cont.source as dbkcode,
cont.trxn_id as docnumber,
cfa.accounting_code as accountgl,
contact.external_identifier as accountrp,
cont.receive_date as date,
ft.total_amount as amounteur,
li.line_total as lineamounteur,
event.title as comment
FROM civicrm_entity_batch eb
LEFT JOIN civicrm_financial_trxn ft ON (eb.entity_id = ft.id AND eb.entity_table = 'civicrm_financial_trxn')
LEFT JOIN civicrm_entity_financial_trxn eft ON (eft.financial_trxn_id = ft.id AND eft.entity_table = 'civicrm_contribution')
LEFT JOIN civicrm_contribution cont ON cont.id = eft.entity_id
LEFT JOIN civicrm_line_item li ON cont.id = li.contribution_id
LEFT JOIN civicrm_financial_type ftype on ftype.id = li.financial_type_id
-- cluelessly joining financial account and financial type, see #4265
LEFT JOIN civicrm_financial_account cfa on ftype.name = cfa.name
LEFT JOIN civicrm_participant_payment partpay on cont.id = partpay.contribution_id
LEFT JOIN civicrm_participant part on partpay.participant_id = part.id
LEFT JOIN civicrm_event event on part.event_id = event.id
LEFT JOIN civicrm_contact contact on contact.id = cont.contact_id
WHERE cfa.account_type_code = 'INC' and eb.batch_id = ( %1 )
-- only contributions with a transaction ID (#4464)
AND cont.trxn_id IS NOT NULL
ORDER BY docnumber, accountgl";
$params = array(1 => array($batchId, 'String'));
$dao = CRM_Core_DAO::executeQuery($sql, $params);
return $dao;
}
/**
* @param $batchId
*
* @return Object
*/
static function generateANTQuery($batchId) {
// Query voor ANT bestand
$results = array();
$params = array('name'=>'event_informatie');
CRM_Core_BAO_CustomGroup::retrieve( $params, $results);
$event_informatie_tabel = $results['table_name'];
$params = array('name'=>'event_informatie_analytische_code');
CRM_Core_BAO_CustomField::retrieve( $params, $results);
$event_informatie_analytische_code_kolom = $results['column_name'];
$sql = "SELECT eb.id as batch_id,
cont.source as dbkcode,
cont.trxn_id as docnumber,
cfa.accounting_code as accountgl,
cont.receive_date as date,
li.line_total as amountgl,
extra_event_informatie.$event_informatie_analytische_code_kolom as zonana1,
event.title as comment
FROM civicrm_entity_batch eb
LEFT JOIN civicrm_financial_trxn ft ON (eb.entity_id = ft.id AND eb.entity_table = 'civicrm_financial_trxn')
LEFT JOIN civicrm_entity_financial_trxn eft ON (eft.financial_trxn_id = ft.id AND eft.entity_table = 'civicrm_contribution')
LEFT JOIN civicrm_contribution cont ON cont.id = eft.entity_id
LEFT JOIN civicrm_line_item li ON cont.id = li.contribution_id
LEFT JOIN civicrm_financial_type ftype on ftype.id = li.financial_type_id
-- cluelessly joining financial account and financial type, see #4265
LEFT JOIN civicrm_financial_account cfa on ftype.name = cfa.name
LEFT JOIN civicrm_participant_payment partpay on cont.id = partpay.contribution_id
LEFT JOIN civicrm_participant part on partpay.participant_id = part.id
LEFT JOIN civicrm_event event on part.event_id = event.id
LEFT JOIN $event_informatie_tabel extra_event_informatie on event.id = extra_event_informatie.entity_id
WHERE cfa.account_type_code = 'INC' and eb.batch_id = ( %1 )
-- only contributions with a transaction ID (#4464)
AND cont.trxn_id IS NOT NULL
ORDER BY docnumber, accountgl";
$params = array(1 => array($batchId, 'String'));
$dao = CRM_Core_DAO::executeQuery($sql, $params);
return $dao;
}
/**
* Create export based on DAO's for act, ant and csf files.
*
* This is a hack. Normally, makeExport expects an array of
* DAO's, but for Winbooks, we have an array of arrays of 3 dao's,
* one for each export file.
*
* But it works, because makeExport uses the output of
* generateExportQuery, which we overloaded as well to generate the
* 3 DAOs.
*
* @param $export array Een array die een batch-ID afbeeldt
* op een nieuwe array, met keys 'csf', 'act' en 'ant', en
* als value een DAO. De bedoeling is dat het overlopen van
* die DAO de velden oplevert die hieronder gebruikt worden.
*/
function makeExport($export) {
$csf = array();
$act = array();
$ant = array();
foreach ($export as $batchId => $dao_array) {
// Execute winbooks querys, and fetch and format values, to assign to tpl
$dao_csf = $dao_array['csf'];
$this->_batchIds = $batchId;
while ($dao_csf->fetch()) {
$csf[$dao_csf->referentie] = array(
'referentie' => $this->format($dao_csf->referentie, 'referentie'),
'naam1' => $dao_csf->naam1,
'naam2' => $this->format($dao_csf->naam2, 'display_name'),
'adres1' => $dao_csf->adres1,
'tel' => $dao_csf->tel,
'bankrekening' => $dao_csf->bankrekening,
'woonplaats' => $dao_csf->woonplaats,
'postcode' => $this->format($dao_csf->postcode, 'postcode'),
'email' => $dao_csf->email,
'categorie' => $dao_csf->categorie,
);
}
$dao_act = $dao_array['act'];
while ($dao_act->fetch()) {
$act[$dao_act->docnumber][] = array(
'dbkcode' => $this->format($dao_act->dbkcode, 'dbkcode'),
'docnumber' => $dao_act->docnumber,
'accountgl' => $dao_act->accountgl,
'accountrp' => $this->format($dao_act->accountrp, 'accountrp'),
'date' => $this->format($dao_act->date, 'date'),
'duedate' => $this->format($dao_act->date, 'duedate'),
'comment' => trim($this->format($dao_act->dbkcode, 'comment') . ' ' . $dao_act->comment),
'amounteur' => $this->format($dao_act->amounteur,'amount'),
'lineamounteur' => $this->format($dao_act->lineamounteur,'lineamounteur'),
// Gestructureerde mededeling
// We maken die hier opnieuw, omdat die bij ons in een custom
// field zit, en we dat custom field hier niet kunnen opvragen.
// Voordeel is dan wel dat we hier de speciale tekens kunnen weglaten,
// zodat Winbooks het ding meteen herkent als OGM (#5443).
'ogm' => CRM_Winbooksexport_Helper::ogm($dao_act->dbkcode, $dao_act->docnumber, FALSE),
);
}
$dao_ant = $dao_array['ant'];
while ($dao_ant->fetch()) {
$ant[$dao_ant->docnumber][] = array(
'dbkcode' => $this->format($dao_ant->dbkcode, 'dbkcode'),
'docnumber' => $dao_ant->docnumber,
'accountgl' => $dao_ant->accountgl,
'date' => $this->format($dao_ant->date, 'date'),
'amountgl' => $this->format($dao_ant->amountgl,'lineamounteur'),
'zonana1' => $dao_ant->zonana1,
'comment' => trim($this->format($dao_ant->dbkcode, 'comment') . ' ' . $dao_ant->comment),
);
}
$exportParams = array(
'csf' => $csf,
'act' => $act,
'ant' => $ant,
);
self::export($exportParams);
}
self::uploadToFtpServer();
}
function uploadToFtpServer() {
$ftp_user = variable_get('chiroftp_user', NULL);
$ftp_pass = variable_get('chiroftp_pass', NULL);
$ftp_url = variable_get('chiroftp_url', NULL);
$ftp_folder = variable_get('chiroftp_folder', NULL) . '/factuurexport-' . date("Y-m-d-G-i");
foreach ($this->_downloadFile as $file) {
$ch = curl_init();
$fp = fopen($file, 'r');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FTP_SSL, true);
curl_setopt($ch, CURLOPT_FTP_CREATE_MISSING_DIRS, true);
curl_setopt($ch, CURLOPT_URL, 'ftp://' . $ftp_user . ':' . $ftp_pass . '@' . $ftp_url . '/' . $ftp_folder . '/' . CRM_Utils_File::cleanFileName(basename($file)));
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file));
curl_exec($ch);
curl_close($ch);
}
}
/**
* @return string
*/
function getMimeType() {
return 'application/octet-stream';
}
/**
* @return string
*/
function getFileExtension() {
return 'winbooks';
}
/**
* @return array
*/
function getHookedTemplateFileNames() {
return array('CRM/Financial/ExportFormat/Winbooks-act.tpl', 'CRM/Financial/ExportFormat/Winbooks-ant.tpl', 'CRM/Financial/ExportFormat/Winbooks-csf.tpl');
}
/*
* $s the input string
* $type can be string, date, or notepad
*/
/**
* Formatteert veld $s om gebruikt te worden als outputveld $type
* voor wblink.
*
* @param $s
* @param string $type
*
* @return bool|mixed|string
*/
static function format($s, $type = 'string') {
$decimals = 2;
$dec_point = '.';
$thousands_separator = '';
switch ($type) {
case 'referentie':
case 'accountrp':
// Convert external identifier to the format that's used in
// Winbooks. Sadly enough, it is not the same format for our
// organization.
// identifier of individual (AD-number)
if (is_numeric($s)) {
// prefix with 'AD'.
$result = 'AD' . $s;
}
// identifier of ploegen (subcontacttype of organization)
else {
// remove spaces and slashes
$result = strtoupper(str_replace(array(" ", "/"), "", $s));
}
break;
case 'display_name':
// HACK. We use this to mess with a supplemental_address_1, containing
// a number between parentheses, that should not be sent to Winbooks.
// So let's remove it.
$result = trim(preg_replace('/\([0-9]+\)$/', '', $s));
break;
case 'postcode':
//Blijkbaar doen we niet aan buitenlandse facturen
$result = 'BE-' . $s;
break;
case 'dbkcode':
if ($s == 'AANSLUIT' || $s == 'U_VERZEK') {
$result = (string) "005";
}
else if ($s == 'DP') {
$result = (string) "006";
}
else {
$result = (string) "007";
}
break;
case 'date':
$result = date('Ymd', strtotime($s));
break;
case 'duedate':
$result = date('Ymd', strtotime('+3 week', strtotime($s)));
break;
case 'comment':
if ($s == 'AANSLUIT' || $s == 'U_VERZEK') {
$result = (string) "Aansluitingsfactuur";
}
else if ($s == 'DP') {
$result = (string) "Dubbelpuntfactuur";
}
else {
// Hier gaat het vermoedelijk om een evenement. We vermelden niets,
// want de naam van het evenement wordt nog aan deze output
// geplakt.
// Separation of concerns m'n gat :-P
// (nee, TODO: dit moet eigenlijk beter.)
$result = (string) "";
}
break;
case 'amount':
// Punt als decimaal scheidingsteken, zie ook #4251
$result = number_format($s, $decimals, $dec_point, $thousands_separator);
break;
case 'lineamounteur':
$value = -1 * $s;
$result = number_format($value, $decimals, $dec_point, $thousands_separator);
break;
case 'string':
break;
}
return $result;
}
/**
* Test whether the batch with given ID contains invoices with a trxn_id.
*
* @param int $batchId
* @return boolean
*/
private static function testExistingTransactionIds($batchId) {
is_numeric($batchId) or die('BatchID should be numeric.');
$result = civicrm_api3('BatchedContribution', 'get', array(
'batch_id' => $batchId,
'trxn_id' => array('>' => ''),
'options' => array('limit' => 1)
));
return ($result['count'] > 0);
}
/**
* Test existence of a contribution with the predecessor of the given trxn_id.
* @param int $trxnId
*/
private static function testPreviousTrxnId($trxnId) {
is_numeric($trxnId) or die('trxnId should be numeric.');
$result = civicrm_api3('Contribution', 'get', array('trxn_id' => $trxnId - 1));
if ($result['count'] != 1) {
CRM_Core_Session::setStatus(ts('Transaction ID discontinuity detected. Transaction %1 not found.', array(1 => $trxnId - 1)));
}
}
/**
* Store invoice number in trxn_id and OGM in custom field.
*
* Eigenlijk is het niet helemaal juist wat we hier doen. We
* gebruiken de functionaliteit om transacties te exporteren, om
* contributies te exporteren. In ons geval wordt er bij iedere
* contributie automatisch een transactie aangemaakt, dat is waarschijnlijk
* ons geluk.
*
* @param int $batchId
*/
public function prepareWinbooksExport($batchId) {
$aansluitingsfactuur_nr = CRM_Core_BAO_Setting::getItem('chirocontribution', 'chirocontribution_max_aansluitingsfactuur_nr');
$cursusfactuur_nr = CRM_Core_BAO_Setting::getItem('chirocontribution', 'chirocontribution_max_cursusfactuur_nr');
self::testPreviousTrxnId($aansluitingsfactuur_nr);
self::testPreviousTrxnId($cursusfactuur_nr);
if (self::testExistingTransactionIds($batchId)) {
CRM_Core_Session::setStatus(ts('Existing transaction IDs were not replaced.'));
}
$result = civicrm_api3('BatchedContribution', 'get', array(
'batch_id' => $batchId,
// FIXME: we should have something like 'IS NULL OR EMPTY'.
'trxn_id' => array('IS NULL' => 1),
// We geven enkel contributies met een total_amount > 0 een
// factuurnummer. In Winbooks.php worden alleen contributies met
// factuurnummer geëxporteerd. Op die manier vermijden we een
// export van nulfacturen (#4464)
'total_amount' => array('>' => 0),
'return' => 'id,financial_type_id,trxn_id',
// Voor de zekerheid sorteren we op contributie-ID.
// Op die manier probeer ik de aansluitingscontributies, die
// gemaakt zijn op volgorde van stamnummer, op dezelfde manier
// doorgeboekt te krijgen naar Winbooks. (#4407)
// FIXME: Als de facturen op termijn automatisch worden gegenereerd bij
// het aansluiten van leden, dan gaat die volgorde niet meer kloppen.
// We moeten ervoor blijven zorgen dat ledenlijst, attesten en facturen
// in dezelfde volgorde worden afgedrukt.
'options' => array('limit' => 0, 'sort' => 'id'),
));
foreach ($result['values'] as $contribution) {
// Factuurnummer en OGM aanpassen van contributions waarbij dit nog
// niet gebeurd is.
$params = array(
'id' => $contribution['id'],
// Pas meteen ook receive_date aan (#4479).
'receive_date' => date("Y-m-d"),
);
if ($contribution['financial_type_id'] == CHIRO_FINANCIAL_TYPE_KOSTEN_EVENT ||
// issue #4546
$contribution['financial_type_id'] == CHIRO_FINANCIAL_TYPE_70100000) {
$params['trxn_id'] = ++$cursusfactuur_nr;
$params[CHIRO_FIELD_CONTRIBUTION_OGM] = CRM_Winbooksexport_Helper::ogm('CURSUS', $cursusfactuur_nr);
}
if ($contribution['financial_type_id'] == CHIRO_FINANCIAL_TYPE_LIDGELD) {
$params['trxn_id'] = ++$aansluitingsfactuur_nr;
$params[CHIRO_FIELD_CONTRIBUTION_OGM] = CRM_Winbooksexport_Helper::ogm('AANSLUIT', $aansluitingsfactuur_nr);
}
$result = civicrm_api3('Contribution', 'create', $params);
if ($result['is_error']) {
// This will crash if e.g. the trxn_id already existed.
throw new Exception($result['error_message']);
}
}
CRM_Core_BAO_Setting::setItem($cursusfactuur_nr, 'chirocontribution', 'chirocontribution_max_cursusfactuur_nr');
CRM_Core_BAO_Setting::setItem($aansluitingsfactuur_nr, 'chirocontribution', 'chirocontribution_max_aansluitingsfactuur_nr');
}
}