From 6c74f8eec8dbc3d22958ae00c190e8dec8d7fe85 Mon Sep 17 00:00:00 2001
From: "diego.barreto" <diego.barreto@eynes.com.ar>
Date: Fri, 28 Jun 2024 14:58:50 -0300
Subject: [PATCH] [FIX][T4778] remove discounts from items products to inform
 to afip, add new items for commercial and agreement discounts in items to
 inform

---
 models/ws_caea_code.py | 42 +++++++++++++++++++++++++++++++++++++-----
 1 file changed, 37 insertions(+), 5 deletions(-)

diff --git a/models/ws_caea_code.py b/models/ws_caea_code.py
index 0536312..3c10131 100644
--- a/models/ws_caea_code.py
+++ b/models/ws_caea_code.py
@@ -439,10 +439,10 @@ class WsCaeaCode(models.Model):
                 for desc in i.no_return_agreement_ids
             ])
             agreement_desc = desc_type_2_3 + desc_type_4_5
-            desc = i.discount
+            desc = 0.00
             precio_u_x_cant = precio_u * cant
-            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)
+            importe_iva = (precio_u_x_cant * 1 - i.discount / 100) * alicuota
+            importe_desc = precio_u_x_cant * (i.discount / 100)
 
             # Codigo de error 1120
             list_tcu = [1, 2, 3, 51, 52, 53]
@@ -540,8 +540,8 @@ class WsCaeaCode(models.Model):
                     # barcode = i.product_id.barcode
                     barcode = '7790001001054'
                     canti_dad = i.quantity
-                    p_unitario = i.price_unit
-                    i_bonific = round(importe_desc, 2)
+                    p_unitario = round(i.discount_price_unit, 2)
+                    i_bonific = 0.00
                 else:
                     barcode = ''
                     canti_dad = ''
@@ -562,6 +562,38 @@ class WsCaeaCode(models.Model):
                     'importeItem': valor_item,
                 },
             })
+
+        env_tax_caea = self.env['ws.tax.caea']
+        tax = invoice.tax_line_ids.filtered(lambda x: x.tax_id.tax_group == 'vat')
+        code = env_tax_caea.get_cod(tax.tax_id)
+        commercial_bonus_amount = invoice.commercial_bonus_amount
+        tax_percentage = tax.percentage
+        agreement_descts_amount = invoice.no_return_agreement_picking + invoice.no_return_agreement_returns + invoice.trade_agreement_bonus + invoice.trade_agreement_discount
+
+        if commercial_bonus_amount > 0:
+            commercial_bonus_discounts = {
+                'item': {
+                    'descripcion': 'Bonificacion comercial',
+                    'codigoUnidadMedida': 99,
+                    'codigoCondicionIVA': code,
+                    'importeIVA': round(commercial_bonus_amount * (tax_percentage / 100), 2),
+                    'importeItem': round(commercial_bonus_amount * (1 + tax_percentage / 100), 2),
+                }
+            }
+            list_items.append(commercial_bonus_discounts)
+
+        if agreement_descts_amount > 0:
+            agreement_descts = {
+                'item': {
+                    'descripcion': 'Descuento negociacion',
+                    'codigoUnidadMedida': 99,
+                    'codigoCondicionIVA': code,
+                    'importeIVA': round(agreement_descts_amount * (tax_percentage / 100), 2),
+                    'importeItem': round(agreement_descts_amount * (1 + tax_percentage / 100), 2),
+                }
+            }
+            list_items.append(agreement_descts)
+        
         if list_items:
             lista_de_items = {
                 'arrayItems': {
-- 
GitLab