From 875421603cc816b7ab338576a43d48ac57aeadfc Mon Sep 17 00:00:00 2001
From: "Matheus Leal Viana (malv)" <malv@odoo.com>
Date: Mon, 8 May 2023 14:30:38 +0000
Subject: [PATCH] [FIX] sale_stock: Correct incoterm when generating
 downpayment invoices

When creating an downpayment invoice for a SO with a incoterm different than the default one, this invoices ignores the incoterm you defined and uses the default value.

This issue is happening because in the _prepare_invoice_values is not passing the value of correct value of the incoterm and the default value is being used.

How to reproduce:
1. Active "incoterms" and choose a default incoterms in settings
2. Create a SO with a incoterm different than the default one
3. Create a downpayment invoice
4. In the new created invoice the incoterm is set as the default incoterm instead of the previously defined

closes odoo/odoo#120823

Opw: 3284556
Signed-off-by: Adrien Widart (awt) <awt@odoo.com>
---
 addons/sale_stock/tests/test_sale_stock.py    | 38 +++++++++++++++++++
 addons/sale_stock/wizard/__init__.py          |  1 +
 .../wizard/sale_make_invoice_advance.py       | 13 +++++++
 3 files changed, 52 insertions(+)
 create mode 100644 addons/sale_stock/wizard/sale_make_invoice_advance.py

diff --git a/addons/sale_stock/tests/test_sale_stock.py b/addons/sale_stock/tests/test_sale_stock.py
index 7275d028b3e0..eebfd9912569 100644
--- a/addons/sale_stock/tests/test_sale_stock.py
+++ b/addons/sale_stock/tests/test_sale_stock.py
@@ -1417,6 +1417,44 @@ class TestSaleStock(TestSaleCommon, ValuationReconciliationTestCommon):
         self.assertEqual(ship01.move_lines.move_orig_ids, (pick01 | pick02).move_lines)
         self.assertEqual(ship02.move_lines.move_orig_ids, (pick01 | pick02).move_lines)
 
+    def test_incoterm_in_advance_payment(self):
+        """When generating a advance payment invoice from a SO, this invoice incoterm should be the same as the SO"""
+
+        incoterm = self.env['account.incoterms'].create({
+            'name': 'Test Incoterm',
+            'code': 'TEST',
+        })
+
+        so = self.env['sale.order'].create({
+            'partner_id': self.partner_a.id,
+            'incoterm': incoterm.id,
+            'order_line': [(0, 0, {
+                'name': self.product_a.name,
+                'product_id': self.product_a.id,
+                'product_uom_qty': 10,
+                'product_uom': self.product_a.uom_id.id,
+                'price_unit': 1,
+            })],
+        })
+        so.action_confirm()
+
+        advance_product = self.env['product.product'].create({
+            'name': 'Deposit',
+            'type': 'service',
+            'invoice_policy': 'order',
+        })
+
+        adv_wiz = self.env['sale.advance.payment.inv'].with_context(active_ids=[so.id]).create({
+            'advance_payment_method': 'percentage',
+            'amount': 5.0,
+            'product_id': advance_product.id,
+        })
+
+        act = adv_wiz.with_context(open_invoices=True).create_invoices()
+        invoice = self.env['account.move'].browse(act['res_id'])
+
+        self.assertEqual(invoice.invoice_incoterm_id.id, incoterm.id)
+
     def test_exception_delivery_partial_multi(self):
         """
         When a backorder is cancelled for a picking in multi-picking,
diff --git a/addons/sale_stock/wizard/__init__.py b/addons/sale_stock/wizard/__init__.py
index 8cacb400495f..3eb0ebaf0054 100644
--- a/addons/sale_stock/wizard/__init__.py
+++ b/addons/sale_stock/wizard/__init__.py
@@ -3,3 +3,4 @@
 
 from . import stock_rules_report
 from . import sale_order_cancel
+from . import sale_make_invoice_advance
diff --git a/addons/sale_stock/wizard/sale_make_invoice_advance.py b/addons/sale_stock/wizard/sale_make_invoice_advance.py
new file mode 100644
index 000000000000..e18cd02e1565
--- /dev/null
+++ b/addons/sale_stock/wizard/sale_make_invoice_advance.py
@@ -0,0 +1,13 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from odoo import models
+
+
+class SaleAdvancePaymentInv(models.TransientModel):
+    _inherit = "sale.advance.payment.inv"
+
+    def _prepare_invoice_values(self, order, name, amount, so_line):
+        invoice_vals = super()._prepare_invoice_values(order, name, amount, so_line)
+        invoice_vals['invoice_incoterm_id'] = order.incoterm.id
+        return invoice_vals
-- 
GitLab