Files
tryton/invoice.py
T

165 lines
5.5 KiB
Python
Raw Normal View History

2014-12-02 12:55:47 +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 itertools import chain
from functools import wraps
2013-09-06 22:34:36 +02:00
from sql import Table
2012-09-24 22:18:09 +02:00
from trytond.model import ModelView, Workflow, fields
from trytond.pool import Pool, PoolMeta
from trytond.transaction import Transaction
2013-09-06 22:34:36 +02:00
from trytond import backend
2012-09-24 22:18:09 +02:00
__all__ = ['Invoice', 'InvoiceLine']
def process_purchase(func):
@wraps(func)
def wrapper(cls, invoices):
pool = Pool()
Purchase = pool.get('purchase.purchase')
2014-09-17 21:45:46 +02:00
with Transaction().set_context(_check_access=False):
purchases = [p for i in cls.browse(invoices) for p in i.purchases]
func(cls, invoices)
2014-09-17 21:45:46 +02:00
with Transaction().set_context(_check_access=False):
Purchase.process(purchases)
return wrapper
2012-09-24 22:18:09 +02:00
class Invoice:
2016-03-04 00:34:21 +01:00
__metaclass__ = PoolMeta
2012-09-24 22:18:09 +02:00
__name__ = 'account.invoice'
purchase_exception_state = fields.Function(fields.Selection([
('', ''),
('ignored', 'Ignored'),
('recreated', 'Recreated'),
], 'Exception State'), 'get_purchase_exception_state')
purchases = fields.Function(fields.One2Many('purchase.purchase', None,
'Purchases'), 'get_purchases', searcher='search_purchases')
2012-09-24 22:18:09 +02:00
@classmethod
def __setup__(cls):
super(Invoice, cls).__setup__()
cls._error_messages.update({
2013-02-24 15:45:26 +01:00
'reset_invoice_purchase': ('You cannot reset to draft '
'an invoice generated by a purchase.'),
2012-09-24 22:18:09 +02:00
})
2014-09-17 21:45:46 +02:00
def get_purchase_exception_state(self, name):
purchases = self.purchases
2012-09-24 22:18:09 +02:00
recreated = tuple(i for p in purchases for i in p.invoices_recreated)
ignored = tuple(i for p in purchases for i in p.invoices_ignored)
2014-09-17 21:45:46 +02:00
if self in recreated:
return 'recreated'
elif self in ignored:
return 'ignored'
return ''
2012-09-24 22:18:09 +02:00
def get_purchases(self, name):
2013-09-06 22:34:36 +02:00
pool = Pool()
PurchaseLine = pool.get('purchase.line')
purchases = set()
for line in self.lines:
if isinstance(line.origin, PurchaseLine):
purchases.add(line.origin.purchase.id)
return list(purchases)
@classmethod
def search_purchases(cls, name, clause):
2017-09-27 18:37:31 +02:00
return [('lines.origin.purchase' + clause[0].lstrip(name),)
+ tuple(clause[1:3]) + ('purchase.line',) + tuple(clause[3:])]
@classmethod
@process_purchase
def delete(cls, invoices):
2012-09-24 22:18:09 +02:00
super(Invoice, cls).delete(invoices)
@classmethod
def copy(cls, invoices, default=None):
2012-03-01 10:20:43 +01:00
if default is None:
default = {}
default = default.copy()
2012-03-27 10:43:54 +02:00
default.setdefault('purchases', None)
2012-09-24 22:18:09 +02:00
return super(Invoice, cls).copy(invoices, default=default)
2012-03-01 10:20:43 +01:00
2012-09-24 22:18:09 +02:00
@classmethod
@ModelView.button
@Workflow.transition('draft')
def draft(cls, invoices):
purchases = list(chain(*(i.purchases for i in invoices)))
2012-12-23 22:30:27 +01:00
if purchases and any(i.state == 'cancel' for i in invoices):
2012-09-24 22:18:09 +02:00
cls.raise_user_error('reset_invoice_purchase')
return super(Invoice, cls).draft(invoices)
@classmethod
@process_purchase
def post(cls, invoices):
super(Invoice, cls).post(invoices)
2012-03-25 00:18:10 +01:00
2012-09-24 22:18:09 +02:00
@classmethod
@process_purchase
def paid(cls, invoices):
super(Invoice, cls).paid(invoices)
@classmethod
@process_purchase
2012-09-24 22:18:09 +02:00
def cancel(cls, invoices):
super(Invoice, cls).cancel(invoices)
2012-09-24 22:18:09 +02:00
class InvoiceLine:
2016-03-04 00:34:21 +01:00
__metaclass__ = PoolMeta
2012-09-24 22:18:09 +02:00
__name__ = 'account.invoice.line'
2012-09-24 22:18:09 +02:00
@classmethod
def __register__(cls, module_name):
2013-09-06 22:34:36 +02:00
TableHandler = backend.get('TableHandler')
cursor = Transaction().connection.cursor()
2013-09-06 22:34:36 +02:00
sql_table = cls.__table__()
super(InvoiceLine, cls).__register__(module_name)
# Migration from 2.6: remove purchase_lines
2013-09-06 22:34:36 +02:00
rel_table_name = 'purchase_line_invoice_lines_rel'
if TableHandler.table_exist(rel_table_name):
2013-09-06 22:34:36 +02:00
rel_table = Table(rel_table_name)
cursor.execute(*rel_table.select(
rel_table.purchase_line, rel_table.invoice_line))
for purchase_line, invoice_line in cursor.fetchall():
2013-09-06 22:34:36 +02:00
cursor.execute(*sql_table.update(
columns=[sql_table.origin],
values=['purchase.line,%s' % purchase_line],
where=sql_table.id == invoice_line))
TableHandler.drop_table(
2013-09-06 22:34:36 +02:00
'purchase.line-account.invoice.line', rel_table_name)
2013-09-19 17:33:53 +02:00
@property
def origin_name(self):
pool = Pool()
PurchaseLine = pool.get('purchase.line')
name = super(InvoiceLine, self).origin_name
if isinstance(self.origin, PurchaseLine):
2017-03-29 22:41:03 +02:00
name = (self.origin.purchase.reference
or self.origin.purchase.rec_name)
2013-09-19 17:33:53 +02:00
return name
@classmethod
def _get_origin(cls):
models = super(InvoiceLine, cls)._get_origin()
models.append('purchase.line')
return models
@classmethod
def delete(cls, lines):
pool = Pool()
Purchase = pool.get('purchase.purchase')
with Transaction().set_context(_check_access=False):
lines = cls.browse(lines)
invoices = (l.invoice for l in lines
if l.type == 'line' and l.invoice)
purchases = [p for i in invoices for p in i.purchases]
super(InvoiceLine, cls).delete(lines)
with Transaction().set_context(_check_access=False):
Purchase.process(purchases)