"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""

from __future__ import annotations
from .lineitem import LineItem, LineItemTypedDict
from datetime import datetime
from panora_sdk.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
)
from pydantic import model_serializer
from typing import List, Optional, TypedDict
from typing_extensions import NotRequired


class UnifiedAccountingPurchaseorderInputFieldMappingsTypedDict(TypedDict):
    r"""The custom field mappings of the object between the remote 3rd party & Panora"""


class UnifiedAccountingPurchaseorderInputFieldMappings(BaseModel):
    r"""The custom field mappings of the object between the remote 3rd party & Panora"""


class UnifiedAccountingPurchaseorderInputTypedDict(TypedDict):
    status: NotRequired[Nullable[str]]
    r"""The status of the purchase order"""
    issue_date: NotRequired[Nullable[datetime]]
    r"""The issue date of the purchase order"""
    purchase_order_number: NotRequired[Nullable[str]]
    r"""The purchase order number"""
    delivery_date: NotRequired[Nullable[datetime]]
    r"""The delivery date for the purchase order"""
    delivery_address: NotRequired[Nullable[str]]
    r"""The UUID of the delivery address"""
    customer: NotRequired[Nullable[str]]
    r"""The UUID of the customer"""
    vendor: NotRequired[Nullable[str]]
    r"""The UUID of the vendor"""
    memo: NotRequired[Nullable[str]]
    r"""A memo or note for the purchase order"""
    company_id: NotRequired[Nullable[str]]
    r"""The UUID of the company"""
    total_amount: NotRequired[Nullable[float]]
    r"""The total amount of the purchase order in cents"""
    currency: NotRequired[Nullable[str]]
    r"""The currency of the purchase order"""
    exchange_rate: NotRequired[Nullable[str]]
    r"""The exchange rate applied to the purchase order"""
    tracking_categories: NotRequired[Nullable[List[str]]]
    r"""The UUIDs of the tracking categories associated with the purchase order"""
    accounting_period_id: NotRequired[Nullable[str]]
    r"""The UUID of the associated accounting period"""
    line_items: NotRequired[List[LineItemTypedDict]]
    r"""The line items associated with this purchase order"""
    field_mappings: NotRequired[
        Nullable[UnifiedAccountingPurchaseorderInputFieldMappingsTypedDict]
    ]
    r"""The custom field mappings of the object between the remote 3rd party & Panora"""


class UnifiedAccountingPurchaseorderInput(BaseModel):
    status: OptionalNullable[str] = UNSET
    r"""The status of the purchase order"""

    issue_date: OptionalNullable[datetime] = UNSET
    r"""The issue date of the purchase order"""

    purchase_order_number: OptionalNullable[str] = UNSET
    r"""The purchase order number"""

    delivery_date: OptionalNullable[datetime] = UNSET
    r"""The delivery date for the purchase order"""

    delivery_address: OptionalNullable[str] = UNSET
    r"""The UUID of the delivery address"""

    customer: OptionalNullable[str] = UNSET
    r"""The UUID of the customer"""

    vendor: OptionalNullable[str] = UNSET
    r"""The UUID of the vendor"""

    memo: OptionalNullable[str] = UNSET
    r"""A memo or note for the purchase order"""

    company_id: OptionalNullable[str] = UNSET
    r"""The UUID of the company"""

    total_amount: OptionalNullable[float] = UNSET
    r"""The total amount of the purchase order in cents"""

    currency: OptionalNullable[str] = UNSET
    r"""The currency of the purchase order"""

    exchange_rate: OptionalNullable[str] = UNSET
    r"""The exchange rate applied to the purchase order"""

    tracking_categories: OptionalNullable[List[str]] = UNSET
    r"""The UUIDs of the tracking categories associated with the purchase order"""

    accounting_period_id: OptionalNullable[str] = UNSET
    r"""The UUID of the associated accounting period"""

    line_items: Optional[List[LineItem]] = None
    r"""The line items associated with this purchase order"""

    field_mappings: OptionalNullable[
        UnifiedAccountingPurchaseorderInputFieldMappings
    ] = UNSET
    r"""The custom field mappings of the object between the remote 3rd party & Panora"""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = [
            "status",
            "issue_date",
            "purchase_order_number",
            "delivery_date",
            "delivery_address",
            "customer",
            "vendor",
            "memo",
            "company_id",
            "total_amount",
            "currency",
            "exchange_rate",
            "tracking_categories",
            "accounting_period_id",
            "line_items",
            "field_mappings",
        ]
        nullable_fields = [
            "status",
            "issue_date",
            "purchase_order_number",
            "delivery_date",
            "delivery_address",
            "customer",
            "vendor",
            "memo",
            "company_id",
            "total_amount",
            "currency",
            "exchange_rate",
            "tracking_categories",
            "accounting_period_id",
            "field_mappings",
        ]
        null_default_fields = []

        serialized = handler(self)

        m = {}

        for n, f in self.model_fields.items():
            k = f.alias or n
            val = serialized.get(k)
            serialized.pop(k, None)

            optional_nullable = k in optional_fields and k in nullable_fields
            is_set = (
                self.__pydantic_fields_set__.intersection({n})
                or k in null_default_fields
            )  # pylint: disable=no-member

            if val is not None and val != UNSET_SENTINEL:
                m[k] = val
            elif val != UNSET_SENTINEL and (
                not k in optional_fields or (optional_nullable and is_set)
            ):
                m[k] = val

        return m
