Skip to content

Commit

Permalink
[OU-ADD] purchase_requisition: migration data
Browse files Browse the repository at this point in the history
- migration analytic_distribution
- join purchase order with purchase order group
  • Loading branch information
SonCrits committed Jul 21, 2023
1 parent abfeb87 commit f103cf7
Show file tree
Hide file tree
Showing 4 changed files with 145 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 @@ -664,7 +664,7 @@ Module coverage 15.0 -> 16.0
+-------------------------------------------------+----------------------+-------------------------------------------------+
| purchase_product_matrix | |No DB layout changes. |
+-------------------------------------------------+----------------------+-------------------------------------------------+
| purchase_requisition | | |
| purchase_requisition |Done | |
+-------------------------------------------------+----------------------+-------------------------------------------------+
| purchase_requisition_stock | | |
+-------------------------------------------------+----------------------+-------------------------------------------------+
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from openupgradelib import openupgrade


def _fill_purchase_order_group(env):
tender_xml = env.ref("purchase_requisition.type_multi")
if tender_xml:
tender_type_id = list(tender_xml.get_external_id().keys())[0]
purchase_group = env["purchase.order"].read_group(
[('requisition_id', '!=', False), ('requisition_id.type_id', '=', tender_type_id)],
['requisition_id'],
['requisition_id']
)
mapped_data = {group['requisition_id'][0]: group['requisition_id_count'] for group in purchase_group}
for data in mapped_data:
order_group = env["purchase.order.group"].create({})
env["purchase.requisition"].browse(data).purchase_ids.write({
'purchase_group_id': order_group.id
})


@openupgrade.migrate()
def migrate(env, version):
_fill_purchase_order_group(env)
openupgrade.delete_records_safely_by_xml_id(
env,
["purchase_requisition.seq_purchase_tender", "purchase_requisition.type_multi"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from openupgradelib import openupgrade


def _fill_analytic_distribution_on_purchase_requisition_line(env):
if not openupgrade.column_exists(
env.cr, "purchase_requisition_line", "analytic_distribution"
):
openupgrade.logged_query(
env.cr,
"""
ALTER TABLE purchase_requisition_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.requis_line_id,
all_line_data.account_analytic_id,
SUM(all_line_data.percentage) AS percentage
FROM (
SELECT
requis_line.id AS requis_line_id,
account.id AS account_analytic_id,
100 AS percentage
FROM purchase_requisition_line requis_line
JOIN account_analytic_account account
ON account.id = requis_line.account_analytic_id
WHERE requis_line.account_analytic_id IS NOT NULL
UNION ALL
SELECT
requis_line.id AS requis_line_id,
dist.account_id AS account_analytic_id,
dist.percentage AS percentage
FROM purchase_requisition_line requis_line
JOIN account_analytic_tag_purchase_requisition_line_rel tag_rel
ON tag_rel.purchase_requisition_line_id = requis_line.id
JOIN account_analytic_distribution dist
ON dist.tag_id = tag_rel.account_analytic_tag_id
) AS all_line_data
GROUP BY all_line_data.requis_line_id, all_line_data.account_analytic_id
)
SELECT
sub.requis_line_id,
jsonb_object_agg(
sub.account_analytic_id::text, sub.percentage
) AS analytic_distribution
FROM sub
GROUP BY sub.requis_line_id
)
UPDATE purchase_requisition_line requis_line
SET analytic_distribution = dist.analytic_distribution
FROM distribution_data dist
WHERE requis_line.id = dist.requis_line_id;
""",
)


@openupgrade.migrate()
def migrate(env, version):
_fill_analytic_distribution_on_purchase_requisition_line(env)
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---Models in module 'purchase_requisition'---
new model purchase.order.group
new model purchase.requisition.alternative.warning [transient]
new model purchase.requisition.create.alternative [transient]
# NOTHING TO DO

---Fields in module 'purchase_requisition'---
purchase_requisition / product.template / purchase_requisition (selection): DEL selection_keys: ['rfq', 'tenders']
purchase_requisition / purchase.order / purchase_group_id (many2one) : NEW relation: purchase.order.group
purchase_requisition / purchase.order.group / order_ids (one2many) : NEW relation: purchase.order
# TO DO: post-migration https://github.com/Viindoo/OpenUpgrade/pull/365#discussion_r1270195905
- Chỗ này chúng ta cần tìm các `purchase_ids` của requision hiện tại có kiểu là call for tender, sau đó nhóm chúng lại vào 1 group.
- Group này sẽ được tạo mỗi khi có 1 purchase_requisition được tạo ?
- Vậy group này tạo sẽ như nào
Sử dụng câu lệnh InSERT INTO purchase_order_group DEFAULT VALUES mỗi khi tìm thấy 1 group cần xác định, sau đó gán purchase_group_id cho những purchase_order vừa tìm kiếm
Vậy làm thế nào để xác định có 1 group cần thêm ?

Tìm kiếm tất cả purchase order có requisition mà type của nó là `type_multi`
Nhóm tất cả chúng lại theo requisition, xác định count(*) là len của chúng
Lặp qua từng group vừa nhóm phía trên, trên mỗi group , tạo 1 purchase_order_group, sau đó gán giá trị purchase_order_group các purchase_order trong group này = purchase_order_group

purchase_requisition / purchase.requisition.line / account_analytic_id (many2one): DEL relation: account.analytic.account
purchase_requisition / purchase.requisition.line / analytic_distribution (json) : NEW hasdefault: compute
purchase_requisition / purchase.requisition.line / analytic_distribution_search (json): NEW
purchase_requisition / purchase.requisition.line / analytic_precision (integer) : NEW hasdefault: default
purchase_requisition / purchase.requisition.line / analytic_tag_ids (many2many) : DEL relation: account.analytic.tag
# DONE pre-migration. comment at https://github.com/Viindoo/OpenUpgrade/pull/462#discussion_r1267849219

---XML records in module 'purchase_requisition'---
DEL ir.actions.act_window: purchase_requisition.tender_type_action
NEW ir.model.access: purchase_requisition.access_purchase_requisition_alternative_warning
NEW ir.model.access: purchase_requisition.access_purchase_requisition_create_alternative
NEW ir.model.access: purchase_requisition.access_purchase_requisition_purchase_order_group
# NOTHING TO DO

DEL ir.sequence: purchase_requisition.seq_purchase_tender (noupdate)
# DONE: post-migration: remove safety noupdate xml record

DEL ir.ui.menu: purchase_requisition.menu_purchase_requisition_type
NEW ir.ui.view: purchase_requisition.purchase_order_kpis_tree_inherit_purchase_requisition
NEW ir.ui.view: purchase_requisition.purchase_order_line_compare_tree
NEW ir.ui.view: purchase_requisition.purchase_order_tree_inherit_purchase_requisition
NEW ir.ui.view: purchase_requisition.purchase_requisition_alternative_warning_form
NEW ir.ui.view: purchase_requisition.purchase_requisition_create_alternative_form
DEL ir.ui.view: purchase_requisition.product_template_form_view_inherit
DEL ir.ui.view: purchase_requisition.res_config_settings_view_form
# NOTHING TO DO

DEL purchase.requisition.type: purchase_requisition.type_multi (noupdate)
# DONE: post-migration: remove safety noupdate xml record

0 comments on commit f103cf7

Please sign in to comment.