Skip to content

Commit

Permalink
[13.0][MIG] account_invoice_supplier_ref_unique
Browse files Browse the repository at this point in the history
  • Loading branch information
astirpe committed Jan 9, 2020
1 parent 1783e5f commit 81f88c7
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 133 deletions.
10 changes: 5 additions & 5 deletions account_invoice_supplier_ref_unique/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ Unique Supplier Invoice Number in Invoice
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Faccount--invoicing-lightgray.png?logo=github
:target: https://github.com/OCA/account-invoicing/tree/12.0/account_invoice_supplier_ref_unique
:target: https://github.com/OCA/account-invoicing/tree/13.0/account_invoice_supplier_ref_unique
:alt: OCA/account-invoicing
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/account-invoicing-12-0/account-invoicing-12-0-account_invoice_supplier_ref_unique
:target: https://translation.odoo-community.org/projects/account-invoicing-13-0/account-invoicing-13-0-account_invoice_supplier_ref_unique
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png
:target: https://runbot.odoo-community.org/runbot/95/12.0
:target: https://runbot.odoo-community.org/runbot/95/13.0
:alt: Try me on Runbot

|badge1| |badge2| |badge3| |badge4| |badge5|
Expand All @@ -40,7 +40,7 @@ Bug Tracker
Bugs are tracked on `GitHub Issues <https://github.com/OCA/account-invoicing/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed and welcomed
`feedback <https://github.com/OCA/account-invoicing/issues/new?body=module:%20account_invoice_supplier_ref_unique%0Aversion:%2012.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
`feedback <https://github.com/OCA/account-invoicing/issues/new?body=module:%20account_invoice_supplier_ref_unique%0Aversion:%2013.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Expand Down Expand Up @@ -74,6 +74,6 @@ OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

This module is part of the `OCA/account-invoicing <https://github.com/OCA/account-invoicing/tree/12.0/account_invoice_supplier_ref_unique>`_ project on GitHub.
This module is part of the `OCA/account-invoicing <https://github.com/OCA/account-invoicing/tree/13.0/account_invoice_supplier_ref_unique>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
23 changes: 10 additions & 13 deletions account_invoice_supplier_ref_unique/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
'name': 'Unique Supplier Invoice Number in Invoice',
'version': '12.0.1.0.0',
'summary': 'Checks that supplier invoices are not entered twice',
'author':
"Savoir-faire Linux, Acsone SA/NV, Odoo Community Association (OCA)",
'maintainer': 'Savoir-faire Linux',
'website': "https://github.com/OCA/account-invoicing",
'license': 'AGPL-3',
'category': 'Accounting & Finance',
'depends': [
'account'
],
'data': ['views/account_invoice.xml'],
"name": "Unique Supplier Invoice Number in Invoice",
"version": "13.0.1.0.0",
"summary": "Checks that supplier invoices are not entered twice",
"author": "Savoir-faire Linux, Acsone SA/NV, Odoo Community Association (OCA)",
"maintainer": "Savoir-faire Linux",
"website": "https://github.com/OCA/account-invoicing",
"license": "AGPL-3",
"category": "Accounting & Finance",
"depends": ["account"],
"data": ["views/account_move.xml"],
}
2 changes: 1 addition & 1 deletion account_invoice_supplier_ref_unique/models/__init__.py
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 account_invoice_supplier_ref_unique/models/account_invoice.py

This file was deleted.

81 changes: 81 additions & 0 deletions account_invoice_supplier_ref_unique/models/account_move.py
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)
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
* Mathieu Benoit <mathieu.benoit@savoirfairelinux.com>
* Alexis de Lattre <alexis.delattre@akretion.com>
* Thomas Binsfeld <thomas.binsfeld@acsone.eu>
* Andrea Stirpe <a.stirpe@onestein.nl>
Binary file not shown.
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 account_invoice_supplier_ref_unique/views/account_invoice.xml

This file was deleted.

14 changes: 14 additions & 0 deletions account_invoice_supplier_ref_unique/views/account_move.xml
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>

0 comments on commit 81f88c7

Please sign in to comment.