From 417782d5ea2523f3952bfa16e2385d2c2f66e597 Mon Sep 17 00:00:00 2001 From: Leo Tran Date: Sat, 19 Mar 2022 20:00:08 +0700 Subject: [PATCH] [FIX] acount: fix query --- .../scripts/account/14.0.1.1/end-migration.py | 27 ++++++++++++ .../account/14.0.1.1/post-migration.py | 44 +++++++------------ .../scripts/account/14.0.1.1/pre-migration.py | 21 +++++++-- 3 files changed, 59 insertions(+), 33 deletions(-) diff --git a/openupgrade_scripts/scripts/account/14.0.1.1/end-migration.py b/openupgrade_scripts/scripts/account/14.0.1.1/end-migration.py index 8285c7cba8b6..591fb8e85ae3 100644 --- a/openupgrade_scripts/scripts/account/14.0.1.1/end-migration.py +++ b/openupgrade_scripts/scripts/account/14.0.1.1/end-migration.py @@ -30,8 +30,35 @@ def _make_correct_account_type(env): env.cr, query, ) + +def _switch_default_account_and_outstanding_account(env): + openupgrade.logged_query( + env.cr, + """ + WITH subquery as ( + SELECT aml.id as aml_id, + aj.payment_debit_account_id, + aj.payment_credit_account_id, + aml.debit, aml.credit + FROM account_move_line aml + JOIN account_journal aj on aml.journal_id = aj.id + WHERE legacy_statement_unreconcile = TRUE + AND aj.type IN ('bank', 'cash') + ) + UPDATE account_move_line aml + SET account_id = + CASE + WHEN subquery.debit > 0 THEN subquery.payment_debit_account_id + WHEN subquery.credit > 0 THEN subquery.payment_credit_account_id + END + FROM subquery + WHERE aml.id = subquery.aml_id + AND (aml.display_type NOT IN ('line_section', 'line_note') OR aml.display_type IS NULL) + """, + ) @openupgrade.migrate() def migrate(env, version): _make_correct_account_type(env) + _switch_default_account_and_outstanding_account(env) diff --git a/openupgrade_scripts/scripts/account/14.0.1.1/post-migration.py b/openupgrade_scripts/scripts/account/14.0.1.1/post-migration.py index 841fd02daf01..e8512d042ecc 100644 --- a/openupgrade_scripts/scripts/account/14.0.1.1/post-migration.py +++ b/openupgrade_scripts/scripts/account/14.0.1.1/post-migration.py @@ -143,6 +143,20 @@ def fill_partial_reconcile_debit_and_credit_amounts(env): OR r.debit_currency_id = c.currency_id) """, ) + openupgrade.logged_query( + env.cr, + """ + UPDATE account_partial_reconcile r + SET debit_amount_currency = r.amount_currency, + credit_amount_currency = r.amount_currency + FROM res_company c + WHERE r.company_id = c.id + AND r.debit_amount_currency IS NULL + AND r.credit_amount_currency is NULL + AND r.credit_currency_id = r.debit_currency_id + AND r.credit_currency_id != c.currency_id + """, + ) # compute debit and credit amount when currencies are different partial_reconcile_lines = ( env["account.partial.reconcile"] @@ -817,33 +831,6 @@ def _create_ir_config_parameter_constraint_start_date(env): ) -def _switch_default_account_and_outstanding_account(env): - openupgrade.logged_query( - env.cr, - """ - WITH subquery as ( - SELECT aml.id as aml_id, - aj.payment_debit_account_id, - aj.payment_credit_account_id, - aml.debit, aml.credit - FROM account_move_line aml - JOIN account_journal aj on aml.journal_id = aj.id - WHERE legacy_statement_unreconcile = TRUE - AND aj.type IN ('bank', 'cash') - ) - UPDATE account_move_line aml - SET account_id = - CASE - WHEN subquery.debit > 0 THEN subquery.payment_debit_account_id - WHEN subquery.credit > 0 THEN subquery.payment_credit_account_id - END - FROM subquery - WHERE aml.id = subquery.aml_id - AND aml.display_type NOT IN ('line_section', 'line_note') - """, - ) - - @openupgrade.migrate() def migrate(env, version): fill_account_journal_posted_before(env) @@ -880,5 +867,4 @@ def migrate(env, version): ["email_template_edi_invoice", "mail_template_data_payment_receipt"], ) _migrate_currency_exchange_account_company(env) - _create_ir_config_parameter_constraint_start_date(env) - _switch_default_account_and_outstanding_account(env) + _create_ir_config_parameter_constraint_start_date(env) diff --git a/openupgrade_scripts/scripts/account/14.0.1.1/pre-migration.py b/openupgrade_scripts/scripts/account/14.0.1.1/pre-migration.py index c8f8e9bdde3c..625c88c89a4a 100644 --- a/openupgrade_scripts/scripts/account/14.0.1.1/pre-migration.py +++ b/openupgrade_scripts/scripts/account/14.0.1.1/pre-migration.py @@ -178,10 +178,23 @@ def _mark_move_line_statement_unreconciled(env): openupgrade.logged_query( env.cr, """ - UPDATE account_move_line - SET legacy_statement_unreconcile = TRUE - WHERE account_internal_type = 'liquidity' - AND statement_line_id IS NULL + UPDATE account_move_line aml + SET legacy_statement_unreconcile = TRUE + WHERE id in + (select aml.id + from account_move_line aml + join account_journal aj on aml.journal_id = aj.id + join account_account aa on aml.account_id = aa.id + where aj.type in ('bank', 'cash') and aml.statement_line_id is NULL + and (aml.account_id = aj.default_debit_account_id or aml.account_id = aj.default_credit_account_id) + and (aml.display_type NOT IN ('line_section', 'line_note') OR aml.display_type is null) + and ( + aj.is_advance_journal = false + or + + aj.is_advance_journal is null -- Anh em xem lai cho nay vi co database ko co truong nay + ) + and aml.parent_state = 'posted') """, )