Skip to content

Commit 572de6c

Browse files
authored
Revert "[13.0][OU-FIX] account: avoid two or more move lines pointing to same invoice line"
1 parent e6ec5e8 commit 572de6c

File tree

1 file changed

+26
-42
lines changed

1 file changed

+26
-42
lines changed

addons/account/migrations/13.0.1.1/post-migration.py

Lines changed: 26 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -238,42 +238,24 @@ def migration_invoice_moves(env):
238238
AND ((ail.product_id IS NULL AND aml.product_id IS NULL) OR ail.product_id = aml.product_id)
239239
AND ((ail.uom_id IS NULL AND aml.product_uom_id IS NULL) OR ail.uom_id = aml.product_uom_id)
240240
"""
241-
# Doing loop to fix where two or more move lines points to the same old invoice line
242-
while True:
243-
# Try first with a stricter criteria for matching invoice lines with account move lines
244-
openupgrade.logged_query(
245-
env.cr, query + """
246-
LEFT JOIN account_move_line aml2 ON aml2.old_invoice_line_id = ail.id
247-
""" + minimal_where + """
248-
AND ail.account_id = aml.account_id
249-
AND ai.commercial_partner_id = aml.partner_id
250-
AND ((ail.account_analytic_id IS NULL AND aml.analytic_account_id IS NULL)
251-
OR ail.account_analytic_id = aml.analytic_account_id)
252-
AND aml.old_invoice_line_id IS NULL AND aml2.id IS NULL
253-
RETURNING aml.id""",
254-
)
255-
# Try now with a more relaxed criteria, as it's possible that users change some data on amls
256-
openupgrade.logged_query(
257-
env.cr, query + """
258-
LEFT JOIN account_move_line aml2 ON aml2.old_invoice_line_id = ail.id
259-
""" + minimal_where + """
260-
AND aml.old_invoice_line_id IS NULL AND aml2.id IS NULL
261-
AND rc.anglo_saxon_accounting IS DISTINCT FROM TRUE
262-
RETURNING aml.id""",
263-
)
264-
openupgrade.logged_query(
265-
env.cr, """
266-
UPDATE account_move_line aml
267-
SET old_invoice_line_id = NULL
268-
FROM (SELECT id, ROW_NUMBER() OVER (PARTITION BY old_invoice_line_id ORDER BY id) repeated_invoice_line
269-
FROM account_move_line
270-
WHERE old_invoice_line_id IS NOT NULL) aml2
271-
WHERE aml.id = aml2.id AND aml2.repeated_invoice_line > 1
272-
RETURNING aml.id""",
273-
)
274-
wrong_aml_ids = tuple(x[0] for x in env.cr.fetchall())
275-
if not wrong_aml_ids:
276-
break
241+
# Try first with a stricter criteria for matching invoice lines with account move lines
242+
openupgrade.logged_query(
243+
env.cr, query + minimal_where + """
244+
AND ail.account_id = aml.account_id
245+
AND ai.commercial_partner_id = aml.partner_id
246+
AND ((ail.account_analytic_id IS NULL AND aml.analytic_account_id IS NULL)
247+
OR ail.account_analytic_id = aml.analytic_account_id)
248+
RETURNING aml.id""",
249+
)
250+
aml_ids = tuple(x[0] for x in env.cr.fetchall())
251+
# Try now with a more relaxed criteria, as it's possible that users change some data on amls
252+
openupgrade.logged_query(
253+
env.cr, query + minimal_where + """
254+
AND aml.old_invoice_line_id IS NULL
255+
AND rc.anglo_saxon_accounting IS DISTINCT FROM TRUE
256+
RETURNING aml.id""",
257+
)
258+
aml_ids += tuple(x[0] for x in env.cr.fetchall())
277259
# 2st: exclude from invoice_tab the grouped ones, and create a new separated ones
278260
openupgrade.logged_query(
279261
env.cr, """
@@ -336,13 +318,15 @@ def migration_invoice_moves(env):
336318
WHERE ail.id IN %s
337319
RETURNING id""", (ail_ids, ),
338320
)
321+
aml_ids += tuple(x[0] for x in env.cr.fetchall())
339322
# 3rd: assure they have a corresponding old_invoice_line_id. If not, exclude them from invoice tab
340-
openupgrade.logged_query(
341-
env.cr, """
342-
UPDATE account_move_line
343-
SET exclude_from_invoice_tab = TRUE
344-
WHERE old_invoice_line_id IS NULL""",
345-
)
323+
if aml_ids:
324+
openupgrade.logged_query(
325+
env.cr, """
326+
UPDATE account_move_line
327+
SET exclude_from_invoice_tab = TRUE
328+
WHERE old_invoice_line_id IS NULL""",
329+
)
346330
# Draft or Cancel Invoice Lines
347331
openupgrade.logged_query(
348332
env.cr, """

0 commit comments

Comments
 (0)