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']
|
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:
|
2016-03-29 11:43:12 +02:00
|
|
|
__metaclass__ = PoolMeta
|
2012-10-17 11:18:43 +02:00
|
|
|
__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',
|
2015-08-11 10:49:07 +02:00
|
|
|
'not_same_dates': 'You are trying to validate an invoice '
|
|
|
|
'where invoice date (%(invoice_date)s) and accounting '
|
|
|
|
'date (%(accounting_date)s) are different. That is not '
|
|
|
|
'permitted, because of invoice number and date '
|
|
|
|
'correlation.',
|
|
|
|
})
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def validate(cls, invoices):
|
|
|
|
super(Invoice, cls).validate(invoices)
|
|
|
|
for invoice in invoices:
|
2016-03-29 17:30:01 +02:00
|
|
|
if invoice.type != 'in':
|
2015-09-28 15:04:28 +02:00
|
|
|
invoice.check_same_dates()
|
2015-08-11 10:49:07 +02:00
|
|
|
|
|
|
|
def check_same_dates(self):
|
|
|
|
pool=Pool()
|
|
|
|
Lang = pool.get('ir.lang')
|
|
|
|
|
|
|
|
if (self.invoice_date and self.accounting_date
|
|
|
|
and self.invoice_date != self.accounting_date):
|
|
|
|
language = Transaction().language
|
|
|
|
languages = Lang.search([('code', '=', language)])
|
|
|
|
if not languages:
|
|
|
|
languages = Lang.search([('code', '=', 'en_US')])
|
|
|
|
language = languages[0]
|
|
|
|
|
|
|
|
self.raise_user_error('not_same_dates', {
|
|
|
|
'invoice_date': Lang.strftime(self.invoice_date, language.code,
|
|
|
|
language.date),
|
|
|
|
'accounting_date': Lang.strftime(self.accounting_date,
|
|
|
|
language.code, language.date),
|
2012-08-27 12:19:36 +02:00
|
|
|
})
|
|
|
|
|
2016-08-11 15:20:20 +02:00
|
|
|
@classmethod
|
|
|
|
def set_number(cls, invoices):
|
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')
|
2015-09-29 15:43:31 +02:00
|
|
|
Module = pool.get('ir.module')
|
2016-08-11 15:20:20 +02:00
|
|
|
to_check = [i for i in invoices
|
|
|
|
if not bool(i.number) and i.type == 'out']
|
2014-08-31 02:14:37 +02:00
|
|
|
|
2016-08-11 15:20:20 +02:00
|
|
|
super(Invoice, cls).set_number(invoices)
|
|
|
|
for invoice in to_check:
|
|
|
|
table = cls.__table__()
|
2014-11-10 13:31:41 +01:00
|
|
|
move = Move.__table__()
|
2015-05-06 11:47:52 +02:00
|
|
|
period = Period.__table__()
|
2015-08-11 10:49:07 +02:00
|
|
|
# As we have a control in the validate that make the
|
|
|
|
# accounting_date have to be the same as invoice_date, in cas it
|
|
|
|
# exist, we can use invoice_date to calculate the period.
|
2016-08-11 15:20:20 +02:00
|
|
|
period_id = Period.find(
|
|
|
|
invoice.company.id, date=invoice.invoice_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') &
|
2016-08-11 15:20:20 +02:00
|
|
|
(table.type == invoice.type) &
|
|
|
|
(table.company == invoice.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'),
|
2016-08-11 15:20:20 +02:00
|
|
|
])
|
2014-11-10 13:51:42 +01:00
|
|
|
if account_invoice_sequence_module_installed:
|
2016-08-11 15:20:20 +02:00
|
|
|
where &= (table.journal == invoice.journal.id)
|
2014-11-10 13:51:42 +01:00
|
|
|
|
2016-08-11 15:20:20 +02:00
|
|
|
where &= (((table.number < invoice.number) &
|
|
|
|
(table.invoice_date > invoice.invoice_date)) |
|
|
|
|
((table.number > invoice.number) &
|
|
|
|
(table.invoice_date < invoice.invoice_date)))
|
2015-05-06 11:47:52 +02:00
|
|
|
query = query.select(table.number, table.invoice_date, where=where,
|
|
|
|
limit=5)
|
2016-03-10 13:34:40 +01:00
|
|
|
cursor = Transaction().connection.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)
|
2016-08-11 15:20:20 +02:00
|
|
|
cls.raise_user_error('invalid_number_date', {
|
|
|
|
'invoice_number': invoice.number,
|
|
|
|
'invoice_date': Lang.strftime(invoice.invoice_date,
|
2014-08-31 02:14:37 +02:00
|
|
|
language.code, language.date),
|
2012-10-17 11:18:43 +02:00
|
|
|
'invoice_count': len(records),
|
|
|
|
'invoices': info,
|
|
|
|
})
|