2012-08-27 12:35:30 +02:00
|
|
|
#This file is part account_invoice_consecutive module for Tryton.
|
|
|
|
#The COPYRIGHT file at the top level of this repository contains
|
|
|
|
#the full copyright notices and license terms.
|
2012-08-27 12:19:36 +02:00
|
|
|
from trytond.model import Workflow, ModelView, ModelSQL
|
|
|
|
from trytond.transaction import Transaction
|
2012-10-17 11:18:43 +02:00
|
|
|
from trytond.pool import Pool, PoolMeta
|
2012-08-27 12:19:36 +02:00
|
|
|
|
2012-10-17 11:18:43 +02:00
|
|
|
__all__ = ['Invoice']
|
|
|
|
__metaclass__ = PoolMeta
|
2012-08-27 12:19:36 +02:00
|
|
|
|
2012-10-17 11:18:43 +02:00
|
|
|
class Invoice:
|
|
|
|
'Invoice'
|
|
|
|
__name__ = 'account.invoice'
|
2012-08-27 12:19:36 +02:00
|
|
|
|
2012-10-17 11:18:43 +02:00
|
|
|
@classmethod
|
|
|
|
def __setup__(cls):
|
|
|
|
super(Invoice, cls).__setup__()
|
|
|
|
cls._error_messages.update({
|
|
|
|
'invalid_number_date': 'You are trying to create '
|
|
|
|
'%(invoice_number)s invoice, date %(invoice_date)s. '
|
|
|
|
'There are %(invoice_count)d invoices before this date:'
|
|
|
|
'\n\n%(invoices)s',
|
2012-08-27 12:19:36 +02:00
|
|
|
})
|
|
|
|
|
2012-10-17 11:18:43 +02:00
|
|
|
def set_number(self):
|
2012-08-27 12:19:36 +02:00
|
|
|
# TODO: When do we check this?
|
|
|
|
#if not invoice.journal_id.check_invoice_lines_tax:
|
|
|
|
#continue
|
2013-03-21 12:18:28 +01:00
|
|
|
res = super(Invoice, self).set_number()
|
2012-10-17 11:18:43 +02:00
|
|
|
if self.type in ('out_invoice', 'out_credit_note'):
|
2012-08-27 12:19:36 +02:00
|
|
|
cursor = Transaction().cursor
|
|
|
|
cursor.execute("""
|
|
|
|
SELECT
|
|
|
|
number,
|
|
|
|
invoice_date
|
|
|
|
FROM
|
|
|
|
account_invoice
|
|
|
|
WHERE
|
2012-10-17 12:13:36 +02:00
|
|
|
type = %s AND company = %s AND (
|
2012-08-27 12:19:36 +02:00
|
|
|
(number < %s AND invoice_date > %s) OR
|
|
|
|
(number > %s AND invoice_date < %s)
|
|
|
|
)
|
2012-10-17 12:13:36 +02:00
|
|
|
""", (self.type, self.company.id,
|
|
|
|
self.number, self.invoice_date,
|
2012-10-17 11:18:43 +02:00
|
|
|
self.number, self.invoice_date))
|
2012-08-27 12:19:36 +02:00
|
|
|
records = cursor.fetchall()
|
|
|
|
if records:
|
|
|
|
limit = 5
|
2012-10-17 11:18:43 +02:00
|
|
|
info = ['%(number)s - %(date)s' % {
|
|
|
|
'number': record[0],
|
|
|
|
'date': unicode(record[1]),
|
|
|
|
} for record in records]
|
|
|
|
info = '\n'.join(info[:limit])
|
2012-08-27 12:19:36 +02:00
|
|
|
self.raise_user_error('invalid_number_date', {
|
2012-10-17 11:18:43 +02:00
|
|
|
'invoice_number': self.number,
|
|
|
|
'invoice_date': self.invoice_date,
|
|
|
|
'invoice_count': len(records),
|
|
|
|
'invoices': info,
|
|
|
|
})
|