2009-10-13 05:58:37 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2008-11-03 19:18:56 +00:00
|
|
|
##############################################################################
|
2009-11-27 11:35:13 +00:00
|
|
|
#
|
2009-10-14 11:15:34 +00:00
|
|
|
# OpenERP, Open Source Management Solution
|
2010-01-12 09:18:39 +00:00
|
|
|
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
|
2008-11-03 19:18:56 +00:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
2009-10-14 11:15:34 +00:00
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
2008-11-03 19:18:56 +00:00
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2009-10-14 11:15:34 +00:00
|
|
|
# GNU Affero General Public License for more details.
|
2008-11-03 19:18:56 +00:00
|
|
|
#
|
2009-10-14 11:15:34 +00:00
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
2009-11-27 11:35:13 +00:00
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2008-11-03 19:18:56 +00:00
|
|
|
#
|
|
|
|
##############################################################################
|
2010-09-28 12:58:00 +00:00
|
|
|
|
2010-09-01 08:23:26 +00:00
|
|
|
import datetime
|
2009-10-14 11:15:34 +00:00
|
|
|
|
2010-09-24 05:48:48 +00:00
|
|
|
from osv import osv, fields
|
2009-03-06 22:18:24 +00:00
|
|
|
from tools.translate import _
|
2008-08-24 14:45:43 +00:00
|
|
|
|
2010-10-01 11:19:29 +00:00
|
|
|
import decimal_precision as dp
|
|
|
|
|
2008-08-24 14:45:43 +00:00
|
|
|
def strToDate(dt):
|
|
|
|
dt_date=datetime.date(int(dt[0:4]),int(dt[5:7]),int(dt[8:10]))
|
|
|
|
return dt_date
|
2008-11-12 13:54:09 +00:00
|
|
|
|
2008-09-11 10:02:28 +00:00
|
|
|
# ---------------------------------------------------------
|
|
|
|
# Budgets
|
|
|
|
# ---------------------------------------------------------
|
|
|
|
class account_budget_post(osv.osv):
|
2010-10-12 12:50:50 +00:00
|
|
|
_name = "account.budget.post"
|
|
|
|
_description = "Budgetary Position"
|
2008-09-11 10:02:28 +00:00
|
|
|
_columns = {
|
|
|
|
'code': fields.char('Code', size=64, required=True),
|
|
|
|
'name': fields.char('Name', size=256, required=True),
|
|
|
|
'account_ids': fields.many2many('account.account', 'account_budget_rel', 'budget_id', 'account_id', 'Accounts'),
|
|
|
|
'crossovered_budget_line': fields.one2many('crossovered.budget.lines', 'general_budget_id', 'Budget Lines'),
|
2009-11-10 12:49:20 +00:00
|
|
|
'company_id': fields.many2one('res.company', 'Company', required=True),
|
2008-09-11 10:02:28 +00:00
|
|
|
}
|
|
|
|
_defaults = {
|
2010-09-28 12:58:00 +00:00
|
|
|
'company_id': lambda self, cr, uid, c: self.pool.get('res.company')._company_default_get(cr, uid, 'account.budget.post', context=c)
|
2008-09-11 10:02:28 +00:00
|
|
|
}
|
2010-09-01 07:01:00 +00:00
|
|
|
_order = "name"
|
2008-09-11 10:02:28 +00:00
|
|
|
|
|
|
|
account_budget_post()
|
|
|
|
|
2008-08-24 14:45:43 +00:00
|
|
|
|
|
|
|
class crossovered_budget(osv.osv):
|
|
|
|
_name = "crossovered.budget"
|
2008-11-12 13:54:09 +00:00
|
|
|
_description = "Budget"
|
2008-08-24 14:45:43 +00:00
|
|
|
|
|
|
|
_columns = {
|
2010-09-01 07:22:57 +00:00
|
|
|
'name': fields.char('Name', size=64, required=True, states={'done':[('readonly',True)]}),
|
|
|
|
'code': fields.char('Code', size=16, required=True, states={'done':[('readonly',True)]}),
|
2010-06-16 11:51:39 +00:00
|
|
|
'creating_user_id': fields.many2one('res.users', 'Responsible User'),
|
|
|
|
'validating_user_id': fields.many2one('res.users', 'Validate User', readonly=True),
|
|
|
|
'date_from': fields.date('Start Date', required=True, states={'done':[('readonly',True)]}),
|
|
|
|
'date_to': fields.date('End Date', required=True, states={'done':[('readonly',True)]}),
|
2012-05-04 12:59:11 +00:00
|
|
|
'state' : fields.selection([('draft','Draft'),('cancel', 'Cancelled'),('confirm','Confirmed'),('validate','Validated'),('done','Done')], 'Status', select=True, required=True, readonly=True),
|
2010-09-27 09:19:07 +00:00
|
|
|
'crossovered_budget_line': fields.one2many('crossovered.budget.lines', 'crossovered_budget_id', 'Budget Lines', states={'done':[('readonly',True)]}),
|
|
|
|
'company_id': fields.many2one('res.company', 'Company', required=True),
|
2008-08-24 14:45:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_defaults = {
|
2010-09-01 08:23:26 +00:00
|
|
|
'state': 'draft',
|
2010-09-29 12:51:08 +00:00
|
|
|
'creating_user_id': lambda self, cr, uid, context: uid,
|
|
|
|
'company_id': lambda self, cr, uid, c: self.pool.get('res.company')._company_default_get(cr, uid, 'account.budget.post', context=c)
|
2008-08-24 14:45:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def budget_confirm(self, cr, uid, ids, *args):
|
|
|
|
self.write(cr, uid, ids, {
|
2010-09-29 12:51:08 +00:00
|
|
|
'state': 'confirm'
|
2008-08-24 14:45:43 +00:00
|
|
|
})
|
|
|
|
return True
|
2010-09-01 07:43:35 +00:00
|
|
|
|
|
|
|
def budget_draft(self, cr, uid, ids, *args):
|
|
|
|
self.write(cr, uid, ids, {
|
2010-09-29 12:51:08 +00:00
|
|
|
'state': 'draft'
|
2010-09-01 07:43:35 +00:00
|
|
|
})
|
|
|
|
return True
|
2008-08-24 14:45:43 +00:00
|
|
|
|
|
|
|
def budget_validate(self, cr, uid, ids, *args):
|
|
|
|
self.write(cr, uid, ids, {
|
2010-09-29 12:51:08 +00:00
|
|
|
'state': 'validate',
|
2008-08-24 14:45:43 +00:00
|
|
|
'validating_user_id': uid,
|
|
|
|
})
|
|
|
|
return True
|
|
|
|
|
|
|
|
def budget_cancel(self, cr, uid, ids, *args):
|
|
|
|
self.write(cr, uid, ids, {
|
2010-09-29 12:51:08 +00:00
|
|
|
'state': 'cancel'
|
2008-08-24 14:45:43 +00:00
|
|
|
})
|
|
|
|
return True
|
|
|
|
|
|
|
|
def budget_done(self, cr, uid, ids, *args):
|
|
|
|
self.write(cr, uid, ids, {
|
2010-09-29 12:51:08 +00:00
|
|
|
'state': 'done'
|
2008-08-24 14:45:43 +00:00
|
|
|
})
|
|
|
|
return True
|
|
|
|
|
|
|
|
crossovered_budget()
|
|
|
|
|
|
|
|
class crossovered_budget_lines(osv.osv):
|
|
|
|
|
2010-11-19 13:48:01 +00:00
|
|
|
def _prac_amt(self, cr, uid, ids, context=None):
|
2008-08-24 14:45:43 +00:00
|
|
|
res = {}
|
2010-10-20 13:54:21 +00:00
|
|
|
result = 0.0
|
2010-12-13 06:43:09 +00:00
|
|
|
if context is None:
|
|
|
|
context = {}
|
2010-11-19 13:48:01 +00:00
|
|
|
for line in self.browse(cr, uid, ids, context=context):
|
2009-01-27 18:10:43 +00:00
|
|
|
acc_ids = [x.id for x in line.general_budget_id.account_ids]
|
2008-08-24 14:45:43 +00:00
|
|
|
if not acc_ids:
|
2011-12-31 07:57:20 +00:00
|
|
|
raise osv.except_osv(_('Error!'),_("The Budget '%s' has no accounts!") % str(line.general_budget_id.name))
|
2008-08-24 14:45:43 +00:00
|
|
|
date_to = line.date_to
|
|
|
|
date_from = line.date_from
|
|
|
|
if context.has_key('wizard_date_from'):
|
|
|
|
date_from = context['wizard_date_from']
|
|
|
|
if context.has_key('wizard_date_to'):
|
|
|
|
date_to = context['wizard_date_to']
|
2010-10-20 13:54:21 +00:00
|
|
|
if line.analytic_account_id.id:
|
|
|
|
cr.execute("SELECT SUM(amount) FROM account_analytic_line WHERE account_id=%s AND (date "
|
2010-09-29 12:51:08 +00:00
|
|
|
"between to_date(%s,'yyyy-mm-dd') AND to_date(%s,'yyyy-mm-dd')) AND "
|
2009-11-27 11:35:13 +00:00
|
|
|
"general_account_id=ANY(%s)", (line.analytic_account_id.id, date_from, date_to,acc_ids,))
|
2010-10-20 13:54:21 +00:00
|
|
|
result = cr.fetchone()[0]
|
2009-01-27 18:10:43 +00:00
|
|
|
if result is None:
|
|
|
|
result = 0.00
|
|
|
|
res[line.id] = result
|
2008-08-24 14:45:43 +00:00
|
|
|
return res
|
|
|
|
|
2010-11-19 13:48:01 +00:00
|
|
|
def _prac(self, cr, uid, ids, name, args, context=None):
|
2008-09-25 13:02:10 +00:00
|
|
|
res={}
|
2010-11-19 13:48:01 +00:00
|
|
|
for line in self.browse(cr, uid, ids, context=context):
|
2010-09-29 12:51:08 +00:00
|
|
|
res[line.id] = self._prac_amt(cr, uid, [line.id], context=context)[line.id]
|
2008-09-25 13:02:10 +00:00
|
|
|
return res
|
|
|
|
|
2010-11-19 13:48:01 +00:00
|
|
|
def _theo_amt(self, cr, uid, ids, context=None):
|
2008-08-24 14:45:43 +00:00
|
|
|
res = {}
|
2010-12-13 06:43:09 +00:00
|
|
|
if context is None:
|
|
|
|
context = {}
|
2010-11-19 13:48:01 +00:00
|
|
|
for line in self.browse(cr, uid, ids, context=context):
|
2010-09-29 12:51:08 +00:00
|
|
|
today = datetime.datetime.today()
|
2008-08-24 14:45:43 +00:00
|
|
|
date_to = today.strftime("%Y-%m-%d")
|
|
|
|
date_from = line.date_from
|
|
|
|
if context.has_key('wizard_date_from'):
|
|
|
|
date_from = context['wizard_date_from']
|
|
|
|
if context.has_key('wizard_date_to'):
|
|
|
|
date_to = context['wizard_date_to']
|
|
|
|
|
|
|
|
if line.paid_date:
|
2010-09-29 12:51:08 +00:00
|
|
|
if strToDate(line.date_to) <= strToDate(line.paid_date):
|
|
|
|
theo_amt = 0.00
|
2008-08-24 14:45:43 +00:00
|
|
|
else:
|
2010-09-29 12:51:08 +00:00
|
|
|
theo_amt = line.planned_amount
|
2008-08-24 14:45:43 +00:00
|
|
|
else:
|
2010-09-29 12:51:08 +00:00
|
|
|
total = strToDate(line.date_to) - strToDate(line.date_from)
|
2008-08-24 14:45:43 +00:00
|
|
|
elapsed = min(strToDate(line.date_to),strToDate(date_to)) - max(strToDate(line.date_from),strToDate(date_from))
|
|
|
|
if strToDate(date_to) < strToDate(line.date_from):
|
|
|
|
elapsed = strToDate(date_to) - strToDate(date_to)
|
2009-11-27 11:35:13 +00:00
|
|
|
|
2009-04-22 12:59:01 +00:00
|
|
|
if total.days:
|
2010-09-29 12:51:08 +00:00
|
|
|
theo_amt = float(elapsed.days / float(total.days)) * line.planned_amount
|
2009-04-22 12:59:01 +00:00
|
|
|
else:
|
|
|
|
theo_amt = line.planned_amount
|
2008-08-24 14:45:43 +00:00
|
|
|
|
2010-09-29 12:51:08 +00:00
|
|
|
res[line.id] = theo_amt
|
2008-08-24 14:45:43 +00:00
|
|
|
return res
|
|
|
|
|
2010-11-19 13:48:01 +00:00
|
|
|
def _theo(self, cr, uid, ids, name, args, context=None):
|
2010-09-29 12:51:08 +00:00
|
|
|
res = {}
|
2010-11-19 13:48:01 +00:00
|
|
|
for line in self.browse(cr, uid, ids, context=context):
|
2010-09-29 12:51:08 +00:00
|
|
|
res[line.id] = self._theo_amt(cr, uid, [line.id], context=context)[line.id]
|
2008-09-25 13:02:10 +00:00
|
|
|
return res
|
|
|
|
|
2010-11-19 13:48:01 +00:00
|
|
|
def _perc(self, cr, uid, ids, name, args, context=None):
|
2008-08-24 14:45:43 +00:00
|
|
|
res = {}
|
2010-11-19 13:48:01 +00:00
|
|
|
for line in self.browse(cr, uid, ids, context=context):
|
2010-09-29 12:51:08 +00:00
|
|
|
if line.theoritical_amount <> 0.00:
|
2010-12-09 09:54:30 +00:00
|
|
|
res[line.id] = float((line.practical_amount or 0.0) / line.theoritical_amount) * 100
|
2008-08-24 14:45:43 +00:00
|
|
|
else:
|
2010-09-29 12:51:08 +00:00
|
|
|
res[line.id] = 0.00
|
2008-08-24 14:45:43 +00:00
|
|
|
return res
|
2010-09-28 12:58:00 +00:00
|
|
|
|
|
|
|
_name = "crossovered.budget.lines"
|
2010-05-19 18:32:32 +00:00
|
|
|
_description = "Budget Line"
|
2008-08-24 14:45:43 +00:00
|
|
|
_columns = {
|
2008-11-12 13:54:09 +00:00
|
|
|
'crossovered_budget_id': fields.many2one('crossovered.budget', 'Budget', ondelete='cascade', select=True, required=True),
|
2011-03-21 11:05:19 +00:00
|
|
|
'analytic_account_id': fields.many2one('account.analytic.account', 'Analytic Account',required=True),
|
2008-11-12 13:54:09 +00:00
|
|
|
'general_budget_id': fields.many2one('account.budget.post', 'Budgetary Position',required=True),
|
2010-06-16 11:51:39 +00:00
|
|
|
'date_from': fields.date('Start Date', required=True),
|
|
|
|
'date_to': fields.date('End Date', required=True),
|
2008-08-24 14:45:43 +00:00
|
|
|
'paid_date': fields.date('Paid Date'),
|
2010-10-01 11:19:29 +00:00
|
|
|
'planned_amount':fields.float('Planned Amount', required=True, digits_compute=dp.get_precision('Account')),
|
2011-07-01 23:41:24 +00:00
|
|
|
'practical_amount':fields.function(_prac, string='Practical Amount', type='float', digits_compute=dp.get_precision('Account')),
|
|
|
|
'theoritical_amount':fields.function(_theo, string='Theoretical Amount', type='float', digits_compute=dp.get_precision('Account')),
|
|
|
|
'percentage':fields.function(_perc, string='Percentage', type='float'),
|
2010-09-27 09:19:07 +00:00
|
|
|
'company_id': fields.related('crossovered_budget_id', 'company_id', type='many2one', relation='res.company', string='Company', store=True, readonly=True)
|
2008-08-24 14:45:43 +00:00
|
|
|
}
|
2010-09-24 05:48:48 +00:00
|
|
|
|
2008-08-24 14:45:43 +00:00
|
|
|
crossovered_budget_lines()
|
|
|
|
|
|
|
|
class account_analytic_account(osv.osv):
|
2010-10-12 12:50:50 +00:00
|
|
|
_inherit = "account.analytic.account"
|
2008-08-24 14:45:43 +00:00
|
|
|
|
|
|
|
_columns = {
|
2010-10-12 12:50:50 +00:00
|
|
|
'crossovered_budget_line': fields.one2many('crossovered.budget.lines', 'analytic_account_id', 'Budget Lines'),
|
2008-08-24 14:45:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
account_analytic_account()
|
|
|
|
|
2010-10-01 11:19:29 +00:00
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|