# coding: utf-8

"""
    Zuora API Reference

    REST API reference for the Zuora Billing, Payments, and Central Platform! Check out the [REST API Overview](https://www.zuora.com/developer/api-references/api/overview/).

    The version of the OpenAPI document: 2024-05-20
    Contact: docs@zuora.com
    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 date
from pydantic import BaseModel, ConfigDict, Field, StrictBool, StrictFloat, StrictInt, StrictStr
from typing import Any, ClassVar, Dict, List, Optional, Union
from typing_extensions import Annotated
from zuora_sdk.models.billing_document_status import BillingDocumentStatus
from zuora_sdk.models.e_invoice_status import EInvoiceStatus
from zuora_sdk.models.memo_revenue_impacting import MemoRevenueImpacting
from zuora_sdk.models.memo_source_type import MemoSourceType
from zuora_sdk.models.tax_status import TaxStatus
from zuora_sdk.models.transferred_to_accounting_status import TransferredToAccountingStatus
from typing import Optional, Set
from typing_extensions import Self

class CreditMemo(BaseModel):
    """
    CreditMemo
    """ # noqa: E501
    # avoid validation
    def __init__(self, **kwargs):
        # Directly assign values without validation using `construct()`
        model = self.construct(**kwargs)
        self.__dict__.update(model.__dict__)

        _dict_all = convert_snake_dict_to_camel(kwargs, self.model_fields, self.__properties)

        _dict = self.to_alias_dict(_dict_all)
        for field_name, field_info in self.model_fields.items():
            alias = field_info.alias or field_name
            if alias in _dict:
                self.__dict__[field_name] = _dict.get(alias)
        self.update_additional_properties(self, _dict_all)
        pass

    def __setattr__(self, name, value):
        # Override setattr to bypass validation when setting attributes
        object.__setattr__(self, name, value)

    integration_id__ns: Optional[Annotated[str, Field(strict=True, max_length=255)]] = Field(default=None, description="ID of the corresponding object in NetSuite. Only available if you have installed the [Zuora Connector for NetSuite](https://www.zuora.com/connect/app/?appId=265). ", alias="IntegrationId__NS")
    integration_status__ns: Optional[Annotated[str, Field(strict=True, max_length=255)]] = Field(default=None, description="Status of the credit memo's synchronization with NetSuite. Only available if you have installed the [Zuora Connector for NetSuite](https://www.zuora.com/connect/app/?appId=265). ", alias="IntegrationStatus__NS")
    origin__ns: Optional[Annotated[str, Field(strict=True, max_length=255)]] = Field(default=None, description="Origin of the corresponding object in NetSuite. Only available if you have installed the [Zuora Connector for NetSuite](https://www.zuora.com/connect/app/?appId=265). ", alias="Origin__NS")
    sync_date__ns: Optional[Annotated[str, Field(strict=True, max_length=255)]] = Field(default=None, description="Date when the credit memo was synchronized with NetSuite. Only available if you have installed the [Zuora Connector for NetSuite](https://www.zuora.com/connect/app/?appId=265). ", alias="SyncDate__NS")
    transaction__ns: Optional[Annotated[str, Field(strict=True, max_length=255)]] = Field(default=None, description="Related transaction in NetSuite. Only available if you have installed the [Zuora Connector for NetSuite](https://www.zuora.com/connect/app/?appId=265). ", alias="Transaction__NS")
    account_id: Optional[StrictStr] = Field(default=None, description="The ID of the customer account associated with the credit memo.", alias="accountId")
    account_number: Optional[StrictStr] = Field(default=None, description="The number of the account associated with the credit memo.", alias="accountNumber")
    amount: Optional[Union[StrictFloat, StrictInt]] = Field(default=None, description="The total amount of the credit memo.")
    applied_amount: Optional[Union[StrictFloat, StrictInt]] = Field(default=None, description="The applied amount of the credit memo.", alias="appliedAmount")
    auto_apply_upon_posting: Optional[StrictBool] = Field(default=None, description="Whether the credit memo automatically applies to the invoice upon posting.", alias="autoApplyUponPosting")
    bill_to_contact_id: Optional[StrictStr] = Field(default=None, description="The ID of the bill-to contact associated with the credit memo. The value of this field is `null` if you have the [Flexible Billing Attributes](https://knowledgecenter.zuora.com/Billing/Subscriptions/Flexible_Billing_Attributes) feature disabled.", alias="billToContactId")
    cancelled_by_id: Optional[StrictStr] = Field(default=None, description="The ID of the Zuora user who cancelled the credit memo.", alias="cancelledById")
    cancelled_on: Optional[StrictStr] = Field(default=None, description="The date and time when the credit memo was cancelled, in `yyyy-mm-dd hh:mm:ss` format.", alias="cancelledOn")
    comment: Optional[StrictStr] = Field(default=None, description="Comments about the credit memo.")
    created_by_id: Optional[StrictStr] = Field(default=None, description="The ID of the Zuora user who created the credit memo.", alias="createdById")
    created_date: Optional[StrictStr] = Field(default=None, description="The date and time when the credit memo was created, in `yyyy-mm-dd hh:mm:ss` format. For example, 2017-03-01 15:31:10.", alias="createdDate")
    credit_memo_date: Optional[date] = Field(default=None, description="The date when the credit memo takes effect, in `yyyy-mm-dd` format. For example, 2017-05-20.", alias="creditMemoDate")
    currency: Optional[StrictStr] = Field(default=None, description="A currency defined in the web-based UI administrative settings.")
    exclude_from_auto_apply_rules: Optional[StrictBool] = Field(default=None, description="Whether the credit memo is excluded from the rule of automatically applying unapplied credit memos to invoices and debit memos during payment runs.", alias="excludeFromAutoApplyRules")
    id: Optional[StrictStr] = Field(default=None, description="The unique ID of the credit memo.")
    invoice_group_number: Optional[Annotated[str, Field(strict=True, max_length=255)]] = Field(default=None, description="The number of invoice group associated with the credit memo.  **Note**: This field is available only if you have the <a href=\"https://knowledgecenter.zuora.com/Zuora_Billing/Bill_your_customers/Bill_customers_at_subscription_level/Flexible_Billing_Attributes\" target=\"_blank\">Flexible Billing Attributes</a> feature enabled. ", alias="invoiceGroupNumber")
    revenue_impacting: Optional[StrictStr] = Field(default=None, alias="revenueImpacting")
    latest_pdf_file_id: Optional[StrictStr] = Field(default=None, description="The ID of the latest PDF file generated for the credit memo. ", alias="latestPDFFileId")
    number: Optional[StrictStr] = Field(default=None, description="The unique identification number of the credit memo. ")
    posted_by_id: Optional[StrictStr] = Field(default=None, description="The ID of the Zuora user who posted the credit memo. ", alias="postedById")
    posted_on: Optional[StrictStr] = Field(default=None, description="The date and time when the credit memo was posted, in `yyyy-mm-dd hh:mm:ss` format.", alias="postedOn")
    reason_code: Optional[StrictStr] = Field(default=None, description="A code identifying the reason for the transaction. The value must be an existing reason code or empty.", alias="reasonCode")
    referred_invoice_id: Optional[StrictStr] = Field(default=None, description="The ID of a referred invoice.", alias="referredInvoiceId")
    refund_amount: Optional[Union[StrictFloat, StrictInt]] = Field(default=None, description="The amount of the refund on the credit memo.", alias="refundAmount")
    reversed: Optional[StrictBool] = Field(default=None, description="Whether the credit memo is reversed.")
    sequence_set_id: Optional[StrictStr] = Field(default=None, description="The ID of the sequence set associated with the credit memo. The value of this field is `null` if you have the [Flexible Billing Attributes](https://knowledgecenter.zuora.com/Billing/Subscriptions/Flexible_Billing_Attributes) feature disabled.", alias="sequenceSetId")
    source: Optional[StrictStr] = Field(default=None, description="The source of the credit memo.  Possible values: - `BillRun`: The credit memo is generated by a bill run. - `API`: The credit memo is created by calling the [Invoice and collect](https://www.zuora.com/developer/api-references/api/operation/Post_TransactionInvoicePayment) operation, or by calling the Orders, Order Line Items, or Fulfillments API operations. - `ApiSubscribe`: The credit memo is created by calling the [Create subscription](https://www.zuora.com/developer/api-references/api/operation/Post_Subscription) and [Create account](https://www.zuora.com/developer/api-references/api/operation/Post_Account) operation. - `ApiAmend`: The credit memo is created by calling the [Update subscription](https://www.zuora.com/developer/api-references/api/operation/Put_Subscription) operation. - `AdhocFromPrpc`: The credit memo is created from a product rate plan charge through the Zuora UI or by calling the [Create a credit memo from a charge](https://www.zuora.com/developer/api-references/api/operation/Post_CreditMemoFromPrpc) operation. - `AdhocFromInvoice`: The credit memo is created from an invoice or created by reversing an invoice. You can create a credit memo from an invoice through the Zuora UI or by calling the [Create credit memo from invoice](https://www.zuora.com/developer/api-references/api/operation/Post_CreditMemoFromInvoice) operation. You can create a credit memo by reversing an invoice through the Zuora UI or by calling the [Reverse invoice](https://www.zuora.com/developer/api-references/api/operation/Put_ReverseInvoice) operation.")
    source_id: Optional[StrictStr] = Field(default=None, description="The ID of the credit memo source.   If a credit memo is generated from a bill run, the value is the number of the corresponding bill run. Otherwise, the value is `null`. ", alias="sourceId")
    source_type: Optional[StrictStr] = Field(default=None, alias="sourceType")
    status: Optional[StrictStr] = None
    target_date: Optional[date] = Field(default=None, description="The target date for the credit memo, in `yyyy-mm-dd` format. For example, 2017-07-20. ", alias="targetDate")
    tax_amount: Optional[Union[StrictFloat, StrictInt]] = Field(default=None, description="The amount of taxation.", alias="taxAmount")
    tax_message: Optional[StrictStr] = Field(default=None, description="The message about the status of tax calculation related to the credit memo. If tax calculation fails in one credit memo, this field displays the reason for the failure.", alias="taxMessage")
    tax_status: Optional[StrictStr] = Field(default=None, alias="taxStatus")
    total_tax_exempt_amount: Optional[Union[StrictFloat, StrictInt]] = Field(default=None, description="The calculated tax amount excluded due to the exemption.", alias="totalTaxExemptAmount")
    transferred_to_accounting: Optional[StrictStr] = Field(default=None, alias="transferredToAccounting")
    tax_auto_calculation: Optional[StrictBool] = Field(default=None, description="Whether to automatically calculate taxes in the credit memo.", alias="taxAutoCalculation")
    unapplied_amount: Optional[Union[StrictFloat, StrictInt]] = Field(default=None, description="The unapplied amount of the credit memo.", alias="unappliedAmount")
    updated_by_id: Optional[StrictStr] = Field(default=None, description="The ID of the Zuora user who last updated the credit memo.", alias="updatedById")
    updated_date: Optional[StrictStr] = Field(default=None, description="The date and time when the credit memo was last updated, in `yyyy-mm-dd hh:mm:ss` format. For example, 2017-03-02 15:36:10.", alias="updatedDate")
    e_invoice_status: Optional[StrictStr] = Field(default=None, alias="eInvoiceStatus")
    e_invoice_error_code: Optional[StrictStr] = Field(default=None, description="eInvoiceErrorCode. ", alias="eInvoiceErrorCode")
    e_invoice_error_message: Optional[StrictStr] = Field(default=None, description="eInvoiceErrorMessage. ", alias="eInvoiceErrorMessage")
    e_invoice_file_id: Optional[StrictStr] = Field(default=None, description="eInvoiceFileId. ", alias="eInvoiceFileId")
    bill_to_contact_snapshot_id: Optional[StrictStr] = Field(default=None, description="billToContactSnapshotId. ", alias="billToContactSnapshotId")
    organization_label: Optional[StrictStr] = Field(default=None, description="organization label. ", alias="organizationLabel")
    additional_properties: Dict[str, Any] = {}
    __properties: ClassVar[List[str]] = ["IntegrationId__NS", "IntegrationStatus__NS", "Origin__NS", "SyncDate__NS", "Transaction__NS", "accountId", "accountNumber", "amount", "appliedAmount", "autoApplyUponPosting", "billToContactId", "cancelledById", "cancelledOn", "comment", "createdById", "createdDate", "creditMemoDate", "currency", "excludeFromAutoApplyRules", "id", "invoiceGroupNumber", "revenueImpacting", "latestPDFFileId", "number", "postedById", "postedOn", "reasonCode", "referredInvoiceId", "refundAmount", "reversed", "sequenceSetId", "source", "sourceId", "sourceType", "status", "targetDate", "taxAmount", "taxMessage", "taxStatus", "totalTaxExemptAmount", "transferredToAccounting", "taxAutoCalculation", "unappliedAmount", "updatedById", "updatedDate", "eInvoiceStatus", "eInvoiceErrorCode", "eInvoiceErrorMessage", "eInvoiceFileId", "billToContactSnapshotId", "organizationLabel"]

    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 CreditMemo 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.
        * Fields in `self.additional_properties` are added to the output dict.
        """
        excluded_fields: Set[str] = set([
            "additional_properties",
        ])

        _dict = self.model_dump(
            by_alias=True,
            exclude=excluded_fields,
            exclude_none=True,
        )
        # puts key-value pairs in additional_properties in the top level
        if self.additional_properties is not None:
            for _key, _value in self.additional_properties.items():
                _dict[_key] = _value

        return _dict

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

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

        obj = convert_snake_dict_to_camel(obj, cls.model_fields, cls.__properties)

        _obj = cls.model_validate(cls.to_alias_dict(obj))
        return cls.update_additional_properties(_obj, obj)

    @classmethod
    def update_additional_properties(cls, obj, _dict: Optional[Dict[str, Any]]):
        # store additional fields in additional_properties

        # store additional fields in additional_properties
        for _key in _dict.keys():
            if _key not in cls.__properties:
                obj.additional_properties[_key] = _dict.get(_key)


        return obj
        pass

    @classmethod
    def to_alias_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional[Dict[str, Any]]:

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

        return {
            "IntegrationId__NS": obj.get("IntegrationId__NS"),
            "IntegrationStatus__NS": obj.get("IntegrationStatus__NS"),
            "Origin__NS": obj.get("Origin__NS"),
            "SyncDate__NS": obj.get("SyncDate__NS"),
            "Transaction__NS": obj.get("Transaction__NS"),
            "accountId": obj.get("accountId"),
            "accountNumber": obj.get("accountNumber"),
            "amount": obj.get("amount"),
            "appliedAmount": obj.get("appliedAmount"),
            "autoApplyUponPosting": obj.get("autoApplyUponPosting"),
            "billToContactId": obj.get("billToContactId"),
            "cancelledById": obj.get("cancelledById"),
            "cancelledOn": obj.get("cancelledOn"),
            "comment": obj.get("comment"),
            "createdById": obj.get("createdById"),
            "createdDate": obj.get("createdDate"),
            "creditMemoDate": obj.get("creditMemoDate"),
            "currency": obj.get("currency"),
            "excludeFromAutoApplyRules": obj.get("excludeFromAutoApplyRules"),
            "id": obj.get("id"),
            "invoiceGroupNumber": obj.get("invoiceGroupNumber"),
            "revenueImpacting": obj.get("revenueImpacting"),
            "latestPDFFileId": obj.get("latestPDFFileId"),
            "number": obj.get("number"),
            "postedById": obj.get("postedById"),
            "postedOn": obj.get("postedOn"),
            "reasonCode": obj.get("reasonCode"),
            "referredInvoiceId": obj.get("referredInvoiceId"),
            "refundAmount": obj.get("refundAmount"),
            "reversed": obj.get("reversed"),
            "sequenceSetId": obj.get("sequenceSetId"),
            "source": obj.get("source"),
            "sourceId": obj.get("sourceId"),
            "sourceType": obj.get("sourceType"),
            "status": obj.get("status"),
            "targetDate": obj.get("targetDate"),
            "taxAmount": obj.get("taxAmount"),
            "taxMessage": obj.get("taxMessage"),
            "taxStatus": obj.get("taxStatus"),
            "totalTaxExemptAmount": obj.get("totalTaxExemptAmount"),
            "transferredToAccounting": obj.get("transferredToAccounting"),
            "taxAutoCalculation": obj.get("taxAutoCalculation"),
            "unappliedAmount": obj.get("unappliedAmount"),
            "updatedById": obj.get("updatedById"),
            "updatedDate": obj.get("updatedDate"),
            "eInvoiceStatus": obj.get("eInvoiceStatus"),
            "eInvoiceErrorCode": obj.get("eInvoiceErrorCode"),
            "eInvoiceErrorMessage": obj.get("eInvoiceErrorMessage"),
            "eInvoiceFileId": obj.get("eInvoiceFileId"),
            "billToContactSnapshotId": obj.get("billToContactSnapshotId"),
            "organizationLabel": obj.get("organizationLabel")
        }
        return _obj


def convert_snake_dict_to_camel(_dict: dict, model_fields, properties):
    if not isinstance(_dict, dict):
        return _dict
    new_dict = {}
    for k, v in _dict.items():
        if k in model_fields:
            # model_fields: key is attribute name like bill_to_contact,
            alias = model_fields.get(k).alias or k
            new_dict[alias] = v
            pass
        else:
            new_key = snake_to_camel(k)
            if properties is not None and isinstance(properties, list) and new_key in properties:
                new_dict[new_key] = v
            else:
                new_dict[k] = v
    return new_dict
    pass


def snake_to_camel(name):
    if name is None or '_' not in name:
        return name
    components = name.split('_')
    return components[0] + ''.join(x.title() for x in components[1:])
