modules_facture.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. use \Sprain\SwissQrBill;
  32. /**
  33. * Parent class of invoice document generators
  34. */
  35. abstract class ModelePDFFactures extends CommonDocGenerator
  36. {
  37. /**
  38. * @var string Error code (or message)
  39. */
  40. public $error = '';
  41. public $posxpicture;
  42. public $posxtva;
  43. public $posxup;
  44. public $posxqty;
  45. public $posxunit;
  46. public $posxdesc;
  47. public $posxdiscount;
  48. public $postotalht;
  49. public $tva;
  50. public $tva_array;
  51. public $localtax1;
  52. public $localtax2;
  53. public $atleastonediscount = 0;
  54. public $atleastoneratenotnull = 0;
  55. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  56. /**
  57. * Return list of active generation modules
  58. *
  59. * @param DoliDB $db Database handler
  60. * @param integer $maxfilenamelength Max length of value to show
  61. * @return array List of templates
  62. */
  63. public static function liste_modeles($db, $maxfilenamelength = 0)
  64. {
  65. // phpcs:enable
  66. $type = 'invoice';
  67. $list = array();
  68. include_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  69. $list = getListOfModels($db, $type, $maxfilenamelength);
  70. return $list;
  71. }
  72. /**
  73. * Get the SwissQR object, including validation
  74. *
  75. * @param Facture $object Invoice object
  76. * @param Translate $langs Translation object
  77. * @return SwissQrBill|bool The valid SwissQR object, or false
  78. */
  79. private function getSwissQrBill(\Facture $object, \Translate $langs) : SwissQrBill\QrBill|bool
  80. {
  81. global $conf;
  82. if ($object->mode_reglement_code != 'VIR') {
  83. $this->error = $langs->transnoentities("SwissQrOnlyVIR");
  84. return false;
  85. }
  86. if (empty($conf->global->INVOICE_ADD_SWISS_QR_CODE)) {
  87. return false;
  88. }
  89. require_once DOL_DOCUMENT_ROOT.'/includes/sprain/swiss-qr-bill/autoload.php';
  90. // Create a new instance of SwissQrBill, containing default headers with fixed values
  91. $qrBill = SwissQrBill\QrBill::create();
  92. // First, check creditor address
  93. $address = SwissQrBill\DataGroup\Element\CombinedAddress::create(
  94. $this->emetteur->name,
  95. $this->emetteur->address,
  96. $this->emetteur->zip . " " . $this->emetteur->town,
  97. $this->emetteur->country_code
  98. );
  99. if (!$address->isValid()) {
  100. $this->error = $langs->transnoentities("SwissQrCreditorAddressInvalid", (string) $address->getViolations());
  101. return false;
  102. }
  103. $qrBill->setCreditor($address);
  104. // Get IBAN from account.
  105. $account = new Account($this->db);
  106. $account->fetch($object->fk_account);
  107. $creditorInformation = SwissQrBill\DataGroup\Element\CreditorInformation::create($account->iban);
  108. if (!$creditorInformation->isValid()) {
  109. $this->error = $langs->transnoentities("SwissQrCreditorInformationInvalid", $account->iban, (string) $creditorInformation->getViolations());
  110. return false;
  111. }
  112. $qrBill->setCreditorInformation($creditorInformation);
  113. if ($creditorInformation->containsQrIban()) {
  114. $this->error = $langs->transnoentities("SwissQrIbanNotImplementedYet", $account->iban);
  115. return false;
  116. }
  117. // Add payment reference CLASSIC-IBAN
  118. // This is what you will need to identify incoming payments.
  119. $qrBill->setPaymentReference(
  120. SwissQrBill\DataGroup\Element\PaymentReference::create(
  121. SwissQrBill\DataGroup\Element\PaymentReference::TYPE_NON,
  122. )
  123. );
  124. // Add payment amount, with currency
  125. $pai = SwissQrBill\DataGroup\Element\PaymentAmountInformation::create($object->multicurrency_code, $object->total_ttc);
  126. if (!$pai->isValid()) {
  127. $this->error = $langs->transnoentities("SwissQrPaymentInformationInvalid", $object->total_ttc, (string) $pai->getViolations());
  128. return false;
  129. }
  130. $qrBill->setPaymentAmountInformation($pai);
  131. // Add some human-readable information about what the bill is for.
  132. $qrBill->setAdditionalInformation(
  133. SwissQrBill\DataGroup\Element\AdditionalInformation::create(
  134. $object->ref
  135. )
  136. );
  137. // Check debtor address; We _know_ zip&town have to be filled, so skip that if unfilled.
  138. if (!empty($object->thirdparty->zip) && !empty($object->thirdparty->town)) {
  139. $address = SwissQrBill\DataGroup\Element\CombinedAddress::create(
  140. $object->thirdparty->name,
  141. $object->thirdparty->address,
  142. $object->thirdparty->zip . " " . $object->thirdparty->town,
  143. $object->thirdparty->country_code
  144. );
  145. if (!$address->isValid()) {
  146. $this->error = $langs->transnoentities("SwissQrDebitorAddressInvalid", (string) $address->getViolations());
  147. return false;
  148. }
  149. $qrBill->setUltimateDebtor($address);
  150. }
  151. return $qrBill;
  152. }
  153. /**
  154. * Get the height for bottom-page QR invoice in mm, depending on the page number.
  155. *
  156. * @param int $pagenbr Page number
  157. * @param Facture $object Invoice object
  158. * @param Translate $langs Translation object
  159. * @return int Height in mm of the bottom-page QR invoice. Can be zero if not on right page; not enabled
  160. */
  161. protected function getHeightForQRInvoice(int $pagenbr, \Facture $object, \Translate $langs) : int
  162. {
  163. global $conf;
  164. // Keep it, reset it after QRinvoice getter
  165. $error = $this->error;
  166. if (!empty($conf->global->INVOICE_ADD_SWISS_QR_CODE)) {
  167. if (!$this->getSwissQrBill($object, $langs)) {
  168. // Reset error to previous one if exists
  169. $this->error = $error;
  170. return 0;
  171. }
  172. // SWIFT's requirementis 105, but we get more room with 100 and the page nuber is in a nice place.
  173. return $pagenbr == 1 ? 100 : 0;
  174. }
  175. return 0;
  176. }
  177. /**
  178. * Add SwissQR invoice at bottom of page 1
  179. *
  180. * @param TCPDF $pdf TCPDF object
  181. * @param Facture $object Invoice object
  182. * @param Translate $langs Translation object
  183. * @return bool for success
  184. */
  185. public function addSwissQRInvoice(\TCPDF $pdf, \Facture $object, \Translate $langs) : bool
  186. {
  187. if (!($qrBill = $this->getSwissQrBill($object, $langs))) {
  188. return false;
  189. }
  190. try {
  191. $pdf->startTransaction();
  192. $pdf->setPage(1);
  193. $pdf->SetTextColor(0, 0, 0);
  194. $output = new SwissQrBill\PaymentPart\Output\TcPdfOutput\TcPdfOutput($qrBill, in_array($langs->shortlang, ['de', 'fr', 'it']) ? $langs->shortlang : 'en', $pdf);
  195. $output->setPrintable(false)->getPaymentPart();
  196. } catch (Exception $e) {
  197. $pdf->rollbackTransaction(true);
  198. return false;
  199. }
  200. return true;
  201. }
  202. }
  203. /**
  204. * Parent class of invoice reference numbering templates
  205. */
  206. abstract class ModeleNumRefFactures
  207. {
  208. /**
  209. * @var string Error code (or message)
  210. */
  211. public $error = '';
  212. /**
  213. * Return if a module can be used or not
  214. *
  215. * @return boolean true if module can be used
  216. */
  217. public function isEnabled()
  218. {
  219. return true;
  220. }
  221. /**
  222. * Returns the default description of the numbering pattern
  223. *
  224. * @return string Descriptive text
  225. */
  226. public function info()
  227. {
  228. global $langs;
  229. $langs->load("bills");
  230. return $langs->trans("NoDescription");
  231. }
  232. /**
  233. * Return an example of numbering
  234. *
  235. * @return string Example
  236. */
  237. public function getExample()
  238. {
  239. global $langs;
  240. $langs->load("bills");
  241. return $langs->trans("NoExample");
  242. }
  243. /**
  244. * Checks if the numbers already in the database do not
  245. * cause conflicts that would prevent this numbering working.
  246. *
  247. * @return boolean false if conflict, true if ok
  248. */
  249. public function canBeActivated()
  250. {
  251. return true;
  252. }
  253. /**
  254. * Renvoi prochaine valeur attribuee
  255. *
  256. * @param Societe $objsoc Objet societe
  257. * @param Facture $invoice Objet facture
  258. * @param string $mode 'next' for next value or 'last' for last value
  259. * @return string Value
  260. */
  261. public function getNextValue($objsoc, $invoice, $mode = 'next')
  262. {
  263. global $langs;
  264. return $langs->trans("NotAvailable");
  265. }
  266. /**
  267. * Renvoi version du modele de numerotation
  268. *
  269. * @return string Valeur
  270. */
  271. public function getVersion()
  272. {
  273. global $langs;
  274. $langs->load("admin");
  275. if ($this->version == 'development') {
  276. return $langs->trans("VersionDevelopment");
  277. } elseif ($this->version == 'experimental') {
  278. return $langs->trans("VersionExperimental");
  279. } elseif ($this->version == 'dolibarr') {
  280. return DOL_VERSION;
  281. } elseif ($this->version) {
  282. return $this->version;
  283. } else {
  284. return $langs->trans("NotAvailable");
  285. }
  286. }
  287. }