html_cerfafr.modules.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. /**
  23. * \file htdocs/core/modules/dons/html_cerfafr.modules.php
  24. * \ingroup don
  25. * \brief Form of donation
  26. */
  27. require_once DOL_DOCUMENT_ROOT.'/core/modules/dons/modules_don.php';
  28. require_once DOL_DOCUMENT_ROOT.'/don/class/don.class.php';
  29. require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  30. /**
  31. * Class to generate document for subscriptions
  32. */
  33. class html_cerfafr extends ModeleDon
  34. {
  35. /**
  36. * Constructor
  37. *
  38. * @param DoliDb $db Database handler
  39. */
  40. public function __construct($db)
  41. {
  42. global $conf, $langs;
  43. $this->db = $db;
  44. $this->name = "cerfafr";
  45. $this->description = $langs->trans('DonationsReceiptModel').' - fr_FR - Cerfa 11580*04';
  46. // Dimension page for size A4
  47. $this->type = 'html';
  48. }
  49. /**
  50. * Return if a module can be used or not
  51. *
  52. * @return boolean true if module can be used
  53. */
  54. public function isEnabled()
  55. {
  56. return true;
  57. }
  58. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  59. /**
  60. * Write the object to document file to disk
  61. *
  62. * @param Don $don Donation object
  63. * @param Translate $outputlangs Lang object for output language
  64. * @param string $currency Currency code
  65. * @return int >0 if OK, <0 if KO
  66. */
  67. public function write_file($don, $outputlangs, $currency = '')
  68. {
  69. // phpcs:enable
  70. global $user, $conf, $langs, $mysoc;
  71. $now = dol_now();
  72. $id = (!is_object($don) ? $don : '');
  73. if (!is_object($outputlangs)) {
  74. $outputlangs = $langs;
  75. }
  76. // Load traductions files required by page
  77. $outputlangs->loadLangs(array("main", "dict", "companies", "bills", "products", "donations"));
  78. $currency = !empty($currency) ? $currency : $conf->currency;
  79. if (!empty($conf->don->dir_output)) {
  80. // Definition of the object don (for upward compatibility)
  81. if (!is_object($don)) {
  82. $don = new Don($this->db);
  83. $ret = $don->fetch($id);
  84. $id = $don->id;
  85. }
  86. // Definition of $dir and $file
  87. if (!empty($don->specimen)) {
  88. $dir = $conf->don->dir_output;
  89. $file = $dir."/SPECIMEN.html";
  90. } else {
  91. $donref = dol_sanitizeFileName($don->ref);
  92. $dir = $conf->don->dir_output."/".$donref;
  93. $file = $dir."/".$donref.".html";
  94. }
  95. if (!file_exists($dir)) {
  96. if (dol_mkdir($dir) < 0) {
  97. $this->error = $langs->trans("ErrorCanNotCreateDir", $dir);
  98. return -1;
  99. }
  100. }
  101. if (file_exists($dir)) {
  102. $formclass = new Form($this->db);
  103. // This is not the proper way to do it but $formclass->form_modes_reglement
  104. // prints the translation instead of returning it
  105. $formclass->load_cache_types_paiements();
  106. if ($don->modepaymentid) {
  107. $paymentmode = $formclass->cache_types_paiements[$don->modepaymentid]['label'];
  108. } else {
  109. $paymentmode = '';
  110. }
  111. $modepaymentcode = !empty($formclass->cache_types_paiements[$don->modepaymentid]['code']) ? $formclass->cache_types_paiements[$don->modepaymentid]['code'] : "";
  112. if ($modepaymentcode == 'CHQ') {
  113. $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>';
  114. } elseif ($modepaymentcode == 'LIQ') {
  115. $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>';
  116. } elseif ($modepaymentcode == 'VIR' || $modepaymentcode == 'PRE' || $modepaymentcode == 'CB') {
  117. $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>';
  118. } else {
  119. $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>';
  120. }
  121. /*
  122. if (empty($don->societe))
  123. {
  124. $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>';
  125. }
  126. else
  127. {
  128. $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>';
  129. }
  130. */
  131. // Define contents
  132. $donmodel = DOL_DOCUMENT_ROOT."/core/modules/dons/html_cerfafr.html";
  133. $form = implode('', file($donmodel));
  134. $form = str_replace('__REF__', $don->id, $form);
  135. $form = str_replace('__DATE__', dol_print_date($don->date, 'day', false, $outputlangs), $form);
  136. //$form = str_replace('__IP__',$user->ip,$form); // TODO $user->ip not exist
  137. $form = str_replace('__AMOUNT__', price($don->amount), $form);
  138. $form = str_replace('__AMOUNTLETTERS__', $this->amountToLetters($don->amount), $form);
  139. $form = str_replace('__CURRENCY__', $outputlangs->transnoentitiesnoconv("Currency".$currency), $form);
  140. $form = str_replace('__CURRENCYCODE__', $conf->currency, $form);
  141. $form = str_replace('__MAIN_INFO_SOCIETE_NOM__', $mysoc->name, $form);
  142. $form = str_replace('__MAIN_INFO_SOCIETE_ADDRESS__', $mysoc->address, $form);
  143. $form = str_replace('__MAIN_INFO_SOCIETE_ZIP__', $mysoc->zip, $form);
  144. $form = str_replace('__MAIN_INFO_SOCIETE_TOWN__', $mysoc->town, $form);
  145. $form = str_replace('__MAIN_INFO_SOCIETE_OBJECT__', $mysoc->object, $form);
  146. $form = str_replace('__DONATOR_FIRSTNAME__', $don->firstname, $form);
  147. $form = str_replace('__DONATOR_LASTNAME__', $don->lastname, $form);
  148. $form = str_replace('__DONATOR_SOCIETE__', $don->societe, $form);
  149. $form = str_replace('__DONATOR_STATUT__', $don->statut, $form);
  150. $form = str_replace('__DONATOR_ADDRESS__', $don->address, $form);
  151. $form = str_replace('__DONATOR_ZIP__', $don->zip, $form);
  152. $form = str_replace('__DONATOR_TOWN__', $don->town, $form);
  153. $form = str_replace('__PAYMENTMODE_LIB__ ', $paymentmode, $form);
  154. $form = str_replace('__NOW__', dol_print_date($now, 'day', false, $outputlangs), $form);
  155. $form = str_replace('__DonationRef__', $outputlangs->trans("DonationRef"), $form);
  156. $form = str_replace('__DonationTitle__', $outputlangs->trans("DonationTitle"), $form);
  157. $form = str_replace('__DonationReceipt__', $outputlangs->trans("DonationReceipt"), $form);
  158. $form = str_replace('__DonationRecipient__', $outputlangs->trans("DonationRecipient"), $form);
  159. $form = str_replace('__DonationDatePayment__', $outputlangs->trans("DonationDatePayment"), $form);
  160. $form = str_replace('__PaymentMode__', $outputlangs->trans("PaymentMode"), $form);
  161. // $form = str_replace('__CodeDon__',$CodeDon,$form);
  162. $form = str_replace('__Name__', $outputlangs->trans("Name"), $form);
  163. $form = str_replace('__Address__', $outputlangs->trans("Address"), $form);
  164. $form = str_replace('__Zip__', $outputlangs->trans("Zip"), $form);
  165. $form = str_replace('__Town__', $outputlangs->trans("Town"), $form);
  166. $form = str_replace('__Object__', $outputlangs->trans("Object"), $form);
  167. $form = str_replace('__Donor__', $outputlangs->trans("Donor"), $form);
  168. $form = str_replace('__Date__', $outputlangs->trans("Date"), $form);
  169. $form = str_replace('__Signature__', $outputlangs->trans("Signature"), $form);
  170. $form = str_replace('__Message__', $outputlangs->trans("Message"), $form);
  171. $form = str_replace('__IConfirmDonationReception__', $outputlangs->trans("IConfirmDonationReception"), $form);
  172. $form = str_replace('__DonationMessage__', $conf->global->DONATION_MESSAGE, $form);
  173. $form = str_replace('__ModePaiement__', $ModePaiement, $form);
  174. $frencharticle = '';
  175. if (preg_match('/fr/i', $outputlangs->defaultlang)) {
  176. $frencharticle = '<font size="+1">Article 200, 238 bis et 978 du code général des impôts (CGI)</font>';
  177. }
  178. $form = str_replace('__FrenchArticle__', $frencharticle, $form);
  179. $frencheligibility = '';
  180. if (preg_match('/fr/i', $outputlangs->defaultlang)) {
  181. $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 :';
  182. }
  183. $form = str_replace('__FrenchEligibility__', $frencheligibility, $form);
  184. $art200 = '';
  185. if ($mysoc->country_code == 'FR') {
  186. if ($conf->global->DONATION_ART200 >= 1) {
  187. $art200 = '<input type="checkbox" disabled="true" checked="checked" >200 du CGI';
  188. } else {
  189. $art200 = '<input type="checkbox" disabled="true">200 du CGI';
  190. }
  191. }
  192. $form = str_replace('__ARTICLE200__', $art200, $form);
  193. $art238 = '';
  194. if ($mysoc->country_code == 'FR') {
  195. if ($conf->global->DONATION_ART238 >= 1) {
  196. $art238 = '<input type="checkbox" disabled="true" checked="checked" >238 bis du CGI';
  197. } else {
  198. $art238 = '<input type="checkbox" disabled="true">238 bis du CGI';
  199. }
  200. }
  201. $form = str_replace('__ARTICLE238__', $art238, $form);
  202. $art978 = '';
  203. if ($mysoc->country_code == 'FR') {
  204. if ($conf->global->DONATION_ART978 >= 1) {
  205. $art978 = '<input type="checkbox" disabled="true" checked="checked" >978 du CGI';
  206. } else {
  207. $art978 = '<input type="checkbox" disabled="true">978 du CGI';
  208. }
  209. }
  210. $form = str_replace('__ARTICLE978__', $art978, $form);
  211. // Save file on disk
  212. dol_syslog("html_cerfafr::write_file $file");
  213. $handle = fopen($file, "w");
  214. fwrite($handle, $form);
  215. fclose($handle);
  216. if (!empty($conf->global->MAIN_UMASK)) {
  217. @chmod($file, octdec($conf->global->MAIN_UMASK));
  218. }
  219. $this->result = array('fullpath'=>$file);
  220. return 1;
  221. } else {
  222. $this->error = $langs->trans("ErrorCanNotCreateDir", $dir);
  223. return 0;
  224. }
  225. } else {
  226. $this->error = $langs->trans("ErrorConstantNotDefined", "DON_OUTPUTDIR");
  227. return 0;
  228. }
  229. }
  230. /**
  231. * numbers to letters
  232. *
  233. * @param mixed $montant amount
  234. * @param mixed $devise1 devise 1 ex: euro
  235. * @param mixed $devise2 devise 2 ex: centimes
  236. * @return string amount in letters
  237. */
  238. private function amountToLetters($montant, $devise1 = '', $devise2 = '')
  239. {
  240. $unite = array();
  241. $dix = array();
  242. $cent = array();
  243. if (empty($devise1)) {
  244. $dev1 = 'euros';
  245. } else {
  246. $dev1 = $devise1;
  247. }
  248. if (empty($devise2)) {
  249. $dev2 = 'centimes';
  250. } else {
  251. $dev2 = $devise2;
  252. }
  253. $valeur_entiere = intval($montant);
  254. $valeur_decimal = intval(round($montant - intval($montant), 2) * 100);
  255. $dix_c = intval($valeur_decimal % 100 / 10);
  256. $cent_c = intval($valeur_decimal % 1000 / 100);
  257. $unite[1] = $valeur_entiere % 10;
  258. $dix[1] = intval($valeur_entiere % 100 / 10);
  259. $cent[1] = intval($valeur_entiere % 1000 / 100);
  260. $unite[2] = intval($valeur_entiere % 10000 / 1000);
  261. $dix[2] = intval($valeur_entiere % 100000 / 10000);
  262. $cent[2] = intval($valeur_entiere % 1000000 / 100000);
  263. $unite[3] = intval($valeur_entiere % 10000000 / 1000000);
  264. $dix[3] = intval($valeur_entiere % 100000000 / 10000000);
  265. $cent[3] = intval($valeur_entiere % 1000000000 / 100000000);
  266. $chif = array('', 'un', 'deux', 'trois', 'quatre', 'cinq', 'six', 'sept', 'huit', 'neuf', 'dix', 'onze', 'douze', 'treize', 'quatorze', 'quinze', 'seize', 'dix sept', 'dix huit', 'dix neuf');
  267. $secon_c = '';
  268. $trio_c = '';
  269. for ($i = 1; $i <= 3; $i++) {
  270. $prim[$i] = '';
  271. $secon[$i] = '';
  272. $trio[$i] = '';
  273. if ($dix[$i] == 0) {
  274. $secon[$i] = '';
  275. $prim[$i] = $chif[$unite[$i]];
  276. } elseif ($dix[$i] == 1) {
  277. $secon[$i] = '';
  278. $prim[$i] = $chif[($unite[$i] + 10)];
  279. } elseif ($dix[$i] == 2) {
  280. if ($unite[$i] == 1) {
  281. $secon[$i] = 'vingt et';
  282. $prim[$i] = $chif[$unite[$i]];
  283. } else {
  284. $secon[$i] = 'vingt';
  285. $prim[$i] = $chif[$unite[$i]];
  286. }
  287. } elseif ($dix[$i] == 3) {
  288. if ($unite[$i] == 1) {
  289. $secon[$i] = 'trente et';
  290. $prim[$i] = $chif[$unite[$i]];
  291. } else {
  292. $secon[$i] = 'trente';
  293. $prim[$i] = $chif[$unite[$i]];
  294. }
  295. } elseif ($dix[$i] == 4) {
  296. if ($unite[$i] == 1) {
  297. $secon[$i] = 'quarante et';
  298. $prim[$i] = $chif[$unite[$i]];
  299. } else {
  300. $secon[$i] = 'quarante';
  301. $prim[$i] = $chif[$unite[$i]];
  302. }
  303. } elseif ($dix[$i] == 5) {
  304. if ($unite[$i] == 1) {
  305. $secon[$i] = 'cinquante et';
  306. $prim[$i] = $chif[$unite[$i]];
  307. } else {
  308. $secon[$i] = 'cinquante';
  309. $prim[$i] = $chif[$unite[$i]];
  310. }
  311. } elseif ($dix[$i] == 6) {
  312. if ($unite[$i] == 1) {
  313. $secon[$i] = 'soixante et';
  314. $prim[$i] = $chif[$unite[$i]];
  315. } else {
  316. $secon[$i] = 'soixante';
  317. $prim[$i] = $chif[$unite[$i]];
  318. }
  319. } elseif ($dix[$i] == 7) {
  320. if ($unite[$i] == 1) {
  321. $secon[$i] = 'soixante et';
  322. $prim[$i] = $chif[$unite[$i] + 10];
  323. } else {
  324. $secon[$i] = 'soixante';
  325. $prim[$i] = $chif[$unite[$i] + 10];
  326. }
  327. } elseif ($dix[$i] == 8) {
  328. if ($unite[$i] == 1) {
  329. $secon[$i] = 'quatre-vingts et';
  330. $prim[$i] = $chif[$unite[$i]];
  331. } else {
  332. $secon[$i] = 'quatre-vingt';
  333. $prim[$i] = $chif[$unite[$i]];
  334. }
  335. } elseif ($dix[$i] == 9) {
  336. if ($unite[$i] == 1) {
  337. $secon[$i] = 'quatre-vingts et';
  338. $prim[$i] = $chif[$unite[$i] + 10];
  339. } else {
  340. $secon[$i] = 'quatre-vingts';
  341. $prim[$i] = $chif[$unite[$i] + 10];
  342. }
  343. }
  344. if ($cent[$i] == 1) {
  345. $trio[$i] = 'cent';
  346. } elseif ($cent[$i] != 0 || $cent[$i] != '') {
  347. $trio[$i] = $chif[$cent[$i]].' cents';
  348. }
  349. }
  350. $chif2 = array('', 'dix', 'vingt', 'trente', 'quarante', 'cinquante', 'soixante', 'soixante-dix', 'quatre-vingts', 'quatre-vingts dix');
  351. $secon_c = $chif2[$dix_c];
  352. if ($cent_c == 1) {
  353. $trio_c = 'cent';
  354. } elseif ($cent_c != 0 || $cent_c != '') {
  355. $trio_c = $chif[$cent_c].' cents';
  356. }
  357. if (($cent[3] == 0 || $cent[3] == '') && ($dix[3] == 0 || $dix[3] == '') && ($unite[3] == 1)) {
  358. $somme = $trio[3].' '.$secon[3].' '.$prim[3].' million ';
  359. } elseif (($cent[3] != 0 && $cent[3] != '') || ($dix[3] != 0 && $dix[3] != '') || ($unite[3] != 0 && $unite[3] != '')) {
  360. $somme = $trio[3].' '.$secon[3].' '.$prim[3].' millions ';
  361. } else {
  362. $somme = $trio[3].' '.$secon[3].' '.$prim[3];
  363. }
  364. if (($cent[2] == 0 || $cent[2] == '') && ($dix[2] == 0 || $dix[2] == '') && ($unite[2] == 1)) {
  365. $somme = $somme.' mille ';
  366. } elseif (($cent[2] != 0 && $cent[2] != '') || ($dix[2] != 0 && $dix[2] != '') || ($unite[2] != 0 && $unite[2] != '')) {
  367. $somme = $somme.$trio[2].' '.$secon[2].' '.$prim[2].' milles ';
  368. } else {
  369. $somme = $somme.$trio[2].' '.$secon[2].' '.$prim[2];
  370. }
  371. $somme = $somme.$trio[1].' '.$secon[1].' '.$prim[1];
  372. $somme = $somme.' '.$dev1.' ';
  373. if (($cent_c == '0' || $cent_c == '') && ($dix_c == '0' || $dix_c == '')) {
  374. return $somme.' et z&eacute;ro '.$dev2;
  375. } else {
  376. return $somme.$trio_c.' '.$secon_c.' '.$dev2;
  377. }
  378. }
  379. }