modules_facture.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. /* Copyright (C) 2003-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2004 Eric Seigne <eric.seigne@ryxeo.com>
  5. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
  6. * Copyright (C) 2014 Marcos García <marcosgdf@gmail.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. * or see https://www.gnu.org/
  21. */
  22. /**
  23. * \file htdocs/core/modules/facture/modules_facture.php
  24. * \ingroup facture
  25. * \brief File that contains parent class for invoices models
  26. * and parent class for invoices numbering models
  27. */
  28. require_once DOL_DOCUMENT_ROOT.'/core/class/commondocgenerator.class.php';
  29. require_once DOL_DOCUMENT_ROOT.'/core/class/commonnumrefgenerator.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php'; // Required because used in classes that inherit
  32. // For the experimental feature using swiss QR invoice generated by composer lib sparin/swiss-qr-bill
  33. use Sprain\SwissQrBill;
  34. /**
  35. * Parent class of invoice document generators
  36. */
  37. abstract class ModelePDFFactures extends CommonDocGenerator
  38. {
  39. public $posxpicture;
  40. public $posxtva;
  41. public $posxup;
  42. public $posxqty;
  43. public $posxunit;
  44. public $posxdesc;
  45. public $posxdiscount;
  46. public $postotalht;
  47. public $tva;
  48. public $tva_array;
  49. public $localtax1;
  50. public $localtax2;
  51. public $atleastonediscount = 0;
  52. public $atleastoneratenotnull = 0;
  53. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  54. /**
  55. * Return list of active generation modules
  56. *
  57. * @param DoliDB $db Database handler
  58. * @param integer $maxfilenamelength Max length of value to show
  59. * @return array List of templates
  60. */
  61. public static function liste_modeles($db, $maxfilenamelength = 0)
  62. {
  63. // phpcs:enable
  64. $type = 'invoice';
  65. $list = array();
  66. include_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  67. $list = getListOfModels($db, $type, $maxfilenamelength);
  68. return $list;
  69. }
  70. /**
  71. * Get the SwissQR object, including validation
  72. *
  73. * @param Facture $object Invoice object
  74. * @param Translate $langs Translation object
  75. * @return SwissQrBill\QrBill|bool The valid SwissQR object, or false
  76. */
  77. private function getSwissQrBill(Facture $object, Translate $langs)
  78. {
  79. global $conf;
  80. if (getDolGlobalString('INVOICE_ADD_SWISS_QR_CODE') != 'bottom') {
  81. return false;
  82. }
  83. if ($object->mode_reglement_code != 'VIR') {
  84. $this->error = $langs->transnoentities("SwissQrOnlyVIR");
  85. return false;
  86. }
  87. if (empty($object->fk_account)) {
  88. $this->error = 'Bank account must be defined to use this experimental feature';
  89. return false;
  90. }
  91. // Load the autoload file generated by composer
  92. if (file_exists(DOL_DOCUMENT_ROOT.'/includes/sprain/swiss-qr-bill/autoload.php')) {
  93. require_once DOL_DOCUMENT_ROOT.'/includes/sprain/swiss-qr-bill/autoload.php';
  94. } elseif (file_exists(DOL_DOCUMENT_ROOT.'/includes/autoload.php')) {
  95. require_once DOL_DOCUMENT_ROOT.'/includes/autoload.php';
  96. } else {
  97. $this->error = 'PHP library sprain/swiss-qr-bill was not found. Please install it with:<br>cd '.dirname(DOL_DOCUMENT_ROOT).'; cp composer.json.disabled composer.json; composer require sprain/swiss-qr-bill;';
  98. return false;
  99. }
  100. // Create a new instance of SwissQrBill, containing default headers with fixed values
  101. $qrBill = SwissQrBill\QrBill::create();
  102. // First, set creditor address
  103. $address = SwissQrBill\DataGroup\Element\CombinedAddress::create(
  104. $this->emetteur->name,
  105. $this->emetteur->address,
  106. $this->emetteur->zip . " " . $this->emetteur->town,
  107. $this->emetteur->country_code
  108. );
  109. if (!$address->isValid()) {
  110. $this->error = $langs->transnoentities("SwissQrCreditorAddressInvalid", (string) $address->getViolations());
  111. return false;
  112. }
  113. $qrBill->setCreditor($address);
  114. // Get IBAN from account.
  115. $account = new Account($this->db);
  116. $account->fetch($object->fk_account);
  117. $creditorInformation = SwissQrBill\DataGroup\Element\CreditorInformation::create($account->iban);
  118. if (!$creditorInformation->isValid()) {
  119. $langs->load("errors");
  120. $this->error = $langs->transnoentities("SwissQrCreditorInformationInvalid", $account->iban, (string) $creditorInformation->getViolations());
  121. return false;
  122. }
  123. $qrBill->setCreditorInformation($creditorInformation);
  124. if ($creditorInformation->containsQrIban()) {
  125. $this->error = $langs->transnoentities("SwissQrIbanNotImplementedYet", $account->iban);
  126. return false;
  127. }
  128. // Add payment reference CLASSIC-IBAN
  129. // This is what you will need to identify incoming payments.
  130. $qrBill->setPaymentReference(
  131. SwissQrBill\DataGroup\Element\PaymentReference::create(
  132. SwissQrBill\DataGroup\Element\PaymentReference::TYPE_NON
  133. )
  134. );
  135. $currencyinvoicecode = $object->multicurrency_code ? $object->multicurrency_code : $conf->currency;
  136. // Add payment amount, with currency
  137. $pai = SwissQrBill\DataGroup\Element\PaymentAmountInformation::create($currencyinvoicecode, $object->total_ttc);
  138. if (!$pai->isValid()) {
  139. $this->error = $langs->transnoentities("SwissQrPaymentInformationInvalid", $object->total_ttc, (string) $pai->getViolations());
  140. return false;
  141. }
  142. $qrBill->setPaymentAmountInformation($pai);
  143. // Add some human-readable information about what the bill is for.
  144. $qrBill->setAdditionalInformation(
  145. SwissQrBill\DataGroup\Element\AdditionalInformation::create(
  146. $object->ref
  147. )
  148. );
  149. // Set debtor address; We _know_ zip&town have to be filled, so skip that if unfilled.
  150. if (!empty($object->thirdparty->zip) && !empty($object->thirdparty->town)) {
  151. $address = SwissQrBill\DataGroup\Element\CombinedAddress::create(
  152. $object->thirdparty->name,
  153. $object->thirdparty->address,
  154. $object->thirdparty->zip . " " . $object->thirdparty->town,
  155. $object->thirdparty->country_code
  156. );
  157. if (!$address->isValid()) {
  158. $this->error = $langs->transnoentities("SwissQrDebitorAddressInvalid", (string) $address->getViolations());
  159. return false;
  160. }
  161. $qrBill->setUltimateDebtor($address);
  162. }
  163. return $qrBill;
  164. }
  165. /**
  166. * Get the height for bottom-page QR invoice in mm, depending on the page number.
  167. *
  168. * @param int $pagenbr Page number
  169. * @param Facture $object Invoice object
  170. * @param Translate $langs Translation object
  171. * @return int Height in mm of the bottom-page QR invoice. Can be zero if not on right page; not enabled
  172. */
  173. protected function getHeightForQRInvoice(int $pagenbr, Facture $object, Translate $langs)
  174. {
  175. if (getDolGlobalString('INVOICE_ADD_SWISS_QR_CODE') == 'bottom') {
  176. // Keep it, to reset it after QRinvoice getter
  177. $error = $this->error;
  178. if (!$this->getSwissQrBill($object, $langs)) {
  179. // Reset error to previous one if exists
  180. if (!empty($error)) {
  181. $this->error = $error;
  182. }
  183. return 0;
  184. }
  185. // SWIFT's requirementis 105, but we get more room with 100 and the page number is in a nice place.
  186. return $pagenbr == 1 ? 100 : 0;
  187. }
  188. return 0;
  189. }
  190. /**
  191. * Add SwissQR invoice at bottom of page 1
  192. *
  193. * @param TCPDF $pdf TCPDF object
  194. * @param Facture $object Invoice object
  195. * @param Translate $langs Translation object
  196. * @return bool True for for success
  197. */
  198. public function addBottomQRInvoice(TCPDF $pdf, Facture $object, Translate $langs): bool
  199. {
  200. if (!($qrBill = $this->getSwissQrBill($object, $langs))) {
  201. return false;
  202. }
  203. try {
  204. $pdf->startTransaction();
  205. $pdf->setPage(1);
  206. $pdf->SetTextColor(0, 0, 0);
  207. $output = new SwissQrBill\PaymentPart\Output\TcPdfOutput\TcPdfOutput($qrBill, in_array($langs->shortlang, ['de', 'fr', 'it']) ? $langs->shortlang : 'en', $pdf);
  208. $output->setPrintable(false)->getPaymentPart();
  209. } catch (Exception $e) {
  210. $pdf->rollbackTransaction(true);
  211. return false;
  212. }
  213. return true;
  214. }
  215. }
  216. /**
  217. * Parent class of invoice reference numbering templates
  218. */
  219. abstract class ModeleNumRefFactures extends CommonNumRefGenerator
  220. {
  221. // No overload code
  222. }