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

« back to all changes in this revision

Viewing changes to funding_pool/account_bank_statement.py

  • Committer: Quentin THEURET
  • Date: 2011-12-12 08:02:59 UTC
  • mto: This revision was merged to the branch mainline in revision 724.
  • Revision ID: qt@tempo-consulting.fr-20111212080259-oul1f0g37hcpubyc
UF-641 [ADD] Added the empty purchase_followup module

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
#
21
21
##############################################################################
22
22
from osv import fields, osv
 
23
import tools
 
24
from tools.translate import _
23
25
 
24
26
class account_bank_statement_line(osv.osv):
25
27
    _inherit = "account.bank.statement.line"
26
28
    _name = "account.bank.statement.line"
27
29
 
28
 
    def _display_analytic_button(self, cr, uid, ids, name, args, context=None):
 
30
    def _display_analytic_button(self, cr, uid, ids, name, args, context={}):
29
31
        """
30
32
        Return True for all element that correspond to some criteria:
31
33
         - The entry state is draft
32
 
         - The account is analytic-a-holic
 
34
         - The account is an expense account
33
35
        """
34
36
        res = {}
35
37
        for absl in self.browse(cr, uid, ids, context=context):
37
39
            # False if st_line is hard posted
38
40
            if absl.state == 'hard':
39
41
                res[absl.id] = False
40
 
            # False if account not allocatable
41
 
            if not absl.account_id.is_analytic_addicted:
 
42
            # False if account not an expense account
 
43
            if absl.account_id.user_type.code not in ['expense']:
42
44
                res[absl.id] = False
43
45
        return res
44
46
 
45
 
    def _get_distribution_state(self, cr, uid, ids, name, args, context=None):
46
 
        """
47
 
        Get state of distribution:
48
 
         - if compatible with the line, then "valid"
49
 
         - if no distribution, then "none"
50
 
         - all other case are "invalid"
51
 
        """
52
 
        # Some verifications
53
 
        if not context:
54
 
            context = {}
55
 
        if isinstance(ids, (int, long)):
56
 
            ids = [ids]
57
 
        # Prepare some values
58
 
        res = {}
59
 
        # Browse all given lines
60
 
        for line in self.read(cr, uid, ids, ['analytic_distribution_id', 'account_id'], context=context):
61
 
            if not line.get('analytic_distribution_id', False):
62
 
                res[line.get('id')] = 'none'
63
 
                continue
64
 
            distribution_id = line.get('analytic_distribution_id')[0]
65
 
            account_id = line.get('account_id', [False])[0]
66
 
            res[line.get('id')] = self.pool.get('analytic.distribution')._get_distribution_state(cr, uid, distribution_id, False, account_id)
67
 
        return res
68
 
 
69
47
    _columns = {
70
48
        'analytic_distribution_id': fields.many2one('analytic.distribution', 'Analytic Distribution'),
71
 
        'display_analytic_button': fields.function(_display_analytic_button, method=True, string='Display analytic button?', type='boolean', readonly=True,
 
49
        'display_analytic_button': fields.function(_display_analytic_button, method=True, string='Display analytic button?', type='boolean', readonly=True, 
72
50
            help="This informs system that we can display or not an analytic button", store=False),
73
 
        'analytic_distribution_state': fields.function(_get_distribution_state, method=True, type='selection',
74
 
            selection=[('none', 'None'), ('valid', 'Valid'), ('invalid', 'Invalid')],
75
 
            string="Distribution state", help="Informs from distribution state among 'none', 'valid', 'invalid."),
76
51
    }
77
52
 
78
53
    _defaults = {
79
54
        'display_analytic_button': lambda *a: True,
80
55
    }
81
56
 
82
 
    def button_analytic_distribution(self, cr, uid, ids, context=None):
 
57
    def button_analytic_distribution(self, cr, uid, ids, context={}):
83
58
        """
84
59
        Launch analytic distribution wizard from a statement line
85
60
        """
86
 
        # Some verifications
87
61
        if not context:
88
62
            context = {}
89
63
        if isinstance(ids, (int, long)):
90
64
            ids = [ids]
91
 
        # Prepare some values
92
 
        absl = self.browse(cr, uid, ids[0], context=context)
93
 
        amount = absl.amount * -1 or 0.0
 
65
        # we get the analytical distribution object linked to this line
 
66
        distrib_id = False
 
67
        statement_line_obj = self.browse(cr, uid, ids[0], context=context)
 
68
        amount = statement_line_obj.amount * -1 or 0.0
94
69
        # Search elements for currency
95
70
        company_currency = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.currency_id.id
96
 
        currency = absl.statement_id.journal_id.currency and absl.statement_id.journal_id.currency.id or company_currency
97
 
        # Get analytic distribution id from this line
98
 
        distrib_id = absl.analytic_distribution_id and absl.analytic_distribution_id.id or False
99
 
        # Prepare values for wizard
100
 
        vals = {
101
 
            'total_amount': amount,
102
 
            'register_line_id': absl.id,
103
 
            'currency_id': currency or False,
104
 
            'state': 'dispatch',
105
 
            'account_id': absl.account_id and absl.account_id.id or False,
106
 
            'posting_date': absl.date,
107
 
            'document_date': absl.document_date,
108
 
        }
109
 
        if distrib_id:
110
 
            vals.update({'distribution_id': distrib_id,})
111
 
        # Create the wizard
112
 
        wiz_obj = self.pool.get('analytic.distribution.wizard')
113
 
        wiz_id = wiz_obj.create(cr, uid, vals, context=context)
114
 
        # Update some context values
 
71
        currency = statement_line_obj.statement_id.journal_id.currency and statement_line_obj.statement_id.journal_id.currency.id or company_currency
 
72
        if statement_line_obj.analytic_distribution_id:
 
73
            distrib_id = statement_line_obj.analytic_distribution_id.id
 
74
        else:
 
75
            distrib_id = self.pool.get('analytic.distribution').create(cr, uid, {}, context=context)
 
76
            newvals={'analytic_distribution_id': distrib_id}
 
77
            super(account_bank_statement_line, self).write(cr, uid, ids, newvals, context=context)
 
78
        wiz_obj = self.pool.get('wizard.costcenter.distribution')
 
79
        wiz_id = wiz_obj.create(cr, uid, {'total_amount': amount, 'distribution_id': distrib_id, 'currency_id': currency}, context=context)
 
80
        # we open a wizard
115
81
        context.update({
116
82
            'active_id': ids[0],
117
83
            'active_ids': ids,
 
84
            'wizard_ids': {'cost_center': wiz_id},
118
85
        })
119
 
        # Open it!
120
86
        return {
121
 
                'name': 'Analytic distribution',
122
87
                'type': 'ir.actions.act_window',
123
 
                'res_model': 'analytic.distribution.wizard',
 
88
                'res_model': 'wizard.costcenter.distribution',
124
89
                'view_type': 'form',
125
90
                'view_mode': 'form',
126
91
                'target': 'new',