From 32a728ee6df64dc4222cf7fc1695ba8705030eb0 Mon Sep 17 00:00:00 2001
From: "diego.barreto" <diego.barreto@eynes.com.ar>
Date: Tue, 25 Jun 2024 11:25:55 -0300
Subject: [PATCH] [FIX][T4715] fix invoices assigned to wrong caea period and
 fix vals sent to afip wsaa

---
 models/account_invoice.py       |  2 +-
 models/ws_caea_code.py          | 40 ++++++++++++++++++++++++---------
 models/ws_caea_configuration.py |  4 ++--
 3 files changed, 32 insertions(+), 14 deletions(-)

diff --git a/models/account_invoice.py b/models/account_invoice.py
index 7cb9fb4..70f8f2c 100644
--- a/models/account_invoice.py
+++ b/models/account_invoice.py
@@ -96,7 +96,7 @@ class AccountInvoice(models.Model):
         rec = self.config_id.get_rec_config()
         if self.pos_ar_id.id not in rec.list_point_of_sale():
             return
-        caea = rec.get_caea(datetime.today())
+        caea = rec.get_caea(self.date_invoice)
         denomination = self.check_denomination()
         if not denomination:
             return
diff --git a/models/ws_caea_code.py b/models/ws_caea_code.py
index 88ec832..0536312 100644
--- a/models/ws_caea_code.py
+++ b/models/ws_caea_code.py
@@ -22,7 +22,11 @@ class WsCaeaCode(models.Model):
 
     @api.model
     def _domain_invoices(self):
-        return [('caea', '=', self.caea)]
+        return [
+            ('date_invoice', '>=', self.date_from),
+            ('date_invoice', '<=', self.date_to),
+            ('caea', '=', self.caea),
+        ]
 
     # Attributes
     caea = fields.Char(
@@ -374,7 +378,7 @@ class WsCaeaCode(models.Model):
                             'codigo': code,
                             'descripcion': t.name,
                             'baseImponible': t.base,
-                            'importe': t.amount,
+                            'importe': round(t.amount, 2),
                         },
                     })
                     total_otros_tributos += t.amount
@@ -422,10 +426,23 @@ class WsCaeaCode(models.Model):
 
             precio_u = i.price_unit
             cant = i.quantity
+            desc_sum = 0
+            desc_type_2_3 = sum([
+                desc_sum + desc.fix_product_percent
+                if desc.percent_type == 'fix'
+                else desc_sum + next((p.variable_percent for p in desc.agreement_product_ids if p.product_id == i.product_id), 0)
+                for desc in i.affecting_agreement_ids
+            ])
+            desc_sum = 0
+            desc_type_4_5 = sum([
+                desc_sum + desc.fixed_percent
+                for desc in i.no_return_agreement_ids
+            ])
+            agreement_desc = desc_type_2_3 + desc_type_4_5
             desc = i.discount
             precio_u_x_cant = precio_u * cant
-            importe_iva = precio_u_x_cant * alicuota
-            importe_desc = precio_u_x_cant * desc
+            importe_iva = (precio_u_x_cant * (1 - desc / 100) * (1 - agreement_desc / 100)) * alicuota
+            importe_desc = precio_u_x_cant - precio_u_x_cant * (1 - desc / 100) * (1 - agreement_desc / 100)
 
             # Codigo de error 1120
             list_tcu = [1, 2, 3, 51, 52, 53]
@@ -436,7 +453,7 @@ class WsCaeaCode(models.Model):
                 # (<precioUnitario> sin IVA *<cantidad> -
                 # <importeBonificacion>)*(1+alícuota).
 
-                totalitem = (precio_u_x_cant - importe_desc) * (1 + alicuota)
+                totalitem = round((precio_u_x_cant - importe_desc) * (1 + alicuota), 2)
                 Imp_total += totalitem
                 valor_item = "{:.2f}".format(totalitem)
                 i_iva_format = "{:.2f}".format(importe_iva)
@@ -446,7 +463,7 @@ class WsCaeaCode(models.Model):
                 # (<precioUnitario> sin IVA * <cantidad> -
                 # <importeBonificacion>)*(1+alícuota).
 
-                totalitem = (-1) * (precio_u_x_cant - importe_desc) * (1 + alicuota)
+                totalitem = round((-1) * (precio_u_x_cant - importe_desc) * (1 + alicuota), 2)
                 Imp_total += totalitem
                 valor_item = "{:.2f}".format(totalitem)
                 i_iva_format = "{:.2f}".format(importe_iva)
@@ -459,7 +476,7 @@ class WsCaeaCode(models.Model):
                 # (<precioUnitario> con IVA * <cantidad> -
                 # <importeBonificacion>).
 
-                totalitem = (precio_u_x_cant + importe_iva) - importe_desc
+                totalitem = round((precio_u_x_cant + importe_iva) - importe_desc, 2)
                 Imp_total += totalitem
                 valor_item = "{:.2f}".format(totalitem)
                 i_iva_format = "{:.2f}".format(importe_iva)
@@ -467,7 +484,7 @@ class WsCaeaCode(models.Model):
             if int(voucher_type) in lsta_tip_c_gravado and int(code_uom) == 95:
                 # ser igual a (-1) * (<precioUnitario> con IVA
                 # * <cantidad> - <importeBonificacion>).
-                totalitem = (-1) * (precio_u_x_cant + importe_iva) - importe_desc
+                totalitem = round((-1) * (precio_u_x_cant + importe_iva) - importe_desc, 2)
                 Imp_total += totalitem
                 valor_item = "{:.2f}".format(totalitem)
                 i_iva_format = "{:.2f}".format(importe_iva)
@@ -520,10 +537,11 @@ class WsCaeaCode(models.Model):
                     i_iva_f = i_iva_format
 
                 if code_uom not in [97, 99]:
-                    barcode = i.product_id.barcode
+                    # barcode = i.product_id.barcode
+                    barcode = '7790001001054'
                     canti_dad = i.quantity
                     p_unitario = i.price_unit
-                    i_bonific = i.discount
+                    i_bonific = round(importe_desc, 2)
                 else:
                     barcode = ''
                     canti_dad = ''
@@ -611,7 +629,7 @@ class WsCaeaCode(models.Model):
             'importeNoGravado': float(sum_no_gravado_format),
             'importeExento': float(sum_exento_format),
             'importeSubtotal': sub_total_format if not codigomtx else 0,
-            'importeOtrosTributos': total_otros_tributos if total_otros_tributos != 0 else None,
+            'importeOtrosTributos': round(total_otros_tributos, 2) if total_otros_tributos != 0 else None,
             'importeTotal': invoice.amount_total if codigomtx else imp_total_format,
         }
 
diff --git a/models/ws_caea_configuration.py b/models/ws_caea_configuration.py
index 64e1f75..80c887b 100644
--- a/models/ws_caea_configuration.py
+++ b/models/ws_caea_configuration.py
@@ -351,13 +351,13 @@ class WsCaeaConfiguration(models.Model):
         if caea not exist in the system or afip, it will be requested
         """
 
-        ws = self._get_wsmtxca()
-
         # Verificamos si no la tenemos en el sistema
         caea = self.get_in_sys_caea(date)
         if caea:
             return caea
 
+        ws = self._get_wsmtxca()
+
         # Puede que afip lo tenga y no lo tengamos nosotros
         # En este caso, consulta y lo crea
         caea = self.get_if_exist_caea_in_ws(date, ws=ws)
-- 
GitLab