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

409.2.18 by Olivier DOSSMANN
UF-665 [ADD] 2 columns for output currency and output amount on account_move_line
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
409.2.20 by Olivier DOSSMANN
UF-665 [ADD] Output amount and output currency for analytic account line
27
from lxml import etree
409.2.18 by Olivier DOSSMANN
UF-665 [ADD] 2 columns for output currency and output amount on account_move_line
28
29
class account_move_line(osv.osv):
30
    _name = 'account.move.line'
31
    _inherit = 'account.move.line'
32
2113.9.2 by Olivier DOSSMANN
UF-2123 [FIX] Warning and errors from Eclipse
33
    # UTP-936: Extract the method to calculate the output here, so that it can be used at other places, for example:
1798.4.2 by duy.vo at msf
UTP-936: Fixed the calculation in pdf and xls
34
    # account_mcdb/report/account_mcdb_export.py, which is used to generate csv reports
1910 by jf
UTP-936 [FIX] Excel exports - Bug when exporting FX adjustments
35
    def calculate_output(self, cr, uid, currency_id, ml, round, context):
1798.4.2 by duy.vo at msf
UTP-936: Fixed the calculation in pdf and xls
36
        currency_obj = self.pool.get('res.currency')
37
        func_amount = ml.amount_currency
38
        original_currency = ml.currency_id.id
2204.27.2 by Sean Carroll
UTP-1161:[FIX] corrected zero results on revaluation rows
39
        if ml.journal_id.type in ['cur_adj','revaluation']:
1963.7.1 by vg at tempo-consulting
UF-2296 [FIX] in case of Current Adjustmeent Journal (MT) if output ccy == fonctional ccy we must return functional amount
40
            # UF-2296: in case of Current Adjustmeent Journal (MT)
41
            # if output ccy == fonctional ccy we must return functional amount
42
            # explanation: export search result account_mcdb/report/account_mcdb_export.py
43
            # uses 'output_currency_id' for amount computing even if no output ccy
44
            return_func_amount = False
45
            if currency_id == ml.functional_currency_id.id:
46
                return_func_amount = True
1798.4.2 by duy.vo at msf
UTP-936: Fixed the calculation in pdf and xls
47
            #UTP-936: In case of MT journal, the conversion is from functional currency to output currency
48
            if ml.debit:
49
                func_amount = ml.debit
50
            elif ml.credit:
51
                func_amount = -ml.credit
1963.7.1 by vg at tempo-consulting
UF-2296 [FIX] in case of Current Adjustmeent Journal (MT) if output ccy == fonctional ccy we must return functional amount
52
            if return_func_amount:  # UF-2296
53
                return func_amount
54
            original_currency = ml.functional_currency_id.id
1798.4.2 by duy.vo at msf
UTP-936: Fixed the calculation in pdf and xls
55
        # Perform the conversion from original currency to selected currency
1910 by jf
UTP-936 [FIX] Excel exports - Bug when exporting FX adjustments
56
        return currency_obj.compute(cr, uid, original_currency, currency_id, func_amount, round=round, context=context)
1798.4.2 by duy.vo at msf
UTP-936: Fixed the calculation in pdf and xls
57
707 by jf
[FIX] Default values
58
    def _get_output(self, cr, uid, ids, field_name, arg, context=None):
409.2.18 by Olivier DOSSMANN
UF-665 [ADD] 2 columns for output currency and output amount on account_move_line
59
        """
409.2.28 by Olivier DOSSMANN
UF-665 [ADD] Output currency regarding fx table
60
        Get an amount regarding currency in context (from 'output' and 'output_currency_id' values).
61
        NB: Pay attention to 'currency_table_id' field in context. It compute amounts regarding another rates.
409.2.18 by Olivier DOSSMANN
UF-665 [ADD] 2 columns for output currency and output amount on account_move_line
62
        """
63
        # Prepare some value
64
        res = {}
65
        # Some verifications
66
        if isinstance(ids, (int, long)):
67
            ids = [ids]
707 by jf
[FIX] Default values
68
        if context is None:
69
            context = {}
409.2.18 by Olivier DOSSMANN
UF-665 [ADD] 2 columns for output currency and output amount on account_move_line
70
        # Return nothing if no 'output_currency_id' in context
71
        if not context or not context.get('output_currency_id', False):
2113.9.2 by Olivier DOSSMANN
UF-2123 [FIX] Warning and errors from Eclipse
72
            for o_id in ids:
73
                res[o_id] = {'output_currency': False, 'output_amount': 0.0, 'output_amount_debit': 0.0, 'output_amount_credit': 0.0}
409.2.18 by Olivier DOSSMANN
UF-665 [ADD] 2 columns for output currency and output amount on account_move_line
74
            return res
75
        # Retrieve currency
2575 by jf
US-369 [FIX] Export JI: if output currency == company currency do not recompute rate
76
        company_currency_id = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.currency_id.id
409.2.18 by Olivier DOSSMANN
UF-665 [ADD] 2 columns for output currency and output amount on account_move_line
77
        currency_id = context.get('output_currency_id')
409.2.42 by Olivier DOSSMANN
UF-665 [FIX] Error when select an output currency that doesn't have any rate
78
        currency_obj = self.pool.get('res.currency')
79
        rate = currency_obj.read(cr, uid, currency_id, ['rate'], context=context).get('rate', False)
409.2.18 by Olivier DOSSMANN
UF-665 [ADD] 2 columns for output currency and output amount on account_move_line
80
        # Do calculation
409.2.42 by Olivier DOSSMANN
UF-665 [FIX] Error when select an output currency that doesn't have any rate
81
        if not rate:
2113.9.2 by Olivier DOSSMANN
UF-2123 [FIX] Warning and errors from Eclipse
82
            for out_id in ids:
83
                res[out_id] = {'output_currency': currency_id, 'output_amount': 0.0, 'output_amount_debit': 0.0, 'output_amount_credit': 0.0}
409.2.42 by Olivier DOSSMANN
UF-665 [FIX] Error when select an output currency that doesn't have any rate
84
            return res
409.2.18 by Olivier DOSSMANN
UF-665 [ADD] 2 columns for output currency and output amount on account_move_line
85
        for ml in self.browse(cr, uid, ids, context=context):
2575 by jf
US-369 [FIX] Export JI: if output currency == company currency do not recompute rate
86
            res[ml.id] = {'output_currency': currency_id, 'output_amount': 0.0, 'output_amount_debit': 0.0, 'output_amount_credit': 0.0}
409.2.18 by Olivier DOSSMANN
UF-665 [ADD] 2 columns for output currency and output amount on account_move_line
87
            # output_amount field
409.2.42 by Olivier DOSSMANN
UF-665 [FIX] Error when select an output currency that doesn't have any rate
88
            # Update with date
89
            context.update({'date': ml.source_date or ml.date or strftime('%Y-%m-%d')})
2113.9.2 by Olivier DOSSMANN
UF-2123 [FIX] Warning and errors from Eclipse
90
            # Now call the common method to calculate the output values
2575 by jf
US-369 [FIX] Export JI: if output currency == company currency do not recompute rate
91
            if currency_id == company_currency_id:
92
                res[ml.id].update({'output_amount': ml.debit - ml.credit, 'output_amount_debit': ml.debit, 'output_amount_credit': ml.credit})
363.11.108 by Olivier DOSSMANN
UF-665 [IMP] Split output amount in output debit and output credit for journal items and analytic journal items for MCDB
93
            else:
2575 by jf
US-369 [FIX] Export JI: if output currency == company currency do not recompute rate
94
                amount = self.calculate_output(cr, uid, currency_id, ml, round=True, context=context)
95
                res[ml.id]['output_amount'] = amount or 0.0
96
                if amount < 0.0:
97
                    res[ml.id]['output_amount_debit'] = 0.0
98
                    res[ml.id]['output_amount_credit'] = abs(amount) or 0.0
99
                else:
100
                    res[ml.id]['output_amount_debit'] = abs(amount) or 0.0
101
                    res[ml.id]['output_amount_credit'] = 0.0
102
                    # or output_currency field
409.2.18 by Olivier DOSSMANN
UF-665 [ADD] 2 columns for output currency and output amount on account_move_line
103
        return res
104
105
    _columns = {
409.2.42 by Olivier DOSSMANN
UF-665 [FIX] Error when select an output currency that doesn't have any rate
106
        'output_amount': fields.function(_get_output, string="Output amount", type='float', method=True, store=False, multi="output_currency"),
363.11.108 by Olivier DOSSMANN
UF-665 [IMP] Split output amount in output debit and output credit for journal items and analytic journal items for MCDB
107
        'output_amount_debit': fields.function(_get_output, string="Output debit", type='float', method=True, store=False, multi="output_currency"),
108
        'output_amount_credit': fields.function(_get_output, string="Output credit", type='float', method=True, store=False, multi="output_currency"),
2113.9.2 by Olivier DOSSMANN
UF-2123 [FIX] Warning and errors from Eclipse
109
        'output_currency': fields.function(_get_output, string="Output curr.", type='many2one', relation='res.currency', method=True, store=False,
409.2.42 by Olivier DOSSMANN
UF-665 [FIX] Error when select an output currency that doesn't have any rate
110
            multi="output_currency"),
409.2.18 by Olivier DOSSMANN
UF-665 [ADD] 2 columns for output currency and output amount on account_move_line
111
    }
112
707 by jf
[FIX] Default values
113
    def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
409.2.20 by Olivier DOSSMANN
UF-665 [ADD] Output amount and output currency for analytic account line
114
        """
115
        Remove output_amount and output_currency field if context doesn't have 'output_currency_id'
116
        """
117
        # Some verifications
707 by jf
[FIX] Default values
118
        if context is None:
119
            context = {}
409.2.20 by Olivier DOSSMANN
UF-665 [ADD] Output amount and output currency for analytic account line
120
        view = super(account_move_line, self).fields_view_get(cr, uid, view_id, view_type, context, toolbar, submenu)
121
        if view_type == 'tree' and (not context or not context.get('output_currency_id', False)):
122
            tree = etree.fromstring(view['arch'])
363.11.108 by Olivier DOSSMANN
UF-665 [IMP] Split output amount in output debit and output credit for journal items and analytic journal items for MCDB
123
            for element in ['output_currency', 'output_amount_debit', 'output_amount_credit']:
124
                element_fields = tree.xpath('/tree/field[@name="' + element + '"]')
125
                for field in element_fields:
126
                    tree.remove(field)
409.2.20 by Olivier DOSSMANN
UF-665 [ADD] Output amount and output currency for analytic account line
127
            view['arch'] = etree.tostring(tree)
2393 by jf
BKLG-7 [IMP] Selectors modifications requests: fields cheque_number + partner_txt added
128
2374.1.1 by vg at tempo-consulting
BKLG-7 [IMP] 1) G/L / Analytical Selectors: add search for cheque number. AJIs: add adhoc fct field to search/display from JI. Insert the field after entry sequence in the treeview for the result (if cheque number asked in the selector).
129
        if view_type == 'tree' and \
130
            context.get('selector_display_cheque_number', False):
131
            # BKLG-7: cheque_number used in G/L selector: display it
132
            view['fields']['cheque_number'] = {
133
                'type': 'char',
134
                'string': 'Cheque Number',
135
            }
2393 by jf
BKLG-7 [IMP] Selectors modifications requests: fields cheque_number + partner_txt added
136
2374.1.1 by vg at tempo-consulting
BKLG-7 [IMP] 1) G/L / Analytical Selectors: add search for cheque number. AJIs: add adhoc fct field to search/display from JI. Insert the field after entry sequence in the treeview for the result (if cheque number asked in the selector).
137
            tree = etree.fromstring(view['arch'])
2393 by jf
BKLG-7 [IMP] Selectors modifications requests: fields cheque_number + partner_txt added
138
2374.1.1 by vg at tempo-consulting
BKLG-7 [IMP] 1) G/L / Analytical Selectors: add search for cheque number. AJIs: add adhoc fct field to search/display from JI. Insert the field after entry sequence in the treeview for the result (if cheque number asked in the selector).
139
            cheque_number_node = etree.Element('field', attrib={
140
                'name': 'cheque_number',
141
            })
142
            # insert it after entry sequence
143
            es_node = tree.find('.//field[@name="move_id"]')
144
            tree.insert(es_node.getparent().index(es_node) + 1,
145
                cheque_number_node)
2393 by jf
BKLG-7 [IMP] Selectors modifications requests: fields cheque_number + partner_txt added
146
2374.1.1 by vg at tempo-consulting
BKLG-7 [IMP] 1) G/L / Analytical Selectors: add search for cheque number. AJIs: add adhoc fct field to search/display from JI. Insert the field after entry sequence in the treeview for the result (if cheque number asked in the selector).
147
            view['arch'] = etree.tostring(tree)
409.2.20 by Olivier DOSSMANN
UF-665 [ADD] Output amount and output currency for analytic account line
148
        return view
149
409.2.18 by Olivier DOSSMANN
UF-665 [ADD] 2 columns for output currency and output amount on account_move_line
150
account_move_line()
151
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: