| 1 |
"""Overrides to Odoo's sequence.mixin for Croatian fiscal invoice format. |
| 2 |
|
| 3 |
This logic shapes the generated `name` for Croatian sales invoices and credit |
| 4 |
notes into the legally required pattern that embeds two journal-level fields: |
| 5 |
|
| 6 |
{JOURNAL_CODE}-{YEAR}-{SEQ}/{OZNAKA_POSLOVNOG_PROSTORA}/{OZNAKA_NAPLATNOG_UREĐAJA} |
| 7 |
|
| 8 |
- For credit notes using a separate refund sequence, alternate identifiers |
| 9 |
(odobrenje) can be configured on the journal. |
| 10 |
- Sequences reset yearly and use a fixed zero-padded counter. |
| 11 |
|
| 12 |
Legal reference: https://porezna-uprava.gov.hr/Regulations#169%7C228, https://porezna-uprava.gov.hr/Propisi#3149|3150 |
| 13 |
""" |
| 14 |
|
| 15 |
import pytz |
| 16 |
|
| 17 |
from odoo import models, api |
| 18 |
from odoo.exceptions import ValidationError |
| 19 |
import re |
| 20 |
import logging |
| 21 |
from datetime import datetime |
| 22 |
|
| 23 |
_logger = logging.getLogger(__name__) |
| 24 |
HR_FORMAT_STR = '{code}-{year:0{year_length}d}-{seq:0{seq_length}d}/{premises_label}/{device_label}' |
| 25 |
HR_REGEX_STR = r'^(?P<code>.*?)-(?P<year>\d{4})-(?P<seq>\d+)/(?P<premises_label>[^/]+)/(?P<device_label>[^/]+)$' |
| 26 |
|
| 27 |
|
| 28 |
class SequenceMixin(models.AbstractModel): |
| 29 |
_inherit = 'sequence.mixin' |
| 30 |
|
| 31 |
@api.model |
| 32 |
def _get_sequence_format_param(self, previous): |
| 33 |
""" |
| 34 |
Build the format string and values for Croatian invoice numbering. |
| 35 |
Extends the base implementation to inject journal fields into the |
| 36 |
name. Handles both the first number of a period and subsequent ones. |
| 37 |
""" |
| 38 |
if self.env.company.country_code != 'HR' or not self.is_sale_document(): |
| 39 |
return super()._get_sequence_format_param(previous) |
| 40 |
|
| 41 |
if not self.journal_id: |
| 42 |
_logger.error("Journal is not set for this invoice.") |
| 43 |
raise ValidationError(self.env._("Journal is not set for this invoice.")) |
| 44 |
if self.move_type == 'out_refund' and self.journal_id.refund_sequence: |
| 45 |
premises_label = self.journal_id.l10n_hr_business_premises_label_refund |
| 46 |
device_label = self.journal_id.l10n_hr_issuing_device_label_refund |
| 47 |
else: |
| 48 |
premises_label = self.journal_id.l10n_hr_business_premises_label |
| 49 |
device_label = self.journal_id.l10n_hr_issuing_device_label |
| 50 |
if not premises_label: |
| 51 |
_logger.error("Business premises label is not set on the journal.") |
| 52 |
raise ValidationError(self.env._("Business premises label is not set on the journal.")) |
| 53 |
if not device_label: |
| 54 |
_logger.error("Issuing device label is not set on the journal.") |
| 55 |
raise ValidationError(self.env._("Issuing device label is not set on the journal.")) |
| 56 |
|
| 57 |
current_year = datetime.now(pytz.timezone('Europe/Zagreb')).year |
| 58 |
format_values = { |
| 59 |
'code': self.journal_id.code, |
| 60 |
'year': current_year, |
| 61 |
'seq': 1, |
| 62 |
'premises_label': premises_label, |
| 63 |
'device_label': device_label, |
| 64 |
'seq_length': 4, # Adjust as needed, e.g., 4 for zero-padding |
| 65 |
'year_length': 4, |
| 66 |
'year_end_length': 0, # Not used in this format |
| 67 |
'month': 0, # Setting to 0 indicates that month is not used |
| 68 |
} |
| 69 |
|
| 70 |
# Existing logic for handling previous sequences |
| 71 |
match = re.match(HR_REGEX_STR, previous) |
| 72 |
if not match: |
| 73 |
if previous != self._get_starting_sequence(): # First invoice in the sequence = no need to raise warnings |
| 74 |
_logger.warning("The previous sequence '%s' does not match the expected format. Resetting to initial sequence.", previous) |
| 75 |
_logger.debug("Reset sequence format: %s, Format values: %s", HR_FORMAT_STR, format_values) |
| 76 |
return HR_FORMAT_STR, format_values |
| 77 |
|
| 78 |
format_values = match.groupdict() |
| 79 |
# Convert 'seq' and 'year' to integers |
| 80 |
try: |
| 81 |
format_values['seq'] = int(format_values.get('seq', '0')) |
| 82 |
except ValueError: |
| 83 |
_logger.error("Invalid sequence number in '%s': seq='%s'", previous, format_values.get('seq')) |
| 84 |
raise ValidationError(self.env._("Invalid sequence number format.")) |
| 85 |
try: |
| 86 |
format_values['year'] = int(format_values.get('year', '0')) |
| 87 |
except ValueError: |
| 88 |
_logger.error("Invalid year in '%s': year='%s'", previous, format_values.get('year')) |
| 89 |
raise ValidationError(self.env._("Invalid year format.")) |
| 90 |
|
| 91 |
format_values['code'] = self.journal_id.code |
| 92 |
format_values['premises_label'] = premises_label |
| 93 |
format_values['device_label'] = device_label |
| 94 |
# Handle 'year_end' if it exists, else set to 0 |
| 95 |
format_values['year_end'] = int(format_values.get('year_end', '0')) if 'year_end' in format_values else 0 |
| 96 |
# Set lengths for dynamic formatting |
| 97 |
format_values['seq_length'] = 4 # Fixed to 4 |
| 98 |
format_values['year_length'] = len(str(format_values['year'])) |
| 99 |
format_values['year_end_length'] = len(str(format_values['year_end'])) # Ensure 'year_end_length' is always set |
| 100 |
# Add 'month' key with a default value |
| 101 |
format_values['month'] = 0 # Setting to 0 indicates that month is not used |
| 102 |
|
| 103 |
_logger.debug("Sequence format: %s, Format values: %s", HR_FORMAT_STR, format_values) |
| 104 |
return HR_FORMAT_STR, format_values |
| 105 |
|
| 106 |
@api.model |
| 107 |
def _deduce_sequence_number_reset(self, name): |
| 108 |
""" |
| 109 |
Detect if the used sequence resets yearly, monthly or never. |
| 110 |
:param name: the sequence that is used as a reference to detect the resetting periodicity. |
| 111 |
""" |
| 112 |
if self.env.company.country_code != 'HR' or not self.is_sale_document(): |
| 113 |
return super()._deduce_sequence_number_reset(name) |
| 114 |
|
| 115 |
match = re.match(HR_REGEX_STR, name or '') |
| 116 |
if match: |
| 117 |
return 'year' |
| 118 |
return super()._deduce_sequence_number_reset(name) |