From 6515e39144aad657c20e4529570a3b47a06a8a02 Mon Sep 17 00:00:00 2001 From: Vladimir Kolchin <42410704+oxy-coach@users.noreply.github.com> Date: Wed, 2 Aug 2023 08:32:24 +0300 Subject: [PATCH] Added currency methods + properties (#174) Added currency methods + properties --- src/Model/Entity/Loyalty/Loyalty.php | 8 + src/Model/Entity/Orders/Order.php | 8 + src/Model/Entity/References/Currency.php | 77 ++++++++ src/Model/Entity/References/DeliveryType.php | 8 + src/Model/Entity/References/Site.php | 8 + .../References/CurrenciesCreateRequest.php | 44 +++++ .../References/CurrenciesResponse.php | 31 ++++ src/ResourceGroup/References.php | 173 ++++++++++++++++++ tests/src/ResourceGroup/LoyaltyTest.php | 7 +- tests/src/ResourceGroup/OrdersTest.php | 2 + tests/src/ResourceGroup/ReferencesTest.php | 105 ++++++++++- 11 files changed, 469 insertions(+), 2 deletions(-) create mode 100644 src/Model/Entity/References/Currency.php create mode 100644 src/Model/Request/References/CurrenciesCreateRequest.php create mode 100644 src/Model/Response/References/CurrenciesResponse.php diff --git a/src/Model/Entity/Loyalty/Loyalty.php b/src/Model/Entity/Loyalty/Loyalty.php index 4a28f29..134169c 100644 --- a/src/Model/Entity/Loyalty/Loyalty.php +++ b/src/Model/Entity/Loyalty/Loyalty.php @@ -46,6 +46,14 @@ class Loyalty */ public $blocked; + /** + * @var string + * + * @JMS\Type("string") + * @JMS\SerializedName("currency") + */ + public $currency; + /** * @var int * diff --git a/src/Model/Entity/Orders/Order.php b/src/Model/Entity/Orders/Order.php index 6015887..db22788 100644 --- a/src/Model/Entity/Orders/Order.php +++ b/src/Model/Entity/Orders/Order.php @@ -48,6 +48,14 @@ class Order */ public $summ; + /** + * @var string + * + * @JMS\Type("string") + * @JMS\SerializedName("currency") + */ + public $currency; + /** * @var int * diff --git a/src/Model/Entity/References/Currency.php b/src/Model/Entity/References/Currency.php new file mode 100644 index 0000000..96abbe2 --- /dev/null +++ b/src/Model/Entity/References/Currency.php @@ -0,0 +1,77 @@ +currency) { + $this->currency = $currency; + } + } +} diff --git a/src/Model/Response/References/CurrenciesResponse.php b/src/Model/Response/References/CurrenciesResponse.php new file mode 100644 index 0000000..ef75948 --- /dev/null +++ b/src/Model/Response/References/CurrenciesResponse.php @@ -0,0 +1,31 @@ +") + * @JMS\SerializedName("currencies") + */ + public $currencies; +} diff --git a/src/ResourceGroup/References.php b/src/ResourceGroup/References.php index 812bc32..a2b943e 100644 --- a/src/ResourceGroup/References.php +++ b/src/ResourceGroup/References.php @@ -13,6 +13,7 @@ use RetailCrm\Api\Enum\RequestMethod; use RetailCrm\Api\Model\Request\References\CostGroupsEditRequest; use RetailCrm\Api\Model\Request\References\CostItemsEditRequest; use RetailCrm\Api\Model\Request\References\CouriersCreateRequest; +use RetailCrm\Api\Model\Request\References\CurrenciesCreateRequest; use RetailCrm\Api\Model\Request\References\DeliveryServicesEditRequest; use RetailCrm\Api\Model\Request\References\DeliveryTypesEditRequest; use RetailCrm\Api\Model\Request\References\LegalEntityEditRequest; @@ -31,6 +32,7 @@ use RetailCrm\Api\Model\Response\References\CostGroupsResponse; use RetailCrm\Api\Model\Response\References\CostItemsResponse; use RetailCrm\Api\Model\Response\References\CountriesResponse; use RetailCrm\Api\Model\Response\References\CouriersResponse; +use RetailCrm\Api\Model\Response\References\CurrenciesResponse; use RetailCrm\Api\Model\Response\References\DeliveryServicesResponse; use RetailCrm\Api\Model\Response\References\DeliveryTypesResponse; use RetailCrm\Api\Model\Response\References\LegalEntitiesResponse; @@ -510,6 +512,177 @@ class References extends AbstractApiResourceGroup return $response; } + /** + * Makes GET "/api/v5/reference/currencies" request. + * + * Example: + * ```php + * use RetailCrm\Api\Factory\SimpleClientFactory; + * use RetailCrm\Api\Interfaces\ApiExceptionInterface; + * + * $client = SimpleClientFactory::createClient('https://test.retailcrm.pro', 'apiKey'); + * + * try { + * $response = $client->references->currencies(); + * } catch (ApiExceptionInterface $exception) { + * echo sprintf( + * 'Error from RetailCRM API (status code: %d): %s', + * $exception->getStatusCode(), + * $exception->getMessage() + * ); + * + * if (count($exception->getErrorResponse()->errors) > 0) { + * echo PHP_EOL . 'Errors: ' . implode(', ', $exception->getErrorResponse()->errors); + * } + * + * return; + * } + * + * echo 'Currencies: ' . print_r($response->currencies, true); + * ``` + * + * @return \RetailCrm\Api\Model\Response\References\CurrenciesResponse + * @throws \RetailCrm\Api\Interfaces\ApiExceptionInterface + * @throws \RetailCrm\Api\Interfaces\ClientExceptionInterface + * @throws \RetailCrm\Api\Exception\Api\AccountDoesNotExistException + * @throws \RetailCrm\Api\Exception\Api\ApiErrorException + * @throws \RetailCrm\Api\Exception\Api\MissingCredentialsException + * @throws \RetailCrm\Api\Exception\Api\MissingParameterException + * @throws \RetailCrm\Api\Exception\Api\ValidationException + * @throws \RetailCrm\Api\Exception\Client\HandlerException + * @throws \RetailCrm\Api\Exception\Client\HttpClientException + */ + public function currencies(): CurrenciesResponse + { + /** @var CurrenciesResponse $response */ + $response = $this->sendRequest( + RequestMethod::GET, + 'reference/currencies', + null, + CurrenciesResponse::class + ); + return $response; + } + + /** + * Makes POST "/api/v5/reference/currencies/create" request. + * + * Example: + * ```php + * use RetailCrm\Api\Factory\SimpleClientFactory; + * use RetailCrm\Api\Interfaces\ApiExceptionInterface; + * use RetailCrm\Api\Model\Entity\References\Currency; + * use RetailCrm\Api\Model\Request\References\CurrenciesCreateRequest; + * + * $client = SimpleClientFactory::createClient('https://test.retailcrm.pro', 'apiKey'); + * + * $entity = new Currency(); + * $entity->code = 'USD'; + * $entity->isAutoConvert = true; + * $entity->autoConvertExtraPercent = 5; + * + * try { + * $response = $client->references->currenciesCreate(new CurrenciesCreateRequest($entity)); + * } catch (ApiExceptionInterface $exception) { + * echo sprintf( + * 'Error from RetailCRM API (status code: %d): %s', + * $exception->getStatusCode(), + * $exception->getMessage() + * ); + * + * if (count($exception->getErrorResponse()->errors) > 0) { + * echo PHP_EOL . 'Errors: ' . implode(', ', $exception->getErrorResponse()->errors); + * } + * + * return; + * } + * + * echo 'Created currency with ID: ' . $response->id; + * ``` + * + * @param \RetailCrm\Api\Model\Request\References\CurrenciesCreateRequest $request + * + * @return \RetailCrm\Api\Model\Response\IdResponse + * @throws \RetailCrm\Api\Interfaces\ApiExceptionInterface + * @throws \RetailCrm\Api\Interfaces\ClientExceptionInterface + * @throws \RetailCrm\Api\Exception\Api\AccountDoesNotExistException + * @throws \RetailCrm\Api\Exception\Api\ApiErrorException + * @throws \RetailCrm\Api\Exception\Api\MissingCredentialsException + * @throws \RetailCrm\Api\Exception\Api\MissingParameterException + * @throws \RetailCrm\Api\Exception\Api\ValidationException + * @throws \RetailCrm\Api\Exception\Client\HandlerException + * @throws \RetailCrm\Api\Exception\Client\HttpClientException + */ + public function currenciesCreate(CurrenciesCreateRequest $request): IdResponse + { + /** @var IdResponse $response */ + $response = $this->sendRequest( + RequestMethod::POST, + 'reference/currencies/create', + $request, + IdResponse::class + ); + return $response; + } + + /** + * Makes POST "/api/v5/reference/currencies/{id}/edit" request. + * + * Example: + * ```php + * use RetailCrm\Api\Factory\SimpleClientFactory; + * use RetailCrm\Api\Interfaces\ApiExceptionInterface; + * use RetailCrm\Api\Model\Entity\References\Currency; + * use RetailCrm\Api\Model\Request\References\CurrenciesCreateRequest; + * + * $client = SimpleClientFactory::createClient('https://test.retailcrm.pro', 'apiKey'); + * + * $entity = new Currency(); + * $entity->code = 'USD'; + * $entity->isAutoConvert = true; + * $entity->autoConvertExtraPercent = 10; + * + * try { + * $response = $client->references->currenciesEdit(1, new CurrenciesCreateRequest($entity)); + * } catch (ApiExceptionInterface $exception) { + * echo sprintf( + * 'Error from RetailCRM API (status code: %d): %s', + * $exception->getStatusCode(), + * $exception->getMessage() + * ); + * + * if (count($exception->getErrorResponse()->errors) > 0) { + * echo PHP_EOL . 'Errors: ' . implode(', ', $exception->getErrorResponse()->errors); + * } + * } + * ``` + * + * @param int $id + * @param \RetailCrm\Api\Model\Request\References\CurrenciesCreateRequest $request + * + * @return \RetailCrm\Api\Model\Response\SuccessResponse + * @throws \RetailCrm\Api\Interfaces\ApiExceptionInterface + * @throws \RetailCrm\Api\Interfaces\ClientExceptionInterface + * @throws \RetailCrm\Api\Exception\Api\AccountDoesNotExistException + * @throws \RetailCrm\Api\Exception\Api\ApiErrorException + * @throws \RetailCrm\Api\Exception\Api\MissingCredentialsException + * @throws \RetailCrm\Api\Exception\Api\MissingParameterException + * @throws \RetailCrm\Api\Exception\Api\ValidationException + * @throws \RetailCrm\Api\Exception\Client\HandlerException + * @throws \RetailCrm\Api\Exception\Client\HttpClientException + */ + public function currenciesEdit(int $id, CurrenciesCreateRequest $request): SuccessResponse + { + /** @var SuccessResponse $response */ + $response = $this->sendRequest( + RequestMethod::POST, + 'reference/currencies/' . $id . '/edit', + $request, + SuccessResponse::class + ); + return $response; + } + /** * Makes GET "/api/v5/reference/delivery-services" request. * diff --git a/tests/src/ResourceGroup/LoyaltyTest.php b/tests/src/ResourceGroup/LoyaltyTest.php index 64310c0..51c5e99 100644 --- a/tests/src/ResourceGroup/LoyaltyTest.php +++ b/tests/src/ResourceGroup/LoyaltyTest.php @@ -618,7 +618,8 @@ EOF; "active": false, "id": 168, "loyalty": { - "id": 1 + "id": 1, + "currency": "RUB" }, "customer": { "id": 5260, @@ -1098,6 +1099,7 @@ EOF; { "active": true, "blocked": false, + "currency": "RUB", "id": 3, "name": "Новая программа", "confirmSmsCharge": false, @@ -1108,6 +1110,7 @@ EOF; { "active": true, "blocked": false, + "currency": "RUB", "id": 4, "name": "Битрикс новый", "confirmSmsCharge": false, @@ -1118,6 +1121,7 @@ EOF; { "active": true, "blocked": false, + "currency": "RUB", "id": 1, "name": "Тестовая программа", "confirmSmsCharge": false, @@ -1154,6 +1158,7 @@ EOF; "loyalty": { "active": true, "blocked": false, + "currency": "RUB", "id": 4, "name": "Битрикс новый", "confirmSmsCharge": false, diff --git a/tests/src/ResourceGroup/OrdersTest.php b/tests/src/ResourceGroup/OrdersTest.php index 22db5e1..38df002 100644 --- a/tests/src/ResourceGroup/OrdersTest.php +++ b/tests/src/ResourceGroup/OrdersTest.php @@ -86,6 +86,7 @@ class OrdersTest extends AbstractApiResourceGroupTestCase "createdAt": "2021-02-25 09:37:48", "statusUpdatedAt": "2021-02-25 09:37:55", "summ": 0, + "currency": "RUB", "totalSumm": 0, "prepaySum": 0, "purchaseSumm": 60, @@ -8185,6 +8186,7 @@ EOF; "createdAt": "2020-12-29 17:35:08", "statusUpdatedAt": "2020-12-31 15:09:31", "summ": 1, + "currency": "RUB", "totalSumm": 1, "prepaySum": 0, "purchaseSumm": 0, diff --git a/tests/src/ResourceGroup/ReferencesTest.php b/tests/src/ResourceGroup/ReferencesTest.php index c2a7e17..b2e5e69 100644 --- a/tests/src/ResourceGroup/ReferencesTest.php +++ b/tests/src/ResourceGroup/ReferencesTest.php @@ -19,6 +19,7 @@ use RetailCrm\Api\Model\Entity\Orders\Delivery\CourierPhone; use RetailCrm\Api\Model\Entity\References\CostGroup; use RetailCrm\Api\Model\Entity\References\CostItem; use RetailCrm\Api\Model\Entity\References\Courier; +use RetailCrm\Api\Model\Entity\References\Currency; use RetailCrm\Api\Model\Entity\References\DeliveryService; use RetailCrm\Api\Model\Entity\References\DeliveryType; use RetailCrm\Api\Model\Entity\References\LegalEntity; @@ -37,6 +38,7 @@ use RetailCrm\Api\Model\Entity\References\StorePhone; use RetailCrm\Api\Model\Request\References\CostGroupsEditRequest; use RetailCrm\Api\Model\Request\References\CostItemsEditRequest; use RetailCrm\Api\Model\Request\References\CouriersCreateRequest; +use RetailCrm\Api\Model\Request\References\CurrenciesCreateRequest; use RetailCrm\Api\Model\Request\References\DeliveryServicesEditRequest; use RetailCrm\Api\Model\Request\References\DeliveryTypesEditRequest; use RetailCrm\Api\Model\Request\References\LegalEntityEditRequest; @@ -489,6 +491,101 @@ EOF; self::assertModelEqualsToResponse($json, $response); } + public function testCurrencies(): void + { + $json = <<<'EOF' +{ + "success": true, + "currencies": [ + { + "id": 1, + "code": "RUB", + "isBase": true, + "isAutoConvert": false + }, + { + "id": 3, + "code": "KZT", + "isBase": false, + "isAutoConvert": true, + "autoConvertExtraPercent": 10 + }, + { + "id": 2, + "code": "USD", + "isBase": false, + "isAutoConvert": true, + "autoConvertExtraPercent": 5 + } + ] +} +EOF; + + $mock = static::createApiMockBuilder('reference/currencies'); + $mock->matchMethod(RequestMethod::GET) + ->reply(200) + ->withBody($json); + + $client = TestClientFactory::createClient($mock->getClient()); + $response = $client->references->currencies(); + + self::assertModelEqualsToResponse($json, $response); + } + + public function testCurrenciesCreate(): void + { + $json = <<<'EOF' +{ + "success": true, + "id": 1 +} +EOF; + + $entity = new Currency(); + $entity->code = 'EUR'; + $entity->isAutoConvert = true; + $entity->autoConvertExtraPercent = 5; + + $request = new CurrenciesCreateRequest($entity); + + $mock = static::createApiMockBuilder('reference/currencies/create'); + $mock->matchMethod(RequestMethod::POST) + ->matchBody(self::encodeForm($request)) + ->reply(200) + ->withBody($json); + + $client = TestClientFactory::createClient($mock->getClient()); + $response = $client->references->currenciesCreate($request); + + self::assertModelEqualsToResponse($json, $response); + } + + public function testCurrenciesEdit(): void + { + $json = <<<'EOF' +{ + "success": true +} +EOF; + + $entity = new Currency(); + $entity->isAutoConvert = true; + $entity->autoConvertExtraPercent = 15; + + $request = new CurrenciesCreateRequest($entity); + + $mock = static::createApiMockBuilder('reference/currencies/3/edit'); + $mock->matchMethod(RequestMethod::POST) + ->matchBody(self::encodeForm($request)) + ->reply(200) + ->withBody($json); + + $client = TestClientFactory::createClient($mock->getClient()); + $response = $client->references->currenciesEdit(3, $request); + + self::assertModelEqualsToResponse($json, $response); + } + public function testDeliveryServices(): void { $json = <<<'EOF' @@ -663,6 +760,7 @@ EOF; "isAutoNetCostCalculation": false, "isCostDependsOnRegionAndWeightAndSum": false, "isCostDependsOnDateTime": false, + "currency": "RUB", "name": "Доставка курьером", "code": "2", "active": true, @@ -702,6 +800,7 @@ EOF; "isAutoNetCostCalculation": false, "isCostDependsOnRegionAndWeightAndSum": false, "isCostDependsOnDateTime": false, + "currency": "RUB", "name": "Почта России", "code": "16", "active": true, @@ -738,6 +837,7 @@ EOF; "isAutoNetCostCalculation": false, "isCostDependsOnRegionAndWeightAndSum": false, "isCostDependsOnDateTime": false, + "currency": "RUB", "name": "Доставка OZON", "code": "ozon-seller", "active": false, @@ -801,6 +901,7 @@ EOF; "isAutoNetCostCalculation": true, "isCostDependsOnRegionAndWeightAndSum": false, "isCostDependsOnDateTime": false, + "currency": "RUB", "name": "Курьерист 24", "code": "crrst-24", "active": true, @@ -2573,6 +2674,7 @@ EOF; "countryIso": "RU" }, "countryIso": "RU", + "currency": "RUB", "senderEmail": "test@example.com", "senderName": "test", "catalogId": "catalog", @@ -2602,7 +2704,8 @@ EOF; "code": "Vog_Gallery", "countryIso": "RU" }, - "countryIso": "RU" + "countryIso": "RU", + "currency": "RUB" } } }