~noviat/openobject-addons/extra-6.0

« back to all changes in this revision

Viewing changes to account_cashflow/wizard/calc_cashflow_balance.py

  • Committer: root
  • Date: 2012-04-10 20:37:39 UTC
  • Revision ID: root@oerp61-20120410203739-flykplw8jzpqa15c
update noviat v6.0 accounting modules

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    OpenERP, Open Source Management Solution
 
5
#    
 
6
#    Copyright (c) 2011 Noviat nv/sa (www.noviat.be). All rights reserved.
 
7
 
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.
 
12
#
 
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.
 
17
#
 
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/>.
 
20
#
 
21
##############################################################################
 
22
 
 
23
import time
 
24
from datetime import datetime, date, timedelta
 
25
from osv import osv, fields
 
26
import netsvc
 
27
from tools.translate import _
 
28
logger=netsvc.Logger()
 
29
 
 
30
class calc_cashflow_balance(osv.osv_memory):
 
31
    _name = 'calc.cashflow.balance'
 
32
    _description = 'Recalculate Cash Flow Balances'
 
33
    _columns = {
 
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),
 
37
    }
 
38
 
 
39
    def calc_cashflow_balance(self, cr, uid, ids, context=None):
 
40
        cfbalance_obj = self.pool.get('account.cashflow.balance')
 
41
        if context is None:
 
42
            context = {}
 
43
 
 
44
        try:            
 
45
            data = self.read(cr, uid, ids, [], context=context)[0]
 
46
        except:
 
47
            raise osv.except_osv(_('Error!'), _('Wizard in incorrect state. Please hit the Cancel button!'))
 
48
            return {}
 
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
 
53
        if nbr_days < 1 :
 
54
            raise osv.except_osv(_('Error!'), _('Invalid Date Range!'))
 
55
            return {}
 
56
 
 
57
        for x in range(0, nbr_days):
 
58
            day = (date_start + timedelta(days=x)).isoformat()
 
59
            # calculate balances
 
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) ' \
 
66
                'UNION ' \
 
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', 
 
72
                (day,day))
 
73
            balances = cr.fetchall()
 
74
            # delete old balances
 
75
            bal_ids = cfbalance_obj.search(cr, uid, [('date', '=', day)], context=context)
 
76
            if bal_ids:
 
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, {
 
82
                    'date': day,
 
83
                    'cashflow_code_id': balance[0],
 
84
                    'balance': balance[1],  
 
85
                    'state': 'confirm',
 
86
                    }, context=context)
 
87
        return {}
 
88
 
 
89
    _defaults = {
 
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,
 
93
    }
 
94
 
 
95
calc_cashflow_balance()
 
96
 
 
97