# Beispiel Buchhaltungsexport

Nur Rechnungen. Als Basis werden die Datev Export Daten verwendet und eine Reihe eigens definierter Felder mitgegeben.&#x20;

```python
export = env['datev.export'].search([('type', '=', 'date_range')], order='create_date desc', limit=1)

DATE_FORMAT = '%Y-%m-%d'
filename = 'Buchungsstapel_%s--%s.xls'%(export.date_begin,export.date_end)

if not export:
  raise Warning("Kein Export gefunden!")

lines = []
lines.extend(export.export_account_invoice()[0])

if not lines:
  raise Warning("Keine Daten gefunden!")

glines = export.group_pos_lines(lines)
invoice_numbers = [l["Belegfeld 1"] for l in glines if l.get("Belegfeld 1", False)]
invoices = {inv.number: inv for inv in env['account.invoice'].search([('number', 'in', invoice_numbers)])}
account_codes = [l["Gegenkonto"] for l in glines if l.get("Gegenkonto", False)]
accounts = {account.code: account for account in env['account.account'].search([('code', 'in', account_codes)])}

headers = [
  "Bestelldatum",
  "Belegdatum",  #für Buchhaltung relevant, praktisch immer ident mit Bestelldatum
  "Belegart",  #wichtig für Ust Buchung "Gutschrift" ODER "Rechnung"
  "Rechnungsnummer",
  "Externe Belegnummer",
  "Kontoart",
  "Kontonr",
  "Beschreibung", #immer der Kundenname
  "Buchungsart",
  "Geschäftsbuchungsgruppe",
  "Produktbuchungsgruppe",
  "Steuersatz=MwStProduktbuchungsgruppe",
  "Betrag",
  "Gegenkontoart",  #immer "Debitor"
  "Gegenkontonr.",  #Inland oder EU
  "Kst Code", #immer die gleiche: 28019
  "Fälligkeitsdatum",
  "Zahldatum", 
  "Art", #Zahlart Webshop
  "Land", #Land der Rechnungsadresse
  "Land Lieferadresse", 
  "UID-Nummer" #nur bei Bestellungen von Firmen im EU Land relevant
  ]

#EU Countries, https://europa.eu/european-union/about-eu/countries_de
eu_countries = """Belgien	Malta
Bulgarien	Niederlande
Dänemark	Österreich
Deutschland	Polen
Estland	Portugal
Finnland	Rumänien
Frankreich	Schweden
Griechenland	Slowakei
Irland	Slowenien
Italien	Spanien
Kroatien	Tschechien
Lettland	Ungarn
Litauen	Zypern
Luxemburg""".split()	

rows = []
for gline in glines:
  invoice_number = gline.get("Belegfeld 1", False)
  invoice = invoice_number and invoices.get(invoice_number, '')
  customer_name = invoice and invoice.partner_id.display_name or ''
  customer_vat = invoice and invoice.partner_id.vat or '-'
  customer_is_company = invoice and invoice.partner_id.company_type == "company"
  date_invoice = invoice and invoice.date_invoice
  date_due = invoice and invoice.date_due
  
  referenzauftrag = invoice and invoice.origin or ''
  
  country_invoice = invoice and invoice.partner_id.country_id.name
  country_delivery = invoice and invoice.partner_shipping_id.country_id.name
  
  zahlart = invoice and (invoice._get_payments_vals() or [{ 'journal_name': "" }]).pop()['journal_name']
  zahldatum = invoice and (invoice._get_payments_vals() or [{ 'date': "" }]).pop()['date']
  
  account_code = gline.get("Gegenkonto", False)
  account = account_code and accounts.get(account_code, '')
  account_type = account and account.name
  
  #derive Tax Rate from Name
  tax_rate = ([int(m) for m in account_type.replace("%", " ").split() if m.isdigit()] or [0]).pop()
  land_kategorie = (("DRITTLAND","EU")[country_invoice in eu_countries], 'INLAND')[country_invoice == 'Österreich']
  
  #GRUPPENLOGIK
  #INLAND
  prodgru = "F%i"%tax_rate
  mwstprodgru = tax_rate
  
  if land_kategorie == "DRITTLAND":
    prodgru = "A0"
    mwstprodgru = "A0"

  if land_kategorie == "EU":
    prodgru = "AIL"
    mwstprodgru = "AIL"
  
  if country_invoice == "Deutschland":
    prodgru = "DE%i"%tax_rate
    mwstprodgru = tax_rate
    
  if country_invoice == "Slowenien":
    prodgru = "SI%i"%tax_rate
    mwstprodgru = "%iSI"%tax_rate   
   
  if country_invoice == "Italien":
    prodgru = "IT%i"%tax_rate
    mwstprodgru = tax_rate  
    
  #GegenkontoNr, laut Vorgabe wie folgt:
  #240000	Österreichische Unternehmer und Privatpersonen
  #240000	EU-Unternehmer
  #240000	Drittland-Unternehmer und Privatpersonen
  #20064291	Deutschland-Privatpersonen
  #20064292	Italien-Privatpersonen
  #20064294	Slowenien-Privatpersonen
  
  gegenkonto = "240000"
  if land_kategorie == "EU" and not customer_is_company:
    gegenkonto = ""
  if country_invoice == "Deutschland" and not customer_is_company:
    gegenkonto = "20064291"
  if country_invoice == "Italien" and not customer_is_company:
    gegenkonto = "20064292"
  if country_invoice == "Slowenien" and not customer_is_company:
    gegenkonto = "20064294"
  
  rows.append([
    date_invoice, #gline.get("Belegdatum", ""), #bestelldatum
    date_invoice, #gline.get("Belegdatum", ""),
    gline.get("Buchungstext", ""),
    gline.get("Belegfeld 1", ""),
    referenzauftrag, #ok to have RefAuftrag here
    "Sachkonto", #fixed
    gline.get("Gegenkonto", ""), #VERTAUSCHT LAUT DEMODATEN MIT GEGENKONTO
    customer_name,
    "Verkauf",
    land_kategorie, #siehe Logik oben fuer diesen und die 2 folgenden Spalten
    prodgru,
    mwstprodgru,
    ('', "-")[gline.get("Soll/Haben-Kennzeichen", "") == "S"] + gline.get("Umsatz", ""),
    "Debitor",
    gegenkonto,
    28019,
    date_due, #fälligkeitsdatum
    zahldatum, #zahldatum
    zahlart, #zahlart webshop
    country_invoice, #country of invoice
    country_delivery, #country of delivery
    customer_vat, #customer UID number if applicable
    ])
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://helpcenter.etron.info/onretail-wiki/handbuch/verwaltungsoberflache/berichte/automatische-reports/beispiel-buchhaltungsexport.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
