2015-05-11 17:48:45 +02:00
|
|
|
# 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.transaction import Transaction
|
2014-08-31 02:14:37 +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
|
|
|
|
2013-04-16 23:07:22 +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 '
|
2014-08-31 02:14:37 +02:00
|
|
|
'%(invoice_number)s invoice on date %(invoice_date)s. '
|
|
|
|
'There are %(invoice_count)d invoices after this date:'
|
2012-10-17 11:18:43 +02:00
|
|
|
'\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?
|
2015-05-11 17:48:45 +02:00
|
|
|
# if not invoice.journal_id.check_invoice_lines_tax:
|
|
|
|
# continue
|
2014-11-10 13:31:41 +01:00
|
|
|
pool = Pool()
|
|
|
|
Period = pool.get('account.period')
|
|
|
|
Move = pool.get('account.move')
|
|
|
|
Lang = pool.get('ir.lang')
|
2014-11-10 13:51:42 +01:00
|
|
|
Module = pool.get('ir.module.module')
|
2014-08-31 02:14:37 +02:00
|
|
|
|
2013-04-16 23:07:22 +02:00
|
|
|
super(Invoice, self).set_number()
|
2012-10-17 11:18:43 +02:00
|
|
|
if self.type in ('out_invoice', 'out_credit_note'):
|
2014-07-18 16:22:06 +02:00
|
|
|
table = self.__table__()
|
2014-11-10 13:31:41 +01:00
|
|
|
move = Move.__table__()
|
2015-05-06 11:47:52 +02:00
|
|
|
period = Period.__table__()
|
2014-11-10 13:31:41 +01:00
|
|
|
accounting_date = self.accounting_date or self.invoice_date
|
|
|
|
period_id = Period.find(self.company.id, date=accounting_date)
|
2015-05-06 11:47:52 +02:00
|
|
|
fiscalyear = Period(period_id).fiscalyear
|
|
|
|
query = table.join(move, condition=(table.move == move.id)).join(
|
|
|
|
period, condition=move.period == period.id)
|
|
|
|
|
2014-11-10 13:51:42 +01:00
|
|
|
where = ((table.state != 'draft') &
|
|
|
|
(table.type == self.type) &
|
|
|
|
(table.company == self.company.id) &
|
2015-05-06 11:47:52 +02:00
|
|
|
(period.fiscalyear == fiscalyear.id))
|
2014-11-10 13:51:42 +01:00
|
|
|
|
|
|
|
account_invoice_sequence_module_installed = Module.search([
|
2015-03-20 19:14:18 +01:00
|
|
|
('name', '=', 'account_invoice_multisequence'),
|
2014-11-10 13:51:42 +01:00
|
|
|
('state', '=', 'installed'),
|
|
|
|
])
|
|
|
|
if account_invoice_sequence_module_installed:
|
|
|
|
where &= (table.journal == self.journal.id)
|
|
|
|
|
|
|
|
where &= (((table.number < self.number) &
|
|
|
|
(table.invoice_date > accounting_date)) |
|
|
|
|
((table.number > self.number) &
|
|
|
|
(table.invoice_date < accounting_date)))
|
2015-05-06 11:47:52 +02:00
|
|
|
query = query.select(table.number, table.invoice_date, where=where,
|
|
|
|
limit=5)
|
2012-08-27 12:19:36 +02:00
|
|
|
cursor = Transaction().cursor
|
2014-07-18 16:22:06 +02:00
|
|
|
cursor.execute(*query)
|
2012-08-27 12:19:36 +02:00
|
|
|
records = cursor.fetchall()
|
|
|
|
if records:
|
2014-08-31 02:14:37 +02:00
|
|
|
language = Transaction().language
|
|
|
|
languages = Lang.search([('code', '=', language)])
|
|
|
|
if not languages:
|
|
|
|
languages = Lang.search([('code', '=', 'en_US')])
|
|
|
|
language = languages[0]
|
2012-10-17 11:18:43 +02:00
|
|
|
info = ['%(number)s - %(date)s' % {
|
|
|
|
'number': record[0],
|
2014-08-31 02:14:37 +02:00
|
|
|
'date': Lang.strftime(record[1], language.code,
|
|
|
|
language.date),
|
2012-10-17 11:18:43 +02:00
|
|
|
} for record in records]
|
2015-05-06 11:47:52 +02:00
|
|
|
info = '\n'.join(info)
|
2012-08-27 12:19:36 +02:00
|
|
|
self.raise_user_error('invalid_number_date', {
|
2014-07-18 16:22:06 +02:00
|
|
|
'invoice_number': self.number,
|
2014-08-31 02:14:37 +02:00
|
|
|
'invoice_date': Lang.strftime(self.invoice_date,
|
|
|
|
language.code, language.date),
|
2012-10-17 11:18:43 +02:00
|
|
|
'invoice_count': len(records),
|
|
|
|
'invoices': info,
|
|
|
|
})
|