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

« back to all changes in this revision

Viewing changes to account_mcdb/account_move_line.py

UF-674 Dev:Commitments registering
UF-668 Dev:Analytical mass reallocation
UF-664 Dev:Multi-criteria data browser

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# -*- coding: utf-8 -*-
 
3
##############################################################################
 
4
#
 
5
#    OpenERP, Open Source Management Solution
 
6
#    Copyright (C) 2011 TeMPO Consulting, MSF. All Rights Reserved
 
7
#    Developer: Olivier DOSSMANN
 
8
#
 
9
#    This program is free software: you can redistribute it and/or modify
 
10
#    it under the terms of the GNU Affero General Public License as
 
11
#    published by the Free Software Foundation, either version 3 of the
 
12
#    License, or (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 Affero General Public License for more details.
 
18
#
 
19
#    You should have received a copy of the GNU Affero General Public License
 
20
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
21
#
 
22
##############################################################################
 
23
 
 
24
from osv import osv
 
25
from osv import fields
 
26
from time import strftime
 
27
from lxml import etree
 
28
 
 
29
class account_move_line(osv.osv):
 
30
    _name = 'account.move.line'
 
31
    _inherit = 'account.move.line'
 
32
 
 
33
    def _get_output(self, cr, uid, ids, field_name, arg, context={}):
 
34
        """
 
35
        Get an amount regarding currency in context (from 'output' and 'output_currency_id' values).
 
36
        NB: Pay attention to 'currency_table_id' field in context. It compute amounts regarding another rates.
 
37
        """
 
38
        # Prepare some value
 
39
        res = {}
 
40
        # Some verifications
 
41
        if isinstance(ids, (int, long)):
 
42
            ids = [ids]
 
43
        # Return nothing if no 'output_currency_id' in context
 
44
        if not context or not context.get('output_currency_id', False):
 
45
            for id in ids:
 
46
                res[id] = {'output_currency': False, 'output_amount': 0.0, 'output_amount_debit': 0.0, 'output_amount_credit': 0.0}
 
47
            return res
 
48
        # Retrieve currency
 
49
        currency_id = context.get('output_currency_id')
 
50
        currency_obj = self.pool.get('res.currency')
 
51
        rate = currency_obj.read(cr, uid, currency_id, ['rate'], context=context).get('rate', False)
 
52
        # Do calculation
 
53
        if not rate:
 
54
            for id in ids:
 
55
                res[id] = {'output_currency': currency_id, 'output_amount': 0.0, 'output_amount_debit': 0.0, 'output_amount_credit': 0.0}
 
56
            return res
 
57
        for ml in self.browse(cr, uid, ids, context=context):
 
58
            res[ml.id] = {'output_currency': False, 'output_amount': 0.0, 'output_amount_debit': 0.0, 'output_amount_credit': 0.0}
 
59
            # output_amount field
 
60
            # Update with date
 
61
            context.update({'date': ml.source_date or ml.date or strftime('%Y-%m-%d')})
 
62
            mnt = self.pool.get('res.currency').compute(cr, uid, ml.currency_id.id, currency_id, ml.amount_currency, round=True, context=context)
 
63
            res[ml.id]['output_amount'] = mnt or 0.0
 
64
            if mnt < 0.0:
 
65
                res[ml.id]['output_amount_debit'] = 0.0
 
66
                res[ml.id]['output_amount_credit'] = abs(mnt) or 0.0
 
67
            else:
 
68
                res[ml.id]['output_amount_debit'] = abs(mnt) or 0.0
 
69
                res[ml.id]['output_amount_credit'] = 0.0
 
70
            # or output_currency field
 
71
            res[ml.id]['output_currency'] = currency_id
 
72
        return res
 
73
 
 
74
    _columns = {
 
75
        'output_amount': fields.function(_get_output, string="Output amount", type='float', method=True, store=False, multi="output_currency"),
 
76
        'output_amount_debit': fields.function(_get_output, string="Output debit", type='float', method=True, store=False, multi="output_currency"),
 
77
        'output_amount_credit': fields.function(_get_output, string="Output credit", type='float', method=True, store=False, multi="output_currency"),
 
78
        'output_currency': fields.function(_get_output, string="Output curr.", type='many2one', relation='res.currency', method=True, store=False, 
 
79
            multi="output_currency"),
 
80
    }
 
81
 
 
82
    def fields_view_get(self, cr, uid, view_id=None, view_type='form', context={}, toolbar=False, submenu=False):
 
83
        """
 
84
        Remove output_amount and output_currency field if context doesn't have 'output_currency_id'
 
85
        """
 
86
        # Some verifications
 
87
        view = super(account_move_line, self).fields_view_get(cr, uid, view_id, view_type, context, toolbar, submenu)
 
88
        if view_type == 'tree' and (not context or not context.get('output_currency_id', False)):
 
89
            tree = etree.fromstring(view['arch'])
 
90
            for element in ['output_currency', 'output_amount_debit', 'output_amount_credit']:
 
91
                element_fields = tree.xpath('/tree/field[@name="' + element + '"]')
 
92
                for field in element_fields:
 
93
                    tree.remove(field)
 
94
            view['arch'] = etree.tostring(tree)
 
95
        return view
 
96
 
 
97
account_move_line()
 
98
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: