Files
tryton/invoice.py
T

159 lines
5.0 KiB
Python
Raw Normal View History

2014-12-02 12:55:52 +01:00
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
from functools import wraps
2013-09-06 22:34:40 +02:00
from sql import Table
2012-09-24 22:18:13 +02:00
from trytond.model import Workflow, fields
from trytond.pool import Pool, PoolMeta
from trytond.transaction import Transaction
2013-09-06 22:34:40 +02:00
from trytond import backend
2012-09-24 22:18:13 +02:00
__all__ = ['Invoice', 'InvoiceLine']
__metaclass__ = PoolMeta
2011-01-16 22:02:53 +01:00
def process_sale(func):
@wraps(func)
def wrapper(cls, invoices):
pool = Pool()
Sale = pool.get('sale.sale')
2014-09-17 21:45:49 +02:00
with Transaction().set_context(_check_access=False):
sales = [s for i in cls.browse(invoices) for s in i.sales]
func(cls, invoices)
2014-09-17 21:45:49 +02:00
with Transaction().set_context(_check_access=False):
Sale.process(sales)
return wrapper
2012-09-24 22:18:13 +02:00
class Invoice:
__name__ = 'account.invoice'
sale_exception_state = fields.Function(fields.Selection([
('', ''),
('ignored', 'Ignored'),
('recreated', 'Recreated'),
], 'Exception State'), 'get_sale_exception_state')
sales = fields.Function(fields.One2Many('sale.sale', None, 'Sales'),
'get_sales', searcher='search_sales')
2012-09-24 22:18:13 +02:00
@classmethod
def __setup__(cls):
super(Invoice, cls).__setup__()
cls._error_messages.update({
'reset_invoice_sale': ('You cannot reset to draft '
'an invoice generated by a sale.'),
})
2012-09-24 22:18:13 +02:00
@classmethod
def get_sale_exception_state(cls, invoices, name):
Sale = Pool().get('sale.sale')
2014-09-17 21:45:49 +02:00
sales = Sale.search([
('invoices', 'in', [i.id for i in invoices]),
])
2012-09-24 22:18:13 +02:00
recreated = tuple(i for p in sales for i in p.invoices_recreated)
ignored = tuple(i for p in sales for i in p.invoices_ignored)
states = {}
for invoice in invoices:
states[invoice.id] = ''
if invoice in recreated:
states[invoice.id] = 'recreated'
elif invoice.id in ignored:
states[invoice.id] = 'ignored'
return states
2011-01-16 22:02:53 +01:00
def get_sales(self, name):
pool = Pool()
SaleLine = pool.get('sale.line')
sales = set()
for line in self.lines:
if isinstance(line.origin, SaleLine):
sales.add(line.origin.sale.id)
return list(sales)
@classmethod
def search_sales(cls, name, clause):
return [('lines.origin.sale.id',) + tuple(clause[1:])
+ ('sale.line',)]
2012-12-04 16:48:25 +01:00
@classmethod
def copy(cls, invoices, default=None):
2012-03-01 10:21:40 +01:00
if default is None:
default = {}
default = default.copy()
2012-03-27 10:43:58 +02:00
default.setdefault('sales', None)
2012-12-04 16:48:25 +01:00
return super(Invoice, cls).copy(invoices, default=default)
2012-03-01 10:21:40 +01:00
2012-09-24 22:18:13 +02:00
@classmethod
@process_sale
2012-09-24 22:18:13 +02:00
def delete(cls, invoices):
super(Invoice, cls).delete(invoices)
@classmethod
@process_sale
def post(cls, invoices):
super(Invoice, cls).post(invoices)
2012-03-25 00:18:13 +01:00
2012-09-24 22:18:13 +02:00
@classmethod
@process_sale
def paid(cls, invoices):
super(Invoice, cls).paid(invoices)
@classmethod
@process_sale
2012-09-24 22:18:13 +02:00
def cancel(cls, invoices):
super(Invoice, cls).cancel(invoices)
2012-03-25 00:18:13 +01:00
2012-09-24 22:18:13 +02:00
@classmethod
@Workflow.transition('draft')
def draft(cls, invoices):
Sale = Pool().get('sale.sale')
2014-09-17 21:45:49 +02:00
sales = Sale.search([
('invoices', 'in', [i.id for i in invoices]),
])
2012-12-23 22:30:44 +01:00
if sales and any(i.state == 'cancel' for i in invoices):
2012-09-24 22:18:13 +02:00
cls.raise_user_error('reset_invoice_sale')
return super(Invoice, cls).draft(invoices)
2011-01-16 22:02:53 +01:00
2012-09-24 22:18:13 +02:00
class InvoiceLine:
__name__ = 'account.invoice.line'
2011-01-16 22:02:53 +01:00
2012-09-24 22:18:13 +02:00
@classmethod
def __register__(cls, module_name):
2013-09-06 22:34:40 +02:00
TableHandler = backend.get('TableHandler')
cursor = Transaction().cursor
2013-09-06 22:34:40 +02:00
sql_table = cls.__table__()
super(InvoiceLine, cls).__register__(module_name)
# Migration from 2.6: remove sale_lines
2013-09-06 22:34:40 +02:00
rel_table_name = 'sale_line_invoice_lines_rel'
if TableHandler.table_exist(cursor, rel_table_name):
rel_table = Table(rel_table_name)
cursor.execute(*rel_table.select(
rel_table.sale_line, rel_table.invoice_line))
for sale_line, invoice_line in cursor.fetchall():
2013-09-06 22:34:40 +02:00
cursor.execute(*sql_table.update(
columns=[sql_table.origin],
values=['sale.line,%s' % sale_line],
where=sql_table.id == invoice_line))
TableHandler.drop_table(cursor,
'sale.line-account.invoice.line', rel_table_name)
2013-09-19 17:34:09 +02:00
@property
def origin_name(self):
pool = Pool()
SaleLine = pool.get('sale.line')
name = super(InvoiceLine, self).origin_name
if isinstance(self.origin, SaleLine):
name = self.origin.sale.rec_name
return name
@classmethod
def _get_origin(cls):
models = super(InvoiceLine, cls)._get_origin()
models.append('sale.line')
return models