From 0e685c3b6156d68ce653bd59b19c06e82aa997fb Mon Sep 17 00:00:00 2001 From: kirill Date: Fri, 22 Jul 2022 17:48:46 +0300 Subject: [PATCH 01/22] -test serializer --- composer.json | 8 +++-- .../Telephony/testSerializer/person.php | 35 +++++++++++++++++++ .../Telephony/testSerializer/serializer.php | 21 +++++++++++ 3 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 src/Services/Telephony/testSerializer/person.php create mode 100644 src/Services/Telephony/testSerializer/serializer.php diff --git a/composer.json b/composer.json index 0a09205e..799df0ef 100644 --- a/composer.json +++ b/composer.json @@ -19,6 +19,7 @@ "require": { "php": "7.4.*|8.*", "ext-json": "*", + "ext-bcmath": "*", "ext-curl": "*", "psr/log": "^1.1.4 || ^2.0 || ^3.0", "fig/http-message-util": "1.1.*", @@ -26,7 +27,9 @@ "symfony/http-client-contracts": "^2.5 || ^3.1", "symfony/http-foundation": "5.4.* || 6.*", "symfony/event-dispatcher": "5.4.* || 6.*", - "ramsey/uuid": "^4.2.3" + "ramsey/uuid": "^4.2.3", + "moneyphp/money": "3.* || 4.*", + "symfony/serializer": "^6.1" }, "require-dev": { "monolog/monolog": "2.1.*", @@ -36,7 +39,8 @@ "phpstan/phpstan": "1.*", "phpunit/phpunit": "9.5.*", "symfony/stopwatch": "5.4.* || 6.*", - "roave/security-advisories": "dev-master" + "roave/security-advisories": "dev-master", + "ext-intl": "*" }, "autoload": { "psr-4": { diff --git a/src/Services/Telephony/testSerializer/person.php b/src/Services/Telephony/testSerializer/person.php new file mode 100644 index 00000000..4e72acfe --- /dev/null +++ b/src/Services/Telephony/testSerializer/person.php @@ -0,0 +1,35 @@ +name; + } + + public function getLastName() + { + return $this->lastName; + } + + + // Setters + public function setName($name) + { + $this->name = $name; + } + + public function setLastName($lastName) + { + $this->lastName = $lastName; + } + +} \ No newline at end of file diff --git a/src/Services/Telephony/testSerializer/serializer.php b/src/Services/Telephony/testSerializer/serializer.php new file mode 100644 index 00000000..96808e9e --- /dev/null +++ b/src/Services/Telephony/testSerializer/serializer.php @@ -0,0 +1,21 @@ +setName('Kirill'); +$person->setLastName('Hramov'); + +$jsonContent = $serializer->serialize($person, 'json'); + +// $jsonContent contains {"name":"foo","age":99,"sportsperson":false,"createdAt":null} + +echo $jsonContent; // or return it in a Response \ No newline at end of file From 76d68d76bf452a06b3e629b240e3b23c30581c3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B8=D1=80=D0=B8=D0=BB=D0=BB=20=D0=A5=D1=80=D0=B0?= =?UTF-8?q?=D0=BC=D0=BE=D0=B2?= Date: Mon, 25 Jul 2022 15:05:18 +0300 Subject: [PATCH 02/22] -add test serialize and deserialize --- composer.json | 4 +- .../Telephony/testSerializer/serializer.php | 21 ------- .../Unit/Services/TestPerson/Person.php | 4 +- tests/Unit/Services/TestPerson/PersonTest.php | 58 +++++++++++++++++++ 4 files changed, 63 insertions(+), 24 deletions(-) delete mode 100644 src/Services/Telephony/testSerializer/serializer.php rename src/Services/Telephony/testSerializer/person.php => tests/Unit/Services/TestPerson/Person.php (86%) create mode 100644 tests/Unit/Services/TestPerson/PersonTest.php diff --git a/composer.json b/composer.json index 799df0ef..fdbf9b3b 100644 --- a/composer.json +++ b/composer.json @@ -29,7 +29,9 @@ "symfony/event-dispatcher": "5.4.* || 6.*", "ramsey/uuid": "^4.2.3", "moneyphp/money": "3.* || 4.*", - "symfony/serializer": "^6.1" + "symfony/serializer": "6.*||^6.1", + "symfony/routing": "^4.4|^5.3|^6.0", + "symfony/property-access": "6.* || 6.1.*" }, "require-dev": { "monolog/monolog": "2.1.*", diff --git a/src/Services/Telephony/testSerializer/serializer.php b/src/Services/Telephony/testSerializer/serializer.php deleted file mode 100644 index 96808e9e..00000000 --- a/src/Services/Telephony/testSerializer/serializer.php +++ /dev/null @@ -1,21 +0,0 @@ -setName('Kirill'); -$person->setLastName('Hramov'); - -$jsonContent = $serializer->serialize($person, 'json'); - -// $jsonContent contains {"name":"foo","age":99,"sportsperson":false,"createdAt":null} - -echo $jsonContent; // or return it in a Response \ No newline at end of file diff --git a/src/Services/Telephony/testSerializer/person.php b/tests/Unit/Services/TestPerson/Person.php similarity index 86% rename from src/Services/Telephony/testSerializer/person.php rename to tests/Unit/Services/TestPerson/Person.php index 4e72acfe..41c3c19f 100644 --- a/src/Services/Telephony/testSerializer/person.php +++ b/tests/Unit/Services/TestPerson/Person.php @@ -2,9 +2,9 @@ declare(strict_types=1); -namespace Bitrix24\SDK\Services\Telephony\testSerializer; +namespace Bitrix24\SDK\Tests\Unit\Services\TestPerson; -class person +class Person { private string $name; private string $lastName; diff --git a/tests/Unit/Services/TestPerson/PersonTest.php b/tests/Unit/Services/TestPerson/PersonTest.php new file mode 100644 index 00000000..0027610a --- /dev/null +++ b/tests/Unit/Services/TestPerson/PersonTest.php @@ -0,0 +1,58 @@ +setName('Kirill'); + $person->setLastName('Khramov'); + + + $jsonContent = $serializer->serialize($person, 'json'); + + + self::assertNotEmpty($jsonContent); + echo $jsonContent; // or return it in a Response + } + + /** + * @test + */ + public function DeserializeTest():void{ + $encoders = [new XmlEncoder(), new JsonEncoder()]; + $normalizers = [new ObjectNormalizer()]; + + $serializer = new Serializer($normalizers, $encoders); + + $data = << + Kirill + Khramov + false + + EOF; + + $person = $serializer->deserialize($data, Person::class, 'xml'); + var_dump($person); + self::assertNotEmpty($person); + } +} \ No newline at end of file From ef264a96de201b7b44395e90d793cac33de00dbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B8=D1=80=D0=B8=D0=BB=D0=BB=20=D0=A5=D1=80=D0=B0?= =?UTF-8?q?=D0=BC=D0=BE=D0=B2?= Date: Tue, 26 Jul 2022 18:02:44 +0300 Subject: [PATCH 03/22] -add test normalize -and edit NetworkTimingsParser.php --- .../TransportLayer/NetworkTimingsParser.php | 14 ++++---- tests/Unit/Services/TestPerson/PersonTest.php | 33 +++++++++++++++++++ 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/src/Infrastructure/HttpClient/TransportLayer/NetworkTimingsParser.php b/src/Infrastructure/HttpClient/TransportLayer/NetworkTimingsParser.php index 696a08a9..9d2b2711 100644 --- a/src/Infrastructure/HttpClient/TransportLayer/NetworkTimingsParser.php +++ b/src/Infrastructure/HttpClient/TransportLayer/NetworkTimingsParser.php @@ -28,12 +28,12 @@ public function __construct(array $httpClientResponseInfo) // get the name lookup time // Time from the start until the name resolving was completed. // When a redirect is followed, the time from each request is added together. - 'namelookup_time_us' => $httpClientResponseInfo['namelookup_time_us'], + 'namelookup_time' => $httpClientResponseInfo['namelookup_time'], // total time in seconds from the start until the connection to the remote host (or proxy) was completed in MICROSECONDS // https://curl.se/libcurl/c/CURLINFO_CONNECT_TIME.html // When a redirect is followed, the time from each request is added together. - 'connect_time_us' => $httpClientResponseInfo['connect_time_us'], + 'connect_time' => $httpClientResponseInfo['connect_time'], // time until the SSL/SSH handshake is completed in MICROSECONDS // https://curl.se/libcurl/c/CURLINFO_APPCONNECT_TIME.html @@ -41,7 +41,7 @@ public function __construct(array $httpClientResponseInfo) // This time is most often close to the CURLINFO_PRETRANSFER_TIME time, except for cases such as HTTP pipelining // where the pretransfer time can be delayed due to waits in line for the pipeline and more. // When a redirect is followed, the time from each request is added together. - 'appconnect_time_us' => $httpClientResponseInfo['appconnect_time_us'], + 'appconnect_time' => $httpClientResponseInfo['appconnect_time'] ?? null, // time until the file transfer start in MICROSECONDS // https://curl.se/libcurl/c/CURLINFO_PRETRANSFER_TIME.html @@ -49,28 +49,28 @@ public function __construct(array $httpClientResponseInfo) // This time-stamp includes all pre-transfer commands and negotiations that are specific to the particular // protocol(s) involved. It includes the sending of the protocol- specific protocol instructions that triggers a transfer. // When a redirect is followed, the time from each request is added together. - 'pretransfer_time_us' => $httpClientResponseInfo['pretransfer_time_us'], + 'pretransfer_time' => $httpClientResponseInfo['pretransfer_time'], // time for all redirection steps in MICROSECONDS // https://curl.se/libcurl/c/CURLINFO_REDIRECT_TIME.html // it took for all redirection steps include name lookup, connect, pretransfer and transfer before // final transaction was started. // CURLINFO_REDIRECT_TIME contains the complete execution time for multiple redirections. - 'redirect_time_us' => $httpClientResponseInfo['redirect_time_us'], + 'redirect_time' => $httpClientResponseInfo['redirect_time'], // time until the first byte is received in MICROSECONDS // it took from the start until the first byte is received by libcurl // https://curl.se/libcurl/c/CURLINFO_STARTTRANSFER_TIME.html // This includes CURLINFO_PRETRANSFER_TIME and also the time the server needs to calculate the result. // When a redirect is followed, the time from each request is added together. - 'starttransfer_time_us' => $httpClientResponseInfo['starttransfer_time_us'], + 'starttransfer_time' => $httpClientResponseInfo['starttransfer_time'], // total time of previous transfer in MICROSECONDS // https://curl.se/libcurl/c/CURLINFO_TOTAL_TIME.html // total time in seconds for the previous transfer, including name resolving, TCP connect etc. // The double represents the time in seconds, including fractions. // When a redirect is followed, the time from each request is added together. - 'total_time_us' => $httpClientResponseInfo['total_time_us'], + 'total_time' => $httpClientResponseInfo['total_time'], ]; } diff --git a/tests/Unit/Services/TestPerson/PersonTest.php b/tests/Unit/Services/TestPerson/PersonTest.php index 0027610a..015ebe75 100644 --- a/tests/Unit/Services/TestPerson/PersonTest.php +++ b/tests/Unit/Services/TestPerson/PersonTest.php @@ -3,11 +3,17 @@ namespace Bitrix24\SDK\Tests\Unit\Services\TestPerson; +use Money\Currency; use PHPUnit\Framework\TestCase; use Symfony\Component\Serializer\Encoder\JsonEncoder; use Symfony\Component\Serializer\Encoder\XmlEncoder; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; use Symfony\Component\Serializer\Serializer; +use Money\Money; +use Money\Currencies\ISOCurrencies; +use Money\Formatter\AggregateMoneyFormatter; +use Money\Formatter\IntlMoneyFormatter; +use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; class PersonTest extends TestCase @@ -27,6 +33,7 @@ public function SerializerTest():void{ $person->setLastName('Khramov'); + $jsonContent = $serializer->serialize($person, 'json'); @@ -55,4 +62,30 @@ public function DeserializeTest():void{ var_dump($person); self::assertNotEmpty($person); } + + /** + * @test + * @throws \Symfony\Component\Serializer\Exception\ExceptionInterface + */ + public function NormalizeMoneyTest():void + { + $encoders = [new XmlEncoder(), new JsonEncoder()]; + $normalizers = [new ObjectNormalizer()]; + + $serializer = new Serializer($normalizers, $encoders); + + $dollars = new Money(100,new Currency('USD')); + + $numberFormatter = new \NumberFormatter('en_US', \NumberFormatter::CURRENCY); + $intlFormatter = new IntlMoneyFormatter($numberFormatter, new ISOCurrencies()); + + $moneyFormatter = new AggregateMoneyFormatter([ + 'USD' => $intlFormatter, + ]); + + $money = $moneyFormatter->format($dollars); + $jsonContent = $serializer->normalize($money,null, [AbstractNormalizer::ATTRIBUTES => ['amount']]); + var_dump($jsonContent); + self::assertNotEmpty($money); + } } \ No newline at end of file From 2f5b09430627c5ba7ada6a9a8e2b95d1ab80efca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B8=D1=80=D0=B8=D0=BB=D0=BB=20=D0=A5=D1=80=D0=B0?= =?UTF-8?q?=D0=BC=D0=BE=D0=B2?= Date: Wed, 3 Aug 2022 11:57:58 +0300 Subject: [PATCH 04/22] -edit PersonTest.php -added a test to check the operation of products and transactions between themselves --- composer.json | 3 +- .../Services/MixedTest/MixedTest.php | 128 ++++++++++++++++++ tests/Unit/Services/TestPerson/PersonTest.php | 11 +- 3 files changed, 138 insertions(+), 4 deletions(-) create mode 100644 tests/Integration/Services/MixedTest/MixedTest.php diff --git a/composer.json b/composer.json index fdbf9b3b..f34ae8dc 100644 --- a/composer.json +++ b/composer.json @@ -42,7 +42,8 @@ "phpunit/phpunit": "9.5.*", "symfony/stopwatch": "5.4.* || 6.*", "roave/security-advisories": "dev-master", - "ext-intl": "*" + "ext-intl": "*", + "ext-http": "*" }, "autoload": { "psr-4": { diff --git a/tests/Integration/Services/MixedTest/MixedTest.php b/tests/Integration/Services/MixedTest/MixedTest.php new file mode 100644 index 00000000..3f0d3c1f --- /dev/null +++ b/tests/Integration/Services/MixedTest/MixedTest.php @@ -0,0 +1,128 @@ + array( + 'NAME' => '1С-Битрикс: Управление сайтом - Старт', + 'CURRENCY_ID' => 'RUB', + 'PRICE' => 4900, + 'SORT' => 4 + ) + ); + $queryDataForProduct1 = http_build_query($dataProduct1, 'https://b24-5p29et.bitrix24.ru/rest/1/113n4wi2ocu6rme0/crm.product.add') . PHP_EOL; + $resQueryForProduct1 = file_get_contents("https://b24-5p29et.bitrix24.ru/rest/1/113n4wi2ocu6rme0/crm.product.add?" . $queryDataForProduct1); + $decodeResQueryForProduct1 = json_decode($resQueryForProduct1, true, 512, JSON_THROW_ON_ERROR); + $idForProduct1 = $decodeResQueryForProduct1['result']; + $curl = curl_init(); + curl_setopt_array($curl, array( + CURLOPT_RETURNTRANSFER => 1, + CURLOPT_URL => $resQueryForProduct1, + )); + $response = curl_exec($curl); + curl_close($curl); + + //Создание продукта 2 + $dataProduct2 = array( + 'fields' => array( + 'NAME' => '1С-Битрикс: Управление сайтом - Старт 2 ', + 'CURRENCY_ID' => 'USD', + 'PRICE' => 4500, + 'SORT' => 1, + ) + ); + $queryDataForProduct2 = http_build_query($dataProduct2, 'https://b24-5p29et.bitrix24.ru/rest/1/113n4wi2ocu6rme0/crm.product.add') . PHP_EOL; + $resQueryForProduct2 = file_get_contents("https://b24-5p29et.bitrix24.ru/rest/1/113n4wi2ocu6rme0/crm.product.add?" . $queryDataForProduct2); + $decodeResQueryForProduct2 = json_decode($resQueryForProduct2, true, 512, JSON_THROW_ON_ERROR); + $idForProduct2 = $decodeResQueryForProduct2['result']; + $curl = curl_init(); + curl_setopt_array($curl, array( + CURLOPT_RETURNTRANSFER => 1, + CURLOPT_URL => $resQueryForProduct2, + )); + $response = curl_exec($curl); + curl_close($curl); + + //Создание Сделки + $dataDeal1 = array( + 'fields' => array( + 'TITLE' => 'test deal', + 'CURRENCY_ID' => 'USD', + 'OPPORTUNITY' => 500 + ) + ); + $queryDataForDeal1 = http_build_query($dataDeal1, 'https://b24-5p29et.bitrix24.ru/rest/1/113n4wi2ocu6rme0/crm.deal.add') . PHP_EOL; + $resQueryForDeal1 = file_get_contents("https://b24-5p29et.bitrix24.ru/rest/1/113n4wi2ocu6rme0/crm.deal.add?" . $queryDataForProduct1); + $decodeResQueryForDeal1 = json_decode($resQueryForDeal1, true, 512, JSON_THROW_ON_ERROR); + $idForDeal1 = $decodeResQueryForDeal1['result']; + $curl = curl_init(); + curl_setopt_array($curl, array( + CURLOPT_RETURNTRANSFER => 1, + CURLOPT_URL => $resQueryForDeal1, + )); + $response = curl_exec($curl); + curl_close($curl); + + //Добавление продукта в сделку + $addProductInDeal = array( + 'id' => $idForDeal1, + 'rows' => array( + [ + 'PRODUCT_ID' => $idForProduct1, + 'PRICE' => 100, + ], + [ + 'PRODUCT_ID' => $idForProduct2, + 'PRICE' => 100, + ] + ) + ); + $queryDataForProductInDeal = http_build_query($addProductInDeal, 'https://b24-5p29et.bitrix24.ru/rest/1/113n4wi2ocu6rme0/crm.deal.productrows.set') . PHP_EOL; + $resQueryForProductInDeal = file_get_contents("https://b24-5p29et.bitrix24.ru/rest/1/113n4wi2ocu6rme0/crm.deal.productrows.set?" . $queryDataForProductInDeal); + $decodeResQueryForDeal1 = json_decode($resQueryForProductInDeal, true, 512, JSON_THROW_ON_ERROR); + $curl = curl_init(); + curl_setopt_array($curl, array( + CURLOPT_RETURNTRANSFER => 1, + CURLOPT_URL => $resQueryForProductInDeal, + )); + $response = curl_exec($curl); + curl_close($curl); + + self::assertNotEmpty($dataProduct1); + self::assertNotEmpty($dataProduct2); + self::assertNotEmpty($dataDeal1); + self::assertNotEmpty($addProductInDeal); + } + + + /** + * @throws \Bitrix24\SDK\Core\Exceptions\InvalidArgumentException + */ + public function setUp(): void + { + $this->productService = Fabric::getServiceBuilder()->getCRMScope()->product(); + $this->dealServices = Fabric::getServiceBuilder()->getCRMScope()->deal(); + } +} diff --git a/tests/Unit/Services/TestPerson/PersonTest.php b/tests/Unit/Services/TestPerson/PersonTest.php index 015ebe75..02154645 100644 --- a/tests/Unit/Services/TestPerson/PersonTest.php +++ b/tests/Unit/Services/TestPerson/PersonTest.php @@ -3,6 +3,8 @@ namespace Bitrix24\SDK\Tests\Unit\Services\TestPerson; +use Bitrix24\SDK\Services\CRM\Deal\Service\Deal; +use Bitrix24\SDK\Services\CRM\Product\Service\Product; use Money\Currency; use PHPUnit\Framework\TestCase; use Symfony\Component\Serializer\Encoder\JsonEncoder; @@ -18,6 +20,8 @@ class PersonTest extends TestCase { + protected Deal $deal; + protected Product $product; /** * @test */ @@ -74,7 +78,7 @@ public function NormalizeMoneyTest():void $serializer = new Serializer($normalizers, $encoders); - $dollars = new Money(100,new Currency('USD')); + $dollars = new Money(100, new Currency('USD')); $numberFormatter = new \NumberFormatter('en_US', \NumberFormatter::CURRENCY); $intlFormatter = new IntlMoneyFormatter($numberFormatter, new ISOCurrencies()); @@ -83,9 +87,10 @@ public function NormalizeMoneyTest():void 'USD' => $intlFormatter, ]); - $money = $moneyFormatter->format($dollars); - $jsonContent = $serializer->normalize($money,null, [AbstractNormalizer::ATTRIBUTES => ['amount']]); + $money = $moneyFormatter->format($dollars); + $jsonContent = $serializer->normalize($money, null, [AbstractNormalizer::ATTRIBUTES => ['amount']]); var_dump($jsonContent); self::assertNotEmpty($money); } + } \ No newline at end of file From 179a2ac7036a67752b91b5c71050c0751a873c58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B8=D1=80=D0=B8=D0=BB=D0=BB=20=D0=A5=D1=80=D0=B0?= =?UTF-8?q?=D0=BC=D0=BE=D0=B2?= Date: Wed, 3 Aug 2022 16:59:37 +0300 Subject: [PATCH 05/22] -add custom Normalizer(not work) --- .../Services/TestPerson/PersonNormalizer.php | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 tests/Unit/Services/TestPerson/PersonNormalizer.php diff --git a/tests/Unit/Services/TestPerson/PersonNormalizer.php b/tests/Unit/Services/TestPerson/PersonNormalizer.php new file mode 100644 index 00000000..8368beb0 --- /dev/null +++ b/tests/Unit/Services/TestPerson/PersonNormalizer.php @@ -0,0 +1,39 @@ +router = $router; + $this->normalizer = $normalizer; + } + + public function normalize($person, string $format = null, array $context = []) + { + $data = $this->normalizer->normalize($person, $format, $context); + var_dump($data); + // Здесь, добавьте, измените или удалите некоторые данные: + return $this->router->generate('topic_show', [ + 'name' => $person->getName(), + ], UrlGeneratorInterface::ABSOLUTE_URL); + } + + public function supportsNormalization($data, string $format = null, array $context = []) + { + return $data instanceof Person; + } + +} \ No newline at end of file From cce3d06747b34855ca18cd0e445260240cd4a32d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B8=D1=80=D0=B8=D0=BB=D0=BB=20=D0=A5=D1=80=D0=B0?= =?UTF-8?q?=D0=BC=D0=BE=D0=B2?= Date: Thu, 4 Aug 2022 14:30:25 +0300 Subject: [PATCH 06/22] -edit MixedTest.php - the test works with the help of services --- composer.json | 3 +- .../Services/MixedTest/MixedTest.php | 115 ++++++------------ 2 files changed, 40 insertions(+), 78 deletions(-) diff --git a/composer.json b/composer.json index f34ae8dc..fdbf9b3b 100644 --- a/composer.json +++ b/composer.json @@ -42,8 +42,7 @@ "phpunit/phpunit": "9.5.*", "symfony/stopwatch": "5.4.* || 6.*", "roave/security-advisories": "dev-master", - "ext-intl": "*", - "ext-http": "*" + "ext-intl": "*" }, "autoload": { "psr-4": { diff --git a/tests/Integration/Services/MixedTest/MixedTest.php b/tests/Integration/Services/MixedTest/MixedTest.php index 3f0d3c1f..fb7bff7b 100644 --- a/tests/Integration/Services/MixedTest/MixedTest.php +++ b/tests/Integration/Services/MixedTest/MixedTest.php @@ -5,115 +5,77 @@ namespace Bitrix24\SDK\Tests\Integration\Services\MixedTest; use Bitrix24\SDK\Services\CRM\Deal\Service\Deal; +use Bitrix24\SDK\Services\CRM\Deal\Service\DealProductRows; use Bitrix24\SDK\Services\CRM\Product\Service\Product; use Monolog\Test\TestCase; use Bitrix24\SDK\Tests\Integration\Fabric; +use Bitrix24\SDK\Core\Exceptions\TransportException; use http; use Bitrix24\SDK\Core\Exceptions\BaseException; -use Bitrix24\SDK\Core\Exceptions\TransportException; + class MixedTest extends TestCase { protected Deal $dealServices; protected Product $productService; + protected DealProductRows $dealProductRows; /** * @test - * @throws \JsonException + * @throws BaseException + * @throws TransportException */ public function DealWithProductsTest(): void { //Создание продукта 1 $dataProduct1 = array( - 'fields' => array( - 'NAME' => '1С-Битрикс: Управление сайтом - Старт', + + 'NAME' => 'wine', 'CURRENCY_ID' => 'RUB', - 'PRICE' => 4900, - 'SORT' => 4 - ) + 'PRICE' => 475, + 'SORT' => 1 + ); - $queryDataForProduct1 = http_build_query($dataProduct1, 'https://b24-5p29et.bitrix24.ru/rest/1/113n4wi2ocu6rme0/crm.product.add') . PHP_EOL; - $resQueryForProduct1 = file_get_contents("https://b24-5p29et.bitrix24.ru/rest/1/113n4wi2ocu6rme0/crm.product.add?" . $queryDataForProduct1); - $decodeResQueryForProduct1 = json_decode($resQueryForProduct1, true, 512, JSON_THROW_ON_ERROR); - $idForProduct1 = $decodeResQueryForProduct1['result']; - $curl = curl_init(); - curl_setopt_array($curl, array( - CURLOPT_RETURNTRANSFER => 1, - CURLOPT_URL => $resQueryForProduct1, - )); - $response = curl_exec($curl); - curl_close($curl); + $productId1 = $this->productService->add($dataProduct1)->getId(); //Создание продукта 2 $dataProduct2 = array( - 'fields' => array( - 'NAME' => '1С-Битрикс: Управление сайтом - Старт 2 ', - 'CURRENCY_ID' => 'USD', - 'PRICE' => 4500, - 'SORT' => 1, - ) + + 'NAME' => 'vodka', + 'CURRENCY_ID' => 'RUB', + 'PRICE' => 675, + 'SORT' => 4 + ); - $queryDataForProduct2 = http_build_query($dataProduct2, 'https://b24-5p29et.bitrix24.ru/rest/1/113n4wi2ocu6rme0/crm.product.add') . PHP_EOL; - $resQueryForProduct2 = file_get_contents("https://b24-5p29et.bitrix24.ru/rest/1/113n4wi2ocu6rme0/crm.product.add?" . $queryDataForProduct2); - $decodeResQueryForProduct2 = json_decode($resQueryForProduct2, true, 512, JSON_THROW_ON_ERROR); - $idForProduct2 = $decodeResQueryForProduct2['result']; - $curl = curl_init(); - curl_setopt_array($curl, array( - CURLOPT_RETURNTRANSFER => 1, - CURLOPT_URL => $resQueryForProduct2, - )); - $response = curl_exec($curl); - curl_close($curl); + $productId2 = $this->productService->add($dataProduct2)->getId(); + //Создание Сделки $dataDeal1 = array( - 'fields' => array( - 'TITLE' => 'test deal', - 'CURRENCY_ID' => 'USD', - 'OPPORTUNITY' => 500 - ) + 'TITLE' => 'sale of alcohol', + 'CURRENCY_ID' => 'RUB', ); - $queryDataForDeal1 = http_build_query($dataDeal1, 'https://b24-5p29et.bitrix24.ru/rest/1/113n4wi2ocu6rme0/crm.deal.add') . PHP_EOL; - $resQueryForDeal1 = file_get_contents("https://b24-5p29et.bitrix24.ru/rest/1/113n4wi2ocu6rme0/crm.deal.add?" . $queryDataForProduct1); - $decodeResQueryForDeal1 = json_decode($resQueryForDeal1, true, 512, JSON_THROW_ON_ERROR); - $idForDeal1 = $decodeResQueryForDeal1['result']; - $curl = curl_init(); - curl_setopt_array($curl, array( - CURLOPT_RETURNTRANSFER => 1, - CURLOPT_URL => $resQueryForDeal1, - )); - $response = curl_exec($curl); - curl_close($curl); - - //Добавление продукта в сделку - $addProductInDeal = array( - 'id' => $idForDeal1, - 'rows' => array( + $dealId1 = $this->dealServices->add($dataDeal1)->getId(); + + $productsForDeal = array( + [ - 'PRODUCT_ID' => $idForProduct1, - 'PRICE' => 100, + 'PRODUCT_ID' => $productId1, + 'PRICE' => 400, + 'QUANTITY' => 1 ], [ - 'PRODUCT_ID' => $idForProduct2, - 'PRICE' => 100, + 'PRODUCT_ID' => $productId2, + 'PRICE' => 500, + 'QUANTITY' => 1 ] - ) + ); - $queryDataForProductInDeal = http_build_query($addProductInDeal, 'https://b24-5p29et.bitrix24.ru/rest/1/113n4wi2ocu6rme0/crm.deal.productrows.set') . PHP_EOL; - $resQueryForProductInDeal = file_get_contents("https://b24-5p29et.bitrix24.ru/rest/1/113n4wi2ocu6rme0/crm.deal.productrows.set?" . $queryDataForProductInDeal); - $decodeResQueryForDeal1 = json_decode($resQueryForProductInDeal, true, 512, JSON_THROW_ON_ERROR); - $curl = curl_init(); - curl_setopt_array($curl, array( - CURLOPT_RETURNTRANSFER => 1, - CURLOPT_URL => $resQueryForProductInDeal, - )); - $response = curl_exec($curl); - curl_close($curl); - - self::assertNotEmpty($dataProduct1); - self::assertNotEmpty($dataProduct2); - self::assertNotEmpty($dataDeal1); - self::assertNotEmpty($addProductInDeal); + + $addProductInDeal = $this->dealProductRows->set($dealId1, $productsForDeal); + + + self::assertGreaterThanOrEqual(1, $productId1); } @@ -124,5 +86,6 @@ public function setUp(): void { $this->productService = Fabric::getServiceBuilder()->getCRMScope()->product(); $this->dealServices = Fabric::getServiceBuilder()->getCRMScope()->deal(); + $this->dealProductRows = Fabric::getServiceBuilder()->getCRMScope()->dealProductRows(); } } From 4feb8b4f14553585f4802dbce518bb6e50f6754c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B8=D1=80=D0=B8=D0=BB=D0=BB=20=D0=A5=D1=80=D0=B0?= =?UTF-8?q?=D0=BC=D0=BE=D0=B2?= Date: Thu, 4 Aug 2022 17:15:26 +0300 Subject: [PATCH 07/22] -update test --- .../Integration/Services/MixedTest/MixedTest.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/tests/Integration/Services/MixedTest/MixedTest.php b/tests/Integration/Services/MixedTest/MixedTest.php index fb7bff7b..b84c223a 100644 --- a/tests/Integration/Services/MixedTest/MixedTest.php +++ b/tests/Integration/Services/MixedTest/MixedTest.php @@ -10,7 +10,6 @@ use Monolog\Test\TestCase; use Bitrix24\SDK\Tests\Integration\Fabric; use Bitrix24\SDK\Core\Exceptions\TransportException; -use http; use Bitrix24\SDK\Core\Exceptions\BaseException; @@ -31,8 +30,8 @@ public function DealWithProductsTest(): void $dataProduct1 = array( 'NAME' => 'wine', - 'CURRENCY_ID' => 'RUB', - 'PRICE' => 475, + 'CURRENCY_ID' => 'USD', + 'PRICE' => 100, 'SORT' => 1 ); @@ -42,8 +41,8 @@ public function DealWithProductsTest(): void $dataProduct2 = array( 'NAME' => 'vodka', - 'CURRENCY_ID' => 'RUB', - 'PRICE' => 675, + 'CURRENCY_ID' => 'USD', + 'PRICE' => 10, 'SORT' => 4 ); @@ -61,7 +60,7 @@ public function DealWithProductsTest(): void [ 'PRODUCT_ID' => $productId1, - 'PRICE' => 400, + 'PRICE' => 5000, 'QUANTITY' => 1 ], [ @@ -75,7 +74,11 @@ public function DealWithProductsTest(): void $addProductInDeal = $this->dealProductRows->set($dealId1, $productsForDeal); + self::assertEquals($productsForDeal[0]['PRICE'],$this->dealProductRows->get($dealId1)->getCoreResponse()->getResponseData()->getResult()->getResultData()[0]['PRICE']); + self::assertEquals($productsForDeal[1]['PRICE'],$this->dealProductRows->get($dealId1)->getCoreResponse()->getResponseData()->getResult()->getResultData()[1]['PRICE']); self::assertGreaterThanOrEqual(1, $productId1); + self::assertGreaterThanOrEqual(1, $productId2); + self::assertTrue((bool)$addProductInDeal); } From ea30810b1678b96eee72c3a6f717655fcf2bdd18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B8=D1=80=D0=B8=D0=BB=D0=BB=20=D0=A5=D1=80=D0=B0?= =?UTF-8?q?=D0=BC=D0=BE=D0=B2?= Date: Sat, 6 Aug 2022 09:00:16 +0300 Subject: [PATCH 08/22] -edit normalizer(but I don't understand how to use it) --- .../Services/TestPerson/PersonNormalizer.php | 29 ++++++++++--------- tests/Unit/Services/TestPerson/PersonTest.php | 21 ++++++++++++++ 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/tests/Unit/Services/TestPerson/PersonNormalizer.php b/tests/Unit/Services/TestPerson/PersonNormalizer.php index 8368beb0..c8b386dc 100644 --- a/tests/Unit/Services/TestPerson/PersonNormalizer.php +++ b/tests/Unit/Services/TestPerson/PersonNormalizer.php @@ -5,35 +5,36 @@ namespace Bitrix24\SDK\Tests\Unit\Services\TestPerson; use Bitrix24\SDK\Tests\Unit\Services\TestPerson\Person; -use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface; +use Symfony\Component\Serializer\Normalizer\NormalizerInterface; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; -class PersonNormalizer implements ContextAwareNormalizerInterface +class PersonNormalizer implements NormalizerInterface, ContextAwareNormalizerInterface { - private UrlGeneratorInterface $router; private ObjectNormalizer $normalizer; - protected Person $person; - public function __construct(UrlGeneratorInterface $router, ObjectNormalizer $normalizer) + public function __construct( ObjectNormalizer $normalizer) { - $this->router = $router; $this->normalizer = $normalizer; } - public function normalize($person, string $format = null, array $context = []) + /** + * @param Person $object + * @throws \Symfony\Component\Serializer\Exception\ExceptionInterface + */ + public function normalize($object, string $format = null, array $context = []):array { - $data = $this->normalizer->normalize($person, $format, $context); - var_dump($data); - // Здесь, добавьте, измените или удалите некоторые данные: - return $this->router->generate('topic_show', [ - 'name' => $person->getName(), - ], UrlGeneratorInterface::ABSOLUTE_URL); + $data = $this->normalizer->normalize($object,$format,$context); + return [ + 'name' => 'new_name', + 'lastname' => 'new_lastname', + ]; } - public function supportsNormalization($data, string $format = null, array $context = []) + public function supportsNormalization($data, string $format = null, array $context = []):bool { return $data instanceof Person; } + } \ No newline at end of file diff --git a/tests/Unit/Services/TestPerson/PersonTest.php b/tests/Unit/Services/TestPerson/PersonTest.php index 02154645..5d0b354f 100644 --- a/tests/Unit/Services/TestPerson/PersonTest.php +++ b/tests/Unit/Services/TestPerson/PersonTest.php @@ -16,6 +16,7 @@ use Money\Formatter\AggregateMoneyFormatter; use Money\Formatter\IntlMoneyFormatter; use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; +use Bitrix24\SDK\Tests\Unit\Services\TestPerson\PersonNormalizer; class PersonTest extends TestCase @@ -67,6 +68,26 @@ public function DeserializeTest():void{ self::assertNotEmpty($person); } + /** + *@test + */ + public function NormalizePersonTest():void + { + $person = new Person(); + $encoders = [new XmlEncoder(), new JsonEncoder()]; + $normalizers = [new PersonNormalizer()]; + + $serializer = new Serializer($normalizers, $encoders); + + $person->setName('Kirill'); + $person->setLastName('Khramov'); + + $jsonContent = $serializer->serialize($person, 'json'); + self::assertNotEmpty($jsonContent); + echo $jsonContent; + + } + /** * @test * @throws \Symfony\Component\Serializer\Exception\ExceptionInterface From 568a508c4beda210adf0ec1824c5e54a327ec4b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B8=D1=80=D0=B8=D0=BB=D0=BB=20=D0=A5=D1=80=D0=B0?= =?UTF-8?q?=D0=BC=D0=BE=D0=B2?= Date: Wed, 10 Aug 2022 18:24:47 +0300 Subject: [PATCH 09/22] -add money result in AbstactCrmItem (not work) --- .../CRM/Common/Result/AbstractCrmItem.php | 5 ++-- .../Deal/Result/DealProductRowItemResult.php | 26 +++++++++++-------- .../Deal/Result/DealProductRowItemsResult.php | 10 +++++++ .../CRM/Deal/Service/DealProductRows.php | 4 ++- .../CRM/Deal/Service/DealProductRowsTest.php | 8 ++++++ 5 files changed, 39 insertions(+), 14 deletions(-) diff --git a/src/Services/CRM/Common/Result/AbstractCrmItem.php b/src/Services/CRM/Common/Result/AbstractCrmItem.php index 0737315f..71d7b341 100644 --- a/src/Services/CRM/Common/Result/AbstractCrmItem.php +++ b/src/Services/CRM/Common/Result/AbstractCrmItem.php @@ -7,6 +7,7 @@ use Bitrix24\SDK\Core\Result\AbstractItem; use Bitrix24\SDK\Services\CRM\Userfield\Exceptions\UserfieldNotFoundException; use DateTimeImmutable; +use Money\Money; class AbstractCrmItem extends AbstractItem { @@ -30,6 +31,8 @@ public function __get($offset) case 'LEAD_ID': case 'CONTACT_ID': case 'QUOTE_ID': + // productRow + case 'OWNER_ID': if ($this->data[$offset] !== '' && $this->data[$offset] !== null) { return (int)$this->data[$offset]; } @@ -39,9 +42,7 @@ public function __get($offset) if ($this->data[$offset] !== '' && $this->data[$offset] !== null && $this->data[$offset] !== '0') { return (int)$this->data[$offset]; } - return null; - // contact case 'EXPORT': case 'HAS_PHONE': diff --git a/src/Services/CRM/Deal/Result/DealProductRowItemResult.php b/src/Services/CRM/Deal/Result/DealProductRowItemResult.php index 5632778b..e0ddb29a 100644 --- a/src/Services/CRM/Deal/Result/DealProductRowItemResult.php +++ b/src/Services/CRM/Deal/Result/DealProductRowItemResult.php @@ -5,30 +5,34 @@ namespace Bitrix24\SDK\Services\CRM\Deal\Result; use Bitrix24\SDK\Core\Result\AbstractItem; +use Bitrix24\SDK\Services\CRM\Common\Result\AbstractCrmItem; +use Money\Money; /** * Class DealProductRowItemResult * - * @property-read int $ID - * @property-read int $OWNER_ID + * @property-read int $ID + * @property-read int $OWNER_ID * @property-read string $OWNER_TYPE - * @property-read int $PRODUCT_ID + * @property-read int $PRODUCT_ID * @property-read string $PRODUCT_NAME - * @property-read string $PRICE - * @property-read string $PRICE_EXCLUSIVE - * @property-read string $PRICE_NETTO - * @property-read string $PRICE_BRUTTO + * @property-read Money $PRICE + * @property-read Money $PRICE_EXCLUSIVE + * @property-read Money $PRICE_NETTO + * @property-read Money $PRICE_BRUTTO * @property-read string $QUANTITY - * @property-read int $DISCOUNT_TYPE_ID + * @property-read int $DISCOUNT_TYPE_ID * @property-read string $DISCOUNT_RATE * @property-read string $DISCOUNT_SUM * @property-read string $TAX_RATE * @property-read string $TAX_INCLUDED * @property-read string $CUSTOMIZED - * @property-read int $MEASURE_CODE + * @property-read int $MEASURE_CODE * @property-read string $MEASURE_NAME - * @property-read int $SORT + * @property-read int $RESERVE_ID + * @property-read int $RESERVE_QUANTITY + * @property-read int $SORT */ -class DealProductRowItemResult extends AbstractItem +class DealProductRowItemResult extends AbstractCrmItem { } \ No newline at end of file diff --git a/src/Services/CRM/Deal/Result/DealProductRowItemsResult.php b/src/Services/CRM/Deal/Result/DealProductRowItemsResult.php index 7104799b..125295a5 100644 --- a/src/Services/CRM/Deal/Result/DealProductRowItemsResult.php +++ b/src/Services/CRM/Deal/Result/DealProductRowItemsResult.php @@ -6,7 +6,9 @@ namespace Bitrix24\SDK\Services\CRM\Deal\Result; use Bitrix24\SDK\Core\Exceptions\BaseException; +use Bitrix24\SDK\Core\Response\Response; use Bitrix24\SDK\Core\Result\AbstractResult; +use Money\Currency; /** * Class DealProductRowItemsResult @@ -15,6 +17,14 @@ */ class DealProductRowItemsResult extends AbstractResult { + private Currency $currency; + + public function __construct(Response $coreResponse,Currency $currency) + { + parent::__construct($coreResponse); + $this->currency = $currency; + } + /** * @return DealProductRowItemResult[] * @throws BaseException diff --git a/src/Services/CRM/Deal/Service/DealProductRows.php b/src/Services/CRM/Deal/Service/DealProductRows.php index 3704bd20..c3867819 100644 --- a/src/Services/CRM/Deal/Service/DealProductRows.php +++ b/src/Services/CRM/Deal/Service/DealProductRows.php @@ -9,6 +9,7 @@ use Bitrix24\SDK\Core\Result\UpdatedItemResult; use Bitrix24\SDK\Services\AbstractService; use Bitrix24\SDK\Services\CRM\Deal\Result\DealProductRowItemsResult; +use Money\Currency; /** * Class DealProductRows @@ -36,7 +37,8 @@ public function get(int $dealId): DealProductRowItemsResult [ 'id' => $dealId, ] - ) + ), + new Currency('RUB') ); } diff --git a/tests/Integration/Services/CRM/Deal/Service/DealProductRowsTest.php b/tests/Integration/Services/CRM/Deal/Service/DealProductRowsTest.php index 0b45ad41..f9afb1c7 100644 --- a/tests/Integration/Services/CRM/Deal/Service/DealProductRowsTest.php +++ b/tests/Integration/Services/CRM/Deal/Service/DealProductRowsTest.php @@ -9,6 +9,7 @@ use Bitrix24\SDK\Services\CRM\Deal\Service\Deal; use Bitrix24\SDK\Services\CRM\Deal\Service\DealProductRows; use Bitrix24\SDK\Tests\Integration\Fabric; +use Money\Money; use PHPUnit\Framework\TestCase; /** @@ -28,6 +29,7 @@ class DealProductRowsTest extends TestCase */ public function testSet(): void { + $newDealId = $this->dealService->add(['TITLE' => 'test deal'])->getId(); $this::assertCount(0, $this->dealProductRowsService->get($newDealId)->getProductRows()); $this::assertTrue( @@ -41,6 +43,12 @@ public function testSet(): void )->isSuccess() ); $this::assertCount(1, $this->dealProductRowsService->get($newDealId)->getProductRows()); + $mas = $this->dealProductRowsService->get($newDealId)->getProductRows()[0]; + var_dump($mas); + var_dump($mas->ID); + var_dump($mas->OWNER_ID); + var_dump($mas->QUANTITY); + var_dump($mas->PRICE); } /** From 385de8e9e0a0a4c90a35279703e65c35a4c94630 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B8=D1=80=D0=B8=D0=BB=D0=BB=20=D0=A5=D1=80=D0=B0?= =?UTF-8?q?=D0=BC=D0=BE=D0=B2?= Date: Sat, 13 Aug 2022 09:21:54 +0300 Subject: [PATCH 10/22] -add money result in AbstactCrmItem (work?) -edit test --- composer.json | 6 +++--- src/Services/CRM/Common/Result/AbstractCrmItem.php | 6 ++++++ .../CRM/Deal/Service/DealProductRowsTest.php | 14 ++++++++++---- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index fdbf9b3b..7485fa08 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,8 @@ "moneyphp/money": "3.* || 4.*", "symfony/serializer": "6.*||^6.1", "symfony/routing": "^4.4|^5.3|^6.0", - "symfony/property-access": "6.* || 6.1.*" + "symfony/property-access": "6.* || 6.1.*", + "ext-intl": "*" }, "require-dev": { "monolog/monolog": "2.1.*", @@ -41,8 +42,7 @@ "phpstan/phpstan": "1.*", "phpunit/phpunit": "9.5.*", "symfony/stopwatch": "5.4.* || 6.*", - "roave/security-advisories": "dev-master", - "ext-intl": "*" + "roave/security-advisories": "dev-master" }, "autoload": { "psr-4": { diff --git a/src/Services/CRM/Common/Result/AbstractCrmItem.php b/src/Services/CRM/Common/Result/AbstractCrmItem.php index 71d7b341..6be40bfd 100644 --- a/src/Services/CRM/Common/Result/AbstractCrmItem.php +++ b/src/Services/CRM/Common/Result/AbstractCrmItem.php @@ -7,6 +7,7 @@ use Bitrix24\SDK\Core\Result\AbstractItem; use Bitrix24\SDK\Services\CRM\Userfield\Exceptions\UserfieldNotFoundException; use DateTimeImmutable; +use Money\Currency; use Money\Money; class AbstractCrmItem extends AbstractItem @@ -50,6 +51,11 @@ public function __get($offset) case 'HAS_IMOL': case 'OPENED': // deal + case 'PRICE': + if ($this->data[$offset] !== '' && $this->data[$offset] !== null) { + return new Money($this->data[$offset],new Currency('RUB')); + } + return null; case 'IS_MANUAL_OPPORTUNITY': case 'CLOSED': case 'IS_NEW': diff --git a/tests/Integration/Services/CRM/Deal/Service/DealProductRowsTest.php b/tests/Integration/Services/CRM/Deal/Service/DealProductRowsTest.php index f9afb1c7..bba2891b 100644 --- a/tests/Integration/Services/CRM/Deal/Service/DealProductRowsTest.php +++ b/tests/Integration/Services/CRM/Deal/Service/DealProductRowsTest.php @@ -9,6 +9,9 @@ use Bitrix24\SDK\Services\CRM\Deal\Service\Deal; use Bitrix24\SDK\Services\CRM\Deal\Service\DealProductRows; use Bitrix24\SDK\Tests\Integration\Fabric; +use Money\Currencies\ISOCurrencies; +use Money\Currency; +use Money\Formatter\DecimalMoneyFormatter; use Money\Money; use PHPUnit\Framework\TestCase; @@ -30,6 +33,10 @@ class DealProductRowsTest extends TestCase public function testSet(): void { + $callCosts = new Money(1000, new Currency('RUB')); + $currencies = new ISOCurrencies(); + + $moneyFormatter = new DecimalMoneyFormatter($currencies); $newDealId = $this->dealService->add(['TITLE' => 'test deal'])->getId(); $this::assertCount(0, $this->dealProductRowsService->get($newDealId)->getProductRows()); $this::assertTrue( @@ -38,6 +45,7 @@ public function testSet(): void [ [ 'PRODUCT_NAME' => 'qqqq', + 'PRICE'=> $moneyFormatter->format($callCosts), ], ] )->isSuccess() @@ -45,10 +53,8 @@ public function testSet(): void $this::assertCount(1, $this->dealProductRowsService->get($newDealId)->getProductRows()); $mas = $this->dealProductRowsService->get($newDealId)->getProductRows()[0]; var_dump($mas); - var_dump($mas->ID); - var_dump($mas->OWNER_ID); - var_dump($mas->QUANTITY); - var_dump($mas->PRICE); + $price = $mas->PRICE; + var_dump($price); } /** From 67b0b486f4fb2be6ea9f4680e2e9672bcfb97a03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B8=D1=80=D0=B8=D0=BB=D0=BB=20=D0=A5=D1=80=D0=B0?= =?UTF-8?q?=D0=BC=D0=BE=D0=B2?= Date: Wed, 17 Aug 2022 15:04:42 +0300 Subject: [PATCH 11/22] -edit money result in AbstactCrmItem (work?) -edit test --- src/Services/CRM/Common/Result/AbstractCrmItem.php | 4 +++- .../Services/CRM/Deal/Service/DealProductRowsTest.php | 8 ++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Services/CRM/Common/Result/AbstractCrmItem.php b/src/Services/CRM/Common/Result/AbstractCrmItem.php index 6be40bfd..d2997b5a 100644 --- a/src/Services/CRM/Common/Result/AbstractCrmItem.php +++ b/src/Services/CRM/Common/Result/AbstractCrmItem.php @@ -52,8 +52,10 @@ public function __get($offset) case 'OPENED': // deal case 'PRICE': + if ($this->data[$offset] !== '' && $this->data[$offset] !== null) { - return new Money($this->data[$offset],new Currency('RUB')); + $var = $this->data[$offset] * 100; + return new Money((string)$var,new Currency('RUB')); } return null; case 'IS_MANUAL_OPPORTUNITY': diff --git a/tests/Integration/Services/CRM/Deal/Service/DealProductRowsTest.php b/tests/Integration/Services/CRM/Deal/Service/DealProductRowsTest.php index bba2891b..35696eca 100644 --- a/tests/Integration/Services/CRM/Deal/Service/DealProductRowsTest.php +++ b/tests/Integration/Services/CRM/Deal/Service/DealProductRowsTest.php @@ -33,7 +33,7 @@ class DealProductRowsTest extends TestCase public function testSet(): void { - $callCosts = new Money(1000, new Currency('RUB')); + $callCosts = new Money(1050, new Currency('RUB')); $currencies = new ISOCurrencies(); $moneyFormatter = new DecimalMoneyFormatter($currencies); @@ -52,9 +52,9 @@ public function testSet(): void ); $this::assertCount(1, $this->dealProductRowsService->get($newDealId)->getProductRows()); $mas = $this->dealProductRowsService->get($newDealId)->getProductRows()[0]; - var_dump($mas); - $price = $mas->PRICE; - var_dump($price); + $money = $moneyFormatter->format($mas->PRICE); + + } /** From ea8484193af3c9b165af6b4a80792b7e7c208a38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B8=D1=80=D0=B8=D0=BB=D0=BB=20=D0=A5=D1=80=D0=B0?= =?UTF-8?q?=D0=BC=D0=BE=D0=B2?= Date: Fri, 19 Aug 2022 18:50:06 +0300 Subject: [PATCH 12/22] -edit test -add improvements to the method DEAL_PRODUCT_ROW so that it can use the currency --- .../CRM/Common/Result/AbstractCrmItem.php | 6 ++- .../Deal/Result/DealProductRowItemResult.php | 26 ++++++++++++- .../Deal/Result/DealProductRowItemsResult.php | 2 +- .../CRM/Deal/Service/DealProductRows.php | 39 ++++++++++++++++++- .../CRM/Deal/Service/DealProductRowsTest.php | 13 +++++-- 5 files changed, 78 insertions(+), 8 deletions(-) diff --git a/src/Services/CRM/Common/Result/AbstractCrmItem.php b/src/Services/CRM/Common/Result/AbstractCrmItem.php index d2997b5a..16a8c501 100644 --- a/src/Services/CRM/Common/Result/AbstractCrmItem.php +++ b/src/Services/CRM/Common/Result/AbstractCrmItem.php @@ -21,6 +21,10 @@ class AbstractCrmItem extends AbstractItem */ public function __get($offset) { + + var_dump('Зашли в AbstractCrmItem'); + var_dump(__METHOD__); + var_dump($offset); // todo унести в отдельный класс и покрыть тестами // приведение полей к реальным типам данных для основных сущностей CRM switch ($offset) { @@ -55,7 +59,7 @@ public function __get($offset) if ($this->data[$offset] !== '' && $this->data[$offset] !== null) { $var = $this->data[$offset] * 100; - return new Money((string)$var,new Currency('RUB')); + return new Money((string)$var,new Currency('USD')); } return null; case 'IS_MANUAL_OPPORTUNITY': diff --git a/src/Services/CRM/Deal/Result/DealProductRowItemResult.php b/src/Services/CRM/Deal/Result/DealProductRowItemResult.php index e0ddb29a..b0d7af3c 100644 --- a/src/Services/CRM/Deal/Result/DealProductRowItemResult.php +++ b/src/Services/CRM/Deal/Result/DealProductRowItemResult.php @@ -4,8 +4,8 @@ namespace Bitrix24\SDK\Services\CRM\Deal\Result; -use Bitrix24\SDK\Core\Result\AbstractItem; use Bitrix24\SDK\Services\CRM\Common\Result\AbstractCrmItem; +use Money\Currency; use Money\Money; /** @@ -35,4 +35,28 @@ */ class DealProductRowItemResult extends AbstractCrmItem { + private Currency $currency; + + /** + * @param \Money\Currency $currency + */ + public function __construct(array $data,Currency $currency) + { + parent::__construct($data); + $this->currency = $currency; + } + + public function __get($offset) + { + + var_dump('Cтрока сделки'); + var_dump(__METHOD__); + var_dump($offset); + var_dump($this->currency->getCode()); + + return parent::__get($offset); + + + } + } \ No newline at end of file diff --git a/src/Services/CRM/Deal/Result/DealProductRowItemsResult.php b/src/Services/CRM/Deal/Result/DealProductRowItemsResult.php index 125295a5..64bbbcc2 100644 --- a/src/Services/CRM/Deal/Result/DealProductRowItemsResult.php +++ b/src/Services/CRM/Deal/Result/DealProductRowItemsResult.php @@ -33,7 +33,7 @@ public function getProductRows(): array { $res = []; foreach ($this->getCoreResponse()->getResponseData()->getResult()->getResultData() as $productRow) { - $res[] = new DealProductRowItemResult($productRow); + $res[] = new DealProductRowItemResult($productRow,$this->currency); } return $res; diff --git a/src/Services/CRM/Deal/Service/DealProductRows.php b/src/Services/CRM/Deal/Service/DealProductRows.php index c3867819..40fe4976 100644 --- a/src/Services/CRM/Deal/Service/DealProductRows.php +++ b/src/Services/CRM/Deal/Service/DealProductRows.php @@ -9,6 +9,7 @@ use Bitrix24\SDK\Core\Result\UpdatedItemResult; use Bitrix24\SDK\Services\AbstractService; use Bitrix24\SDK\Services\CRM\Deal\Result\DealProductRowItemsResult; +use Bitrix24\SDK\Services\CRM\Deal\Result\DealResult; use Money\Currency; /** @@ -29,7 +30,7 @@ class DealProductRows extends AbstractService * @throws BaseException * @throws TransportException */ - public function get(int $dealId): DealProductRowItemsResult + public function getStupid(int $dealId, Currency $currency): DealProductRowItemsResult { return new DealProductRowItemsResult( $this->core->call( @@ -38,10 +39,44 @@ public function get(int $dealId): DealProductRowItemsResult 'id' => $dealId, ] ), - new Currency('RUB') + $currency ); } + public function getSmart(int $dealId): DealProductRowItemsResult + { + $deal = new DealResult($this->core->call('crm.deal.get', ['id' => $dealId])); + $currency = new Currency($deal->deal()->CURRENCY_ID); + return new DealProductRowItemsResult( + $this->core->call( + 'crm.deal.productrows.get', + [ + 'id' => $dealId, + ] + ), + $currency + ); + } + public function getSuperSmart(int $dealId): DealProductRowItemsResult + { + /* $deal = new DealResult($this->core->call('crm.deal.get', ['id' => $dealId])); + $currency = new Currency($deal->deal()->CURRENCY_ID); + return new DealProductRowItemsResult( + $this->core->call( + 'crm.deal.productrows.get', + [ + 'id' => $dealId, + ] + ), + $currency + );*/ + // todo Получить сделку и табличную часть за один запрос к Api + } + + public function getSuperSuperSmart(){ + // todo Метод позволяет экономить один запрос если мы уже знаем валюту, а если не знаем то делает этот запрос. + } + /** * Creates or updates product entries inside the specified deal. * diff --git a/tests/Integration/Services/CRM/Deal/Service/DealProductRowsTest.php b/tests/Integration/Services/CRM/Deal/Service/DealProductRowsTest.php index 35696eca..7816fa3e 100644 --- a/tests/Integration/Services/CRM/Deal/Service/DealProductRowsTest.php +++ b/tests/Integration/Services/CRM/Deal/Service/DealProductRowsTest.php @@ -64,19 +64,26 @@ public function testSet(): void */ public function testGet(): void { - $newDealId = $this->dealService->add(['TITLE' => 'test deal'])->getId(); - $this::assertCount(0, $this->dealProductRowsService->get($newDealId)->getProductRows()); + $callCosts = new Money(1050, new Currency('EUR')); + $currencies = new ISOCurrencies(); + + $moneyFormatter = new DecimalMoneyFormatter($currencies); + $newDealId = $this->dealService->add(['TITLE' => 'test deal','CURRENCY_ID'=>$callCosts->getCurrency()->getCode()])->getId(); $this::assertTrue( $this->dealProductRowsService->set( $newDealId, [ [ 'PRODUCT_NAME' => 'qqqq', + 'PRICE'=> $moneyFormatter->format($callCosts), ], ] )->isSuccess() ); - $this::assertCount(1, $this->dealProductRowsService->get($newDealId)->getProductRows()); + $res = $this->dealProductRowsService->getSmart($newDealId); + foreach ($res->getProductRows() as $productRow){ + var_dump($productRow->PRICE); + } } public function setUp(): void From 53b855f8dcf8df022061e945cccf69490cdfc4cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B8=D1=80=D0=B8=D0=BB=D0=BB=20=D0=A5=D1=80=D0=B0?= =?UTF-8?q?=D0=BC=D0=BE=D0=B2?= Date: Wed, 24 Aug 2022 16:58:47 +0300 Subject: [PATCH 13/22] -add method getSuperSmart for DealProductRows.php method 2in1(crm.deal.get,crm.deal.productrows.get) --- .../CRM/Common/Result/AbstractCrmItem.php | 5 +-- .../Deal/Result/DealProductRowItemResult.php | 10 +---- .../CRM/Deal/Service/DealProductRows.php | 44 +++++++++++-------- .../CRM/Deal/Service/DealProductRowsTest.php | 2 +- 4 files changed, 29 insertions(+), 32 deletions(-) diff --git a/src/Services/CRM/Common/Result/AbstractCrmItem.php b/src/Services/CRM/Common/Result/AbstractCrmItem.php index 16a8c501..5f75cb10 100644 --- a/src/Services/CRM/Common/Result/AbstractCrmItem.php +++ b/src/Services/CRM/Common/Result/AbstractCrmItem.php @@ -22,9 +22,7 @@ class AbstractCrmItem extends AbstractItem public function __get($offset) { - var_dump('Зашли в AbstractCrmItem'); - var_dump(__METHOD__); - var_dump($offset); + // todo унести в отдельный класс и покрыть тестами // приведение полей к реальным типам данных для основных сущностей CRM switch ($offset) { @@ -56,7 +54,6 @@ public function __get($offset) case 'OPENED': // deal case 'PRICE': - if ($this->data[$offset] !== '' && $this->data[$offset] !== null) { $var = $this->data[$offset] * 100; return new Money((string)$var,new Currency('USD')); diff --git a/src/Services/CRM/Deal/Result/DealProductRowItemResult.php b/src/Services/CRM/Deal/Result/DealProductRowItemResult.php index b0d7af3c..0f3f2716 100644 --- a/src/Services/CRM/Deal/Result/DealProductRowItemResult.php +++ b/src/Services/CRM/Deal/Result/DealProductRowItemResult.php @@ -38,9 +38,10 @@ class DealProductRowItemResult extends AbstractCrmItem private Currency $currency; /** + * @param array $data * @param \Money\Currency $currency */ - public function __construct(array $data,Currency $currency) + public function __construct(array $data, Currency $currency) { parent::__construct($data); $this->currency = $currency; @@ -48,15 +49,8 @@ public function __construct(array $data,Currency $currency) public function __get($offset) { - - var_dump('Cтрока сделки'); - var_dump(__METHOD__); - var_dump($offset); - var_dump($this->currency->getCode()); - return parent::__get($offset); - } } \ No newline at end of file diff --git a/src/Services/CRM/Deal/Service/DealProductRows.php b/src/Services/CRM/Deal/Service/DealProductRows.php index 40fe4976..fcfd21ab 100644 --- a/src/Services/CRM/Deal/Service/DealProductRows.php +++ b/src/Services/CRM/Deal/Service/DealProductRows.php @@ -25,10 +25,10 @@ class DealProductRows extends AbstractService * @link https://training.bitrix24.com/rest_help/crm/deals/crm_deal_productrows_get.php * * @param int $dealId - * + * @param \Money\Currency $currency * @return DealProductRowItemsResult - * @throws BaseException - * @throws TransportException + * @throws \Bitrix24\SDK\Core\Exceptions\BaseException + * @throws \Bitrix24\SDK\Core\Exceptions\TransportException */ public function getStupid(int $dealId, Currency $currency): DealProductRowItemsResult { @@ -39,10 +39,14 @@ public function getStupid(int $dealId, Currency $currency): DealProductRowItemsR 'id' => $dealId, ] ), - $currency + $currency ); } + /** + * @throws \Bitrix24\SDK\Core\Exceptions\TransportException + * @throws \Bitrix24\SDK\Core\Exceptions\BaseException + */ public function getSmart(int $dealId): DealProductRowItemsResult { $deal = new DealResult($this->core->call('crm.deal.get', ['id' => $dealId])); @@ -57,25 +61,27 @@ public function getSmart(int $dealId): DealProductRowItemsResult $currency ); } + + /** + * @throws \Bitrix24\SDK\Core\Exceptions\BaseException + */ public function getSuperSmart(int $dealId): DealProductRowItemsResult { - /* $deal = new DealResult($this->core->call('crm.deal.get', ['id' => $dealId])); - $currency = new Currency($deal->deal()->CURRENCY_ID); - return new DealProductRowItemsResult( - $this->core->call( - 'crm.deal.productrows.get', - [ - 'id' => $dealId, - ] - ), - $currency - );*/ + $data = $this->core->call('batch',array( + 'halt'=>0, + 'cmd'=>array( + $deal = new DealResult($this->core->call('crm.deal.get', ['id' => $dealId])), + $dealProductRow = new DealProductRowItemsResult($this->core->call('crm.deal.productrows.get',['id' => $dealId,]),new Currency($deal->deal()->CURRENCY_ID)) + ) + )); + return $dealProductRow; // todo Получить сделку и табличную часть за один запрос к Api } - public function getSuperSuperSmart(){ - // todo Метод позволяет экономить один запрос если мы уже знаем валюту, а если не знаем то делает этот запрос. - } + public function getSuperSuperSmart() + { + // todo Метод позволяет экономить один запрос если мы уже знаем валюту, а если не знаем то делает этот запрос. + } /** * Creates or updates product entries inside the specified deal. @@ -115,7 +121,7 @@ public function set(int $dealId, array $productRows): UpdatedItemResult $this->core->call( 'crm.deal.productrows.set', [ - 'id' => $dealId, + 'id' => $dealId, 'rows' => $productRows, ] ) diff --git a/tests/Integration/Services/CRM/Deal/Service/DealProductRowsTest.php b/tests/Integration/Services/CRM/Deal/Service/DealProductRowsTest.php index 7816fa3e..cf53c493 100644 --- a/tests/Integration/Services/CRM/Deal/Service/DealProductRowsTest.php +++ b/tests/Integration/Services/CRM/Deal/Service/DealProductRowsTest.php @@ -80,7 +80,7 @@ public function testGet(): void ] )->isSuccess() ); - $res = $this->dealProductRowsService->getSmart($newDealId); + $res = $this->dealProductRowsService->getSuperSmart($newDealId); foreach ($res->getProductRows() as $productRow){ var_dump($productRow->PRICE); } From fdcbb218a16a4f56ac75e024166a60d40d747b06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B8=D1=80=D0=B8=D0=BB=D0=BB=20=D0=A5=D1=80=D0=B0?= =?UTF-8?q?=D0=BC=D0=BE=D0=B2?= Date: Mon, 29 Aug 2022 12:50:29 +0300 Subject: [PATCH 14/22] =?UTF-8?q?-=D1=82=D0=B5=D0=BF=D0=B5=D1=80=D1=8C=20?= =?UTF-8?q?=D0=B2=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B5=20AbstarctCrmItem?= =?UTF-8?q?=20=D0=BC=D0=BE=D0=B6=D0=BD=D0=BE=20=D0=BE=D0=B1=D1=80=D0=B0?= =?UTF-8?q?=D1=89=D0=B0=D1=82=D1=8C=D1=81=D1=8F=20=D0=BA=20currency,=20?= =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B8=D0=BB=D1=81=D1=8F=20=D1=8D=D1=82=D0=BE?= =?UTF-8?q?=20=D0=BF=D1=83=D1=82=D0=B5=D0=BC=20=D0=B7=D0=B0=D0=BC=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D0=BE=D0=B1=D0=BB=D0=B0=D1=81=D1=82=D0=B8=20?= =?UTF-8?q?=D0=B2=D0=B8=D0=B4=D0=B8=D0=BC=D0=BE=D1=81=D1=82=D0=B8=20=D1=81?= =?UTF-8?q?=20private=20=D0=BD=D0=B0=20protected=20=D0=B2=20DealProductRow?= =?UTF-8?q?ItemResult.php.(=D0=A2=D0=B0=D0=BA=20=D0=BC=D0=BE=D0=B6=D0=BD?= =?UTF-8?q?=D0=BE=20=D0=B4=D0=B5=D0=BB=D0=B0=D1=82=D1=8C=20=3F)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CRM/Common/Result/AbstractCrmItem.php | 4 ++- .../Deal/Result/DealProductRowItemResult.php | 3 +- .../CRM/Deal/Service/DealProductRows.php | 22 +++++++++---- .../CRM/Deal/Service/DealProductRowsTest.php | 33 ++++++++++++++++++- 4 files changed, 51 insertions(+), 11 deletions(-) diff --git a/src/Services/CRM/Common/Result/AbstractCrmItem.php b/src/Services/CRM/Common/Result/AbstractCrmItem.php index 5f75cb10..abab2784 100644 --- a/src/Services/CRM/Common/Result/AbstractCrmItem.php +++ b/src/Services/CRM/Common/Result/AbstractCrmItem.php @@ -5,6 +5,7 @@ namespace Bitrix24\SDK\Services\CRM\Common\Result; use Bitrix24\SDK\Core\Result\AbstractItem; +use Bitrix24\SDK\Services\CRM\Deal\Result\DealProductRowItemsResult; use Bitrix24\SDK\Services\CRM\Userfield\Exceptions\UserfieldNotFoundException; use DateTimeImmutable; use Money\Currency; @@ -19,6 +20,7 @@ class AbstractCrmItem extends AbstractItem * * @return bool|\DateTimeImmutable|int|mixed|null */ + public function __get($offset) { @@ -56,7 +58,7 @@ public function __get($offset) case 'PRICE': if ($this->data[$offset] !== '' && $this->data[$offset] !== null) { $var = $this->data[$offset] * 100; - return new Money((string)$var,new Currency('USD')); + return new Money((string)$var,new Currency($this->currency->getCode())); } return null; case 'IS_MANUAL_OPPORTUNITY': diff --git a/src/Services/CRM/Deal/Result/DealProductRowItemResult.php b/src/Services/CRM/Deal/Result/DealProductRowItemResult.php index 0f3f2716..540d161f 100644 --- a/src/Services/CRM/Deal/Result/DealProductRowItemResult.php +++ b/src/Services/CRM/Deal/Result/DealProductRowItemResult.php @@ -35,7 +35,7 @@ */ class DealProductRowItemResult extends AbstractCrmItem { - private Currency $currency; + protected Currency $currency; /** * @param array $data @@ -50,7 +50,6 @@ public function __construct(array $data, Currency $currency) public function __get($offset) { return parent::__get($offset); - } } \ No newline at end of file diff --git a/src/Services/CRM/Deal/Service/DealProductRows.php b/src/Services/CRM/Deal/Service/DealProductRows.php index fcfd21ab..6bbedf6d 100644 --- a/src/Services/CRM/Deal/Service/DealProductRows.php +++ b/src/Services/CRM/Deal/Service/DealProductRows.php @@ -67,14 +67,22 @@ public function getSmart(int $dealId): DealProductRowItemsResult */ public function getSuperSmart(int $dealId): DealProductRowItemsResult { - $data = $this->core->call('batch',array( + $res = $this->core->call('batch',[ 'halt'=>0, - 'cmd'=>array( - $deal = new DealResult($this->core->call('crm.deal.get', ['id' => $dealId])), - $dealProductRow = new DealProductRowItemsResult($this->core->call('crm.deal.productrows.get',['id' => $dealId,]),new Currency($deal->deal()->CURRENCY_ID)) - ) - )); - return $dealProductRow; + 'cmd'=>[ + $deal = new DealResult( $this->core->call('crm.deal.get', ['id' => $dealId])), + $rows = new DealProductRowItemsResult($this->core->call('crm.deal.productrows.get',['id' => $dealId,]),new Currency($deal->deal()->CURRENCY_ID)), + + ], + ]); + return $rows; + /*$data = $res->getResponseData()->getResult(); + $array = $data->getResultData()['result']['deal']['CURRENCY_ID']; + var_dump($array); + return new DealProductRowItemsResult( + $data->getResultData()['result']['deal']['ID'] , + new Currency($array) + );*/ // todo Получить сделку и табличную часть за один запрос к Api } diff --git a/tests/Integration/Services/CRM/Deal/Service/DealProductRowsTest.php b/tests/Integration/Services/CRM/Deal/Service/DealProductRowsTest.php index cf53c493..b2c7accf 100644 --- a/tests/Integration/Services/CRM/Deal/Service/DealProductRowsTest.php +++ b/tests/Integration/Services/CRM/Deal/Service/DealProductRowsTest.php @@ -60,7 +60,6 @@ public function testSet(): void /** * @throws BaseException * @throws TransportException - * @covers \Bitrix24\SDK\Services\CRM\Deal\Service\DealProductRows::get */ public function testGet(): void { @@ -86,9 +85,41 @@ public function testGet(): void } } + /** + * @throws BaseException + * @throws TransportException + */ + public function testBatch():void{ + $callCosts = new Money(1050, new Currency('EUR')); + $currencies = new ISOCurrencies(); + + $moneyFormatter = new DecimalMoneyFormatter($currencies); + $newDealId = $this->dealService->add(['TITLE' => 'test deal','CURRENCY_ID'=>$callCosts->getCurrency()->getCode()])->getId(); + $this::assertTrue( + $this->dealProductRowsService->set( + $newDealId, + [ + [ + 'PRODUCT_NAME' => 'qqqq', + 'PRICE'=> $moneyFormatter->format($callCosts), + ], + ] + )->isSuccess() + ); + $data = $this->core->call('batch',array( + 'halt'=>0, + 'cmd'=>array( + 'deal'=>'crm.deal.get?id='.$newDealId, + 'productrow'=>'crm.deal.productrows.get?ID=$result[deal]['.$newDealId.'][ID]', + ) + )); + print_r($data); + } + public function setUp(): void { $this->dealService = Fabric::getServiceBuilder()->getCRMScope()->deal(); $this->dealProductRowsService = Fabric::getServiceBuilder()->getCRMScope()->dealProductRows(); + $this->core=Fabric::getCore(); } } \ No newline at end of file From 22c4e7099df3849e2fd1a7f72c12b243639e6d7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B8=D1=80=D0=B8=D0=BB=D0=BB=20=D0=A5=D1=80=D0=B0?= =?UTF-8?q?=D0=BC=D0=BE=D0=B2?= Date: Tue, 30 Aug 2022 13:39:37 +0300 Subject: [PATCH 15/22] -add method getSuperSuperSmart --- .../CRM/Common/Result/AbstractCrmItem.php | 2 +- .../CRM/Deal/Service/DealProductRows.php | 37 ++++++++++++++++++- .../CRM/Deal/Service/DealProductRowsTest.php | 11 +++--- 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/src/Services/CRM/Common/Result/AbstractCrmItem.php b/src/Services/CRM/Common/Result/AbstractCrmItem.php index abab2784..2d70acde 100644 --- a/src/Services/CRM/Common/Result/AbstractCrmItem.php +++ b/src/Services/CRM/Common/Result/AbstractCrmItem.php @@ -5,7 +5,6 @@ namespace Bitrix24\SDK\Services\CRM\Common\Result; use Bitrix24\SDK\Core\Result\AbstractItem; -use Bitrix24\SDK\Services\CRM\Deal\Result\DealProductRowItemsResult; use Bitrix24\SDK\Services\CRM\Userfield\Exceptions\UserfieldNotFoundException; use DateTimeImmutable; use Money\Currency; @@ -58,6 +57,7 @@ public function __get($offset) case 'PRICE': if ($this->data[$offset] !== '' && $this->data[$offset] !== null) { $var = $this->data[$offset] * 100; + var_dump($offset); return new Money((string)$var,new Currency($this->currency->getCode())); } return null; diff --git a/src/Services/CRM/Deal/Service/DealProductRows.php b/src/Services/CRM/Deal/Service/DealProductRows.php index 6bbedf6d..9ef44840 100644 --- a/src/Services/CRM/Deal/Service/DealProductRows.php +++ b/src/Services/CRM/Deal/Service/DealProductRows.php @@ -44,8 +44,10 @@ public function getStupid(int $dealId, Currency $currency): DealProductRowItemsR } /** - * @throws \Bitrix24\SDK\Core\Exceptions\TransportException + * @param int $dealId + * @return \Bitrix24\SDK\Services\CRM\Deal\Result\DealProductRowItemsResult * @throws \Bitrix24\SDK\Core\Exceptions\BaseException + * @throws \Bitrix24\SDK\Core\Exceptions\TransportException */ public function getSmart(int $dealId): DealProductRowItemsResult { @@ -63,7 +65,10 @@ public function getSmart(int $dealId): DealProductRowItemsResult } /** + * @param int $dealId + * @return \Bitrix24\SDK\Services\CRM\Deal\Result\DealProductRowItemsResult * @throws \Bitrix24\SDK\Core\Exceptions\BaseException + * @throws \Bitrix24\SDK\Core\Exceptions\TransportException */ public function getSuperSmart(int $dealId): DealProductRowItemsResult { @@ -86,8 +91,36 @@ public function getSuperSmart(int $dealId): DealProductRowItemsResult // todo Получить сделку и табличную часть за один запрос к Api } - public function getSuperSuperSmart() + /** + * @param int $dealId + * @param \Money\Currency|null $currency + * @return \Bitrix24\SDK\Services\CRM\Deal\Result\DealProductRowItemsResult + * @throws \Bitrix24\SDK\Core\Exceptions\BaseException + * @throws \Bitrix24\SDK\Core\Exceptions\TransportException + */ + public function getSuperSuperSmart(int $dealId, Currency $currency = null): DealProductRowItemsResult { + if ($currency === null){ + $res = $this->core->call('batch',[ + 'halt'=>0, + 'cmd'=>[ + $deal = new DealResult( $this->core->call('crm.deal.get', ['id' => $dealId])), + $rows = new DealProductRowItemsResult($this->core->call('crm.deal.productrows.get',['id' => $dealId,]),new Currency($deal->deal()->CURRENCY_ID)), + + ], + ]); + return $rows; + }else{ + return new DealProductRowItemsResult( + $this->core->call( + 'crm.deal.productrows.get', + [ + 'id' => $dealId, + ] + ), + $currency + ); + } // todo Метод позволяет экономить один запрос если мы уже знаем валюту, а если не знаем то делает этот запрос. } diff --git a/tests/Integration/Services/CRM/Deal/Service/DealProductRowsTest.php b/tests/Integration/Services/CRM/Deal/Service/DealProductRowsTest.php index b2c7accf..3aaf26d6 100644 --- a/tests/Integration/Services/CRM/Deal/Service/DealProductRowsTest.php +++ b/tests/Integration/Services/CRM/Deal/Service/DealProductRowsTest.php @@ -38,7 +38,7 @@ public function testSet(): void $moneyFormatter = new DecimalMoneyFormatter($currencies); $newDealId = $this->dealService->add(['TITLE' => 'test deal'])->getId(); - $this::assertCount(0, $this->dealProductRowsService->get($newDealId)->getProductRows()); + $this::assertCount(0, $this->dealProductRowsService->getSuperSmart($newDealId)->getProductRows()); $this::assertTrue( $this->dealProductRowsService->set( $newDealId, @@ -50,8 +50,8 @@ public function testSet(): void ] )->isSuccess() ); - $this::assertCount(1, $this->dealProductRowsService->get($newDealId)->getProductRows()); - $mas = $this->dealProductRowsService->get($newDealId)->getProductRows()[0]; + $this::assertCount(1, $this->dealProductRowsService->getSuperSmart($newDealId)->getProductRows()); + $mas = $this->dealProductRowsService->getSuperSmart($newDealId)->getProductRows()[0]; $money = $moneyFormatter->format($mas->PRICE); @@ -63,7 +63,7 @@ public function testSet(): void */ public function testGet(): void { - $callCosts = new Money(1050, new Currency('EUR')); + $callCosts = new Money(1050, new Currency('USD')); $currencies = new ISOCurrencies(); $moneyFormatter = new DecimalMoneyFormatter($currencies); @@ -79,7 +79,8 @@ public function testGet(): void ] )->isSuccess() ); - $res = $this->dealProductRowsService->getSuperSmart($newDealId); + $currency = $callCosts->getCurrency(); + $res = $this->dealProductRowsService->getStupid($newDealId,$currency); foreach ($res->getProductRows() as $productRow){ var_dump($productRow->PRICE); } From e42100f5f55be5422dd7425af94718be18114048 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B8=D1=80=D0=B8=D0=BB=D0=BB=20=D0=A5=D1=80=D0=B0?= =?UTF-8?q?=D0=BC=D0=BE=D0=B2?= Date: Wed, 31 Aug 2022 13:08:25 +0300 Subject: [PATCH 16/22] -add method getSuperSuperSmart -delete var_dump --- src/Services/CRM/Common/Result/AbstractCrmItem.php | 1 - .../Services/CRM/Deal/Service/DealProductRowsTest.php | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Services/CRM/Common/Result/AbstractCrmItem.php b/src/Services/CRM/Common/Result/AbstractCrmItem.php index 2d70acde..638366df 100644 --- a/src/Services/CRM/Common/Result/AbstractCrmItem.php +++ b/src/Services/CRM/Common/Result/AbstractCrmItem.php @@ -57,7 +57,6 @@ public function __get($offset) case 'PRICE': if ($this->data[$offset] !== '' && $this->data[$offset] !== null) { $var = $this->data[$offset] * 100; - var_dump($offset); return new Money((string)$var,new Currency($this->currency->getCode())); } return null; diff --git a/tests/Integration/Services/CRM/Deal/Service/DealProductRowsTest.php b/tests/Integration/Services/CRM/Deal/Service/DealProductRowsTest.php index 3aaf26d6..e5e92790 100644 --- a/tests/Integration/Services/CRM/Deal/Service/DealProductRowsTest.php +++ b/tests/Integration/Services/CRM/Deal/Service/DealProductRowsTest.php @@ -80,7 +80,7 @@ public function testGet(): void )->isSuccess() ); $currency = $callCosts->getCurrency(); - $res = $this->dealProductRowsService->getStupid($newDealId,$currency); + $res = $this->dealProductRowsService->getSuperSuperSmart($newDealId,$currency); foreach ($res->getProductRows() as $productRow){ var_dump($productRow->PRICE); } From e1037c9df860b8571c4309a46a1d6dd5e37665d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B8=D1=80=D0=B8=D0=BB=D0=BB=20=D0=A5=D1=80=D0=B0?= =?UTF-8?q?=D0=BC=D0=BE=D0=B2?= Date: Mon, 5 Sep 2022 20:06:53 +0300 Subject: [PATCH 17/22] - edit composer.json (-delete symfony routing) --- composer.json | 1 - 1 file changed, 1 deletion(-) diff --git a/composer.json b/composer.json index 7485fa08..e09b099d 100644 --- a/composer.json +++ b/composer.json @@ -30,7 +30,6 @@ "ramsey/uuid": "^4.2.3", "moneyphp/money": "3.* || 4.*", "symfony/serializer": "6.*||^6.1", - "symfony/routing": "^4.4|^5.3|^6.0", "symfony/property-access": "6.* || 6.1.*", "ext-intl": "*" }, From 3c4569892b6eff8d440225effa51e712286fdac4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B8=D1=80=D0=B8=D0=BB=D0=BB=20=D0=A5=D1=80=D0=B0?= =?UTF-8?q?=D0=BC=D0=BE=D0=B2?= Date: Mon, 5 Sep 2022 20:41:29 +0300 Subject: [PATCH 18/22] - removed unnecessary files --- .../Services/MixedTest/MixedTest.php | 94 -------------- tests/Unit/Services/TestPerson/Person.php | 35 ------ .../Services/TestPerson/PersonNormalizer.php | 40 ------ tests/Unit/Services/TestPerson/PersonTest.php | 117 ------------------ 4 files changed, 286 deletions(-) delete mode 100644 tests/Integration/Services/MixedTest/MixedTest.php delete mode 100644 tests/Unit/Services/TestPerson/Person.php delete mode 100644 tests/Unit/Services/TestPerson/PersonNormalizer.php delete mode 100644 tests/Unit/Services/TestPerson/PersonTest.php diff --git a/tests/Integration/Services/MixedTest/MixedTest.php b/tests/Integration/Services/MixedTest/MixedTest.php deleted file mode 100644 index b84c223a..00000000 --- a/tests/Integration/Services/MixedTest/MixedTest.php +++ /dev/null @@ -1,94 +0,0 @@ - 'wine', - 'CURRENCY_ID' => 'USD', - 'PRICE' => 100, - 'SORT' => 1 - - ); - $productId1 = $this->productService->add($dataProduct1)->getId(); - - //Создание продукта 2 - $dataProduct2 = array( - - 'NAME' => 'vodka', - 'CURRENCY_ID' => 'USD', - 'PRICE' => 10, - 'SORT' => 4 - - ); - $productId2 = $this->productService->add($dataProduct2)->getId(); - - - //Создание Сделки - $dataDeal1 = array( - 'TITLE' => 'sale of alcohol', - 'CURRENCY_ID' => 'RUB', - ); - $dealId1 = $this->dealServices->add($dataDeal1)->getId(); - - $productsForDeal = array( - - [ - 'PRODUCT_ID' => $productId1, - 'PRICE' => 5000, - 'QUANTITY' => 1 - ], - [ - 'PRODUCT_ID' => $productId2, - 'PRICE' => 500, - 'QUANTITY' => 1 - ] - - ); - - $addProductInDeal = $this->dealProductRows->set($dealId1, $productsForDeal); - - - self::assertEquals($productsForDeal[0]['PRICE'],$this->dealProductRows->get($dealId1)->getCoreResponse()->getResponseData()->getResult()->getResultData()[0]['PRICE']); - self::assertEquals($productsForDeal[1]['PRICE'],$this->dealProductRows->get($dealId1)->getCoreResponse()->getResponseData()->getResult()->getResultData()[1]['PRICE']); - self::assertGreaterThanOrEqual(1, $productId1); - self::assertGreaterThanOrEqual(1, $productId2); - self::assertTrue((bool)$addProductInDeal); - } - - - /** - * @throws \Bitrix24\SDK\Core\Exceptions\InvalidArgumentException - */ - public function setUp(): void - { - $this->productService = Fabric::getServiceBuilder()->getCRMScope()->product(); - $this->dealServices = Fabric::getServiceBuilder()->getCRMScope()->deal(); - $this->dealProductRows = Fabric::getServiceBuilder()->getCRMScope()->dealProductRows(); - } -} diff --git a/tests/Unit/Services/TestPerson/Person.php b/tests/Unit/Services/TestPerson/Person.php deleted file mode 100644 index 41c3c19f..00000000 --- a/tests/Unit/Services/TestPerson/Person.php +++ /dev/null @@ -1,35 +0,0 @@ -name; - } - - public function getLastName() - { - return $this->lastName; - } - - - // Setters - public function setName($name) - { - $this->name = $name; - } - - public function setLastName($lastName) - { - $this->lastName = $lastName; - } - -} \ No newline at end of file diff --git a/tests/Unit/Services/TestPerson/PersonNormalizer.php b/tests/Unit/Services/TestPerson/PersonNormalizer.php deleted file mode 100644 index c8b386dc..00000000 --- a/tests/Unit/Services/TestPerson/PersonNormalizer.php +++ /dev/null @@ -1,40 +0,0 @@ -normalizer = $normalizer; - } - - /** - * @param Person $object - * @throws \Symfony\Component\Serializer\Exception\ExceptionInterface - */ - public function normalize($object, string $format = null, array $context = []):array - { - $data = $this->normalizer->normalize($object,$format,$context); - return [ - 'name' => 'new_name', - 'lastname' => 'new_lastname', - ]; - } - - public function supportsNormalization($data, string $format = null, array $context = []):bool - { - return $data instanceof Person; - } - - -} \ No newline at end of file diff --git a/tests/Unit/Services/TestPerson/PersonTest.php b/tests/Unit/Services/TestPerson/PersonTest.php deleted file mode 100644 index 5d0b354f..00000000 --- a/tests/Unit/Services/TestPerson/PersonTest.php +++ /dev/null @@ -1,117 +0,0 @@ -setName('Kirill'); - $person->setLastName('Khramov'); - - - - $jsonContent = $serializer->serialize($person, 'json'); - - - self::assertNotEmpty($jsonContent); - echo $jsonContent; // or return it in a Response - } - - /** - * @test - */ - public function DeserializeTest():void{ - $encoders = [new XmlEncoder(), new JsonEncoder()]; - $normalizers = [new ObjectNormalizer()]; - - $serializer = new Serializer($normalizers, $encoders); - - $data = << - Kirill - Khramov - false - - EOF; - - $person = $serializer->deserialize($data, Person::class, 'xml'); - var_dump($person); - self::assertNotEmpty($person); - } - - /** - *@test - */ - public function NormalizePersonTest():void - { - $person = new Person(); - $encoders = [new XmlEncoder(), new JsonEncoder()]; - $normalizers = [new PersonNormalizer()]; - - $serializer = new Serializer($normalizers, $encoders); - - $person->setName('Kirill'); - $person->setLastName('Khramov'); - - $jsonContent = $serializer->serialize($person, 'json'); - self::assertNotEmpty($jsonContent); - echo $jsonContent; - - } - - /** - * @test - * @throws \Symfony\Component\Serializer\Exception\ExceptionInterface - */ - public function NormalizeMoneyTest():void - { - $encoders = [new XmlEncoder(), new JsonEncoder()]; - $normalizers = [new ObjectNormalizer()]; - - $serializer = new Serializer($normalizers, $encoders); - - $dollars = new Money(100, new Currency('USD')); - - $numberFormatter = new \NumberFormatter('en_US', \NumberFormatter::CURRENCY); - $intlFormatter = new IntlMoneyFormatter($numberFormatter, new ISOCurrencies()); - - $moneyFormatter = new AggregateMoneyFormatter([ - 'USD' => $intlFormatter, - ]); - - $money = $moneyFormatter->format($dollars); - $jsonContent = $serializer->normalize($money, null, [AbstractNormalizer::ATTRIBUTES => ['amount']]); - var_dump($jsonContent); - self::assertNotEmpty($money); - } - -} \ No newline at end of file From 5b837e4ee482493c278d8c4d4cea1c1ccf31fceb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B8=D1=80=D0=B8=D0=BB=D0=BB=20=D0=A5=D1=80=D0=B0?= =?UTF-8?q?=D0=BC=D0=BE=D0=B2?= Date: Sat, 10 Sep 2022 13:50:45 +0300 Subject: [PATCH 19/22] - edit method --- .../CRM/Deal/Service/DealProductRows.php | 18 +++++++----------- .../CRM/Deal/Service/DealProductRowsTest.php | 3 ++- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/Services/CRM/Deal/Service/DealProductRows.php b/src/Services/CRM/Deal/Service/DealProductRows.php index 9ef44840..e8955c47 100644 --- a/src/Services/CRM/Deal/Service/DealProductRows.php +++ b/src/Services/CRM/Deal/Service/DealProductRows.php @@ -75,19 +75,15 @@ public function getSuperSmart(int $dealId): DealProductRowItemsResult $res = $this->core->call('batch',[ 'halt'=>0, 'cmd'=>[ - $deal = new DealResult( $this->core->call('crm.deal.get', ['id' => $dealId])), - $rows = new DealProductRowItemsResult($this->core->call('crm.deal.productrows.get',['id' => $dealId,]),new Currency($deal->deal()->CURRENCY_ID)), - + 'deal' => sprintf('crm.deal.get?ID=%s', $dealId), + 'rows' => sprintf('crm.deal.productrows.get?ID=%s', $dealId) ], ]); - return $rows; - /*$data = $res->getResponseData()->getResult(); - $array = $data->getResultData()['result']['deal']['CURRENCY_ID']; - var_dump($array); - return new DealProductRowItemsResult( - $data->getResultData()['result']['deal']['ID'] , - new Currency($array) - );*/ + $data = $res->getResponseData()->getResult()->getResultData(); + $currency =$data['result']['deal']['CURRENCY_ID']; + $deal_test_id = $data['result']['deal']['ID']; + // А как нам вернуть DealProductRowItemsResult????(первый параметр принимает coreResponse) + return new DealProductRowItemsResult($deal_test_id,$currency); // todo Получить сделку и табличную часть за один запрос к Api } diff --git a/tests/Integration/Services/CRM/Deal/Service/DealProductRowsTest.php b/tests/Integration/Services/CRM/Deal/Service/DealProductRowsTest.php index e5e92790..887fab9b 100644 --- a/tests/Integration/Services/CRM/Deal/Service/DealProductRowsTest.php +++ b/tests/Integration/Services/CRM/Deal/Service/DealProductRowsTest.php @@ -80,7 +80,7 @@ public function testGet(): void )->isSuccess() ); $currency = $callCosts->getCurrency(); - $res = $this->dealProductRowsService->getSuperSuperSmart($newDealId,$currency); + $res = $this->dealProductRowsService->getSuperSmart($newDealId); foreach ($res->getProductRows() as $productRow){ var_dump($productRow->PRICE); } @@ -117,6 +117,7 @@ public function testBatch():void{ print_r($data); } + public function setUp(): void { $this->dealService = Fabric::getServiceBuilder()->getCRMScope()->deal(); From ba6fbe8dbbe072388976e04ac704d2f198529a51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B8=D1=80=D0=B8=D0=BB=D0=BB=20=D0=A5=D1=80=D0=B0?= =?UTF-8?q?=D0=BC=D0=BE=D0=B2?= Date: Sat, 10 Sep 2022 17:21:38 +0300 Subject: [PATCH 20/22] - delete serializer in composer.json and its dependencies - edit method in DealProductRows.php(work) --- composer.json | 2 - .../Deal/Result/DealProductRowItemResult.php | 6 --- .../CRM/Deal/Service/DealProductRows.php | 37 +++++++++---------- .../CRM/Deal/Service/DealProductRowsTest.php | 4 +- 4 files changed, 19 insertions(+), 30 deletions(-) diff --git a/composer.json b/composer.json index e09b099d..af46885c 100644 --- a/composer.json +++ b/composer.json @@ -29,8 +29,6 @@ "symfony/event-dispatcher": "5.4.* || 6.*", "ramsey/uuid": "^4.2.3", "moneyphp/money": "3.* || 4.*", - "symfony/serializer": "6.*||^6.1", - "symfony/property-access": "6.* || 6.1.*", "ext-intl": "*" }, "require-dev": { diff --git a/src/Services/CRM/Deal/Result/DealProductRowItemResult.php b/src/Services/CRM/Deal/Result/DealProductRowItemResult.php index 540d161f..da551122 100644 --- a/src/Services/CRM/Deal/Result/DealProductRowItemResult.php +++ b/src/Services/CRM/Deal/Result/DealProductRowItemResult.php @@ -46,10 +46,4 @@ public function __construct(array $data, Currency $currency) parent::__construct($data); $this->currency = $currency; } - - public function __get($offset) - { - return parent::__get($offset); - } - } \ No newline at end of file diff --git a/src/Services/CRM/Deal/Service/DealProductRows.php b/src/Services/CRM/Deal/Service/DealProductRows.php index e8955c47..d1a37a34 100644 --- a/src/Services/CRM/Deal/Service/DealProductRows.php +++ b/src/Services/CRM/Deal/Service/DealProductRows.php @@ -80,11 +80,8 @@ public function getSuperSmart(int $dealId): DealProductRowItemsResult ], ]); $data = $res->getResponseData()->getResult()->getResultData(); - $currency =$data['result']['deal']['CURRENCY_ID']; - $deal_test_id = $data['result']['deal']['ID']; - // А как нам вернуть DealProductRowItemsResult????(первый параметр принимает coreResponse) - return new DealProductRowItemsResult($deal_test_id,$currency); - // todo Получить сделку и табличную часть за один запрос к Api + $currency = new Currency($data['result']['deal']['CURRENCY_ID']); + return new DealProductRowItemsResult($res,$currency); } /** @@ -100,24 +97,24 @@ public function getSuperSuperSmart(int $dealId, Currency $currency = null): Deal $res = $this->core->call('batch',[ 'halt'=>0, 'cmd'=>[ - $deal = new DealResult( $this->core->call('crm.deal.get', ['id' => $dealId])), - $rows = new DealProductRowItemsResult($this->core->call('crm.deal.productrows.get',['id' => $dealId,]),new Currency($deal->deal()->CURRENCY_ID)), - + 'deal' => sprintf('crm.deal.get?ID=%s', $dealId), + 'rows' => sprintf('crm.deal.productrows.get?ID=%s', $dealId) ], ]); - return $rows; - }else{ - return new DealProductRowItemsResult( - $this->core->call( - 'crm.deal.productrows.get', - [ - 'id' => $dealId, - ] - ), - $currency - ); + $data = $res->getResponseData()->getResult()->getResultData(); + $currency = new Currency($data['result']['deal']['CURRENCY_ID']); + return new DealProductRowItemsResult($res,$currency); } - // todo Метод позволяет экономить один запрос если мы уже знаем валюту, а если не знаем то делает этот запрос. + + return new DealProductRowItemsResult( + $this->core->call( + 'crm.deal.productrows.get', + [ + 'id' => $dealId, + ] + ), + $currency + ); } /** diff --git a/tests/Integration/Services/CRM/Deal/Service/DealProductRowsTest.php b/tests/Integration/Services/CRM/Deal/Service/DealProductRowsTest.php index 887fab9b..d3fb1458 100644 --- a/tests/Integration/Services/CRM/Deal/Service/DealProductRowsTest.php +++ b/tests/Integration/Services/CRM/Deal/Service/DealProductRowsTest.php @@ -63,7 +63,7 @@ public function testSet(): void */ public function testGet(): void { - $callCosts = new Money(1050, new Currency('USD')); + $callCosts = new Money(1050, new Currency('EUR')); $currencies = new ISOCurrencies(); $moneyFormatter = new DecimalMoneyFormatter($currencies); @@ -80,7 +80,7 @@ public function testGet(): void )->isSuccess() ); $currency = $callCosts->getCurrency(); - $res = $this->dealProductRowsService->getSuperSmart($newDealId); + $res = $this->dealProductRowsService->getSuperSuperSmart($newDealId); foreach ($res->getProductRows() as $productRow){ var_dump($productRow->PRICE); } From a05285e19d65e1ee50bb23d358e4ebb29a97eda0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B8=D1=80=D0=B8=D0=BB=D0=BB=20=D0=A5=D1=80=D0=B0?= =?UTF-8?q?=D0=BC=D0=BE=D0=B2?= Date: Fri, 30 Sep 2022 16:50:12 +0300 Subject: [PATCH 21/22] - fix error phpstan --- .../CRM/Common/Result/AbstractCrmItem.php | 14 +++++++++++++ .../Deal/Result/DealProductRowItemResult.php | 10 --------- .../CRM/Deal/Service/DealProductRows.php | 21 ------------------- 3 files changed, 14 insertions(+), 31 deletions(-) diff --git a/src/Services/CRM/Common/Result/AbstractCrmItem.php b/src/Services/CRM/Common/Result/AbstractCrmItem.php index 980128b5..23112c8a 100644 --- a/src/Services/CRM/Common/Result/AbstractCrmItem.php +++ b/src/Services/CRM/Common/Result/AbstractCrmItem.php @@ -14,6 +14,20 @@ class AbstractCrmItem extends AbstractItem { private const CRM_USERFIELD_PREFIX = 'UF_CRM_'; + /** + * @var \Money\Currency + */ + private Currency $currency ; + + public function __construct(array $data, Currency $currency = null) + { + parent::__construct($data); + if ($currency !== null){ + $this->currency = $currency; + } + + } + /** * @param int|string $offset * diff --git a/src/Services/CRM/Deal/Result/DealProductRowItemResult.php b/src/Services/CRM/Deal/Result/DealProductRowItemResult.php index da551122..51af99eb 100644 --- a/src/Services/CRM/Deal/Result/DealProductRowItemResult.php +++ b/src/Services/CRM/Deal/Result/DealProductRowItemResult.php @@ -35,15 +35,5 @@ */ class DealProductRowItemResult extends AbstractCrmItem { - protected Currency $currency; - /** - * @param array $data - * @param \Money\Currency $currency - */ - public function __construct(array $data, Currency $currency) - { - parent::__construct($data); - $this->currency = $currency; - } } \ No newline at end of file diff --git a/src/Services/CRM/Deal/Service/DealProductRows.php b/src/Services/CRM/Deal/Service/DealProductRows.php index bd647c57..3ef4e644 100644 --- a/src/Services/CRM/Deal/Service/DealProductRows.php +++ b/src/Services/CRM/Deal/Service/DealProductRows.php @@ -43,27 +43,6 @@ public function getStupid(int $dealId, Currency $currency): DealProductRowItemsR ); } - /** - * @param int $dealId - * @return \Bitrix24\SDK\Services\CRM\Deal\Result\DealProductRowItemsResult - * @throws \Bitrix24\SDK\Core\Exceptions\BaseException - * @throws \Bitrix24\SDK\Core\Exceptions\TransportException - */ - public function getSmart(int $dealId): DealProductRowItemsResult - { - $deal = new DealResult($this->core->call('crm.deal.get', ['id' => $dealId])); - $currency = new Currency($deal->deal()->CURRENCY_ID); - return new DealProductRowItemsResult( - $this->core->call( - 'crm.deal.productrows.get', - [ - 'id' => $dealId, - ] - ), - $currency - ); - } - /** * @param int $dealId * @return \Bitrix24\SDK\Services\CRM\Deal\Result\DealProductRowItemsResult From c75b49eea485f9a68bdc43efffeeeaf0df25030b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B8=D1=80=D0=B8=D0=BB=D0=BB=20=D0=A5=D1=80=D0=B0?= =?UTF-8?q?=D0=BC=D0=BE=D0=B2?= Date: Wed, 19 Oct 2022 22:32:37 +0300 Subject: [PATCH 22/22] =?UTF-8?q?-=20=D0=BF=D0=BE=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=B8=D0=BB=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=83=20=D0=BC?= =?UTF-8?q?=D0=B5=D1=82=D0=BE=D0=B4=D0=B0=20get=20=D0=B2=20=D0=BE=D0=B1?= =?UTF-8?q?=D1=8A=D0=B5=D0=BA=D1=82=D0=B5=20DealProductRowItemsResult.php?= =?UTF-8?q?=20(=D0=A2=D0=B5=D0=BF=D0=B5=D1=80=D1=8C=20=D0=B2=20=D1=82?= =?UTF-8?q?=D0=B5=D1=81=D1=82=D0=B0=D1=85=20=D0=BC=D0=BE=D0=B6=D0=B5=D0=BC?= =?UTF-8?q?=20=D0=B8=20=D1=83=D0=BA=D0=B0=D0=B7=D1=8B=D0=B2=D0=B0=D1=82?= =?UTF-8?q?=D1=8C=20=D0=B2=D0=B0=D0=BB=D1=8E=D1=82=D1=83=20=D0=B8=D0=BB?= =?UTF-8?q?=D0=B8=20=D0=BD=D0=B5=20=D1=83=D0=BA=D0=B0=D0=B7=D1=8B=D0=B2?= =?UTF-8?q?=D0=B0=D1=82=D1=8C).=20-=20=D1=83=D0=B1=D1=80=D0=B0=D0=BB=20?= =?UTF-8?q?=D0=BB=D0=B8=D1=88=D0=BD=D0=B8=D0=B5=20=D0=BC=D0=B5=D1=82=D0=BE?= =?UTF-8?q?=D0=B4=D1=8B=20-=20=D0=BF=D0=BE=D0=BF=D1=80=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20-=20=D0=B4=D0=BE?= =?UTF-8?q?=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20=D0=B2=20AbstractCrmItem.php?= =?UTF-8?q?=20=D0=BE=D1=81=D1=82=D0=B0=D0=BB=D1=8C=D0=BD=D1=8B=D0=B5=20?= =?UTF-8?q?=D0=BF=D0=BE=D0=BB=D1=8F=20=D1=81=D0=B2=D1=8F=D0=B7=D0=B0=D0=BD?= =?UTF-8?q?=D0=BD=D1=8B=D0=B5=20=D1=81=20=D0=B4=D0=B5=D0=BD=D1=8C=D0=B3?= =?UTF-8?q?=D0=B0=D0=BC=D0=B8.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CRM/Common/Result/AbstractCrmItem.php | 11 ++-- .../Deal/Result/DealProductRowItemsResult.php | 10 +++- .../CRM/Deal/Service/DealProductRows.php | 54 +++---------------- .../CRM/Deal/Service/DealProductRowsTest.php | 33 ++++++------ 4 files changed, 40 insertions(+), 68 deletions(-) diff --git a/src/Services/CRM/Common/Result/AbstractCrmItem.php b/src/Services/CRM/Common/Result/AbstractCrmItem.php index 23112c8a..f05d6cc8 100644 --- a/src/Services/CRM/Common/Result/AbstractCrmItem.php +++ b/src/Services/CRM/Common/Result/AbstractCrmItem.php @@ -17,12 +17,12 @@ class AbstractCrmItem extends AbstractItem /** * @var \Money\Currency */ - private Currency $currency ; + private Currency $currency; public function __construct(array $data, Currency $currency = null) { parent::__construct($data); - if ($currency !== null){ + if ($currency !== null) { $this->currency = $currency; } @@ -66,10 +66,13 @@ public function __get($offset) case 'HAS_IMOL': case 'OPENED': // deal + case 'PRICE_EXCLUSIVE': + case 'PRICE_NETTO': + case 'PRICE_BRUTTO': case 'PRICE': if ($this->data[$offset] !== '' && $this->data[$offset] !== null) { - $var = $this->data[$offset] * 100; - return new Money((string)$var,new Currency($this->currency->getCode())); + $var = $this->data[$offset] * 100; + return new Money((string)$var, new Currency($this->currency->getCode())); } return null; case 'IS_MANUAL_OPPORTUNITY': diff --git a/src/Services/CRM/Deal/Result/DealProductRowItemsResult.php b/src/Services/CRM/Deal/Result/DealProductRowItemsResult.php index 25b15866..9cf683aa 100644 --- a/src/Services/CRM/Deal/Result/DealProductRowItemsResult.php +++ b/src/Services/CRM/Deal/Result/DealProductRowItemsResult.php @@ -32,8 +32,14 @@ public function __construct(Response $coreResponse,Currency $currency) public function getProductRows(): array { $res = []; - foreach ($this->getCoreResponse()->getResponseData()->getResult()['result']['rows'] as $productRow) { - $res[] = new DealProductRowItemResult($productRow,$this->currency); + if(!empty($this->getCoreResponse()->getResponseData()->getResult()['result']['rows'])) { + foreach ($this->getCoreResponse()->getResponseData()->getResult()['result']['rows'] as $productRow) { + $res[] = new DealProductRowItemResult($productRow, $this->currency); + } + } else { + foreach ($this->getCoreResponse()->getResponseData()->getResult() as $productRow) { + $res[] = new DealProductRowItemResult($productRow, $this->currency); + } } return $res; diff --git a/src/Services/CRM/Deal/Service/DealProductRows.php b/src/Services/CRM/Deal/Service/DealProductRows.php index 3ef4e644..42cacb1d 100644 --- a/src/Services/CRM/Deal/Service/DealProductRows.php +++ b/src/Services/CRM/Deal/Service/DealProductRows.php @@ -24,58 +24,18 @@ class DealProductRows extends AbstractService * * @link https://training.bitrix24.com/rest_help/crm/deals/crm_deal_productrows_get.php * - * @param int $dealId - * @param \Money\Currency $currency - * @return DealProductRowItemsResult - * @throws \Bitrix24\SDK\Core\Exceptions\BaseException - * @throws \Bitrix24\SDK\Core\Exceptions\TransportException - */ - public function getStupid(int $dealId, Currency $currency): DealProductRowItemsResult - { - return new DealProductRowItemsResult( - $this->core->call( - 'crm.deal.productrows.get', - [ - 'id' => $dealId, - ] - ), - $currency - ); - } - - /** - * @param int $dealId - * @return \Bitrix24\SDK\Services\CRM\Deal\Result\DealProductRowItemsResult - * @throws \Bitrix24\SDK\Core\Exceptions\BaseException - * @throws \Bitrix24\SDK\Core\Exceptions\TransportException - */ - public function getSuperSmart(int $dealId): DealProductRowItemsResult - { - $res = $this->core->call('batch',[ - 'halt'=>0, - 'cmd'=>[ - 'deal' => sprintf('crm.deal.get?ID=%s', $dealId), - 'rows' => sprintf('crm.deal.productrows.get?ID=%s', $dealId) - ], - ]); - $data = $res->getResponseData()->getResult(); - $currency = new Currency($data['result']['deal']['CURRENCY_ID']); - return new DealProductRowItemsResult($res,$currency); - } - - /** * @param int $dealId * @param \Money\Currency|null $currency * @return \Bitrix24\SDK\Services\CRM\Deal\Result\DealProductRowItemsResult * @throws \Bitrix24\SDK\Core\Exceptions\BaseException * @throws \Bitrix24\SDK\Core\Exceptions\TransportException */ - public function getSuperSuperSmart(int $dealId, Currency $currency = null): DealProductRowItemsResult + public function get(int $dealId, Currency $currency = null): DealProductRowItemsResult { - if ($currency === null){ - $res = $this->core->call('batch',[ - 'halt'=>0, - 'cmd'=>[ + if ($currency === null) { + $res = $this->core->call('batch', [ + 'halt' => 0, + 'cmd' => [ 'deal' => sprintf('crm.deal.get?ID=%s', $dealId), 'rows' => sprintf('crm.deal.productrows.get?ID=%s', $dealId) ], @@ -84,7 +44,6 @@ public function getSuperSuperSmart(int $dealId, Currency $currency = null): Deal $currency = new Currency($data['result']['deal']['CURRENCY_ID']); return new DealProductRowItemsResult($res,$currency); } - return new DealProductRowItemsResult( $this->core->call( 'crm.deal.productrows.get', @@ -92,10 +51,11 @@ public function getSuperSuperSmart(int $dealId, Currency $currency = null): Deal 'id' => $dealId, ] ), - $currency + $currency ); } + /** * Creates or updates product entries inside the specified deal. * diff --git a/tests/Integration/Services/CRM/Deal/Service/DealProductRowsTest.php b/tests/Integration/Services/CRM/Deal/Service/DealProductRowsTest.php index 42a487e3..2c286da0 100644 --- a/tests/Integration/Services/CRM/Deal/Service/DealProductRowsTest.php +++ b/tests/Integration/Services/CRM/Deal/Service/DealProductRowsTest.php @@ -33,26 +33,24 @@ class DealProductRowsTest extends TestCase public function testSet(): void { - $callCosts = new Money(1050, new Currency('RUB')); + $callCosts = new Money(1050, new Currency('USD')); $currencies = new ISOCurrencies(); $moneyFormatter = new DecimalMoneyFormatter($currencies); $newDealId = $this->dealService->add(['TITLE' => 'test deal'])->getId(); - $this::assertCount(0, $this->dealProductRowsService->getSuperSmart($newDealId)->getProductRows()); + $this::assertCount(5, $this->dealProductRowsService->get($newDealId)->getProductRows()); $this::assertTrue( $this->dealProductRowsService->set( $newDealId, [ [ - 'PRODUCT_NAME' => 'qqqq', - 'PRICE'=> $moneyFormatter->format($callCosts), + 'PRODUCT_NAME' => 'wine', + 'PRICE' => $moneyFormatter->format($callCosts), ], ] )->isSuccess() ); - $this::assertCount(1, $this->dealProductRowsService->getSuperSmart($newDealId)->getProductRows()); - $mas = $this->dealProductRowsService->getSuperSmart($newDealId)->getProductRows()[0]; - $money = $moneyFormatter->format($mas->PRICE); + $this::assertCount(1, $this->dealProductRowsService->get($newDealId)->getProductRows()); } @@ -63,26 +61,31 @@ public function testSet(): void */ public function testGet(): void { - $callCosts = new Money(1050, new Currency('EUR')); + $callCosts = new Money(1050, new Currency('USD')); $currencies = new ISOCurrencies(); $moneyFormatter = new DecimalMoneyFormatter($currencies); - $newDealId = $this->dealService->add(['TITLE' => 'test deal','CURRENCY_ID'=>$callCosts->getCurrency()->getCode()])->getId(); + $newDealId = $this->dealService->add(['TITLE' => 'test deal', 'CURRENCY_ID' => $callCosts->getCurrency()->getCode()])->getId(); $this::assertTrue( $this->dealProductRowsService->set( $newDealId, [ [ - 'PRODUCT_NAME' => 'qqqq', - 'PRICE'=> $moneyFormatter->format($callCosts), + 'PRODUCT_NAME' => 'wine', + 'PRICE' => $moneyFormatter->format($callCosts), ], ] )->isSuccess() ); $currency = $callCosts->getCurrency(); - $res = $this->dealProductRowsService->getSuperSuperSmart($newDealId); - foreach ($res->getProductRows() as $productRow){ - var_dump($productRow->PRICE); + + $resultWithoutAvailableCurrency = $this->dealProductRowsService->get($newDealId); + $resultWithAvailableCurrency = $this->dealProductRowsService->get($newDealId, $currency); + foreach ($resultWithoutAvailableCurrency->getProductRows() as $productRow) { + $this::assertEquals($callCosts, $productRow->PRICE); + } + foreach ($resultWithAvailableCurrency->getProductRows() as $productRow) { + $this::assertEquals($callCosts, $productRow->PRICE); } } @@ -90,6 +93,6 @@ public function setUp(): void { $this->dealService = Fabric::getServiceBuilder()->getCRMScope()->deal(); $this->dealProductRowsService = Fabric::getServiceBuilder()->getCRMScope()->dealProductRows(); - $this->core=Fabric::getCore(); + $this->core = Fabric::getCore(); } } \ No newline at end of file