@@ -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 ()
846914def 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 )
0 commit comments