<?php

namespace Tops\AmazonSellingPartnerAPI\Api;

class VendorOrdersApi extends BaseApi
{
    /**
    * Get Purchase Orders
    *
    * @param array $params array of input params
    * @param string $params['limit'] The limit to the number of records returned. Default value is 100 records.
    * @param string $params['CreatedAfter'] A date used for selecting orders created after (or at) a specified time. Only orders placed after the specified time are returned. Either the CreatedAfter parameter or the LastUpdatedAfter parameter is required. Both cannot be empty. The date must be in ISO 8601 format.
    * @param string $params['CreatedBefore'] A date used for selecting orders created before (or at) a specified time. Only orders placed before the specified time are returned. The date must be in ISO 8601 format.
    * @param string $params['changedAfter'] A date used for selecting orders that were last updated after (or at) a specified time. An update is defined as any change in order status, including the creation of a new order. Includes updates made by Amazon and by the seller. The date must be in ISO 8601 format.
    * @param string $params['changedBefore'] A date used for selecting orders that were last updated before (or at) a specified time. An update is defined as any change in order status, including the creation of a new order. Includes updates made by Amazon and by the seller. The date must be in ISO 8601 format.
    * @param string $params['purchaseOrderState'] A list of OrderStatus values used to filter the results. (Possible values: New, Acknowledged, Closed)
    * @param string $params['NextToken']  	A string token returned in the response of your previous request.
    *
    * @return Json
    *
    * @author Jignesh Kapadia <jignesh@topsinfosolutions.com>
    *
    * About more Ref:- https://developer-docs.amazon.com/sp-api/docs/vendor-orders-api-v1-reference#getpurchaseorders
    */

    public function getPurchaseOrders(array $params)
    {
        $resource_path = '/vendor/orders/v1/purchaseOrders';
       
        $url = $this->config->getHost() . $resource_path;
        $http_body = '';
        $query_params = [];
        $method = 'GET';

        if (array_key_exists('limit', $params) && !empty($params['limit'])) {
            $query_params['limit'] = $params['limit'];
        }

        if (array_key_exists('createdAfter', $params) && !empty($params['createdAfter'])) {
            $query_params['createdAfter'] = $params['createdAfter'];
        }

        if (array_key_exists('createdBefore', $params) && !empty($params['createdBefore'])) {
            $query_params['createdBefore'] = $params['createdBefore'];
        }

        if (array_key_exists('changedAfter', $params) && !empty($params['changedAfter'])) {
            $query_params['changedAfter'] = $params['changedAfter'];
        }

        if (array_key_exists('changedBefore', $params) && !empty($params['changedBefore'])) {
            $query_params['changedBefore'] = $params['changedBefore'];
        }

        if (array_key_exists('purchaseOrderState', $params) && !empty($params['purchaseOrderState'])) {
            $query_params['purchaseOrderState'] = $params['purchaseOrderState'];
        }

        if (array_key_exists('nextToken', $params) && !empty($params['nextToken'])) {
            $query_params['nextToken'] = $params['nextToken'];
        }

        $curl_headers = $this->generateRequest($resource_path, $query_params, $http_body, $this->config, $method);

        return $this->sendRequest($url, $query_params, $http_body, $curl_headers, $method);
    }

    /**
    * Operation Get Purchase Order
    *
    * @param string $purchase_order_number An Amazon-defined order identifier, 8-character alpha-numeric code.
    *
    * @return json
    *
    * @author Jignesh Kapadia <chetan@topsinfosolutions.com>
    *
    * About more Ref :- https://developer-docs.amazon.com/sp-api/docs/vendor-orders-api-v1-reference#getpurchaseorder
    */

    public function getPurchaseOrder($purchase_order_number)
    {
        // check the required parameter 'order_id' is set
        if (empty($purchase_order_number)) {
            throw new \InvalidArgumentException('Missing the required parameter $purchase_order_number when calling getPurchaseOrder');
        }

        $resource_path = '/vendor/orders/v1/purchaseOrders/'.$purchase_order_number;
        $query_params = [];
        $http_body = '';
        $method = 'GET';
       
        $curl_headers = $this->generateRequest($resource_path, $query_params, $http_body, $this->config, $method);

        $url = $this->config->getHost() . $resource_path;

        return $this->sendRequest($url, $query_params, $http_body, $curl_headers, $method);
    }

    /**
    * Operation Submit Acknowledgement
    *
    * @param array $body required
    *
    * @return json
    *
    * @author Jignesh Kapadia <jignesh@topsinfosolutions.com>
    *
    * About more Ref :- https://developer-docs.amazon.com/sp-api/docs/vendor-orders-api-v1-reference#post-vendorordersv1acknowledgements
    */
    public function submitAcknowledgement($body)
    {
        $resource_path = '/vendor/orders/v1/acknowledgements';
        $http_body = $body;
        $query_params = [];
        $method = 'POST';

        $url = $this->config->getHost() . $resource_path;

        $curl_headers = $this->generateRequest($resource_path, $query_params, $http_body, $this->config, $method);

        return $this->sendRequest($url, $query_params, $http_body, $curl_headers, $method);
    }

    /**
    * Get Purchase Orders Status
    *
    * @param array $params array of input params
    * @param string $params['limit'] The limit to the number of records returned. Default value is 100 records.
    * @param string $params['createdAfter'] A date used for selecting orders created after (or at) a specified time. Only orders placed after the specified time are returned. Either the CreatedAfter parameter or the LastUpdatedAfter parameter is required. Both cannot be empty. The date must be in ISO 8601 format.
    * @param string $params['createdBefore'] A date used for selecting orders created before (or at) a specified time. Only orders placed before the specified time are returned. The date must be in ISO 8601 format.
    * @param string $params['changedAfter'] A date used for selecting orders that were last updated after (or at) a specified time. An update is defined as any change in order status, including the creation of a new order. Includes updates made by Amazon and by the seller. The date must be in ISO 8601 format.
    * @param string $params['updatedAfter'] A date used for selecting orders that were last updated after (or at) a specified time. An update is defined as any change in order status, including the creation of a new order. Includes updates made by Amazon and by the seller. The date must be in ISO 8601 format.
    * @param string $params['updatedBefore'] A date used for selecting orders that were last updated before (or at) a specified time. An update is defined as any change in order status, including the creation of a new order. Includes updates made by Amazon and by the seller. The date must be in ISO 8601 format.
    * @param string $params['purchaseOrderState'] A list of OrderStatus values used to filter the results. (Possible values: New, Acknowledged, Closed)
    * @param string $params['nextToken']    A string token returned in the response of your previous request.
    *
    * @return Json
    *
    * @author Jignesh Kapadia <jignesh@topsinfosolutions.com>
    *
    * About more Ref:- https://developer-docs.amazon.com/sp-api/docs/vendor-orders-api-v1-reference#get-vendorordersv1purchaseordersstatus
    */

    public function getPurchaseOrdersStatus(array $params)
    {
        $resource_path = '/vendor/orders/v1/purchaseOrdersStatus';
       
        $url = $this->config->getHost() . $resource_path;
        $http_body = '';
        $query_params = [];
        $method = 'GET';

        if (array_key_exists('limit', $params) && !empty($params['limit'])) {
            $query_params['limit'] = $params['limit'];
        }

        if (array_key_exists('purchaseOrderNumber', $params) && !empty($params['purchaseOrderNumber'])) {
            $query_params['purchaseOrderNumber'] = $params['purchaseOrderNumber'];
        }

        if (array_key_exists('createdAfter', $params) && !empty($params['createdAfter'])) {
            $query_params['createdAfter'] = $params['createdAfter'];
        }

        if (array_key_exists('createdBefore', $params) && !empty($params['createdBefore'])) {
            $query_params['createdBefore'] = $params['createdBefore'];
        }

        if (array_key_exists('updatedAfter', $params) && !empty($params['updatedAfter'])) {
            $query_params['updatedAfter'] = $params['updatedAfter'];
        }

        if (array_key_exists('updatedBefore', $params) && !empty($params['updatedBefore'])) {
            $query_params['updatedBefore'] = $params['updatedBefore'];
        }

        if (array_key_exists('purchaseOrderState', $params) && !empty($params['purchaseOrderState'])) {
            $query_params['purchaseOrderState'] = $params['purchaseOrderState'];
        }

        if (array_key_exists('itemConfirmationStatus', $params) && !empty($params['itemConfirmationStatus'])) {
            $query_params['itemConfirmationStatus'] = $params['itemConfirmationStatus'];
        }

        if (array_key_exists('itemReceiveStatus', $params) && !empty($params['itemReceiveStatus'])) {
            $query_params['itemReceiveStatus'] = $params['itemReceiveStatus'];
        }

        if (array_key_exists('nextToken', $params) && !empty($params['nextToken'])) {
            $query_params['nextToken'] = $params['nextToken'];
        }

        $curl_headers = $this->generateRequest($resource_path, $query_params, $http_body, $this->config, $method);

        return $this->sendRequest($url, $query_params, $http_body, $curl_headers, $method);
    }
}
