# This file was auto-generated by Fern from our API Definition.

import typing

from ..core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ..core.request_options import RequestOptions
from ..types.receipt_bulk_upsert_async_response import ReceiptBulkUpsertAsyncResponse
from ..types.receipt_create_response import ReceiptCreateResponse
from ..types.receipt_detail import ReceiptDetail
from ..types.receipt_update_response import ReceiptUpdateResponse
from .raw_client import AsyncRawReceiptsClient, RawReceiptsClient
from .types.get_receipts_root_response import GetReceiptsRootResponse
from .types.patch_receipts_id_request_items_item import PatchReceiptsIdRequestItemsItem
from .types.receipt_bulk_upsert_request_receipts_item import ReceiptBulkUpsertRequestReceiptsItem
from .types.receipt_create_request_items_item import ReceiptCreateRequestItemsItem

# this is used as the default value for optional parameters
OMIT = typing.cast(typing.Any, ...)


class ReceiptsClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._raw_client = RawReceiptsClient(client_wrapper=client_wrapper)

    @property
    def with_raw_response(self) -> RawReceiptsClient:
        """
        Retrieves a raw implementation of this client that returns raw responses.

        Returns
        -------
        RawReceiptsClient
        """
        return self._raw_client

    def get_receipts_root(
        self,
        *,
        ottimate_company_id: typing.Optional[int] = None,
        ottimate_location_id: typing.Optional[int] = None,
        ottimate_po_id: typing.Optional[str] = None,
        status: typing.Optional[str] = None,
        date_from: typing.Optional[str] = None,
        date_to: typing.Optional[str] = None,
        expand: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> GetReceiptsRootResponse:
        """
        Returns a paginated list of receipts, filterable by company, location, purchase order, status, and date range.

        Parameters
        ----------
        ottimate_company_id : typing.Optional[int]
            Filter by company ID

        ottimate_location_id : typing.Optional[int]
            Filter by location ID

        ottimate_po_id : typing.Optional[str]
            Filter by purchase order ID

        status : typing.Optional[str]
            Filter by receipt status

        date_from : typing.Optional[str]
            Filter by start date (YYYY-MM-DD)

        date_to : typing.Optional[str]
            Filter by end date (YYYY-MM-DD)

        expand : typing.Optional[str]
            Comma-separated list of fields to expand. Supported values: items. When expand=items is used, date_from and date_to must both be provided and within a 30-day window.

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        GetReceiptsRootResponse
            Successful response with paginated receipt list

        Examples
        --------
        from ottimate import Ottimate

        client = Ottimate(
            api_key="YOUR_API_KEY",
            api_version="YOUR_API_VERSION",
            api_key="YOUR_API_KEY",
        )
        client.receipts.get_receipts_root(
            ottimate_company_id=15196,
            ottimate_location_id=12345,
            expand="items",
        )
        """
        _response = self._raw_client.get_receipts_root(
            ottimate_company_id=ottimate_company_id,
            ottimate_location_id=ottimate_location_id,
            ottimate_po_id=ottimate_po_id,
            status=status,
            date_from=date_from,
            date_to=date_to,
            expand=expand,
            request_options=request_options,
        )
        return _response.data

    def post_receipts_root(
        self,
        *,
        receipt_number: str,
        date: str,
        total_amount: float,
        items: typing.Sequence[ReceiptCreateRequestItemsItem],
        ottimate_po_id: typing.Optional[str] = OMIT,
        ottimate_location_id: typing.Optional[int] = OMIT,
        erp_vendor_id: typing.Optional[str] = OMIT,
        po_number: typing.Optional[str] = OMIT,
        po_external_id: typing.Optional[str] = OMIT,
        external_id: typing.Optional[str] = OMIT,
        invoice_number_ref: typing.Optional[str] = OMIT,
        note: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ReceiptCreateResponse:
        """
        Creates a new receipt linked to an existing purchase order. The `receipt_number` field is required and must be provided by the client

        Parameters
        ----------
        receipt_number : str

        date : str

        total_amount : float

        items : typing.Sequence[ReceiptCreateRequestItemsItem]

        ottimate_po_id : typing.Optional[str]
            Ottimate-assigned purchase order ID. Required unless using PO lookup fields (erp_vendor_id + ottimate_location_id + po_number/po_external_id).

        ottimate_location_id : typing.Optional[int]
            Location ID for PO lookup. Required when using lookup fields instead of ottimate_po_id.

        erp_vendor_id : typing.Optional[str]
            ERP vendor ID for PO lookup. Required when using lookup fields instead of ottimate_po_id.

        po_number : typing.Optional[str]
            PO number for lookup. Use with erp_vendor_id + ottimate_location_id when ottimate_po_id is not provided.

        po_external_id : typing.Optional[str]
            External ID (dd_unique_key) of the PO for lookup. Alternative to po_number.

        external_id : typing.Optional[str]
            External unique identifier for this receipt.

        invoice_number_ref : typing.Optional[str]
            Invoice reference number

        note : typing.Optional[str]

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        ReceiptCreateResponse
            Successfully created

        Examples
        --------
        from ottimate import Ottimate
        from ottimate.receipts import ReceiptCreateRequestItemsItem

        client = Ottimate(
            api_key="YOUR_API_KEY",
            api_version="YOUR_API_VERSION",
            api_key="YOUR_API_KEY",
        )
        client.receipts.post_receipts_root(
            ottimate_po_id="poi_UFo9WZegaaEU",
            external_id="ERP-RCP-001",
            receipt_number="RCP-2024-001",
            date="2024-01-16",
            total_amount=1450.0,
            items=[
                ReceiptCreateRequestItemsItem(
                    ottimate_po_item_id="660e8400-e29b-41d4-a716-446655440001",
                    quantity=95.0,
                    price=2.5,
                )
            ],
        )
        """
        _response = self._raw_client.post_receipts_root(
            receipt_number=receipt_number,
            date=date,
            total_amount=total_amount,
            items=items,
            ottimate_po_id=ottimate_po_id,
            ottimate_location_id=ottimate_location_id,
            erp_vendor_id=erp_vendor_id,
            po_number=po_number,
            po_external_id=po_external_id,
            external_id=external_id,
            invoice_number_ref=invoice_number_ref,
            note=note,
            request_options=request_options,
        )
        return _response.data

    def get_receipts_id(self, id: str, *, request_options: typing.Optional[RequestOptions] = None) -> ReceiptDetail:
        """
        Returns detailed information for a single receipt including all line items.

        Parameters
        ----------
        id : str
            The unique ID of the receipt

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        ReceiptDetail
            Successful response with receipt details

        Examples
        --------
        from ottimate import Ottimate

        client = Ottimate(
            api_key="YOUR_API_KEY",
            api_version="YOUR_API_VERSION",
            api_key="YOUR_API_KEY",
        )
        client.receipts.get_receipts_id(
            id="racc_90832490382904802384",
        )
        """
        _response = self._raw_client.get_receipts_id(id, request_options=request_options)
        return _response.data

    def patch_receipts_id(
        self,
        id: str,
        *,
        receipt_number: typing.Optional[str] = OMIT,
        date: typing.Optional[str] = OMIT,
        total_amount: typing.Optional[float] = OMIT,
        invoice_number_ref: typing.Optional[str] = OMIT,
        note: typing.Optional[str] = OMIT,
        items: typing.Optional[typing.Sequence[PatchReceiptsIdRequestItemsItem]] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ReceiptUpdateResponse:
        """
        Updates a receipt. Note: `status`, `receipt_number`, `external_id`, and `ottimate_po_id` cannot be changed.

        Parameters
        ----------
        id : str
            The unique ID of the receipt

        receipt_number : typing.Optional[str]

        date : typing.Optional[str]

        total_amount : typing.Optional[float]

        invoice_number_ref : typing.Optional[str]

        note : typing.Optional[str]

        items : typing.Optional[typing.Sequence[PatchReceiptsIdRequestItemsItem]]
            Items with `id` update the existing item. Items without `id` create a new item. When creating, `ottimate_po_item_id` and `quantity` are required.

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        ReceiptUpdateResponse
            Successfully updated

        Examples
        --------
        from ottimate import Ottimate

        client = Ottimate(
            api_key="YOUR_API_KEY",
            api_version="YOUR_API_VERSION",
            api_key="YOUR_API_KEY",
        )
        client.receipts.patch_receipts_id(
            id="racc_90832490382904802384",
            total_amount=1400.0,
            note="Updated quantity on delivery",
        )
        """
        _response = self._raw_client.patch_receipts_id(
            id,
            receipt_number=receipt_number,
            date=date,
            total_amount=total_amount,
            invoice_number_ref=invoice_number_ref,
            note=note,
            items=items,
            request_options=request_options,
        )
        return _response.data

    def post_receipts_bulk(
        self,
        *,
        ottimate_company_id: int,
        receipts: typing.Sequence[ReceiptBulkUpsertRequestReceiptsItem],
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ReceiptBulkUpsertAsyncResponse:
        """
        Creates or updates multiple receipts in a single request. Maximum 100 receipts and 1000 total line items per request. Processing is always asynchronous via Celery and returns a batch tracking ID for polling progress.

        Parameters
        ----------
        ottimate_company_id : int
            Company ID for all receipts in this batch

        receipts : typing.Sequence[ReceiptBulkUpsertRequestReceiptsItem]
            Max 100 receipts with max 1000 total items

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        ReceiptBulkUpsertAsyncResponse
            Asynchronous processing started. Poll batch_url for progress.

        Examples
        --------
        from ottimate import Ottimate
        from ottimate.receipts import (
            ReceiptBulkUpsertRequestReceiptsItem,
            ReceiptBulkUpsertRequestReceiptsItemItemsItem,
        )

        client = Ottimate(
            api_key="YOUR_API_KEY",
            api_version="YOUR_API_VERSION",
            api_key="YOUR_API_KEY",
        )
        client.receipts.post_receipts_bulk(
            ottimate_company_id=15196,
            receipts=[
                ReceiptBulkUpsertRequestReceiptsItem(
                    ottimate_po_id="poi_UFo9WZegaaEU",
                    external_id="ERP-RCP-001",
                    receipt_number="RCP-2024-001",
                    date="2024-01-16",
                    total_amount=1450.0,
                    items=[
                        ReceiptBulkUpsertRequestReceiptsItemItemsItem(
                            ottimate_po_item_id="660e8400-e29b-41d4-a716-446655440001",
                            quantity=95.0,
                            price=2.5,
                        )
                    ],
                )
            ],
        )
        """
        _response = self._raw_client.post_receipts_bulk(
            ottimate_company_id=ottimate_company_id, receipts=receipts, request_options=request_options
        )
        return _response.data


class AsyncReceiptsClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawReceiptsClient(client_wrapper=client_wrapper)

    @property
    def with_raw_response(self) -> AsyncRawReceiptsClient:
        """
        Retrieves a raw implementation of this client that returns raw responses.

        Returns
        -------
        AsyncRawReceiptsClient
        """
        return self._raw_client

    async def get_receipts_root(
        self,
        *,
        ottimate_company_id: typing.Optional[int] = None,
        ottimate_location_id: typing.Optional[int] = None,
        ottimate_po_id: typing.Optional[str] = None,
        status: typing.Optional[str] = None,
        date_from: typing.Optional[str] = None,
        date_to: typing.Optional[str] = None,
        expand: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> GetReceiptsRootResponse:
        """
        Returns a paginated list of receipts, filterable by company, location, purchase order, status, and date range.

        Parameters
        ----------
        ottimate_company_id : typing.Optional[int]
            Filter by company ID

        ottimate_location_id : typing.Optional[int]
            Filter by location ID

        ottimate_po_id : typing.Optional[str]
            Filter by purchase order ID

        status : typing.Optional[str]
            Filter by receipt status

        date_from : typing.Optional[str]
            Filter by start date (YYYY-MM-DD)

        date_to : typing.Optional[str]
            Filter by end date (YYYY-MM-DD)

        expand : typing.Optional[str]
            Comma-separated list of fields to expand. Supported values: items. When expand=items is used, date_from and date_to must both be provided and within a 30-day window.

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        GetReceiptsRootResponse
            Successful response with paginated receipt list

        Examples
        --------
        import asyncio

        from ottimate import AsyncOttimate

        client = AsyncOttimate(
            api_key="YOUR_API_KEY",
            api_version="YOUR_API_VERSION",
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.receipts.get_receipts_root(
                ottimate_company_id=15196,
                ottimate_location_id=12345,
                expand="items",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.get_receipts_root(
            ottimate_company_id=ottimate_company_id,
            ottimate_location_id=ottimate_location_id,
            ottimate_po_id=ottimate_po_id,
            status=status,
            date_from=date_from,
            date_to=date_to,
            expand=expand,
            request_options=request_options,
        )
        return _response.data

    async def post_receipts_root(
        self,
        *,
        receipt_number: str,
        date: str,
        total_amount: float,
        items: typing.Sequence[ReceiptCreateRequestItemsItem],
        ottimate_po_id: typing.Optional[str] = OMIT,
        ottimate_location_id: typing.Optional[int] = OMIT,
        erp_vendor_id: typing.Optional[str] = OMIT,
        po_number: typing.Optional[str] = OMIT,
        po_external_id: typing.Optional[str] = OMIT,
        external_id: typing.Optional[str] = OMIT,
        invoice_number_ref: typing.Optional[str] = OMIT,
        note: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ReceiptCreateResponse:
        """
        Creates a new receipt linked to an existing purchase order. The `receipt_number` field is required and must be provided by the client

        Parameters
        ----------
        receipt_number : str

        date : str

        total_amount : float

        items : typing.Sequence[ReceiptCreateRequestItemsItem]

        ottimate_po_id : typing.Optional[str]
            Ottimate-assigned purchase order ID. Required unless using PO lookup fields (erp_vendor_id + ottimate_location_id + po_number/po_external_id).

        ottimate_location_id : typing.Optional[int]
            Location ID for PO lookup. Required when using lookup fields instead of ottimate_po_id.

        erp_vendor_id : typing.Optional[str]
            ERP vendor ID for PO lookup. Required when using lookup fields instead of ottimate_po_id.

        po_number : typing.Optional[str]
            PO number for lookup. Use with erp_vendor_id + ottimate_location_id when ottimate_po_id is not provided.

        po_external_id : typing.Optional[str]
            External ID (dd_unique_key) of the PO for lookup. Alternative to po_number.

        external_id : typing.Optional[str]
            External unique identifier for this receipt.

        invoice_number_ref : typing.Optional[str]
            Invoice reference number

        note : typing.Optional[str]

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        ReceiptCreateResponse
            Successfully created

        Examples
        --------
        import asyncio

        from ottimate import AsyncOttimate
        from ottimate.receipts import ReceiptCreateRequestItemsItem

        client = AsyncOttimate(
            api_key="YOUR_API_KEY",
            api_version="YOUR_API_VERSION",
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.receipts.post_receipts_root(
                ottimate_po_id="poi_UFo9WZegaaEU",
                external_id="ERP-RCP-001",
                receipt_number="RCP-2024-001",
                date="2024-01-16",
                total_amount=1450.0,
                items=[
                    ReceiptCreateRequestItemsItem(
                        ottimate_po_item_id="660e8400-e29b-41d4-a716-446655440001",
                        quantity=95.0,
                        price=2.5,
                    )
                ],
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.post_receipts_root(
            receipt_number=receipt_number,
            date=date,
            total_amount=total_amount,
            items=items,
            ottimate_po_id=ottimate_po_id,
            ottimate_location_id=ottimate_location_id,
            erp_vendor_id=erp_vendor_id,
            po_number=po_number,
            po_external_id=po_external_id,
            external_id=external_id,
            invoice_number_ref=invoice_number_ref,
            note=note,
            request_options=request_options,
        )
        return _response.data

    async def get_receipts_id(
        self, id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> ReceiptDetail:
        """
        Returns detailed information for a single receipt including all line items.

        Parameters
        ----------
        id : str
            The unique ID of the receipt

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        ReceiptDetail
            Successful response with receipt details

        Examples
        --------
        import asyncio

        from ottimate import AsyncOttimate

        client = AsyncOttimate(
            api_key="YOUR_API_KEY",
            api_version="YOUR_API_VERSION",
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.receipts.get_receipts_id(
                id="racc_90832490382904802384",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.get_receipts_id(id, request_options=request_options)
        return _response.data

    async def patch_receipts_id(
        self,
        id: str,
        *,
        receipt_number: typing.Optional[str] = OMIT,
        date: typing.Optional[str] = OMIT,
        total_amount: typing.Optional[float] = OMIT,
        invoice_number_ref: typing.Optional[str] = OMIT,
        note: typing.Optional[str] = OMIT,
        items: typing.Optional[typing.Sequence[PatchReceiptsIdRequestItemsItem]] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ReceiptUpdateResponse:
        """
        Updates a receipt. Note: `status`, `receipt_number`, `external_id`, and `ottimate_po_id` cannot be changed.

        Parameters
        ----------
        id : str
            The unique ID of the receipt

        receipt_number : typing.Optional[str]

        date : typing.Optional[str]

        total_amount : typing.Optional[float]

        invoice_number_ref : typing.Optional[str]

        note : typing.Optional[str]

        items : typing.Optional[typing.Sequence[PatchReceiptsIdRequestItemsItem]]
            Items with `id` update the existing item. Items without `id` create a new item. When creating, `ottimate_po_item_id` and `quantity` are required.

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        ReceiptUpdateResponse
            Successfully updated

        Examples
        --------
        import asyncio

        from ottimate import AsyncOttimate

        client = AsyncOttimate(
            api_key="YOUR_API_KEY",
            api_version="YOUR_API_VERSION",
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.receipts.patch_receipts_id(
                id="racc_90832490382904802384",
                total_amount=1400.0,
                note="Updated quantity on delivery",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.patch_receipts_id(
            id,
            receipt_number=receipt_number,
            date=date,
            total_amount=total_amount,
            invoice_number_ref=invoice_number_ref,
            note=note,
            items=items,
            request_options=request_options,
        )
        return _response.data

    async def post_receipts_bulk(
        self,
        *,
        ottimate_company_id: int,
        receipts: typing.Sequence[ReceiptBulkUpsertRequestReceiptsItem],
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ReceiptBulkUpsertAsyncResponse:
        """
        Creates or updates multiple receipts in a single request. Maximum 100 receipts and 1000 total line items per request. Processing is always asynchronous via Celery and returns a batch tracking ID for polling progress.

        Parameters
        ----------
        ottimate_company_id : int
            Company ID for all receipts in this batch

        receipts : typing.Sequence[ReceiptBulkUpsertRequestReceiptsItem]
            Max 100 receipts with max 1000 total items

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        ReceiptBulkUpsertAsyncResponse
            Asynchronous processing started. Poll batch_url for progress.

        Examples
        --------
        import asyncio

        from ottimate import AsyncOttimate
        from ottimate.receipts import (
            ReceiptBulkUpsertRequestReceiptsItem,
            ReceiptBulkUpsertRequestReceiptsItemItemsItem,
        )

        client = AsyncOttimate(
            api_key="YOUR_API_KEY",
            api_version="YOUR_API_VERSION",
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.receipts.post_receipts_bulk(
                ottimate_company_id=15196,
                receipts=[
                    ReceiptBulkUpsertRequestReceiptsItem(
                        ottimate_po_id="poi_UFo9WZegaaEU",
                        external_id="ERP-RCP-001",
                        receipt_number="RCP-2024-001",
                        date="2024-01-16",
                        total_amount=1450.0,
                        items=[
                            ReceiptBulkUpsertRequestReceiptsItemItemsItem(
                                ottimate_po_item_id="660e8400-e29b-41d4-a716-446655440001",
                                quantity=95.0,
                                price=2.5,
                            )
                        ],
                    )
                ],
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.post_receipts_bulk(
            ottimate_company_id=ottimate_company_id, receipts=receipts, request_options=request_options
        )
        return _response.data
