diff --git a/bakeup/contrib/export.py b/bakeup/contrib/export.py index 6bf7379..72ba787 100644 --- a/bakeup/contrib/export.py +++ b/bakeup/contrib/export.py @@ -26,8 +26,11 @@ class DataExport: XLSX: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", YAML: "text/yaml; charset=utf-8", } + delimiter = "," - def __init__(self, export_format, data, headers, title, dataset_kwargs=None): + def __init__( + self, export_format, data, headers, title, delimiter=",", dataset_kwargs=None + ): if not self.is_valid_format(export_format): raise TypeError( 'Export format "{}" is not supported.'.format(export_format) @@ -35,6 +38,7 @@ def __init__(self, export_format, data, headers, title, dataset_kwargs=None): self.format = export_format self.dataset = self.data_to_dataset(data, headers, title, dataset_kwargs) + self.delimiter = delimiter @classmethod def is_valid_format(self, export_format): @@ -61,7 +65,7 @@ def export(self): """ Returns the string/bytes for the current export format """ - return self.dataset.export(self.format) + return self.dataset.export(self.format, delimiter=self.delimiter) def response(self, filename=None): """ @@ -77,7 +81,7 @@ def response(self, filename=None): filename ) - response.write(self.export().encode("latin-1", "ignore")) + response.write(self.export().encode("iso-8859-1", "ignore")) return response @@ -87,6 +91,7 @@ class ExportMixin: export_class = DataExport export_format = DataExport.CSV + delimiter = "," def get_export_filename(self, export_format): return "{}.{}".format(self.export_name, export_format) @@ -94,16 +99,17 @@ def get_export_filename(self, export_format): def get_dataset_kwargs(self): return self.dataset_kwargs - def create_export(self, export_format): + def create_export(self, export_format, delimiter): exporter = self.export_class( export_format=export_format, data=self.get_data(), headers=self.get_headers(), title=self.export_name, + delimiter=delimiter, dataset_kwargs=self.get_dataset_kwargs(), ) return exporter.response(filename=self.get_export_filename(export_format)) def render_to_response(self, context, **kwargs): - return self.create_export(self.export_format) + return self.create_export(self.export_format, self.delimiter) diff --git a/bakeup/workshop/exports.py b/bakeup/workshop/exports.py index 8e0d6a1..7a4070c 100644 --- a/bakeup/workshop/exports.py +++ b/bakeup/workshop/exports.py @@ -1,4 +1,5 @@ from django.db.models import Q +from django.utils import numberformat from django.utils.timezone import now from django.utils.translation import gettext_lazy as _ from django.views.generic import ListView @@ -59,6 +60,7 @@ def export_name(self): class CustomerBillbeeExportView(StaffPermissionsMixin, ExportMixin, FilterView): filterset_class = CustomerFilter model = Customer + delimiter = ";" def get_headers(self): headers = [ @@ -383,23 +385,33 @@ def get_data(self): rows = [] for customer_order in customer_orders: for customer_order_position in customer_order.positions.all(): + price = "" + if customer_order_position.price: + decimal_value = customer_order_position.price_total.amount + price = numberformat.format(decimal_value, ",", use_l10n=True) + price_total = "" + if customer_order_position.price_total: + decimal_value = customer_order_position.price_total.amount + price_total = numberformat.format(decimal_value, ",", use_l10n=True) rows.append( [ customer_order_position.order.id, customer_order_position.product.pk, - customer_order.created, + customer_order.created.strftime( + "%Y-%m-%d %H:%M:%S" + ), # format yyyy-MM-dd HH:mm:ss "", # Bezahlmethode customer_order_position.product.name, "", # Variante "", # Einzelpreis Netto - customer_order_position.price, # Einzelpreis Brutto + price, # Einzelpreis Brutto "0", # Versandkosten Netto "0", # Versandkosten Brutto "0", # Nachnahmegebühr "7", # MwSt. % customer_order_position.quantity, "", # Preis Netto - customer_order_position.price_total, # Preis Brutto + price_total, # Preis Brutto "EUR", "", # Versand-Datum "", # Rechn. Firma