File: invoice.py

package info (click to toggle)
tryton-modules-analytic-invoice 1%3A5.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 336 kB
  • sloc: python: 285; xml: 36; makefile: 6; sh: 3
file content (109 lines) | stat: -rw-r--r-- 3,915 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# 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
from trytond.pool import PoolMeta, Pool
from trytond.pyson import Eval, If

from trytond.modules.analytic_account import AnalyticMixin

__all__ = ['InvoiceLine', 'AnalyticAccountEntry']


class InvoiceLine(AnalyticMixin, metaclass=PoolMeta):
    __name__ = 'account.invoice.line'

    @classmethod
    def __setup__(cls):
        super(InvoiceLine, cls).__setup__()
        cls.analytic_accounts.domain = [
            ('company', '=', If(~Eval('company'),
                    Eval('context', {}).get('company', -1),
                    Eval('company', -1))),
            ]
        cls.analytic_accounts.depends.append('company')
        cls.analytic_accounts.states = {
            'invisible': Eval('type') != 'line',
            'readonly': Eval('invoice_state') != 'draft',
            }
        cls.analytic_accounts.depends.extend(['type', 'invoice_state'])

    def _credit(self):
        pool = Pool()
        AnalyticAccountEntry = pool.get('analytic.account.entry')

        line = super(InvoiceLine, self)._credit()
        if self.analytic_accounts:
            new_entries = AnalyticAccountEntry.copy(self.analytic_accounts,
                default={
                    'origin': None,
                    })
            line.analytic_accounts = new_entries
        return line

    def get_move_lines(self):
        lines = super(InvoiceLine, self).get_move_lines()
        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:
            date = self.invoice.accounting_date or self.invoice.invoice_date
            for line in lines:
                analytic_lines = []
                for entry in self.analytic_accounts:
                    analytic_lines.extend(
                        entry.get_analytic_lines(line, date))
                line.analytic_lines = analytic_lines
        return lines


class AnalyticAccountEntry(metaclass=PoolMeta):
    __name__ = 'analytic.account.entry'

    @classmethod
    def _get_origin(cls):
        pool = Pool()
        origins = super(AnalyticAccountEntry, cls)._get_origin()
        origins.append('account.invoice.line')
        try:
            pool.get('account.asset')
            origins.append('account.asset')
        except KeyError:
            pass
        return origins

    @fields.depends('origin')
    def on_change_with_company(self, name=None):
        pool = Pool()
        InvoiceLine = pool.get('account.invoice.line')
        try:
            Asset = pool.get('account.asset')
        except KeyError:
            Asset = None
        company = super(AnalyticAccountEntry, self).on_change_with_company(
            name)
        if (isinstance(self.origin, InvoiceLine)
                or (Asset and isinstance(self.origin, Asset))):
            company = self.origin.company.id if self.origin.company else None
        return company

    @classmethod
    def search_company(cls, name, clause):
        pool = Pool()
        domain = super(AnalyticAccountEntry, cls).search_company(name, clause),
        domain = ['OR',
            domain,
            (('origin.' + clause[0],) + tuple(clause[1:3])
                + ('account.invoice.line',) + tuple(clause[3:])),
            ]
        try:
            pool.get('account.asset')
            domain.append(
                (('origin.' + clause[0],) + tuple(clause[1:3])
                    + ('account.asset',) + tuple(clause[3:])))
        except KeyError:
            pass
        return domain