File: invoice.py

package info (click to toggle)
tryton-modules-analytic-invoice 1:1.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 192 kB
  • ctags: 41
  • sloc: python: 316; xml: 22; makefile: 2
file content (233 lines) | stat: -rw-r--r-- 10,012 bytes parent folder | download
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#This file is part of Tryton.  The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
"Invoice"
from trytond.model import ModelView, ModelSQL, fields
from trytond.pyson import Not, Equal, Eval


class InvoiceLine(ModelSQL, ModelView):
    _name = 'account.invoice.line'

    analytic_accounts = fields.Many2One('analytic_account.account.selection',
            'Analytic Accounts',
            states={
                'invisible': Not(Equal(Eval('type'), 'line')),
            })

    def _view_look_dom_arch(self, cursor, user, tree, type, context=None):
        analytic_account_obj = self.pool.get('analytic_account.account')
        analytic_account_obj.convert_view(cursor, user, tree, context=context)
        arch, fields = super(InvoiceLine, self)._view_look_dom_arch(cursor,
                user, tree, type, context=context)
        return arch, fields

    def fields_get(self, cursor, user, fields_names=None, context=None):
        analytic_account_obj = self.pool.get('analytic_account.account')

        res = super(InvoiceLine, self).fields_get(cursor, user, fields_names,
                context=context)

        analytic_accounts_field = super(InvoiceLine, self).fields_get(cursor,
                user, ['analytic_accounts'],
                context=context)['analytic_accounts']

        res.update(analytic_account_obj.analytic_accounts_fields_get(cursor,
            user, analytic_accounts_field, fields_names, context=context))
        return res

    def read(self, cursor, user, ids, fields_names=None, context=None):
        selection_obj = self.pool.get('analytic_account.account.selection')

        int_id = False
        if isinstance(ids, (int, long)):
            int_id = True
            ids = [ids]

        if fields_names:
            fields_names2 = [x for x in fields_names
                    if not x.startswith('analytic_account_')]
        else:
            fields_names2 = fields_names

        res = super(InvoiceLine, self).read(cursor, user, ids,
                fields_names=fields_names2, context=context)

        if not fields_names:
            fields_names = list(set(self._columns.keys() \
                    + self._inherit_fields.keys()))

        root_ids = []
        for field in fields_names:
            if field.startswith('analytic_account_') and '.' not in field:
                root_ids.append(int(field[len('analytic_account_'):]))
        if root_ids:
            id2record = {}
            for record in res:
                id2record[record['id']] = record
            lines = self.browse(cursor, user, ids, context=context)
            for line in lines:
                if line.type != 'line':
                    continue
                if not line.analytic_accounts:
                    continue
                for account in line.analytic_accounts.accounts:
                    if account.root.id in root_ids:
                        id2record[line.id]['analytic_account_' \
                                + str(account.root.id)] = account.id
                        for field in fields_names:
                            if field.startswith('analytic_account_' + \
                                    str(account.root.id) + '.'):
                                ham, field2 = field.split('.', 1)
                                id2record[line.id][field] = account[field2]
        if int_id:
            return res[0]
        return res

    def create(self, cursor, user, vals, context=None):
        selection_obj = self.pool.get('analytic_account.account.selection')
        vals = vals.copy()
        selection_vals = {}
        for field in vals.keys():
            if field.startswith('analytic_account_'):
                if vals[field]:
                    selection_vals.setdefault('accounts', [])
                    selection_vals['accounts'].append(('add', vals[field]))
                del vals[field]
        if vals.get('analytic_accounts'):
            selection_obj.write(cursor, user, vals['analytic_accounts'],
                    selection_vals, context=context)
        elif vals.get('type', 'line') == 'line':
            selection_id = selection_obj.create(cursor, user, selection_vals,
                    context=context)
            vals['analytic_accounts'] = selection_id
        return super(InvoiceLine, self).create(cursor, user, vals,
                context=context)

    def write(self, cursor, user, ids, vals, context=None):
        selection_obj = self.pool.get('analytic_account.account.selection')
        vals = vals.copy()
        if isinstance(ids, (int, long)):
            ids = [ids]
        selection_vals = {}
        for field in vals.keys():
            if field.startswith('analytic_account_'):
                root_id = int(field[len('analytic_account_'):])
                selection_vals[root_id] = vals[field]
                del vals[field]
        if selection_vals:
            lines = self.browse(cursor, user, ids, context=context)
            for line in lines:
                if line.type != 'line':
                    continue
                accounts = []
                if not line.analytic_accounts:
                    # Create missing selection
                    selection_id = selection_obj.create(cursor, 0, {},
                            context=context)
                    self.write(cursor, user, line.id, {
                        'analytic_accounts': selection_id,
                        }, context=context)
                    line = self.browse(cursor, user, line.id, context=context)
                for account in line.analytic_accounts.accounts:
                    if account.root.id in selection_vals:
                        value = selection_vals[account.root.id]
                        if value:
                            accounts.append(value)
                    else:
                        accounts.append(account.id)
                for account_id in selection_vals.values():
                    if account_id \
                            and account_id not in accounts:
                        accounts.append(account_id)
                selection_obj.write(cursor, user, line.analytic_accounts.id, {
                    'accounts': [('set', accounts)],
                    }, context=context)
        return super(InvoiceLine, self).write(cursor, user, ids, vals,
                context=context)

    def delete(self, cursor, user, ids, context=None):
        selection_obj = self.pool.get('analytic_account.account.selection')

        if isinstance(ids, (int, long)):
            ids = [ids]
        selection_ids = []
        lines = self.browse(cursor, user, ids, context=context)
        for line in lines:
            if line.analytic_accounts:
                selection_ids.append(line.analytic_accounts.id)

        res = super(InvoiceLine, self).delete(cursor, user, ids,
                context=context)
        selection_obj.delete(cursor, user, selection_ids, context=context)
        return res

    def copy(self, cursor, user, ids, default=None, context=None):
        selection_obj = self.pool.get('analytic_account.account.selection')

        new_ids = super(InvoiceLine, self).copy(cursor, user, ids,
                default=default, context=context)

        int_id = False
        if isinstance(new_ids, (int, long)):
            int_id = True
            new_ids = [new_ids]

        for line in self.browse(cursor, user, new_ids, context=context):
            if line.analytic_accounts:
                selection_id = selection_obj.copy(cursor, user,
                        line.analytic_accounts.id, context=context)
                self.write(cursor, user, line.id, {
                    'analytic_accounts': selection_id,
                    }, context=context)

        if int_id:
            return new_ids[0]
        return new_ids

    def get_move_line(self, cursor, user, line, context=None):
        res = super(InvoiceLine, self).get_move_line(cursor, user, line,
                context=context)
        if line.analytic_accounts and line.analytic_accounts.accounts:
            res['analytic_lines'] = []
            for account in line.analytic_accounts.accounts:
                vals = {}
                vals['name'] = line.description
                vals['debit'] = res['debit']
                vals['credit'] = res['credit']
                vals['account'] = account.id
                vals['journal'] = line.invoice.journal.id
                vals['date'] = line.invoice.invoice_date
                vals['reference'] = line.invoice.reference
                vals['party'] = line.invoice.party.id
                res['analytic_lines'].append(('create', vals))
        return res

InvoiceLine()


class Account(ModelSQL, ModelView):
    _name = 'analytic_account.account'

    def delete(self, cursor, user, ids, context=None):
        account_invoice_line_obj = self.pool.get('account.invoice.line')
        res = super(Account, self).delete(cursor, user, ids, context=context)
        # Restart the cache on the fields_view_get method of account.invoice.line
        account_invoice_line_obj.fields_view_get(cursor.dbname)
        return res

    def create(self, cursor, user, vals, context=None):
        account_invoice_line_obj = self.pool.get('account.invoice.line')
        res = super(Account, self).create(cursor, user, vals, context=context)
        # Restart the cache on the fields_view_get method of account.invoice.line
        account_invoice_line_obj.fields_view_get(cursor.dbname)
        return res

    def write(self, cursor, user, ids, vals, context=None):
        account_invoice_line_obj = self.pool.get('account.invoice.line')
        res = super(Account, self).write(cursor, user, ids, vals,
                context=context)
        # Restart the cache on the fields_view_get method of account.invoice.line
        account_invoice_line_obj.fields_view_get(cursor.dbname)
        return res

Account()