modules_facture.php 9.5 KB

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