Skip to content

Commit

Permalink
[REF] l10n_do_accounting: use v16 _compute_name() code
Browse files Browse the repository at this point in the history
  • Loading branch information
jose-pcg committed May 16, 2024
1 parent 390cb15 commit 0bf118d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 79 deletions.
2 changes: 1 addition & 1 deletion l10n_do_accounting/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"category": "Localization",
"license": "LGPL-3",
"website": "https://github.com/odoo-dominicana",
"version": "16.0.1.5.4",
"version": "16.0.1.5.5",
# any module necessary for this one to work correctly
"depends": ["l10n_latam_invoice_document", "l10n_do"],
# always loaded
Expand Down
98 changes: 20 additions & 78 deletions l10n_do_accounting/models/monkey_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,89 +8,31 @@ class AccountMove(models.Model):

@api.depends("posted_before", "state", "journal_id", "date")
def _compute_name(self):
def journal_key(move):
return (move.journal_id, move.journal_id.refund_sequence and move.move_type)

def date_key(move):
return (move.date.year, move.date.month)

grouped = defaultdict( # key: journal_id, move_type
lambda: defaultdict( # key: first adjacent (date.year, date.month)
lambda: {
"records": self.env["account.move"],
"format": False,
"format_values": False,
"reset": False,
}
)
)
self = self.sorted(lambda m: (m.date, m.ref or "", m.id))
highest_name = self[0]._get_last_sequence(lock=False) if self else False

# Group the moves by journal and month
for move in self:
if (
not highest_name
and move == self[0]
and not move.posted_before
and move.date
):
# In the form view, we need to compute a default sequence so that the user can edit
# it. We only check the first move as an approximation (enough for new in form view)
pass
elif (move.name and move.name != "/") or move.state != "posted":
try:
if not move.posted_before:
move._constrains_date_sequence()
# Has already a name or is not posted, we don't add to a batch
continue
except ValidationError:
# Has never been posted and the name doesn't match the date: recompute it
pass
group = grouped[journal_key(move)][date_key(move)]
if not group["records"]:
# Compute all the values needed to sequence this whole group
move._set_next_sequence()
(
group["format"],
group["format_values"],
) = move._get_sequence_format_param(move.name)
group["reset"] = move._deduce_sequence_number_reset(move.name)
group["records"] += move

# Fusion the groups depending on the sequence reset and the format used because `seq` is
# the same counter for multiple groups that might be spread in multiple months.
final_batches = []
for journal_group in grouped.values():
journal_group_changed = True
for date_group in journal_group.values():
if (
journal_group_changed
or final_batches[-1]["format"] != date_group["format"]
or dict(final_batches[-1]["format_values"], seq=0)
!= dict(date_group["format_values"], seq=0)
):
final_batches += [date_group]
journal_group_changed = False
elif date_group["reset"] == "never":
final_batches[-1]["records"] += date_group["records"]
elif (
date_group["reset"] == "year"
and final_batches[-1]["records"][0].date.year
== date_group["records"][0].date.year
):
final_batches[-1]["records"] += date_group["records"]
if move.state == "cancel":
continue

move_has_name = move.name and move.name != "/"
if move_has_name or move.state != "posted":
if not move.posted_before and not move._sequence_matches_date():
if move._get_last_sequence(lock=False):
move.name = False
continue
else:
final_batches += [date_group]

# Give the name based on previously computed values
for batch in final_batches:
for move in batch["records"]:
move.name = batch["format"].format(**batch["format_values"])
batch["format_values"]["seq"] += 1
batch["records"]._compute_split_sequence()
if (
move_has_name
and move.posted_before
or not move_has_name
and move._get_last_sequence(lock=False)
):
continue
if move.date and (not move_has_name or not move._sequence_matches_date()):
move._set_next_sequence()

self.filtered(lambda m: not m.name).name = "/"
self.filtered(lambda m: not m.name and not move.quick_edit_mode).name = "/"
self._inverse_name()

for move in self.filtered(
lambda x: x.country_code == "DO"
Expand Down

0 comments on commit 0bf118d

Please sign in to comment.