Files
tryton/modules/analytic_purchase/purchase.py
T

71 lines
2.5 KiB
Python
Raw Normal View History

2014-12-02 12:54:59 +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 trytond.model import fields
2012-09-24 22:17:41 +02:00
from trytond.pool import Pool, PoolMeta
2016-09-27 19:37:51 +02:00
from trytond.pyson import Eval, If
2012-09-24 22:17:41 +02:00
2015-01-02 14:04:56 +01:00
from trytond.modules.analytic_account import AnalyticMixin
2018-06-26 19:11:45 +02:00
class PurchaseLine(AnalyticMixin, metaclass=PoolMeta):
2012-09-24 22:17:41 +02:00
__name__ = 'purchase.line'
2008-12-05 13:26:25 +01:00
2016-09-27 19:37:51 +02:00
@classmethod
def __setup__(cls):
super(PurchaseLine, cls).__setup__()
cls.analytic_accounts.domain = [
('company', '=', If(~Eval('_parent_purchase'),
Eval('context', {}).get('company', -1),
Eval('_parent_purchase', {}).get('company', -1))),
]
cls.analytic_accounts.states = {
'invisible': Eval('type') != 'line',
'readonly': Eval('purchase_state') != 'draft',
}
cls.analytic_accounts.depends.extend(['type', 'purchase_state'])
2016-09-27 19:37:51 +02:00
2016-03-20 22:58:42 +01:00
def get_invoice_line(self):
2015-01-02 14:04:56 +01:00
pool = Pool()
AnalyticAccountEntry = pool.get('analytic.account.entry')
2008-12-05 13:26:25 +01:00
2016-03-20 22:58:42 +01:00
invoice_lines = super(PurchaseLine, self).get_invoice_line()
2012-09-24 22:17:41 +02:00
for invoice_line in invoice_lines:
2015-01-02 14:04:56 +01:00
new_entries = AnalyticAccountEntry.copy(self.analytic_accounts,
default={
'origin': None,
})
invoice_line.analytic_accounts = new_entries
2012-09-24 22:17:41 +02:00
return invoice_lines
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:41 +02:00
@classmethod
2015-01-02 14:04:56 +01:00
def _get_origin(cls):
origins = super(AnalyticAccountEntry, cls)._get_origin()
return origins + ['purchase.line']
2016-09-27 19:37:51 +02:00
@fields.depends('origin')
def on_change_with_company(self, name=None):
pool = Pool()
PurchaseLine = pool.get('purchase.line')
company = super(AnalyticAccountEntry, self).on_change_with_company(
name)
if isinstance(self.origin, PurchaseLine) and self.origin.purchase:
company = self.origin.purchase.company.id
return company
@classmethod
def search_company(cls, name, clause):
operator = clause[1]
if operator.startswith('!') or operator.startswith('not '):
bool_op = 'AND'
else:
bool_op = 'OR'
2016-09-27 19:37:51 +02:00
domain = super(AnalyticAccountEntry, cls).search_company(name, clause)
return [bool_op,
2016-09-27 19:37:51 +02:00
domain,
('origin.purchase.' + clause[0],
*clause[1:3], 'purchase.line', *clause[3:]),
2016-09-27 19:37:51 +02:00
]