Launchpad automatic translations update.
[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 import openerp.addons.decimal_precision as dp
27
28 class account_move_line_reconcile(osv.osv_memory):
29     """
30     Account move line reconcile wizard, it checks for the write off the reconcile entry or directly reconcile.
31     """
32     _name = 'account.move.line.reconcile'
33     _description = 'Account move line reconcile'
34     _columns = {
35         'trans_nbr': fields.integer('# of Transaction', readonly=True),
36         'credit': fields.float('Credit amount', readonly=True, digits_compute=dp.get_precision('Account')),
37         'debit': fields.float('Debit amount', readonly=True, digits_compute=dp.get_precision('Account')),
38         'writeoff': fields.float('Write-Off amount', readonly=True, digits_compute=dp.get_precision('Account')),
39     }
40
41     def default_get(self, cr, uid, fields, context=None):
42         res = super(account_move_line_reconcile, self).default_get(cr, uid, fields, context=context)
43         data = self.trans_rec_get(cr, uid, context['active_ids'], context)
44         if 'trans_nbr' in fields:
45             res.update({'trans_nbr':data['trans_nbr']})
46         if 'credit' in fields:
47             res.update({'credit':data['credit']})
48         if 'debit' in fields:
49             res.update({'debit':data['debit']})
50         if 'writeoff' in fields:
51             res.update({'writeoff':data['writeoff']})
52         return res
53
54     def trans_rec_get(self, cr, uid, ids, context=None):
55         account_move_line_obj = self.pool.get('account.move.line')
56         if context is None:
57             context = {}
58         credit = debit = 0
59         account_id = False
60         count = 0
61         for line in account_move_line_obj.browse(cr, uid, context['active_ids'], context=context):
62             if not line.reconcile_id and not line.reconcile_id.id:
63                 count += 1
64                 credit += line.credit
65                 debit += line.debit
66                 account_id = line.account_id.id
67         return {'trans_nbr': count, 'account_id': account_id, 'credit': credit, 'debit': debit, 'writeoff': debit - credit}
68
69     def trans_rec_addendum_writeoff(self, cr, uid, ids, context=None):
70         return self.pool.get('account.move.line.reconcile.writeoff').trans_rec_addendum(cr, uid, ids, context)
71
72     def trans_rec_reconcile_partial_reconcile(self, cr, uid, ids, context=None):
73         return self.pool.get('account.move.line.reconcile.writeoff').trans_rec_reconcile_partial(cr, uid, ids, context)
74
75     def trans_rec_reconcile_full(self, cr, uid, ids, context=None):
76         account_move_line_obj = self.pool.get('account.move.line')
77         period_obj = self.pool.get('account.period')
78         date = False
79         period_id = False
80         journal_id= False
81         account_id = False
82
83         if context is None:
84             context = {}
85
86         date = time.strftime('%Y-%m-%d')
87         ctx = dict(context or {}, account_period_prefer_normal=True)
88         ids = period_obj.find(cr, uid, dt=date, context=ctx)
89         if ids:
90             period_id = ids[0]
91         account_move_line_obj.reconcile(cr, uid, context['active_ids'], 'manual', account_id,
92                                         period_id, journal_id, context=context)
93         return {'type': 'ir.actions.act_window_close'}
94
95 account_move_line_reconcile()
96
97 class account_move_line_reconcile_writeoff(osv.osv_memory):
98     """
99     It opens the write off wizard form, in that user can define the journal, account, analytic account for reconcile
100     """
101     _name = 'account.move.line.reconcile.writeoff'
102     _description = 'Account move line reconcile (writeoff)'
103     _columns = {
104         'journal_id': fields.many2one('account.journal','Write-Off Journal', required=True),
105         'writeoff_acc_id': fields.many2one('account.account','Write-Off account', required=True),
106         'date_p': fields.date('Date'),
107         'comment': fields.char('Comment', size= 64, required=True),
108         'analytic_id': fields.many2one('account.analytic.account', 'Analytic Account', domain=[('parent_id', '!=', False)]),
109     }
110     _defaults = {
111         'date_p': lambda *a: time.strftime('%Y-%m-%d'),
112         'comment': 'Write-off',
113     }
114
115     def trans_rec_addendum(self, cr, uid, ids, context=None):
116         mod_obj = self.pool.get('ir.model.data')
117         if context is None:
118             context = {}
119         model_data_ids = mod_obj.search(cr, uid,[('model','=','ir.ui.view'),('name','=','account_move_line_reconcile_writeoff')], context=context)
120         resource_id = mod_obj.read(cr, uid, model_data_ids, fields=['res_id'], context=context)[0]['res_id']
121         return {
122             'name': _('Reconcile Writeoff'),
123             'context': context,
124             'view_type': 'form',
125             'view_mode': 'form',
126             'res_model': 'account.move.line.reconcile.writeoff',
127             'views': [(resource_id,'form')],
128             'type': 'ir.actions.act_window',
129             'target': 'new',
130         }
131
132     def trans_rec_reconcile_partial(self, cr, uid, ids, context=None):
133         account_move_line_obj = self.pool.get('account.move.line')
134         if context is None:
135             context = {}
136         account_move_line_obj.reconcile_partial(cr, uid, context['active_ids'], 'manual', context=context)
137         return {'type': 'ir.actions.act_window_close'}
138
139     def trans_rec_reconcile(self, cr, uid, ids, context=None):
140         account_move_line_obj = self.pool.get('account.move.line')
141         period_obj = self.pool.get('account.period')
142         if context is None:
143             context = {}
144         data = self.read(cr, uid, ids,context=context)[0]
145         account_id = data['writeoff_acc_id'][0]
146         context['date_p'] = data['date_p']
147         journal_id = data['journal_id'][0]
148         context['comment'] = data['comment']
149         if data['analytic_id']:
150             context['analytic_id'] = data['analytic_id'][0]
151         if context['date_p']:
152             date = context['date_p']
153         context['account_period_prefer_normal'] = True
154         ids = period_obj.find(cr, uid, dt=date, context=context)
155         if ids:
156             period_id = ids[0]
157
158         account_move_line_obj.reconcile(cr, uid, context['active_ids'], 'manual', account_id,
159                 period_id, journal_id, context=context)
160         return {'type': 'ir.actions.act_window_close'}
161
162 account_move_line_reconcile_writeoff()
163
164 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: