From 21f47ae102e646c20fd8de0f2e99b6f17f346968 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?R=C3=A9mi=20=28Le=20Filament=29?= <remi@le-filament.com>
Date: Mon, 6 May 2024 08:57:59 +0200
Subject: [PATCH] [MIG] migration scripts for keeping fields on account.move

---
 __manifest__.py                         | 14 +++++------
 migrations/13.0.1.1.0/post-migration.py | 33 +++++++++++++++++++++++++
 migrations/13.0.1.1.0/pre-migration.py  | 12 +++++++++
 models/account.py                       |  2 +-
 models/res_users.py                     |  2 +-
 models/sale.py                          |  1 -
 6 files changed, 54 insertions(+), 10 deletions(-)
 create mode 100644 migrations/13.0.1.1.0/post-migration.py
 create mode 100644 migrations/13.0.1.1.0/pre-migration.py

diff --git a/__manifest__.py b/__manifest__.py
index ebe743b..589ef5d 100644
--- a/__manifest__.py
+++ b/__manifest__.py
@@ -1,7 +1,7 @@
 {
     "name": "3A Dentaire - Account",
     "summary": "Modification des modules account pour 3A",
-    "version": "12.1.1.0",
+    "version": "13.0.1.1.0",
     "author": "Le Filament",
     "license": "AGPL-3",
     "application": False,
@@ -10,11 +10,11 @@
         "account", "sale"
     ],
     "data": [
-        'security/security_rules.xml',
-        "views/account_bank_statement.xml",
-        "views/account_move_line.xml",
-        "views/account_views.xml",
-        "views/sale_views.xml",
-        "views/menu.xml",
+        #'security/security_rules.xml',
+        #"views/account_bank_statement.xml",
+        #"views/account_move_line.xml",
+        #"views/account_views.xml",
+        #"views/sale_views.xml",
+        #"views/menu.xml",
     ]
 }
diff --git a/migrations/13.0.1.1.0/post-migration.py b/migrations/13.0.1.1.0/post-migration.py
new file mode 100644
index 0000000..311ac97
--- /dev/null
+++ b/migrations/13.0.1.1.0/post-migration.py
@@ -0,0 +1,33 @@
+# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
+
+from openupgradelib import openupgrade
+from psycopg2 import sql
+
+
+def update_account_move_user_rel_table(cr):
+    table = "openupgrade_legacy_13_0_aiu_rel"
+    openupgrade.logged_query(
+        cr, sql.SQL(
+            """INSERT INTO account_move_res_users_rel
+            (invoice_id, user_id)
+            SELECT am.id, user_id
+            FROM {} rel
+            JOIN account_move am ON am.old_invoice_id = rel.invoice_id"""
+        ).format(sql.Identifier(table))
+    )
+
+def update_account_move_user_provider_id(cr):
+    openupgrade.logged_query(
+        cr, """
+            UPDATE account_move am
+            SET user_provider_id = ai.user_provider_id
+            FROM account_invoice ai
+            WHERE am.old_invoice_id = ai.id
+            """
+    )
+
+
+@openupgrade.migrate(use_env=True)
+def migrate(env, version):
+    update_account_move_user_rel_table(env.cr)
+    update_account_move_user_provider_id(env.cr)
diff --git a/migrations/13.0.1.1.0/pre-migration.py b/migrations/13.0.1.1.0/pre-migration.py
new file mode 100644
index 0000000..3e7e9ea
--- /dev/null
+++ b/migrations/13.0.1.1.0/pre-migration.py
@@ -0,0 +1,12 @@
+# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
+
+from openupgradelib import openupgrade
+
+_tables_rename = [
+    ("account_invoice_res_users_rel", "openupgrade_legacy_13_0_aiu_rel")
+]
+
+
+@openupgrade.migrate(use_env=True)
+def migrate(env, version):
+    openupgrade.rename_tables(env.cr, _tables_rename)
diff --git a/models/account.py b/models/account.py
index b4d7895..1176c8e 100644
--- a/models/account.py
+++ b/models/account.py
@@ -5,7 +5,7 @@ from odoo import fields, models
 
 
 class AccountInvoice(models.Model):
-    _inherit = "account.invoice"
+    _inherit = "account.move"
 
     # ------------------------------------------------------
     # Fields declaration
diff --git a/models/res_users.py b/models/res_users.py
index d89b863..431db16 100644
--- a/models/res_users.py
+++ b/models/res_users.py
@@ -17,7 +17,7 @@ class ResUsers(models.Model):
         string='Commandes',
         copy=False)
     invoice_second_ids = fields.Many2many(
-        comodel_name='account.invoice',
+        comodel_name='account.move',
         column1='user_id',
         column2='invoice_id',
         string='Factures',
diff --git a/models/sale.py b/models/sale.py
index ad43f33..fbf63c1 100644
--- a/models/sale.py
+++ b/models/sale.py
@@ -50,7 +50,6 @@ class SaleOrder(models.Model):
     # ------------------------------------------------------
     # Business methods
     # ------------------------------------------------------
-    @api.multi
     def _prepare_invoice(self):
         values = super(SaleOrder, self)._prepare_invoice()
         values.update({
-- 
GitLab