dolibarr 23.0.3
html_cerfafr.modules.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2005-2006 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2012 Regis Houssin <regis.houssin@inodbox.com>
5 * Copyright (C) 2012 Marcos García <marcosgdf@gmail.com>
6 * Copyright (C) 2014-2020 Alexandre Spangaro <aspangaro@open-dsi.fr>
7 * Copyright (C) 2015 Benoit Bruchard <benoitb21@gmail.com>
8 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
9 * Copyright (C) 2025 Frédéric France <frederic.france@free.fr>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <https://www.gnu.org/licenses/>.
23 */
24
30require_once DOL_DOCUMENT_ROOT.'/core/modules/dons/modules_don.php';
31require_once DOL_DOCUMENT_ROOT.'/don/class/don.class.php';
32require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
33
34
39{
45 public function __construct($db)
46 {
47 global $conf, $langs;
48
49 $this->db = $db;
50 $this->name = "cerfafr";
51 $this->description = $langs->trans('DonationsReceiptModel').' - fr_FR - Cerfa 11580*04';
52
53 // Dimension page for size A4
54 $this->type = 'html';
55 }
56
57
63 public function isEnabled()
64 {
65 return true;
66 }
67
68
69 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
78 public function write_file($don, $outputlangs, $currency = '')
79 {
80 // phpcs:enable
81 global $user, $conf, $langs, $mysoc;
82
83 $now = dol_now();
84 $id = (!is_object($don) ? $don : '');
85
86 if (!is_object($outputlangs)) {
87 $outputlangs = $langs;
88 }
89
90 // Load traductions files required by page
91 $outputlangs->loadLangs(array("main", "dict", "companies", "bills", "products", "donations"));
92
93 $currency = !empty($currency) ? $currency : $conf->currency;
94
95 if (!empty($conf->don->dir_output)) {
96 // Definition of the object don (for upward compatibility)
97 if (!is_object($don)) {
98 $don = new Don($this->db);
99 $ret = $don->fetch($id);
100 $id = $don->id;
101 }
102
103 // Definition of $dir and $file
104 if (!empty($don->specimen)) {
105 $dir = $conf->don->dir_output;
106 $file = $dir."/SPECIMEN.html";
107 } else {
108 $donref = dol_sanitizeFileName($don->ref);
109 $dir = $conf->don->dir_output."/".$donref;
110 $file = $dir."/".$donref.".html";
111 }
112
113 if (!file_exists($dir)) {
114 if (dol_mkdir($dir) < 0) {
115 $this->error = $langs->trans("ErrorCanNotCreateDir", $dir);
116 return -1;
117 }
118 }
119
120 if (file_exists($dir)) {
121 $formclass = new Form($this->db);
122
123 // This is not the proper way to do it but $formclass->form_modes_reglement
124 // prints the translation instead of returning it
125 $formclass->load_cache_types_paiements();
126 if ($don->modepaymentid) {
127 $paymentmode = $formclass->cache_types_paiements[$don->modepaymentid]['label'];
128 } else {
129 $paymentmode = '';
130 }
131 $modepaymentcode = !empty($formclass->cache_types_paiements[$don->modepaymentid]['code']) ? $formclass->cache_types_paiements[$don->modepaymentid]['code'] : "";
132 if ($modepaymentcode == 'CHQ') {
133 $ModePaiement = '<td width="25%"><input type="checkbox"> Remise d\'espèces</td><td width="25%"><input type="checkbox" disabled="true" checked="checked"> Chèque</td><td width="50%"><input type="checkbox"> Virement, prélèvement, carte bancaire</td>';
134 } elseif ($modepaymentcode == 'LIQ') {
135 $ModePaiement = '<td width="25%"><input type="checkbox" checked="checked"> Remise d\'espèces</td><td width="25%"><input type="checkbox"> Chèque</td><td width="50%"><input type="checkbox"> Virement, prélèvement, carte bancaire</td>';
136 } elseif ($modepaymentcode == 'VIR' || $modepaymentcode == 'PRE' || $modepaymentcode == 'CB') {
137 $ModePaiement = '<td width="25%"><input type="checkbox"> Remise d\'espèces</td><td width="25%"><input type="checkbox"> Chèque</td><td width="50%"><input type="checkbox" checked="checked"> Virement, prélèvement, carte bancaire</td>';
138 } else {
139 $ModePaiement = '<td width="25%"><input type="checkbox"> Remise d\'espèces</td><td width="25%"><input type="checkbox"> Chèque</td><td width="50%"><input type="checkbox"> Virement, prélèvement, carte bancaire</td>';
140 }
141
142 /*
143 if (empty($don->societe))
144 {
145 $CodeDon = '<td width="33%"><input type="checkbox" disabled="true" checked="checked" > 200 du CGI</td><td width="33%"><input type="checkbox" disabled="true" > 238 bis du CGI</td><td width="33%"><input type="checkbox" disabled="true" > 978 du CGI</td>';
146 }
147 else
148 {
149 $CodeDon = '<td width="33%"><input type="checkbox" disabled="true" > 200 du CGI</td><td width="33%"><input type="checkbox" disabled="true" checked="checked" > 238 bis du CGI</td><td width="33%"><input type="checkbox" disabled="true" > 978 du CGI</td>';
150 }
151 */
152
153 // Define contents
154 $donmodel = DOL_DOCUMENT_ROOT."/core/modules/dons/html_cerfafr.html";
155
156 $form = file_get_contents($donmodel);
157
158 $form = str_replace('__REF__', (string) $don->id, $form);
159 $form = str_replace('__DATE__', dol_print_date($don->date, 'day', false, $outputlangs), $form);
160 //$form = str_replace('__IP__',$user->ip,$form); // TODO $user->ip not exist
161 $form = str_replace('__AMOUNT__', price($don->amount), $form);
162 $form = str_replace('__AMOUNTLETTERS__', $this->amountToLetters($don->amount), $form);
163 $form = str_replace('__CURRENCY__', $outputlangs->transnoentitiesnoconv("Currency".$currency), $form);
164 $form = str_replace('__CURRENCYCODE__', $conf->currency, $form);
165 $form = str_replace('__MAIN_INFO_SOCIETE_NOM__', $mysoc->name, $form);
166 $form = str_replace('__MAIN_INFO_SOCIETE_ADDRESS__', (string) $mysoc->address, $form);
167 $form = str_replace('__MAIN_INFO_SOCIETE_ZIP__', (string) $mysoc->zip, $form);
168 $form = str_replace('__MAIN_INFO_SOCIETE_TOWN__', (string) $mysoc->town, $form);
169 $form = str_replace('__MAIN_INFO_SOCIETE_OBJECT__', $mysoc->socialobject, $form);
170 $form = str_replace('__DONATOR_FIRSTNAME__', (string) $don->firstname, $form);
171 $form = str_replace('__DONATOR_LASTNAME__', (string) $don->lastname, $form);
172 $form = str_replace('__DONATOR_SOCIETE__', (string) $don->societe, $form);
173 $form = str_replace('__DONATOR_STATUT__', (string) $don->statut, $form);
174 $form = str_replace('__DONATOR_ADDRESS__', (string) $don->address, $form);
175 $form = str_replace('__DONATOR_ZIP__', (string) $don->zip, $form);
176 $form = str_replace('__DONATOR_TOWN__', (string) $don->town, $form);
177 $form = str_replace('__PAYMENTMODE_LIB__ ', (string) $paymentmode, $form);
178 $form = str_replace('__NOW__', dol_print_date($now, 'day', false, $outputlangs), $form);
179 $form = str_replace('__DONATION_PAYMENT_MODE__', $ModePaiement, $form);
180 $form = str_replace('__DONATION_MESSAGE__', getDolGlobalString('DONATION_MESSAGE'), $form);
181
182 // Replace with a generic replacement feature '__(XXX)__' must become $outputlangs->trans('XXX')
183 $form = make_substitutions($form, array(), $outputlangs);
184
185 $frencharticle = '';
186 if (preg_match('/fr/i', $outputlangs->defaultlang)) {
187 $frencharticle = '<font size="+1">Article 200, 238 bis et 978 du code général des impôts (CGI)</font>';
188 }
189 $form = str_replace('__FrenchArticle__', $frencharticle, $form);
190
191 $frencheligibility = '';
192 if (preg_match('/fr/i', $outputlangs->defaultlang)) {
193 $frencheligibility = 'Le bénéficiaire certifie sur l\'honneur que les dons et versements qu\'il reçoit ouvrent droit à la réduction d\'impôt prévue à l\'article :';
194 }
195 $form = str_replace('__FrenchEligibility__', $frencheligibility, $form);
196
197 $art200 = '';
198 if ($mysoc->country_code == 'FR') {
199 if (getDolGlobalInt('DONATION_ART200') >= 1) {
200 $art200 = '<input type="checkbox" disabled="true" checked="checked" >200 du CGI';
201 } else {
202 $art200 = '<input type="checkbox" disabled="true">200 du CGI';
203 }
204 }
205 $form = str_replace('__ARTICLE200__', $art200, $form);
206
207 $art238 = '';
208 if ($mysoc->country_code == 'FR') {
209 if (getDolGlobalInt('DONATION_ART238') >= 1) {
210 $art238 = '<input type="checkbox" disabled="true" checked="checked" >238 bis du CGI';
211 } else {
212 $art238 = '<input type="checkbox" disabled="true">238 bis du CGI';
213 }
214 }
215 $form = str_replace('__ARTICLE238__', $art238, $form);
216
217 $art978 = '';
218 if ($mysoc->country_code == 'FR') {
219 if (getDolGlobalInt('DONATION_ART978') >= 1) {
220 $art978 = '<input type="checkbox" disabled="true" checked="checked" >978 du CGI';
221 } else {
222 $art978 = '<input type="checkbox" disabled="true">978 du CGI';
223 }
224 }
225 $form = str_replace('__ARTICLE978__', $art978, $form);
226
227 // Save file on disk
228 dol_syslog("html_cerfafr::write_file $file");
229 $handle = fopen($file, "w");
230 fwrite($handle, $form);
231 fclose($handle);
232 dolChmod($file);
233
234 $this->result = array('fullpath' => $file);
235
236 return 1;
237 } else {
238 $this->error = $langs->trans("ErrorCanNotCreateDir", $dir);
239 return 0;
240 }
241 } else {
242 $this->error = $langs->trans("ErrorConstantNotDefined", "DON_OUTPUTDIR");
243 return 0;
244 }
245 }
246
255 private function amountToLetters($montant, $devise1 = '', $devise2 = '')
256 {
257 $unite = array();
258 $dix = array();
259 $cent = array();
260 if (empty($devise1)) {
261 $dev1 = 'euros';
262 } else {
263 $dev1 = $devise1;
264 }
265 if (empty($devise2)) {
266 $dev2 = 'centimes';
267 } else {
268 $dev2 = $devise2;
269 }
270 $valeur_entiere = intval($montant);
271 $valeur_decimal = intval(round($montant - intval($montant), 2) * 100);
272 $dix_c = intval($valeur_decimal % 100 / 10);
273 $cent_c = intval($valeur_decimal % 1000 / 100);
274 $unite[1] = $valeur_entiere % 10;
275 $dix[1] = intval($valeur_entiere % 100 / 10);
276 $cent[1] = intval($valeur_entiere % 1000 / 100);
277 $unite[2] = intval($valeur_entiere % 10000 / 1000);
278 $dix[2] = intval($valeur_entiere % 100000 / 10000);
279 $cent[2] = intval($valeur_entiere % 1000000 / 100000);
280 $unite[3] = intval($valeur_entiere % 10000000 / 1000000);
281 $dix[3] = intval($valeur_entiere % 100000000 / 10000000);
282 $cent[3] = intval($valeur_entiere % 1000000000 / 100000000);
283 $chif = array('', 'un', 'deux', 'trois', 'quatre', 'cinq', 'six', 'sept', 'huit', 'neuf', 'dix', 'onze', 'douze', 'treize', 'quatorze', 'quinze', 'seize', 'dix sept', 'dix huit', 'dix neuf');
284 $secon_c = '';
285 $trio_c = '';
286 $prim = array();
287 $secon = array();
288 $trio = array();
289 // @phpstan-ignore-next-line
290 '@phan-var string[] $prim
291 @phan-var string[] $secon
292 @phan-var string[] $trio
293 ';
294 for ($i = 1; $i <= 3; $i++) {
295 $prim[$i] = '';
296 $secon[$i] = '';
297 $trio[$i] = '';
298 if ($dix[$i] == 0) {
299 $secon[$i] = '';
300 $prim[$i] = $chif[$unite[$i]];
301 } elseif ($dix[$i] == 1) {
302 $secon[$i] = '';
303 $prim[$i] = $chif[($unite[$i] + 10)];
304 } elseif ($dix[$i] == 2) {
305 if ($unite[$i] == 1) {
306 $secon[$i] = 'vingt et';
307 $prim[$i] = $chif[$unite[$i]];
308 } else {
309 $secon[$i] = 'vingt';
310 $prim[$i] = $chif[$unite[$i]];
311 }
312 } elseif ($dix[$i] == 3) {
313 if ($unite[$i] == 1) {
314 $secon[$i] = 'trente et';
315 $prim[$i] = $chif[$unite[$i]];
316 } else {
317 $secon[$i] = 'trente';
318 $prim[$i] = $chif[$unite[$i]];
319 }
320 } elseif ($dix[$i] == 4) {
321 if ($unite[$i] == 1) {
322 $secon[$i] = 'quarante et';
323 $prim[$i] = $chif[$unite[$i]];
324 } else {
325 $secon[$i] = 'quarante';
326 $prim[$i] = $chif[$unite[$i]];
327 }
328 } elseif ($dix[$i] == 5) {
329 if ($unite[$i] == 1) {
330 $secon[$i] = 'cinquante et';
331 $prim[$i] = $chif[$unite[$i]];
332 } else {
333 $secon[$i] = 'cinquante';
334 $prim[$i] = $chif[$unite[$i]];
335 }
336 } elseif ($dix[$i] == 6) {
337 if ($unite[$i] == 1) {
338 $secon[$i] = 'soixante et';
339 $prim[$i] = $chif[$unite[$i]];
340 } else {
341 $secon[$i] = 'soixante';
342 $prim[$i] = $chif[$unite[$i]];
343 }
344 } elseif ($dix[$i] == 7) {
345 if ($unite[$i] == 1) {
346 $secon[$i] = 'soixante et';
347 $prim[$i] = $chif[$unite[$i] + 10];
348 } else {
349 $secon[$i] = 'soixante';
350 $prim[$i] = $chif[$unite[$i] + 10];
351 }
352 } elseif ($dix[$i] == 8) {
353 if ($unite[$i] == 1) {
354 $secon[$i] = 'quatre-vingts et';
355 $prim[$i] = $chif[$unite[$i]];
356 } else {
357 $secon[$i] = 'quatre-vingt';
358 $prim[$i] = $chif[$unite[$i]];
359 }
360 } elseif ($dix[$i] == 9) {
361 if ($unite[$i] == 1) {
362 $secon[$i] = 'quatre-vingts et';
363 $prim[$i] = $chif[$unite[$i] + 10];
364 } else {
365 $secon[$i] = 'quatre-vingts';
366 $prim[$i] = $chif[$unite[$i] + 10];
367 }
368 }
369 if ($cent[$i] == 1) {
370 $trio[$i] = 'cent';
371 } elseif ($cent[$i] != 0 || $cent[$i] != '') {
372 $trio[$i] = $chif[$cent[$i]].' cents';
373 }
374 }
375
376
377 $chif2 = array('', 'dix', 'vingt', 'trente', 'quarante', 'cinquante', 'soixante', 'soixante-dix', 'quatre-vingts', 'quatre-vingts dix');
378 $secon_c = $chif2[$dix_c];
379 if ($cent_c == 1) {
380 $trio_c = 'cent';
381 } elseif ($cent_c != 0 || $cent_c != '') {
382 $trio_c = $chif[$cent_c].' cents';
383 }
384
385 if (($cent[3] == 0 || $cent[3] == '') && ($dix[3] == 0 || $dix[3] == '') && ($unite[3] == 1)) {
386 $somme = $trio[3].' '.$secon[3].' '.$prim[3].' million ';
387 } elseif (($cent[3] != 0 && $cent[3] != '') || ($dix[3] != 0 && $dix[3] != '') || ($unite[3] != 0 && $unite[3] != '')) {
388 $somme = $trio[3].' '.$secon[3].' '.$prim[3].' millions ';
389 } else {
390 $somme = $trio[3].' '.$secon[3].' '.$prim[3];
391 }
392
393 if (($cent[2] == 0 || $cent[2] == '') && ($dix[2] == 0 || $dix[2] == '') && ($unite[2] == 1)) {
394 $somme .= ' mille ';
395 } elseif (($cent[2] != 0 && $cent[2] != '') || ($dix[2] != 0 && $dix[2] != '') || ($unite[2] != 0 && $unite[2] != '')) {
396 $somme .= $trio[2].' '.$secon[2].' '.$prim[2].' milles ';
397 } else {
398 $somme .= $trio[2].' '.$secon[2].' '.$prim[2];
399 }
400
401 $somme .= $trio[1].' '.$secon[1].' '.$prim[1];
402
403 $somme .= ' '.$dev1.' ';
404
405 if (($cent_c == '0' || $cent_c == '') && ($dix_c == '0' || $dix_c == '')) {
406 return $somme.' et z&eacute;ro '.$dev2;
407 } else {
408 return $somme.$trio_c.' '.$secon_c.' '.$dev2;
409 }
410 }
411}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
Class to manage donations.
Definition don.class.php:41
Class to manage generation of HTML components Only common components must be here.
Parent class of subscription templates.
Class to generate document for subscriptions.
write_file($don, $outputlangs, $currency='')
Write the object to document file to disk.
isEnabled()
Return if a module can be used or not.
__construct($db)
Constructor.
amountToLetters($montant, $devise1='', $devise2='')
numbers to letters
global $mysoc
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
dol_now($mode='gmt')
Return date for now.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0, $allowdash=0)
Clean a string to use it as a file name.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
dolChmod($filepath, $newmask='')
Change mod of a file.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
make_substitutions($text, $substitutionarray, $outputlangs=null, $converttextinhtmlifnecessary=0)
Make substitution into a text string, replacing keys with vals from $substitutionarray (oldval=>newva...
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
if(preg_match('/(crypted|dolcrypt):/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
'integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]',...
Definition repair.php:125
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:128