~julie-w/unifield-wm/UTP-925

« back to all changes in this revision

Viewing changes to account_override/account_analytic_line.py

UF-359 [ADD] Account override module integration

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
#-*- encoding: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
 
 
27
class account_analytic_line(osv.osv):
 
28
    _inherit = 'account.analytic.line'
 
29
 
 
30
    def _get_amount_currency(self, cr, uid, ids, field_name=None, arg=None, context={}):
 
31
        """
 
32
        Get amount currency.
 
33
        Get amount currency given in move attached to analytic line or default stored amount if no move_id exists.
 
34
        """
 
35
        # Verifications
 
36
        if not context:
 
37
            context = {}
 
38
        if isinstance(ids, (int, long)):
 
39
            ids = [ids]
 
40
        # Prepare some values
 
41
        res = {}
 
42
        for aal in self.browse(cr, uid, ids, context=context):
 
43
            if aal.move_id:
 
44
                res[aal.id] = aal.move_id.amount_currency
 
45
            else:
 
46
                sql = "SELECT amount_currency FROM %s WHERE id = %s" % (self._table, aal.id)
 
47
                cr.execute(sql)
 
48
                sql_result = cr.fetchone()
 
49
                res[aal.id] = sql_result and sql_result[0] or None
 
50
        return res
 
51
 
 
52
    def _set_amount_currency(self, cr, uid, id, name=None, value=None, fnct_inv_arg=None, context={}):
 
53
        """
 
54
        Set amount currency if no move_id in value
 
55
        """
 
56
        if name and value:
 
57
            sql = "UPDATE %s SET %s = %s WHERE id = %s" % (self._table, name, value, id)
 
58
            cr.execute(sql)
 
59
        return True
 
60
 
 
61
    _columns = {
 
62
        'reversal_origin': fields.many2one('account.analytic.line', string="Reversal origin", readonly=True, help="Line that have been reversed."),
 
63
        'invoice_line_id': fields.many2one('account.invoice.line', string="Invoice line", readonly=True, help="Invoice line from which this line is linked."),
 
64
        'source_date': fields.date('Source date', help="Date used for FX rate re-evaluation"),
 
65
        'amount_currency': fields.function(_get_amount_currency, fnct_inv=_set_amount_currency, method=True, store=True, string="Amount currency", type="float", readonly="True", help="")
 
66
    }
 
67
 
 
68
    def copy(self, cr, uid, defaults, context={}):
 
69
        """
 
70
        Update amount_currency from previous element
 
71
        """
 
72
        amt = self.read(cr, uid, defaults, ['amount_currency'], context=context).get('amount_currency', False)
 
73
        res = super(account_analytic_line, self).copy(cr, uid, defaults, context=context)
 
74
        self.write(cr, uid, [res], {'amount_currency': amt}, context=context)
 
75
        return res
 
76
 
 
77
account_analytic_line()
 
78
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: