modules_facture.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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.'/product/class/product.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php'; // Required because used in classes that inherit
  31. // For the experimental feature using swiss QR invoice generated by composer lib sparin/swiss-qr-bill
  32. use Sprain\SwissQrBill;
  33. /**
  34. * Parent class of invoice document generators
  35. */
  36. abstract class ModelePDFFactures extends CommonDocGenerator
  37. {
  38. /**
  39. * @var string Error code (or message)
  40. */
  41. public $error = '';
  42. public $posxpicture;
  43. public $posxtva;
  44. public $posxup;
  45. public $posxqty;
  46. public $posxunit;
  47. public $posxdesc;
  48. public $posxdiscount;
  49. public $postotalht;
  50. public $tva;
  51. public $tva_array;
  52. public $localtax1;
  53. public $localtax2;
  54. public $atleastonediscount = 0;
  55. public $atleastoneratenotnull = 0;
  56. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  57. /**
  58. * Return list of active generation modules
  59. *
  60. * @param DoliDB $db Database handler
  61. * @param integer $maxfilenamelength Max length of value to show
  62. * @return array List of templates
  63. */
  64. public static function liste_modeles($db, $maxfilenamelength = 0)
  65. {
  66. // phpcs:enable
  67. $type = 'invoice';
  68. $list = array();
  69. include_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  70. $list = getListOfModels($db, $type, $maxfilenamelength);
  71. return $list;
  72. }
  73. /**
  74. * Get the SwissQR object, including validation
  75. *
  76. * @param Facture $object Invoice object
  77. * @param Translate $langs Translation object
  78. * @return SwissQrBill\QrBill|bool The valid SwissQR object, or false
  79. */
  80. private function getSwissQrBill(Facture $object, Translate $langs)
  81. {
  82. global $conf;
  83. if (getDolGlobalString('INVOICE_ADD_SWISS_QR_CODE') != 'bottom') {
  84. return false;
  85. }
  86. if ($object->mode_reglement_code != 'VIR') {
  87. $this->error = $langs->transnoentities("SwissQrOnlyVIR");
  88. return false;
  89. }
  90. if (empty($object->fk_account)) {
  91. $this->error = 'Bank account must be defined to use this experimental feature';
  92. return false;
  93. }
  94. // Load the autoload file generated by composer
  95. if (file_exists(DOL_DOCUMENT_ROOT.'/includes/sprain/swiss-qr-bill/autoload.php')) {
  96. require_once DOL_DOCUMENT_ROOT.'/includes/sprain/swiss-qr-bill/autoload.php';
  97. } elseif (file_exists(DOL_DOCUMENT_ROOT.'/includes/autoload.php')) {
  98. require_once DOL_DOCUMENT_ROOT.'/includes/autoload.php';
  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) : int
  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
  220. {
  221. /**
  222. * @var string Error code (or message)
  223. */
  224. public $error = '';
  225. /**
  226. * Return if a module can be used or not
  227. *
  228. * @return boolean true if module can be used
  229. */
  230. public function isEnabled()
  231. {
  232. return true;
  233. }
  234. /**
  235. * Returns the default description of the numbering pattern
  236. *
  237. * @return string Descriptive text
  238. */
  239. public function info()
  240. {
  241. global $langs;
  242. $langs->load("bills");
  243. return $langs->trans("NoDescription");
  244. }
  245. /**
  246. * Return an example of numbering
  247. *
  248. * @return string Example
  249. */
  250. public function getExample()
  251. {
  252. global $langs;
  253. $langs->load("bills");
  254. return $langs->trans("NoExample");
  255. }
  256. /**
  257. * Checks if the numbers already in the database do not
  258. * cause conflicts that would prevent this numbering working.
  259. *
  260. * @return boolean false if conflict, true if ok
  261. */
  262. public function canBeActivated()
  263. {
  264. return true;
  265. }
  266. /**
  267. * Renvoi prochaine valeur attribuee
  268. *
  269. * @param Societe $objsoc Objet societe
  270. * @param Facture $invoice Objet facture
  271. * @param string $mode 'next' for next value or 'last' for last value
  272. * @return string Value
  273. */
  274. public function getNextValue($objsoc, $invoice, $mode = 'next')
  275. {
  276. global $langs;
  277. return $langs->trans("NotAvailable");
  278. }
  279. /**
  280. * Renvoi version du modele de numerotation
  281. *
  282. * @return string Valeur
  283. */
  284. public function getVersion()
  285. {
  286. global $langs;
  287. $langs->load("admin");
  288. if ($this->version == 'development') {
  289. return $langs->trans("VersionDevelopment");
  290. } elseif ($this->version == 'experimental') {
  291. return $langs->trans("VersionExperimental");
  292. } elseif ($this->version == 'dolibarr') {
  293. return DOL_VERSION;
  294. } elseif ($this->version) {
  295. return $this->version;
  296. } else {
  297. return $langs->trans("NotAvailable");
  298. }
  299. }
  300. }