from odoo import models, fields import xml.etree.ElementTree as ET from xml.dom import minidom import base64 class CoretaxFaktur(models.Model): _name = 'coretax.faktur' _description = 'Export Faktur ke XML' export_file = fields.Binary(string="Export File", ) export_filename = fields.Char(string="Export File", ) def generate_xml(self): # Buat root XML root = ET.Element('TaxInvoiceBulk', { 'xmlns:xsi': "http://www.w3.org/2001/XMLSchema-instance", 'xsi:noNamespaceSchemaLocation': "TaxInvoice.xsd" }) ET.SubElement(root, 'TIN').text = 'xxxxxxxxxxxxxxxx' # Tambahkan elemen ListOfTaxInvoice list_of_tax_invoice = ET.SubElement(root, 'ListOfTaxInvoice') # Dapatkan data faktur inv_obj = self.env['account.move'] invoices = inv_obj.search([('is_efaktur_exported','!=',True), ('state','=','posted'), ('efaktur_id','!=', False), ('move_type','=','out_invoice')]) for invoice in invoices: tax_invoice = ET.SubElement(list_of_tax_invoice, 'TaxInvoice') # Tambahkan elemen faktur ET.SubElement(tax_invoice, 'TaxInvoiceDate').text = invoice.invoice_date.strftime('%Y-%m-%d') if invoice.invoice_date else '' ET.SubElement(tax_invoice, 'TaxInvoiceOpt').text = 'Normal' ET.SubElement(tax_invoice, 'TrxCode').text = '01' ET.SubElement(tax_invoice, 'AddInfo') ET.SubElement(tax_invoice, 'CustomDoc') ET.SubElement(tax_invoice, 'RefDesc') ET.SubElement(tax_invoice, 'FacilityStamp') ET.SubElement(tax_invoice, 'SellerIDTKU').text = '0000000000000000000000' ET.SubElement(tax_invoice, 'BuyerTin').text = 'xxxxxxxxxxxxxxxx' ET.SubElement(tax_invoice, 'BuyerDocument').text = 'TIN' ET.SubElement(tax_invoice, 'BuyerCountry').text = 'IND' ET.SubElement(tax_invoice, 'BuyerEmail').text = invoice.partner_id.email or '' ET.SubElement(tax_invoice, 'BuyerIDTKU').text = '0000000000000000000000' # Tambahkan elemen ListOfGoodService list_of_good_service = ET.SubElement(tax_invoice, 'ListOfGoodService') for line in invoice.invoice_line_ids: good_service = ET.SubElement(list_of_good_service, 'GoodService') ET.SubElement(good_service, 'Opt').text = 'A' ET.SubElement(good_service, 'Code').text = '000000' ET.SubElement(good_service, 'Name').text = line.name ET.SubElement(good_service, 'Unit').text = 'UM.0001' ET.SubElement(good_service, 'Price').text = str(line.price_unit) ET.SubElement(good_service, 'Qty').text = str(line.quantity) ET.SubElement(good_service, 'TotalDiscount').text = '100000' ET.SubElement(good_service, 'TaxBase').text = str(line.price_subtotal) ET.SubElement(good_service, 'OtherTaxBase').text = str(line.price_subtotal) ET.SubElement(good_service, 'VATRate').text = '11' ET.SubElement(good_service, 'VAT').text = str(line.price_total * 0.11) ET.SubElement(good_service, 'STLGRate').text = '20' ET.SubElement(good_service, 'STLG').text = '580000' # Pretty print XML xml_str = ET.tostring(root, encoding='utf-8') xml_pretty = minidom.parseString(xml_str).toprettyxml(indent=" ") return xml_pretty def export_to_download(self): # Generate XML content xml_content = self.generate_xml() # Encode content to Base64 xml_encoded = base64.b64encode(xml_content.encode('utf-8')) # Buat attachment untuk XML attachment = self.env['ir.attachment'].create({ 'name': 'Faktur_XML.xml', 'type': 'binary', 'datas': xml_encoded, 'mimetype': 'application/xml', 'store_fname': 'faktur_xml.xml', }) # Kembalikan URL untuk download dengan header 'Content-Disposition' response = { 'type': 'ir.actions.act_url', 'url': '/web/content/{}?download=true'.format(attachment.id), 'target': 'self', } return response