Skip to content

Commit

Permalink
Adjust billbee export to work properly (localize price and date field)
Browse files Browse the repository at this point in the history
  • Loading branch information
bruecksen committed Nov 8, 2024
1 parent b8bf2d3 commit d63c34c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
16 changes: 11 additions & 5 deletions bakeup/contrib/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,19 @@ 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)
)

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):
Expand All @@ -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):
"""
Expand All @@ -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


Expand All @@ -87,23 +91,25 @@ class ExportMixin:
export_class = DataExport

export_format = DataExport.CSV
delimiter = ","

def get_export_filename(self, export_format):
return "{}.{}".format(self.export_name, 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)
18 changes: 15 additions & 3 deletions bakeup/workshop/exports.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -59,6 +60,7 @@ def export_name(self):
class CustomerBillbeeExportView(StaffPermissionsMixin, ExportMixin, FilterView):
filterset_class = CustomerFilter
model = Customer
delimiter = ";"

def get_headers(self):
headers = [
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d63c34c

Please sign in to comment.