| Conditions | 14 |
| Paths | 26 |
| Total Lines | 101 |
| Code Lines | 60 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 150 | private function getSwissQrBill(Facture $object, Translate $langs) |
||
| 151 | { |
||
| 152 | global $conf; |
||
| 153 | |||
| 154 | if (getDolGlobalString('INVOICE_ADD_SWISS_QR_CODE') != 'bottom') { |
||
| 155 | return false; |
||
| 156 | } |
||
| 157 | |||
| 158 | if ($object->mode_reglement_code != 'VIR') { |
||
| 159 | $this->error = $langs->transnoentities("SwissQrOnlyVIR"); |
||
| 160 | return false; |
||
| 161 | } |
||
| 162 | |||
| 163 | if (empty($object->fk_account)) { |
||
| 164 | $this->error = 'Bank account must be defined to use this experimental feature'; |
||
| 165 | return false; |
||
| 166 | } |
||
| 167 | |||
| 168 | // Load the autoload file generated by composer |
||
| 169 | if (file_exists(DOL_DOCUMENT_ROOT . '/includes/sprain/swiss-qr-bill/autoload.php')) { |
||
| 170 | require_once constant('DOL_DOCUMENT_ROOT') . '/includes/sprain/swiss-qr-bill/autoload.php'; |
||
| 171 | } elseif (file_exists(DOL_DOCUMENT_ROOT . '/includes/autoload.php')) { |
||
| 172 | require_once constant('DOL_DOCUMENT_ROOT') . '/includes/autoload.php'; |
||
| 173 | } else { |
||
| 174 | $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;'; |
||
| 175 | return false; |
||
| 176 | } |
||
| 177 | |||
| 178 | // Create a new instance of SwissQrBill, containing default headers with fixed values |
||
| 179 | $qrBill = SwissQrBill\QrBill::create(); |
||
| 180 | |||
| 181 | // First, set creditor address |
||
| 182 | $address = SwissQrBill\DataGroup\Element\CombinedAddress::create( |
||
| 183 | $this->emetteur->name, |
||
| 184 | $this->emetteur->address, |
||
| 185 | $this->emetteur->zip . " " . $this->emetteur->town, |
||
| 186 | $this->emetteur->country_code |
||
| 187 | ); |
||
| 188 | if (!$address->isValid()) { |
||
| 189 | $this->error = $langs->transnoentities("SwissQrCreditorAddressInvalid", (string)$address->getViolations()); |
||
| 190 | return false; |
||
| 191 | } |
||
| 192 | $qrBill->setCreditor($address); |
||
| 193 | |||
| 194 | // Get IBAN from account. |
||
| 195 | $account = new Account($this->db); |
||
| 196 | $account->fetch($object->fk_account); |
||
| 197 | $creditorInformation = SwissQrBill\DataGroup\Element\CreditorInformation::create($account->iban); |
||
| 198 | if (!$creditorInformation->isValid()) { |
||
| 199 | $langs->load("errors"); |
||
| 200 | $this->error = $langs->transnoentities("SwissQrCreditorInformationInvalid", $account->iban, (string)$creditorInformation->getViolations()); |
||
| 201 | return false; |
||
| 202 | } |
||
| 203 | $qrBill->setCreditorInformation($creditorInformation); |
||
| 204 | |||
| 205 | if ($creditorInformation->containsQrIban()) { |
||
| 206 | $this->error = $langs->transnoentities("SwissQrIbanNotImplementedYet", $account->iban); |
||
| 207 | return false; |
||
| 208 | } |
||
| 209 | |||
| 210 | // Add payment reference CLASSIC-IBAN |
||
| 211 | // This is what you will need to identify incoming payments. |
||
| 212 | $qrBill->setPaymentReference( |
||
| 213 | SwissQrBill\DataGroup\Element\PaymentReference::create( |
||
| 214 | SwissQrBill\DataGroup\Element\PaymentReference::TYPE_NON |
||
| 215 | ) |
||
| 216 | ); |
||
| 217 | |||
| 218 | $currencyinvoicecode = $object->multicurrency_code ? $object->multicurrency_code : $conf->currency; |
||
| 219 | |||
| 220 | // Add payment amount, with currency |
||
| 221 | $pai = SwissQrBill\DataGroup\Element\PaymentAmountInformation::create($currencyinvoicecode, $object->total_ttc); |
||
| 222 | if (!$pai->isValid()) { |
||
| 223 | $this->error = $langs->transnoentities("SwissQrPaymentInformationInvalid", $object->total_ttc, (string)$pai->getViolations()); |
||
| 224 | return false; |
||
| 225 | } |
||
| 226 | $qrBill->setPaymentAmountInformation($pai); |
||
| 227 | |||
| 228 | // Add some human-readable information about what the bill is for. |
||
| 229 | $qrBill->setAdditionalInformation( |
||
| 230 | SwissQrBill\DataGroup\Element\AdditionalInformation::create( |
||
| 231 | $object->ref |
||
| 232 | ) |
||
| 233 | ); |
||
| 234 | |||
| 235 | // Set debtor address; We _know_ zip&town have to be filled, so skip that if unfilled. |
||
| 236 | if (!empty($object->thirdparty->zip) && !empty($object->thirdparty->town)) { |
||
| 237 | $address = SwissQrBill\DataGroup\Element\CombinedAddress::create( |
||
| 238 | $object->thirdparty->name, |
||
| 239 | $object->thirdparty->address, |
||
| 240 | $object->thirdparty->zip . " " . $object->thirdparty->town, |
||
| 241 | $object->thirdparty->country_code |
||
| 242 | ); |
||
| 243 | if (!$address->isValid()) { |
||
| 244 | $this->error = $langs->transnoentities("SwissQrDebitorAddressInvalid", (string)$address->getViolations()); |
||
| 245 | return false; |
||
| 246 | } |
||
| 247 | $qrBill->setUltimateDebtor($address); |
||
| 248 | } |
||
| 249 | |||
| 250 | return $qrBill; |
||
| 251 | } |
||
| 253 |