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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2011 MSF, TeMPO consulting
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from osv import fields, osv

class sale_order_line_compute_currency(osv.osv):
    _inherit = "sale.order.line"

    def _amount_currency_line(self, cr, uid, ids, field_name, arg, context=None):
        cur_obj = self.pool.get('res.currency')
        res = {}
        for line in self.browse(cr, uid, ids, context=context):
            try:
                res[line.id] = cur_obj.compute(cr, uid, line.currency_id.id,
                        line.functional_currency_id.id, line.price_subtotal, round=True)
            except osv.except_osv:
                res[line.id] = 0
        return res
    
    _columns = {
        'currency_id': fields.related('order_id', 'currency_id', type="many2one", relation="res.currency", string="Currency", store=False, readonly=True),
        'functional_subtotal': fields.function(_amount_currency_line, method=True, store=False, string='Functional Subtotal', readonly=True),
        'functional_currency_id': fields.related('company_id', 'currency_id', type="many2one", relation="res.currency", string="Functional Currency", store=False, readonly=True),
    }
    
sale_order_line_compute_currency()

class sale_order_compute_currency(osv.osv):
    _inherit = "sale.order"

    def _amount_currency(self, cr, uid, ids, field_name, arg, context=None):
        cur_obj = self.pool.get('res.currency')
        res = {}
        for order in self.browse(cr, uid, ids, context=context):
            # The confirmed date, if present, is used as a "freeze" date
            # for the currency rate
            ctx = {}
            if order.date_confirm:
                ctx['date'] = order.date_confirm
            try:
                res[order.id] = {
                                'functional_amount_untaxed':cur_obj.compute(cr, uid, order.currency_id.id,
                                                                            order.functional_currency_id.id, order.amount_untaxed, round=True, context=ctx),
                                'functional_amount_tax':cur_obj.compute(cr, uid, order.currency_id.id,
                                                                        order.functional_currency_id.id, order.amount_tax, round=True, context=ctx),
                                'functional_amount_total':cur_obj.compute(cr, uid, order.currency_id.id,
                                                                          order.functional_currency_id.id, order.amount_total, round=True, context=ctx),
                                }
            except osv.except_osv:
                res[order.id] = {
                                 'functional_amount_untaxed':0,
                                 'functional_amount_tax':0,
                                 'functional_amount_total':0
                                }
        return res
    
    _columns = {
        'currency_id': fields.related('pricelist_id', 'currency_id', type="many2one", relation="res.currency", string="Currency", store=False, readonly=True),
        'functional_amount_untaxed': fields.function(_amount_currency, method=True, store=False, type='float', string='Functional Untaxed Amount', multi='amount_untaxed, amount_tax, amount_total'),
        'functional_amount_tax': fields.function(_amount_currency, method=True, store=False, type='float', string='Functional Taxes', multi='amount_untaxed, amount_tax, amount_total'),
        'functional_amount_total': fields.function(_amount_currency, method=True, store=False, type='float', string='Functional Total', multi='amount_untaxed, amount_tax, amount_total'),
        'functional_currency_id': fields.related('company_id', 'currency_id', type="many2one", relation="res.currency", string="Functional Currency", store=False, readonly=True),
    }
    
sale_order_compute_currency()

class purchase_order_line_compute_currency(osv.osv):
    _inherit = "purchase.order.line"

    def _amount_currency_line(self, cr, uid, ids, field_name, arg, context=None):
        cur_obj = self.pool.get('res.currency')
        res = {}
        for line in self.browse(cr, uid, ids, context=context):
            try:
                res[line.id] = cur_obj.compute(cr, uid, line.currency_id.id,
                        line.functional_currency_id.id, line.price_subtotal, round=True)
            except osv.except_osv:
                res[line.id] = 0
        return res
    
    _columns = {
        'currency_id': fields.related('order_id', 'currency_id', type="many2one", relation="res.currency", string="Currency", store=False, readonly=True),
        'functional_subtotal': fields.function(_amount_currency_line, method=True, store=False, string='Functional Subtotal'),
        'functional_currency_id': fields.related('company_id', 'currency_id', type="many2one", relation="res.currency", string="Functional Currency", store=False, readonly=True),
    }
    
purchase_order_line_compute_currency()

class purchase_order_compute_currency(osv.osv):
    _inherit = "purchase.order"

    def _amount_currency(self, cr, uid, ids, field_name, arg, context=None):
        cur_obj = self.pool.get('res.currency')
        res = {}
        for order in self.browse(cr, uid, ids, context=context):
            # The approved date, if present, is used as a "freeze" date
            # for the currency rate
            ctx = {}
            if order.date_approve:
                ctx['date'] = order.date_approve
            try:
                res[order.id] = {
                                'functional_amount_untaxed':cur_obj.compute(cr, uid, order.currency_id.id,
                                                                            order.functional_currency_id.id, order.amount_untaxed, round=True, context=ctx),
                                'functional_amount_tax':cur_obj.compute(cr, uid, order.currency_id.id,
                                                                        order.functional_currency_id.id, order.amount_tax, round=True, context=ctx),
                                'functional_amount_total':cur_obj.compute(cr, uid, order.currency_id.id,
                                                                          order.functional_currency_id.id, order.amount_total, round=True, context=ctx),
                                }
            except osv.except_osv:
                res[order.id] = {
                                 'functional_amount_untaxed':0,
                                 'functional_amount_tax':0,
                                 'functional_amount_total':0
                                }
        return res
    
    _columns = {
        'currency_id': fields.related('pricelist_id', 'currency_id', type="many2one", relation="res.currency", string="Currency", store=False, readonly=True),
        'functional_amount_untaxed': fields.function(_amount_currency, method=True, store=False, type='float', string='Functional Untaxed Amount', multi='amount_untaxed, amount_tax, amount_total'),
        'functional_amount_tax': fields.function(_amount_currency, method=True, store=False, type='float', string='Functional Taxes', multi='amount_untaxed, amount_tax, amount_total'),
        'functional_amount_total': fields.function(_amount_currency, method=True, store=False, type='float', string='Functional Total', multi='amount_untaxed, amount_tax, amount_total'),
        'functional_currency_id': fields.related('company_id', 'currency_id', type="many2one", relation="res.currency", string="Functional Currency", store=False, readonly=True),
    }
    
purchase_order_compute_currency()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: