Skip to content

Commit

Permalink
[OU-ADD] sale: migration
Browse files Browse the repository at this point in the history
  • Loading branch information
duong77476-viindoo committed Dec 16, 2023
1 parent 73c8df2 commit 8e6faab
Show file tree
Hide file tree
Showing 5 changed files with 263 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docsource/modules150-160.rst
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ Module coverage 15.0 -> 16.0
+-------------------------------------------------+----------------------+-------------------------------------------------+
| resource | Done | |
+-------------------------------------------------+----------------------+-------------------------------------------------+
| sale | | |
| sale | Done | |
+-------------------------------------------------+----------------------+-------------------------------------------------+
| |del| sale_coupon | | |
+-------------------------------------------------+----------------------+-------------------------------------------------+
Expand Down
17 changes: 17 additions & 0 deletions openupgrade_scripts/scripts/sale/16.0.1.2/post-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from openupgradelib import openupgrade


def try_delete_noupdate_records(env):
openupgrade.delete_records_safely_by_xml_id(
env,
[
"sale.mail_notification_paynow_online",
"sale.sale_payment_acquirer_onboarding_wizard_rule",
],
)


@openupgrade.migrate()
def migrate(env, version):
openupgrade.load_data(env.cr, "sale", "16.0.1.2/noupdate_changes.xml")
try_delete_noupdate_records(env)
147 changes: 147 additions & 0 deletions openupgrade_scripts/scripts/sale/16.0.1.2/pre-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
from openupgradelib import openupgrade

_models_renames = [
(
"sale.payment.acquirer.onboarding.wizard",
"sale.payment.provider.onboarding.wizard",
),
]

_tables_renames = [
(
"sale_payment_acquirer_onboarding_wizard",
"sale_payment_provider_onboarding_wizard",
),
]

_renames_xmlids = [
(
"sale_management.menu_product_attribute_action",
"sale.menu_product_attribute_action",
),
]


def _noupdate_switch(env):
openupgrade.set_xml_ids_noupdate_value(
env, "sale", ["model_sale_order_action_share"], False
)


def _remove_table_constraints(env):
openupgrade.delete_sql_constraint_safely(
env, "sale", "sale_order", "date_order_conditional_required"
)


def _drop_sql_views(env):
openupgrade.logged_query(
env.cr,
"""
DROP VIEW IF EXISTS report_all_channels_sales
""",
)


def _fast_fill_analytic_distribution_on_sale_order_line(env):
"""
Dynamically fill analytic_distribution for model that inherit from analytic.mixin
Hmmm this method should be placed in the library
Take a look with example of account.move.line
The idea is to take all the distribution of an account.move.line
which has analytic.tag then form it as a jsonb like {'1': 100, '2': 50}
and also check if the table has analytic_account_column then check if it
exist in the analytic_account of all the analytic_distribution of the analytic tags
then take it as the 100%, which mean an account.move.line both specify '2' as the
analytic.account.id and it has 1 analytic.tag also have that analytic.account then
the value will sum together
"""
if not openupgrade.column_exists(
env.cr, "sale_order_line", "analytic_distribution"
):
openupgrade.logged_query(
env.cr,
"""
ALTER TABLE sale_order_line
ADD COLUMN IF NOT EXISTS analytic_distribution jsonb;
""",
)

openupgrade.logged_query(
env.cr,
"""
WITH distribution_data AS (
WITH sub AS (
SELECT
all_line_data.sale_order_line_id,
all_line_data.analytic_account_id,
SUM(all_line_data.percentage) AS percentage
FROM (
SELECT
so_line.id AS sale_order_line_id,
account.id AS analytic_account_id,
100 AS percentage
FROM sale_order_line so_line
JOIN sale_order so ON so.id = so_line.order_id
JOIN account_analytic_account account
ON account.id = so.analytic_account_id
WHERE so.analytic_account_id IS NOT NULL
UNION ALL
SELECT
so_line.id AS sale_order_line_id,
dist.account_id AS analytic_account_id,
dist.percentage AS percentage
FROM sale_order_line so_line
JOIN account_analytic_tag_sale_order_line_rel tag_rel
ON tag_rel.sale_order_line_id = so_line.id
JOIN account_analytic_distribution dist
ON dist.tag_id = tag_rel.account_analytic_tag_id
JOIN account_analytic_tag aat
ON aat.id = tag_rel.account_analytic_tag_id
WHERE aat.active_analytic_distribution = true
) AS all_line_data
GROUP BY all_line_data.sale_order_line_id, all_line_data.analytic_account_id
)
SELECT
sub.sale_order_line_id,
jsonb_object_agg(sub.analytic_account_id::text, sub.percentage)
AS analytic_distribution
FROM sub
GROUP BY sub.sale_order_line_id
)
UPDATE sale_order_line so_line SET analytic_distribution = dist.analytic_distribution
FROM distribution_data dist WHERE so_line.id = dist.sale_order_line_id
""",
)


def _create_ir_model_data_sale_default_invoice_email_template(env):
openupgrade.logged_query(
env.cr,
"""
INSERT INTO ir_model_data (name, res_id, module, model, noupdate)
SELECT
'default_invoice_email_template',
ir_config_parameter.id,
'sale',
'ir.config_parameter',
TRUE
FROM ir_config_parameter
WHERE key = 'sale.default_invoice_email_template'
""",
)


@openupgrade.migrate()
def migrate(env, version):
openupgrade.rename_models(env.cr, _models_renames)
openupgrade.rename_tables(env.cr, _tables_renames)
openupgrade.rename_xmlids(env.cr, _renames_xmlids)
_drop_sql_views(env)
_noupdate_switch(env)
_remove_table_constraints(env)
_fast_fill_analytic_distribution_on_sale_order_line(env)
_create_ir_model_data_sale_default_invoice_email_template(env)
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ DEL ir.model.access: sale.access_product_product_attribute_custom_value_sale_man
DEL ir.model.access: sale.access_report_all_channels_sales
DEL ir.model.access: sale.access_sale_payment_acquirer_onboarding_wizard
DEL ir.model.access: sale.access_sale_report_manager
ir.model.constraint: sale.constraint_sale_order_date_order_conditional_required (changed definition: is now 'check((state in('sale','done') and date_order is not null) or state not in('sale','done'))' ('check((state in('sale','done') and date_order is not null) or state not in('sale','done') )'))
NEW ir.rule: sale.sale_payment_provider_onboarding_wizard_rule (noupdate)
DEL ir.rule: sale.sale_payment_acquirer_onboarding_wizard_rule (noupdate)
NEW ir.ui.menu: sale.menu_product_attribute_action [renamed from sale_management module]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
---Models in module 'sale'---
obsolete model report.all.channels.sales [sql_view]
# DONE: pre-migration: drop view

obsolete model report.sale.report_saleproforma [abstract]
# NOTHING TO DO: obsolete model, does not exist in the database

obsolete model sale.payment.acquirer.onboarding.wizard [transient]
new model sale.payment.provider.onboarding.wizard [transient]
# DONE: pre-migration: renamed model sale.payment.acquirer.onboarding.wizard to sale.payment.provider.onboarding.wizard

---Fields in module 'sale'---
sale / account.analytic.applicability / business_domain (False) : NEW selection_keys: ['bill', 'expense', 'general', 'invoice', 'purchase_order', 'sale_order'], mode: modify
# NOTHING TO DO

sale / account.bank.statement.line / partner_shipping_id (many2one): module is now 'account' ('sale')
sale / account.move / partner_shipping_id (many2one): module is now 'account' ('sale')
sale / account.move.line / is_downpayment (boolean) : NEW
sale / account.payment / partner_shipping_id (many2one): module is now 'account' ('sale')
# NOTHING TO DO

sale / payment.acquirer / so_reference_type (selection) : DEL selection_keys: ['partner', 'so_name']
sale / payment.provider / so_reference_type (selection) : NEW selection_keys: ['partner', 'so_name'], hasdefault: default
# NOTHING TO DO

sale / sale.order / show_update_fpos (boolean) : NEW
sale / sale.order / show_update_pricelist (boolean): not stored anymore
# NOTHING TO DO: handle by ORM

sale / sale.order.line / analytic_distribution_stored_char (char): NEW isfunction: function, stored
# NOTHING TO DO: handle by ORM

sale / sale.order.line / analytic_tag_ids (many2many) : DEL relation: account.analytic.tag
# DONE: fast fill analytic_distribution in pre-migration

sale / sale.order.line / product_type (selection) : previously in module sale_stock
# NOTHING TO DO: handle by ORM

sale / sale.order.line / qty_delivered (float) : not a function anymore
# NOTHING TO DO: only the inverse compute function was removed

sale / sale.order.line / qty_delivered_manual (float) : DEL
# NOTHING TO DO: not used any more, this field is useless since the new compute method from 13.0

---XML records in module 'sale'---
NEW ir.actions.act_window: sale.action_open_sale_payment_provider_onboarding_wizard
DEL ir.actions.act_window: sale.action_open_sale_onboarding_payment_acquirer_wizard
DEL ir.actions.act_window: sale.action_sale_order_form_view
# NOTHING TO DO: noupdate="0" records

ir.actions.server: sale.model_sale_order_action_share (noupdate switched)
# DONE: pre-migration: noupdate switched to 0

NEW ir.config_parameter: sale.default_invoice_email_template (noupdate)
# DONE: pre-migration: create noupdated record in ir_model_data

NEW ir.model.access: sale.access_product_product_attribute_custom_value_sale_user
NEW ir.model.access: sale.access_product_tag_sale_manager
NEW ir.model.access: sale.access_sale_payment_provider_onboarding_wizard
DEL ir.model.access: sale.access_account_account_sale_manager
DEL ir.model.access: sale.access_account_account_type_sale_salesman
DEL ir.model.access: sale.access_account_analytic_tag_sale_salesman
DEL ir.model.access: sale.access_account_move_manager
DEL ir.model.access: sale.access_account_tax_sale_manager
DEL ir.model.access: sale.access_product_product_attribute_custom_value_sale_manager
DEL ir.model.access: sale.access_report_all_channels_sales
DEL ir.model.access: sale.access_sale_payment_acquirer_onboarding_wizard
DEL ir.model.access: sale.access_sale_report_manager
# NOTHING TO DO

ir.model.constraint: sale.constraint_sale_order_date_order_conditional_required (changed definition: is now 'check((state in('sale','done') and date_order is not null) or state not in('sale','done'))' ('check((state in('sale','done') and date_order is not null) or state not in('sale','done') )'))
# DONE: pre-migration: dropped constraint and let ORM add it again with the new definition

NEW ir.rule: sale.sale_payment_provider_onboarding_wizard_rule (noupdate)
DEL ir.rule: sale.sale_payment_acquirer_onboarding_wizard_rule (noupdate)
# DONE: post-migration: delete record noupdate="1"

NEW ir.ui.menu: sale.menu_product_attribute_action [renamed from sale_management module]
# DONE: pre-migration: renamed xmlids

DEL ir.ui.menu: sale.menu_product
NEW ir.ui.view: sale.payment_provider_form
DEL ir.ui.view: sale.acquirer_form_inherit_sale
# NOTHING TO DO

DEL ir.ui.view: sale.mail_notification_paynow_online (noupdate)
# DONE: post-migration: delete record noupdate="1"

DEL ir.ui.view: sale.product_template_form_view_invoice_policy
DEL ir.ui.view: sale.product_template_sale_form_view
DEL ir.ui.view: sale.report_invoice_document_inherit_sale
DEL ir.ui.view: sale.sale_onboarding_order_confirmation_form
NEW mail.template: sale.mail_template_sale_cancellation (noupdate)
# NOTHING TO DO

DEL res.groups: sale.group_delivery_invoice_address [renamed to account module]
# NOTHING TO DO: handle in migration script of 'account' module

0 comments on commit 8e6faab

Please sign in to comment.