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