account_invoice.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # -*- encoding: utf-8 -*-
  2. ###############################################################################
  3. #
  4. # Odoo, Open Source Management Solution
  5. # This module copyright (C) 2010 - 2014 Savoir-faire Linux
  6. # (<http://www.savoirfairelinux.com>).
  7. # Copyright (C) 2015 Akretion (http://www.akretion.com)
  8. # @author Alexis de Lattre <alexis.delattre@akretion.com>
  9. #
  10. # This program is free software: you can redistribute it and/or modify
  11. # it under the terms of the GNU Affero General Public License as
  12. # published by the Free Software Foundation, either version 3 of the
  13. # License, or (at your option) any later version.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU Affero General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU Affero General Public License
  21. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. #
  23. ###############################################################################
  24. from openerp import models, fields, api, _
  25. from openerp.exceptions import ValidationError
  26. class AccountInvoice(models.Model):
  27. _inherit = "account.invoice"
  28. supplier_invoice_number = fields.Char(copy=False)
  29. @api.one
  30. @api.constrains('supplier_invoice_number')
  31. def _check_unique_supplier_invoice_number_insensitive(self):
  32. if (
  33. self.supplier_invoice_number and
  34. self.type in ('in_invoice', 'in_refund')):
  35. same_supplier_inv_num = self.search([
  36. ('commercial_partner_id', '=', self.commercial_partner_id.id),
  37. ('type', 'in', ('in_invoice', 'in_refund')),
  38. ('supplier_invoice_number',
  39. '=ilike',
  40. self.supplier_invoice_number),
  41. ('id', '!=', self.id),
  42. ])
  43. if same_supplier_inv_num:
  44. raise ValidationError(
  45. _("The invoice/refund with supplier invoice number '%s' "
  46. "already exists in Odoo under the number '%s' "
  47. "for supplier '%s'.") % (
  48. same_supplier_inv_num[0].supplier_invoice_number,
  49. same_supplier_inv_num[0].number or '-',
  50. same_supplier_inv_num[0].partner_id.display_name))