Debian Codegraph @

addons/l10n_hu_edi/wizard/l10n_hu_edi_tax_audit_export.py

raw symbols SCIP
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2
3 import base64
4 import contextlib
5 import io
6 import zipfile
7
8 from odoo import api, models, fields, _
9 from odoo.exceptions import UserError
10
11
12 class L10n_Hu_EdiTax_Audit_Export(models.TransientModel):
13 _name = 'l10n_hu_edi.tax_audit_export'
14 _description = 'Tax audit export - Adóhatósági Ellenőrzési Adatszolgáltatás'
15
16 selection_mode = fields.Selection(
17 string='Selection mode',
18 selection=[
19 ('date', 'By date'),
20 ('name', 'By serial number'),
21 ],
22 default='date',
23 )
24 date_from = fields.Date(
25 string='Date From'
26 )
27 date_to = fields.Date(
28 string='Date To'
29 )
30 name_from = fields.Char(
31 string='Name From'
32 )
33 name_to = fields.Char(
34 string='Name To'
35 )
36 filename = fields.Char(
37 string='File name',
38 compute='_compute_filename'
39 )
40 export_file = fields.Binary(
41 string='Generated File',
42 readonly=True
43 )
44
45 @api.depends('selection_mode', 'date_from', 'date_to', 'name_from', 'name_to')
46 def _compute_filename(self):
47 domain = [
48 ('move_type', 'in', ('out_invoice', 'out_refund')),
49 ('state', '=', 'posted'),
50 ('country_code', '=', 'HU'),
51 ]
52 if self.selection_mode == 'date':
53 date_from = self.date_from
54 if not date_from:
55 first_invoice = self.env['account.move'].search(domain, order='date', limit=1)
56 date_from = first_invoice.date
57 date_to = self.date_to or fields.Date.today()
58 self.filename = f'export_{date_from}_{date_to}.zip'
59
60 else:
61 name_from = self.name_from
62 if not name_from:
63 first_invoice = self.env['account.move'].search(domain, order='name', limit=1)
64 name_from = first_invoice.name
65 name_to = self.name_to
66 if not name_to:
67 last_invoice = self.env['account.move'].search(domain, order='name desc', limit=1)
68 name_to = last_invoice.name
69 self.filename = f'export_{name_from.replace("/", "")}_{name_to.replace("/", "")}.zip'
70
71 def action_export(self):
72 self.ensure_one()
73 domain = [
74 ('move_type', 'in', ('out_invoice', 'out_refund')),
75 ('state', '=', 'posted'),
76 ('country_code', '=', 'HU'),
77 ]
78 if self.selection_mode == 'date':
79 if self.date_from:
80 domain.append(('date', '>=', self.date_from))
81 if self.date_to:
82 domain.append(('date', '<=', self.date_to))
83 else:
84 if self.name_from:
85 domain.append(('name', '>=', self.name_from))
86 if self.name_to:
87 domain.append(('name', '<=', self.name_to))
88
89 invoices = self.env['account.move'].search(domain)
90 if not invoices:
91 raise UserError(_('No invoice to export!'))
92
93 with io.BytesIO() as buf:
94 with zipfile.ZipFile(buf, mode='w', compression=zipfile.ZIP_DEFLATED, allowZip64=False) as zf:
95 # To correctly generate the XML for invoices created before l10n_hu_edi was installed,
96 # we need to temporarily set the chain index and line numbers, so we do this in a savepoint.
97 with contextlib.closing(self.env.cr.savepoint(flush=False)):
98 for invoice in invoices.sorted(lambda i: i.create_date):
99 if invoice.l10n_hu_edi_state:
100 # Case 1: An XML was already generated for this invoice.
101 invoice_xml = base64.b64decode(invoice.l10n_hu_edi_attachment)
102 else:
103 # Case 2: No XML was generated for this invoice.
104 if not invoice.l10n_hu_invoice_chain_index:
105 invoice._l10n_hu_edi_set_chain_index()
106 invoice_xml = invoice._l10n_hu_edi_generate_xml()
107
108 filename = f'{invoice.name.replace("/", "_")}.xml'
109 zf.writestr(filename, invoice_xml)
110 self.export_file = base64.b64encode(buf.getvalue())
111
112 return {
113 'type': 'ir.actions.act_window',
114 'res_model': self._name,
115 'view_mode': 'form',
116 'res_id': self.id,
117 'views': [(False, 'form')],
118 'target': 'new',
119 }