Debian Codegraph @

addons/l10n_si/models/account_move.py

raw symbols SCIP
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2
3 import re
4
5 from odoo import models
6
7
8 class AccountMove(models.Model):
9 _inherit = 'account.move'
10
11 def _get_invoice_reference_si_partner(self):
12 """
13 Generate the Slovenian structured payment reference using the partner's ID.
14 Format: SI01 (P1-P2-P3)K
15 - P1: Last two digits of the invoice year
16 - P2: Partner ID
17 - P3: Journal ID
18 - K: Check digit
19
20 :return: the formatted structured reference string (SI01...)
21 """
22 self.ensure_one()
23 p3 = str(self.partner_id.id)
24 return self._build_invoice_reference(p3)
25
26 def _get_invoice_reference_si_invoice(self):
27 """
28 Generate the Slovenian structured payment reference using the invoice sequence number.
29
30 Format: SI01 (P1-P2-P3)K
31 - P1: Last two digits of the invoice year
32 - P2: Trailing digits of the invoice name (sequence number)
33 - P3: Journal ID
34 - K: Check digit
35
36 :return: the formatted structured reference string (SI01...)
37 """
38 self.ensure_one()
39 match = re.search(r'(\d+)$', self.name or '')
40 p3 = str(int(match.group(1))) if match else '0'
41 return self._build_invoice_reference(p3)
42
43 def _build_invoice_reference(self, p3):
44 """Builds the reference using a shared structure for both methods."""
45 p1 = str(self.journal_id.id)
46 p2 = str(self.invoice_date.year)[-2:]
47 reference_base = f"{p1}-{p2}-{p3}"
48
49 # Calculate check digit
50 digits = [int(d) for d in reference_base if d.isdigit()]
51 weights = list(range(2, 14))[:len(digits)]
52 weighted_sum = sum(d * w for d, w in zip(reversed(digits), weights))
53 check_digit = 11 - (weighted_sum % 11)
54 check_digit = 0 if check_digit in (10, 11) else check_digit
55 return f"SI01 {reference_base}{check_digit}"