Skip to content

Commit

Permalink
add sale_order_default_payment_term
Browse files Browse the repository at this point in the history
  • Loading branch information
jans23 committed Mar 6, 2025
1 parent 1ea106f commit 3bb5e6b
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 0 deletions.
30 changes: 30 additions & 0 deletions sale_order_default_payment_term/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
===============================
Sale Order Default Payment Term
===============================

This module sets the first payment term as default when creating a new sales order.

Features
========

* Automatically sets the first payment term (ordered by ID) as the default payment term for new sales orders
* Overrides the payment_term_id field in the sale.order model to provide this default value

Configuration
=============

No special configuration is needed. After installing the module, the first payment term in the system will be automatically set as default for new sales orders. You can change the order of payment terms as usual.

Usage
=====

* When creating a new sales order, the payment term will be automatically set to the first payment term in the system
* Users can still manually change the payment term if needed

Testing
=======

The module includes unit tests to verify:

* The default payment term is correctly set when creating a new sales order
* Manually selected payment terms are respected
1 change: 1 addition & 0 deletions sale_order_default_payment_term/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models, tests
14 changes: 14 additions & 0 deletions sale_order_default_payment_term/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "Sale Order Default Payment Term",
"version": "15.0.1.0.0",
"category": "Sales",
"summary": "Set the first payment term as default when creating a new sales order",
"author": "Nitrokey GmbH",
"website": "https://www.nitrokey.com",
"license": "AGPL-3",
"depends": ["sale", "account"],
"data": [],
"installable": True,
"auto_install": False,
"application": False,
}
1 change: 1 addition & 0 deletions sale_order_default_payment_term/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import sale
28 changes: 28 additions & 0 deletions sale_order_default_payment_term/models/sale.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from odoo import api, models


class SaleOrder(models.Model):
_inherit = "sale.order"

@api.model
def _default_payment_term_id(self):
"""Set the first payment term as default when creating a new sales order."""
# Get the first payment term from the system using default ordering (sequence, id)
first_payment_term = self.env["account.payment.term"].search([], limit=1)
return first_payment_term

@api.model
def create(self, vals):
"""Override create to set default payment term if needed."""
# If partner_id is provided but payment_term_id is not
if vals.get("partner_id") and not vals.get("payment_term_id"):
# Check if partner has a payment term
partner = self.env["res.partner"].browse(vals["partner_id"])
if partner.property_payment_term_id:
# Use partner's payment term
vals["payment_term_id"] = partner.property_payment_term_id.id
else:
# Use our default
vals["payment_term_id"] = self._default_payment_term_id().id

return super(SaleOrder, self).create(vals)
1 change: 1 addition & 0 deletions sale_order_default_payment_term/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_default_payment_term
127 changes: 127 additions & 0 deletions sale_order_default_payment_term/tests/test_default_payment_term.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
from odoo.tests.common import TransactionCase


class TestDefaultPaymentTerm(TransactionCase):
"""Test the default payment term functionality."""

def setUp(self):
super(TestDefaultPaymentTerm, self).setUp()
# Create test data
self.payment_term_1 = self.env["account.payment.term"].create(
{
"name": "Test Payment Term 1",
"line_ids": [
(
0,
0,
{
"value": "balance",
"days": 0,
},
)
],
}
)
self.payment_term_2 = self.env["account.payment.term"].create(
{
"name": "Test Payment Term 2",
"line_ids": [
(
0,
0,
{
"value": "balance",
"days": 30,
},
)
],
}
)
self.partner = self.env["res.partner"].create(
{
"name": "Test Partner",
}
)

def test_default_payment_term(self):
"""Test that the first payment term is set as default when creating a new sales order."""
# Get the first payment term from the system using default ordering (sequence, id)
first_payment_term = self.env["account.payment.term"].search([], limit=1)

# Make sure we have the actual ID for comparison
self.assertTrue(
first_payment_term.exists(),
"At least one payment term must exist in the system",
)

# Create a new sales order
sale_order = self.env["sale.order"].create(
{
"partner_id": self.partner.id,
}
)

# Check that the payment term is set to the first payment term
self.assertEqual(
sale_order.payment_term_id.id,
first_payment_term.id,
"The default payment term should be the first payment term in the system",
)

def test_manual_payment_term_selection(self):
"""Test that a manually selected payment term is respected."""
# Create a new sales order with a specific payment term
sale_order = self.env["sale.order"].create(
{
"partner_id": self.partner.id,
"payment_term_id": self.payment_term_2.id,
}
)

# Check that the payment term is set to the specified payment term
self.assertEqual(
sale_order.payment_term_id,
self.payment_term_2,
"The payment term should be the one specified during creation",
)

def test_partner_payment_term(self):
"""Test that the partner's payment term is used when available."""
# Set a payment term on the partner
self.partner.property_payment_term_id = self.payment_term_2

# Verify the partner's payment term is set correctly
self.assertEqual(
self.partner.property_payment_term_id.id,
self.payment_term_2.id,
"Partner's payment term should be set correctly",
)

# Create a new sales order with this partner
sale_order = self.env["sale.order"].create(
{
"partner_id": self.partner.id,
}
)

# Debug: Log payment term IDs
import logging

_logger = logging.getLogger(__name__)
_logger.info(
"Partner payment term ID: %s", self.partner.property_payment_term_id.id
)
_logger.info("Sale order payment term ID: %s", sale_order.payment_term_id.id)
_logger.info("Test payment term 2 ID: %s", self.payment_term_2.id)

# Our module sets the default payment term, but the standard Odoo behavior
# should prioritize the partner's payment term if it exists.
# This test verifies that the standard behavior is preserved.
self.assertEqual(
sale_order.payment_term_id.id,
self.payment_term_2.id,
"The payment term should be the one from the partner's property_payment_term_id",
)

# Clean up: Unset the payment term on the partner to avoid influencing other tests
self.partner.property_payment_term_id = False

0 comments on commit 3bb5e6b

Please sign in to comment.