~unifield-team/unifield-wm/wm-2418-rw-fix

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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# -*- 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
import decimal_precision as dp
from tools.translate import _


class account_move_compute_currency(osv.osv):
    _inherit = "account.move"
    
    def _book_amount_compute(self, cr, uid, ids, name, args, context=None):
        """
        On the same model of the function defined in account>account.py,
        we compute the booking amount
        """
        if not ids: return {}
        cr.execute( """SELECT move_id, SUM(debit_currency) 
                    FROM account_move_line 
                    WHERE move_id IN %s 
                    GROUP BY move_id""", (tuple(ids),))
        result = dict(cr.fetchall())
        for id in ids:
            result.setdefault(id, 0.0)
        return result
    
    def _get_currency(self, cr, uid, ids, fields, arg, context=None):
        """
        get booking currency: we look at the currency_id of the first line
        """
        if not context:
            context = {}
        res = {}
        for move in self.pool.get('account.move').browse(cr, uid, ids, context=context):
            res[move.id] = {}
            if move.line_id:
                line = move.line_id[0]
                if line.currency_id:
                    res[move.id] = line.currency_id.id
                else:
                    res[move.id] = False
        return res

    def _search_currency(self, cr, uid, obj, name, args, context=None):
        """
        Search move in which lines have the given currency
        """
        # Prepare some elements
        newargs = []
        if not context:
            context = {}
        if not args:
            return newargs
        sql_base = """
        SELECT ml.id FROM account_move ml, account_move_line aml
        WHERE aml.move_id = ml.id
        AND aml.currency_id"""
        for arg in args:
            if args[0] and args[0][1] and args[0][1] in ['in', '='] and args[0][2]:
                # create SQL request
                sql = sql_base + ' in %s\nGROUP BY ml.id'
                second = args[0][2]
                # execute it and fetch result
                if isinstance(second, (int, long)):
                    second = [second]
                cr.execute(sql, (tuple(second),))
                res = cr.fetchall()
                newargs.append(('id', 'in', [x and x[0] for x in res]))
            else:
                raise osv.except_osv(_('Error'), _('Operator not supported.'))
        return newargs

    def onchange_journal_id(self, cr, uid, ids, journal_id=False, context=None):
        """
        Change currency_id regarding journal.
        If journal have a currency, set manual_currency_id to the journal's currency and change field to readonly.
        If journal doesn't have any currency: No changes on currency.

        """
        if not context:
            context = {}
        if isinstance(ids, (int, long)):
            ids = [ids]
        res = super(account_move_compute_currency, self).onchange_journal_id(cr, uid, ids, journal_id, context)
        if 'value' not in res:
            res['value'] = {}
        if not journal_id:
            res['value'].update({'block_manual_currency_id': False,})
            return res
        j = self.pool.get('account.journal').read(cr, uid, journal_id, ['currency'])
        if j and j.get('currency', False):
            res['value'].update({'manual_currency_id': j.get('currency'), 'block_manual_currency_id': True,})
        else:
            res['value'].update({'block_manual_currency_id': False,})
        return res

    _columns = {
        'functional_currency_id': fields.related('company_id', 'currency_id', type="many2one", relation="res.currency", string="Functional Currency", store=False),
        'currency_id': fields.function(_get_currency, method=True, type="many2one", relation="res.currency", string='Book. Currency', help="The optional other currency if it is a multi-currency entry."),
        'manual_currency_id': fields.many2one('res.currency', "Book. Currency"),
        'book_amount': fields.function(_book_amount_compute, method=True, string='Book Amount', digits_compute=dp.get_precision('Account'), type='float'),
        'block_manual_currency_id': fields.boolean("Block manual currency field", help="Block manual currency field if journal have a currency."),
    }

    _defaults = {
        'manual_currency_id': lambda *a: False,
        'block_manual_currency_id': lambda *a: False,
    }

    def balance_move(self, cr, uid, ids, context=None):
        """
        Balance move
        """
        if not context:
            context = {}
        for move in self.browse(cr, uid, ids, context):
            amount = 0
            amount_currency = 0
            sorted_line_ids = move.line_id
            sorted_line_ids.sort(key=lambda x: abs(x.debit - x.credit), reverse=True)
            for line in sorted_line_ids:
                amount += line.debit - line.credit
                amount_currency += line.amount_currency
            
            if len(sorted_line_ids) > 2:
                if abs(amount_currency) > 10 ** -4 and abs(amount) < 10 ** -4:
                    # The move is balanced, but there is a difference in the converted amounts;
                    # the second-biggest move line is modified accordingly
                    line_to_be_balanced = sorted_line_ids[1]
                    amount_currency = line_to_be_balanced.amount_currency - amount_currency
                    debit_currency = 0.0
                    credit_currency = 0.0
                    if amount_currency > 0:
                        debit_currency = amount_currency
                    else:
                        credit_currency = -amount_currency
                    # write() is not called to avoid a loop and a refresh of the rates
                    cr.execute('update account_move_line set amount_currency=%s, \
                                                             debit_currency=%s, \
                                                             credit_currency=%s where id=%s',
                              (amount_currency, debit_currency, credit_currency, line_to_be_balanced.id))
                elif abs(amount) > 10 ** -4 and abs(amount_currency) < 10 ** -4:
                    # The move is balanced, but there is a difference in the converted amounts;
                    # the second-biggest move line is modified accordingly
                    line_to_be_balanced = sorted_line_ids[1]
                    amount = line_to_be_balanced.debit - line_to_be_balanced.credit - amount
                    debit = 0.0
                    credit = 0.0
                    if amount > 0:
                        debit = amount
                    else:
                        credit = -amount
                    # write() is not called to avoid a loop and a refresh of the rates
                    cr.execute('update account_move_line set debit=%s, \
                                                             credit=%s where id=%s',
                              (debit, credit, line_to_be_balanced.id))
        return True

    def validate(self, cr, uid, ids, context=None):
        """
        Balance move before its validation
        """
        self.balance_move(cr, uid, ids, context=context)
        return super(account_move_compute_currency, self).validate(cr, uid, ids, context)

    def create(self, cr, uid, vals, context=None):
        """
        Add currency if none for manual entry
        """
        if not context:
            context = {}
        if not 'manual_currency_id' in vals or not vals.get('manual_currency_id'):
            if 'journal_id' in vals:
                j = self.pool.get('account.journal').read(cr, uid, vals.get('journal_id'), ['currency'])
                if j and j.get('currency', False):
                    vals.update({'manual_currency_id': j.get('currency')[0]})
                    # Add currency to context for journal items lines
                    if not 'manual_currency_id' in context:
                        context['manual_currency_id'] = j.get('currency')[0]
        return super(account_move_compute_currency, self).create(cr, uid, vals, context)

    def write(self, cr, uid, ids, vals, context=None):
        """
        Change manual currency regarding journal
        """
        if not context:
            context = {}
        res = []
        for m in self.read(cr, uid, ids, ['journal_id', 'status', 'line_id']):
            j_id = m.get('journal_id', False) and m.get('journal_id')[0] or False
            if 'journal_id' in vals:
                j_id = vals.get('journal_id')
            journal = self.pool.get('account.journal').read(cr, uid, j_id, ['currency'])
            if journal and journal.get('currency', False):
                vals.update({'manual_currency_id': journal.get('currency')[0], 'block_manual_currency_id': True,})
                # Add currency to context for journal items lines
                if not 'manual_currency_id' in context:
                    context['manual_currency_id'] = journal.get('currency')[0]
            tmp_res = super(account_move_compute_currency, self).write(cr, uid, [m.get('id')], vals, context)
            res.append(tmp_res)
            # Recompute account move lines debit/credit
            if 'manual_currency_id' in vals and m.get('status') == 'manu':
                for ml in self.pool.get('account.move.line').browse(cr, uid, m.get('line_id', []), context=context):
                    self.pool.get('account.move.line').write(cr, uid, [ml.id], {'currency_id': vals.get('manual_currency_id'), 'debit_currency': ml.debit_currency, 'credit_currency': ml.credit_currency}, context=context)
        return res

account_move_compute_currency()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: