-
-
Notifications
You must be signed in to change notification settings - Fork 695
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[OU-ADD] purchase_requisition: migration data
- migration analytic_distribution - join purchase order with purchase order group - remove safety noupdate xml record
- Loading branch information
Showing
4 changed files
with
151 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
openupgrade_scripts/scripts/purchase_requisition/16.0.0.1/post-migration.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from openupgradelib import openupgrade | ||
|
||
|
||
def _fill_purchase_order_group(env): | ||
tender_xml = env.ref("purchase_requisition.type_multi", raise_if_not_found=False) | ||
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"], | ||
) |
67 changes: 67 additions & 0 deletions
67
openupgrade_scripts/scripts/purchase_requisition/16.0.0.1/pre-migration.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
50 changes: 50 additions & 0 deletions
50
openupgrade_scripts/scripts/purchase_requisition/16.0.0.1/upgrade_analysis_work.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |