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

« back to all changes in this revision

Viewing changes to funding_pool/account_move_line.py

  • Committer: chloups208
  • Date: 2011-06-30 14:54:26 UTC
  • mto: (307.2.1 unifield-wm)
  • mto: This revision was merged to the branch mainline in revision 311.
  • Revision ID: chloups208@chloups208-laptop-20110630145426-qsj5j0pp3e5b23bc
[UF-53][UF-58][UF-63] initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
 
##############################################################################
3
 
#
4
 
#    OpenERP, Open Source Management Solution
5
 
#    Copyright (C) 2011 MSF, TeMPO consulting
6
 
#
7
 
#    This program is free software: you can redistribute it and/or modify
8
 
#    it under the terms of the GNU Affero General Public License as
9
 
#    published by the Free Software Foundation, either version 3 of the
10
 
#    License, or (at your option) any later version.
11
 
#
12
 
#    This program is distributed in the hope that it will be useful,
13
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
#    GNU Affero General Public License for more details.
16
 
#
17
 
#    You should have received a copy of the GNU Affero General Public License
18
 
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 
#
20
 
##############################################################################
21
 
 
22
 
from osv import fields, osv
23
 
import tools
24
 
from tools.translate import _
25
 
 
26
 
class account_move_line(osv.osv):
27
 
    _inherit = 'account.move.line'
28
 
 
29
 
    _columns = {
30
 
        'analytic_distribution_id': fields.many2one('analytic.distribution', 'Analytic Distribution'),
31
 
    }
32
 
 
33
 
    def create_analytic_lines(self, cr, uid, ids, context=None):
34
 
        acc_ana_line_obj = self.pool.get('account.analytic.line')
35
 
        company_currency = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.currency_id.id
36
 
        for obj_line in self.browse(cr, uid, ids, context=context):
37
 
            if obj_line.analytic_distribution_id and obj_line.account_id.user_type_code == 'expense':
38
 
                if not obj_line.journal_id.analytic_journal_id:
39
 
                    raise osv.except_osv(_('No Analytic Journal !'),_("You have to define an analytic journal on the '%s' journal!") % (obj_line.journal_id.name, ))
40
 
                distrib_obj = self.pool.get('analytic.distribution').browse(cr, uid, obj_line.analytic_distribution_id.id, context=context)
41
 
                # create lines
42
 
                for distrib_lines in [distrib_obj.cost_center_lines, distrib_obj.funding_pool_lines, distrib_obj.free_1_lines, distrib_obj.free_2_lines]:
43
 
                    for distrib_line in distrib_lines:
44
 
                        context.update({'date': obj_line.source_date or obj_line.date})
45
 
                        line_vals = {
46
 
                                     'name': obj_line.name,
47
 
                                     'date': obj_line.date,
48
 
                                     'ref': obj_line.ref,
49
 
                                     'journal_id': obj_line.journal_id.analytic_journal_id.id,
50
 
                                     'amount': -1 * self.pool.get('res.currency').compute(cr, uid, obj_line.currency_id.id, company_currency, 
51
 
                                        distrib_line.amount or 0.0, round=False, context=context),
52
 
                                     'amount_currency': -1 * distrib_line.amount,
53
 
                                     'account_id': distrib_line.analytic_id.id,
54
 
                                     'general_account_id': obj_line.account_id.id,
55
 
                                     'move_id': obj_line.id,
56
 
                                     'distribution_id': distrib_obj.id,
57
 
                                     'user_id': uid,
58
 
                                     'currency_id': obj_line.currency_id.id,
59
 
                        }
60
 
                        # Update values if we come from a funding pool
61
 
                        if distrib_line._name == 'funding.pool.distribution.line':
62
 
                            line_vals.update({'cost_center_id': distrib_line.cost_center_id and distrib_line.cost_center_id.id or False,})
63
 
                        # Add source_date value for account_move_line that are a correction of another account_move_line
64
 
                        if obj_line.corrected_line_id and obj_line.source_date:
65
 
                            line_vals.update({'source_date': obj_line.source_date})
66
 
                        self.pool.get('account.analytic.line').create(cr, uid, line_vals, context=context)
67
 
        return True
68
 
 
69
 
    def button_analytic_distribution(self, cr, uid, ids, context={}):
70
 
        """
71
 
        Launch the analytic distribution wizard from a journal item (account_move_line)
72
 
        """
73
 
        # Some verifications
74
 
        if not context:
75
 
            context = {}
76
 
        if isinstance(ids, (int, long)):
77
 
            ids = [ids]
78
 
        # we get the analytical distribution object linked to this line
79
 
        distrib_id = False
80
 
        move_line_obj = self.browse(cr, uid, ids[0], context=context)
81
 
        # Get amount using account_move_line amount_currency field
82
 
        amount = move_line_obj.amount_currency and move_line_obj.amount_currency or 0.0
83
 
        # Search elements for currency
84
 
        company_currency = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.currency_id.id
85
 
        currency = move_line_obj.currency_id and move_line_obj.currency_id.id or company_currency
86
 
        if move_line_obj.analytic_distribution_id:
87
 
            distrib_id = move_line_obj.analytic_distribution_id.id
88
 
        else:
89
 
            raise osv.except_osv(_('No Analytic Distribution !'),_("You have to define an analytic distribution on the move line!"))
90
 
        wiz_obj = self.pool.get('wizard.costcenter.distribution')
91
 
        wiz_id = wiz_obj.create(cr, uid, {'total_amount': amount, 'distribution_id': distrib_id, 'currency_id': currency}, context=context)
92
 
        # we open a wizard
93
 
        context.update({
94
 
            'active_id': ids[0],
95
 
            'active_ids': ids,
96
 
            'wizard_ids': {'cost_center': wiz_id},
97
 
        })
98
 
        return {
99
 
                'type': 'ir.actions.act_window',
100
 
                'res_model': 'wizard.costcenter.distribution',
101
 
                'view_type': 'form',
102
 
                'view_mode': 'form',
103
 
                'target': 'new',
104
 
                'res_id': [wiz_id],
105
 
                'context': context,
106
 
        }
107
 
 
108
 
account_move_line()
109
 
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: