dolibarr 25.0.0-alpha
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-2026 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.'/societe/class/societe.class.php';
33require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
34require_once DOL_DOCUMENT_ROOT.'/core/lib/functionsnumtoword.lib.php';
35
36
41{
46 public $version = 'dolibarr';
47
51 public $position = 60;
52
53
59 public function __construct($db)
60 {
61 global $langs;
62
63 $this->db = $db;
64 $this->name = "Cerfa HTML FR";
65 $this->description = $langs->trans('DonationsReceiptModel').' - fr_FR - Cerfa 11580*04';
66
67 // Dimension page for size A4
68 $this->type = 'html';
69 }
70
71
77 public function isEnabled()
78 {
79 return true;
80 }
81
82
83 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
92 public function write_file($don, $outputlangs, $currency = '')
93 {
94 // phpcs:enable
95 global $user, $conf, $langs, $mysoc;
96
97 $now = dol_now();
98 $id = (!is_object($don) ? $don : '');
99
100 if (!is_object($outputlangs)) {
101 $outputlangs = $langs;
102 }
103
104 // Load traductions files required by page
105 $outputlangs->loadLangs(array("main", "dict", "companies", "bills", "products", "donations"));
106
107 $currency = !empty($currency) ? $currency : $conf->currency;
108
109 if (!empty($conf->don->dir_output)) {
110 // Definition of the object don (for upward compatibility)
111 if (!is_object($don)) {
112 $don = new Don($this->db);
113 $ret = $don->fetch($id);
114 $id = $don->id;
115 }
116
117 // Definition of $dir and $file
118 if (!empty($don->specimen)) {
119 $dir = $conf->don->dir_output;
120 $file = $dir."/SPECIMEN.html";
121 } else {
122 $donref = dol_sanitizeFileName((string) $don->ref);
123 $dir = $conf->don->dir_output."/".$donref;
124 $file = $dir."/".$donref.".html";
125 }
126
127 if (!file_exists($dir)) {
128 if (dol_mkdir($dir) < 0) {
129 $this->error = $langs->trans("ErrorCanNotCreateDir", $dir);
130 return -1;
131 }
132 }
133
134 if (file_exists($dir)) {
135 $formclass = new Form($this->db);
136
137 // This is not the proper way to do it but $formclass->form_modes_reglement
138 // prints the translation instead of returning it
139 $formclass->load_cache_types_paiements();
140 // Don::fetch() stores the payment mode into mode_reglement_id. The modepaymentid property is only
141 // set by the create/update forms, so it is always empty when the document is built from a fetched
142 // donation and no payment mode checkbox was ever ticked on the receipt.
143 $modepaymentid = !empty($don->mode_reglement_id) ? $don->mode_reglement_id : $don->modepaymentid;
144 if ($modepaymentid) {
145 $paymentmode = !empty($formclass->cache_types_paiements[(int) $modepaymentid]['label']) ? $formclass->cache_types_paiements[$modepaymentid]['label'] : '';
146 } else {
147 $paymentmode = '';
148 }
149 $modepaymentcode = !empty($formclass->cache_types_paiements[(int) $modepaymentid]['code']) ? $formclass->cache_types_paiements[(int) $modepaymentid]['code'] : "";
150 if ($modepaymentcode == 'CHQ') {
151 $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>';
152 } elseif ($modepaymentcode == 'LIQ') {
153 $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>';
154 } elseif ($modepaymentcode == 'VIR' || $modepaymentcode == 'PRE' || $modepaymentcode == 'CB') {
155 $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>';
156 } else {
157 $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>';
158 }
159
160 /*
161 if (empty($don->societe))
162 {
163 $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>';
164 }
165 else
166 {
167 $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>';
168 }
169 */
170 // Donator identity.
171 // When option DONATION_USE_THIRDPARTIES is on, the donation form does not show the name/address
172 // inputs at all, so llx_don only holds fk_soc and every donator field stays empty. The receipt
173 // would then be issued without the mandatory identity of the donator, so fall back on the linked
174 // third party for each field left empty.
175 $donatorsociete = (string) $don->societe;
176 $donatorlastname = (string) $don->lastname;
177 $donatorfirstname = (string) $don->firstname;
178 $donatoraddress = (string) $don->address;
179 $donatorzip = (string) $don->zip;
180 $donatortown = (string) $don->town;
181
182 if (!empty($don->socid) && $don->socid > 0) {
183 $donatorthirdparty = new Societe($this->db);
184 if ($donatorthirdparty->fetch($don->socid) > 0) {
185 if (dol_strlen(trim($donatorsociete.$donatorlastname.$donatorfirstname)) == 0) {
186 // A third party holds a single name field, even for a private individual, so it goes
187 // to the "Name" cell of the form and the "Firstname" cell is left empty.
188 $donatorsociete = (string) $donatorthirdparty->name;
189 }
190 if (dol_strlen(trim($donatoraddress)) == 0) {
191 $donatoraddress = (string) $donatorthirdparty->address;
192 }
193 if (dol_strlen(trim($donatorzip)) == 0) {
194 $donatorzip = (string) $donatorthirdparty->zip;
195 }
196 if (dol_strlen(trim($donatortown)) == 0) {
197 $donatortown = (string) $donatorthirdparty->town;
198 }
199 } else {
200 dol_syslog("html_cerfafr::write_file Failed to load thirdparty ".$don->socid." linked to donation ".$don->id.", donator block will remain empty - ".$donatorthirdparty->error, LOG_ERR);
201 }
202 }
203
204 if (dol_strlen(trim($donatorsociete.$donatorlastname.$donatorfirstname)) == 0) {
205 dol_syslog("html_cerfafr::write_file No name found for the donator of donation ".$don->id.", the receipt will not be compliant", LOG_WARNING);
206 }
207
208 // Define contents
209 $donmodel = DOL_DOCUMENT_ROOT."/core/modules/dons/html_cerfafr.html";
210
211 $form = file_get_contents($donmodel);
212
213 // TODO Remove what was already included into getCommonSubstitutionArray / make_substitutions
214 $form = str_replace('__REF__', (string) $don->id, $form);
215 $form = str_replace('__DATE__', dol_print_date($don->date, 'day', false, $outputlangs), $form);
216 //$form = str_replace('__IP__',$user->ip,$form); // TODO $user->ip not exist
217 $form = str_replace('__AMOUNT__', price($don->amount), $form);
218 $form = str_replace('__AMOUNT_TEXT__', dol_convertToWord($don->amount, $outputlangs, $outputlangs->transnoentitiesnoconv("Currency".$currency), true), $form);
219
220 $form = str_replace('__DONATOR_FIRSTNAME__', dol_escape_htmltag($donatorfirstname), $form);
221 // The template concatenates __DONATOR_SOCIETE__ and __DONATOR_LASTNAME__ with no separator,
222 // so add a line break when both are filled in.
223 $donatorlastnameprefix = (dol_strlen(trim($donatorsociete)) > 0 && dol_strlen(trim($donatorlastname)) > 0) ? '<br>' : '';
224 $form = str_replace('__DONATOR_LASTNAME__', $donatorlastnameprefix.dol_escape_htmltag($donatorlastname), $form);
225 $form = str_replace('__DONATOR_SOCIETE__', dol_escape_htmltag($donatorsociete), $form);
226
227 $form = str_replace('__DONATOR_STATUT__', (string) $don->statut, $form);
228 $form = str_replace('__DONATOR_ADDRESS__', dol_nl2br(dol_escape_htmltag($donatoraddress, 0, 1)), $form);
229 $form = str_replace('__DONATOR_ZIP__', dol_escape_htmltag($donatorzip), $form);
230 $form = str_replace('__DONATOR_TOWN__', dol_escape_htmltag($donatortown), $form);
231
232 $form = str_replace('__PAYMENTMODE_LIB__ ', (string) $paymentmode, $form);
233 $form = str_replace('__NOW__', dol_print_date($now, 'day', false, $outputlangs), $form);
234 $form = str_replace('__DONATION_PAYMENT_MODE__', $ModePaiement, $form);
235 $form = str_replace('__DONATION_MESSAGE__', getDolGlobalString('DONATION_MESSAGE'), $form);
236
237 // Replace with a generic replacement feature '__(XXX)__' must become $outputlangs->trans('XXX')
238 $substitutionkey = getCommonSubstitutionArray($outputlangs, 0, null, $don);
239
240 $form = make_substitutions($form, $substitutionkey, $outputlangs);
241
242
243 $frencharticle = '';
244 if (preg_match('/fr/i', $outputlangs->defaultlang)) {
245 $frencharticle = '<font size="+1">Article 200, 238 bis et 978 du code général des impôts (CGI)</font>';
246 }
247 $form = str_replace('__FrenchArticle__', $frencharticle, $form);
248
249 $frencheligibility = '';
250 if (preg_match('/fr/i', $outputlangs->defaultlang)) {
251 $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 :';
252 }
253 $form = str_replace('__FrenchEligibility__', $frencheligibility, $form);
254
255 $art200 = '';
256 if ($mysoc->country_code == 'FR') {
257 if (getDolGlobalInt('DONATION_ART200') >= 1) {
258 $art200 = '<input type="checkbox" disabled="true" checked="checked" >200 du CGI';
259 } else {
260 $art200 = '<input type="checkbox" disabled="true">200 du CGI';
261 }
262 }
263 $form = str_replace('__ARTICLE200__', $art200, $form);
264
265 $art238 = '';
266 if ($mysoc->country_code == 'FR') {
267 if (getDolGlobalInt('DONATION_ART238') >= 1) {
268 $art238 = '<input type="checkbox" disabled="true" checked="checked" >238 bis du CGI';
269 } else {
270 $art238 = '<input type="checkbox" disabled="true">238 bis du CGI';
271 }
272 }
273 $form = str_replace('__ARTICLE238__', $art238, $form);
274
275 $art978 = '';
276 if ($mysoc->country_code == 'FR') {
277 if (getDolGlobalInt('DONATION_ART978') >= 1) {
278 $art978 = '<input type="checkbox" disabled="true" checked="checked" >978 du CGI';
279 } else {
280 $art978 = '<input type="checkbox" disabled="true">978 du CGI';
281 }
282 }
283 $form = str_replace('__ARTICLE978__', $art978, $form);
284
285 // Save file on disk
286 dol_syslog("html_cerfafr::write_file $file");
287 $handle = fopen($file, "w");
288 fwrite($handle, $form);
289 fclose($handle);
290 dolChmod($file);
291
292 $this->result = array('fullpath' => $file);
293
294 return 1;
295 } else {
296 $this->error = $langs->trans("ErrorCanNotCreateDir", $dir);
297 return 0;
298 }
299 } else {
300 $this->error = $langs->trans("ErrorConstantNotDefined", "DON_OUTPUTDIR");
301 return 0;
302 }
303 }
304}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
$propal type
'integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]',...
Definition propal.php:281
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
Class to manage donations.
Definition don.class.php:42
Class to manage generation of HTML components Only common components must be here.
Parent class of subscription templates.
Class to manage third parties objects (customers, suppliers, prospects...)
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.
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_strlen($string, $stringencoding='UTF-8', $binarycount=0)
Make a strlen call.
dol_nl2br($stringtoencode, $nl2brmode=0, $forxml=false)
Replace CRLF in string with a HTML BR tag.
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)
dol_convertToWord($num, $langs, $currency='', $centimes=false)
Function to return a number into a text.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Definition html.lib.php:183
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:165