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

import typing
from json.decoder import JSONDecodeError

from ..core.api_error import ApiError
from ..core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ..core.http_response import AsyncHttpResponse, HttpResponse
from ..core.jsonable_encoder import jsonable_encoder
from ..core.pydantic_utilities import parse_obj_as
from ..core.request_options import RequestOptions
from ..core.serialization import convert_and_respect_annotation_metadata
from ..errors.bad_request_error import BadRequestError
from ..errors.not_found_error import NotFoundError
from ..errors.unauthorized_error import UnauthorizedError
from ..types.error_response import ErrorResponse
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 .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 RawReceiptsClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._client_wrapper = client_wrapper

    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,
    ) -> HttpResponse[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
        -------
        HttpResponse[GetReceiptsRootResponse]
            Successful response with paginated receipt list
        """
        _response = self._client_wrapper.httpx_client.request(
            "receipts",
            method="GET",
            params={
                "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,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    GetReceiptsRootResponse,
                    parse_obj_as(
                        type_=GetReceiptsRootResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 400:
                raise BadRequestError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        ErrorResponse,
                        parse_obj_as(
                            type_=ErrorResponse,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 401:
                raise UnauthorizedError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        ErrorResponse,
                        parse_obj_as(
                            type_=ErrorResponse,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    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,
    ) -> HttpResponse[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
        -------
        HttpResponse[ReceiptCreateResponse]
            Successfully created
        """
        _response = self._client_wrapper.httpx_client.request(
            "receipts",
            method="POST",
            json={
                "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,
                "receipt_number": receipt_number,
                "date": date,
                "total_amount": total_amount,
                "invoice_number_ref": invoice_number_ref,
                "note": note,
                "items": convert_and_respect_annotation_metadata(
                    object_=items, annotation=typing.Sequence[ReceiptCreateRequestItemsItem], direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ReceiptCreateResponse,
                    parse_obj_as(
                        type_=ReceiptCreateResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 400:
                raise BadRequestError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        ErrorResponse,
                        parse_obj_as(
                            type_=ErrorResponse,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def get_receipts_id(
        self, id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[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
        -------
        HttpResponse[ReceiptDetail]
            Successful response with receipt details
        """
        _response = self._client_wrapper.httpx_client.request(
            f"receipts/{jsonable_encoder(id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ReceiptDetail,
                    parse_obj_as(
                        type_=ReceiptDetail,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 404:
                raise NotFoundError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        ErrorResponse,
                        parse_obj_as(
                            type_=ErrorResponse,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    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,
    ) -> HttpResponse[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
        -------
        HttpResponse[ReceiptUpdateResponse]
            Successfully updated
        """
        _response = self._client_wrapper.httpx_client.request(
            f"receipts/{jsonable_encoder(id)}",
            method="PATCH",
            json={
                "receipt_number": receipt_number,
                "date": date,
                "total_amount": total_amount,
                "invoice_number_ref": invoice_number_ref,
                "note": note,
                "items": convert_and_respect_annotation_metadata(
                    object_=items, annotation=typing.Sequence[PatchReceiptsIdRequestItemsItem], direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ReceiptUpdateResponse,
                    parse_obj_as(
                        type_=ReceiptUpdateResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 400:
                raise BadRequestError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        ErrorResponse,
                        parse_obj_as(
                            type_=ErrorResponse,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        ErrorResponse,
                        parse_obj_as(
                            type_=ErrorResponse,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def post_receipts_bulk(
        self,
        *,
        ottimate_company_id: int,
        receipts: typing.Sequence[ReceiptBulkUpsertRequestReceiptsItem],
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[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
        -------
        HttpResponse[ReceiptBulkUpsertAsyncResponse]
            Asynchronous processing started. Poll batch_url for progress.
        """
        _response = self._client_wrapper.httpx_client.request(
            "receipts/bulk",
            method="POST",
            json={
                "ottimate_company_id": ottimate_company_id,
                "receipts": convert_and_respect_annotation_metadata(
                    object_=receipts,
                    annotation=typing.Sequence[ReceiptBulkUpsertRequestReceiptsItem],
                    direction="write",
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ReceiptBulkUpsertAsyncResponse,
                    parse_obj_as(
                        type_=ReceiptBulkUpsertAsyncResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 400:
                raise BadRequestError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        ErrorResponse,
                        parse_obj_as(
                            type_=ErrorResponse,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)


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

    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,
    ) -> AsyncHttpResponse[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
        -------
        AsyncHttpResponse[GetReceiptsRootResponse]
            Successful response with paginated receipt list
        """
        _response = await self._client_wrapper.httpx_client.request(
            "receipts",
            method="GET",
            params={
                "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,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    GetReceiptsRootResponse,
                    parse_obj_as(
                        type_=GetReceiptsRootResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 400:
                raise BadRequestError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        ErrorResponse,
                        parse_obj_as(
                            type_=ErrorResponse,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 401:
                raise UnauthorizedError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        ErrorResponse,
                        parse_obj_as(
                            type_=ErrorResponse,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    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,
    ) -> AsyncHttpResponse[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
        -------
        AsyncHttpResponse[ReceiptCreateResponse]
            Successfully created
        """
        _response = await self._client_wrapper.httpx_client.request(
            "receipts",
            method="POST",
            json={
                "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,
                "receipt_number": receipt_number,
                "date": date,
                "total_amount": total_amount,
                "invoice_number_ref": invoice_number_ref,
                "note": note,
                "items": convert_and_respect_annotation_metadata(
                    object_=items, annotation=typing.Sequence[ReceiptCreateRequestItemsItem], direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ReceiptCreateResponse,
                    parse_obj_as(
                        type_=ReceiptCreateResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 400:
                raise BadRequestError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        ErrorResponse,
                        parse_obj_as(
                            type_=ErrorResponse,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def get_receipts_id(
        self, id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[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
        -------
        AsyncHttpResponse[ReceiptDetail]
            Successful response with receipt details
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"receipts/{jsonable_encoder(id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ReceiptDetail,
                    parse_obj_as(
                        type_=ReceiptDetail,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 404:
                raise NotFoundError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        ErrorResponse,
                        parse_obj_as(
                            type_=ErrorResponse,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    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,
    ) -> AsyncHttpResponse[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
        -------
        AsyncHttpResponse[ReceiptUpdateResponse]
            Successfully updated
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"receipts/{jsonable_encoder(id)}",
            method="PATCH",
            json={
                "receipt_number": receipt_number,
                "date": date,
                "total_amount": total_amount,
                "invoice_number_ref": invoice_number_ref,
                "note": note,
                "items": convert_and_respect_annotation_metadata(
                    object_=items, annotation=typing.Sequence[PatchReceiptsIdRequestItemsItem], direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ReceiptUpdateResponse,
                    parse_obj_as(
                        type_=ReceiptUpdateResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 400:
                raise BadRequestError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        ErrorResponse,
                        parse_obj_as(
                            type_=ErrorResponse,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        ErrorResponse,
                        parse_obj_as(
                            type_=ErrorResponse,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def post_receipts_bulk(
        self,
        *,
        ottimate_company_id: int,
        receipts: typing.Sequence[ReceiptBulkUpsertRequestReceiptsItem],
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[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
        -------
        AsyncHttpResponse[ReceiptBulkUpsertAsyncResponse]
            Asynchronous processing started. Poll batch_url for progress.
        """
        _response = await self._client_wrapper.httpx_client.request(
            "receipts/bulk",
            method="POST",
            json={
                "ottimate_company_id": ottimate_company_id,
                "receipts": convert_and_respect_annotation_metadata(
                    object_=receipts,
                    annotation=typing.Sequence[ReceiptBulkUpsertRequestReceiptsItem],
                    direction="write",
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ReceiptBulkUpsertAsyncResponse,
                    parse_obj_as(
                        type_=ReceiptBulkUpsertAsyncResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 400:
                raise BadRequestError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        ErrorResponse,
                        parse_obj_as(
                            type_=ErrorResponse,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)
