Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Rest of product slicing logic

Tomasz Szymanski4c4309ca6d32
diff --git a/saleor/order/utils.py b/saleor/order/utils.py
index b49a6abbd5..724d6d950a 100644
--- a/saleor/order/utils.py
+++ b/saleor/order/utils.py
@@ -342,23 +342,31 @@ def match_orders_with_new_user(user: User) -> None:
Order.objects.confirmed().filter(user_email=user.email, user=None).update(user=user)
+def _chunk_products(products, product_limit):
+ chunks = []
+ for i in range(0, len(products), product_limit):
+ limit = i + product_limit
+ chunks.append(products[i:limit])
+ return chunks
+
+
+def _get_page_count(products_first_page, rest_of_products):
+ if not rest_of_products and len(products_first_page) == 4:
+ return 2
+ try:
+ append_last_page = 1 if len(rest_of_products[-1]) > 11 else 0
+ return 1 + len(rest_of_products) + append_last_page
+ except IndexError:
+ return 1
+
+
def generate_invoice_pdf_for_order(invoice):
logo_path = static_finders.find("images/logo-light.svg")
all_products = invoice.order.lines.all()
_product_limit = 3 if len(all_products) < 4 else 4
products_first_page = all_products[:_product_limit]
- rest_of_products = []
- for i in range(0, len(all_products[_product_limit:]), 13):
- limit = i + 13
- rest_of_products.append(all_products[_product_limit:][i:limit])
-
- try:
- page_count = (
- 1 + len(rest_of_products) + (1 if len(rest_of_products[-1]) > 11 else 0)
- )
- except IndexError:
- page_count = 1
+ rest_of_products = _chunk_products(all_products[_product_limit:], 13)
rendered_template = get_template("invoice.html").render(
{
@@ -367,7 +375,7 @@ def generate_invoice_pdf_for_order(invoice):
"logo_path": f"file://{logo_path}",
"products_first_page": products_first_page,
"rest_of_products": rest_of_products,
- "page_count": page_count,
+ "page_count": _get_page_count(products_first_page, rest_of_products),
}
)
content_file = ContentFile(HTML(string=rendered_template).write_pdf())
diff --git a/templates/invoice.html b/templates/invoice.html
index c18b580be6..00d35df981 100644
--- a/templates/invoice.html
+++ b/templates/invoice.html
@@ -66,7 +66,11 @@
}
.content-tight-padded {
- padding: 0 20px 10px 20px;
+ padding: 0 20px 0 20px;
+ }
+
+ .padded-top {
+ padding-top: 15px;
}
.normal-text {
@@ -200,7 +204,7 @@
</div>
</div>
<div class="section-left">
- <div class="content-padded">
+ <div class="content-tight-padded padded-top">
<span class="header-title">
{% trans "BILLING ADDRESS" context "Invoice PDF" %}
</span>
@@ -229,7 +233,7 @@
</div>
</div>
<div class="section-right">
- <div class="content-padded">
+ <div class="content-tight-padded padded-top">
<span class="header-title">
{% trans "SHIPMENT ADDRESS" context "Invoice PDF" %}
</span>
@@ -261,7 +265,7 @@
<div class="content-separator">&nbsp;</div>
</div>
<div class="section-left">
- <div class="content-padded">
+ <div class="content-tight-padded padded-top">
<span class="header-title">{% trans "PAYMENT METHOD" context "Invoice PDF" %}</span>
<br />
<span class="normal-text">{{ order.get_last_payment.gateway }}</span>
@@ -269,7 +273,7 @@
</div>
</div>
<div class="section-right">
- <div class="content-padded">
+ <div class="content-tight-padded padded-top">
<span class="header-title">{% trans "SHIPMENT METHOD" context "Invoice PDF" %}</span>
<br />
<span class="normal-text">{{ order.shipping_method_name }}</span>
@@ -281,7 +285,6 @@
</div>
<div class="content-padded">
<span class="header-title">{% trans "ITEMS ORDERED" context "Invoice PDF table header" %}</span>
- <br />
{% include "invoice_products_table.html" with products=products_first_page %}
<div class="page-footer">
<span class="page-counter header-category">