Skip to content

Commit

Permalink
[ADD] migration script for module 'hr_expense' (OCA#565)
Browse files Browse the repository at this point in the history
  • Loading branch information
JordiBForgeFlow authored and hbrunn committed Jul 29, 2016
1 parent 2b69bca commit a77f4da
Show file tree
Hide file tree
Showing 4 changed files with 226 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ hr_expense / hr.expense / quantity (float) : NEW re
hr_expense / hr.expense / state (selection) : NEW required: required, selection_keys: ['approve', 'cancel', 'done', 'draft', 'post', 'submit'], req_default: draft
hr_expense / hr.expense / tax_ids (many2many) : NEW relation: account.tax
hr_expense / hr.expense / unit_amount (float) : NEW required: required
hr_expense / hr.expense / website_message_ids (one2many): NEW relation: mail.message
hr_expense / product.template / can_be_expensed (boolean) : NEW
hr_expense / product.template / hr_expense_ok (boolean) : DEL
hr_expense / product.template / hr_expense_ok (boolean) : DEL
---XML records in module 'hr_expense'---
NEW ir.actions.act_window: hr_expense.action_approved_expense
NEW ir.actions.act_window: hr_expense.action_hr_expense_report_filtered
Expand Down
113 changes: 113 additions & 0 deletions addons/hr_expense/migrations/9.0.2.0/openupgrade_analysis_work.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
---Fields in module 'hr_expense'---
hr_expense / hr.expense / bank_journal_id (many2one) : NEW relation: account.journal
# Nothing to do : new fields

hr_expense / hr.expense / analytic_account_id (many2one): NEW relation: account.analytic.account
# TODO : get the value from hr.expense.line

hr_expense / hr.expense / account_move_id (many2one) : NEW relation: account.move
hr_expense / hr.expense / company_id (many2one) : NEW relation: res.company
hr_expense / hr.expense / currency_id (many2one) : NEW relation: res.currency
hr_expense / hr.expense / date (date) : NEW
hr_expense / hr.expense / department_id (many2one) : NEW relation: hr.department
hr_expense / hr.expense / employee_id (many2one) : NEW relation: hr.employee, required: required, req_default: function
hr_expense / hr.expense / journal_id (many2one) : NEW relation: account.journal
hr_expense / hr.expense / name (char) : NEW required: required
# Nothing To Do : Renaming the table will solve this.

hr_expense / hr.expense / description (text) : NEW
# TODO : rename the field
# Done : done in post-migration script

hr_expense / hr.expense / message_follower_ids (one2many): NEW relation: mail.followers
hr_expense / hr.expense / message_ids (one2many) : NEW relation: mail.message
hr_expense / hr.expense / message_last_post (datetime) : NEW
# TODO : ??? # Concerns module Mail (has to be done in another migration script specifically for that module) ---> Nothing to do

hr_expense / hr.expense / payment_mode (selection) : NEW selection_keys: ['company_account', 'own_account']
hr_expense / hr.expense / product_id (many2one) : NEW relation: product.product, required: required
hr_expense / hr.expense / product_uom_id (many2one) : NEW relation: product.uom, required: required, req_default: function
hr_expense / hr.expense / quantity (float) : NEW required: required, req_default: 1
hr_expense / hr.expense / unit_amount (float) : NEW required: required
# TODO : create new hr.expense record for each hr.expense.line (product)

hr_expense / hr.expense / state (selection) : NEW required: required, selection_keys: ['approve', 'cancel', 'done', 'draft', 'post', 'submit'], req_default: draft
# TODO : Need to map the values the new values
#Done : done in post-migration script

hr_expense / hr.expense / tax_ids (many2many) : NEW relation: account.tax
# Nothing to do : new field

hr_expense / product.template / can_be_expensed (boolean) : NEW
hr_expense / product.template / hr_expense_ok (boolean) : DEL
# TODO : rename the field
# Done : done in post-migration script


---XML records in module 'hr_expense'---
NEW ir.actions.act_window: hr_expense.action_approved_expense
NEW ir.actions.act_window: hr_expense.action_hr_expense_report_filtered
NEW ir.actions.act_window: hr_expense.action_request_approve_expense
NEW ir.actions.act_window: hr_expense.hr_expense_action
NEW ir.actions.act_window: hr_expense.hr_expense_action_from_department
NEW ir.actions.act_window: hr_expense.hr_expense_refuse_wizard_action
DEL ir.actions.act_window: hr_expense.action_hr_expense_report_all
DEL ir.actions.act_window: hr_expense.product_normal_form_view_installer
NEW ir.actions.act_window.view: hr_expense.hr_expense_form_action
NEW ir.actions.act_window.view: hr_expense.hr_expense_kanban_action
NEW ir.actions.act_window.view: hr_expense.hr_expense_tree_action
NEW ir.actions.client: hr_expense.action_client_expense_menu
NEW ir.actions.server: hr_expense.hr_expense_entry_action_server
NEW ir.actions.server: hr_expense.hr_expense_submit_action_server
NEW ir.model.access: hr_expense.access_hr_expense_employee
NEW ir.model.access: hr_expense.access_hr_expense_user
DEL ir.model.access: hr_expense.access_account_analytic_journal_user
DEL ir.model.access: hr_expense.access_account_fiscalyear_user
DEL ir.model.access: hr_expense.access_account_journal_period_manager
DEL ir.model.access: hr_expense.access_account_period_user
DEL ir.model.access: hr_expense.access_hr_expense_expense_employee
DEL ir.model.access: hr_expense.access_hr_expense_expense_user
DEL ir.model.access: hr_expense.access_hr_expense_line_employee
DEL ir.model.access: hr_expense.access_hr_expense_line_user
DEL ir.model.access: hr_expense.access_hr_expense_report_manager
DEL ir.model.access: hr_expense.access_product_price_type_user
DEL ir.sequence.type: hr_expense.seq_type_hr_expense_invoice
NEW ir.ui.menu: hr_expense.menu_expense_approved
NEW ir.ui.menu: hr_expense.menu_expense_to_approve
NEW ir.ui.menu: hr_expense.menu_hr_expense
NEW ir.ui.menu: hr_expense.menu_hr_expense_configuration
NEW ir.ui.menu: hr_expense.menu_hr_expense_root
DEL ir.ui.menu: hr_expense.menu_hr_expense_report_all
DEL ir.ui.menu: hr_expense.next_id_49
NEW ir.ui.view: hr_expense.hr_department_view_kanban
NEW ir.ui.view: hr_expense.hr_expense_form_view
NEW ir.ui.view: hr_expense.hr_expense_kanban_view
NEW ir.ui.view: hr_expense.hr_expense_my_tree
NEW ir.ui.view: hr_expense.hr_expense_refuse_wizard_view_form
NEW ir.ui.view: hr_expense.hr_expense_view_graph
NEW ir.ui.view: hr_expense.hr_expense_view_pivot
DEL ir.ui.view: hr_expense.product_expense_installer_tree_view
DEL ir.ui.view: hr_expense.view_editable_expenses_tree
DEL ir.ui.view: hr_expense.view_expenses_form
DEL ir.ui.view: hr_expense.view_expenses_line_tree
DEL ir.ui.view: hr_expense.view_hr_expense_report_graph
DEL ir.ui.view: hr_expense.view_hr_expense_report_search
NEW ir.values: hr_expense.action_hr_expense_account_entry
NEW ir.values: hr_expense.action_hr_expense_submit
NEW mail.message.subtype: hr_expense.mt_department_expense_confirmed
NEW web.tip: hr_expense.expense_tip_1
NEW web.tip: hr_expense.expense_tip_2
NEW web.tip: hr_expense.expense_tip_3
DEL workflow: hr_expense.wkf_expenses
DEL workflow.activity: hr_expense.act_accepted
DEL workflow.activity: hr_expense.act_confirm
DEL workflow.activity: hr_expense.act_done
DEL workflow.activity: hr_expense.act_draft
DEL workflow.activity: hr_expense.act_refused
DEL workflow.transition: hr_expense.t1
DEL workflow.transition: hr_expense.t10
DEL workflow.transition: hr_expense.t2
DEL workflow.transition: hr_expense.t4
DEL workflow.transition: hr_expense.t6
DEL workflow.transition: hr_expense.t7
DEL workflow.transition: hr_expense.t8
78 changes: 78 additions & 0 deletions addons/hr_expense/migrations/9.0.2.0/post-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# -*- coding: utf-8 -*-
# © 2016 Eficent Business and IT Consulting Services S.L.
# © 2016 Serpent Consulting Services Pvt. Ltd.
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).

import logging
from openupgradelib import openupgrade
logger = logging.getLogger('OpenUpgrade')


def map_expense_state(cr):
# Mapping values of state field for hr_expense
openupgrade.map_values(
cr, openupgrade.get_legacy_name('state'), 'state',
[('confirm', 'submit'), ('accepted', 'approve'), ('done', 'post'),
('paid', 'done'), ('cancelled', 'cancel')],
table='hr_expense')


def hr_expense(cr):
# Sets hr_expense_line product values to hr_expense
cr.execute("""
UPDATE hr_expense h SET product_id = l.product_id, unit_amount =
l.unit_amount, quantity = l.unit_quantity,
analytic_account_id = l.analytic_account FROM hr_expense_line l
WHERE l.expense_id = h.id
""")

# Counting one2many hr_expense_line and later creating hr_expense record
# for it.
cr.execute("""
SELECT * from (SELECT expense_id, COUNT(expense_id) AS "no_of_expenses",
case when COUNT(expense_id)>1 then true else null end as "consider"
FROM hr_expense_line GROUP BY expense_id) as voila where consider is
not null""")
expense_count = cr.dictfetchall()
for x in expense_count:
expense = x['expense_id']
no_of_expense = x['no_of_expenses']
cr.execute("""
select id from hr_expense_line where expense_id = %s
""" % expense)
expense_line_ids = cr.fetchall()
line_ids = [n[0] for n in expense_line_ids[1:]]
for z, p in zip(range(no_of_expense-1), line_ids):
cr.execute("""
INSERT INTO hr_expense
(company_id, currency_id, journal_id, employee_id, state,
date, account_move_id, name, bank_journal_id,
untaxed_amount, payment_mode, analytic_account_id,
create_date, write_date, create_uid, write_uid,
product_uom_id, unit_amount, quantity, product_id)
SELECT company_id, currency_id, journal_id, employee_id,
state, date, account_move_id, name, bank_journal_id, 0.00.
'own_account',
(select analytic_account from hr_expense_line where id =
%(a)s),
(select create_date from hr_expense_line where id = %(a)s),
(select write_date from hr_expense_line where id = %(a)s),
(select create_uid from hr_expense_line where id = %(a)s),
(select write_uid from hr_expense_line where id = %(a)s),
(select uom_id from hr_expense_line where id = %(a)s),
(select unit_amount from hr_expense_line where id = %(a)s),
(select unit_quantity from hr_expense_line where id = %(a)s),
(select product_id from hr_expense_line where id = %(a)s)
FROM hr_expense where id = %(b)s
""" % {'a': p, 'b': expense})
cr.execute("""
UPDATE hr_expense h SET total_amount =
(select cast(round(unit_amount*quantity) as
decimal(18,2)) from hr_expense e where e.id=h.id)
""")


@openupgrade.migrate()
def migrate(cr, version):
map_expense_state(cr)
hr_expense(cr)
34 changes: 34 additions & 0 deletions addons/hr_expense/migrations/9.0.2.0/pre-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# -*- coding: utf-8 -*-
# © 2016 Eficent Business and IT Consulting Services S.L.
# © 2016 Serpent Consulting Services Pvt. Ltd.
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).


from openupgradelib import openupgrade


column_copies = {
'hr_expense': [
('state', None, None),
],
}

column_renames = {
'product_template': [
('hr_expense_ok', 'can_be_expensed'),
],
'hr_expense': [
('note', 'description'),
],
}

table_renames = [
('hr_expense_expense', 'hr_expense'),
]


@openupgrade.migrate()
def migrate(cr, version):
openupgrade.rename_tables(cr, table_renames)
openupgrade.rename_columns(cr, column_renames)
openupgrade.copy_columns(cr, column_copies)

0 comments on commit a77f4da

Please sign in to comment.