2020-07-29 15:14:23 +00:00
|
|
|
import datetime
|
|
|
|
import zipfile
|
2020-09-11 16:57:58 +00:00
|
|
|
from csv import QUOTE_NONNUMERIC
|
2022-01-27 14:38:05 +00:00
|
|
|
from io import BytesIO
|
2020-09-11 16:57:58 +00:00
|
|
|
|
2020-07-29 15:14:23 +00:00
|
|
|
import frappe
|
|
|
|
import pandas as pd
|
|
|
|
from frappe import _
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2020-07-29 15:14:23 +00:00
|
|
|
from .datev_constants import DataCategory
|
|
|
|
|
|
|
|
|
|
|
|
def get_datev_csv(data, filters, csv_class):
|
2020-09-11 16:57:58 +00:00
|
|
|
"""
|
|
|
|
Fill in missing columns and return a CSV in DATEV Format.
|
2020-07-29 15:14:23 +00:00
|
|
|
|
|
|
|
For automatic processing, DATEV requires the first line of the CSV file to
|
|
|
|
hold meta data such as the length of account numbers oder the category of
|
|
|
|
the data.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
data -- array of dictionaries
|
|
|
|
filters -- dict
|
|
|
|
csv_class -- defines DATA_CATEGORY, FORMAT_NAME and COLUMNS
|
|
|
|
"""
|
|
|
|
empty_df = pd.DataFrame(columns=csv_class.COLUMNS)
|
|
|
|
data_df = pd.DataFrame.from_records(data)
|
|
|
|
result = empty_df.append(data_df, sort=True)
|
|
|
|
|
|
|
|
if csv_class.DATA_CATEGORY == DataCategory.TRANSACTIONS:
|
2022-03-28 13:22:46 +00:00
|
|
|
result["Belegdatum"] = pd.to_datetime(result["Belegdatum"])
|
2020-07-29 15:14:23 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
result["Beleginfo - Inhalt 6"] = pd.to_datetime(result["Beleginfo - Inhalt 6"])
|
|
|
|
result["Beleginfo - Inhalt 6"] = result["Beleginfo - Inhalt 6"].dt.strftime("%d%m%Y")
|
2021-09-01 13:57:54 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
result["Fälligkeit"] = pd.to_datetime(result["Fälligkeit"])
|
|
|
|
result["Fälligkeit"] = result["Fälligkeit"].dt.strftime("%d%m%y")
|
2021-09-01 13:57:54 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
result.sort_values(by="Belegdatum", inplace=True, kind="stable", ignore_index=True)
|
2021-09-01 13:57:54 +00:00
|
|
|
|
2020-07-29 15:14:23 +00:00
|
|
|
if csv_class.DATA_CATEGORY == DataCategory.ACCOUNT_NAMES:
|
2022-03-28 13:22:46 +00:00
|
|
|
result["Sprach-ID"] = "de-DE"
|
2020-07-29 15:14:23 +00:00
|
|
|
|
|
|
|
data = result.to_csv(
|
|
|
|
# Reason for str(';'): https://github.com/pandas-dev/pandas/issues/6035
|
2022-03-28 13:22:46 +00:00
|
|
|
sep=";",
|
2020-07-29 15:14:23 +00:00
|
|
|
# European decimal seperator
|
2022-03-28 13:22:46 +00:00
|
|
|
decimal=",",
|
2020-07-29 15:14:23 +00:00
|
|
|
# Windows "ANSI" encoding
|
2022-03-28 13:22:46 +00:00
|
|
|
encoding="latin_1",
|
2020-07-29 15:14:23 +00:00
|
|
|
# format date as DDMM
|
2022-03-28 13:22:46 +00:00
|
|
|
date_format="%d%m",
|
2020-07-29 15:14:23 +00:00
|
|
|
# Windows line terminator
|
2022-03-28 13:22:46 +00:00
|
|
|
line_terminator="\r\n",
|
2020-07-29 15:14:23 +00:00
|
|
|
# Do not number rows
|
|
|
|
index=False,
|
|
|
|
# Use all columns defined above
|
|
|
|
columns=csv_class.COLUMNS,
|
|
|
|
# Quote most fields, even currency values with "," separator
|
2022-03-28 13:22:46 +00:00
|
|
|
quoting=QUOTE_NONNUMERIC,
|
2020-07-29 15:14:23 +00:00
|
|
|
)
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
data = data.encode("latin_1", errors="replace")
|
2020-07-29 15:14:23 +00:00
|
|
|
|
|
|
|
header = get_header(filters, csv_class)
|
2022-03-28 13:22:46 +00:00
|
|
|
header = ";".join(header).encode("latin_1", errors="replace")
|
2020-07-29 15:14:23 +00:00
|
|
|
|
|
|
|
# 1st Row: Header with meta data
|
|
|
|
# 2nd Row: Data heading (Überschrift der Nutzdaten), included in `data` here.
|
|
|
|
# 3rd - nth Row: Data (Nutzdaten)
|
2022-03-28 13:22:46 +00:00
|
|
|
return header + b"\r\n" + data
|
2020-07-29 15:14:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_header(filters, csv_class):
|
2022-03-28 13:22:46 +00:00
|
|
|
description = filters.get("voucher_type", csv_class.FORMAT_NAME)
|
|
|
|
company = filters.get("company")
|
|
|
|
datev_settings = frappe.get_doc("DATEV Settings", {"client": company})
|
|
|
|
default_currency = frappe.get_value("Company", company, "default_currency")
|
|
|
|
coa = frappe.get_value("Company", company, "chart_of_accounts")
|
|
|
|
coa_short_code = "04" if "SKR04" in coa else ("03" if "SKR03" in coa else "")
|
2020-07-29 15:14:23 +00:00
|
|
|
|
|
|
|
header = [
|
|
|
|
# DATEV format
|
2022-03-28 13:22:46 +00:00
|
|
|
# "DTVF" = created by DATEV software,
|
|
|
|
# "EXTF" = created by other software
|
2020-07-29 15:14:23 +00:00
|
|
|
'"EXTF"',
|
|
|
|
# version of the DATEV format
|
2022-03-28 13:22:46 +00:00
|
|
|
# 141 = 1.41,
|
|
|
|
# 510 = 5.10,
|
|
|
|
# 720 = 7.20
|
|
|
|
"700",
|
2020-07-29 15:14:23 +00:00
|
|
|
csv_class.DATA_CATEGORY,
|
|
|
|
'"%s"' % csv_class.FORMAT_NAME,
|
|
|
|
# Format version (regarding format name)
|
|
|
|
csv_class.FORMAT_VERSION,
|
|
|
|
# Generated on
|
2022-03-28 13:22:46 +00:00
|
|
|
datetime.datetime.now().strftime("%Y%m%d%H%M%S") + "000",
|
2020-07-29 15:14:23 +00:00
|
|
|
# Imported on -- stays empty
|
2022-03-28 13:22:46 +00:00
|
|
|
"",
|
2020-07-29 15:14:23 +00:00
|
|
|
# Origin. Any two symbols, will be replaced by "SV" on import.
|
|
|
|
'"EN"',
|
|
|
|
# I = Exported by
|
|
|
|
'"%s"' % frappe.session.user,
|
|
|
|
# J = Imported by -- stays empty
|
2022-03-28 13:22:46 +00:00
|
|
|
"",
|
2020-07-29 15:14:23 +00:00
|
|
|
# K = Tax consultant number (Beraternummer)
|
2022-03-28 13:22:46 +00:00
|
|
|
datev_settings.get("consultant_number", "0000000"),
|
2020-07-29 15:14:23 +00:00
|
|
|
# L = Tax client number (Mandantennummer)
|
2022-03-28 13:22:46 +00:00
|
|
|
datev_settings.get("client_number", "00000"),
|
2020-07-29 15:14:23 +00:00
|
|
|
# M = Start of the fiscal year (Wirtschaftsjahresbeginn)
|
2022-03-28 13:22:46 +00:00
|
|
|
frappe.utils.formatdate(filters.get("fiscal_year_start"), "yyyyMMdd"),
|
2020-07-29 15:14:23 +00:00
|
|
|
# N = Length of account numbers (Sachkontenlänge)
|
2022-03-28 13:22:46 +00:00
|
|
|
str(filters.get("account_number_length", 4)),
|
2020-07-29 15:14:23 +00:00
|
|
|
# O = Transaction batch start date (YYYYMMDD)
|
2022-03-28 13:22:46 +00:00
|
|
|
frappe.utils.formatdate(filters.get("from_date"), "yyyyMMdd")
|
|
|
|
if csv_class.DATA_CATEGORY == DataCategory.TRANSACTIONS
|
|
|
|
else "",
|
2020-07-29 15:14:23 +00:00
|
|
|
# P = Transaction batch end date (YYYYMMDD)
|
2022-03-28 13:22:46 +00:00
|
|
|
frappe.utils.formatdate(filters.get("to_date"), "yyyyMMdd")
|
|
|
|
if csv_class.DATA_CATEGORY == DataCategory.TRANSACTIONS
|
|
|
|
else "",
|
2020-07-29 15:14:23 +00:00
|
|
|
# Q = Description (for example, "Sales Invoice") Max. 30 chars
|
2022-03-28 13:22:46 +00:00
|
|
|
'"{}"'.format(_(description)) if csv_class.DATA_CATEGORY == DataCategory.TRANSACTIONS else "",
|
2020-07-29 15:14:23 +00:00
|
|
|
# R = Diktatkürzel
|
2022-03-28 13:22:46 +00:00
|
|
|
"",
|
2020-07-29 15:14:23 +00:00
|
|
|
# S = Buchungstyp
|
2022-03-28 13:22:46 +00:00
|
|
|
# 1 = Transaction batch (Finanzbuchführung),
|
|
|
|
# 2 = Annual financial statement (Jahresabschluss)
|
|
|
|
"1" if csv_class.DATA_CATEGORY == DataCategory.TRANSACTIONS else "",
|
2020-07-29 15:14:23 +00:00
|
|
|
# T = Rechnungslegungszweck
|
2022-03-28 13:22:46 +00:00
|
|
|
# 0 oder leer = vom Rechnungslegungszweck unabhängig
|
|
|
|
# 50 = Handelsrecht
|
|
|
|
# 30 = Steuerrecht
|
|
|
|
# 64 = IFRS
|
|
|
|
# 40 = Kalkulatorik
|
|
|
|
# 11 = Reserviert
|
|
|
|
# 12 = Reserviert
|
|
|
|
"0" if csv_class.DATA_CATEGORY == DataCategory.TRANSACTIONS else "",
|
2020-07-29 15:14:23 +00:00
|
|
|
# U = Festschreibung
|
|
|
|
# TODO: Filter by Accounting Period. In export for closed Accounting Period, this will be "1"
|
2022-03-28 13:22:46 +00:00
|
|
|
"0",
|
2020-07-29 15:14:23 +00:00
|
|
|
# V = Default currency, for example, "EUR"
|
2022-03-28 13:22:46 +00:00
|
|
|
'"%s"' % default_currency if csv_class.DATA_CATEGORY == DataCategory.TRANSACTIONS else "",
|
2020-07-29 15:14:23 +00:00
|
|
|
# reserviert
|
2022-03-28 13:22:46 +00:00
|
|
|
"",
|
2020-07-29 15:14:23 +00:00
|
|
|
# Derivatskennzeichen
|
2022-03-28 13:22:46 +00:00
|
|
|
"",
|
2020-07-29 15:14:23 +00:00
|
|
|
# reserviert
|
2022-03-28 13:22:46 +00:00
|
|
|
"",
|
2020-07-29 15:14:23 +00:00
|
|
|
# reserviert
|
2022-03-28 13:22:46 +00:00
|
|
|
"",
|
2020-07-29 15:14:23 +00:00
|
|
|
# SKR
|
|
|
|
'"%s"' % coa_short_code,
|
|
|
|
# Branchen-Lösungs-ID
|
2022-03-28 13:22:46 +00:00
|
|
|
"",
|
2020-07-29 15:14:23 +00:00
|
|
|
# reserviert
|
2022-03-28 13:22:46 +00:00
|
|
|
"",
|
2020-07-29 15:14:23 +00:00
|
|
|
# reserviert
|
2022-03-28 13:22:46 +00:00
|
|
|
"",
|
2020-07-29 15:14:23 +00:00
|
|
|
# Anwendungsinformation (Verarbeitungskennzeichen der abgebenden Anwendung)
|
2022-03-28 13:22:46 +00:00
|
|
|
"",
|
2020-07-29 15:14:23 +00:00
|
|
|
]
|
|
|
|
return header
|
|
|
|
|
|
|
|
|
2020-11-09 14:47:56 +00:00
|
|
|
def zip_and_download(zip_filename, csv_files):
|
2020-09-11 16:57:58 +00:00
|
|
|
"""
|
|
|
|
Put CSV files in a zip archive and send that to the client.
|
2020-07-29 15:14:23 +00:00
|
|
|
|
|
|
|
Params:
|
2020-11-09 14:47:56 +00:00
|
|
|
zip_filename Name of the zip file
|
|
|
|
csv_files list of dicts [{'file_name': 'my_file.csv', 'csv_data': 'comma,separated,values'}]
|
2020-07-29 15:14:23 +00:00
|
|
|
"""
|
|
|
|
zip_buffer = BytesIO()
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
zip_file = zipfile.ZipFile(zip_buffer, mode="w", compression=zipfile.ZIP_DEFLATED)
|
2020-11-09 14:47:56 +00:00
|
|
|
for csv_file in csv_files:
|
2022-03-28 13:22:46 +00:00
|
|
|
zip_file.writestr(csv_file.get("file_name"), csv_file.get("csv_data"))
|
2020-11-09 14:47:56 +00:00
|
|
|
|
|
|
|
zip_file.close()
|
2020-07-29 15:14:23 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
frappe.response["filecontent"] = zip_buffer.getvalue()
|
|
|
|
frappe.response["filename"] = zip_filename
|
|
|
|
frappe.response["type"] = "binary"
|