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