From f38d02a1656c20745a6e2a880f7dda550157c055 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20=C4=90=E1=BA=A1i=20D=C6=B0=C6=A1ng?= Date: Thu, 13 Jun 2024 14:28:48 +0700 Subject: [PATCH 1/2] [OU-ADD] hr_expense: migration to 17.0 --- docsource/modules160-170.rst | 2 +- .../hr_expense/17.0.2.0/post-migration.py | 24 +++ .../hr_expense/17.0.2.0/pre-migration.py | 153 ++++++++++++++++++ .../17.0.2.0/upgrade_analysis_work.txt | 98 +++++++++++ 4 files changed, 276 insertions(+), 1 deletion(-) create mode 100644 openupgrade_scripts/scripts/hr_expense/17.0.2.0/post-migration.py create mode 100644 openupgrade_scripts/scripts/hr_expense/17.0.2.0/pre-migration.py create mode 100644 openupgrade_scripts/scripts/hr_expense/17.0.2.0/upgrade_analysis_work.txt diff --git a/docsource/modules160-170.rst b/docsource/modules160-170.rst index 90b72d524ce5..28cd65b6834f 100644 --- a/docsource/modules160-170.rst +++ b/docsource/modules160-170.rst @@ -162,7 +162,7 @@ Module coverage 16.0 -> 17.0 +---------------------------------------------------+----------------------+-------------------------------------------------+ | hr_contract | Done | | +---------------------------------------------------+----------------------+-------------------------------------------------+ -| hr_expense | | | +| hr_expense | Done | | +---------------------------------------------------+----------------------+-------------------------------------------------+ | hr_fleet | | | +---------------------------------------------------+----------------------+-------------------------------------------------+ diff --git a/openupgrade_scripts/scripts/hr_expense/17.0.2.0/post-migration.py b/openupgrade_scripts/scripts/hr_expense/17.0.2.0/post-migration.py new file mode 100644 index 000000000000..2b8586a1f67a --- /dev/null +++ b/openupgrade_scripts/scripts/hr_expense/17.0.2.0/post-migration.py @@ -0,0 +1,24 @@ +# Copyright 2024 Viindoo Technology Joint Stock Company (Viindoo) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +from openupgradelib import openupgrade + + +def _company_update_company_expense_allowed_payment_method_line(env): + env.cr.execute( + """ + SELECT id, company_expense_journal_id FROM res_company company + """ + ) + for company_id, company_expense_journal_id in env.cr.fetchall(): + company = env["res.company"].browse(company_id) + if company_expense_journal_id: + journal = env["account.journal"].browse(company_expense_journal_id) + company.company_expense_allowed_payment_method_line_ids = ( + journal.outbound_payment_method_line_ids + ) + + +@openupgrade.migrate() +def migrate(env, version): + openupgrade.load_data(env, "hr_expense", "17.0.2.0/noupdate_changes.xml") + _company_update_company_expense_allowed_payment_method_line(env) diff --git a/openupgrade_scripts/scripts/hr_expense/17.0.2.0/pre-migration.py b/openupgrade_scripts/scripts/hr_expense/17.0.2.0/pre-migration.py new file mode 100644 index 000000000000..39f2cf3e9022 --- /dev/null +++ b/openupgrade_scripts/scripts/hr_expense/17.0.2.0/pre-migration.py @@ -0,0 +1,153 @@ +# Copyright 2024 Viindoo Technology Joint Stock Company (Viindoo) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +from openupgradelib import openupgrade + +_fields_renames = [ + ( + "hr.expense", + "hr_expense", + "amount_tax", + "tax_amount_currency", + ), + ( + "hr.expense", + "hr_expense", + "amount_tax_company", + "tax_amount", + ), + ( + "hr.expense", + "hr_expense", + "price_unit", + "unit_price", + ), + ( + "hr.expense", + "hr_expense", + "untaxed_amount", + "untaxed_amount_currency", + ), + ( + "hr.expense", + "hr_expense", + "total_amount", + "total_amount_currency", + ), + ( + "hr.expense", + "hr_expense", + "total_amount_company", + "total_amount", + ), + ( + "hr.expense.sheet", + "hr_expense_sheet", + "total_amount_taxes", + "total_tax_amount", + ), +] + + +def _am_update_expense_sheet_id(env): + openupgrade.logged_query( + env.cr, + """ + ALTER TABLE account_move + ADD COLUMN IF NOT EXISTS expense_sheet_id INTEGER + """, + ) + openupgrade.logged_query( + env.cr, + """ + UPDATE account_move am + SET expense_sheet_id = sheet.id + FROM hr_expense_sheet sheet + WHERE am.id = sheet.account_move_id + """, + ) + + +def _hr_expense_update_state(env): + openupgrade.logged_query( + env.cr, + """ + UPDATE hr_expense expense + SET state = 'reported' + FROM hr_expense_sheet sheet + WHERE expense.sheet_id = sheet.id AND + sheet.state = 'draft' + """, + ) + + +def _hr_expense_sheet_fill_approval_state(env): + openupgrade.logged_query( + env.cr, + """ + ALTER TABLE hr_expense_sheet + ADD COLUMN IF NOT EXISTS approval_state VARCHAR + """, + ) + openupgrade.logged_query( + env.cr, + """ + UPDATE hr_expense_sheet + SET approval_state = CASE + WHEN state = 'submit' then 'submit' + WHEN state in ('approve', 'post', 'done') then 'approve' + WHEN state = 'cancel' then 'cancel' + END + """, + ) + + +def _hr_expense_sheet_journal(env): + openupgrade.logged_query( + env.cr, + """ + ALTER TABLE hr_expense_sheet + ADD COLUMN IF NOT EXISTS employee_journal_id INTEGER, + ADD COLUMN IF NOT EXISTS payment_method_line_id INTEGER; + """, + ) + openupgrade.logged_query( + env.cr, + """ + UPDATE hr_expense_sheet sheet + SET payment_method_line_id = method_line.id + FROM account_journal journal + JOIN account_payment_method_line method_line + ON method_line.journal_id = journal.id + JOIN account_payment_method method + ON method.id = method_line.payment_method_id + WHERE sheet.bank_journal_id = journal.id + AND method.payment_type = 'outbound' + """, + ) + openupgrade.logged_query( + env.cr, + """ + UPDATE hr_expense_sheet + SET employee_journal_id = journal_id + WHERE journal_id IS NOT NULL + """, + ) + openupgrade.logged_query( + env.cr, + """ + UPDATE hr_expense_sheet sheet + SET journal_id = CASE + WHEN bank_journal_id IS NOT NULL + AND expense.payment_mode = 'company_account' THEN bank_journal_id + FROM hr_expense expense + WHERE expense.sheet_id = sheet.id + """, + ) + + +@openupgrade.migrate() +def migrate(env, version): + _am_update_expense_sheet_id(env) + openupgrade.rename_fields(env, _fields_renames) + _hr_expense_update_state(env) + _hr_expense_sheet_fill_approval_state(env) diff --git a/openupgrade_scripts/scripts/hr_expense/17.0.2.0/upgrade_analysis_work.txt b/openupgrade_scripts/scripts/hr_expense/17.0.2.0/upgrade_analysis_work.txt new file mode 100644 index 000000000000..e65f3845f433 --- /dev/null +++ b/openupgrade_scripts/scripts/hr_expense/17.0.2.0/upgrade_analysis_work.txt @@ -0,0 +1,98 @@ +---Models in module 'hr_expense'--- +---Fields in module 'hr_expense'--- +hr_expense / account.bank.statement.line / expense_sheet_id (one2many) : type is now 'many2one' ('one2many') +hr_expense / account.move / expense_sheet_id (one2many) : type is now 'many2one' ('one2many') +hr_expense / account.payment / expense_sheet_id (one2many) : type is now 'many2one' ('one2many') +# DONE pre-migration: set expense_sheet_id for each account_move_id present at hr.expense.sheet + +hr_expense / hr.employee / filter_for_expense (boolean) : NEW +# NOTHING TO DO: no store field + +hr_expense / hr.expense / activity_user_id (many2one) : not related anymore +hr_expense / hr.expense / activity_user_id (many2one) : now a function +hr_expense / hr.expense / rating_ids (one2many) : NEW relation: rating.rating +# NOTHING TO DO + +hr_expense / hr.expense / amount_tax (float) : DEL +hr_expense / hr.expense / amount_tax_company (float) : DEL +# DONE pre-migration: rename amount_tax to tax_amount_currency, amount_tax_company to tax_amount + + +hr_expense / hr.expense / is_refused (boolean) : DEL +# NOTHING TO DO: dead code https://github.com/odoo/odoo/pull/110518 + +hr_expense / hr.expense / price_unit (float) : NEW required, isfunction: function, stored +# DONE pre-migration: rename to unit_price + +hr_expense / hr.expense / reference (char) : DEL +hr_expense / hr.expense / sample (boolean) : DEL +# NOTHING TO DO: deprecated field https://github.com/odoo/odoo/pull/130244/commits/266b482264576903cf736ffe2bddfc71f7a7a4ec + +hr_expense / hr.expense / state (selection) : selection_keys is now '['approved', 'done', 'draft', 'refused', 'reported', 'submitted']' ('['approved', 'done', 'draft', 'refused', 'reported']') +# DONE pre-migration: if any expense has expense sheet and the sheet is on draft state, then update state of expense to 'reported' + +hr_expense / hr.expense / tax_amount (float) : NEW isfunction: function, stored +hr_expense / hr.expense / tax_amount_currency (float) : NEW isfunction: function, stored +# DONE pre-migration: rename from amount_tax_company, amount_tax + +hr_expense / hr.expense / total_amount_company (float) : DEL +hr_expense / hr.expense / total_amount_currency (float) : NEW hasdefault: compute +hr_expense / hr.expense / unit_amount (float) : DEL required +hr_expense / hr.expense / untaxed_amount (float) : DEL +hr_expense / hr.expense / untaxed_amount_currency (float): NEW isfunction: function, stored +# DONE pre-migration: total_amount_company -> total_amount, total_amount -> total_amount_currency, untaxed_amount -> untaxed_amount_currency + +hr_expense / hr.expense.sheet / account_move_id (many2one) : DEL relation: account.move +hr_expense / hr.expense.sheet / account_move_ids (one2many) : NEW relation: account.move +# DONE pre-migration: set expense_sheet_id for each account_move_id present at hr.expense.sheet + +hr_expense / hr.expense.sheet / activity_user_id (many2one) : not related anymore +hr_expense / hr.expense.sheet / activity_user_id (many2one) : now a function +# NOTHING TO DO + +hr_expense / hr.expense.sheet / address_id (many2one) : DEL relation: res.partner +# NOTHING TO DO: deprecated field + +hr_expense / hr.expense.sheet / amount_residual (float) : not related anymore +hr_expense / hr.expense.sheet / amount_residual (float) : now a function +# NOTHING TO DO + +hr_expense / hr.expense.sheet / approval_state (selection) : NEW selection_keys: ['approve', 'cancel', 'submit'] +# DONE pre-migration: create column and fill value according to state of expense sheet + +hr_expense / hr.expense.sheet / bank_journal_id (many2one) : DEL relation: account.journal +# TODO: update outbout payment method line of bank_journal_id into 'payment_method_line_id', https://github.com/odoo/odoo/pull/110518/ + +hr_expense / hr.expense.sheet / department_id (many2one) : now related +# NOTHING TO DO + +hr_expense / hr.expense.sheet / employee_journal_id (many2one): NEW relation: account.journal, hasdefault: default +hr_expense / hr.expense.sheet / journal_id (many2one) : now a function +# DONE pre-migration: fill value by 'journal_id' then update 'journal_id' by 'payment_method_line_id.jounal_id' if 'paid by company (bank_journal_id)', https://github.com/odoo/odoo/pull/110518/ + +hr_expense / hr.expense.sheet / payment_method_line_id (many2one): NEW relation: account.payment.method.line, hasdefault: compute +# DONE: update outbount payment method line of bank_journal_id into 'payment_method_line_id', https://github.com/odoo/odoo/pull/110518/ + +hr_expense / hr.expense.sheet / rating_ids (one2many) : NEW relation: rating.rating +hr_expense / hr.expense.sheet / state (selection) : now a function +# NOTHING TO DO + +hr_expense / hr.expense.sheet / total_amount_taxes (float) : DEL +hr_expense / hr.expense.sheet / total_tax_amount (float) : NEW isfunction: function, stored +# DONE pre-migration total_amount_taxes - > total_tax_amount + +hr_expense / res.company / company_expense_allowed_payment_method_line_ids (many2many): NEW relation: account.payment.method.line +hr_expense / res.company / company_expense_journal_id (many2one): DEL relation: account.journal +# DONE post-migration: update 'company_expense_allowed_payment_method_line_ids' using 'company_expense_journal_id.outbound_payment_method_line_ids', https://github.com/odoo/odoo/pull/110518/ + +hr_expense / res.company / expense_product_id (many2one) : NEW relation: product.product +# NOTHING TO DO + +---XML records in module 'hr_expense'--- +ir.actions.act_window: hr_expense.action_hr_expense_sheet_my_all (deleted domain) +NEW ir.actions.report: hr_expense.action_report_expense_sheet_img +NEW ir.ui.view: hr_expense.product_product_expense_kanban_view +NEW ir.ui.view: hr_expense.report_expense_sheet_img +NEW mail.message.subtype: hr_expense.mt_expense_entry_delete (noupdate) +NEW mail.message.subtype: hr_expense.mt_expense_reset (noupdate) +# NOTHING TO DO From a52d908c781ea7685f613f06cce7c2558cacd1c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miquel=20Ra=C3=AFch?= Date: Fri, 14 Mar 2025 18:22:11 +0100 Subject: [PATCH 2/2] [OU-FIX] hr_expense: finish migration --- .../hr_expense/17.0.2.0/post-migration.py | 11 +++++++- .../hr_expense/17.0.2.0/pre-migration.py | 26 ++++++++++++++----- .../17.0.2.0/upgrade_analysis_work.txt | 21 +++++++-------- 3 files changed, 38 insertions(+), 20 deletions(-) diff --git a/openupgrade_scripts/scripts/hr_expense/17.0.2.0/post-migration.py b/openupgrade_scripts/scripts/hr_expense/17.0.2.0/post-migration.py index 2b8586a1f67a..07112ed53d01 100644 --- a/openupgrade_scripts/scripts/hr_expense/17.0.2.0/post-migration.py +++ b/openupgrade_scripts/scripts/hr_expense/17.0.2.0/post-migration.py @@ -20,5 +20,14 @@ def _company_update_company_expense_allowed_payment_method_line(env): @openupgrade.migrate() def migrate(env, version): - openupgrade.load_data(env, "hr_expense", "17.0.2.0/noupdate_changes.xml") _company_update_company_expense_allowed_payment_method_line(env) + openupgrade.load_data(env, "hr_expense", "17.0.2.0/noupdate_changes.xml") + openupgrade.delete_record_translations( + env.cr, + "hr_expense", + ( + "hr_expense_template_refuse_reason", + "hr_expense_template_register", + "hr_expense_template_register_no_user", + ), + ) diff --git a/openupgrade_scripts/scripts/hr_expense/17.0.2.0/pre-migration.py b/openupgrade_scripts/scripts/hr_expense/17.0.2.0/pre-migration.py index 39f2cf3e9022..0bfb329791ab 100644 --- a/openupgrade_scripts/scripts/hr_expense/17.0.2.0/pre-migration.py +++ b/openupgrade_scripts/scripts/hr_expense/17.0.2.0/pre-migration.py @@ -18,8 +18,8 @@ ( "hr.expense", "hr_expense", + "unit_amount", "price_unit", - "unit_price", ), ( "hr.expense", @@ -68,6 +68,17 @@ def _am_update_expense_sheet_id(env): def _hr_expense_update_state(env): + openupgrade.logged_query( + env.cr, + """ + UPDATE hr_expense expense + SET state = 'submitted' + FROM hr_expense_sheet sheet + WHERE expense.sheet_id = sheet.id AND + expense.state in ('reported', 'done') AND + sheet.account_move_id IS NULL + """, + ) openupgrade.logged_query( env.cr, """ @@ -75,7 +86,7 @@ def _hr_expense_update_state(env): SET state = 'reported' FROM hr_expense_sheet sheet WHERE expense.sheet_id = sheet.id AND - sheet.state = 'draft' + sheet.state = 'draft' """, ) @@ -121,7 +132,7 @@ def _hr_expense_sheet_journal(env): JOIN account_payment_method method ON method.id = method_line.payment_method_id WHERE sheet.bank_journal_id = journal.id - AND method.payment_type = 'outbound' + AND method.payment_type = 'outbound' """, ) openupgrade.logged_query( @@ -136,11 +147,11 @@ def _hr_expense_sheet_journal(env): env.cr, """ UPDATE hr_expense_sheet sheet - SET journal_id = CASE - WHEN bank_journal_id IS NOT NULL - AND expense.payment_mode = 'company_account' THEN bank_journal_id + SET journal_id = sheet.bank_journal_id FROM hr_expense expense - WHERE expense.sheet_id = sheet.id + WHERE expense.sheet_id = sheet.id AND + expense.payment_mode = 'company_account' AND + sheet.bank_journal_id IS NOT NULL """, ) @@ -151,3 +162,4 @@ def migrate(env, version): openupgrade.rename_fields(env, _fields_renames) _hr_expense_update_state(env) _hr_expense_sheet_fill_approval_state(env) + _hr_expense_sheet_journal(env) diff --git a/openupgrade_scripts/scripts/hr_expense/17.0.2.0/upgrade_analysis_work.txt b/openupgrade_scripts/scripts/hr_expense/17.0.2.0/upgrade_analysis_work.txt index e65f3845f433..7de26e5d41bd 100644 --- a/openupgrade_scripts/scripts/hr_expense/17.0.2.0/upgrade_analysis_work.txt +++ b/openupgrade_scripts/scripts/hr_expense/17.0.2.0/upgrade_analysis_work.txt @@ -3,6 +3,8 @@ hr_expense / account.bank.statement.line / expense_sheet_id (one2many) : type is now 'many2one' ('one2many') hr_expense / account.move / expense_sheet_id (one2many) : type is now 'many2one' ('one2many') hr_expense / account.payment / expense_sheet_id (one2many) : type is now 'many2one' ('one2many') +hr_expense / hr.expense.sheet / account_move_id (many2one) : DEL relation: account.move +hr_expense / hr.expense.sheet / account_move_ids (one2many) : NEW relation: account.move # DONE pre-migration: set expense_sheet_id for each account_move_id present at hr.expense.sheet hr_expense / hr.employee / filter_for_expense (boolean) : NEW @@ -21,31 +23,28 @@ hr_expense / hr.expense / amount_tax_company (float) : DEL hr_expense / hr.expense / is_refused (boolean) : DEL # NOTHING TO DO: dead code https://github.com/odoo/odoo/pull/110518 +hr_expense / hr.expense / unit_amount (float) : DEL required hr_expense / hr.expense / price_unit (float) : NEW required, isfunction: function, stored -# DONE pre-migration: rename to unit_price +# DONE pre-migration: rename to price_unit hr_expense / hr.expense / reference (char) : DEL hr_expense / hr.expense / sample (boolean) : DEL # NOTHING TO DO: deprecated field https://github.com/odoo/odoo/pull/130244/commits/266b482264576903cf736ffe2bddfc71f7a7a4ec hr_expense / hr.expense / state (selection) : selection_keys is now '['approved', 'done', 'draft', 'refused', 'reported', 'submitted']' ('['approved', 'done', 'draft', 'refused', 'reported']') +# DONE pre-migration: if any expense has expense sheet and the sheet doesn't have account move and the expense is on reported or done state, then update state of expense to 'submitted' https://github.com/odoo/odoo/commit/8f9ca3e29e21596cd317d976edaf454cd0f33bf1 # DONE pre-migration: if any expense has expense sheet and the sheet is on draft state, then update state of expense to 'reported' hr_expense / hr.expense / tax_amount (float) : NEW isfunction: function, stored hr_expense / hr.expense / tax_amount_currency (float) : NEW isfunction: function, stored -# DONE pre-migration: rename from amount_tax_company, amount_tax +# DONE pre-migration: rename from amount_tax_company, amount_tax hr_expense / hr.expense / total_amount_company (float) : DEL hr_expense / hr.expense / total_amount_currency (float) : NEW hasdefault: compute -hr_expense / hr.expense / unit_amount (float) : DEL required hr_expense / hr.expense / untaxed_amount (float) : DEL hr_expense / hr.expense / untaxed_amount_currency (float): NEW isfunction: function, stored # DONE pre-migration: total_amount_company -> total_amount, total_amount -> total_amount_currency, untaxed_amount -> untaxed_amount_currency -hr_expense / hr.expense.sheet / account_move_id (many2one) : DEL relation: account.move -hr_expense / hr.expense.sheet / account_move_ids (one2many) : NEW relation: account.move -# DONE pre-migration: set expense_sheet_id for each account_move_id present at hr.expense.sheet - hr_expense / hr.expense.sheet / activity_user_id (many2one) : not related anymore hr_expense / hr.expense.sheet / activity_user_id (many2one) : now a function # NOTHING TO DO @@ -61,17 +60,15 @@ hr_expense / hr.expense.sheet / approval_state (selection) : NEW se # DONE pre-migration: create column and fill value according to state of expense sheet hr_expense / hr.expense.sheet / bank_journal_id (many2one) : DEL relation: account.journal -# TODO: update outbout payment method line of bank_journal_id into 'payment_method_line_id', https://github.com/odoo/odoo/pull/110518/ +hr_expense / hr.expense.sheet / payment_method_line_id (many2one): NEW relation: account.payment.method.line, hasdefault: compute +# DONE: pre-migration: update outbound payment method line of bank_journal_id into 'payment_method_line_id', https://github.com/odoo/odoo/pull/110518/ hr_expense / hr.expense.sheet / department_id (many2one) : now related # NOTHING TO DO hr_expense / hr.expense.sheet / employee_journal_id (many2one): NEW relation: account.journal, hasdefault: default hr_expense / hr.expense.sheet / journal_id (many2one) : now a function -# DONE pre-migration: fill value by 'journal_id' then update 'journal_id' by 'payment_method_line_id.jounal_id' if 'paid by company (bank_journal_id)', https://github.com/odoo/odoo/pull/110518/ - -hr_expense / hr.expense.sheet / payment_method_line_id (many2one): NEW relation: account.payment.method.line, hasdefault: compute -# DONE: update outbount payment method line of bank_journal_id into 'payment_method_line_id', https://github.com/odoo/odoo/pull/110518/ +# DONE pre-migration: fill value by 'employee_journal_id' then update 'journal_id' by 'payment_method_line_id.journal_id' if 'paid by company (bank_journal_id)', https://github.com/odoo/odoo/pull/110518/ hr_expense / hr.expense.sheet / rating_ids (one2many) : NEW relation: rating.rating hr_expense / hr.expense.sheet / state (selection) : now a function