From 78393b609f16816ce1203d8ce4f1e493d2f1da3b Mon Sep 17 00:00:00 2001
From: "noe.izquierdo" <noe.izquierdo@openpyme.mx>
Date: Wed, 11 Oct 2023 16:45:20 -0600
Subject: [PATCH] =?UTF-8?q?fix(account.invoice):=20se=20corrige=20migraci?=
 =?UTF-8?q?=C3=B3n=20para=20soporta=20el=20cambio=20del=20campo=20cfdi=5Ff?=
 =?UTF-8?q?iscal=5Fregime=5Fid?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

se corrige migración y validación para soportar la eliminación del campo cfdi_fiscal_regime_id, ya
que el campo no se mostrará en facturas si no se toma directo de la ficha del cliente.
---
 .../migrations/3.5.0/post-migration.py        | 82 ++++++++++---------
 l10n_mx_facturae/models/account_invoice.py    |  3 -
 2 files changed, 42 insertions(+), 43 deletions(-)

diff --git a/l10n_mx_facturae/migrations/3.5.0/post-migration.py b/l10n_mx_facturae/migrations/3.5.0/post-migration.py
index 444b5f4c66..6579f6e722 100644
--- a/l10n_mx_facturae/migrations/3.5.0/post-migration.py
+++ b/l10n_mx_facturae/migrations/3.5.0/post-migration.py
@@ -7,48 +7,50 @@ from openerp import SUPERUSER_ID
 
 @tools.migrate(use_env=True, uid=SUPERUSER_ID)
 def migrate(env, installed_version):
-    tools.logged_query(
-        env.cr,
-        """
-        WITH account_regimen_code as (
-            SELECT
-                ai.id AS invoice_id,
-                fr.id AS fiscal_regime_id
-            FROM account_invoice as ai
-            LEFT JOIN account_fiscal_position as afp ON afp.id = ai.fiscal_position
-            LEFT JOIN cfdi_fiscal_regime AS fr ON afp.code = fr.code
-            WHERE afp.code in (
-                '601','603','605','606','607','608','609','610','611','612','614',
-                '615','616','620','621','622','623','624','628','629','630'
+    legacy_name = tools.get_legacy_name("cfdi_fiscal_regime_id")
+    if tools.column_exists(env.cr, "account_invoice", legacy_name):
+        tools.logged_query(
+            env.cr,
+            """
+            WITH account_regimen_code as (
+                SELECT
+                    ai.id AS invoice_id,
+                    fr.id AS fiscal_regime_id
+                FROM account_invoice as ai
+                LEFT JOIN account_fiscal_position as afp ON afp.id = ai.fiscal_position
+                LEFT JOIN cfdi_fiscal_regime AS fr ON afp.code = fr.code
+                WHERE afp.code in (
+                    '601','603','605','606','607','608','609','610','611','612','614',
+                    '615','616','620','621','622','623','624','628','629','630'
+                )
             )
-        )
 
-        UPDATE account_invoice AS ai
-        SET cfdi_fiscal_regime_id=fr.fiscal_regime_id
-        FROM account_regimen_code AS fr
-        WHERE ai.id=fr.invoice_id
-        """,
-    )
+            UPDATE account_invoice AS ai
+            SET cfdi_fiscal_regime_id=fr.fiscal_regime_id
+            FROM account_regimen_code AS fr
+            WHERE ai.id=fr.invoice_id
+            """,
+        )
 
-    tools.logged_query(
-        env.cr,
-        """
-        WITH account_regimen_code as (
-            SELECT
-                ai.id AS invoice_id,
-                fr.id AS fiscal_regime_id
-            FROM account_invoice as ai
-            LEFT JOIN account_fiscal_position as afp ON afp.id = ai.fiscal_position
-            LEFT JOIN cfdi_fiscal_regime AS fr ON afp.code = fr.code
-            WHERE afp.code in (
-                '601','603','605','606','607','608','609','610','611','612','614',
-                '615','616','620','621','622','623','624','628','629','630'
+        tools.logged_query(
+            env.cr,
+            """
+            WITH account_regimen_code as (
+                SELECT
+                    ai.id AS invoice_id,
+                    fr.id AS fiscal_regime_id
+                FROM account_invoice as ai
+                LEFT JOIN account_fiscal_position as afp ON afp.id = ai.fiscal_position
+                LEFT JOIN cfdi_fiscal_regime AS fr ON afp.code = fr.code
+                WHERE afp.code in (
+                    '601','603','605','606','607','608','609','610','611','612','614',
+                    '615','616','620','621','622','623','624','628','629','630'
+                )
             )
-        )
 
-        UPDATE account_invoice AS ai
-        SET fiscal_position=Null
-        FROM account_regimen_code AS fr
-        WHERE ai.id=fr.invoice_id
-        """,
-    )
+            UPDATE account_invoice AS ai
+            SET fiscal_position=Null
+            FROM account_regimen_code AS fr
+            WHERE ai.id=fr.invoice_id
+            """,
+        )
diff --git a/l10n_mx_facturae/models/account_invoice.py b/l10n_mx_facturae/models/account_invoice.py
index 294f08df6a..57faf12cd2 100644
--- a/l10n_mx_facturae/models/account_invoice.py
+++ b/l10n_mx_facturae/models/account_invoice.py
@@ -274,8 +274,6 @@ class AccountInvoice(models.Model):
         for invoice in self.filtered(lambda i: i.type in ("out_invoice", "out_refund")):
             partner = invoice.partner_id
             if partner:
-                if partner.cfdi_fiscal_regime_id.id:
-                    invoice.cfdi_fiscal_regime_id = partner.cfdi_fiscal_regime_id.id
                 if partner.cfdi_use.id:
                     invoice.cfdi_use = partner.cfdi_use.id
                 invoice.payment_method_id = partner.payment_method_id.id
@@ -402,7 +400,6 @@ class AccountInvoice(models.Model):
                 [
                     "cfdi_use",
                     "partner_id",
-                    "cfdi_fiscal_regime_id",
                     "payment_type_ids",
                     "address_issued_id",
                     "journal_id",
-- 
GitLab