dolibarr 20.0.0
modules_facture.php
Go to the documentation of this file.
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
30require_once DOL_DOCUMENT_ROOT.'/core/class/commondocgenerator.class.php';
31require_once DOL_DOCUMENT_ROOT.'/core/class/commonnumrefgenerator.class.php';
32require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
33require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php'; // Required because used in classes that inherit
34
35// For the experimental feature using swiss QR invoice generated by composer lib sparin/swiss-qr-bill
36use Sprain\SwissQrBill;
37
42{
43 public $posxpicture;
44 public $posxtva;
45 public $posxup;
46 public $posxqty;
47 public $posxunit;
48 public $posxdesc;
49 public $posxdiscount;
50 public $postotalht;
51
52 public $tva;
53 public $tva_array;
54 public $localtax1;
55 public $localtax2;
56
57 public $atleastonediscount = 0;
58 public $atleastoneratenotnull = 0;
59
60
61 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
69 public static function liste_modeles($db, $maxfilenamelength = 0)
70 {
71 // phpcs:enable
72 $type = 'invoice';
73 $list = array();
74
75 include_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
76 $list = getListOfModels($db, $type, $maxfilenamelength);
77
78 return $list;
79 }
80
88 private function getSwissQrBill(Facture $object, Translate $langs)
89 {
90 global $conf;
91
92 if (getDolGlobalString('INVOICE_ADD_SWISS_QR_CODE') != 'bottom') {
93 return false;
94 }
95
96 if ($object->mode_reglement_code != 'VIR') {
97 $this->error = $langs->transnoentities("SwissQrOnlyVIR");
98 return false;
99 }
100
101 if (empty($object->fk_account)) {
102 $this->error = 'Bank account must be defined to use this experimental feature';
103 return false;
104 }
105
106 // Load the autoload file generated by composer
107 if (file_exists(DOL_DOCUMENT_ROOT.'/includes/sprain/swiss-qr-bill/autoload.php')) {
108 require_once DOL_DOCUMENT_ROOT.'/includes/sprain/swiss-qr-bill/autoload.php';
109 } elseif (file_exists(DOL_DOCUMENT_ROOT.'/includes/autoload.php')) {
110 require_once DOL_DOCUMENT_ROOT.'/includes/autoload.php';
111 } else {
112 $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;';
113 return false;
114 }
115
116 // Create a new instance of SwissQrBill, containing default headers with fixed values
117 $qrBill = SwissQrBill\QrBill::create();
118
119 // First, set creditor address
120 $address = SwissQrBill\DataGroup\Element\CombinedAddress::create(
121 $this->emetteur->name,
122 $this->emetteur->address,
123 $this->emetteur->zip . " " . $this->emetteur->town,
124 $this->emetteur->country_code
125 );
126 if (!$address->isValid()) {
127 $this->error = $langs->transnoentities("SwissQrCreditorAddressInvalid", (string) $address->getViolations());
128 return false;
129 }
130 $qrBill->setCreditor($address);
131
132 // Get IBAN from account.
133 $account = new Account($this->db);
134 $account->fetch($object->fk_account);
135 $creditorInformation = SwissQrBill\DataGroup\Element\CreditorInformation::create($account->iban);
136 if (!$creditorInformation->isValid()) {
137 $langs->load("errors");
138 $this->error = $langs->transnoentities("SwissQrCreditorInformationInvalid", $account->iban, (string) $creditorInformation->getViolations());
139 return false;
140 }
141 $qrBill->setCreditorInformation($creditorInformation);
142
143 if ($creditorInformation->containsQrIban()) {
144 $this->error = $langs->transnoentities("SwissQrIbanNotImplementedYet", $account->iban);
145 return false;
146 }
147
148 // Add payment reference CLASSIC-IBAN
149 // This is what you will need to identify incoming payments.
150 $qrBill->setPaymentReference(
151 SwissQrBill\DataGroup\Element\PaymentReference::create(
152 SwissQrBill\DataGroup\Element\PaymentReference::TYPE_NON
153 )
154 );
155
156 $currencyinvoicecode = $object->multicurrency_code ? $object->multicurrency_code : $conf->currency;
157
158 // Add payment amount, with currency
159 $pai = SwissQrBill\DataGroup\Element\PaymentAmountInformation::create($currencyinvoicecode, $object->total_ttc);
160 if (!$pai->isValid()) {
161 $this->error = $langs->transnoentities("SwissQrPaymentInformationInvalid", $object->total_ttc, (string) $pai->getViolations());
162 return false;
163 }
164 $qrBill->setPaymentAmountInformation($pai);
165
166 // Add some human-readable information about what the bill is for.
167 $qrBill->setAdditionalInformation(
168 SwissQrBill\DataGroup\Element\AdditionalInformation::create(
169 $object->ref
170 )
171 );
172
173 // Set debtor address; We _know_ zip&town have to be filled, so skip that if unfilled.
174 if (!empty($object->thirdparty->zip) && !empty($object->thirdparty->town)) {
175 $address = SwissQrBill\DataGroup\Element\CombinedAddress::create(
176 $object->thirdparty->name,
177 $object->thirdparty->address,
178 $object->thirdparty->zip . " " . $object->thirdparty->town,
179 $object->thirdparty->country_code
180 );
181 if (!$address->isValid()) {
182 $this->error = $langs->transnoentities("SwissQrDebitorAddressInvalid", (string) $address->getViolations());
183 return false;
184 }
185 $qrBill->setUltimateDebtor($address);
186 }
187
188 return $qrBill;
189 }
190
199 protected function getHeightForQRInvoice(int $pagenbr, Facture $object, Translate $langs)
200 {
201 if (getDolGlobalString('INVOICE_ADD_SWISS_QR_CODE') == 'bottom') {
202 // Keep it, to reset it after QRinvoice getter
203 $error = $this->error;
204
205 if (!$this->getSwissQrBill($object, $langs)) {
206 // Reset error to previous one if exists
207 if (!empty($error)) {
208 $this->error = $error;
209 }
210 return 0;
211 }
212 // SWIFT's requirementis 105, but we get more room with 100 and the page number is in a nice place.
213 return $pagenbr == 1 ? 100 : 0;
214 }
215
216 return 0;
217 }
218
227 public function addBottomQRInvoice(TCPDF $pdf, Facture $object, Translate $langs): bool
228 {
229 if (!($qrBill = $this->getSwissQrBill($object, $langs))) {
230 return false;
231 }
232
233 try {
234 $pdf->startTransaction();
235
236 $pdf->setPage(1);
237 $pdf->SetTextColor(0, 0, 0);
238 $output = new SwissQrBill\PaymentPart\Output\TcPdfOutput\TcPdfOutput($qrBill, in_array($langs->shortlang, ['de', 'fr', 'it']) ? $langs->shortlang : 'en', $pdf);
239 $output->setPrintable(false)->getPaymentPart();
240 } catch (Exception $e) {
241 $pdf->rollbackTransaction(true);
242 return false;
243 }
244 return true;
245 }
246}
247
252{
253 // No overload code
254}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
Class to manage bank accounts.
Parent class for documents (PDF, ODT, ...) generators.
Parent class for number ref generators.
Class to manage invoices.
Parent class of invoice reference numbering templates.
Parent class of invoice document generators.
getSwissQrBill(Facture $object, Translate $langs)
Get the SwissQR object, including validation.
addBottomQRInvoice(TCPDF $pdf, Facture $object, Translate $langs)
Add SwissQR invoice at bottom of page 1.
getHeightForQRInvoice(int $pagenbr, Facture $object, Translate $langs)
Get the height for bottom-page QR invoice in mm, depending on the page number.
static liste_modeles($db, $maxfilenamelength=0)
Return list of active generation modules.
Class to manage translations.
getListOfModels($db, $type, $maxfilenamelength=0)
Return list of activated modules usable for document generation.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.