Merge branch 'master' of https://github.com/odoo/odoo
[odoo/odoo.git] / addons / account / wizard / account_reconcile.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 import time
23
24 from openerp.osv import fields, osv
25 from openerp.tools.translate import _
26 from openerp.tools.float_utils import float_round
27 import openerp.addons.decimal_precision as dp
28
29 class account_move_line_reconcile(osv.osv_memory):
30     """
31     Account move line reconcile wizard, it checks for the write off the reconcile entry or directly reconcile.
32     """
33     _name = 'account.move.line.reconcile'
34     _description = 'Account move line reconcile'
35     _columns = {
36         'trans_nbr': fields.integer('# of Transaction', readonly=True),
37         'credit': fields.float('Credit amount', readonly=True, digits_compute=dp.get_precision('Account')),
38         'debit': fields.float('Debit amount', readonly=True, digits_compute=dp.get_precision('Account')),
39         'writeoff': fields.float('Write-Off amount', readonly=True, digits_compute=dp.get_precision('Account')),
40     }
41
42     def default_get(self, cr, uid, fields, context=None):
43         res = super(account_move_line_reconcile, self).default_get(cr, uid, fields, context=context)
44         data = self.trans_rec_get(cr, uid, context['active_ids'], context)
45         if 'trans_nbr' in fields:
46             res.update({'trans_nbr':data['trans_nbr']})
47         if 'credit' in fields:
48             res.update({'credit':data['credit']})
49         if 'debit' in fields:
50             res.update({'debit':data['debit']})
51         if 'writeoff' in fields:
52             res.update({'writeoff':data['writeoff']})
53         return res
54
55     def trans_rec_get(self, cr, uid, ids, context=None):
56         account_move_line_obj = self.pool.get('account.move.line')
57         if context is None:
58             context = {}
59         credit = debit = 0
60         account_id = False
61         count = 0
62         for line in account_move_line_obj.browse(cr, uid, context['active_ids'], context=context):
63             if not line.reconcile_id and not line.reconcile_id.id:
64                 count += 1
65                 credit += line.credit
66                 debit += line.debit
67                 account_id = line.account_id.id
68         precision = self.pool['decimal.precision'].precision_get(cr, uid, 'Account')
69         writeoff = float_round(debit-credit, precision_digits=precision)
70         credit = float_round(credit, precision_digits=precision)
71         debit = float_round(debit, precision_digits=precision)
72         return {'trans_nbr': count, 'account_id': account_id, 'credit': credit, 'debit': debit, 'writeoff': writeoff}
73
74     def trans_rec_addendum_writeoff(self, cr, uid, ids, context=None):
75         return self.pool.get('account.move.line.reconcile.writeoff').trans_rec_addendum(cr, uid, ids, context)
76
77     def trans_rec_reconcile_partial_reconcile(self, cr, uid, ids, context=None):
78         return self.pool.get('account.move.line.reconcile.writeoff').trans_rec_reconcile_partial(cr, uid, ids, context)
79
80     def trans_rec_reconcile_full(self, cr, uid, ids, context=None):
81         account_move_line_obj = self.pool.get('account.move.line')
82         period_obj = self.pool.get('account.period')
83         date = False
84         period_id = False
85         journal_id= False
86         account_id = False
87
88         if context is None:
89             context = {}
90
91         date = time.strftime('%Y-%m-%d')
92         ids = period_obj.find(cr, uid, dt=date, context=context)
93         if ids:
94             period_id = ids[0]
95         account_move_line_obj.reconcile(cr, uid, context['active_ids'], 'manual', account_id,
96                                         period_id, journal_id, context=context)
97         return {'type': 'ir.actions.act_window_close'}
98
99
100 class account_move_line_reconcile_writeoff(osv.osv_memory):
101     """
102     It opens the write off wizard form, in that user can define the journal, account, analytic account for reconcile
103     """
104     _name = 'account.move.line.reconcile.writeoff'
105     _description = 'Account move line reconcile (writeoff)'
106     _columns = {
107         'journal_id': fields.many2one('account.journal','Write-Off Journal', required=True),
108         'writeoff_acc_id': fields.many2one('account.account','Write-Off account', required=True),
109         'date_p': fields.date('Date'),
110         'comment': fields.char('Comment', required=True),
111         'analytic_id': fields.many2one('account.analytic.account', 'Analytic Account', domain=[('parent_id', '!=', False)]),
112     }
113     _defaults = {
114         'date_p': lambda *a: time.strftime('%Y-%m-%d'),
115         'comment': _('Write-off'),
116     }
117
118     def trans_rec_addendum(self, cr, uid, ids, context=None):
119         mod_obj = self.pool.get('ir.model.data')
120         if context is None:
121             context = {}
122         model_data_ids = mod_obj.search(cr, uid,[('model','=','ir.ui.view'),('name','=','account_move_line_reconcile_writeoff')], context=context)
123         resource_id = mod_obj.read(cr, uid, model_data_ids, fields=['res_id'], context=context)[0]['res_id']
124         return {
125             'name': _('Reconcile Writeoff'),
126             'context': context,
127             'view_type': 'form',
128             'view_mode': 'form',
129             'res_model': 'account.move.line.reconcile.writeoff',
130             'views': [(resource_id,'form')],
131             'type': 'ir.actions.act_window',
132             'target': 'new',
133         }
134
135     def trans_rec_reconcile_partial(self, cr, uid, ids, context=None):
136         account_move_line_obj = self.pool.get('account.move.line')
137         if context is None:
138             context = {}
139         account_move_line_obj.reconcile_partial(cr, uid, context['active_ids'], 'manual', context=context)
140         return {'type': 'ir.actions.act_window_close'}
141
142     def trans_rec_reconcile(self, cr, uid, ids, context=None):
143         context = dict(context or {})
144         account_move_line_obj = self.pool.get('account.move.line')
145         period_obj = self.pool.get('account.period')
146         if context is None:
147             context = {}
148         data = self.read(cr, uid, ids,context=context)[0]
149         account_id = data['writeoff_acc_id'][0]
150         context['date_p'] = data['date_p']
151         journal_id = data['journal_id'][0]
152         context['comment'] = data['comment']
153         if data['analytic_id']:
154             context['analytic_id'] = data['analytic_id'][0]
155         if context['date_p']:
156             date = context['date_p']
157         ids = period_obj.find(cr, uid, dt=date, context=context)
158         if ids:
159             period_id = ids[0]
160
161         account_move_line_obj.reconcile(cr, uid, context['active_ids'], 'manual', account_id,
162                 period_id, journal_id, context=context)
163         return {'type': 'ir.actions.act_window_close'}
164
165
166 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: