|
| 1 | +# Copyright 2026 Hunki Enterprises BV |
| 2 | +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). |
| 3 | + |
| 4 | +from openupgradelib import openupgrade |
| 5 | + |
| 6 | +from odoo import fields |
| 7 | + |
| 8 | + |
| 9 | +def update_from_coa_generic(env, spec): |
| 10 | + """ |
| 11 | + Update some models from the COA according to spec: |
| 12 | + {model_name1: [fieldname1, fieldname2, ...], model_name2: [...], ...} |
| 13 | +
|
| 14 | + TODO: Move this to openupgradelib if needed for more migrations than |
| 15 | + account, stock_account |
| 16 | + """ |
| 17 | + |
| 18 | + for company in env["res.company"].search([]): |
| 19 | + AccountChartTemplate = env["account.chart.template"].with_context( |
| 20 | + default_company_id=company.id, |
| 21 | + allowed_company_ids=company.ids, |
| 22 | + tracking_disable=True, |
| 23 | + delay_account_group_sync=True, |
| 24 | + lang="en_US", |
| 25 | + chart_template_load=True, |
| 26 | + ) |
| 27 | + |
| 28 | + if company.chart_template not in AccountChartTemplate._template_register: |
| 29 | + openupgrade.logger.error( |
| 30 | + "chart template %s unknown (not yet migrated?)", |
| 31 | + company.chart_template, |
| 32 | + ) |
| 33 | + continue |
| 34 | + |
| 35 | + def ref_or_id(ref_or_id, model_name, AccountChartTemplate=AccountChartTemplate): |
| 36 | + if isinstance(ref_or_id, int): |
| 37 | + return env[model_name].browse(ref_or_id) |
| 38 | + return AccountChartTemplate.ref(ref_or_id, False) or env[model_name].browse( |
| 39 | + [] |
| 40 | + ) |
| 41 | + |
| 42 | + template_data = AccountChartTemplate._get_chart_template_data( |
| 43 | + company.chart_template |
| 44 | + ) |
| 45 | + |
| 46 | + company_data = {} |
| 47 | + for model_name, field_names in spec.items(): |
| 48 | + company_data[model_name] = { |
| 49 | + record_id: { |
| 50 | + key: value |
| 51 | + for key, value in record_data.items() |
| 52 | + if key in field_names and not ref_or_id(record_id, model_name)[key] |
| 53 | + } |
| 54 | + for record_id, record_data in template_data[model_name].items() |
| 55 | + if any(record_data.get(key) for key in field_names) |
| 56 | + } |
| 57 | + AccountChartTemplate._load_data(company_data) |
| 58 | + |
| 59 | + |
| 60 | +def update_from_coa(env): |
| 61 | + """ |
| 62 | + Set fields account.account#{account_stock_expense_id,account_stock_variation_id} |
| 63 | + and res.company#{account_stock_journal_id,account_stock_valuation_id} |
| 64 | + from localization if not set elsewhere and the localization sets it |
| 65 | + """ |
| 66 | + update_from_coa_generic( |
| 67 | + env, |
| 68 | + { |
| 69 | + "res.company": ["account_stock_journal_id", "account_stock_valuation_id"], |
| 70 | + "account.account": [ |
| 71 | + "account_stock_expense_id", |
| 72 | + "account_stock_variation_id", |
| 73 | + ], |
| 74 | + }, |
| 75 | + ) |
| 76 | + |
| 77 | + |
| 78 | +def stock_lot_avg_cost(env): |
| 79 | + """ |
| 80 | + Run compute method on stock.lot#avg_cost |
| 81 | + """ |
| 82 | + lots = ( |
| 83 | + env["stock.lot"] |
| 84 | + .search( |
| 85 | + [ |
| 86 | + ("lot_valuated", "=", True), |
| 87 | + ] |
| 88 | + ) |
| 89 | + .with_context(to_date=fields.Datetime.now()) |
| 90 | + ) |
| 91 | + for records in openupgrade.chunked(lots): |
| 92 | + records._compute_avg_cost() |
| 93 | + |
| 94 | + |
| 95 | +def stock_move_is_fields(env): |
| 96 | + """ |
| 97 | + Run compute methods for stock.move#is_* |
| 98 | + """ |
| 99 | + moves = env["stock.move"].search( |
| 100 | + [ |
| 101 | + ("state", "=", "done"), |
| 102 | + ] |
| 103 | + ) |
| 104 | + for records in openupgrade.chunked(moves): |
| 105 | + records._compute_is_in() |
| 106 | + records._compute_is_out() |
| 107 | + records._compute_is_dropship() |
| 108 | + |
| 109 | + |
| 110 | +@openupgrade.migrate() |
| 111 | +def migrate(env, version): |
| 112 | + update_from_coa(env) |
| 113 | + stock_lot_avg_cost(env) |
| 114 | + stock_move_is_fields(env) |
0 commit comments