|
| 1 | +# Copyright 2025 ForgeFlow S.L. (https://www.forgeflow.com) |
| 2 | +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). |
| 3 | + |
| 4 | +from openupgradelib import openupgrade |
| 5 | + |
| 6 | +field_renames = [ |
| 7 | + ( |
| 8 | + "account.move", |
| 9 | + "account_move", |
| 10 | + "l10n_dk_currency_rate_at_transaction", |
| 11 | + "invoice_currency_rate", |
| 12 | + ), |
| 13 | + ("account.move", "account_move", "payment_id", "origin_payment_id"), |
| 14 | + ("account.move", "account_move", "reversal_move_id", "reversal_move_ids"), |
| 15 | + ("account.move", "account_move", "send_and_print_values", "sending_data"), |
| 16 | + ("account.payment", "account_payment", "destination_journal_id", "journal_id"), |
| 17 | +] |
| 18 | + |
| 19 | +_new_columns = [ |
| 20 | + ("account.bank.statement.line", "company_id", "many2one"), |
| 21 | + ("account.bank.statement.line", "journal_id", "many2one"), |
| 22 | + ("account.journal", "autocheck_on_post", "boolean", True), |
| 23 | + ("account.move", "amount_untaxed_in_currency_signed", "float"), |
| 24 | + ("account.move", "checked", "boolean"), |
| 25 | + ("account.move", "preferred_payment_method_line_id", "many2one"), |
| 26 | + ("account.reconcile.model", "counterpart_type", "selection", "general"), |
| 27 | + ("account.tax", "price_include_override", "selection", "tax_excluded"), |
| 28 | + ("account.payment", "name", "char"), |
| 29 | + ("account.payment", "date", "date"), |
| 30 | + ("account.payment", "memo", "char"), |
| 31 | + ("account.payment", "state", "selection"), |
| 32 | + ("account.payment", "is_sent", "boolean"), |
| 33 | +] |
| 34 | + |
| 35 | + |
| 36 | +def rename_selection_option(env): |
| 37 | + openupgrade.logged_query( |
| 38 | + env.cr, |
| 39 | + """ |
| 40 | + UPDATE account_account |
| 41 | + SET internal_group = 'off' |
| 42 | + WHERE internal_group = 'off_balance'""", |
| 43 | + ) |
| 44 | + openupgrade.logged_query( |
| 45 | + env.cr, |
| 46 | + """ |
| 47 | + UPDATE account_report |
| 48 | + SET default_opening_date_filter = 'last_' || substr( |
| 49 | + default_opening_date_filter, 10) |
| 50 | + WHERE left(default_opening_date_filter, 9) = 'previous_'""", |
| 51 | + ) |
| 52 | + openupgrade.logged_query( |
| 53 | + env.cr, |
| 54 | + """ |
| 55 | + UPDATE account_report_expression |
| 56 | + SET date_scope = 'strict_range' |
| 57 | + WHERE date_scope = 'normal'""", |
| 58 | + ) |
| 59 | + openupgrade.logged_query( |
| 60 | + env.cr, |
| 61 | + """ |
| 62 | + UPDATE account_tax |
| 63 | + SET price_include_override = 'tax_included' |
| 64 | + WHERE price_include""", |
| 65 | + ) |
| 66 | + |
| 67 | + |
| 68 | +def update_account_move_amount_untaxed_in_currency_signed(env): |
| 69 | + openupgrade.logged_query( |
| 70 | + env.cr, |
| 71 | + """ |
| 72 | + UPDATE account_move |
| 73 | + SET amount_untaxed_in_currency_signed = CASE |
| 74 | + WHEN move_type IN ('out_invoice', 'in_refund', 'out_receipt') |
| 75 | + THEN COALESCE(amount_untaxed, 0.0) |
| 76 | + ELSE (-1) * COALESCE(amount_untaxed, 0.0) END""", |
| 77 | + ) |
| 78 | + |
| 79 | + |
| 80 | +def update_account_move_checked(env): |
| 81 | + openupgrade.logged_query( |
| 82 | + env.cr, |
| 83 | + """ |
| 84 | + UPDATE account_move |
| 85 | + SET checked = TRUE |
| 86 | + WHERE to_check IS DISTINCT FROM TRUE""", |
| 87 | + ) |
| 88 | + |
| 89 | + |
| 90 | +def fill_account_move_preferred_payment_method_line_id(env): |
| 91 | + if openupgrade.column_exists(env.cr, "account_move", "preferred_payment_method_id"): |
| 92 | + # having account_check_printing module |
| 93 | + openupgrade.logged_query( |
| 94 | + env.cr, |
| 95 | + """ |
| 96 | + UPDATE account_move am |
| 97 | + SET preferred_payment_method_line_id = COALESCE(apml.id, apml2.id) |
| 98 | + FROM account_payment_method apm |
| 99 | + LEFT JOIN account_payment_method_line apml ON |
| 100 | + apml.payment_method_id = apm.id AND apml.journal_id = am.journal_id |
| 101 | + LEFT JOIN account_payment_method_line apml2 ON |
| 102 | + apml2.payment_method_id = apm.id AND apml2.journal_id IS NULL |
| 103 | + WHERE am.preferred_payment_method_id = apm.id""", |
| 104 | + ) |
| 105 | + |
| 106 | + |
| 107 | +def adapt_account_move_sending_data(env): |
| 108 | + # sp_partner_id -> author_partner_id: |
| 109 | + openupgrade.logged_query( |
| 110 | + env.cr, |
| 111 | + """ |
| 112 | + UPDATE account_move |
| 113 | + SET sending_data = jsonb_set(sending_data::jsonb - 'sp_partner_id', |
| 114 | + '{author_partner_id}', sending_data::jsonb->'sp_partner_id') |
| 115 | + WHERE sending_data IS NOT NULL AND sending_data::jsonb ? 'sp_partner_id'""", |
| 116 | + ) |
| 117 | + # sp_user_id -> author_user_id: |
| 118 | + openupgrade.logged_query( |
| 119 | + env.cr, |
| 120 | + """ |
| 121 | + UPDATE account_move |
| 122 | + SET sending_data = jsonb_set(sending_data::jsonb - 'sp_user_id', |
| 123 | + '{author_user_id}', sending_data::jsonb->'sp_user_id') |
| 124 | + WHERE sending_data IS NOT NULL AND sending_data::jsonb ? 'sp_user_id'""", |
| 125 | + ) |
| 126 | + # mail_template_id -> mail_template: |
| 127 | + env.cr.execute( |
| 128 | + """ |
| 129 | + SELECT id, sending_data::jsonb->'mail_template_id' as mail_template_id |
| 130 | + FROM account_move |
| 131 | + WHERE sending_data IS NOT NULL AND sending_data::jsonb ? 'mail_template_id'""", |
| 132 | + ) |
| 133 | + for move_id, mail_template_id in env.cr.fetchall(): |
| 134 | + mail_template = env.ref(mail_template_id) |
| 135 | + openupgrade.logged_query( |
| 136 | + env.cr, |
| 137 | + f""" |
| 138 | + UPDATE account_move |
| 139 | + SET sending_data = jsonb_set(sending_data::jsonb - 'mail_template_id', |
| 140 | + '{{mail_template}}', {mail_template}) |
| 141 | + WHERE id = {move_id}""", |
| 142 | + ) |
| 143 | + # send_mail: True -> 'sending_methods': {"email"}: |
| 144 | + openupgrade.logged_query( |
| 145 | + env.cr, |
| 146 | + """ |
| 147 | + UPDATE account_move |
| 148 | + SET sending_data = jsonb_set(sending_data::jsonb - 'send_mail', |
| 149 | + '{sending_methods}', '{"email"}'::jsonb) |
| 150 | + WHERE sending_data IS NOT NULL |
| 151 | + AND sending_data::jsonb @> '{"send_mail": True}'::jsonb""", |
| 152 | + ) |
| 153 | + |
| 154 | + |
| 155 | +def fill_account_payment(env): |
| 156 | + openupgrade.logged_query( |
| 157 | + env.cr, |
| 158 | + """ |
| 159 | + UPDATE account_payment ap |
| 160 | + SET memo = am.ref, |
| 161 | + state= CASE WHEN am.state = 'cancel' THEN 'canceled', |
| 162 | + WHEN am.payment_state = 'paid' THEN 'paid', |
| 163 | + WHEN am.state = 'posted' THEN 'in_process', |
| 164 | + ELSE am.state END, |
| 165 | + is_sent = am.is_move_sent, |
| 166 | + name = CASE WHEN am.name != '/' THEN am.name, |
| 167 | + ELSE 'Draft Payment', |
| 168 | + date = ap.create_date::date, |
| 169 | + journal_id = CASE WHEN ap.journal_id IS NULL |
| 170 | + AND aj.type in ('bank', 'cash', 'credit') |
| 171 | + THEN am.journal_id ELSE ap.journal_id END |
| 172 | + FROM account_move am |
| 173 | + LEFT JOIN account_journal aj ON am.journal_id = aj.id |
| 174 | + WHERE ap.move_id = am.id""", |
| 175 | + ) |
| 176 | + |
| 177 | + |
| 178 | +@openupgrade.migrate() |
| 179 | +def migrate(env, version): |
| 180 | + openupgrade.rename_fields(env, field_renames) |
| 181 | + openupgrade.add_columns(env, _new_columns) |
| 182 | + update_account_move_amount_untaxed_in_currency_signed(env) |
| 183 | + update_account_move_checked(env) |
| 184 | + fill_account_move_preferred_payment_method_line_id(env) |
| 185 | + adapt_account_move_sending_data(env) |
| 186 | + rename_selection_option(env) |
| 187 | + fill_account_payment(env) |
| 188 | + openupgrade.convert_field_to_html( |
| 189 | + env.cr, |
| 190 | + "account_tax", |
| 191 | + "description", |
| 192 | + "description", |
| 193 | + verbose=False, |
| 194 | + translate=True, |
| 195 | + ) |
0 commit comments