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

« back to all changes in this revision

Viewing changes to bias_electronic_invoice_v6/wizard/wizard_redo_einvoice.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 *
 
35
import datetime
 
36
from lxml import etree
 
37
import os
 
38
import tools
 
39
 
 
40
 
 
41
invoices_form = '''<?xml version="1.0"?>
 
42
<form string="Select the invoices to ReGenerate"  width="800" height="250" >
 
43
        <field name="invoice_ids" domain = "[('state','in',('open','paid')), ('type', 'in', ('out_invoice','out_refund'))]"  width="800" height="250"  />
 
44
        <field name="keep_date" />
 
45
</form>'''
 
46
 
 
47
 
 
48
invoices_fields = {
 
49
        'invoice_ids': {'string': 'Invoices', 'type': 'many2many', 'relation': 'account.invoice',},
 
50
        'keep_date': {'string': 'Keep same Date?', 'type': 'boolean', }
 
51
}
 
52
 
 
53
done_form = '''<?xml version="1.0"?>
 
54
<form string="Regeneration Done!!!">
 
55
<separator string="Regeneration Done!!!"/>
 
56
</form>'''
 
57
 
 
58
done_fields = {}
 
59
 
 
60
 
 
61
 
 
62
 
 
63
class wizard_redo_einvoice(wizard.interface):
 
64
 
 
65
 
 
66
        def _get_new_xml(self, cr, uid, data, context):
 
67
                pool = pooler.get_pool(cr.dbname)
 
68
                invoice_obj = pool.get('account.invoice')
 
69
                attach_obj = pool.get('ir.attachment')
 
70
                context = {}
 
71
                for inv_id in data['ids']:
 
72
                        if not data['form']['keep_date']:
 
73
                                context = {}
 
74
                        else:
 
75
                                date = invoice_obj.browse(cr, uid, inv_id).sign_date
 
76
                                date = date.split(' ')
 
77
                                same_date = date[0] + 'T' + date[1]
 
78
                                context['date'] = same_date
 
79
                        cfe= invoice_obj.create_xml(cr, uid, [inv_id], context)
 
80
                        cfe_str = etree.tostring(cfe, pretty_print=True, encoding='utf-8')
 
81
                        attachment_ids = attach_obj.search(cr, uid, [('res_id','=',inv_id),('res_model','=', 'account.invoice')])
 
82
                        attach_brw_ids = attach_obj.browse(cr, uid, attachment_ids)
 
83
                        for attach_brw in attach_brw_ids:
 
84
                                attach_name = attach_brw.name
 
85
                                attach_name = attach_name + '.old'
 
86
                                query = "UPDATE ir_attachment set name='%s' where id=%s"%(attach_name, attach_brw.id)
 
87
                                cr.execute("UPDATE ir_attachment set name='%s' where id=%s"%(attach_name, attach_brw.id))
 
88
                        invoice_obj.attach_xml(cr, uid, inv_id, cfe_str)
 
89
                return {}
 
90
 
 
91
        def _get_defaults(self, cr, uid, data, context):
 
92
                data['form'] = {'invoice_ids': data['ids']}
 
93
                return data['form']
 
94
 
 
95
        def _get_all_md5(self, cr, uid, data, context):
 
96
                pool = pooler.get_pool(cr.dbname)
 
97
                cr.execute("SELECT id from account_invoice where sign_date between '2010-01-01 00:00:00' and '2011-03-01 00:00:00'")
 
98
                all_ids = cr.fetchall()
 
99
                all_ids = [a[0] for a in all_ids]
 
100
                ids = []
 
101
                for inv_brw in pool.get('account.invoice').browse(cr, uid, all_ids):
 
102
                        cadena = inv_brw.cadena
 
103
                        sello = inv_brw.digital_signature
 
104
                        certfname = os.path.join(tools.config['root_path'], 'filestore/%s/%s'%(cr.dbname, inv_brw.company_id.certificate.store_fname))
 
105
                        verify = pool.get('account.invoice').cfdutil_verifySello(cadena, certfname, sello)
 
106
                        if verify:
 
107
                                ids.append(inv_brw.id)
 
108
                data['form'] = {'invoice_ids': ids}
 
109
                return data['form']
 
110
 
 
111
 
 
112
        states = {
 
113
                'init': {
 
114
                        'actions': [_get_defaults],
 
115
                        'result': {'type':'form', 
 
116
                                   'arch':invoices_form, 
 
117
                                   'fields':invoices_fields, 
 
118
                                   'state':[('end','Cancel'),('md5','MD5 to SHA1'),('export','Export')]}
 
119
                        },
 
120
                'export' : {
 
121
                        'actions' : [_get_new_xml],
 
122
                        'result' : {'type' : 'form',
 
123
                                    'arch' : done_form,
 
124
                                    'fields' : done_fields,
 
125
                                    'state' : [('close', 'Ok','gtk-ok') ]}
 
126
                        },
 
127
                'md5': {
 
128
                        'actions': [_get_all_md5],
 
129
                        'result': {'type':'form', 
 
130
                                   'arch':invoices_form, 
 
131
                                   'fields':invoices_fields, 
 
132
                                   'state':[('end','Cancel'),('md5','MD5 to SHA1'),('export','Export')]}
 
133
                        },
 
134
                'close': {
 
135
                        'actions': [],
 
136
                        'result': {'type': 'state', 'state':'end'}
 
137
                        }
 
138
                
 
139
                }
 
140
wizard_redo_einvoice('account.invoice.redo.einvoice')
 
141