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

« back to all changes in this revision

Viewing changes to bias_electronic_invoice_mys/wizard/wizard_electronic_invoice_report.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
##############################################################################
 
2
#
 
3
# Copyright (c) 2005-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
 
4
#
 
5
# WARNING: This program as such is intended to be used by professional
 
6
# programmers who take the whole responsability of assessing all potential
 
7
# consequences resulting from its eventual inadequacies and bugs
 
8
# End users who are looking for a ready-to-use solution with commercial
 
9
# garantees and support are strongly adviced to contract a Free Software
 
10
# Service Company
 
11
#
 
12
# This program is Free Software; you can redistribute it and/or
 
13
# modify it under the terms of the GNU General Public License
 
14
# as published by the Free Software Foundation; either version 2
 
15
# of the License, or (at your option) any later version.
 
16
#
 
17
# This program is distributed in the hope that it will be useful,
 
18
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
19
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
20
# GNU General Public License for more details.
 
21
#
 
22
# You should have received a copy of the GNU General Public License
 
23
# along with this program; if not, write to the Free Software
 
24
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
25
#
 
26
##############################################################################
 
27
 
 
28
import pooler
 
29
import wizard
 
30
import base64
 
31
from osv import osv
 
32
import time
 
33
import mx.DateTime
 
34
from mx.DateTime import RelativeDateTime, now, DateTime, localtime
 
35
from lxml.builder import ElementMaker
 
36
from lxml import etree
 
37
import SOAPpy
 
38
import re
 
39
 
 
40
import StringIO
 
41
import csv
 
42
 
 
43
 
 
44
dates_form = '''<?xml version="1.0"?>
 
45
<form string="Select period">
 
46
        <field name="company_id"/>
 
47
        <field name="fiscalyear" colspan="4"/>
 
48
        <field name="period" colspan="4" domain="[('fiscalyear_id','=',fiscalyear)]"/>
 
49
</form>'''
 
50
 
 
51
dates_fields = {
 
52
        'company_id': {'string': 'Company', 'type': 'many2one',
 
53
                'relation': 'res.company', 'required': True},
 
54
        'fiscalyear': {'string': 'Fiscal year', 'type': 'many2one', 'relation': 'account.fiscalyear',
 
55
                'help': 'Keep empty for all open fiscal year'},
 
56
        'period': {'string': 'Period', 'type': 'many2one', 'relation': 'account.period',
 
57
                'help': ''},
 
58
}
 
59
 
 
60
export_form = """<?xml version="1.0"?>
 
61
        <form string="Payment Export">
 
62
        <field name="report"/>
 
63
        <field name="note" colspan="4" height="300" width="800" nolabel="1"/>
 
64
        </form>"""
 
65
 
 
66
export_fields = {
 
67
        'report' : {
 
68
        'string':'Export File',
 
69
        'type':'binary',
 
70
        'required': False,
 
71
        'readonly':True,
 
72
        },
 
73
        'note' : {'string':'Log','type':'text'},
 
74
}
 
75
 
 
76
 
 
77
def _get_report(self,cr,uid,data,context):
 
78
        pool = pooler.get_pool(cr.dbname)
 
79
        fy_id = data['form']['fiscalyear']
 
80
        pe_id = data['form']['period']
 
81
        company_id = data['form']['company_id']
 
82
        ano = int(pool.get('account.fiscalyear').browse(cr, uid, fy_id, context=context).date_start[:4])
 
83
        mes = int(pool.get('account.period').browse(cr, uid, pe_id, context=context).date_start[5:7])
 
84
        rfc = pool.get('res.company').browse(cr, uid, company_id, context=context).partner_id.vat
 
85
        rfc = re.sub('-','',rfc)
 
86
        res = file = self._reporte(rfc,mes,ano)
 
87
        buf=StringIO.StringIO()
 
88
        writer=buf.write(res)
 
89
        out=base64.encodestring(buf.getvalue())
 
90
        buf.close()
 
91
        
 
92
        return {'note': res, 'report': out}
 
93
        
 
94
        
 
95
        
 
96
class wizard_electronic_report_invoice(wizard.interface):
 
97
        def _reporte(self, rfc, mes, ano):
 
98
                E = ElementMaker(namespace="http://www.buzonfiscal.com/ns/xsd/bf/bfcorp/2", 
 
99
                                 nsmap={
 
100
                                         'soapenv':"http://schemas.xmlsoap.org/soap/envelope/", 
 
101
                                         'ns':"http://www.buzonfiscal.com/ns/xsd/bf/bfcorp/2"},)
 
102
                
 
103
                DOC = E.RequestReporteMensual
 
104
                reporte = DOC({'rfcEmisor':rfc, 'mes':str(mes), 'anio':str(ano)})
 
105
                reporte_xml = etree.tostring(reporte)
 
106
                reporte_xml = '<?xml version="1.0" encoding="UTF-8"?>' + reporte_xml
 
107
                util = SOAPpy.WSDL.Proxy("CorporativoWS2.2.wsdl")
 
108
                reporte = util.GeneraReporteMensual(reporte_xml)
 
109
                return reporte
 
110
 
 
111
        def _get_defaults(self, cr, uid, data, context):
 
112
                pool = pooler.get_pool(cr.dbname)
 
113
                fiscalyear_obj = pool.get('account.fiscalyear')
 
114
                data['form']['fiscalyear'] = fiscalyear_obj.find(cr, uid)
 
115
                user = pool.get('res.users').browse(cr, uid, uid, context=context)
 
116
                if user.company_id:
 
117
                        company_id = user.company_id.id
 
118
                else:
 
119
                        company_id = pool.get('res.company').search(cr, uid,
 
120
                                        [('parent_id', '=', False)])[0]
 
121
                data['form']['company_id'] = company_id
 
122
                return data['form']
 
123
 
 
124
        states = {
 
125
        'init': {
 
126
                'actions': [_get_defaults],
 
127
                'result': {'type':'form', 
 
128
                        'arch':dates_form, 
 
129
                        'fields':dates_fields, 
 
130
                        'state':[('end','Cancel'),('export','Export')]}
 
131
                },
 
132
        'export' : {
 
133
                'actions' : [_get_report],
 
134
                'result' : {'type' : 'form',
 
135
                        'arch' : export_form,
 
136
                        'fields' : export_fields,
 
137
                        'state' : [('close', 'Ok','gtk-ok') ]}
 
138
        },
 
139
        'close': {
 
140
                'actions': [],
 
141
                'result': {'type': 'state', 'state':'end'}
 
142
        }
 
143
 
 
144
    }
 
145
wizard_electronic_report_invoice('account.invoice.electronic.report')
 
146