2
# -*- coding: utf-8 -*-
3
##############################################################################
5
# OpenERP, Open Source Management Solution
6
# Copyright (C) 2011 TeMPO Consulting, MSF. All Rights Reserved
8
# This program is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU Affero General Public License as
10
# published by the Free Software Foundation, either version 3 of the
11
# License, or (at your option) any later version.
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU Affero General Public License for more details.
18
# You should have received a copy of the GNU Affero General Public License
19
# along with this program. If not, see <http://www.gnu.org/licenses/>.
21
##############################################################################
22
from osv import fields, osv
24
from datetime import datetime
25
from dateutil.relativedelta import relativedelta
26
from tools.translate import _
28
class account_model_line(osv.osv):
29
_name = "account.model.line"
30
_inherit = "account.model.line"
33
'analytic_distribution_id': fields.many2one('analytic.distribution', 'Analytic Distribution'),
34
'account_user_type_code': fields.related('account_id', 'user_type_code', type="char", string="Account User Type Code", store=False)
37
def button_analytic_distribution(self, cr, uid, ids, context=None):
39
Launch analytic distribution wizard on an invoice line
44
if isinstance(ids, (int, long)):
47
raise osv.except_osv(_('Error'), _('No model line given. Please save your model line before.'))
48
model_line = self.browse(cr, uid, ids[0], context=context)
50
amount = abs(model_line.debit - model_line.credit)
51
currency = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.currency_id.id
52
# Get analytic distribution id from this line
53
distrib_id = model_line.analytic_distribution_id and model_line.analytic_distribution_id.id or False
54
# Prepare values for wizard
56
'total_amount': amount,
57
'model_line_id': model_line.id,
58
'currency_id': currency,
60
'account_id': model_line.account_id.id,
63
vals.update({'distribution_id': distrib_id,})
65
wiz_obj = self.pool.get('analytic.distribution.wizard')
66
wiz_id = wiz_obj.create(cr, uid, vals, context=context)
67
# Update some context values
74
'name': 'Analytic distribution',
75
'type': 'ir.actions.act_window',
76
'res_model': 'analytic.distribution.wizard',
86
class account_model(osv.osv):
87
_name = "account.model"
88
_inherit = "account.model"
90
# @@@override@account.account_model.generate()
91
def generate(self, cr, uid, ids, datas={}, context=None):
94
account_move_obj = self.pool.get('account.move')
95
account_move_line_obj = self.pool.get('account.move.line')
96
pt_obj = self.pool.get('account.payment.term')
97
ana_obj = self.pool.get('analytic.distribution')
102
if datas.get('date', False):
103
context.update({'date': datas['date']})
105
period_id = self.pool.get('account.period').find(cr, uid, dt=context.get('date', False))
107
raise osv.except_osv(_('No period found !'), _('Unable to find a valid period !'))
108
period_id = period_id[0]
110
for model in self.browse(cr, uid, ids, context=context):
111
entry['name'] = model.name%{'year':time.strftime('%Y'), 'month':time.strftime('%m'), 'date':time.strftime('%Y-%m')}
112
move_id = account_move_obj.create(cr, uid, {
113
'ref': entry['name'],
114
'period_id': period_id,
115
'journal_id': model.journal_id.id,
116
'date': context.get('date',time.strftime('%Y-%m-%d'))
118
move_ids.append(move_id)
119
for line in model.lines_id:
122
'journal_id': model.journal_id.id,
123
'period_id': period_id,
125
if line.account_id.user_type_code == 'expense':
126
if not line.analytic_distribution_id:
127
raise osv.except_osv(_('No Analytic Distribution !'),_("You have to define an analytic distribution on the '%s' line!") % (line.name))
128
if not model.journal_id.analytic_journal_id:
129
raise osv.except_osv(_('No Analytic Journal !'),_("You have to define an analytic journal on the '%s' journal!") % (model.journal_id.name,))
130
new_distribution_id = ana_obj.copy(cr, uid, line.analytic_distribution_id.id, {}, context=context)
131
val.update({'analytic_distribution_id': new_distribution_id})
133
date_maturity = time.strftime('%Y-%m-%d')
134
if line.date_maturity == 'partner':
135
if not line.partner_id:
136
raise osv.except_osv(_('Error !'), _("Maturity date of entry line generated by model line '%s' of model '%s' is based on partner payment term!" \
137
"\nPlease define partner on it!")%(line.name, model.name))
138
if line.partner_id.property_payment_term:
139
payment_term_id = line.partner_id.property_payment_term.id
140
pterm_list = pt_obj.compute(cr, uid, payment_term_id, value=1, date_ref=date_maturity)
142
pterm_list = [l[0] for l in pterm_list]
144
date_maturity = pterm_list[-1]
148
'quantity': line.quantity,
150
'credit': line.credit,
151
'account_id': line.account_id.id,
153
'partner_id': line.partner_id.id,
154
'date': context.get('date',time.strftime('%Y-%m-%d')),
155
'date_maturity': date_maturity
158
c.update({'journal_id': model.journal_id.id,'period_id': period_id})
159
account_move_line_obj.create(cr, uid, val, context=c)
165
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: