~openerp-commiter/openobject-addons/extra-6.0

« back to all changes in this revision

Viewing changes to pxgo_account_admin_tools/revalidate_moves_wizard.py

  • Committer: Borja L.S.
  • Date: 2010-05-20 17:10:42 UTC
  • Revision ID: borjals@pexego.es-20100520171042-iot70m8r9b80uyko
[ADD] pxgo_account_admin_tools: New accounting wizards for administrators
  
  Account Adminitration Tools:
  
- Adds a wizard to import accounts from CSV files. This may be useful
  to import the initial accounts into OpenERP.

- Adds a wizard to import account moves from CSV files. This may be 
  useful to import the initial balance into OpenERP.

- Adds a wizard to set the receivable/payable account of the partners,
  in moves and invoices where a generic receivable/payable account
  was used instead.

- Adds a wizard to revalidate confirmed account moves so their analytic
  lines are regenerated. This may be used to fix the data after bugs 
  like https://bugs.launchpad.net/openobject-addons/+bug/582988
  The wizard also lets you find account moves missing their analytic 
  lines.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
# -*- encoding: utf-8 -*-
 
3
##############################################################################
 
4
#
 
5
#    OpenERP, Open Source Management Solution
 
6
#    Copyright (C) 2004-2009 Pexego Sistemas Informáticos. All Rights Reserved
 
7
#    $Id$
 
8
#
 
9
#    This program is free software: you can redistribute it and/or modify
 
10
#    it under the terms of the GNU General Public License as published by
 
11
#    the Free Software Foundation, either version 3 of the License, or
 
12
#    (at your option) any later version.
 
13
#
 
14
#    This program is distributed in the hope that it will be useful,
 
15
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
#    GNU General Public License for more details.
 
18
#
 
19
#    You should have received a copy of the GNU General Public License
 
20
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
21
#
 
22
##############################################################################
 
23
"""
 
24
Revalidate Account Moves Wizard
 
25
"""
 
26
__author__ = "Borja López Soilán (Pexego)"
 
27
 
 
28
import re
 
29
from osv import fields,osv
 
30
from tools.translate import _
 
31
 
 
32
class pxgo_revalidate_moves_wizard(osv.osv_memory):
 
33
    """
 
34
    Revalidate Account Moves Wizard
 
35
 
 
36
    Revalidates all the (already confirmed) moves, so their analytic lines
 
37
    are recomputed (to fix the data after problems like this:
 
38
    https://bugs.launchpad.net/openobject-addons/+bug/582988).
 
39
    """
 
40
    _name = "pxgo_revalidate_moves_wizard"
 
41
    _description = "Revalidate Account Moves Wizard"
 
42
 
 
43
    _columns = {
 
44
        'company_id': fields.many2one('res.company', 'Company', required=True, readonly=True),
 
45
        'period_ids': fields.many2many('account.period', 'pxgo_revalidate_moves_wizard_period_rel', 'wizard_id', 'period_id', "Periods"),
 
46
        'move_ids': fields.many2many('account.move', 'pxgo_revalidate_moves_wizard_moves_rel', 'wizard_id', 'move_id', 'Moves'),
 
47
        'state': fields.selection([('new','New'), ('ready', 'Ready'), ('done','Done')], 'Status', readonly=True),
 
48
    }
 
49
 
 
50
    _defaults = {
 
51
        'state': lambda *a: 'new',
 
52
    }
 
53
 
 
54
    def action_skip_new(self, cr, uid, ids, context=None):
 
55
        """
 
56
        Skips to the ready state.
 
57
        """
 
58
        for wiz in self.browse(cr, uid, ids, context):
 
59
            self.write(cr, uid, [wiz.id], { 'state': 'ready' })
 
60
        return True
 
61
 
 
62
    def action_find_moves_missing_analytic_lines(self, cr, uid, ids, context=None):
 
63
        """
 
64
        Finds account moves with missing analytic lines and adds them
 
65
        to the move_ids many2many field.
 
66
        """
 
67
        for wiz in self.browse(cr, uid, ids, context):
 
68
            period_ids = [period.id for period in wiz.period_ids]
 
69
            query = """
 
70
                    SELECT account_move_line.move_id FROM account_move_line
 
71
                    LEFT JOIN account_analytic_line
 
72
                    ON account_analytic_line.move_id = account_move_line.id
 
73
                    WHERE account_move_line.analytic_account_id IS NOT NULL AND account_analytic_line.id IS NULL
 
74
                    """
 
75
            periods_str = ','.join(map(str, period_ids))
 
76
            if period_ids:
 
77
                query += """      AND period_id IN (%s)""" % periods_str
 
78
                
 
79
            cr.execute(query)
 
80
            move_ids = filter(None, map(lambda x:x[0], cr.fetchall()))
 
81
            self.write(cr, uid, [wiz.id], { 
 
82
                    'move_ids': [(6, 0, move_ids)],
 
83
                    'state': 'ready'
 
84
                })
 
85
        return True
 
86
 
 
87
    def action_revalidate_moves(self, cr, uid, ids, context=None):
 
88
        """
 
89
        Calls the validate method of the account moves for each move in the
 
90
        move_ids many2many.
 
91
        """
 
92
        for wiz in self.browse(cr, uid, ids, context):
 
93
            for move in wiz.move_ids:
 
94
                # We validate the moves one by one to prevent problems
 
95
                self.pool.get('account.move').validate(cr, uid, [move.id], context)
 
96
            self.write(cr, uid, [wiz.id], { 'state': 'done' })
 
97
        return True
 
98
 
 
99
pxgo_revalidate_moves_wizard()
 
100
 
 
101