dolibarr  20.0.0-beta
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 MDW <mdeweerd@users.noreply.github.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <https://www.gnu.org/licenses/>.
22  */
23 
29 require_once DOL_DOCUMENT_ROOT.'/core/modules/dons/modules_don.php';
30 require_once DOL_DOCUMENT_ROOT.'/don/class/don.class.php';
31 require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
32 
33 
37 class html_cerfafr extends ModeleDon
38 {
44  public function __construct($db)
45  {
46  global $conf, $langs;
47 
48  $this->db = $db;
49  $this->name = "cerfafr";
50  $this->description = $langs->trans('DonationsReceiptModel').' - fr_FR - Cerfa 11580*04';
51 
52  // Dimension page for size A4
53  $this->type = 'html';
54  }
55 
56 
62  public function isEnabled()
63  {
64  return true;
65  }
66 
67 
68  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
77  public function write_file($don, $outputlangs, $currency = '')
78  {
79  // phpcs:enable
80  global $user, $conf, $langs, $mysoc;
81 
82  $now = dol_now();
83  $id = (!is_object($don) ? $don : '');
84 
85  if (!is_object($outputlangs)) {
86  $outputlangs = $langs;
87  }
88 
89  // Load traductions files required by page
90  $outputlangs->loadLangs(array("main", "dict", "companies", "bills", "products", "donations"));
91 
92  $currency = !empty($currency) ? $currency : $conf->currency;
93 
94  if (!empty($conf->don->dir_output)) {
95  // Definition of the object don (for upward compatibility)
96  if (!is_object($don)) {
97  $don = new Don($this->db);
98  $ret = $don->fetch($id);
99  $id = $don->id;
100  }
101 
102  // Definition of $dir and $file
103  if (!empty($don->specimen)) {
104  $dir = $conf->don->dir_output;
105  $file = $dir."/SPECIMEN.html";
106  } else {
107  $donref = dol_sanitizeFileName($don->ref);
108  $dir = $conf->don->dir_output."/".$donref;
109  $file = $dir."/".$donref.".html";
110  }
111 
112  if (!file_exists($dir)) {
113  if (dol_mkdir($dir) < 0) {
114  $this->error = $langs->trans("ErrorCanNotCreateDir", $dir);
115  return -1;
116  }
117  }
118 
119  if (file_exists($dir)) {
120  $formclass = new Form($this->db);
121 
122  // This is not the proper way to do it but $formclass->form_modes_reglement
123  // prints the translation instead of returning it
124  $formclass->load_cache_types_paiements();
125  if ($don->modepaymentid) {
126  $paymentmode = $formclass->cache_types_paiements[$don->modepaymentid]['label'];
127  } else {
128  $paymentmode = '';
129  }
130  $modepaymentcode = !empty($formclass->cache_types_paiements[$don->modepaymentid]['code']) ? $formclass->cache_types_paiements[$don->modepaymentid]['code'] : "";
131  if ($modepaymentcode == 'CHQ') {
132  $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>';
133  } elseif ($modepaymentcode == 'LIQ') {
134  $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>';
135  } elseif ($modepaymentcode == 'VIR' || $modepaymentcode == 'PRE' || $modepaymentcode == 'CB') {
136  $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>';
137  } else {
138  $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>';
139  }
140 
141  /*
142  if (empty($don->societe))
143  {
144  $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>';
145  }
146  else
147  {
148  $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>';
149  }
150  */
151 
152  // Define contents
153  $donmodel = DOL_DOCUMENT_ROOT."/core/modules/dons/html_cerfafr.html";
154  $form = implode('', file($donmodel));
155  $form = str_replace('__REF__', (string) $don->id, $form);
156  $form = str_replace('__DATE__', dol_print_date($don->date, 'day', false, $outputlangs), $form);
157  //$form = str_replace('__IP__',$user->ip,$form); // TODO $user->ip not exist
158  $form = str_replace('__AMOUNT__', price($don->amount), $form);
159  $form = str_replace('__AMOUNTLETTERS__', $this->amountToLetters($don->amount), $form);
160  $form = str_replace('__CURRENCY__', $outputlangs->transnoentitiesnoconv("Currency".$currency), $form);
161  $form = str_replace('__CURRENCYCODE__', $conf->currency, $form);
162  $form = str_replace('__MAIN_INFO_SOCIETE_NOM__', $mysoc->name, $form);
163  $form = str_replace('__MAIN_INFO_SOCIETE_ADDRESS__', $mysoc->address, $form);
164  $form = str_replace('__MAIN_INFO_SOCIETE_ZIP__', $mysoc->zip, $form);
165  $form = str_replace('__MAIN_INFO_SOCIETE_TOWN__', $mysoc->town, $form);
166  $form = str_replace('__MAIN_INFO_SOCIETE_OBJECT__', $mysoc->socialobject, $form);
167  $form = str_replace('__DONATOR_FIRSTNAME__', $don->firstname, $form);
168  $form = str_replace('__DONATOR_LASTNAME__', $don->lastname, $form);
169  $form = str_replace('__DONATOR_SOCIETE__', $don->societe, $form);
170  $form = str_replace('__DONATOR_STATUT__', (string) $don->statut, $form);
171  $form = str_replace('__DONATOR_ADDRESS__', $don->address, $form);
172  $form = str_replace('__DONATOR_ZIP__', $don->zip, $form);
173  $form = str_replace('__DONATOR_TOWN__', $don->town, $form);
174  $form = str_replace('__PAYMENTMODE_LIB__ ', $paymentmode, $form);
175  $form = str_replace('__NOW__', dol_print_date($now, 'day', false, $outputlangs), $form);
176  $form = str_replace('__DonationRef__', $outputlangs->trans("DonationRef"), $form);
177  $form = str_replace('__DonationTitle__', $outputlangs->trans("DonationTitle"), $form);
178  $form = str_replace('__DonationReceipt__', $outputlangs->trans("DonationReceipt"), $form);
179  $form = str_replace('__DonationRecipient__', $outputlangs->trans("DonationRecipient"), $form);
180  $form = str_replace('__DonationDatePayment__', $outputlangs->trans("DonationDatePayment"), $form);
181  $form = str_replace('__PaymentMode__', $outputlangs->trans("PaymentMode"), $form);
182  // $form = str_replace('__CodeDon__',$CodeDon,$form);
183  $form = str_replace('__Name__', $outputlangs->trans("Name"), $form);
184  $form = str_replace('__Address__', $outputlangs->trans("Address"), $form);
185  $form = str_replace('__Zip__', $outputlangs->trans("Zip"), $form);
186  $form = str_replace('__Town__', $outputlangs->trans("Town"), $form);
187  $form = str_replace('__Object__', $outputlangs->trans("Object"), $form);
188  $form = str_replace('__Donor__', $outputlangs->trans("Donor"), $form);
189  $form = str_replace('__Date__', $outputlangs->trans("Date"), $form);
190  $form = str_replace('__Signature__', $outputlangs->trans("Signature"), $form);
191  $form = str_replace('__Message__', $outputlangs->trans("Message"), $form);
192  $form = str_replace('__IConfirmDonationReception__', $outputlangs->trans("IConfirmDonationReception"), $form);
193  $form = str_replace('__DonationMessage__', $conf->global->DONATION_MESSAGE, $form);
194 
195  $form = str_replace('__ModePaiement__', $ModePaiement, $form);
196 
197  $frencharticle = '';
198  if (preg_match('/fr/i', $outputlangs->defaultlang)) {
199  $frencharticle = '<font size="+1">Article 200, 238 bis et 978 du code général des impôts (CGI)</font>';
200  }
201  $form = str_replace('__FrenchArticle__', $frencharticle, $form);
202 
203  $frencheligibility = '';
204  if (preg_match('/fr/i', $outputlangs->defaultlang)) {
205  $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 :';
206  }
207  $form = str_replace('__FrenchEligibility__', $frencheligibility, $form);
208 
209  $art200 = '';
210  if ($mysoc->country_code == 'FR') {
211  if (getDolGlobalInt('DONATION_ART200') >= 1) {
212  $art200 = '<input type="checkbox" disabled="true" checked="checked" >200 du CGI';
213  } else {
214  $art200 = '<input type="checkbox" disabled="true">200 du CGI';
215  }
216  }
217  $form = str_replace('__ARTICLE200__', $art200, $form);
218 
219  $art238 = '';
220  if ($mysoc->country_code == 'FR') {
221  if (getDolGlobalInt('DONATION_ART238') >= 1) {
222  $art238 = '<input type="checkbox" disabled="true" checked="checked" >238 bis du CGI';
223  } else {
224  $art238 = '<input type="checkbox" disabled="true">238 bis du CGI';
225  }
226  }
227  $form = str_replace('__ARTICLE238__', $art238, $form);
228 
229  $art978 = '';
230  if ($mysoc->country_code == 'FR') {
231  if (getDolGlobalInt('DONATION_ART978') >= 1) {
232  $art978 = '<input type="checkbox" disabled="true" checked="checked" >978 du CGI';
233  } else {
234  $art978 = '<input type="checkbox" disabled="true">978 du CGI';
235  }
236  }
237  $form = str_replace('__ARTICLE978__', $art978, $form);
238 
239  // Save file on disk
240  dol_syslog("html_cerfafr::write_file $file");
241  $handle = fopen($file, "w");
242  fwrite($handle, $form);
243  fclose($handle);
244  dolChmod($file);
245 
246  $this->result = array('fullpath' => $file);
247 
248  return 1;
249  } else {
250  $this->error = $langs->trans("ErrorCanNotCreateDir", $dir);
251  return 0;
252  }
253  } else {
254  $this->error = $langs->trans("ErrorConstantNotDefined", "DON_OUTPUTDIR");
255  return 0;
256  }
257  }
258 
267  private function amountToLetters($montant, $devise1 = '', $devise2 = '')
268  {
269  $unite = array();
270  $dix = array();
271  $cent = array();
272  if (empty($devise1)) {
273  $dev1 = 'euros';
274  } else {
275  $dev1 = $devise1;
276  }
277  if (empty($devise2)) {
278  $dev2 = 'centimes';
279  } else {
280  $dev2 = $devise2;
281  }
282  $valeur_entiere = intval($montant);
283  $valeur_decimal = intval(round($montant - intval($montant), 2) * 100);
284  $dix_c = intval($valeur_decimal % 100 / 10);
285  $cent_c = intval($valeur_decimal % 1000 / 100);
286  $unite[1] = $valeur_entiere % 10;
287  $dix[1] = intval($valeur_entiere % 100 / 10);
288  $cent[1] = intval($valeur_entiere % 1000 / 100);
289  $unite[2] = intval($valeur_entiere % 10000 / 1000);
290  $dix[2] = intval($valeur_entiere % 100000 / 10000);
291  $cent[2] = intval($valeur_entiere % 1000000 / 100000);
292  $unite[3] = intval($valeur_entiere % 10000000 / 1000000);
293  $dix[3] = intval($valeur_entiere % 100000000 / 10000000);
294  $cent[3] = intval($valeur_entiere % 1000000000 / 100000000);
295  $chif = array('', 'un', 'deux', 'trois', 'quatre', 'cinq', 'six', 'sept', 'huit', 'neuf', 'dix', 'onze', 'douze', 'treize', 'quatorze', 'quinze', 'seize', 'dix sept', 'dix huit', 'dix neuf');
296  $secon_c = '';
297  $trio_c = '';
298  $prim = array();
299  $secon = array();
300  $trio = array();
301  '@phan-var string[] $prim
302  @phan-var string[] $secon
303  @phan-var string[] $trio
304  ';
305  for ($i = 1; $i <= 3; $i++) {
306  $prim[$i] = '';
307  $secon[$i] = '';
308  $trio[$i] = '';
309  if ($dix[$i] == 0) {
310  $secon[$i] = '';
311  $prim[$i] = $chif[$unite[$i]];
312  } elseif ($dix[$i] == 1) {
313  $secon[$i] = '';
314  $prim[$i] = $chif[($unite[$i] + 10)];
315  } elseif ($dix[$i] == 2) {
316  if ($unite[$i] == 1) {
317  $secon[$i] = 'vingt et';
318  $prim[$i] = $chif[$unite[$i]];
319  } else {
320  $secon[$i] = 'vingt';
321  $prim[$i] = $chif[$unite[$i]];
322  }
323  } elseif ($dix[$i] == 3) {
324  if ($unite[$i] == 1) {
325  $secon[$i] = 'trente et';
326  $prim[$i] = $chif[$unite[$i]];
327  } else {
328  $secon[$i] = 'trente';
329  $prim[$i] = $chif[$unite[$i]];
330  }
331  } elseif ($dix[$i] == 4) {
332  if ($unite[$i] == 1) {
333  $secon[$i] = 'quarante et';
334  $prim[$i] = $chif[$unite[$i]];
335  } else {
336  $secon[$i] = 'quarante';
337  $prim[$i] = $chif[$unite[$i]];
338  }
339  } elseif ($dix[$i] == 5) {
340  if ($unite[$i] == 1) {
341  $secon[$i] = 'cinquante et';
342  $prim[$i] = $chif[$unite[$i]];
343  } else {
344  $secon[$i] = 'cinquante';
345  $prim[$i] = $chif[$unite[$i]];
346  }
347  } elseif ($dix[$i] == 6) {
348  if ($unite[$i] == 1) {
349  $secon[$i] = 'soixante et';
350  $prim[$i] = $chif[$unite[$i]];
351  } else {
352  $secon[$i] = 'soixante';
353  $prim[$i] = $chif[$unite[$i]];
354  }
355  } elseif ($dix[$i] == 7) {
356  if ($unite[$i] == 1) {
357  $secon[$i] = 'soixante et';
358  $prim[$i] = $chif[$unite[$i] + 10];
359  } else {
360  $secon[$i] = 'soixante';
361  $prim[$i] = $chif[$unite[$i] + 10];
362  }
363  } elseif ($dix[$i] == 8) {
364  if ($unite[$i] == 1) {
365  $secon[$i] = 'quatre-vingts et';
366  $prim[$i] = $chif[$unite[$i]];
367  } else {
368  $secon[$i] = 'quatre-vingt';
369  $prim[$i] = $chif[$unite[$i]];
370  }
371  } elseif ($dix[$i] == 9) {
372  if ($unite[$i] == 1) {
373  $secon[$i] = 'quatre-vingts et';
374  $prim[$i] = $chif[$unite[$i] + 10];
375  } else {
376  $secon[$i] = 'quatre-vingts';
377  $prim[$i] = $chif[$unite[$i] + 10];
378  }
379  }
380  if ($cent[$i] == 1) {
381  $trio[$i] = 'cent';
382  } elseif ($cent[$i] != 0 || $cent[$i] != '') {
383  $trio[$i] = $chif[$cent[$i]].' cents';
384  }
385  }
386 
387 
388  $chif2 = array('', 'dix', 'vingt', 'trente', 'quarante', 'cinquante', 'soixante', 'soixante-dix', 'quatre-vingts', 'quatre-vingts dix');
389  $secon_c = $chif2[$dix_c];
390  if ($cent_c == 1) {
391  $trio_c = 'cent';
392  } elseif ($cent_c != 0 || $cent_c != '') {
393  $trio_c = $chif[$cent_c].' cents';
394  }
395 
396  if (($cent[3] == 0 || $cent[3] == '') && ($dix[3] == 0 || $dix[3] == '') && ($unite[3] == 1)) {
397  $somme = $trio[3].' '.$secon[3].' '.$prim[3].' million ';
398  } elseif (($cent[3] != 0 && $cent[3] != '') || ($dix[3] != 0 && $dix[3] != '') || ($unite[3] != 0 && $unite[3] != '')) {
399  $somme = $trio[3].' '.$secon[3].' '.$prim[3].' millions ';
400  } else {
401  $somme = $trio[3].' '.$secon[3].' '.$prim[3];
402  }
403 
404  if (($cent[2] == 0 || $cent[2] == '') && ($dix[2] == 0 || $dix[2] == '') && ($unite[2] == 1)) {
405  $somme = $somme.' mille ';
406  } elseif (($cent[2] != 0 && $cent[2] != '') || ($dix[2] != 0 && $dix[2] != '') || ($unite[2] != 0 && $unite[2] != '')) {
407  $somme = $somme.$trio[2].' '.$secon[2].' '.$prim[2].' milles ';
408  } else {
409  $somme = $somme.$trio[2].' '.$secon[2].' '.$prim[2];
410  }
411 
412  $somme = $somme.$trio[1].' '.$secon[1].' '.$prim[1];
413 
414  $somme = $somme.' '.$dev1.' ';
415 
416  if (($cent_c == '0' || $cent_c == '') && ($dix_c == '0' || $dix_c == '')) {
417  return $somme.' et z&eacute;ro '.$dev2;
418  } else {
419  return $somme.$trio_c.' '.$secon_c.' '.$dev2;
420  }
421  }
422 }
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.
Definition: modules_don.php:37
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
print *****$script_file(".$version.") pid cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
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.
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
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:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:123
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition: repair.php:126