-
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(models): link service/donation directly to customer
- Loading branch information
Showing
5 changed files
with
127 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
weblate_web/migrations/0030_donation_customer_service_customer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Generated by Django 5.0.6 on 2024-10-11 11:58 | ||
|
||
import django.db.models.deletion | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("payments", "0028_alter_customer_email"), | ||
("weblate_web", "0029_package_category"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="donation", | ||
name="customer", | ||
field=models.ForeignKey( | ||
null=True, | ||
on_delete=django.db.models.deletion.CASCADE, | ||
to="payments.customer", | ||
), | ||
), | ||
migrations.AddField( | ||
model_name="service", | ||
name="customer", | ||
field=models.ForeignKey( | ||
null=True, | ||
on_delete=django.db.models.deletion.CASCADE, | ||
to="payments.customer", | ||
), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# Generated by Django 5.0.6 on 2024-10-11 11:59 | ||
|
||
from django.conf import settings | ||
from django.db import migrations | ||
from django_countries import countries | ||
from fakturace.storage import InvoiceStorage | ||
|
||
|
||
def get_customer(customer_model, payments, invoice_storage, instance_name): | ||
# First look at the invoice | ||
for payment in payments: | ||
if not payment.invoice: | ||
continue | ||
try: | ||
invoice = invoice_storage.get(payment.invoice) | ||
except KeyError: | ||
print(f"<{instance_name}>: Could not load invoice {payment.invoice}!") | ||
continue | ||
|
||
# Try using database model | ||
contact_id = invoice.invoice["contact"] | ||
if contact_id.startswith("web-"): | ||
contact_pk = int(contact_id[4:]) | ||
try: | ||
return customer_model.objects.get(pk=contact_pk) | ||
except customer_model.DoesNotExist: | ||
print( | ||
f"<{instance_name}>: Could not load contact {contact_pk} ({contact_id})" | ||
) | ||
|
||
# Create from the data | ||
print(f"<{instance_name}>: Matching customer for {invoice.contact['name']}") | ||
return customer_model.objects.get_or_create( | ||
vat=invoice.contact.get("vat_reg", None), | ||
name=invoice.contact["name"], | ||
address=invoice.contact["address"], | ||
city=invoice.contact["city"], | ||
country=countries.by_name(invoice.contact["country"]), | ||
user_id=-1, | ||
origin="https://weblate.org/auto", | ||
email=invoice.contact.get("email", ""), | ||
)[0] | ||
|
||
print(f"<{instance_name}>: Fallback to model: {payments[0].customer.name}") | ||
|
||
# Fallback to model | ||
return payments[0].customer | ||
|
||
|
||
def update_customer(apps, schema_editor): | ||
print() | ||
Donation = apps.get_model("weblate_web", "Donation") | ||
Service = apps.get_model("weblate_web", "Service") | ||
Payment = apps.get_model("payments", "Payment") | ||
Customer = apps.get_model("payments", "Customer") | ||
|
||
invoice_storage = InvoiceStorage(settings.PAYMENT_FAKTURACE) | ||
|
||
for donation in Donation.objects.filter(customer=None): | ||
payment = Payment.objects.get(pk=donation.payment) | ||
donation.customer = get_customer( | ||
Customer, [payment], invoice_storage, f"Donation {donation.id}" | ||
) | ||
donation.save(update_fields=["customer"]) | ||
|
||
for service in Service.objects.filter(customer=None): | ||
payments = Payment.objects.filter( | ||
pk__in=service.subscription_set.values_list("payment", flat=True) | ||
) | ||
if not payments: | ||
print( | ||
f"<Service {service.pk}> missing payment {service.status}: {service.site_title} {service.site_url} {service.note}" | ||
) | ||
continue | ||
service.customer = get_customer( | ||
Customer, payments, invoice_storage, f"Service {service.id}" | ||
) | ||
service.save(update_fields=["customer"]) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("weblate_web", "0030_donation_customer_service_customer"), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(update_customer, migrations.RunPython.noop, elidable=True), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters