~unifield-team/unifield-wm/us-826

« back to all changes in this revision

Viewing changes to analytic_distribution/account_bank_statement.py

  • Committer: jf
  • Date: 2012-06-13 12:43:21 UTC
  • mfrom: (827.5.11 uf-635)
  • Revision ID: jf@tempo4-20120613124321-2b8cwgl86gyy2tb7
UF-635 [DEV] Documents workflow: Graphic representation
lp:~unifield-team/unifield-wm/uf-635 revno 838

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
#-*- encoding:utf-8 -*-
 
3
##############################################################################
 
4
#
 
5
#    OpenERP, Open Source Management Solution
 
6
#    Copyright (C) 2011 MSF, TeMPO Consulting.
 
7
#
 
8
#    This program is free software: you can redistribute it and/or modify
 
9
#    it under the terms of the GNU Affero General Public License as
 
10
#    published by the Free Software Foundation, either version 3 of the
 
11
#    License, or (at your option) any later version.
 
12
#
 
13
#    This program is distributed in the hope that it will be useful,
 
14
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
#    GNU Affero General Public License for more details.
 
17
#
 
18
#    You should have received a copy of the GNU Affero General Public License
 
19
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
#
 
21
##############################################################################
 
22
from osv import fields, osv
 
23
import tools
 
24
from tools.translate import _
 
25
 
 
26
class account_bank_statement_line(osv.osv):
 
27
    _inherit = "account.bank.statement.line"
 
28
    _name = "account.bank.statement.line"
 
29
 
 
30
    def _display_analytic_button(self, cr, uid, ids, name, args, context=None):
 
31
        """
 
32
        Return True for all element that correspond to some criteria:
 
33
         - The entry state is draft
 
34
         - The account is an expense account
 
35
        """
 
36
        res = {}
 
37
        for absl in self.browse(cr, uid, ids, context=context):
 
38
            res[absl.id] = True
 
39
            # False if st_line is hard posted
 
40
            if absl.state == 'hard':
 
41
                res[absl.id] = False
 
42
            # False if account not an expense account
 
43
            if absl.account_id.user_type.code not in ['expense']:
 
44
                res[absl.id] = False
 
45
        return res
 
46
 
 
47
    _columns = {
 
48
        'analytic_distribution_id': fields.many2one('analytic.distribution', 'Analytic Distribution'),
 
49
        'display_analytic_button': fields.function(_display_analytic_button, method=True, string='Display analytic button?', type='boolean', readonly=True, 
 
50
            help="This informs system that we can display or not an analytic button", store=False),
 
51
    }
 
52
 
 
53
    _defaults = {
 
54
        'display_analytic_button': lambda *a: True,
 
55
    }
 
56
 
 
57
    def button_analytic_distribution(self, cr, uid, ids, context=None):
 
58
        """
 
59
        Launch analytic distribution wizard from a statement line
 
60
        """
 
61
        # Some verifications
 
62
        if not context:
 
63
            context = {}
 
64
        if isinstance(ids, (int, long)):
 
65
            ids = [ids]
 
66
        # Prepare some values
 
67
        absl = self.browse(cr, uid, ids[0], context=context)
 
68
        amount = absl.amount * -1 or 0.0
 
69
        # Search elements for currency
 
70
        company_currency = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.currency_id.id
 
71
        currency = absl.statement_id.journal_id.currency and absl.statement_id.journal_id.currency.id or company_currency
 
72
        # Get analytic distribution id from this line
 
73
        distrib_id = absl.analytic_distribution_id and absl.analytic_distribution_id.id or False
 
74
        # Prepare values for wizard
 
75
        vals = {
 
76
            'total_amount': amount,
 
77
            'register_line_id': absl.id,
 
78
            'currency_id': currency or False,
 
79
            'state': 'dispatch',
 
80
            'account_id': absl.account_id and absl.account_id.id or False,
 
81
        }
 
82
        if distrib_id:
 
83
            vals.update({'distribution_id': distrib_id,})
 
84
        # Create the wizard
 
85
        wiz_obj = self.pool.get('analytic.distribution.wizard')
 
86
        wiz_id = wiz_obj.create(cr, uid, vals, context=context)
 
87
        # Update some context values
 
88
        context.update({
 
89
            'active_id': ids[0],
 
90
            'active_ids': ids,
 
91
        })
 
92
        # Open it!
 
93
        return {
 
94
                'name': 'Analytic distribution',
 
95
                'type': 'ir.actions.act_window',
 
96
                'res_model': 'analytic.distribution.wizard',
 
97
                'view_type': 'form',
 
98
                'view_mode': 'form',
 
99
                'target': 'new',
 
100
                'res_id': [wiz_id],
 
101
                'context': context,
 
102
        }
 
103
 
 
104
account_bank_statement_line()
 
105
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: