Skip to content

Commit

Permalink
fix(template): fix template rendering and optimize queries
Browse files Browse the repository at this point in the history
  • Loading branch information
sepehr-akbarzadeh committed Oct 17, 2024
1 parent be6fe1c commit 2190343
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 82 deletions.
Binary file added invoices/logos/sageteam-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion sage_invoice/admin/invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ItemInline(admin.TabularInline):
readonly_fields = ("total_price",)


class ColumnInline(admin.TabularInline):
class ColumnInline(admin.StackedInline):
model = Column
extra = 1

Expand Down
24 changes: 24 additions & 0 deletions sage_invoice/migrations/0003_alter_item_quantity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 5.1.2 on 2024-10-17 11:46

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("sage_invoice", "0002_alter_invoice_template_choice"),
]

operations = [
migrations.AlterField(
model_name="item",
name="quantity",
field=models.PositiveIntegerField(
blank=True,
db_comment="The quantity of the invoice item",
help_text="The quantity of the item.",
null=True,
verbose_name="Quantity",
),
),
]
26 changes: 26 additions & 0 deletions sage_invoice/migrations/0004_alter_expense_invoice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 5.1.2 on 2024-10-17 13:00

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("sage_invoice", "0003_alter_item_quantity"),
]

operations = [
migrations.AlterField(
model_name="expense",
name="invoice",
field=models.OneToOneField(
db_comment="Reference to the associated invoice",
help_text="The invoice associated with this total.",
on_delete=django.db.models.deletion.CASCADE,
related_name="expense",
to="sage_invoice.invoice",
verbose_name="Invoice",
),
),
]
2 changes: 1 addition & 1 deletion sage_invoice/models/expense.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class Expense(models.Model):
"Invoice",
verbose_name=_("Invoice"),
on_delete=models.CASCADE,
related_name="total",
related_name="expense",
help_text=_("The invoice associated with this total."),
db_comment="Reference to the associated invoice",
)
Expand Down
8 changes: 6 additions & 2 deletions sage_invoice/models/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class Item(models.Model):
)
quantity = models.PositiveIntegerField(
verbose_name=_("Quantity"),
default=1,
null=True,
blank=True,
help_text=_("The quantity of the item."),
db_comment="The quantity of the invoice item",
)
Expand Down Expand Up @@ -49,7 +50,10 @@ class Item(models.Model):
)

def save(self, *args, **kwargs):
self.total_price = self.quantity * self.unit_price
if self.quantity:
self.total_price = self.quantity * self.unit_price
else:
self.total_price = self.unit_price
super().save(*args, **kwargs)

def __str__(self):
Expand Down
165 changes: 116 additions & 49 deletions sage_invoice/templates/quotation_4.html
Original file line number Diff line number Diff line change
@@ -1,53 +1,64 @@
<!DOCTYPE html>
<html class="no-js" lang="en">
{% load static custom_filters %}
{% load static custom_filters humanize i18n %}

<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="author" content="Laralink">
<title>{{ title }}</title>
<link rel="stylesheet" href="{% static 'assets/css/style.css' %}">

</head>

<body>
<div class="tm_container">
<div class="tm_invoice_wrap">
<div class="tm_invoice tm_style1" id="tm_download_section">
<div class="tm_invoice_in">
<div class="tm_invoice_head tm_align_center tm_mb20">
<div class="tm_invoice_left">
{% if logo_url %}
<div class="tm_logo"><img src="{{ logo_url }}" alt="Logo"></div>
{% if invoice.logo %}
<div class="tm_logo">
<img src="{{ invoice.logo.url }}" alt="Logo">
</div>
{% endif %}
</div>
<div class="tm_invoice_right tm_text_right">
<div class="tm_primary_color tm_f50 tm_text_uppercase">{{ title }}</div>
<div class="tm_primary_color tm_f50 tm_text_uppercase">{{ invoice.title }}</div>
</div>
</div>
<div class="tm_invoice_info tm_mb20">
<div class="tm_invoice_seperator tm_gray_bg"></div>
<div class="tm_invoice_info_list">
<p class="tm_invoice_number tm_m0">Invoice No: <b class="tm_primary_color">{{ tracking_code }}</b></p>
<p class="tm_invoice_date tm_m0">Date: <b class="tm_primary_color">{{ invoice_date }}</b></p>
<p class="tm_invoice_number tm_m0">
{% trans "Invoice No: " %}
<b class="tm_primary_color">{{ invoice.tracking_code }}</b>
</p>
<p class="tm_invoice_date tm_m0">
{% trans "Date: " %}
<b class="tm_primary_color">{{ invoice.invoice_date }}</b>
</p>
</div>
</div>
<div class="tm_invoice_head tm_mb10">
<div class="tm_invoice_left">
<p class="tm_mb2"><b class="tm_primary_color">Invoice To:</b></p>
<p class="tm_mb2">
<b class="tm_primary_color">
{% trans "Invoice To:" %}
</b>
</p>
<p>
{{ customer_name }} <br>
{{ customer_email }} <br />
{{ customer_phone }}
{{ invoice.customer_name }} <br>
{% if customer_email %}
{{ customer_email|default:'' }}
{% else %}
{{ customer_phone|default:'' }}
{% endif %}
</p>
</div>
<div class="tm_invoice_right tm_text_right">
<p class="tm_mb2"><b class="tm_primary_color">Pay To:</b></p>
<p>
Your Company Name <br>
Company Address <br />
</p>

</div>
</div>

Expand All @@ -58,29 +69,54 @@
<table>
<thead>
<tr>
<th class="tm_width_3 tm_semi_bold tm_primary_color tm_gray_bg">Item</th>
<th class="tm_width_4 tm_semi_bold tm_primary_color tm_gray_bg">Description</th>
{% for column in custom_columns %}
<th class="tm_width_2 tm_semi_bold tm_primary_color tm_gray_bg">{{ column }}</th>
<th class="tm_width_1 tm_semi_bold tm_primary_color tm_gray_bg">{% trans "Item" %}</th>
<th class="tm_width_12 tm_semi_bold tm_primary_color tm_gray_bg">{% trans "Description" %}</th>
<th class="tm_width_1 tm_semi_bold tm_primary_color tm_gray_bg">
{% trans "Qty" %}
</th>
{% for column in invoice.columns.all %}
<th class="tm_width_2 tm_semi_bold tm_primary_color tm_gray_bg">
{{ column }}
</th>
{% endfor %}
<th class="tm_width_2 tm_semi_bold tm_primary_color tm_gray_bg">Price</th>
<th class="tm_width_1 tm_semi_bold tm_primary_color tm_gray_bg">Qty</th>
<th class="tm_width_2 tm_semi_bold tm_primary_color tm_gray_bg tm_text_right">Total</th>
<th class="tm_width_2 tm_semi_bold tm_primary_color tm_gray_bg">
{% trans "Price" %}
</th>

<th class="tm_width_2 tm_semi_bold tm_primary_color tm_gray_bg tm_text_right">
{% trans "Total" %}
</th>

</tr>
</thead>
<tbody>
{% for item in items %}
{% for item in invoice.items.all %}
<tr>
<td class="tm_width_3">{{ forloop.counter }}. </td>
<td class="tm_width_4">{{ item.description }}</td>
{% for column in custom_columns %}
<td class="tm_width_2">{{ item.custom_data|get_item:column }}</td>
<td class="tm_width_1">
{% if item.quantity %}
{{ item.quantity }}
{% else %}
-
{% endif %}
{{ item.measurement|default:'' }}
</td>
{% for column in invoice.columns.all %}
<td class="tm_width_2">
{% if column.item == item %}
{{ column.value }}
{% else %}
-
{% endif %}
</td>
{% endfor %}
<td class="tm_width_2">{{ item.unit_price }} {{ currency }}</td>
<td class="tm_width_1">{{ item.quantity }} {{ item.measurement }}</td>
<td class="tm_width_2 tm_text_right">{{ item.total_price }} {{ currency }}</td>

<td class="tm_width_2">
{{ item.unit_price|intcomma }} {{ invoice.currency }}
</td>
<td class="tm_width_2 tm_text_right">
{{ item.total_price|intcomma }} {{ invoice.currency }}
</td>
</tr>
{% endfor %}
</tbody>
Expand All @@ -92,36 +128,60 @@
<!-- Payment and Totals -->
<div class="tm_invoice_footer">
<div class="tm_left_footer">
<p class="tm_mb2"><b class="tm_primary_color">Payment info:</b></p>
<p class="tm_mb2">
<b class="tm_primary_color">
{% trans "Payment info:" %}
</b>
</p>
<p class="tm_m0">{{ status }}</p>
{% if sign_url %}
<img src="{{ sign_url }}" alt="Sign">
{% else %}
<img src="{% static 'assets/img/default_signature.png' %}" alt="Sign">
{% if invoice.signature %}
<img src="{{ invoice.signature.url }}" style="max-width: 20%;" alt="Sign">
{% else %}
<img src="{% static 'assets/img/default_signature.png' %}" alt="Sign">
{% endif %}
</div>
<div class="tm_right_footer">
<table>
<tbody>
<tr>
<td class="tm_width_3 tm_primary_color tm_border_none tm_bold">Subtotal</td>
<td class="tm_width_3 tm_primary_color tm_text_right tm_border_none tm_bold">{{ subtotal }} {{currency}}</td>
<td class="tm_width_3 tm_primary_color tm_border_none tm_bold">
{% trans "Subtotal" %}
</td>
<td class="tm_width_3 tm_primary_color tm_text_right tm_border_none tm_bold">
{{ invoice.expense.subtotal|intcomma }}
{{currency}}
</td>
</tr>
<tr>
<td class="tm_width_3 tm_primary_color tm_border_none tm_pt0">Tax ({{ tax_percentage }}%)</td>
<td class="tm_width_3 tm_primary_color tm_text_right tm_border_none tm_pt0">{{ tax_amount }}</td>
<td class="tm_width_3 tm_primary_color tm_border_none tm_pt0">
{% trans "Tax" %} ({{ invoice.expense.tax_percentage }}%)
</td>
<td class="tm_width_3 tm_primary_color tm_text_right tm_border_none tm_pt0">
{{ invoice.expense.tax_amount }}
</td>
</tr>
<tr>
<td class="tm_width_3 tm_primary_color tm_border_none tm_pt0">Discount ({{ discount_percentage }}%)</td>
<td class="tm_width_3 tm_primary_color tm_text_right tm_border_none tm_pt0">-{{ discount_amount }}</td>
<td class="tm_width_3 tm_primary_color tm_border_none tm_pt0">
{% trans "Discount" %} ({{ invoice.expense.discount_percentage }}%)
</td>
<td class="tm_width_3 tm_primary_color tm_text_right tm_border_none tm_pt0">
- {{ invoice.expense.discount_amount }}
</td>
</tr>
<tr>
<td class="tm_width_3 tm_primary_color tm_border_none tm_pt0">Concession ({{ concession_percentage }}%)</td>
<td class="tm_width_3 tm_primary_color tm_text_right tm_border_none tm_pt0">-{{ concession_amount }}</td>
<td class="tm_width_3 tm_primary_color tm_border_none tm_pt0">
{% trans "Concession" %}
({{ invoice.expense.concession_percentage }}%)</td>
<td class="tm_width_3 tm_primary_color tm_text_right tm_border_none tm_pt0">
-{{ invoice.expense.concession_amount }}
</td>
</tr>
<tr class="tm_border_top tm_border_bottom">
<td class="tm_width_3 tm_border_top_0 tm_bold tm_f16 tm_primary_color">Grand Total</td>
<td class="tm_width_3 tm_border_top_0 tm_bold tm_f16 tm_primary_color tm_text_right">{{ grand_total }} {{currency}}</td>
<td class="tm_width_3 tm_border_top_0 tm_bold tm_f16 tm_primary_color">
{% trans "Grand Total" %}
</td>
<td class="tm_width_3 tm_border_top_0 tm_bold tm_f16 tm_primary_color tm_text_right">
{{ invoice.expense.total_amount|intcomma }} {{ invoice.currency}}</td>
</tr>
</tbody>
</table>
Expand All @@ -139,14 +199,20 @@
</div>
</div>
{% endfor %}


</div>
</div>
<div class="tm_invoice_btns tm_hide_print">
<a href="javascript:window.print()" class="tm_invoice_btn tm_color1">
<span class="tm_btn_icon">
<svg xmlns="http://www.w3.org/2000/svg" class="ionicon" viewBox="0 0 512 512"><path d="M384 368h24a40.12 40.12 0 0040-40V168a40.12 40.12 0 00-40-40H104a40.12 40.12 0 00-40 40v160a40.12 40.12 0 0040 40h24" fill="none" stroke="currentColor" stroke-linejoin="round" stroke-width="32"/><rect x="128" y="240" width="256" height="208" rx="24.32" ry="24.32" fill="none" stroke="currentColor" stroke-linejoin="round" stroke-width="32"/><path d="M384 128v-24a40.12 40.12 0 00-40-40H168a40.12 40.12 0 00-40 40v24" fill="none" stroke="currentColor" stroke-linejoin="round" stroke-width="32"/><circle cx="392" cy="184" r="24" fill="currentColor"/></svg>
<svg xmlns="http://www.w3.org/2000/svg" class="ionicon" viewBox="0 0 512 512">
<path
d="M384 368h24a40.12 40.12 0 0040-40V168a40.12 40.12 0 00-40-40H104a40.12 40.12 0 00-40 40v160a40.12 40.12 0 0040 40h24"
fill="none" stroke="currentColor" stroke-linejoin="round" stroke-width="32" />
<rect x="128" y="240" width="256" height="208" rx="24.32" ry="24.32" fill="none" stroke="currentColor"
stroke-linejoin="round" stroke-width="32" />
<path d="M384 128v-24a40.12 40.12 0 00-40-40H168a40.12 40.12 0 00-40 40v24" fill="none"
stroke="currentColor" stroke-linejoin="round" stroke-width="32" />
<circle cx="392" cy="184" r="24" fill="currentColor" /></svg>
</span>
<span class="tm_btn_text">Print</span>
</a>
Expand All @@ -158,4 +224,5 @@
<script src="{% static 'assets/js/html2canvas.min.js' %}"></script>
<script src="{% static 'assets/js/main.js' %}"></script>
</body>
</html>

</html>
Loading

0 comments on commit 2190343

Please sign in to comment.