-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathaccount_move.py
More file actions
281 lines (241 loc) · 10.7 KB
/
Copy pathaccount_move.py
File metadata and controls
281 lines (241 loc) · 10.7 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2013-2015 XCG Consulting (http://www.xcg-consulting.fr/)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import logging
from openerp import SUPERUSER_ID
from openerp.osv import fields, expression, osv
from openerp.tools.translate import _
import yaml
_logger = logging.getLogger(__name__)
# account.move.line fields that don't become read-only when the account.move
# object is posted.
list_readonly_loose = [
'date_maturity'
]
list_noreadonly = [
'a1_id', 'a2_id', 'a3_id', 'a4_id', 'a5_id'
]
list_readonly_condition = (
'[["move_state", "=", "posted"]]'
)
list_readonly_condition_loose = (
'[["move_state", "=", "posted"], '
'["reconcile_id", "!=", false]]'
)
class account_move(osv.Model):
_name = 'account.move'
_inherit = [
'account.move',
'mail.thread',
]
def _links_get(self, cr, uid, context=None):
"""Gets links value for reference field"""
obj = self.pool.get('res.request.link')
ids = obj.search(cr, uid, [])
res = obj.read(cr, uid, ids, ['object', 'name'], context)
return [(r['object'], r['name']) for r in res]
_columns = {
# Redefine this field to remove the read-only constraint; it is however
# carefully propagated to line fields via attributes inserted from the
# fields_view_get function (except for fields we want to keep
# modifiable).
'line_id': fields.one2many(
'account.move.line',
'move_id',
'Entries'
),
'object_reference': fields.reference(
u"Linked Object",
selection=_links_get,
size=128,
)
}
def _analysis_control(self, cr, uid, ids, context=None):
"""This controls the account.move.line analysis dimensions settings
set on account.account It will perform this only when attempting to
post a complete move and will compile all errors coming from move
lines in a single message
"""
# move_dict = {}
ans_obj = self.pool['analytic.structure']
ans_dict = ans_obj.get_dimensions_names(
cr, uid, 'account_move_line', context=context
)
required_lines = {}
clear_lines = {key: [] for key in ans_dict.iterkeys()}
for move in self.browse(cr, uid, ids, context=context):
for aml in move.line_id:
required_field_list = []
for ordering, name in ans_dict.iteritems():
control_field = aml.account_id['t{0}_ctl'.format(ordering)]
analytic_field = aml['a{0}_id'.format(ordering)]
if control_field == '1' and not analytic_field:
required_field_list.append(name)
elif control_field == '3' and analytic_field:
clear_lines[ordering].append(aml.id)
if required_field_list:
required_lines[aml.name] = required_field_list
if required_lines:
msg_analysis = _(
"Unable to post! The following analysis codes are mandatory:"
)
msg_analysis += '\n'
msg_analysis += yaml.safe_dump(required_lines)
raise osv.except_osv(_('Error!'), msg_analysis)
if clear_lines:
aml_obj = self.pool['account.move.line']
for ordering, lines in clear_lines.iteritems():
if lines:
vals = {'a{0}_id'.format(ordering): False}
aml_obj.write(cr, uid, lines, vals, context=context)
def post(self, cr, uid, ids, context=None):
"""override the post method so all lines can be check against analysis
controls
"""
self._analysis_control(cr, uid, ids, context=context)
return super(account_move, self).post(cr, uid, ids, context=context)
def fields_view_get(self, cr, uid, view_id=None, view_type='form',
context=None, toolbar=False, submenu=False):
"""We display analysis codes on the account.move form inserting them
in the one2many field containing account move lines
"""
if context is None:
context = {}
res = super(account_move,
self).fields_view_get(cr, uid, view_id=view_id,
view_type=view_type,
context=context,
toolbar=toolbar,
submenu=False)
ans_obj = self.pool['analytic.structure']
line_id_field = res['fields'].get('line_id')
ans_obj.analytic_fields_subview_get(
cr, uid, 'account_move_line', line_id_field, context=context
)
return res
def multi_validate(self, cr, uid, ids, context=None):
"""Validate the selected accounting entries.
"""
if context is None:
context = {}
ids = context.get('active_ids', ids)
self.button_validate(cr, uid, ids, context=context)
# Don't return anything so it doesn't crash when calling from a form
# view.
def _check_duplicate_ref_on_account_move_same_account(
self, cr, uid, ids, context=None
):
""" Check that if the company does not allow duplicate ref/account.
That it is not
"""
am_brl = self.browse(cr, uid, ids, context)
for am_br in am_brl:
if (
not am_br.company_id.
allow_duplicate_ref_on_account_move_same_account
):
# check that there is no other line with same
# partner but different ref on the move
# Use SUPERUSER_ID to be sure to find all
am_same_ref_ids = self.search(
cr, SUPERUSER_ID,
[('ref', '=', am_br.ref), ('id', '!=', am_br.id)],
context=context)
if am_same_ref_ids:
_logger.debug("Found ref %s in other account move"
% am_br.ref)
# make a set of the values for easy comparison
partners = {line.partner_id.id for line in am_br.line_id}
_logger.debug(partners)
am_same_ref_brl = self.browse(
cr, SUPERUSER_ID, am_same_ref_ids, context)
# check that partner is not the same if ref are
for am_same_ref_br in am_same_ref_brl:
for aml_br in am_same_ref_br.line_id:
if aml_br.partner_id.id in partners:
_logger.debug(
"Found same partner in move %(move)d line "
"%(line)d" % {
'move': am_br.id, 'line': aml_br.id})
return False
else:
_logger.debug("Found different partner "
"%(partner)s in move %(move)d line "
"%(line)d" % {
'move': am_br.id,
'line': aml_br.id,
'partner':aml_br.partner_id.id})
else:
_logger.debug("Ref %s unique in account move"
% am_br.ref)
return True
_constraints = [
(
_check_duplicate_ref_on_account_move_same_account,
"Duplicate reference on same partner with that account on this journal",
['ref']
),
]
class mail_message(osv.Model):
_inherit = 'mail.message'
def message_read(
self, cr, uid, ids=None, domain=None, message_unload_ids=None,
thread_level=0, context=None, parent_id=False, limit=None
):
"""Override this function to include account.move.line notifications
within account.move notification lists.
:todo This applies to every mail.message object; maybe find a better
solution that doesn't involve modifying objects globally.
"""
# Example domain: [['model', '=', 'account.move'], ['res_id', '=', 7]]
# Avoid recursion...
if domain and ['model', '=', 'account.move.line'] not in domain:
am_obj = self.pool['account.move']
line_domain = []
# Look for a "res_id = X" domain part.
for domain_index, domain_part in enumerate(domain):
if (domain_index < len(domain) - 1 and
domain_part == ['model', '=', 'account.move']
):
next_part = domain[domain_index + 1]
if next_part[0] == 'res_id' and next_part[1] == '=':
move_id = next_part[2]
line_ids = am_obj.read(
cr, uid,
move_id,
['line_id'],
context=context
)['line_id']
line_domain = [
('model', '=', 'account.move.line'),
('res_id', 'in', line_ids)
]
if line_domain:
domain = expression.OR([
expression.normalize_domain(line_domain),
expression.normalize_domain(domain)
])
# Make sure our domain is applied. When "ids" is set, the
# domain is ignored.
ids = None
return super(mail_message, self).message_read(
cr, uid, ids=ids, domain=domain,
message_unload_ids=message_unload_ids, thread_level=thread_level,
context=context, parent_id=parent_id, limit=limit
)