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

« back to all changes in this revision

Viewing changes to msf_accrual/msf_accrual_line.py

  • Committer: Matthieu Dietrich
  • Date: 2012-03-07 11:26:19 UTC
  • mto: This revision was merged to the branch mainline in revision 675.
  • Revision ID: matthieu.dietrich@geneva.msf.org-20120307112619-ze1fu6cmfij8smhk
UF-820: [IMP] accrual management added

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
 
 
24
class msf_accrual_line(osv.osv):
 
25
    _name = 'msf.accrual.line'
 
26
    
 
27
    def onchange_period(self, cr, uid, ids, period_id, context=None):
 
28
        period = self.pool.get('account.period').browse(cr, uid, period_id, context=context)
 
29
        return {'value': {'date': period.date_stop}}
 
30
    
 
31
    def _get_functional_amount(self, cr, uid, ids, field_name, arg, context=None):
 
32
        res = {}
 
33
        for accrual_line in self.browse(cr, uid, ids, context=context):
 
34
            date_context = {'date': accrual_line.date}
 
35
            res[accrual_line.id] =  self.pool.get('res.currency').compute(cr,
 
36
                                                                          uid,
 
37
                                                                          accrual_line.currency_id.id,
 
38
                                                                          accrual_line.functional_currency_id.id, 
 
39
                                                                          accrual_line.accrual_amount or 0.0,
 
40
                                                                          round=True,
 
41
                                                                          context=date_context)
 
42
        return res
 
43
    
 
44
    _columns = {
 
45
        'date': fields.date("Date"),
 
46
        'period_id': fields.many2one('account.period', 'Period', required=True, domain=[('state', '=', 'draft')]),
 
47
        'description': fields.char('Description', size=64, required=True),
 
48
        'reference': fields.char('Reference', size=64, required=True),
 
49
        'expense_account_id': fields.many2one('account.account', 'Expense Account', required=True, domain=[('type', '!=', 'view'), ('user_type_code', '=', 'expense')]),
 
50
        'accrual_account_id': fields.many2one('account.account', 'Accrual Account', required=True, domain=[('type', '!=', 'view'), ('user_type_code', 'in', ['receivables', 'payables', 'debt'])]),
 
51
        'accrual_amount': fields.float('Accrual Amount', required=True),
 
52
        'currency_id': fields.many2one('res.currency', 'Currency', required=True),
 
53
        'journal_id': fields.many2one('account.journal', 'Journal', required=True),
 
54
        'third_party_type': fields.selection([('res.partner', 'Partner'),
 
55
                                              ('hr.employee', 'Employee')], 'Third Party', required=True),
 
56
        'partner_id': fields.many2one('res.partner', 'Third Party Partner'),
 
57
        'employee_id': fields.many2one('hr.employee', 'Third Party Employee'),
 
58
        'analytic_distribution_id': fields.many2one('analytic.distribution', 'Analytic Distribution'),
 
59
        'functional_amount': fields.function(_get_functional_amount, method=True, store=False, string="Functional Amount", type="float", readonly="True"),
 
60
        'functional_currency_id': fields.many2one('res.currency', 'Functional Currency', required=True, readonly=True),
 
61
        'state': fields.selection([('draft', 'Draft'),
 
62
                                   ('posted', 'Posted')], 'Status', required=True),
 
63
        # Field to store the third party's name for list view
 
64
        'third_party_name': fields.char('Third Party', size=64),
 
65
    }
 
66
    
 
67
    _defaults = {
 
68
        'third_party_type': 'res.partner',
 
69
        'journal_id': lambda self,cr,uid,c: self.pool.get('account.journal').search(cr, uid, [('code', '=', 'AC')])[0],
 
70
        'functional_currency_id': lambda self,cr,uid,c: self.pool.get('res.users').browse(cr, uid, uid, c).company_id.currency_id.id,
 
71
        'state': 'draft',
 
72
    }
 
73
    
 
74
    def create(self, cr, uid, vals, context=None):
 
75
        if context is None:
 
76
            context = {}
 
77
        if 'third_party_type' in vals:
 
78
            if vals['third_party_type'] == 'hr.employee' and 'employee_id' in vals:
 
79
                employee = self.pool.get('hr.employee').browse(cr, uid, vals['employee_id'], context=context)
 
80
                vals['third_party_name'] = employee.name
 
81
            elif vals['third_party_type'] == 'res.partner' and 'partner_id' in vals:
 
82
                partner = self.pool.get('res.partner').browse(cr, uid, vals['partner_id'], context=context)
 
83
                vals['third_party_name'] = partner.name
 
84
        if 'period_id' in vals:
 
85
            period = self.pool.get('account.period').browse(cr, uid, vals['period_id'], context=context)
 
86
            vals['date'] = period.date_stop
 
87
        return super(msf_accrual_line, self).create(cr, uid, vals, context=context)
 
88
    
 
89
    def write(self, cr, uid, ids, vals, context=None):
 
90
        if context is None:
 
91
            context = {}
 
92
        if 'third_party_type' in vals:
 
93
            if vals['third_party_type'] == 'hr.employee' and 'employee_id' in vals:
 
94
                employee = self.pool.get('hr.employee').browse(cr, uid, vals['employee_id'], context=context)
 
95
                vals['third_party_name'] = employee.name
 
96
            elif vals['third_party_type'] == 'res.partner' and 'partner_id' in vals:
 
97
                partner = self.pool.get('res.partner').browse(cr, uid, vals['partner_id'], context=context)
 
98
                vals['third_party_name'] = partner.name
 
99
        if 'period_id' in vals:
 
100
            period = self.pool.get('account.period').browse(cr, uid, vals['period_id'], context=context)
 
101
            vals['date'] = period.date_stop
 
102
        return super(msf_accrual_line, self).write(cr, uid, ids, vals, context=context)
 
103
    
 
104
    def button_duplicate(self, cr, uid, ids, context={}):
 
105
        """
 
106
        Copy given lines and delete all links
 
107
        """
 
108
        # Some verifications
 
109
        if not context:
 
110
            context = {}
 
111
        if isinstance(ids, (int, long)):
 
112
            ids = [ids]
 
113
        # Browse lines
 
114
        for line in self.browse(cr, uid, ids, context=context):
 
115
            default_vals = ({
 
116
                'description': '(copy) ' + line.description,
 
117
            })
 
118
            if line.analytic_distribution_id:
 
119
                # the distribution must be copied, not just the id
 
120
                new_distrib_id = self.pool.get('analytic.distribution').copy(cr, uid, line.analytic_distribution_id.id, {}, context=context)
 
121
                if new_distrib_id:
 
122
                    default_vals.update({'analytic_distribution_id': new_distrib_id})
 
123
            self.copy(cr, uid, line.id, default_vals, context=context)
 
124
        return True
 
125
    
 
126
    def button_analytic_distribution(self, cr, uid, ids, context={}):
 
127
        """
 
128
        Launch analytic distribution wizard on an invoice line
 
129
        """
 
130
        # Some verifications
 
131
        if not context:
 
132
            context = {}
 
133
        if isinstance(ids, (int, long)):
 
134
            ids = [ids]
 
135
        # Prepare some values
 
136
        accrual_line = self.browse(cr, uid, ids[0], context=context)
 
137
        # Search elements for currency
 
138
        currency = accrual_line.currency_id and accrual_line.currency_id.id
 
139
        # Get analytic distribution id from this line
 
140
        distrib_id = accrual_line and accrual_line.analytic_distribution_id and accrual_line.analytic_distribution_id.id or False
 
141
        # Prepare values for wizard
 
142
        vals = {
 
143
            'total_amount': accrual_line.accrual_amount or 0.0,
 
144
            'accrual_line_id': accrual_line.id,
 
145
            'currency_id': currency or False,
 
146
            'state': 'dispatch',
 
147
            'account_id': accrual_line.expense_account_id and accrual_line.expense_account_id.id or False,
 
148
        }
 
149
        if distrib_id:
 
150
            vals.update({'distribution_id': distrib_id,})
 
151
        # Create the wizard
 
152
        wiz_obj = self.pool.get('analytic.distribution.wizard')
 
153
        wiz_id = wiz_obj.create(cr, uid, vals, context=context)
 
154
        # Update some context values
 
155
        context.update({
 
156
            'active_id': ids[0],
 
157
            'active_ids': ids,
 
158
            'from_accrual_line': True
 
159
        })
 
160
        # Open it!
 
161
        return {
 
162
                'type': 'ir.actions.act_window',
 
163
                'res_model': 'analytic.distribution.wizard',
 
164
                'view_type': 'form',
 
165
                'view_mode': 'form',
 
166
                'target': 'new',
 
167
                'res_id': [wiz_id],
 
168
                'context': context,
 
169
        }
 
170
    
 
171
msf_accrual_line()
 
172
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: