trytond-account_invoice_con.../invoice.py

105 lines
4.0 KiB
Python
Raw Normal View History

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
from trytond.pool import Pool, PoolMeta
from sql.aggregate import Sum
from sql.functions import Round
from trytond.i18n import gettext
from trytond.exceptions import UserError
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
2018-08-24 12:16:31 +02:00
class Invoice(metaclass=PoolMeta):
2012-10-17 11:18:43 +02:00
__name__ = 'account.invoice'
2012-08-27 12:19:36 +02:00
@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':
invoice.check_same_dates()
def check_same_dates(self):
2018-01-09 15:53:17 +01:00
pool = Pool()
Lang = pool.get('ir.lang')
if (self.invoice_date and self.accounting_date
2018-01-09 15:53:17 +01:00
and self.invoice_date != self.accounting_date):
language = Transaction().language
languages = Lang.search([('code', '=', language)], limit=1)
if not languages:
languages = Lang.search([('code', '=', 'en')], limit=1)
language, = languages
raise UserError(gettext(
'account_invoice_consecutive.not_same_dates',
2019-01-06 10:08:36 +01:00
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
@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
pool = Pool()
Period = pool.get('account.period')
Lang = pool.get('ir.lang')
2015-09-29 15:43:31 +02:00
Module = pool.get('ir.module')
Inv = pool.get('account.invoice')
to_check = [i for i in invoices if i.type == 'out' and not i.number]
super(Invoice, cls).set_number(invoices)
for invoice in to_check:
# 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.
period_id = Period.find(
invoice.company.id, date=invoice.invoice_date)
fiscalyear = Period(period_id).fiscalyear
domain = [
('number', '!=', None),
('type', '=', invoice.type),
('company', '=', invoice.company.id),
('move.period.fiscalyear', '=', fiscalyear.id),
['OR', [
('number', '<', invoice.number),
('invoice_date', '>', invoice.invoice_date),
], [
('number', '>', invoice.number),
('invoice_date', '<', invoice.invoice_date),
],],
]
if invoice.untaxed_amount >= 0:
domain.append(('untaxed_amount', '>=', 0))
else:
domain.append(('untaxed_amount', '<', 0))
account_invoice_sequence_module_installed = Module.search([
('name', '=', 'account_invoice_multisequence'),
('state', '=', 'activated'),
])
if account_invoice_sequence_module_installed:
domain.append(('journal', '=', invoice.journal.id))
invs = Inv.search(domain, limit=5)
if invs:
language = Lang.get()
2012-10-17 11:18:43 +02:00
info = ['%(number)s - %(date)s' % {
'number': inv.number,
'date': language.strftime(inv.invoice_date),
} for inv in invs]
info = '\n'.join(info)
raise UserError(gettext(
2019-01-06 10:08:36 +01:00
'account_invoice_consecutive.invalid_number_date',
invoice_number=invoice.number,
invoice_date=language.strftime(invoice.invoice_date),
invoice_count=len(invs),
invoices=info))