1
# -*- encoding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
6
# Copyright (c) 2011 Noviat nv/sa (www.noviat.be). 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
##############################################################################
24
from datetime import datetime, date, timedelta
25
from osv import osv, fields
27
from tools.translate import _
28
logger=netsvc.Logger()
30
class calc_cashflow_balance(osv.osv_memory):
31
_name = 'calc.cashflow.balance'
32
_description = 'Recalculate Cash Flow Balances'
34
'date_start': fields.date('Start Date', required=True),
35
'date_stop': fields.date('End Date', required=True),
36
'company_id': fields.many2one('res.company', 'Company', required=True),
39
def calc_cashflow_balance(self, cr, uid, ids, context=None):
40
cfbalance_obj = self.pool.get('account.cashflow.balance')
45
data = self.read(cr, uid, ids, [], context=context)[0]
47
raise osv.except_osv(_('Error!'), _('Wizard in incorrect state. Please hit the Cancel button!'))
49
date_start = datetime.strptime(data['date_start'], '%Y-%m-%d').date()
50
date_stop = datetime.strptime(data['date_stop'], '%Y-%m-%d').date()
51
company_id = data['company_id']
52
nbr_days = (date_stop - date_start).days + 1
54
raise osv.except_osv(_('Error!'), _('Invalid Date Range!'))
57
for x in range(0, nbr_days):
58
day = (date_start + timedelta(days=x)).isoformat()
60
cr.execute('SELECT cashflow_code_id, sum(amount) FROM ( ' \
61
'(SELECT c.cashflow_code_id AS cashflow_code_id, sum(l.amount) AS amount ' \
62
'FROM account_cashflow_line AS c '\
63
'INNER JOIN account_bank_statement_line AS l ON c.st_line_id=l.id ' \
64
'WHERE l.val_date = %s and c.cashflow_code_id IS NOT NULL ' \
65
'GROUP BY c.cashflow_code_id) ' \
67
'(SELECT p.cashflow_code_id AS cashflow_code_id, sum(p.amount) AS amount ' \
68
'FROM account_cashflow_provision_line AS p ' \
69
'WHERE p.val_date = %s ' \
70
'GROUP BY p.cashflow_code_id) ' \
71
') AS u GROUP BY u.cashflow_code_id',
73
balances = cr.fetchall()
75
bal_ids = cfbalance_obj.search(cr, uid, [('date', '=', day)], context=context)
77
cfbalance_obj.write(cr, uid, bal_ids, {'state': 'draft'}, context=context)
78
cfbalance_obj.unlink(cr, uid, bal_ids, context=context)
79
# create new balances with the calculated values
80
for balance in balances:
81
cfbalance_obj.create(cr, uid, {
83
'cashflow_code_id': balance[0],
84
'balance': balance[1],
90
'date_start': lambda *a: time.strftime('%Y-%m-%d'),
91
'date_stop': lambda *a: time.strftime('%Y-%m-%d'),
92
'company_id': lambda self,cr,uid,c: self.pool.get('res.users').browse(cr, uid, uid, c).company_id.id,
95
calc_cashflow_balance()