from typing import Any from django import forms from django.utils.translation import gettext_lazy as _ from apps.customers.application.dto import BillingUnitData, ContactData, CustomerDetailsData from apps.customers.domain.enums import CustomerType from apps.customers.presentation.labels import TYPE_CHOICES from apps.shared.presentation.forms import DaisyFormMixin class CustomerForm(DaisyFormMixin, forms.Form): name = forms.CharField(label=_("Name"), max_length=200) type = forms.ChoiceField( label=_("Type"), choices=TYPE_CHOICES, initial=CustomerType.COMPANY.value ) business_id = forms.CharField( label=_("Business ID"), max_length=20, required=False, help_text=_("For example 1234567-1") ) personal_identity_code = forms.CharField( label=_("Personal identity code"), max_length=20, required=False, # The stored code is never sent back to the browser. widget=forms.TextInput(attrs={"autocomplete": "off", "spellcheck": "false"}), help_text=_("Needed only for invoicing a consumer."), ) remove_personal_id = forms.BooleanField( label=_("Remove personal identity code"), required=False ) def __init__( self, *args: Any, stored_personal_id: str | None = None, can_manage_personal_id: bool = False, is_internal: bool = False, **kwargs: Any, ) -> None: super().__init__(*args, **kwargs) self.fields["type"].widget.attrs["x-model"] = "type" if stored_personal_id: self.fields["personal_identity_code"].help_text = _( "Stored: %(masked)s. Leave empty to keep it." ) % {"masked": stored_personal_id} if not (stored_personal_id and can_manage_personal_id): del self.fields["remove_personal_id"] if is_internal: self.fields["type"].disabled = True self.fields["type"].help_text = _("The internal customer is always a company.") def to_dto(self) -> CustomerDetailsData: data = self.cleaned_data customer_type = CustomerType(data["type"]) is_company = customer_type is CustomerType.COMPANY return CustomerDetailsData( name=data["name"], type=customer_type, business_id=data["business_id"] if is_company else "", personal_identity_code="" if is_company else data["personal_identity_code"], remove_personal_id=bool(data.get("remove_personal_id")) and not is_company, ) class ContactForm(DaisyFormMixin, forms.Form): name = forms.CharField(label=_("Name"), max_length=150) title = forms.CharField(label=_("Title or role"), max_length=150, required=False) phone = forms.CharField( label=_("Phone"), max_length=50, required=False, widget=forms.TextInput(attrs={"type": "tel"}), ) email = forms.EmailField(label=_("Email"), required=False) note = forms.CharField( label=_("Note"), required=False, widget=forms.Textarea(attrs={"rows": 3}) ) def to_dto(self) -> ContactData: return ContactData(**self.cleaned_data) class BillingUnitForm(DaisyFormMixin, forms.Form): name = forms.CharField(label=_("Name"), max_length=200) billing_address = forms.CharField( label=_("Billing address"), required=False, widget=forms.Textarea(attrs={"rows": 3}), help_text=_("Postal address or billing email address."), ) e_invoice_address = forms.CharField( label=_("E-invoice address"), max_length=40, required=False, help_text=_("12–35 letters or digits."), ) e_invoice_operator = forms.CharField( label=_("Operator ID"), max_length=35, required=False, help_text=_("Required when an e-invoice address is given."), ) reference = forms.CharField(label=_("Reference"), max_length=100, required=False) def to_dto(self) -> BillingUnitData: return BillingUnitData(**self.cleaned_data) class CustomerFilterForm(DaisyFormMixin, forms.Form): q = forms.CharField(label=_("Search"), required=False, help_text=_("Name or business ID")) type = forms.ChoiceField( label=_("Type"), required=False, choices=[("", _("All types")), *TYPE_CHOICES] )