[IMP]account:string chaged from credit note to refund
[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 import 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         ids = period_obj.find(cr, uid, dt=date, context=context)
88         if ids:
89             period_id = ids[0]
90         account_move_line_obj.reconcile(cr, uid, context['active_ids'], 'manual', account_id,
91                                         period_id, journal_id, context=context)
92         return {'type': 'ir.actions.act_window_close'}
93
94 account_move_line_reconcile()
95
96 class account_move_line_reconcile_writeoff(osv.osv_memory):
97     """
98     It opens the write off wizard form, in that user can define the journal, account, analytic account for reconcile
99     """
100     _name = 'account.move.line.reconcile.writeoff'
101     _description = 'Account move line reconcile (writeoff)'
102     _columns = {
103         'journal_id': fields.many2one('account.journal','Write-Off Journal', required=True),
104         'writeoff_acc_id': fields.many2one('account.account','Write-Off account', required=True),
105         'date_p': fields.date('Date'),
106         'comment': fields.char('Comment', size= 64, required=True),
107         'analytic_id': fields.many2one('account.analytic.account', 'Analytic Account', domain=[('parent_id', '!=', False)]),
108     }
109     _defaults = {
110         'date_p': lambda *a: time.strftime('%Y-%m-%d'),
111         'comment': 'Write-off',
112     }
113
114     def trans_rec_addendum(self, cr, uid, ids, context=None):
115         mod_obj = self.pool.get('ir.model.data')
116         if context is None:
117             context = {}
118         model_data_ids = mod_obj.search(cr, uid,[('model','=','ir.ui.view'),('name','=','account_move_line_reconcile_writeoff')], context=context)
119         resource_id = mod_obj.read(cr, uid, model_data_ids, fields=['res_id'], context=context)[0]['res_id']
120         return {
121             'name': _('Reconcile Writeoff'),
122             'context': context,
123             'view_type': 'form',
124             'view_mode': 'form',
125             'res_model': 'account.move.line.reconcile.writeoff',
126             'views': [(resource_id,'form')],
127             'type': 'ir.actions.act_window',
128             'target': 'new',
129         }
130
131     def trans_rec_reconcile_partial(self, cr, uid, ids, context=None):
132         account_move_line_obj = self.pool.get('account.move.line')
133         if context is None:
134             context = {}
135         account_move_line_obj.reconcile_partial(cr, uid, context['active_ids'], 'manual', context=context)
136         return {'type': 'ir.actions.act_window_close'}
137
138     def trans_rec_reconcile(self, cr, uid, ids, context=None):
139         account_move_line_obj = self.pool.get('account.move.line')
140         period_obj = self.pool.get('account.period')
141         if context is None:
142             context = {}
143         data = self.read(cr, uid, ids,context=context)[0]
144         account_id = data['writeoff_acc_id'][0]
145         context['date_p'] = data['date_p']
146         journal_id = data['journal_id'][0]
147         context['comment'] = data['comment']
148         if data['analytic_id']:
149             context['analytic_id'] = data['analytic_id'][0]
150         if context['date_p']:
151             date = context['date_p']
152
153         ids = period_obj.find(cr, uid, dt=date, context=context)
154         if ids:
155             period_id = ids[0]
156
157         account_move_line_obj.reconcile(cr, uid, context['active_ids'], 'manual', account_id,
158                 period_id, journal_id, context=context)
159         return {'type': 'ir.actions.act_window_close'}
160
161 account_move_line_reconcile_writeoff()
162
163 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: