# coding: utf-8

"""
    (v2.0) Dynamics 365 Business Central

    (v2.0) Business Central Standard APIs

    The version of the OpenAPI document: 2.0.0
    Generated by OpenAPI Generator (https://openapi-generator.tech)

    Do not edit the class manually.
"""  # noqa: E501


from __future__ import annotations
import pprint
import re  # noqa: F401
import json

from datetime import datetime
from pydantic import BaseModel, ConfigDict, Field, StrictFloat, StrictInt, StrictStr
from typing import Any, ClassVar, Dict, List, Optional, Union
from typing_extensions import Annotated
from pybusinesscentral.model.account import Account
from pybusinesscentral.model.general_ledger_entry_dimensions_inner import GeneralLedgerEntryDimensionsInner
from typing import Optional, Set
from typing_extensions import Self

class GeneralLedgerEntry(BaseModel):
    """
    GeneralLedgerEntry
    """ # noqa: E501
    id: Optional[StrictInt] = Field(default=None, description="(v1.0) The id property for the Dynamics 365 Business Central generalLedgerEntry entity")
    posting_date: Optional[datetime] = Field(default=None, description="(v1.0) The postingDate property for the Dynamics 365 Business Central generalLedgerEntry entity", alias="postingDate")
    document_number: Optional[Annotated[str, Field(strict=True, max_length=20)]] = Field(default=None, description="(v1.0) The documentNumber property for the Dynamics 365 Business Central generalLedgerEntry entity", alias="documentNumber")
    document_type: Optional[StrictStr] = Field(default=None, description="(v1.0) The documentType property for the Dynamics 365 Business Central generalLedgerEntry entity", alias="documentType")
    account_id: Optional[StrictStr] = Field(default=None, description="(v1.0) The accountId property for the Dynamics 365 Business Central generalLedgerEntry entity", alias="accountId")
    account_number: Optional[Annotated[str, Field(strict=True, max_length=20)]] = Field(default=None, description="(v1.0) The accountNumber property for the Dynamics 365 Business Central generalLedgerEntry entity", alias="accountNumber")
    description: Optional[Annotated[str, Field(strict=True, max_length=100)]] = Field(default=None, description="(v1.0) The description property for the Dynamics 365 Business Central generalLedgerEntry entity")
    debit_amount: Optional[Union[StrictFloat, StrictInt]] = Field(default=None, description="(v1.0) The debitAmount property for the Dynamics 365 Business Central generalLedgerEntry entity", alias="debitAmount")
    credit_amount: Optional[Union[StrictFloat, StrictInt]] = Field(default=None, description="(v1.0) The creditAmount property for the Dynamics 365 Business Central generalLedgerEntry entity", alias="creditAmount")
    dimensions: Optional[List[Optional[GeneralLedgerEntryDimensionsInner]]] = None
    last_modified_date_time: Optional[datetime] = Field(default=None, description="(v1.0) The lastModifiedDateTime property for the Dynamics 365 Business Central generalLedgerEntry entity", alias="lastModifiedDateTime")
    account: Optional[Account] = None
    __properties: ClassVar[List[str]] = ["id", "postingDate", "documentNumber", "documentType", "accountId", "accountNumber", "description", "debitAmount", "creditAmount", "dimensions", "lastModifiedDateTime", "account"]

    model_config = ConfigDict(
        populate_by_name=True,
        validate_assignment=True,
        protected_namespaces=(),
    )


    def to_str(self) -> str:
        """Returns the string representation of the model using alias"""
        return pprint.pformat(self.model_dump(by_alias=True))

    def to_json(self) -> str:
        """Returns the JSON representation of the model using alias"""
        # TODO: pydantic v2: use .model_dump_json(by_alias=True, exclude_unset=True) instead
        return json.dumps(self.to_dict())

    @classmethod
    def from_json(cls, json_str: str) -> Optional[Self]:
        """Create an instance of GeneralLedgerEntry from a JSON string"""
        return cls.from_dict(json.loads(json_str))

    def to_dict(self) -> Dict[str, Any]:
        """Return the dictionary representation of the model using alias.

        This has the following differences from calling pydantic's
        `self.model_dump(by_alias=True)`:

        * `None` is only added to the output dict for nullable fields that
          were set at model initialization. Other fields with value `None`
          are ignored.
        """
        excluded_fields: Set[str] = set([
        ])

        _dict = self.model_dump(
            by_alias=True,
            exclude=excluded_fields,
            exclude_none=True,
        )
        # override the default output from pydantic by calling `to_dict()` of each item in dimensions (list)
        _items = []
        if self.dimensions:
            for _item_dimensions in self.dimensions:
                if _item_dimensions:
                    _items.append(_item_dimensions.to_dict())
            _dict['dimensions'] = _items
        # override the default output from pydantic by calling `to_dict()` of account
        if self.account:
            _dict['account'] = self.account.to_dict()
        # set to None if posting_date (nullable) is None
        # and model_fields_set contains the field
        if self.posting_date is None and "posting_date" in self.model_fields_set:
            _dict['postingDate'] = None

        # set to None if document_number (nullable) is None
        # and model_fields_set contains the field
        if self.document_number is None and "document_number" in self.model_fields_set:
            _dict['documentNumber'] = None

        # set to None if document_type (nullable) is None
        # and model_fields_set contains the field
        if self.document_type is None and "document_type" in self.model_fields_set:
            _dict['documentType'] = None

        # set to None if account_id (nullable) is None
        # and model_fields_set contains the field
        if self.account_id is None and "account_id" in self.model_fields_set:
            _dict['accountId'] = None

        # set to None if account_number (nullable) is None
        # and model_fields_set contains the field
        if self.account_number is None and "account_number" in self.model_fields_set:
            _dict['accountNumber'] = None

        # set to None if description (nullable) is None
        # and model_fields_set contains the field
        if self.description is None and "description" in self.model_fields_set:
            _dict['description'] = None

        # set to None if debit_amount (nullable) is None
        # and model_fields_set contains the field
        if self.debit_amount is None and "debit_amount" in self.model_fields_set:
            _dict['debitAmount'] = None

        # set to None if credit_amount (nullable) is None
        # and model_fields_set contains the field
        if self.credit_amount is None and "credit_amount" in self.model_fields_set:
            _dict['creditAmount'] = None

        # set to None if last_modified_date_time (nullable) is None
        # and model_fields_set contains the field
        if self.last_modified_date_time is None and "last_modified_date_time" in self.model_fields_set:
            _dict['lastModifiedDateTime'] = None

        # set to None if account (nullable) is None
        # and model_fields_set contains the field
        if self.account is None and "account" in self.model_fields_set:
            _dict['account'] = None

        return _dict

    @classmethod
    def from_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional[Self]:
        """Create an instance of GeneralLedgerEntry from a dict"""
        if obj is None:
            return None

        if not isinstance(obj, dict):
            return cls.model_validate(obj)

        _obj = cls.model_validate({
            "id": obj.get("id"),
            "postingDate": obj.get("postingDate"),
            "documentNumber": obj.get("documentNumber"),
            "documentType": obj.get("documentType"),
            "accountId": obj.get("accountId"),
            "accountNumber": obj.get("accountNumber"),
            "description": obj.get("description"),
            "debitAmount": obj.get("debitAmount"),
            "creditAmount": obj.get("creditAmount"),
            "dimensions": [GeneralLedgerEntryDimensionsInner.from_dict(_item) for _item in obj["dimensions"]] if obj.get("dimensions") is not None else None,
            "lastModifiedDateTime": obj.get("lastModifiedDateTime"),
            "account": Account.from_dict(obj["account"]) if obj.get("account") is not None else None
        })
        return _obj


