Skip to content

Commit

Permalink
feat: better subscription period formatting on invoice
Browse files Browse the repository at this point in the history
Do not include it in description, include it as separate fields. This
makes it also disappear from the payment info where the field is
limited.
  • Loading branch information
nijel committed Nov 8, 2024
1 parent 1c4ab44 commit 02c51c5
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 5.1.2 on 2024-11-08 13:21

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("invoices", "0014_alter_invoice_currency"),
]

operations = [
migrations.AddField(
model_name="invoiceitem",
name="end_date",
field=models.DateField(blank=True, null=True),
),
migrations.AddField(
model_name="invoiceitem",
name="start_date",
field=models.DateField(blank=True, null=True),
),
]
25 changes: 20 additions & 5 deletions weblate_web/invoices/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
TEMPLATES_PATH = Path(__file__).parent / "templates"


def date_format(value: datetime.datetime) -> str:
def date_format(value: datetime.datetime | datetime.date) -> str:
return value.strftime("%-d %b %Y")


Expand Down Expand Up @@ -518,6 +518,8 @@ def duplicate(
quantity_unit=item.quantity_unit,
unit_price=item.unit_price,
package=item.package,
start_date=item.start_date,
end_date=item.end_date,
)
return invoice

Expand Down Expand Up @@ -587,6 +589,8 @@ class InvoiceItem(models.Model):
package = models.ForeignKey(
"weblate_web.Package", on_delete=models.deletion.SET_NULL, null=True, blank=True
)
start_date = models.DateField(blank=True, null=True)
end_date = models.DateField(blank=True, null=True)

def __str__(self) -> str:
return f"{self.description} ({self.display_quantity}) {self.display_price}"
Expand All @@ -613,6 +617,9 @@ def save( # type: ignore[override]
)
extra_fields.append("unit_price")
if not self.description:
self.description = self.package.verbose
extra_fields.append("description")

if (start_date := self.invoice.extra.get("start_date")) and (
repeat := self.package.get_repeat()
):
Expand All @@ -624,10 +631,9 @@ def save( # type: ignore[override]
+ get_period_delta(repeat)
- datetime.timedelta(days=1)
)
self.description = f"{self.package.verbose} [{date_format(start_date)} - {date_format(end_date)}]"
else:
self.description = self.package.verbose
extra_fields.append("description")
self.start_date = start_date.date()
self.end_date = end_date.date()
extra_fields.extend(("start_date", "end_date"))

if extra_fields and update_fields is not None:
update_fields = tuple(set(update_fields).union(extra_fields))
Expand All @@ -639,6 +645,15 @@ def save( # type: ignore[override]
update_fields=update_fields,
)

@property
def has_date_range(self) -> bool:
return self.start_date is not None and self.end_date is not None

def get_date_range_display(self) -> str:
if self.start_date is not None and self.end_date is not None:
return f"{date_format(self.start_date)} - {date_format(self.end_date)}"
return ""

def clean(self):
if not self.description and not self.package:
raise ValidationError(
Expand Down
5 changes: 4 additions & 1 deletion weblate_web/invoices/templates/invoice-template.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ <h2>Issued by</h2>
<tbody>
{% for item in invoice.all_items %}
<tr>
<td>{{ item.description }}</td>
<td>
{{ item.description }}
{% if item.has_date_range %}<em class="period">{{ item.get_date_range_display }}</em>{% endif %}
</td>
<td>{{ item.display_quantity }}</td>
<td>{{ item.display_price }}</td>
<td>{{ item.display_total_price }}</td>
Expand Down
4 changes: 4 additions & 0 deletions weblate_web/invoices/templates/invoice.css
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,14 @@ footer {
#details {
width: 100%;
}
em.period {
display: block;
}
footer p,
footer table {
margin-top: 5mm;
}
em.period,
footer p,
footer p a,
#details,
Expand Down

0 comments on commit 02c51c5

Please sign in to comment.