forked from OCA/product-attribute
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[13.0][MIG] account_invoice_supplier_ref_unique
- Loading branch information
Showing
10 changed files
with
183 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
from . import account_invoice | ||
from . import account_move |
71 changes: 0 additions & 71 deletions
71
account_invoice_supplier_ref_unique/models/account_invoice.py
This file was deleted.
Oops, something went wrong.
81 changes: 81 additions & 0 deletions
81
account_invoice_supplier_ref_unique/models/account_move.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# Copyright 2016 Acsone | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). | ||
|
||
from odoo import _, api, fields, models | ||
from odoo.exceptions import ValidationError | ||
|
||
|
||
class AccountMove(models.Model): | ||
_inherit = "account.move" | ||
|
||
supplier_invoice_number = fields.Char( | ||
string="Vendor invoice number", | ||
readonly=True, | ||
states={"draft": [("readonly", False)]}, | ||
copy=False, | ||
) | ||
|
||
@api.constrains("supplier_invoice_number") | ||
def _check_unique_supplier_invoice_number_insensitive(self): | ||
""" | ||
Check if an other vendor bill has the same supplier_invoice_number | ||
and the same commercial_partner_id than the current instance | ||
""" | ||
for rec in self: | ||
if rec.supplier_invoice_number and rec.is_purchase_document( | ||
include_receipts=True | ||
): | ||
same_supplier_inv_num = rec.search( | ||
[ | ||
("commercial_partner_id", "=", rec.commercial_partner_id.id), | ||
("type", "in", ("in_invoice", "in_refund")), | ||
( | ||
"supplier_invoice_number", | ||
"=ilike", | ||
rec.supplier_invoice_number, | ||
), | ||
("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.supplier_invoice_number, | ||
same_supplier_inv_num.name or "-", | ||
same_supplier_inv_num.partner_id.display_name, | ||
) | ||
) | ||
|
||
@api.onchange("supplier_invoice_number") | ||
def _onchange_supplier_invoice_number(self): | ||
if not self.ref: | ||
self.ref = self.supplier_invoice_number | ||
|
||
def _reverse_moves(self, default_values_list=None, cancel=False): | ||
# OVERRIDE | ||
if not default_values_list: | ||
default_values_list = [{} for move in self] | ||
for move, default_values in zip(self, default_values_list): | ||
if ( | ||
move | ||
and move.is_purchase_document(include_receipts=True) | ||
and default_values.get("ref") | ||
): | ||
default_values.update({"ref": ""}) | ||
return super()._reverse_moves( | ||
default_values_list=default_values_list, cancel=cancel | ||
) | ||
|
||
def copy(self, default=None): | ||
""" | ||
The unique vendor invoice number is not copied in vendor bills | ||
""" | ||
if self.is_purchase_document(include_receipts=True): | ||
default = dict(default or {}, ref="") | ||
return super().copy(default) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
100 changes: 71 additions & 29 deletions
100
account_invoice_supplier_ref_unique/tests/test_account_invoice_supplier_ref_unique.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,95 @@ | ||
# Copyright 2016 Acsone | ||
# Copyright 2020 Onestein (<https://www.onestein.eu>) | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). | ||
|
||
from odoo import fields | ||
from odoo.exceptions import ValidationError | ||
from odoo.tests.common import SavepointCase | ||
|
||
from odoo.addons.account.tests.invoice_test_common import InvoiceTestCommon | ||
|
||
class TestAccountInvoiceSupplierRefUnique(SavepointCase): | ||
|
||
class TestAccountInvoiceSupplierRefUnique(InvoiceTestCommon): | ||
@classmethod | ||
def setUpClass(cls): | ||
super(TestAccountInvoiceSupplierRefUnique, cls).setUpClass() | ||
super().setUpClass() | ||
|
||
# ENVIRONMENTS | ||
cls.account_account = cls.env['account.account'] | ||
cls.account_invoice = cls.env['account.invoice'].with_context( | ||
{'tracking_disable': True}) | ||
cls.account_account = cls.env["account.account"] | ||
cls.account_invoice = cls.env["account.move"].with_context( | ||
{"tracking_disable": True} | ||
) | ||
|
||
# INSTANCES | ||
cls.partner = cls.env.ref('base.res_partner_2') | ||
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) | ||
[ | ||
( | ||
"user_type_id", | ||
"=", | ||
cls.env.ref("account.data_account_type_receivable").id, | ||
) | ||
], | ||
limit=1, | ||
) | ||
# Invoice with unique reference 'ABC123' | ||
cls.invoice = cls.account_invoice.create({ | ||
'partner_id': cls.partner.id, | ||
'account_id': cls.account.id, | ||
'type': 'in_invoice', | ||
'supplier_invoice_number': 'ABC123'}) | ||
cls.invoice = cls.account_invoice.create( | ||
{ | ||
"partner_id": cls.partner.id, | ||
"type": "in_invoice", | ||
"supplier_invoice_number": "ABC123", | ||
} | ||
) | ||
|
||
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_invoice.create({ | ||
'partner_id': self.partner.id, | ||
'account_id': self.account.id, | ||
'type': 'in_invoice', | ||
'supplier_invoice_number': 'ABC123'}) | ||
self.account_invoice.create( | ||
{ | ||
"partner_id": self.partner.id, | ||
"type": "in_invoice", | ||
"supplier_invoice_number": "ABC123", | ||
} | ||
) | ||
# A new invoice instance with a new supplier_invoice_number | ||
self.account_invoice.create({ | ||
'partner_id': self.partner.id, | ||
'account_id': self.account.id, | ||
'type': 'in_invoice', | ||
'supplier_invoice_number': 'ABC123bis'}) | ||
self.account_invoice.create( | ||
{ | ||
"partner_id": self.partner.id, | ||
"type": "in_invoice", | ||
"supplier_invoice_number": "ABC123bis", | ||
} | ||
) | ||
|
||
def test_onchange_supplier_invoice_number(self): | ||
self.invoice._onchange_supplier_invoice_number() | ||
self.assertEqual(self.invoice.reference, | ||
self.invoice.supplier_invoice_number, | ||
"_onchange_supplier_invoice_number") | ||
self.assertEqual( | ||
self.invoice.ref, | ||
self.invoice.supplier_invoice_number, | ||
"_onchange_supplier_invoice_number", | ||
) | ||
|
||
def test_copy_invoice(self): | ||
invoice2 = self.invoice.copy() | ||
self.assertNotEqual(self.invoice.ref, "") | ||
self.assertEqual(invoice2.ref, "") | ||
|
||
def test_reverse_invoice(self): | ||
move_reversal = ( | ||
self.env["account.move.reversal"] | ||
.with_context(active_model="account.move", active_ids=self.invoice.ids) | ||
.create( | ||
{ | ||
"date": fields.Date.today(), | ||
"reason": "no reason", | ||
"refund_method": "refund", | ||
} | ||
) | ||
) | ||
reversal = move_reversal.reverse_moves() | ||
refund = self.env["account.move"].browse(reversal["res_id"]) | ||
self.assertNotEqual(self.invoice.ref, "") | ||
self.assertEqual(refund.ref, "") | ||
|
||
def test_reverse_moves_robustness(self): | ||
res = self.invoice._reverse_moves() | ||
self.assertTrue(res.is_purchase_document(include_receipts=True)) |
14 changes: 0 additions & 14 deletions
14
account_invoice_supplier_ref_unique/views/account_invoice.xml
This file was deleted.
Oops, something went wrong.
14 changes: 14 additions & 0 deletions
14
account_invoice_supplier_ref_unique/views/account_move.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<odoo> | ||
|
||
<record id="view_move_form" model="ir.ui.view"> | ||
<field name="model">account.move</field> | ||
<field name="inherit_id" ref="account.view_move_form"/> | ||
<field name="arch" type="xml"> | ||
<field name="ref" position="before"> | ||
<field name="supplier_invoice_number" attrs="{'invisible': [('type', 'in', ['out_invoice', 'out_refund'])]}"/> | ||
</field> | ||
</field> | ||
</record> | ||
|
||
</odoo> |