Skip to content

Instantly share code, notes, and snippets.

@gigidn
Last active February 6, 2019 19:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gigidn/c5b5ab6bba2a00978e63dc4298611f39 to your computer and use it in GitHub Desktop.
Save gigidn/c5b5ab6bba2a00978e63dc4298611f39 to your computer and use it in GitHub Desktop.
esportazione fatture
# -*- coding: utf-8 -*-
# Part of Teuron Odoo Addons. See LICENSE file for full copyright and licensing details.
import base64
import io
import zipfile
import datetime
from odoo import api, fields, models, tools, _
class ExportInvoices(models.TransientModel):
_name = "fe.export_invoice"
_description = 'Invoices Export'
name = fields.Char('File Name', readonly=True)
data = fields.Binary('File', readonly=True)
state = fields.Selection([('choose', 'choose'), ('get', 'get')], # choose language or get the file
default='choose')
@api.multi
def act_getfile(self):
fe_messages_model = self.env['fe.message']
message_ids = self.env.context.get('active_ids', False)
fe_messages = fe_messages_model.browse(message_ids)
men_zip = io.BytesIO()
zf = zipfile.ZipFile(men_zip, "a", zipfile.ZIP_DEFLATED, False)
for message in fe_messages:
print(message)
zf.writestr(
message.datas_fname,
base64.b64decode(message.datas)
)
zf.close()
men_zip.seek(0)
out = base64.b64encode(men_zip.read())
this = self[0]
name = 'inv_export_{}.zip'.format(int(datetime.datetime.now().timestamp()))
this.write({'state': 'get', 'data': out, 'name': name})
return {
'type': 'ir.actions.act_window',
'res_model': 'fe.export_invoice',
'view_mode': 'form',
'view_type': 'form',
'res_id': this.id,
'views': [(False, 'form')],
'target': 'new',
}
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data>
<record id="wizard_fe_export_export" model="ir.ui.view">
<field name="name">Export Invoices</field>
<field name="model">fe.export_invoice</field>
<field name="arch" type="xml">
<form string="Export Invoices">
<field invisible="1" name="state"/>
<field name="name" invisible="1"/>
<div states="get">
<h2>Export Complete</h2>
<p>Here is the exported file: <field name="data" readonly="1" filename="name"/></p>
</div>
<footer states="choose">
<button name="act_getfile" string="Export" type="object" class="btn-primary"/>
<button special="cancel" string="Cancel" type="object" class="btn-secondary"/>
</footer>
<footer states="get">
<button special="cancel" string="Close" type="object" class="btn-primary"/>
</footer>
</form>
</field>
</record>
<act_window name="Export Electronic Inv"
res_model="fe.export_invoice"
src_model="fe.message"
view_mode="form"
target="new"
key2="client_action_multi"
id="action_wizard_fe_export_export"
view_id="wizard_fe_export_export"/>
</data>
</odoo>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment