Files
tryton/modules/purchase/invoice.py
T

166 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 functools import wraps
2018-12-29 14:20:29 +01:00
from trytond.i18n import gettext
2012-09-24 22:18:09 +02:00
from trytond.model import ModelView, Workflow, fields
2018-12-29 14:20:29 +01:00
from trytond.model.exceptions import AccessError
2012-09-24 22:18:09 +02:00
from trytond.pool import Pool, PoolMeta
from trytond.pyson import Eval
from trytond.transaction import Transaction, without_check_access
2012-09-24 22:18:09 +02:00
def process_purchase(func):
@wraps(func)
def wrapper(cls, invoices):
pool = Pool()
Purchase = pool.get('purchase.purchase')
2022-03-09 00:57:17 +01:00
transaction = Transaction()
context = transaction.context
with without_check_access():
2018-07-27 09:27:49 +02:00
purchases = set(
p for i in cls.browse(invoices) for p in i.purchases)
func(cls, invoices)
2018-07-27 09:27:49 +02:00
if purchases:
2022-03-09 00:57:17 +01:00
with transaction.set_context(
queue_batch=context.get('queue_batch', True)):
Purchase.__queue__.process(purchases)
return wrapper
2018-06-26 19:11:51 +02:00
class Invoice(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.Many2Many(
'purchase.purchase', None, None, "Purchases"),
'get_purchases', searcher='search_purchases')
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):
return [('lines.origin.purchase' + clause[0][len(name):],
*clause[1:3], 'purchase.line', *clause[3:])]
@classmethod
@process_purchase
def delete(cls, invoices):
2012-09-24 22:18:09 +02:00
super(Invoice, cls).delete(invoices)
@classmethod
@ModelView.button
@Workflow.transition('draft')
def draft(cls, invoices):
2018-12-29 14:20:29 +01:00
for invoice in invoices:
if invoice.purchases and invoice.state == 'cancelled':
2018-12-29 14:20:29 +01:00
raise AccessError(
gettext('purchase.msg_purchase_invoice_reset_draft',
invoice=invoice.rec_name))
2012-09-24 22:18:09 +02:00
return super(Invoice, cls).draft(invoices)
@classmethod
@process_purchase
2021-02-27 15:23:46 +01:00
def _post(cls, invoices):
super()._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)
2018-06-26 19:11:51 +02:00
class InvoiceLine(metaclass=PoolMeta):
2012-09-24 22:18:09 +02:00
__name__ = 'account.invoice.line'
@classmethod
def __setup__(cls):
super().__setup__()
if not cls.origin.domain:
cls.origin.domain = {}
cls.origin.domain['purchase.line'] = [
('type', '=', Eval('type')),
]
@fields.depends('origin')
def on_change_with_product_uom_category(self, name=None):
pool = Pool()
PurchaseLine = pool.get('purchase.line')
category = super().on_change_with_product_uom_category(name=name)
# Enforce the same unit category as they are used to compute the
# remaining quantity to invoice and the quantity to receive.
# Use getattr as reference field can have negative id
if (isinstance(self.origin, PurchaseLine)
and getattr(self.origin, 'unit', None)):
2023-03-26 09:49:00 +02:00
category = self.origin.unit.category
return category
2021-07-29 09:06:38 +02:00
def get_warehouse(self, name):
pool = Pool()
PurchaseLine = pool.get('purchase.line')
warehouse = super().get_warehouse(name)
if (not warehouse
and isinstance(self.origin, PurchaseLine)
and self.origin.purchase.warehouse):
warehouse = self.origin.purchase.warehouse.id
return warehouse
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) and self.origin.id >= 0:
2019-05-22 18:37:08 +02:00
name = 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')
2022-03-09 00:57:17 +01:00
transaction = Transaction()
context = transaction.context
with without_check_access():
2018-07-27 09:27:49 +02:00
invoices = (l.invoice for l in cls.browse(lines)
if l.type == 'line' and l.invoice)
2018-07-27 09:27:49 +02:00
purchases = set(p for i in invoices for p in i.purchases)
super(InvoiceLine, cls).delete(lines)
2018-07-27 09:27:49 +02:00
if purchases:
2022-03-09 00:57:17 +01:00
with transaction.set_context(
queue_batch=context.get('queue_batch', True)):
Purchase.__queue__.process(purchases)