Files
tryton/modules/analytic_invoice/invoice.py
T

138 lines
4.6 KiB
Python
Raw Normal View History

2014-12-02 12:54:57 +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.
2016-09-27 19:36:53 +02:00
from trytond.model import fields
2015-01-02 14:04:56 +01:00
from trytond.modules.analytic_account import AnalyticMixin
2021-12-11 17:58:42 +01:00
from trytond.pool import Pool, PoolMeta
from trytond.pyson import Eval
2015-01-02 14:04:56 +01:00
2008-05-26 12:13:55 +02:00
2018-06-26 19:11:45 +02:00
class InvoiceLine(AnalyticMixin, metaclass=PoolMeta):
2012-09-24 22:17:40 +02:00
__name__ = 'account.invoice.line'
2008-12-05 13:26:08 +01:00
2016-09-27 19:36:53 +02:00
@classmethod
def __setup__(cls):
2025-04-04 00:45:18 +02:00
super().__setup__()
2016-09-27 19:36:53 +02:00
cls.analytic_accounts.domain = [
('company', '=', Eval('company', -1)),
2016-09-27 19:36:53 +02:00
]
cls.analytic_accounts.states = {
'invisible': Eval('type') != 'line',
'readonly': Eval('invoice_state') != 'draft',
}
2016-09-27 19:36:53 +02:00
2012-09-24 22:17:40 +02:00
def _credit(self):
2015-01-02 14:04:56 +01:00
pool = Pool()
AnalyticAccountEntry = pool.get('analytic.account.entry')
2008-12-05 13:26:08 +01:00
2025-04-04 00:45:18 +02:00
line = super()._credit()
2012-09-24 22:17:40 +02:00
if self.analytic_accounts:
2015-01-02 14:04:56 +01:00
new_entries = AnalyticAccountEntry.copy(self.analytic_accounts,
default={
'origin': None,
})
line.analytic_accounts = new_entries
return line
2015-01-02 14:04:56 +01:00
def get_move_lines(self):
2025-04-04 00:45:18 +02:00
lines = super().get_move_lines()
2016-10-12 22:36:54 +02:00
if self.invoice and self.invoice.type:
type_ = self.invoice.type
else:
type_ = self.invoice_type
asset_depreciable = (self.product and type_ == 'in'
and self.product.type == 'assets'
and getattr(self.product, 'depreciable', False))
if self.analytic_accounts and not asset_depreciable:
2016-10-05 19:20:35 +02:00
date = self.invoice.accounting_date or self.invoice.invoice_date
for line in lines:
analytic_lines = []
2015-01-02 14:04:56 +01:00
for entry in self.analytic_accounts:
2016-10-05 19:20:35 +02:00
analytic_lines.extend(
entry.get_analytic_lines(line, date))
line.analytic_lines = analytic_lines
return lines
2008-05-26 12:13:55 +02:00
2023-05-30 16:30:04 +02:00
class InvoiceDeferred(metaclass=PoolMeta):
__name__ = 'account.invoice.deferred'
def get_move(self, period=None):
move = super().get_move(period=period)
if self.invoice_line.analytic_accounts:
for line in move.lines:
if line.account.type.statement != 'income':
continue
analytic_lines = []
for entry in self.invoice_line.analytic_accounts:
analytic_lines.extend(
entry.get_analytic_lines(line, move.date))
line.analytic_lines = analytic_lines
return move
2018-06-26 19:11:45 +02:00
class AnalyticAccountEntry(metaclass=PoolMeta):
2015-01-02 14:04:56 +01:00
__name__ = 'analytic.account.entry'
2012-09-24 22:17:40 +02:00
@classmethod
2015-01-02 14:04:56 +01:00
def _get_origin(cls):
2016-10-12 22:36:54 +02:00
pool = Pool()
2025-04-04 00:45:18 +02:00
origins = super()._get_origin()
2016-10-12 22:36:54 +02:00
origins.append('account.invoice.line')
try:
pool.get('account.asset')
origins.append('account.asset')
except KeyError:
pass
return origins
2016-09-27 19:36:53 +02:00
@fields.depends('origin')
def on_change_with_company(self, name=None):
pool = Pool()
InvoiceLine = pool.get('account.invoice.line')
2016-10-12 22:36:54 +02:00
try:
Asset = pool.get('account.asset')
except KeyError:
Asset = None
2023-03-26 09:49:00 +02:00
company = super().on_change_with_company(name=name)
2016-10-12 22:36:54 +02:00
if (isinstance(self.origin, InvoiceLine)
or (Asset and isinstance(self.origin, Asset))):
2023-03-26 09:49:00 +02:00
company = self.origin.company
2016-09-27 19:36:53 +02:00
return company
@classmethod
def search_company(cls, name, clause):
2016-10-12 22:36:54 +02:00
pool = Pool()
2025-04-04 00:45:18 +02:00
domain = super().search_company(name, clause),
2016-10-12 22:36:54 +02:00
domain = ['OR',
2016-09-27 19:36:53 +02:00
domain,
2017-09-27 18:37:27 +02:00
(('origin.' + clause[0],) + tuple(clause[1:3])
+ ('account.invoice.line',) + tuple(clause[3:])),
2016-09-27 19:36:53 +02:00
]
2016-10-12 22:36:54 +02:00
try:
pool.get('account.asset')
domain.append(
2017-09-27 18:37:27 +02:00
(('origin.' + clause[0],) + tuple(clause[1:3])
+ ('account.asset',) + tuple(clause[3:])))
2016-10-12 22:36:54 +02:00
except KeyError:
pass
return domain
@fields.depends('origin')
def on_change_with_editable(self, name=None):
pool = Pool()
InvoiceLine = pool.get('account.invoice.line')
try:
Asset = pool.get('account.asset')
except KeyError:
Asset = None
editable = super().on_change_with_editable(name=name)
if isinstance(self.origin, InvoiceLine):
if self.origin.invoice_state != 'draft':
editable = False
elif Asset and isinstance(self.origin, Asset):
if self.origin.state != 'draft':
editable = False
return editable