Jelajahi Sumber

upd Descarga masiva

erickabrego 7 bulan lalu
induk
melakukan
e3b6720978

+ 1 - 0
custom_sat_connection/__manifest__.py

@@ -21,6 +21,7 @@
         'views/account_account.xml',
         'views/account_move.xml',
         'views/account_esignature_certificate.xml',
+        'views/ir_attachment.xml',
         'wizards/account_cfdi_sat.xml',
         'wizards/account_cfdi_zip.xml',
         'wizards/account_cfdi_xml.xml',

+ 1 - 0
custom_sat_connection/models/__init__.py

@@ -11,3 +11,4 @@ from . import res_company
 from . import res_partner
 from . import res_config_settings
 from . import account_move
+from . import ir_attachment

+ 74 - 0
custom_sat_connection/models/ir_attachment.py

@@ -0,0 +1,74 @@
+from odoo import api, fields, models
+from os.path import basename
+from zipfile import ZipFile
+from tempfile import TemporaryDirectory
+import base64
+import os.path
+
+class IrAttachment(models.Model):
+    _inherit = "ir.attachment"
+
+    def download_massive_zip(self):
+        self = self.with_user(1)
+        zip_name = "Descarga masiva.zip"
+        zip_file = self.env['ir.attachment'].sudo().search([('name', '=', zip_name)], limit=1)
+        if zip_file:
+            zip_file.sudo().unlink()
+
+        # Funcion para decodificar el archivo
+        def isBase64_decodestring(s):
+            try:
+                decode_archive = base64.decodebytes(s)
+                return decode_archive
+            except Exception as e:
+                raise ValidationError('Error:', + str(e))
+
+        tempdir_file = TemporaryDirectory()
+        location_tempdir = tempdir_file.name
+        # Creando ruta dinamica para poder guardar el archivo zip
+        date_act = fields.Date.today()
+        file_name = 'DescargaMasiva(Fecha de descarga' + " - " + str(date_act) + ")"
+        file_name_zip = file_name + ".zip"
+        zipfilepath = os.path.join(location_tempdir, file_name_zip)
+        path_files = os.path.join(location_tempdir)
+
+        # Creando zip
+        for file in self:
+            object_name = file.name
+            ruta_ob = object_name
+            object_handle = open(os.path.join(location_tempdir, ruta_ob), "wb")
+            object_handle.write(isBase64_decodestring(file.datas))
+            object_handle.close()
+
+        with ZipFile(zipfilepath, 'w') as zip_obj:
+            for file in os.listdir(path_files):
+                file_path = os.path.join(path_files, file)
+                if file_path != zipfilepath:
+                    zip_obj.write(file_path, basename(file_path))
+
+        with open(zipfilepath, 'rb') as file_data:
+            bytes_content = file_data.read()
+            encoded = base64.b64encode(bytes_content)
+
+        data = {
+            'name': zip_name,
+            'type': 'binary',
+            'datas': encoded,
+            'company_id': self.env.company.id
+        }
+        attachment = self.env['ir.attachment'].sudo().create(data)
+        return self.download_zip(file_name_zip, attachment.id)
+
+    def download_zip(self, filename, id_file):
+        path = "/web/binary/download_document?"
+        model = "ir.attachment"
+
+        url = path + "model={}&id={}&filename={}".format(model, id_file, filename)
+        return {
+            'type': 'ir.actions.act_url',
+            'url': url,
+            'target': 'self',
+        }
+    
+    
+

+ 21 - 0
custom_sat_connection/views/ir_attachment.xml

@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="utf-8"?>
+<odoo>
+    <data>
+
+        <record id="sat_connection_ir_attachment_list" model="ir.ui.view">
+            <field name="name">sat_connection_ir_attachment_list</field>
+            <field name="model">ir.attachment</field>
+            <field name="inherit_id" ref="data_cleaning.view_data_storage_attachment_tree"/>
+            <field name="arch" type="xml">
+
+                <xpath expr="//field[@name='name']" position="before">
+                    <header>
+                        <button name="download_massive_zip" string="Descargar zip" class="btn btn-primary" type="object"/>
+                    </header>
+                </xpath>
+
+            </field>
+        </record>
+
+    </data>
+</odoo>