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

« back to all changes in this revision

Viewing changes to account_mcdb/account_move_line.py

UF-73: [MERGE] Merge with unifield-wm branch

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=None):
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
 
        if context is None:
44
 
            context = {}
45
 
        # Return nothing if no 'output_currency_id' in context
46
 
        if not context or not context.get('output_currency_id', False):
47
 
            for id in ids:
48
 
                res[id] = {'output_currency': False, 'output_amount': 0.0, 'output_amount_debit': 0.0, 'output_amount_credit': 0.0}
49
 
            return res
50
 
        # Retrieve currency
51
 
        currency_id = context.get('output_currency_id')
52
 
        currency_obj = self.pool.get('res.currency')
53
 
        rate = currency_obj.read(cr, uid, currency_id, ['rate'], context=context).get('rate', False)
54
 
        # Do calculation
55
 
        if not rate:
56
 
            for id in ids:
57
 
                res[id] = {'output_currency': currency_id, 'output_amount': 0.0, 'output_amount_debit': 0.0, 'output_amount_credit': 0.0}
58
 
            return res
59
 
        for ml in self.browse(cr, uid, ids, context=context):
60
 
            res[ml.id] = {'output_currency': False, 'output_amount': 0.0, 'output_amount_debit': 0.0, 'output_amount_credit': 0.0}
61
 
            # output_amount field
62
 
            # Update with date
63
 
            context.update({'date': ml.source_date or ml.date or strftime('%Y-%m-%d')})
64
 
            mnt = self.pool.get('res.currency').compute(cr, uid, ml.currency_id.id, currency_id, ml.amount_currency, round=True, context=context)
65
 
            res[ml.id]['output_amount'] = mnt or 0.0
66
 
            if mnt < 0.0:
67
 
                res[ml.id]['output_amount_debit'] = 0.0
68
 
                res[ml.id]['output_amount_credit'] = abs(mnt) or 0.0
69
 
            else:
70
 
                res[ml.id]['output_amount_debit'] = abs(mnt) or 0.0
71
 
                res[ml.id]['output_amount_credit'] = 0.0
72
 
            # or output_currency field
73
 
            res[ml.id]['output_currency'] = currency_id
74
 
        return res
75
 
 
76
 
    _columns = {
77
 
        'output_amount': fields.function(_get_output, string="Output amount", type='float', method=True, store=False, multi="output_currency"),
78
 
        'output_amount_debit': fields.function(_get_output, string="Output debit", type='float', method=True, store=False, multi="output_currency"),
79
 
        'output_amount_credit': fields.function(_get_output, string="Output credit", type='float', method=True, store=False, multi="output_currency"),
80
 
        'output_currency': fields.function(_get_output, string="Output curr.", type='many2one', relation='res.currency', method=True, store=False, 
81
 
            multi="output_currency"),
82
 
    }
83
 
 
84
 
    def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
85
 
        """
86
 
        Remove output_amount and output_currency field if context doesn't have 'output_currency_id'
87
 
        """
88
 
        # Some verifications
89
 
        if context is None:
90
 
            context = {}
91
 
        view = super(account_move_line, self).fields_view_get(cr, uid, view_id, view_type, context, toolbar, submenu)
92
 
        if view_type == 'tree' and (not context or not context.get('output_currency_id', False)):
93
 
            tree = etree.fromstring(view['arch'])
94
 
            for element in ['output_currency', 'output_amount_debit', 'output_amount_credit']:
95
 
                element_fields = tree.xpath('/tree/field[@name="' + element + '"]')
96
 
                for field in element_fields:
97
 
                    tree.remove(field)
98
 
            view['arch'] = etree.tostring(tree)
99
 
        return view
100
 
 
101
 
account_move_line()
102
 
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: