forked from OCA/account-financial-tools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
account_deposit.py
340 lines (313 loc) · 14.3 KB
/
account_deposit.py
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# -*- coding: utf-8 -*-
###############################################################################
#
# account_check_deposit for Odoo/OpenERP
# Copyright (C) 2012-2015 Akretion (http://www.akretion.com/)
# @author: Benoît GUILLOT <[email protected]>
# @author: Chafique DELLI <[email protected]>
# @author: Alexis de Lattre <[email protected]>
#
# 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/>.
#
###############################################################################
from openerp.osv import fields, orm
from openerp.tools.translate import _
import openerp.addons.decimal_precision as dp
class account_check_deposit(orm.Model):
_name = "account.check.deposit"
_description = "Account Check Deposit"
_order = 'deposit_date desc'
def _compute_check_deposit(self, cr, uid, ids, name, args, context=None):
res = {}
for deposit in self.browse(cr, uid, ids, context=context):
total = 0.0
count = 0
reconcile = False
currency_none_same_company_id = False
if deposit.company_id.currency_id != deposit.currency_id:
currency_none_same_company_id = deposit.currency_id.id
for line in deposit.check_payment_ids:
count += 1
if currency_none_same_company_id:
total += line.amount_currency
else:
total += line.debit
if deposit.move_id:
for line in deposit.move_id.line_id:
if line.debit > 0 and line.reconcile_id:
reconcile = True
res[deposit.id] = {
'total_amount': total,
'is_reconcile': reconcile,
'currency_none_same_company_id': currency_none_same_company_id,
'check_count': count,
}
return res
_columns = {
'name': fields.char(
'Name', size=64, readonly=True),
'check_payment_ids': fields.one2many(
'account.move.line', 'check_deposit_id', 'Check Payments',
states={'done': [('readonly', '=', True)]}),
'deposit_date': fields.date(
'Deposit Date', required=True,
states={'done': [('readonly', '=', True)]}),
'journal_id': fields.many2one(
'account.journal', 'Journal', domain=[('type', '=', 'bank')],
required=True, states={'done': [('readonly', '=', True)]}),
'journal_default_account_id': fields.related(
'journal_id', 'default_debit_account_id', type='many2one',
relation='account.account',
string='Default Debit Account of the Journal'),
'currency_id': fields.many2one(
'res.currency', 'Currency', required=True,
states={'done': [('readonly', '=', True)]}),
'currency_none_same_company_id': fields.function(
_compute_check_deposit, type='many2one',
relation='res.currency', multi='deposit',
string='Currency (False if same as company)'),
'state': fields.selection([
('draft', 'Draft'),
('done', 'Done'),
], 'Status', readonly=True),
'move_id': fields.many2one(
'account.move', 'Journal Entry', readonly=True),
'partner_bank_id': fields.many2one(
'res.partner.bank', 'Bank Account', required=True,
domain="[('company_id', '=', company_id)]",
states={'done': [('readonly', '=', True)]}),
'line_ids': fields.related(
'move_id', 'line_id', relation='account.move.line',
type='one2many', string='Lines', readonly=True),
'company_id': fields.many2one(
'res.company', 'Company', required=True,
change_default=True,
states={'done': [('readonly', '=', True)]}),
'total_amount': fields.function(
_compute_check_deposit, multi='deposit',
string="Total Amount", readonly=True,
type="float", digits_compute=dp.get_precision('Account')),
'check_count': fields.function(
_compute_check_deposit, multi='deposit', readonly=True,
string="Number of Checks", type="integer"),
'is_reconcile': fields.function(
_compute_check_deposit, multi='deposit', readonly=True,
string="Reconcile", type="boolean"),
}
_defaults = {
'name': '/',
'deposit_date': fields.date.context_today,
'state': 'draft',
'company_id': lambda self, cr, uid, c: self.pool['res.company'].
_company_default_get(cr, uid, 'account.check.deposit', context=c),
}
def _check_deposit(self, cr, uid, ids):
for deposit in self.browse(cr, uid, ids):
deposit_currency = deposit.currency_id
if deposit_currency == deposit.company_id.currency_id:
for line in deposit.check_payment_ids:
if line.currency_id:
raise orm.except_orm(
_('Error:'),
_("The check with amount %s and reference '%s' "
"is in currency %s but the deposit is in "
"currency %s.") % (
line.debit, line.ref or '',
line.currency_id.name,
deposit_currency.name))
else:
for line in deposit.check_payment_ids:
if line.currency_id != deposit_currency:
raise orm.except_orm(
_('Error:'),
_("The check with amount %s and reference '%s' "
"is in currency %s but the deposit is in "
"currency %s.") % (
line.debit, line.ref or '',
line.currency_id.name,
deposit_currency.name))
return True
_constraints = [(
_check_deposit,
"All the checks of the deposit must be in the currency of the deposit",
['currency_id', 'check_payment_ids', 'company_id']
)]
def unlink(self, cr, uid, ids, context=None):
for deposit in self.browse(cr, uid, ids, context=context):
if deposit.state == 'done':
raise orm.except_orm(
_('Error!'),
_("The deposit '%s' is in valid state, so you must "
"cancel it before deleting it.")
% deposit.name)
return super(account_check_deposit, self).unlink(
cr, uid, ids, context=context)
def backtodraft(self, cr, uid, ids, context=None):
for deposit in self.browse(cr, uid, ids, context=context):
if deposit.move_id:
# It will raise here if journal_id.update_posted = False
deposit.move_id.button_cancel()
for line in deposit.check_payment_ids:
if line.reconcile_id:
line.reconcile_id.unlink()
deposit.move_id.unlink()
deposit.write({'state': 'draft'})
return True
def create(self, cr, uid, vals, context=None):
if vals.get('name', '/') == '/':
vals['name'] = self.pool['ir.sequence'].\
next_by_code(cr, uid, 'account.check.deposit')
return super(account_check_deposit, self).\
create(cr, uid, vals, context=context)
def _prepare_account_move_vals(self, cr, uid, deposit, context=None):
date = deposit.deposit_date
period_obj = self.pool['account.period']
period_ids = period_obj.find(cr, uid, dt=date, context=context)
# period_ids will always have a value, cf the code of find()
move_vals = {
'journal_id': deposit.journal_id.id,
'date': date,
'period_id': period_ids[0],
'name': _('Check Deposit %s') % deposit.name,
'ref': deposit.name,
}
return move_vals
def _prepare_move_line_vals(
self, cr, uid, line, context=None):
assert (line.debit > 0), 'Debit must have a value'
return {
'name': _('Check Deposit - Ref. Check %s') % line.ref,
'credit': line.debit,
'debit': 0.0,
'account_id': line.account_id.id,
'partner_id': line.partner_id.id,
'currency_id': line.currency_id.id or False,
'amount_currency': line.amount_currency * -1,
}
def _prepare_counterpart_move_lines_vals(
self, cr, uid, deposit, total_debit, total_amount_currency,
context=None):
return {
'name': _('Check Deposit %s') % deposit.name,
'debit': total_debit,
'credit': 0.0,
'account_id': deposit.company_id.check_deposit_account_id.id,
'partner_id': False,
'currency_id': deposit.currency_none_same_company_id.id or False,
'amount_currency': total_amount_currency,
}
def validate_deposit(self, cr, uid, ids, context=None):
am_obj = self.pool['account.move']
aml_obj = self.pool['account.move.line']
if context is None:
context = {}
for deposit in self.browse(cr, uid, ids, context=context):
move_vals = self._prepare_account_move_vals(
cr, uid, deposit, context=context)
context['journal_id'] = move_vals['journal_id']
context['period_id'] = move_vals['period_id']
move_id = am_obj.create(cr, uid, move_vals, context=context)
total_debit = 0.0
total_amount_currency = 0.0
to_reconcile_line_ids = []
for line in deposit.check_payment_ids:
total_debit += line.debit
total_amount_currency += line.amount_currency
line_vals = self._prepare_move_line_vals(
cr, uid, line, context=context)
line_vals['move_id'] = move_id
move_line_id = aml_obj.create(
cr, uid, line_vals, context=context)
to_reconcile_line_ids.append([line.id, move_line_id])
# Create counter-part
if not deposit.company_id.check_deposit_account_id:
raise orm.except_orm(
_('Configuration Error:'),
_("Missing Account for Check Deposits on the "
"company '%s'.") % deposit.company_id.name)
counter_vals = self._prepare_counterpart_move_lines_vals(
cr, uid, deposit, total_debit, total_amount_currency,
context=context)
counter_vals['move_id'] = move_id
aml_obj.create(cr, uid, counter_vals, context=context)
am_obj.post(cr, uid, [move_id], context=context)
deposit.write({'state': 'done', 'move_id': move_id})
# We have to reconcile after post()
for reconcile_line_ids in to_reconcile_line_ids:
aml_obj.reconcile(
cr, uid, reconcile_line_ids, context=context)
return True
def onchange_company_id(
self, cr, uid, ids, company_id, currency_id, context=None):
vals = {}
if company_id:
company = self.pool['res.company'].browse(
cr, uid, company_id, context=context)
if currency_id:
if company.currency_id.id == currency_id:
vals['currency_none_same_company_id'] = False
else:
vals['currency_none_same_company_id'] = currency_id
partner_bank_ids = self.pool['res.partner.bank'].search(
cr, uid, [('company_id', '=', company_id)], context=context)
if len(partner_bank_ids) == 1:
vals['partner_bank_id'] = partner_bank_ids[0]
else:
vals['currency_none_same_company_id'] = False
vals['partner_bank_id'] = False
return {'value': vals}
def onchange_journal_id(self, cr, uid, ids, journal_id, context=None):
vals = {}
if journal_id:
journal = self.pool['account.journal'].browse(
cr, uid, journal_id, context=context)
vals['journal_default_account_id'] = \
journal.default_debit_account_id.id
if journal.currency:
vals['currency_id'] = journal.currency.id
else:
vals['currency_id'] = journal.company_id.currency_id.id
else:
vals['journal_default_account_id'] = False
return {'value': vals}
def onchange_currency_id(
self, cr, uid, ids, currency_id, company_id, context=None):
vals = {}
if currency_id and company_id:
company = self.pool['res.company'].browse(
cr, uid, company_id, context=context)
if company.currency_id.id == currency_id:
vals['currency_none_same_company_id'] = False
else:
vals['currency_none_same_company_id'] = currency_id
else:
vals['currency_none_same_company_id'] = False
return {'value': vals}
class account_move_line(orm.Model):
_inherit = "account.move.line"
_columns = {
'check_deposit_id': fields.many2one(
'account.check.deposit',
'Check Deposit'),
}
class res_company(orm.Model):
_inherit = 'res.company'
_columns = {
'check_deposit_account_id': fields.many2one(
'account.account', 'Account for Check Deposits',
domain=[
('type', '<>', 'view'),
('type', '<>', 'closed'),
('reconcile', '=', True)]),
}