[FIX] account_budget: theoretical amount: ignore future and use datetime

This commit is contained in:
Simon Lejeune 2014-10-22 17:50:02 +02:00
parent 1561a528a5
commit 4bdf04578d
1 changed files with 21 additions and 18 deletions

View File

@ -22,14 +22,20 @@
import datetime import datetime
from openerp.osv import fields, osv from openerp.osv import fields, osv
from openerp.tools import ustr from openerp.tools import ustr, DEFAULT_SERVER_DATE_FORMAT
from openerp.tools.translate import _ from openerp.tools.translate import _
import openerp.addons.decimal_precision as dp import openerp.addons.decimal_precision as dp
# ---------------------------------------------------------
# Utils
# ---------------------------------------------------------
def strToDate(dt): def strToDate(dt):
dt_date=datetime.date(int(dt[0:4]),int(dt[5:7]),int(dt[8:10])) return datetime.date(int(dt[0:4]), int(dt[5:7]), int(dt[8:10]))
return dt_date
def strToDatetime(strdate):
return datetime.datetime.strptime(strdate, DEFAULT_SERVER_DATE_FORMAT)
# --------------------------------------------------------- # ---------------------------------------------------------
# Budgets # Budgets
@ -135,17 +141,12 @@ class crossovered_budget_lines(osv.osv):
return res return res
def _theo_amt(self, cr, uid, ids, context=None): def _theo_amt(self, cr, uid, ids, context=None):
res = {} if context is None:
if context is None:
context = {} context = {}
res = {}
for line in self.browse(cr, uid, ids, context=context): for line in self.browse(cr, uid, ids, context=context):
today = datetime.datetime.today() today = datetime.datetime.now()
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: if line.paid_date:
if strToDate(line.date_to) <= strToDate(line.paid_date): if strToDate(line.date_to) <= strToDate(line.paid_date):
@ -153,13 +154,15 @@ class crossovered_budget_lines(osv.osv):
else: else:
theo_amt = line.planned_amount theo_amt = line.planned_amount
else: else:
total = strToDate(line.date_to) - strToDate(line.date_from) line_timedelta = strToDatetime(line.date_to) - strToDatetime(line.date_from)
elapsed = min(strToDate(line.date_to),strToDate(date_to)) - max(strToDate(line.date_from),strToDate(date_from)) elapsed_timedelta = today - (strToDatetime(line.date_from))
if strToDate(date_to) < strToDate(line.date_from):
elapsed = strToDate(date_to) - strToDate(date_to)
if total.days: if elapsed_timedelta.days < 0:
theo_amt = float((elapsed.days + 1) / float(total.days + 1)) * line.planned_amount # If the budget line has not started yet, theoretical amount should be zero
theo_amt = 0.00
elif line_timedelta.days > 0 and today < strToDatetime(line.date_to):
# If today is between the budget line date_from and date_to
theo_amt = (elapsed_timedelta.total_seconds() / line_timedelta.total_seconds()) * line.planned_amount
else: else:
theo_amt = line.planned_amount theo_amt = line.planned_amount