diff --git a/openupgrade_scripts/scripts/account/17.0.1.2/post-migration.py b/openupgrade_scripts/scripts/account/17.0.1.2/post-migration.py index 2008133676c..5e18569ff92 100644 --- a/openupgrade_scripts/scripts/account/17.0.1.2/post-migration.py +++ b/openupgrade_scripts/scripts/account/17.0.1.2/post-migration.py @@ -107,53 +107,82 @@ def _onboarding_state_migration(env): def _account_payment_term_migration(env): """ - In post we will update the value_amount field - to respect v17 to ensure total percentage will not - exceed 100% of not <100% - In v16, the payment term might have some cases like - -Case 1 - line 1: value - balance, value_amount - 0.0 - line 2: value - percent, value_amount - 50 - line 3: value - percent, value_amount - 45 - -Case 2 - line 1: value - balance, value_amount - 0.0 - line 2: value - percent, value_amount - 100 - NOTE: in pre we already convert value_amount of balance to 100.0 % - AFTER migration: line 1 of case 1 will have 'value_amount' is 5% - line 2 of case 2 will have 'value_amount' is 100% while line 2 is 0.0% + Switch balance lines to percent and compute the remaining percentage, and convert + old multiple column system to the new delay_type + nb_days. + + https://github.com/odoo/odoo/pull/110274 """ - payment_terms = ( - env["account.payment.term"].with_context(active_test=False).search([]) + openupgrade.logged_query( + env.cr, + """ + UPDATE account_payment_term_line + SET value = 'percent', + value_amount = 100.0 - COALESCE(percentages.percentage, 0) + FROM ( + SELECT + payment_id, + SUM( + CASE WHEN l.value='percent' THEN l.value_amount + ELSE 0 END + ) percentage + FROM account_payment_term_line l + GROUP BY payment_id + ) percentages + WHERE + value = 'balance' AND + percentages.payment_id = account_payment_term_line.payment_id + """, + ) + openupgrade.logged_query( + env.cr, + """ + UPDATE account_payment_term_line + SET delay_type = CASE + WHEN end_month AND COALESCE(months, 0) = 0 + AND COALESCE(days, 0) = 0 + THEN 'days_after_end_of_month' + WHEN end_month AND months = 1 AND COALESCE(days, 0) = 0 + THEN 'days_after_end_of_next_month' + WHEN end_month AND COALESCE(months, 0) <= 1 AND days > 0 + THEN 'days_end_of_month_on_the' + ELSE 'days_after' + END, + nb_days = CASE + WHEN end_month AND months <= 1 + THEN COALESCE(days, 0) + COALESCE(days_after, 0) + ELSE + COALESCE(months, 0)*30 + COALESCE(days, 0) + + COALESCE(days_after, 0) + END + """, + ) + + +def _account_payment_term_early_payment_discount(env): + """Only payment terms with one line and the early payment discount activated are + valid now, so we are going to discard other previous configurations. + """ + openupgrade.logged_query( + env.cr, + """ + WITH sub AS ( + SELECT * FROM ( + SELECT *, + row_number() over (partition BY payment_id ORDER BY id) AS rnum + FROM account_payment_term_line + ) t + WHERE t.rnum = 1 + AND t.discount_days IS NOT NULL + AND t.discount_percentage > 0 + ) + UPDATE account_payment_term apt + SET early_discount = True, + discount_days = sub.discount_days, + discount_percentage = sub.discount_percentage + FROM sub + WHERE sub.payment_id = apt.id + """, ) - for term in payment_terms: - term_lines = term.line_ids.filtered(lambda line: line.value == "percent") - value_amount_total = sum(term_lines.mapped("value_amount")) - if value_amount_total and value_amount_total > 100.0: - term_lines_with_100_percentage = term_lines.filtered( - lambda line: line.value_amount == 100 - ) - term_lines_below_100_percentage = term_lines.filtered( - lambda line: line.value_amount < 100 - ) - if len(term_lines_with_100_percentage) > 1: - ( - term_lines_with_100_percentage - term_lines_with_100_percentage[0] - ).write( - { - "value_amount": 0.0, - } - ) - if term_lines_below_100_percentage: - remaining_line = term_lines - term_lines_below_100_percentage - if remaining_line: - remaining_line.write( - { - "value_amount": 100 - - sum( - term_lines_below_100_percentage.mapped("value_amount") - ) - } - ) def convert_from_company_dependent( @@ -295,16 +324,8 @@ def _account_tax_group_migration(env): ) if tax_group_name: - new_imd = imd.copy( - { - "res_id": new_tax_group.id, - } - ) - new_imd.write( - { - "name": f"{company_id}_{tax_group_name}", - } - ) + new_imd = imd.copy({"res_id": new_tax_group.id}) + new_imd.write({"name": f"{company_id}_{tax_group_name}"}) openupgrade.logged_query( env.cr, @@ -338,98 +359,6 @@ def _account_tax_group_migration(env): ) -def _account_payment_term_migration(env): - """ - https://github.com/odoo/odoo/pull/110274 - """ - openupgrade.logged_query( - env.cr, - """ - UPDATE account_payment_term_line - SET value = 'percent', - value_amount = 100.0 - COALESCE(percentages.percentage, 0) - FROM ( - SELECT - payment_id, - SUM( - CASE WHEN l.value='percent' THEN l.value_amount - ELSE 0 END - ) percentage - FROM account_payment_term_line l - GROUP BY payment_id - ) percentages - WHERE - value = 'balance' AND - percentages.payment_id = account_payment_term_line.payment_id - """, - ) - openupgrade.logged_query( - env.cr, - """ - UPDATE account_payment_term_line - SET delay_type = CASE - WHEN end_month = true AND COALESCE(months, 0) = 0 - AND COALESCE(days, 0) = 0 - THEN 'days_after_end_of_month' - WHEN end_month = true AND months = 1 AND COALESCE(days, 0) = 0 - THEN 'days_after_end_of_next_month' - WHEN end_month = true AND COALESCE(months, 0) <= 1 AND days > 0 - THEN 'days_end_of_month_on_the' - ELSE 'days_after' - END, - nb_days = CASE - WHEN end_month = true AND months <= 1 - THEN COALESCE(days, 0) + COALESCE(days_after, 0) - ELSE - COALESCE(months, 0)*30 + COALESCE(days, 0) + - COALESCE(days_after, 0) - END - """, - ) - openupgrade.logged_query( - env.cr, - """ - UPDATE account_payment_term term - SET early_pay_discount_computation = com.early_pay_discount_computation - FROM res_company com - WHERE term.company_id = com.id - """, - ) - openupgrade.logged_query( - env.cr, - """ - UPDATE account_payment_term term - SET early_discount = true - WHERE EXISTS ( - SELECT 1 - FROM account_payment_term_line t1 - WHERE t1.payment_id = term.id - AND t1.discount_days IS NOT NULL - AND t1.discount_percentage IS NOT NULL - AND t1.discount_percentage > 0.0 - ); - """, - ) - openupgrade.logged_query( - env.cr, - """ - WITH tmp as( - SELECT payment_id, MAX(discount_days) discount_days, - sum(discount_percentage) discount_percentage - FROM account_payment_term_line - WHERE discount_days IS NOT NULL AND discount_percentage IS NOT NULL - AND discount_percentage > 0.0 - GROUP BY payment_id - ) - UPDATE account_payment_term term - SET discount_days = tmp.discount_days, - discount_percentage = tmp.discount_percentage - FROM tmp - WHERE tmp.payment_id = term.id - """, - ) - - def _force_install_account_payment_term_module_module(env): """ Force install account_payment_term if we need @@ -445,7 +374,7 @@ def _force_install_account_payment_term_module_module(env): ) ) if needs_account_payment_term and account_payment_term_module: - account_payment_term_module.button_install() + account_payment_term_module.state = "to install" openupgrade.copy_columns( env.cr, { @@ -469,6 +398,7 @@ def _force_install_account_payment_term_module_module(env): @openupgrade.migrate() def migrate(env, version): _account_payment_term_migration(env) + _account_payment_term_early_payment_discount(env) _force_install_account_payment_term_module_module(env) openupgrade.load_data(env, "account", "17.0.1.2/noupdate_changes.xml") openupgrade.delete_records_safely_by_xml_id( diff --git a/openupgrade_scripts/scripts/account/17.0.1.2/pre-migration.py b/openupgrade_scripts/scripts/account/17.0.1.2/pre-migration.py index ec205bfeb45..14c03937f96 100644 --- a/openupgrade_scripts/scripts/account/17.0.1.2/pre-migration.py +++ b/openupgrade_scripts/scripts/account/17.0.1.2/pre-migration.py @@ -122,10 +122,7 @@ def _map_account_report_filter_account_type(env): openupgrade.rename_columns( - env.cr, - { - "account_report": [("filter_account_type", None)], - }, + env.cr, {"account_report": [("filter_account_type", None)]} ) openupgrade.logged_query( env.cr, @@ -139,7 +136,7 @@ def _map_account_report_filter_account_type(env): f""" UPDATE account_report SET filter_account_type = CASE - WHEN {openupgrade.get_legacy_name('filter_account_type')} = TRUE THEN 'both' + WHEN {openupgrade.get_legacy_name('filter_account_type')} THEN 'both' ELSE 'disabled' END """, @@ -199,6 +196,17 @@ def _generic_coa_rename_xml_id(env): openupgrade.rename_xmlids(env.cr, xmlids_renames) +def _convert_account_tax_description(env): + openupgrade.rename_columns(env.cr, {"account_tax": [("description", None)]}) + openupgrade.logged_query(env.cr, "ALTER TABLE account_tax ADD description JSONB") + openupgrade.logged_query( + env.cr, + f"""UPDATE account_tax + SET description = {'en_US': {openupgrade.get_legacy_name('description')}} + """, + ) + + def _am_create_delivery_date_column(env): """ Create column then in module need them like l10n_de and sale_stock will fill value, @@ -227,26 +235,6 @@ def _am_create_incoterm_location_column(env): ) -def _aml_update_invoice_date_like_amount_move(env): - openupgrade.logged_query( - env.cr, - """ - ALTER TABLE account_move_line - ADD COLUMN IF NOT EXISTS invoice_date DATE - """, - ) - openupgrade.logged_query( - env.cr, - """ - UPDATE account_move_line aml - SET invoice_date = am.invoice_date - FROM account_move am - WHERE aml.move_id = am.id - AND am.invoice_date IS NOT NULL - """, - ) - - def _am_uniquify_name(env): """ Make move names unique per journal to satisfy the constraint v17 creates @@ -265,21 +253,29 @@ def _am_uniquify_name(env): def _account_report_update_figure_type(env): - openupgrade.logged_query( + openupgrade.copy_columns( env.cr, - """ - UPDATE account_report_column - SET figure_type = 'string' - WHERE figure_type = 'none' - """, + { + "account_report_column": [("figure_type", None, None)], + "account_report_expression": [("figure_type", None, None)], + }, ) - openupgrade.logged_query( + old_column = openupgrade.get_legacy_name("figure_type") + openupgrade.map_values( env.cr, - """ - UPDATE account_report_expression - SET figure_type = 'string' - WHERE figure_type = 'none' - """, + old_column, + "figure_type", + {"none": "string"}, + False, + "account_report_column", + ) + openupgrade.map_values( + env.cr, + old_column, + "figure_type", + {"none": "string"}, + False, + "account_report_expression", ) @@ -319,6 +315,24 @@ def _res_partner_bank_create_column(env): ) +def _pre_create_early_pay_discount_computation(env): + """Avoid triggering the computed method and fill the corresponding value from + companies. + """ + openupgrade.logged_query( + env.cr, "ALTER TABLE res_company ADD early_pay_discount_computation VARCHAR" + ) + openupgrade.logged_query( + env.cr, + """ + UPDATE account_payment_term apt + SET early_pay_discount_computation = com.early_pay_discount_computation + FROM res_company rc + WHERE apt.company_id = rc.id + """, + ) + + @openupgrade.migrate() def migrate(env, version): _map_account_report_filter_account_type(env) @@ -335,11 +349,11 @@ def migrate(env, version): ) openupgrade.rename_fields(env, _fields_renames) convert_column_translatable(env.cr, "account_tax", "description", "jsonb") - openupgrade.copy_columns(env.cr, _columns_copies) + _convert_account_tax_description(env) _am_create_delivery_date_column(env) _am_create_incoterm_location_column(env) - _aml_update_invoice_date_like_amount_move(env) _am_uniquify_name(env) _account_report_update_figure_type(env) _account_tax_repartition_line_merge_repartition_lines_m2o(env) _res_partner_bank_create_column(env) + _pre_create_early_pay_discount_computation(env) diff --git a/openupgrade_scripts/scripts/account/17.0.1.2/upgrade_analysis_work.txt b/openupgrade_scripts/scripts/account/17.0.1.2/upgrade_analysis_work.txt index ade9aa12c50..812538e1f29 100644 --- a/openupgrade_scripts/scripts/account/17.0.1.2/upgrade_analysis_work.txt +++ b/openupgrade_scripts/scripts/account/17.0.1.2/upgrade_analysis_work.txt @@ -8,18 +8,22 @@ obsolete model account.reconcile.model.line.template obsolete model account.reconcile.model.template obsolete model account.tax.repartition.line.template obsolete model account.tax.template -# NOTHING TO DO: these models has been convert to csv definition +# NOTHING TO DO: these models has been converted to CSV definition and existing company records XML-ID follow the same logic obsolete model account.invoice.send [transient] new model account.move.send [transient] -# NOTHING TO DO +# NOTHING TO DO: Replaced feature, but nothing to transfer, being transient models ---Fields in module 'account'--- account / account.account / _order : _order is now 'code, company_id' ('is_off_balance, code, company_id') account / account.account / is_off_balance (boolean) : DEL +# NOTHING TO DO: is_off_balance dissapears, but nobody will miss it, as it was underused and even incorrect (due to blocking certain operations) + account / account.account / message_main_attachment_id (many2one): DEL relation: ir.attachment +# NOTHING TO DO: Feature not used in general + account / account.account / rating_ids (one2many) : NEW relation: rating.rating -# NOTHING TO DO +# NOTHING TO DO: New feature account / account.account.template / account_type (selection) : DEL selection_keys: ['asset_cash', 'asset_current', 'asset_fixed', 'asset_non_current', 'asset_prepayments', 'asset_receivable', 'equity', 'equity_unaffected', 'expense', 'expense_depreciation', 'expense_direct_cost', 'income', 'income_other', 'liability_credit_card', 'liability_current', 'liability_non_current', 'liability_payable', 'off_balance'] account / account.account.template / chart_template_id (many2one) : DEL relation: account.chart.template @@ -92,55 +96,67 @@ account / account.fiscal.position.template / tax_ids (one2many) account / account.fiscal.position.template / vat_required (boolean) : DEL account / account.fiscal.position.template / zip_from (char) : DEL account / account.fiscal.position.template / zip_to (char) : DEL -# NOTHING TO DO - -account / account.full.reconcile / name (char) : DEL required -account / account.move.line / matching_number (char) : not a function anymore -# NOTHING TO DO - account / account.group.template / chart_template_id (many2one) : DEL relation: account.chart.template, required account / account.group.template / code_prefix_end (char) : DEL account / account.group.template / code_prefix_start (char) : DEL account / account.group.template / name (char) : DEL required account / account.group.template / parent_id (many2one) : DEL relation: account.group.template -# NOTHING TO DO +# NOTHING TO DO: These models has been converted to CSV definition and existing company records XML-ID follow the same logic + +account / account.full.reconcile / name (char) : DEL required +account / account.move.line / matching_number (char) : not a function anymore +# NOTHING TO DO: The matching is now expressed through the record ID stored in the aml, instead of a sequence stored in the afr, so no real feature loss account / account.journal / access_token (char) : NEW +# NOTHING TO DO: It's filled dynamically when the content is shared + account / account.journal / activity_user_id (many2one) : not related anymore account / account.journal / activity_user_id (many2one) : now a function account / account.journal / alias_domain (char) : not a function anymore account / account.journal / alias_domain (char) : now related account / account.journal / alias_name (char) : not a function anymore account / account.journal / alias_name (char) : now related +# NOTHING TO DO: Changes on the underlying mixins, but without impact + account / account.journal / message_main_attachment_id (many2one): DEL relation: ir.attachment +# NOTHING TO DO: Non used feature removed + account / account.journal / rating_ids (one2many) : NEW relation: rating.rating +# NOTHING TO DO: New feature (not used, but brought by inheritance) + account / account.move / _order : _order is now 'date desc, name desc, invoice_date desc, id desc' ('date desc, name desc, id desc') +# NOTHING TO DO: Criteria order added, but in third place, not impacting in usual cases + account / account.move / activity_user_id (many2one) : not related anymore account / account.move / activity_user_id (many2one) : now a function -# NOTHING TO DO +# NOTHING TO DO: Changes on the underlying mixins, but without impact account / account.move / amount_total_words (char) : previously in module l10n_dz # NOTHING TO DO: compute no store account / account.move / delivery_date (date) : NEW isfunction: function, stored -# DONE pre-migration: Create column then in module need them like l10n_de and sale_stock will fill value, https://github.com/odoo/odoo/pull/116643 +# DONE: pre-migration: Pre-create the column for not triggering the method. Modules modifying the computation (l10n_de and sale_stock) will fill the value. More info at https://github.com/odoo/odoo/pull/116643 account / account.move / incoterm_location (char) : NEW hasdefault: compute -# DONE pre-migration: Create column then in sale_stock, purchase_stock will fill it in pre, pr: https://github.com/odoo/odoo/pull/118954 +# DONE: pre-migration: Pre-create the column for not triggering the method. Modules modifying the computation (sale_stock and purchase_stock) will fill the value. More info at https://github.com/odoo/odoo/pull/118954 account / account.move / invoice_pdf_report_file (binary): NEW attachment: True -# DONE post-migration: update message_main_attachment_id with res_field and res_id of each invoice, see '_link_invoice_documents' in 'account.move.send' +# DONE: post-migration: update message_main_attachment_id with res_field and res_id of each invoice, see '_link_invoice_documents' in 'account.move.send' account / account.move / rating_ids (one2many) : NEW relation: rating.rating +# NOTHING TO DO: New feature by inheritance (although not used) + account / account.move / send_and_print_values (json) : NEW -# NOTHING TO DO +# NOTHING TO DO: This field is for storing transient data to indicate that the invoice is "programmed" to be sent, which is a new feature, so empty is OK. account / account.move.line / discount_percentage (float) : DEL +# NOTHING TO DO: Auxiliary field for Early Payment discounts from payment terms that seems unused. + account / account.move.line / display_type (selection) : selection_keys is now '['cogs', 'discount', 'epd', 'line_note', 'line_section', 'payment_term', 'product', 'rounding', 'tax']' ('['cogs', 'epd', 'line_note', 'line_section', 'payment_term', 'product', 'rounding', 'tax']') -# NOTHING TO DO: new feature https://github.com/odoo/odoo/pull/133286 +# NOTHING TO DO: new feature "Discount allocation" https://github.com/odoo/odoo/pull/133286 account / account.move.line / invoice_date (date) : NEW isrelated: related, stored -# DONE pre-migration: create column and fill value +# NOTHING TO DO: ORM resolves by SQL this filling being a direct related field with 1 level depth. account / account.move.line / tax_audit (char) : DEL # NOTHING TO DO: deprecated @@ -150,28 +166,39 @@ account / account.partial.reconcile / company_id (many2one) : not r account / account.payment / activity_user_id (many2one) : not related anymore account / account.payment / activity_user_id (many2one) : now a function +# NOTHING TO DO: New feature by inheritance. + account / account.payment / amount_total_words (char) : previously in module l10n_dz +# NOTHING TO DO: Compute non stored field. + account / account.payment / rating_ids (one2many) : NEW relation: rating.rating -# NOTHING TO DO +# NOTHING TO DO: New feature by inheritance. account / account.payment / qr_code (char) : type is now 'html' ('char') # NOTHING TO DO: no store field -account / account.payment.term / currency_id (many2one) : NEW relation: res.currency, hasdefault: default account / account.payment.term / discount_days (integer) : NEW hasdefault: default account / account.payment.term / discount_percentage (float) : NEW hasdefault: default account / account.payment.term / early_discount (boolean) : NEW +account / account.payment.term.line / discount_days (integer) : DEL +account / account.payment.term.line / discount_percentage (float) : DEL +# DONE: post-migration: Transfer when possible the early payment discount from lines to header. + account / account.payment.term / early_pay_discount_computation (selection): NEW selection_keys: ['excluded', 'included', 'mixed'], hasdefault: compute +account / res.company / early_pay_discount_computation (selection): DEL selection_keys: ['excluded', 'included', 'mixed'] +# DONE: pre-migration: Pre-create the column for avoiding triggering the compute and transfer data from companies to all the payment terms. + +account / account.payment.term / currency_id (many2one) : NEW relation: res.currency, hasdefault: default +# NOTHING TO DO: Compute non stored + account / account.payment.term.line / days (integer) : DEL required account / account.payment.term.line / days_after (integer) : DEL account / account.payment.term.line / delay_type (selection) : NEW required, selection_keys: ['days_after', 'days_after_end_of_month', 'days_after_end_of_next_month'], hasdefault: default -account / account.payment.term.line / discount_days (integer) : DEL -account / account.payment.term.line / discount_percentage (float) : DEL account / account.payment.term.line / end_month (boolean) : DEL account / account.payment.term.line / months (integer) : DEL required account / account.payment.term.line / nb_days (integer) : NEW hasdefault: compute account / account.payment.term.line / value (selection) : selection_keys is now '['fixed', 'percent']' ('['balance', 'fixed', 'percent']') -# DONE pre-migration: update discount_percentage (sum of all line percentage), delay_type, value (replace balance with percentage) https://github.com/odoo/odoo/pull/110274/ +# DONE: post-migration: convert columns, type and percentage https://github.com/odoo/odoo/pull/110274/ account / account.reconcile.model / message_main_attachment_id (many2one): DEL relation: ir.attachment account / account.reconcile.model / rating_ids (one2many) : NEW relation: rating.rating @@ -226,7 +253,7 @@ account / account.report / default_opening_date_filter (selection account / account.report / filter_account_type (boolean) : selection_keys is now '['both', 'disabled', 'payable', 'receivable']' ('False') account / account.report / filter_account_type (boolean) : type is now 'selection' ('boolean') -# DONE: map False > disabled and True > both in pre-migration +# DONE: pre-migration: map False > disabled and True > both. account / account.report / filter_aml_ir_filters (boolean): NEW hasdefault: compute account / account.report / filter_hide_0_lines (selection): NEW selection_keys: ['by_default', 'never', 'optional'], hasdefault: compute @@ -242,7 +269,7 @@ account / account.report.line / user_groupby (char) : NEW account / account.report.column / figure_type (selection) : selection_keys is now '['boolean', 'date', 'datetime', 'float', 'integer', 'monetary', 'percentage', 'string']' ('['date', 'datetime', 'float', 'integer', 'monetary', 'none', 'percentage']') account / account.report.expression / figure_type (selection) : selection_keys is now '['boolean', 'date', 'datetime', 'float', 'integer', 'monetary', 'percentage', 'string']' ('['date', 'datetime', 'float', 'integer', 'monetary', 'none', 'percentage']') -# DONE pre-migration: update none -> string +# DONE pre-migration: copy column for preservation and update none -> string account / account.tax / invoice_label (char) : NEW # DONE pre-migration: create jsonb column(because translate=True) and fill value with 'description' field https://github.com/odoo/odoo/pull/113236/commits @@ -339,9 +366,6 @@ account / res.company / chart_template_id (many2one) : DEL re account / res.company / display_invoice_amount_total_words (boolean): NEW # NOTHING TO DO: new feature -account / res.company / early_pay_discount_computation (selection): DEL selection_keys: ['excluded', 'included', 'mixed'] -# DONE pre-migration: Update in all payment terms of every company - account / res.company / invoice_is_download (boolean) : NEW hasdefault: default account / res.company / invoice_is_print (boolean) : DEL # DONE pre-migration: rename field https://github.com/odoo/odoo/pull/119397