Skip to content

Commit

Permalink
[ADD] Do not raise error if protected fields are unchanged.
Browse files Browse the repository at this point in the history
  • Loading branch information
gfcapalbo committed Nov 13, 2023
1 parent b084355 commit 4460929
Showing 1 changed file with 49 additions and 3 deletions.
52 changes: 49 additions & 3 deletions l10n_nl_tax_statement/models/account_move_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,56 @@ def _get_l10n_nl_vat_statement_protected_fields(self):
"tax_tag_ids",
]

def check_field_is_equal(self, changed_protected_field, values):
self.ensure_one()
if self._fields.get(changed_protected_field) and self._fields[
changed_protected_field
].type not in ["many2many", "one2many"]:
return self[changed_protected_field] == values[changed_protected_field]
# if field is X2M we must translate commands to values.
# the only acceptable value is [[6,0,self[changed_protected_field].ids]]
# wich is what the web client posts in case there is a editable X2M in form
# that is unchanged.
return (
values[changed_protected_field][0] == 6
and values[changed_protected_field][2] == self[changed_protected_field].ids
)

def write(self, values):
if self._l10n_nl_vat_statement_should_check_write(values):
self._l10n_nl_vat_statement_check_state()
return super().write(values)
# before doing anything we check the nl_vat_statement:check_state,
# if this is not allowed it will raise and no further operations
# are necessary.
if not self._l10n_nl_vat_statement_should_check_write(values):
return super().write(values)
# now we add code to check if any of the modified fields present in
# values are the same as existing field value. this is a known limitation
# of odoo web client, it passes all non-readonly value fields in a form
# for writing , even if the value has not been changed.
res = True
protected_fields = self._get_l10n_nl_vat_statement_protected_fields()
protected_fields_in_values = [x for x in values.keys() if x in protected_fields]
if not protected_fields_in_values:
return super().write(values)
# there are protected fields in values dict, we allow them to pass if they
# do not actually change values.
for this in self:
# there are protected fields in dict, we proceed to check if such keys
# are in reality leaving the field the same.
# we need to recreate thisi dict every time
clean_values = dict(values)
for changed_protected_field in protected_fields_in_values:
is_equal = this.check_field_is_equal(
changed_protected_field, clean_values
)
if is_equal:
clean_values.pop(changed_protected_field)
# in case of recordset write we need to evaluate write one by one
# and re-check that our newi clean dict is allowed
if this._l10n_nl_vat_statement_should_check_write(clean_values):
if this._l10n_nl_vat_statement_should_check_write(clean_values):
this._l10n_nl_vat_statement_check_state()
res = res and super(AccountMoveLine, this).write(clean_values)
return res

@api.model
def _l10n_nl_vat_statement_should_check_write(self, values):
Expand Down

0 comments on commit 4460929

Please sign in to comment.