Skip to content

Commit 79172fe

Browse files
Merge pull request #5122 from ForgeFlow/14.0-fix-account-company_dependent
[14.0][OU-FIX] account: convert cash rounding fields to company_dependent
2 parents e347582 + eaad6d2 commit 79172fe

2 files changed

Lines changed: 71 additions & 1 deletion

File tree

openupgrade_scripts/scripts/account/14.0.1.1/post-migration.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,8 +842,77 @@ def update_payment_state_partial(env):
842842
)
843843

844844

845+
def cash_rounding_fields_to_company_dependent(env):
846+
"""Usually for such cases, openupgrade.convert_to_company_dependent() should
847+
normally be used, but that function does not seem to support converting
848+
a field to company-dependent without changing its name at the same time.
849+
moreover, it stores boolean values even when they are false (what odoo
850+
does not), and it creates values for all companies, which does not make
851+
sense when a record is linked to a particular company only.
852+
"""
853+
field_names = ["profit_account_id"]
854+
if openupgrade.column_exists(env.cr, "account_cash_rounding", "loss_account_id"):
855+
# loss_account_id comes from pos_cash_rounding
856+
field_names += ["loss_account_id"]
857+
for field_name in field_names:
858+
field_id = (env.ref(f"account.field_account_cash_rounding__{field_name}").id,)
859+
# this many2one property stores its value in the value_reference column
860+
openupgrade.logged_query(
861+
env.cr,
862+
f"""
863+
insert into ir_property (
864+
company_id, fields_id, value_reference, name, res_id, type
865+
)
866+
select
867+
aa.company_id,
868+
%(field_id)s,
869+
'account.account,' || acr.{field_name},
870+
'{field_name}',
871+
'account.cash.rounding,' || acr.id,
872+
'many2one'
873+
from account_cash_rounding acr
874+
join account_account aa ON acr.profit_account_id = aa.id
875+
where
876+
acr.{field_name} is not null
877+
order by acr.id
878+
""",
879+
{"field_id": field_id},
880+
)
881+
if field_name == "loss_account_id":
882+
# for account.cash.rounding records that are not linked to
883+
# a company (i.e, to a profit_account_id), create an
884+
# ir.property record for each company.
885+
openupgrade.logged_query(
886+
env.cr,
887+
"""
888+
insert into ir_property (
889+
company_id,
890+
fields_id,
891+
value_reference,
892+
name,
893+
res_id,
894+
type
895+
)
896+
select
897+
rc.id,
898+
%(field_id)s,
899+
'account.account,' || acr.loss_account_id,
900+
'loss_account_id',
901+
'account.cash.rounding,' || acr.id,
902+
'many2one'
903+
from account_cash_rounding acr
904+
inner join res_company as rc on
905+
acr.profit_account_id is null
906+
and acr.loss_account_id is not null
907+
order by acr.id, rc.id
908+
""",
909+
{"field_id": field_id},
910+
)
911+
912+
845913
@openupgrade.migrate()
846914
def migrate(env, version):
915+
cash_rounding_fields_to_company_dependent(env)
847916
fill_account_journal_posted_before(env)
848917
fill_code_prefix_end_field(env)
849918
fill_default_account_id_field(env)

openupgrade_scripts/scripts/account/14.0.1.1/upgrade_analysis_work.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ account / account.bank.statement.line / payment_ref (char) : NEW
113113
account / account.cash.rounding / account_id (many2one) : DEL relation: account.account
114114
account / account.cash.rounding / profit_account_id (many2one) : NEW relation: account.account, hasdefault
115115
account / account.cash.rounding / loss_account_id (many2one) : previously in module pos_cash_rounding
116-
# DONE: pre_migration: "profit_account_id" is a rename of the "account_id" field.
116+
# DONE: pre-migration: "profit_account_id" is a rename of the "account_id" field.
117+
# DONE: post-migration: transform both fields to company-dependent (filling ir_property)
117118

118119
account / account.chart.template / account_journal_suspense_account_id (many2one): NEW relation: account.account.template
119120
# NOTHING TO DO: new features (filled in data on some localization modules)

0 commit comments

Comments
 (0)