1
# -*- coding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
5
# Copyright (C) 2011 MSF, TeMPO Consulting.
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.
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.
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/>.
20
##############################################################################
22
from osv import fields, osv
24
class msf_accrual_line(osv.osv):
25
_name = 'msf.accrual.line'
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}}
31
def _get_functional_amount(self, cr, uid, ids, field_name, arg, context=None):
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,
37
accrual_line.currency_id.id,
38
accrual_line.functional_currency_id.id,
39
accrual_line.accrual_amount or 0.0,
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),
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,
74
def create(self, cr, uid, vals, context=None):
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)
89
def write(self, cr, uid, ids, vals, context=None):
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)
104
def button_duplicate(self, cr, uid, ids, context={}):
106
Copy given lines and delete all links
111
if isinstance(ids, (int, long)):
114
for line in self.browse(cr, uid, ids, context=context):
116
'description': '(copy) ' + line.description,
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)
122
default_vals.update({'analytic_distribution_id': new_distrib_id})
123
self.copy(cr, uid, line.id, default_vals, context=context)
126
def button_analytic_distribution(self, cr, uid, ids, context={}):
128
Launch analytic distribution wizard on an invoice line
133
if isinstance(ids, (int, long)):
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
143
'total_amount': accrual_line.accrual_amount or 0.0,
144
'accrual_line_id': accrual_line.id,
145
'currency_id': currency or False,
147
'account_id': accrual_line.expense_account_id and accrual_line.expense_account_id.id or False,
150
vals.update({'distribution_id': distrib_id,})
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
158
'from_accrual_line': True
162
'type': 'ir.actions.act_window',
163
'res_model': 'analytic.distribution.wizard',
172
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: