~openbias/bias-trunk/bias-public-trunk

« back to all changes in this revision

Viewing changes to bias_account_report/wizard/wizard_fiscal_statements_balance.py

  • Committer: Jose Patricio
  • Date: 2011-10-19 03:16:40 UTC
  • Revision ID: josepato@bias.com.mx-20111019031640-05zd7r5lxwx084qu
el push inicial

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
#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
 
6
#    $Id$
 
7
#
 
8
#    This program is free software: you can redistribute it and/or modify
 
9
#    it under the terms of the GNU General Public License as published by
 
10
#    the Free Software Foundation, either version 3 of the License, or
 
11
#    (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 General Public License for more details.
 
17
#
 
18
#    You should have received a copy of the GNU General Public License
 
19
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
#
 
21
##############################################################################
 
22
 
 
23
import time
 
24
import wizard
 
25
import pooler
 
26
from tools.translate import _
 
27
import StringIO
 
28
import base64
 
29
import re
 
30
 
 
31
fiscal_form = '''<?xml version="1.0"?>
 
32
<form string="Fiscal Statements">
 
33
        <field name="company_id" colspan="4"/>
 
34
        <newline/>
 
35
        <field name="fiscalyear"/>
 
36
        <field domain="[('fiscalyear_id','=',fiscalyear)]" name="period_id"/>
 
37
        <field name="level"/>
 
38
        <field name="cost_center_id"/>
 
39
</form>'''
 
40
 
 
41
fiscal_fields = {
 
42
    'company_id': {'string': 'Company', 'type': 'many2one', 'relation': 'res.company', 'required': True},
 
43
    'period_id': {'string': 'Period', 'type': 'many2one', 'relation':'account.period', 'required': True},
 
44
    'fiscal_statements_id': {'string': 'Fiscal Statement', 'type': 'many2one', 'relation': 'fiscal.statements', 'readonly': True},
 
45
    'fiscalyear': {
 
46
        'string':'Fiscal year', 'type': 'many2one', 'relation': 'account.fiscalyear', 'required': True,
 
47
        'help': 'Keep empty for all open fiscal year',
 
48
        'default': lambda *a:False},
 
49
    'level': {'string': 'Level', 'type': 'integer', 'default': lambda *a:1},
 
50
    'cost_center_id': {'string': 'Cost Center', 'type': 'many2one', 'relation': 'account.cost.center', 'help': 'Keep empty for all cost centers'},
 
51
        'journal_ids': {'string': 'Journal', 'type': 'many2many', 'relation': 'account.journal', 'help': 'Keep empty for all journals'},
 
52
}
 
53
 
 
54
export_form = '''<?xml version="1.0"?>
 
55
<form string="Export Report">
 
56
        <separator string="File" colspan="4"/>
 
57
        <field name="file"/>
 
58
</form>'''
 
59
 
 
60
export_fields = {
 
61
        'file': {'string': 'File', 'type': 'binary', 'help': 'File created for this report', 'readonly':True},
 
62
 
 
63
}
 
64
 
 
65
 
 
66
 
 
67
def _get_period(self, cr, uid, data, context={}):
 
68
    pool = pooler.get_pool(cr.dbname)
 
69
    fiscalyear_obj = pool.get('account.fiscalyear')
 
70
    ids = pool.get('account.period').find(cr, uid, context=context)
 
71
    user = pooler.get_pool(cr.dbname).get('res.users').browse(cr, uid, uid, context=context)
 
72
    if user.company_id:
 
73
        company_id = user.company_id.id
 
74
    else:
 
75
        company_id = pooler.get_pool(cr.dbname).get('res.company').search(cr, uid, [('parent_id', '=', False)])[0]
 
76
    report_id = pool.get('financial.reports').search(cr, uid, [('company_id','=',company_id)])[0]
 
77
    statement = pool.get('financial.reports').browse(cr, uid, report_id).statement_balance_id.id
 
78
    period_id = False
 
79
    if len(ids):
 
80
        period_id = ids[0]
 
81
    return {
 
82
        'fiscalyear': fiscalyear_obj.find(cr, uid),
 
83
        'company_id': company_id,
 
84
        'period_id': period_id,
 
85
        'fiscal_statements_id': statement
 
86
    }
 
87
 
 
88
 
 
89
class wizard_fiscal_statements_balance(wizard.interface):
 
90
 
 
91
    def _check_ouput_csv(self, cr, uid, data, context):
 
92
        import csv
 
93
        import types
 
94
        result = pooler.get_pool(cr.dbname).get('fiscal.statements').get_result(cr, uid, data, context)
 
95
        buf = StringIO.StringIO()
 
96
        for data in result:
 
97
            row = []
 
98
            csvData = ''
 
99
            for d in data:
 
100
                if type(d).__name__ == 'unicode':
 
101
                    d = d.encode('utf-8')
 
102
                if type(d)==types.StringType:
 
103
                    csvData += (csvData and ',' or '') + '"' + str(d.replace('\n',' ').replace('\t',' ')) + '"'
 
104
                else:
 
105
                    csvData += (csvData and ',' or '') + str(d)
 
106
            buf.write(csvData+'\n')
 
107
        out=base64.encodestring(buf.getvalue())
 
108
        buf.close()
 
109
        res = {}
 
110
        res['file'] = out
 
111
        return res
 
112
 
 
113
    def _check(self, cr, uid, data, context):
 
114
        format = pooler.get_pool(cr.dbname).get('fiscal.statements').browse(cr, uid, data['form']['fiscal_statements_id'], context).format
 
115
        if format == 'balance':
 
116
            return 'report_balance'           
 
117
        else:
 
118
            return 'report_income'
 
119
 
 
120
    states = {
 
121
        'init': {
 
122
            'actions': [_get_period],
 
123
            'result': {'type':'form', 'arch':fiscal_form, 'fields':fiscal_fields,
 
124
            'state':[('end','Cancel','gtk-cancel'),('report_csv','Excel','gtk-convert'),('checkreport','Print','gtk-print')]}
 
125
        },
 
126
        'report_csv': {
 
127
            'actions': [_check_ouput_csv],
 
128
            'result': {'type':'form','arch':export_form,'fields':export_fields,'state':[('end','Ok')]},
 
129
        },
 
130
        'checkreport': {
 
131
            'actions': [],
 
132
            'result': {'type':'choice','next_state':_check}
 
133
        },
 
134
        'report_balance': {
 
135
            'actions': [],
 
136
            'result': {'type':'print', 'report':'fiscal_statements_balance', 'state':'end'}
 
137
        },
 
138
        'report_income': {
 
139
            'actions': [],
 
140
            'result': {'type':'print', 'report':'fiscal_statements_income', 'state':'end'}
 
141
        },
 
142
    }
 
143
wizard_fiscal_statements_balance('fiscal.statements.balance')
 
144
 
 
145
 
 
146
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
 
147