mirror of
https://gh.llkk.cc/https://github.com/WeblateOrg/hosted.git
synced 2026-07-23 13:13:45 +08:00
76 lines
2.1 KiB
Python
76 lines
2.1 KiB
Python
# Generated by Codex on 2026-05-25
|
|
|
|
from django.core.exceptions import ValidationError
|
|
from django.db import migrations
|
|
|
|
DEFAULT_DB = "default"
|
|
PAYMENTS_DB = "payments_db"
|
|
|
|
|
|
def get_payment_pk(invoice):
|
|
if isinstance(invoice.payment, dict):
|
|
return invoice.payment.get("pk")
|
|
return None
|
|
|
|
|
|
def iter_payment_pks(billing, invoice_model):
|
|
for invoice in (
|
|
invoice_model.objects.using(DEFAULT_DB)
|
|
.filter(billing_id=billing.pk)
|
|
.order_by("-start", "-pk")
|
|
.iterator()
|
|
):
|
|
if payment_pk := get_payment_pk(invoice):
|
|
yield payment_pk
|
|
|
|
if not isinstance(billing.payment, dict):
|
|
return
|
|
|
|
for payment_pk in reversed(billing.payment.get("all", [])):
|
|
yield payment_pk
|
|
|
|
if payment_pk := billing.payment.get("recurring"):
|
|
yield payment_pk
|
|
|
|
|
|
def fill_billing_customer_name(apps, schema_editor) -> None:
|
|
if schema_editor.connection.alias != DEFAULT_DB:
|
|
return
|
|
|
|
Billing = apps.get_model("billing", "Billing")
|
|
Invoice = apps.get_model("billing", "Invoice")
|
|
Payment = apps.get_model("payments", "Payment")
|
|
|
|
for billing in (
|
|
Billing.objects.using(DEFAULT_DB).filter(customer_name="").iterator()
|
|
):
|
|
for payment_pk in iter_payment_pks(billing, Invoice):
|
|
try:
|
|
payment = (
|
|
Payment.objects.using(PAYMENTS_DB)
|
|
.select_related("customer")
|
|
.get(pk=payment_pk)
|
|
)
|
|
except (Payment.DoesNotExist, TypeError, ValueError, ValidationError):
|
|
continue
|
|
|
|
if not payment.customer.name:
|
|
continue
|
|
|
|
billing.customer_name = payment.customer.name
|
|
billing.save(using=DEFAULT_DB, update_fields=["customer_name"])
|
|
break
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
dependencies = [
|
|
("billing", "0010_billing_customer_name"),
|
|
(
|
|
"payments",
|
|
"0034_customer_vat_validated_customer_vat_validation_error_and_more",
|
|
),
|
|
]
|
|
|
|
operations = [
|
|
migrations.RunPython(fill_billing_customer_name, migrations.RunPython.noop),
|
|
]
|