~noviat/openobject-addons/extra-6.0

« back to all changes in this revision

Viewing changes to account_cashflow/wizard/calc_cashflow_opening_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_opening_balance(osv.osv_memory):
 
31
    _name = 'calc.cashflow.opening.balance'
 
32
    _description = 'Recalculate Cash Flow Opening 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_opening_balance(self, cr, uid, ids, context=None):
 
40
        balopen_obj = self.pool.get('account.cashflow.opening.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
 
 
53
        nbr_days = (date_stop - date_start).days + 1
 
54
        if nbr_days < 1 :
 
55
            raise osv.except_osv(_('Error!'), _('Invalid Date Range!'))
 
56
            return {}
 
57
 
 
58
        cr.execute("SELECT id FROM account_cashflow_code WHERE type='init' AND company_id = %s", (company_id,))
 
59
        res_init_id = cr.fetchall()
 
60
        if len(res_init_id) != 1:
 
61
            err_str = len(res_init_id) and 'Multiple' or 'No' 
 
62
            raise osv.except_osv('Configuration Error', 
 
63
                _("%s Cash Flow Codes of type='Initial Balance' defined for your Company !") % err_str)
 
64
        balance_init_id = res_init_id[0][0]
 
65
 
 
66
        for x in range(0, nbr_days):
 
67
            date = (date_start + timedelta(days=x)).isoformat()
 
68
            balopen_obj.calc_opening_balance(cr, uid, date, balance_init_id, company_id)
 
69
 
 
70
        return {}
 
71
 
 
72
    _defaults = {
 
73
        'date_start': lambda *a: time.strftime('%Y-%m-%d'),
 
74
        'date_stop': lambda *a: time.strftime('%Y-%m-%d'),
 
75
        'company_id': lambda self,cr,uid,c: self.pool.get('res.users').browse(cr, uid, uid, c).company_id.id,
 
76
    }
 
77
 
 
78
calc_cashflow_opening_balance()
 
79
 
 
80