1
# -*- encoding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
5
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
8
# This program is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License as published by
10
# the Free Software Foundation, either version 3 of the License, or
11
# (at your option) any later version.
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
18
# You should have received a copy of the GNU General Public License
19
# along with this program. If not, see <http://www.gnu.org/licenses/>.
21
##############################################################################
25
from osv import fields, osv
26
from tools.translate import _
29
from mx.DateTime import RelativeDateTime, now, DateTime, localtime
33
class account_move_line(osv.osv):
34
_inherit = "account.move.line"
40
def reconcile(self, cr, uid, ids, type='auto', writeoff_acc_id=False, writeoff_period_id=False, writeoff_journal_id=False, context={}):
41
id_set = ','.join(map(str, ids))
43
lines = self.browse(cr, uid, ids, context=context)
44
unrec_lines = filter(lambda x: not x['reconcile_id'], lines)
49
for line in unrec_lines:
50
if line.state <> 'valid':
51
raise osv.except_osv(_('Error'),
52
_('Entry "%s" is not valid !') % line.name)
53
credit += line['credit']
54
debit += line['debit']
55
currency += line['amount_currency'] or 0.0
56
account_id = line['account_id']['id']
57
partner_id = (line['partner_id'] and line['partner_id']['id']) or False
58
writeoff = debit - credit
59
# Ifdate_p in context => take this date
60
if context.has_key('date_p') and context['date_p']:
61
date=context['date_p']
63
date = time.strftime('%Y-%m-%d')
65
cr.execute('SELECT account_id, reconcile_id \
66
FROM account_move_line \
67
WHERE id IN ('+id_set+') \
68
GROUP BY account_id,reconcile_id')
70
#TODO: move this check to a constraint in the account_move_reconcile object
71
if (len(r) != 1) and not context.get('fy_closing', False):
72
raise osv.except_osv(_('Error'), _('Entries are not of the same account or already reconciled ! '))
74
raise osv.except_osv(_('Error'), _('Entry is already reconciled'))
75
account = self.pool.get('account.account').browse(cr, uid, account_id, context=context)
76
if not context.get('fy_closing', False) and not account.reconcile:
77
raise osv.except_osv(_('Error'), _('The account is not defined to be reconciled !'))
79
raise osv.except_osv(_('Error'), _('Some entries are already reconciled !'))
81
if (not self.pool.get('res.currency').is_zero(cr, uid, account.company_id.currency_id, writeoff)) or \
82
(account.currency_id and (not self.pool.get('res.currency').is_zero(cr, uid, account.currency_id, currency))):
83
if not writeoff_acc_id:
84
raise osv.except_osv(_('Warning'), _('You have to provide an account for the write off entry !'))
88
self_credit = writeoff
94
self_debit = -writeoff
96
# If comment exist in context, take it
97
if 'comment' in context and context['comment']:
98
label=context['comment']
107
'credit':self_credit,
108
'account_id':account_id,
110
'partner_id':partner_id,
111
'currency_id': account.currency_id.id or False,
112
'amount_currency': account.currency_id.id and -currency or 0.0
119
'account_id':writeoff_acc_id,
121
'partner_id':partner_id
125
writeoff_move_id = self.pool.get('account.move').create(cr, uid, {
126
'period_id': writeoff_period_id,
127
'journal_id': writeoff_journal_id,
130
'line_id': writeoff_lines
133
writeoff_line_ids = self.search(cr, uid, [('move_id', '=', writeoff_move_id), ('account_id', '=', account_id)])
134
ids += writeoff_line_ids
136
r_id = self.pool.get('account.move.reconcile').create(cr, uid, {
139
'line_id': map(lambda x: (4,x,False), ids),
140
'line_partial_ids': map(lambda x: (3,x,False), ids)
142
wf_service = netsvc.LocalService("workflow")
143
# the id of the move.reconcile is written in the move.line (self) by the create method above
144
# because of the way the line_id are defined: (4, x, False)
146
wf_service.trg_trigger(uid, 'account.move.line', id, cr)
151
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: