Skip to content

Commit

Permalink
[IMP] oxigen_account: Do not allow to repeat the reference on an acco…
Browse files Browse the repository at this point in the history
…unt move
  • Loading branch information
etobella authored and eantones committed Sep 22, 2023
1 parent 88eb1e0 commit 2a84c5b
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
32 changes: 32 additions & 0 deletions oxigen_account/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json

from odoo import _, api, models
from odoo.exceptions import ValidationError
from odoo.tools import date_utils


Expand All @@ -15,6 +16,37 @@ def _onchange_ref(self):
move.payment_reference = move.ref
move._onchange_payment_reference()

@api.constrains("ref")
def _check_move_supplier_ref(self):
"""
Check if an other vendor bill has the same ref
and the same commercial_partner_id than the current instance
"""
for rec in self:
if rec.ref and rec.is_purchase_document(include_receipts=True):
same_supplier_inv_num = rec.search(
[
("commercial_partner_id", "=", rec.commercial_partner_id.id),
("move_type", "in", ("in_invoice", "in_refund")),
("ref", "=ilike", rec.ref),
("id", "!=", rec.id),
],
limit=1,
)
if same_supplier_inv_num:
raise ValidationError(
_(
"The invoice/refund with supplier invoice number '%s' "
"already exists in Odoo under the number '%s' "
"for supplier '%s'."
)
% (
same_supplier_inv_num.ref,
same_supplier_inv_num.name or "-",
same_supplier_inv_num.partner_id.display_name,
)
)

@api.depends("move_type", "line_ids.amount_residual")
def _compute_payments_widget_reconciled_info(self):
res = super()._compute_payments_widget_reconciled_info()
Expand Down
1 change: 1 addition & 0 deletions oxigen_account/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_supplier_invoice
61 changes: 61 additions & 0 deletions oxigen_account/tests/test_supplier_invoice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from odoo import fields
from odoo.exceptions import ValidationError
from odoo.tests import tagged

from odoo.addons.account.tests.common import AccountTestInvoicingCommon


@tagged("post_install", "-at_install")
class TestSupplierInvoice(AccountTestInvoicingCommon):
@classmethod
def setUpClass(cls, chart_template_ref=None):
super().setUpClass(chart_template_ref=chart_template_ref)

# ENVIRONMENTS
cls.account_account = cls.env["account.account"]
cls.account_move = cls.env["account.move"].with_context(
{"tracking_disable": True}
)

# INSTANCES
cls.partner = cls.env.ref("base.res_partner_2")
# Account for invoice
cls.account = cls.account_account.search(
[
(
"user_type_id",
"=",
cls.env.ref("account.data_account_type_receivable").id,
)
],
limit=1,
)
# Invoice with unique reference 'ABC123'
cls.invoice = cls.account_move.create(
{
"partner_id": cls.partner.id,
"invoice_date": fields.Date.today(),
"move_type": "in_invoice",
"ref": "ABC123",
"invoice_line_ids": [(0, 0, {"partner_id": cls.partner.id})],
}
)

def test_check_unique_supplier_invoice_number_insensitive(self):
# A new invoice instance with an existing supplier_invoice_number
with self.assertRaises(ValidationError):
self.account_move.create(
{
"partner_id": self.partner.id,
"move_type": "in_invoice",
"ref": "ABC123",
}
)
# A new invoice instance with a new supplier_invoice_number
self.account_move.create(
{
"partner_id": self.partner.id,
"move_type": "in_invoice",
"ref": "ABC123bis",
}
)

0 comments on commit 2a84c5b

Please sign in to comment.