Skip to content

Commit

Permalink
docs(sage_invoice): Add the new jinja 2 template, Change the models n…
Browse files Browse the repository at this point in the history
…ame add note opition
  • Loading branch information
radinceorc committed Sep 16, 2024
1 parent 05582eb commit 28125e2
Show file tree
Hide file tree
Showing 33 changed files with 1,484 additions and 358 deletions.
4 changes: 2 additions & 2 deletions sage_invoice/admin/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .category import InvoiceCategoryAdmin
from .category import CategoryAdmin
from .invoice import InvoiceAdmin

__all__ = ["InvoiceAdmin", "InvoiceCategoryAdmin"]
__all__ = ["InvoiceAdmin", "CategoryAdmin"]
4 changes: 2 additions & 2 deletions sage_invoice/admin/actions/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .show import show_invoice
from .download_pdf import export_pdf

__all__ = ["show_invoice"]
__all__ = ["export_pdf"]
11 changes: 11 additions & 0 deletions sage_invoice/admin/actions/download_pdf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.contrib import admin
from django.shortcuts import redirect
from django.urls import reverse


@admin.action(description="Download selected invoice as PDF")
def export_pdf(modeladmin, request, queryset):
invoice_ids = queryset.values_list("id", flat=True)
invoice_ids_str = ",".join(map(str, invoice_ids))
url = f"{reverse('download_invoices')}?invoice_ids={invoice_ids_str}"
return redirect(url)
11 changes: 0 additions & 11 deletions sage_invoice/admin/actions/show.py

This file was deleted.

6 changes: 3 additions & 3 deletions sage_invoice/admin/category.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from django.contrib import admin
from django.utils.translation import gettext_lazy as _

from sage_invoice.models import InvoiceCategory
from sage_invoice.models import Category


@admin.register(InvoiceCategory)
class InvoiceCategoryAdmin(admin.ModelAdmin):
@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
admin_priority = 2
list_display = ("title", "description")
search_fields = ("title", "description")
Expand Down
42 changes: 25 additions & 17 deletions sage_invoice/admin/invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,33 @@
from django.utils.translation import gettext_lazy as _
from import_export.admin import ImportExportModelAdmin

from sage_invoice.admin.actions import show_invoice
from sage_invoice.models import Expense, Invoice, InvoiceColumn, InvoiceItem
from sage_invoice.admin.actions import export_pdf
from sage_invoice.models import Column, Expense, Invoice, Item
from sage_invoice.resource import InvoiceResource


class InvoiceItemInline(admin.TabularInline):
model = InvoiceItem
class ItemInline(admin.TabularInline):
model = Item
extra = 0
min_num = 1
readonly_fields = ("total_price",)


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


class ExpenseInline(admin.TabularInline):
model = Expense
extra = 1
readonly_fields = ("subtotal", "tax_amount", "discount_amount", "total_amount")
readonly_fields = (
"subtotal",
"tax_amount",
"discount_amount",
"total_amount",
"concession_amount",
)


@admin.register(Invoice)
Expand All @@ -31,12 +37,12 @@ class InvoiceAdmin(ImportExportModelAdmin, admin.ModelAdmin):
admin_priority = 1
list_display = ("title", "invoice_date", "customer_name", "status")
search_fields = ("customer_name", "status", "customer_email")
autocomplete_fields = ("category",)
save_on_top = True
list_filter = ("status", "invoice_date", "category")
ordering = ("-invoice_date",)
autocomplete_fields = ("category",)
readonly_fields = ("slug",)
actions = [show_invoice]
actions = [export_pdf]

class Media:
js = ("assets/js/invoice_admin.js",)
Expand All @@ -53,19 +59,21 @@ def get_fieldsets(self, request, obj=None):
"tracking_code",
"due_date",
"customer_name",
"customer_email",
"contacts",
"category",
"receipt",
),
"description": _(
"Basic details of the invoice including title, date, and customer information."
"""
Basic details of the invoice including title, date,
and customer information."""
),
},
),
(
_("Status & Notes"),
_("Status & Currency"),
{
"fields": ("status", "notes"),
"fields": ("status", "notes", "currency"),
"description": _(
"Current status of the invoice and any additional notes."
),
Expand All @@ -91,19 +99,19 @@ def get_fieldsets(self, request, obj=None):

return fieldsets

inlines = [InvoiceItemInline, InvoiceColumnInline, ExpenseInline]
inlines = [ItemInline, ColumnInline, ExpenseInline]

def get_inline_instances(self, request, obj=None):
inlines = []
if obj and obj.pk:
inlines = [
InvoiceItemInline(self.model, self.admin_site),
InvoiceColumnInline(self.model, self.admin_site),
ItemInline(self.model, self.admin_site),
ColumnInline(self.model, self.admin_site),
ExpenseInline(self.model, self.admin_site),
]
else:
inlines = [
InvoiceItemInline(self.model, self.admin_site),
ItemInline(self.model, self.admin_site),
ExpenseInline(self.model, self.admin_site),
]
return inlines
29 changes: 29 additions & 0 deletions sage_invoice/helpers/choice.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,32 @@
class InvoiceStatus(models.TextChoices):
PAID = ("paid", "PAID")
UNPAID = ("unpaid", "UNPAID")


class Currency(models.TextChoices):
USD = ("USD", "US Dollar")
EUR = ("EUR", "Euro")
GBP = ("GBP", "British Pound")
JPY = ("JPY", "Japanese Yen")
AUD = ("AUD", "Australian Dollar")
CAD = ("CAD", "Canadian Dollar")
CHF = ("CHF", "Swiss Franc")
CNY = ("CNY", "Chinese Yuan")
INR = ("INR", "Indian Rupee")
RUB = ("RUB", "Russian Ruble")
AED = ("AED", "UAE Dirham")
SAR = ("SAR", "Saudi Riyal")
TRY = ("TRY", "Turkish Lira")
BRL = ("BRL", "Brazilian Real")
ZAR = ("ZAR", "South African Rand")
NZD = ("NZD", "New Zealand Dollar")
KRW = ("KRW", "South Korean Won")
SGD = ("SGD", "Singapore Dollar")
MXN = ("MXN", "Mexican Peso")
IRR = ("IRR", "Iranian Rial")
TOMAN = ("TOMAN", "Iranian Toman")
QAR = ("QAR", "Qatari Riyal")
KWD = ("KWD", "Kuwaiti Dinar")
BHD = ("BHD", "Bahraini Dinar")
OMR = ("OMR", "Omani Rial")
EGP = ("EGP", "Egyptian Pound")
15 changes: 15 additions & 0 deletions sage_invoice/helpers/funcs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import os
import secrets
from datetime import datetime

from django.conf import settings

Expand Down Expand Up @@ -28,3 +30,16 @@ def get_template_choices(is_receipt=False):
]

return choices or [("", "No Templates Available")]


def generate_tracking_code(user_input: str, creation_date: datetime) -> str:
"""Generate a unique tracking code based on user input and the creation
date.
Returns:
str: A unique tracking code.
"""
date_str = creation_date.strftime("%Y%m%d")
random_number = secrets.randbelow(8000) + 1000
tracking_code = f"{user_input}-{date_str}-{random_number}"
return tracking_code
10 changes: 5 additions & 5 deletions sage_invoice/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .category import Category
from .column import Column
from .expense import Expense
from .invoice import Invoice
from .invoice_category import InvoiceCategory
from .invoice_column import InvoiceColumn
from .invoice_item import InvoiceItem
from .invoice_total import Expense
from .item import Item

__all__ = ["Invoice", "InvoiceColumn", "InvoiceItem", "Expense", "InvoiceCategory"]
__all__ = ["Invoice", "Column", "Item", "Expense", "Category"]
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,20 @@
from sage_tools.mixins.models import TitleSlugMixin


class InvoiceCategory(TitleSlugMixin):
class Category(TitleSlugMixin):
description = models.CharField(
verbose_name=_("Description"),
max_length=255,
verbose_name=_("Description"),
null=True,
blank=True,
help_text=_("Description of the Category."),
db_comment="The description of the category",
db_comment="Description of the Category",
)

def __str__(self):
return f"{self.title}"

def __repr__(self):
return f"{self.title}"

class Meta:
verbose_name = _("Invoice Category")
verbose_name_plural = _("Invoice Categories ")
db_table = "sage_invoice_category"
verbose_name = _("Category")
verbose_name_plural = _("Categories ")
db_table = "sage_invoice_cat"
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.utils.translation import gettext_lazy as _


class InvoiceColumn(models.Model):
class Column(models.Model):
priority = models.PositiveIntegerField(
verbose_name=_("Priority"),
help_text=_("The priority associated with each custom column."),
Expand Down Expand Up @@ -30,7 +30,7 @@ class InvoiceColumn(models.Model):
db_comment="invoice of the custom column",
)
item = models.ForeignKey(
"InvoiceItem",
"Item",
on_delete=models.CASCADE,
related_name="columns",
verbose_name=_("Item"),
Expand All @@ -42,10 +42,10 @@ def __str__(self):
return f"{self.column_name}"

def __repr__(self) -> str:
return f"Invoice Column> {self.column_name}"
return f"Column> {self.column_name}"

class Meta:
verbose_name = _("Invoice Column")
verbose_name_plural = _("Invoice Columns")
verbose_name = _("Column")
verbose_name_plural = _("Columns")
db_table = "sage_invoice_columns"
ordering = ["priority"]
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ class Expense(models.Model):
help_text=_("The discount percentage applied to the invoice."),
db_comment="The percentage of discount applied to the invoice",
)
concession_percentage = models.DecimalField(
verbose_name=_("Concession Percentage"),
max_digits=5,
decimal_places=2,
default=Decimal("0.00"),
help_text=_("The concession percentage applied to the invoice."),
db_comment="The percentage of concession applied to the invoice",
)
tax_amount = models.DecimalField(
verbose_name=_("Tax Amount"),
max_digits=10,
Expand All @@ -45,6 +53,14 @@ class Expense(models.Model):
help_text=_("The calculated discount amount."),
db_comment="The total discount amount calculated based on the subtotal and discount percentage",
)
concession_amount = models.DecimalField(
verbose_name=_("Concession Amount"),
max_digits=10,
decimal_places=2,
default=Decimal("0.00"),
help_text=_("The calculated concession amount."),
db_comment="The total concession amount calculated based on the subtotal and discount percentage",
)
total_amount = models.DecimalField(
verbose_name=_("Total Amount"),
max_digits=10,
Expand Down
Loading

0 comments on commit 28125e2

Please sign in to comment.