From 0bb194b1f8c4ba8d69d67f58f288b2358e25a647 Mon Sep 17 00:00:00 2001 From: AnthonyAxenov Date: Wed, 27 May 2020 23:10:30 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=D0=A3=D0=BB=D1=83=D1=87=D1=88=D0=B5=D0=BD?= =?UTF-8?q?=D1=8B=20=D0=B8=D1=81=D0=BA=D0=BB=D1=8E=D1=87=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - добавлены теги ФФД для удобства - тексты исключений переведены на англ. язык во избежание проблем с кодировками - новые исключения TooMany и TooLong для удобства, подходящие по смыслу исключения унаследованы от них --- src/AtolOnline/Entities/Document.php | 4 +- src/AtolOnline/Entities/Item.php | 11 +++-- .../AtolCashierTooLongException.php | 24 +++++------ .../AtolCorrectionInfoException.php | 14 +------ .../Exceptions/AtolEmailEmptyException.php | 22 +++++----- .../Exceptions/AtolEmailTooLongException.php | 26 +++++------- .../Exceptions/AtolEmailValidateException.php | 11 ++++- src/AtolOnline/Exceptions/AtolException.php | 31 +++++++++++++- .../AtolInnWrongLengthException.php | 14 ++++++- ...ption.php => AtolInvalidUuidException.php} | 7 ++-- .../Exceptions/AtolKktLoginEmptyException.php | 14 +------ .../AtolKktLoginTooLongException.php | 18 ++------- .../AtolKktPasswordEmptyException.php | 14 +------ .../Exceptions/AtolNameTooLongException.php | 29 +++++++------- .../AtolPaymentAddressTooLongException.php | 23 +++++------ .../Exceptions/AtolPhoneTooLongException.php | 29 +++++++------- .../Exceptions/AtolPriceTooHighException.php | 25 +++++------- .../Exceptions/AtolTooLongException.php | 40 +++++++++++++++++++ ...Exception.php => AtolTooManyException.php} | 13 ++++-- .../Exceptions/AtolTooManyItemsException.php | 17 ++------ .../AtolTooManyPaymentsException.php | 29 +++++++------- .../Exceptions/AtolTooManyVatsException.php | 29 +++++++------- .../Exceptions/AtolUnitTooLongException.php | 25 +++++------- .../AtolUserdataTooLongException.php | 27 +++++-------- .../AtolWrongDocumentTypeException.php | 3 +- tests/Feature/ItemTest.php | 23 +++++------ 26 files changed, 262 insertions(+), 260 deletions(-) rename src/AtolOnline/Exceptions/{AtolUuidValidateException.php => AtolInvalidUuidException.php} (76%) create mode 100644 src/AtolOnline/Exceptions/AtolTooLongException.php rename src/AtolOnline/Exceptions/{AtolQuantityTooHighException.php => AtolTooManyException.php} (72%) diff --git a/src/AtolOnline/Entities/Document.php b/src/AtolOnline/Entities/Document.php index d06d986..db0c651 100644 --- a/src/AtolOnline/Entities/Document.php +++ b/src/AtolOnline/Entities/Document.php @@ -271,8 +271,8 @@ class Document extends Entity public function setCashier(?string $cashier) { $cashier = trim($cashier); - if (strlen($cashier) > 64) { - throw new AtolCashierTooLongException($cashier); + if ((function_exists('mb_strlen') ? mb_strlen($cashier) : strlen($cashier)) > 64) { + throw new AtolCashierTooLongException($cashier, 64); } $this->cashier = $cashier; return $this; diff --git a/src/AtolOnline/Entities/Item.php b/src/AtolOnline/Entities/Item.php index 0d5457b..4d7c026 100644 --- a/src/AtolOnline/Entities/Item.php +++ b/src/AtolOnline/Entities/Item.php @@ -11,11 +11,10 @@ namespace AtolOnline\Entities; use AtolOnline\{Exceptions\AtolNameTooLongException, Exceptions\AtolPriceTooHighException, - Exceptions\AtolQuantityTooHighException, + Exceptions\AtolTooManyException, Exceptions\AtolUnitTooLongException, Exceptions\AtolUserdataTooLongException, - Traits\RublesKopeksConverter -}; + Traits\RublesKopeksConverter}; /** * Предмет расчёта (товар, услуга) @@ -83,7 +82,7 @@ class Item extends Entity * @param string|null $payment_method Способ расчёта * @throws AtolNameTooLongException Слишком длинное наименование * @throws AtolPriceTooHighException Слишком высокая цена за одну единицу - * @throws AtolQuantityTooHighException Слишком большое количество + * @throws AtolTooManyException Слишком большое количество * @throws AtolUnitTooLongException Слишком длинное название единицы измерения */ public function __construct( @@ -188,7 +187,7 @@ class Item extends Entity * @param float $quantity Количество * @param string|null $measurement_unit Единица измерения количества * @return $this - * @throws AtolQuantityTooHighException Слишком большое количество + * @throws AtolTooManyException Слишком большое количество * @throws AtolPriceTooHighException Слишком высокая общая стоимость * @throws AtolUnitTooLongException Слишком длинное название единицы измерения */ @@ -196,7 +195,7 @@ class Item extends Entity { $quantity = round($quantity, 3); if ($quantity > 99999.999) { - throw new AtolQuantityTooHighException($quantity, 99999.999); + throw new AtolTooManyException($quantity, 99999.999); } $this->quantity = $quantity; $this->calcSum(); diff --git a/src/AtolOnline/Exceptions/AtolCashierTooLongException.php b/src/AtolOnline/Exceptions/AtolCashierTooLongException.php index 5917713..3f0ec20 100644 --- a/src/AtolOnline/Exceptions/AtolCashierTooLongException.php +++ b/src/AtolOnline/Exceptions/AtolCashierTooLongException.php @@ -9,26 +9,22 @@ namespace AtolOnline\Exceptions; -use Throwable; - /** * Исключение, возникающее при попытке указать слишком длинное имя кассира * * @package AtolOnline\Exceptions */ -class AtolCashierTooLongException extends AtolException +class AtolCashierTooLongException extends AtolTooLongException { /** - * AtolCashierTooLongException constructor. - * - * @param $name - * @param string $message - * @param int $code - * @param Throwable|null $previous + * @inheritDoc */ - public function __construct($name, $message = "", $code = 0, Throwable $previous = null) - { - $message = $message ?: 'Слишком длинное имя кассира (макс. длина 64, фактически '.strlen($name).'): '.$name; - parent::__construct($message, $code, $previous); - } + protected $ffd_tags = [ + 1021, + ]; + + /** + * @var string Сообщение об ошибке + */ + protected $message = 'Cashier name is too long'; } \ No newline at end of file diff --git a/src/AtolOnline/Exceptions/AtolCorrectionInfoException.php b/src/AtolOnline/Exceptions/AtolCorrectionInfoException.php index 594f336..bda765b 100644 --- a/src/AtolOnline/Exceptions/AtolCorrectionInfoException.php +++ b/src/AtolOnline/Exceptions/AtolCorrectionInfoException.php @@ -9,8 +9,6 @@ namespace AtolOnline\Exceptions; -use Throwable; - /** * Исключение, возникающее при попытке зарегистрировать документ без данных коррекции * @@ -19,15 +17,7 @@ use Throwable; class AtolCorrectionInfoException extends AtolException { /** - * AtolNoCorrectionInfoException constructor. - * - * @param string $message - * @param int $code - * @param Throwable|null $previous + * @var string Сообщение об ошибке */ - public function __construct($message = "", $code = 0, Throwable $previous = null) - { - $message = $message ?: 'В документе отсутствуют данные коррекции'; - parent::__construct($message, $code, $previous); - } + protected $message = 'Document must have correction info'; } \ No newline at end of file diff --git a/src/AtolOnline/Exceptions/AtolEmailEmptyException.php b/src/AtolOnline/Exceptions/AtolEmailEmptyException.php index 17cd69d..8dcbc30 100644 --- a/src/AtolOnline/Exceptions/AtolEmailEmptyException.php +++ b/src/AtolOnline/Exceptions/AtolEmailEmptyException.php @@ -9,8 +9,6 @@ namespace AtolOnline\Exceptions; -use Throwable; - /** * Исключение, возникающее при попытке указать пустой email * @@ -19,15 +17,15 @@ use Throwable; class AtolEmailEmptyException extends AtolException { /** - * AtolEmailEmptyException constructor. - * - * @param string $message - * @param int $code - * @param Throwable|null $previous + * @inheritDoc */ - public function __construct($message = "", $code = 0, Throwable $previous = null) - { - $message = $message ?: 'Email не может быть пустым'; - parent::__construct($message, $code, $previous); - } + protected $ffd_tags = [ + 1008, + 1117, + ]; + + /** + * @var string Сообщение об ошибке + */ + protected $message = 'Email cannot be empty'; } \ No newline at end of file diff --git a/src/AtolOnline/Exceptions/AtolEmailTooLongException.php b/src/AtolOnline/Exceptions/AtolEmailTooLongException.php index 77f73c1..e3bfc2b 100644 --- a/src/AtolOnline/Exceptions/AtolEmailTooLongException.php +++ b/src/AtolOnline/Exceptions/AtolEmailTooLongException.php @@ -9,27 +9,23 @@ namespace AtolOnline\Exceptions; -use Throwable; - /** * Исключение, возникающее при попытке указать слишком длинный email * * @package AtolOnline\Exceptions */ -class AtolEmailTooLongException extends AtolException +class AtolEmailTooLongException extends AtolTooLongException { /** - * AtolEmailTooLongException constructor. - * - * @param $email - * @param $max - * @param string $message - * @param int $code - * @param Throwable|null $previous + * @inheritDoc */ - public function __construct($email, $max, $message = "", $code = 0, Throwable $previous = null) - { - $message = $message ?: 'Слишком длинный email (макс. длина '.$max.', фактически '.strlen($email).'): '.$email; - parent::__construct($message, $code, $previous); - } + protected $ffd_tags = [ + 1008, + 1117, + ]; + + /** + * @var string Сообщение об ошибке + */ + protected $message = 'Email is is too long'; } \ No newline at end of file diff --git a/src/AtolOnline/Exceptions/AtolEmailValidateException.php b/src/AtolOnline/Exceptions/AtolEmailValidateException.php index a0d867f..ff1bcdb 100644 --- a/src/AtolOnline/Exceptions/AtolEmailValidateException.php +++ b/src/AtolOnline/Exceptions/AtolEmailValidateException.php @@ -18,6 +18,14 @@ use Throwable; */ class AtolEmailValidateException extends AtolException { + /** + * @inheritDoc + */ + protected $ffd_tags = [ + 1008, + 1117, + ]; + /** * AtolEmailValidateException constructor. * @@ -28,7 +36,6 @@ class AtolEmailValidateException extends AtolException */ public function __construct($email, $message = "", $code = 0, Throwable $previous = null) { - $message = $message ?: 'Некорректный email: '.$email; - parent::__construct($message, $code, $previous); + parent::__construct($message ?: 'Invalid email: '.$email, $code, $previous); } } \ No newline at end of file diff --git a/src/AtolOnline/Exceptions/AtolException.php b/src/AtolOnline/Exceptions/AtolException.php index 0696f2b..ddcbe9b 100644 --- a/src/AtolOnline/Exceptions/AtolException.php +++ b/src/AtolOnline/Exceptions/AtolException.php @@ -10,6 +10,7 @@ namespace AtolOnline\Exceptions; use Exception; +use Throwable; /** * Исключение, возникающее при работе с АТОЛ Онлайн @@ -18,5 +19,33 @@ use Exception; */ class AtolException extends Exception { - + /** + * @var int[] Теги ФФД + */ + protected $ffd_tags = null; + + /** + * AtolException constructor. + * + * @param string $message + * @param int $code + * @param \Throwable|null $previous + */ + public function __construct($message = "", $code = 0, Throwable $previous = null) + { + if ($this->getFfdTags()) { + $message .= ' [FFD tags: '.implode(', ', $this->getFfdTags()).']'; + } + parent::__construct($message, $code, $previous); + } + + /** + * Возвращает теги ФФД, с которыми связано исключение + * + * @return array|null + */ + protected function getFfdTags(): ?array + { + return $this->ffd_tags; + } } \ No newline at end of file diff --git a/src/AtolOnline/Exceptions/AtolInnWrongLengthException.php b/src/AtolOnline/Exceptions/AtolInnWrongLengthException.php index cc96bc8..6f63c76 100644 --- a/src/AtolOnline/Exceptions/AtolInnWrongLengthException.php +++ b/src/AtolOnline/Exceptions/AtolInnWrongLengthException.php @@ -18,6 +18,16 @@ use Throwable; */ class AtolInnWrongLengthException extends AtolException { + /** + * @inheritDoc + */ + protected $ffd_tags = [ + 1016, + 1018, + 1226, + 1228, + ]; + /** * AtolInnWrongLengthException constructor. * @@ -28,7 +38,7 @@ class AtolInnWrongLengthException extends AtolException */ public function __construct($inn, $message = "", $code = 0, Throwable $previous = null) { - $message = $message ?: 'Длина ИНН должна быть 10 или 12 цифр, фактически '.strlen($inn).': '.$inn; - parent::__construct($message, $code, $previous); + parent::__construct($message ?: 'INN length must be 10 or 12 digits only, but actual is '. + (function_exists('mb_strlen') ? mb_strlen($inn) : strlen($inn)).')', $code, $previous); } } \ No newline at end of file diff --git a/src/AtolOnline/Exceptions/AtolUuidValidateException.php b/src/AtolOnline/Exceptions/AtolInvalidUuidException.php similarity index 76% rename from src/AtolOnline/Exceptions/AtolUuidValidateException.php rename to src/AtolOnline/Exceptions/AtolInvalidUuidException.php index 6f5e9d0..c454262 100644 --- a/src/AtolOnline/Exceptions/AtolUuidValidateException.php +++ b/src/AtolOnline/Exceptions/AtolInvalidUuidException.php @@ -16,10 +16,10 @@ use Throwable; * * @package AtolOnline\Exceptions */ -class AtolUuidValidateException extends AtolException +class AtolInvalidUuidException extends AtolException { /** - * AtolUuidValidateException constructor. + * AtolInvalidUuidException constructor. * * @param $uuid * @param string $message @@ -28,7 +28,6 @@ class AtolUuidValidateException extends AtolException */ public function __construct($uuid, $message = "", $code = 0, Throwable $previous = null) { - $message = $message ?: 'Некорректный UUID: '.$uuid; - parent::__construct($message, $code, $previous); + parent::__construct($message ?: 'Invalid UUID: '.$uuid, $code, $previous); } } \ No newline at end of file diff --git a/src/AtolOnline/Exceptions/AtolKktLoginEmptyException.php b/src/AtolOnline/Exceptions/AtolKktLoginEmptyException.php index 248d545..3eb4408 100644 --- a/src/AtolOnline/Exceptions/AtolKktLoginEmptyException.php +++ b/src/AtolOnline/Exceptions/AtolKktLoginEmptyException.php @@ -9,8 +9,6 @@ namespace AtolOnline\Exceptions; -use Throwable; - /** * Исключение, возникающее при попытке указать пустой логин ККТ * @@ -19,15 +17,7 @@ use Throwable; class AtolKktLoginEmptyException extends AtolException { /** - * AtolKktLoginEmptyException constructor. - * - * @param string $message - * @param int $code - * @param Throwable|null $previous + * @var string Сообщение об ошибке */ - public function __construct($message = "", $code = 0, Throwable $previous = null) - { - $message = $message ?: 'Логин ККТ не может быть пустым'; - parent::__construct($message, $code, $previous); - } + protected $message = 'KKT login cannot be empty'; } \ No newline at end of file diff --git a/src/AtolOnline/Exceptions/AtolKktLoginTooLongException.php b/src/AtolOnline/Exceptions/AtolKktLoginTooLongException.php index 393e14c..c52c0cc 100644 --- a/src/AtolOnline/Exceptions/AtolKktLoginTooLongException.php +++ b/src/AtolOnline/Exceptions/AtolKktLoginTooLongException.php @@ -9,27 +9,15 @@ namespace AtolOnline\Exceptions; -use Throwable; - /** * Исключение, возникающее при попытке указать слишком длинный логин ККТ * * @package AtolOnline\Exceptions */ -class AtolKktLoginTooLongException extends AtolException +class AtolKktLoginTooLongException extends AtolTooLongException { /** - * AtolKktLoginTooLongException constructor. - * - * @param $login - * @param $max - * @param string $message - * @param int $code - * @param Throwable|null $previous + * @var string Сообщение об ошибке */ - public function __construct($login, $max, $message = "", $code = 0, Throwable $previous = null) - { - $message = $message ?: 'Слишком длинный логин ККТ (макс. длина '.$max.', фактически '.strlen($login).'): '.$login; - parent::__construct($message, $code, $previous); - } + protected $message = 'KKT login is too long'; } \ No newline at end of file diff --git a/src/AtolOnline/Exceptions/AtolKktPasswordEmptyException.php b/src/AtolOnline/Exceptions/AtolKktPasswordEmptyException.php index 323cc7d..97b88a3 100644 --- a/src/AtolOnline/Exceptions/AtolKktPasswordEmptyException.php +++ b/src/AtolOnline/Exceptions/AtolKktPasswordEmptyException.php @@ -9,8 +9,6 @@ namespace AtolOnline\Exceptions; -use Throwable; - /** * Исключение, возникающее при попытке указать пустой пароль ККТ * @@ -19,15 +17,7 @@ use Throwable; class AtolKktPasswordEmptyException extends AtolException { /** - * AtolKktPasswordEmptyException constructor. - * - * @param string $message - * @param int $code - * @param Throwable|null $previous + * @var string Сообщение об ошибке */ - public function __construct($message = "", $code = 0, Throwable $previous = null) - { - $message = $message ?: 'Пароль ККТ не может быть пустым'; - parent::__construct($message, $code, $previous); - } + protected $message = 'KKT password cannot be empty'; } \ No newline at end of file diff --git a/src/AtolOnline/Exceptions/AtolNameTooLongException.php b/src/AtolOnline/Exceptions/AtolNameTooLongException.php index 4024d86..ba115f8 100644 --- a/src/AtolOnline/Exceptions/AtolNameTooLongException.php +++ b/src/AtolOnline/Exceptions/AtolNameTooLongException.php @@ -9,27 +9,26 @@ namespace AtolOnline\Exceptions; -use Throwable; - /** * Исключение, возникающее при попытке указать слишком длинное имя * * @package AtolOnline\Exceptions */ -class AtolNameTooLongException extends AtolException +class AtolNameTooLongException extends AtolTooLongException { /** - * AtolNameTooLongException constructor. - * - * @param $name - * @param $max - * @param string $message - * @param int $code - * @param Throwable|null $previous + * @inheritDoc */ - public function __construct($name, $max, $message = "", $code = 0, Throwable $previous = null) - { - $message = $message ?: 'Слишком длинное имя/наименование (макс. длина '.$max.', фактически '.strlen($name).'): '.$name; - parent::__construct($message, $code, $previous); - } + protected $ffd_tags = [ + 1026, + 1030, + 1085, + 1225, + 1227, + ]; + + /** + * @var string Сообщение об ошибке + */ + protected $message = 'Name is too long'; } \ No newline at end of file diff --git a/src/AtolOnline/Exceptions/AtolPaymentAddressTooLongException.php b/src/AtolOnline/Exceptions/AtolPaymentAddressTooLongException.php index 15edb79..225165a 100644 --- a/src/AtolOnline/Exceptions/AtolPaymentAddressTooLongException.php +++ b/src/AtolOnline/Exceptions/AtolPaymentAddressTooLongException.php @@ -9,8 +9,6 @@ namespace AtolOnline\Exceptions; -use Throwable; - /** * Исключение, возникающее при попытке указать слишком длинный платёжный адрес * @@ -19,17 +17,14 @@ use Throwable; class AtolPaymentAddressTooLongException extends AtolException { /** - * AtolPaymentAddressTooLongException constructor. - * - * @param $address - * @param $max - * @param string $message - * @param int $code - * @param Throwable|null $previous + * @inheritDoc */ - public function __construct($address, $max, $message = "", $code = 0, Throwable $previous = null) - { - $message = $message ?: 'Слишком длинный адрес (макс. длина '.$max.', фактически '.strlen($address).'): '.$address; - parent::__construct($message, $code, $previous); - } + protected $ffd_tags = [ + 1187, + ]; + + /** + * @var string Сообщение об ошибке + */ + protected $message = 'Payment address is too long'; } \ No newline at end of file diff --git a/src/AtolOnline/Exceptions/AtolPhoneTooLongException.php b/src/AtolOnline/Exceptions/AtolPhoneTooLongException.php index 2f83282..f379c2a 100644 --- a/src/AtolOnline/Exceptions/AtolPhoneTooLongException.php +++ b/src/AtolOnline/Exceptions/AtolPhoneTooLongException.php @@ -9,27 +9,26 @@ namespace AtolOnline\Exceptions; -use Throwable; - /** * Исключение, возникающее при попытке указать слишком длинный телефон * * @package AtolOnline\Exceptions */ -class AtolPhoneTooLongException extends AtolException +class AtolPhoneTooLongException extends AtolTooLongException { /** - * AtolPhoneTooLongException constructor. - * - * @param $phone - * @param $max - * @param string $message - * @param int $code - * @param Throwable|null $previous + * @inheritDoc */ - public function __construct($phone, $max, $message = "", $code = 0, Throwable $previous = null) - { - $message = $message ?: 'Слишком длинный телефон (макс. длина '.$max.', фактически '.strlen($phone).'): '.$phone; - parent::__construct($message, $code, $previous); - } + protected $ffd_tags = [ + 1008, + 1073, + 1074, + 1075, + 1171, + ]; + + /** + * @var string Сообщение об ошибке + */ + protected $message = 'Phone is too long'; } \ No newline at end of file diff --git a/src/AtolOnline/Exceptions/AtolPriceTooHighException.php b/src/AtolOnline/Exceptions/AtolPriceTooHighException.php index 114a867..238d26b 100644 --- a/src/AtolOnline/Exceptions/AtolPriceTooHighException.php +++ b/src/AtolOnline/Exceptions/AtolPriceTooHighException.php @@ -9,27 +9,22 @@ namespace AtolOnline\Exceptions; -use Throwable; - /** * Исключение, возникающее при попытке указать слишком высокую цену (сумму) * * @package AtolOnline\Exceptions */ -class AtolPriceTooHighException extends AtolException +class AtolPriceTooHighException extends AtolTooManyException { /** - * AtolPriceTooHighException constructor. - * - * @param $price - * @param $max - * @param string $message - * @param int $code - * @param Throwable|null $previous + * @inheritDoc */ - public function __construct($price, $max, $message = "", $code = 0, Throwable $previous = null) - { - $message = $message ?: 'Слишком большая сумма (макс. '.$max.'): '.$price; - parent::__construct($message, $code, $previous); - } + protected $ffd_tags = [ + 1079, + ]; + + /** + * @var string Сообщение об ошибке + */ + protected $message = 'Price is too high'; } \ No newline at end of file diff --git a/src/AtolOnline/Exceptions/AtolTooLongException.php b/src/AtolOnline/Exceptions/AtolTooLongException.php new file mode 100644 index 0000000..499a6da --- /dev/null +++ b/src/AtolOnline/Exceptions/AtolTooLongException.php @@ -0,0 +1,40 @@ +message.' (max length - '.$max.', actual length - '. + (function_exists('mb_strlen') ? mb_strlen($string) : strlen($string)).')', $code, $previous); + } +} \ No newline at end of file diff --git a/src/AtolOnline/Exceptions/AtolQuantityTooHighException.php b/src/AtolOnline/Exceptions/AtolTooManyException.php similarity index 72% rename from src/AtolOnline/Exceptions/AtolQuantityTooHighException.php rename to src/AtolOnline/Exceptions/AtolTooManyException.php index d4ed67d..427c96b 100644 --- a/src/AtolOnline/Exceptions/AtolQuantityTooHighException.php +++ b/src/AtolOnline/Exceptions/AtolTooManyException.php @@ -12,14 +12,19 @@ namespace AtolOnline\Exceptions; use Throwable; /** - * Исключение, возникающее при попытке указать слишком большое количество + * Исключение, возникающее при попытке указать слишком большое количество чего-либо * * @package AtolOnline\Exceptions */ -class AtolQuantityTooHighException extends AtolException +class AtolTooManyException extends AtolException { /** - * AtolQuantityTooHighException constructor. + * @var string Сообщение об ошибке + */ + protected $message = 'Quantity is too high'; + + /** + * AtolTooManyException constructor. * * @param $quantity * @param $max @@ -29,7 +34,7 @@ class AtolQuantityTooHighException extends AtolException */ public function __construct($quantity, $max, $message = "", $code = 0, Throwable $previous = null) { - $message = $message ?: 'Слишком большое количество (макс. '.$max.'): '.$quantity; + $message = $message ?: $this->message.' (max - '.$max.', actual - '.$quantity.')'; parent::__construct($message, $code, $previous); } } \ No newline at end of file diff --git a/src/AtolOnline/Exceptions/AtolTooManyItemsException.php b/src/AtolOnline/Exceptions/AtolTooManyItemsException.php index 96478eb..ef0c8d1 100644 --- a/src/AtolOnline/Exceptions/AtolTooManyItemsException.php +++ b/src/AtolOnline/Exceptions/AtolTooManyItemsException.php @@ -9,26 +9,15 @@ namespace AtolOnline\Exceptions; -use Throwable; - /** * Исключение, возникающее при попытке добавить слишком много предметов расчёта в массив * * @package AtolOnline\Exceptions */ -class AtolTooManyItemsException extends AtolException +class AtolTooManyItemsException extends AtolTooManyException { /** - * AtolTooManyItemsException constructor. - * - * @param int $max - * @param string $message - * @param int $code - * @param Throwable|null $previous + * @var string Сообщение об ошибке */ - public function __construct($max, $message = "", $code = 0, Throwable $previous = null) - { - $message = $message ?: 'Слишком много предметов расчёта (макс. '.$max.')'; - parent::__construct($message, $code, $previous); - } + protected $message = 'Too many items'; } \ No newline at end of file diff --git a/src/AtolOnline/Exceptions/AtolTooManyPaymentsException.php b/src/AtolOnline/Exceptions/AtolTooManyPaymentsException.php index 53e652a..14f483d 100644 --- a/src/AtolOnline/Exceptions/AtolTooManyPaymentsException.php +++ b/src/AtolOnline/Exceptions/AtolTooManyPaymentsException.php @@ -9,26 +9,25 @@ namespace AtolOnline\Exceptions; -use Throwable; - /** - * Исключение, возникающее при попытке добавить слишком много ставок НДС в массив + * Исключение, возникающее при попытке добавить слишком много платежей в массив * * @package AtolOnline\Exceptions */ -class AtolTooManyPaymentsException extends AtolException +class AtolTooManyPaymentsException extends AtolTooManyException { /** - * AtolTooManyPaymentsException constructor. - * - * @param int $max - * @param string $message - * @param int $code - * @param Throwable|null $previous + * @inheritDoc */ - public function __construct($max, $message = "", $code = 0, Throwable $previous = null) - { - $message = $message ?: 'Слишком много платежей (макс. '.$max.')'; - parent::__construct($message, $code, $previous); - } + protected $ffd_tags = [ + 1031, + 1081, + 1215, + 1217, + ]; + + /** + * @var string Сообщение об ошибке + */ + protected $message = 'Too many payments'; } \ No newline at end of file diff --git a/src/AtolOnline/Exceptions/AtolTooManyVatsException.php b/src/AtolOnline/Exceptions/AtolTooManyVatsException.php index 960de42..cb35d57 100644 --- a/src/AtolOnline/Exceptions/AtolTooManyVatsException.php +++ b/src/AtolOnline/Exceptions/AtolTooManyVatsException.php @@ -9,26 +9,27 @@ namespace AtolOnline\Exceptions; -use Throwable; - /** * Исключение, возникающее при попытке добавить слишком много ставок НДС в массив * * @package AtolOnline\Exceptions */ -class AtolTooManyVatsException extends AtolException +class AtolTooManyVatsException extends AtolTooManyException { /** - * AtolTooManyVatsException constructor. - * - * @param int $max - * @param string $message - * @param int $code - * @param Throwable|null $previous + * @inheritDoc */ - public function __construct($max, $message = "", $code = 0, Throwable $previous = null) - { - $message = $message ?: 'Слишком много ставок НДС (макс. '.$max.')'; - parent::__construct($message, $code, $previous); - } + protected $ffd_tags = [ + 1102, + 1103, + 1104, + 1105, + 1106, + 1107, + ]; + + /** + * @var string Сообщение об ошибке + */ + protected $message = 'Too many vats'; } \ No newline at end of file diff --git a/src/AtolOnline/Exceptions/AtolUnitTooLongException.php b/src/AtolOnline/Exceptions/AtolUnitTooLongException.php index 422d3ba..f000170 100644 --- a/src/AtolOnline/Exceptions/AtolUnitTooLongException.php +++ b/src/AtolOnline/Exceptions/AtolUnitTooLongException.php @@ -9,27 +9,22 @@ namespace AtolOnline\Exceptions; -use Throwable; - /** * Исключение, возникающее при попытке указать слишком длинный телефон * * @package AtolOnline\Exceptions */ -class AtolUnitTooLongException extends AtolException +class AtolUnitTooLongException extends AtolTooLongException { /** - * AtolUnitTooLongException constructor. - * - * @param $unit - * @param $max - * @param string $message - * @param int $code - * @param Throwable|null $previous + * @inheritDoc */ - public function __construct($unit, $max, $message = "", $code = 0, Throwable $previous = null) - { - $message = $message ?: 'Слишком длинное название единицы измерения (макс. длина '.$max.', фактически '.strlen($unit).'): '.$unit; - parent::__construct($message, $code, $previous); - } + protected $ffd_tags = [ + 1197, + ]; + + /** + * @var string Сообщение об ошибке + */ + protected $message = 'Measurement unit is too long'; } \ No newline at end of file diff --git a/src/AtolOnline/Exceptions/AtolUserdataTooLongException.php b/src/AtolOnline/Exceptions/AtolUserdataTooLongException.php index adec4a5..46cf2b1 100644 --- a/src/AtolOnline/Exceptions/AtolUserdataTooLongException.php +++ b/src/AtolOnline/Exceptions/AtolUserdataTooLongException.php @@ -9,27 +9,22 @@ namespace AtolOnline\Exceptions; -use Throwable; - /** - * Исключение, возникающее при попытке указать слишком длинный телефон + * Исключение, возникающее при попытке указать слишком длинный дополнительный реквизит * * @package AtolOnline\Exceptions */ -class AtolUserdataTooLongException extends AtolException +class AtolUserdataTooLongException extends AtolTooLongException { /** - * AtolUserdataTooLongException constructor. - * - * @param $data - * @param $max - * @param string $message - * @param int $code - * @param Throwable|null $previous + * @inheritDoc */ - public function __construct($data, $max, $message = "", $code = 0, Throwable $previous = null) - { - $message = $message ?: 'Слишком длинный дополнительный реквизит (макс. длина '.$max.', фактически '.strlen($data).'): '.$data; - parent::__construct($message, $code, $previous); - } + protected $ffd_tags = [ + 1191, + ]; + + /** + * @var string Сообщение об ошибке + */ + protected $message = 'User data is too long'; } \ No newline at end of file diff --git a/src/AtolOnline/Exceptions/AtolWrongDocumentTypeException.php b/src/AtolOnline/Exceptions/AtolWrongDocumentTypeException.php index 0cdbbd3..c7144ed 100644 --- a/src/AtolOnline/Exceptions/AtolWrongDocumentTypeException.php +++ b/src/AtolOnline/Exceptions/AtolWrongDocumentTypeException.php @@ -28,7 +28,6 @@ class AtolWrongDocumentTypeException extends AtolException */ public function __construct($type, $message = "", $code = 0, Throwable $previous = null) { - $message = $message ?: 'Некорректный тип документа: ожидался \'receipt\' или \'correction\', указан \''.$type.'\''; - parent::__construct($message, $code, $previous); + parent::__construct($message ?: "Wrong document type: 'receipt' or 'correction' expected, but '$type' provided", $code, $previous); } } \ No newline at end of file diff --git a/tests/Feature/ItemTest.php b/tests/Feature/ItemTest.php index df0f52e..b3db8f6 100644 --- a/tests/Feature/ItemTest.php +++ b/tests/Feature/ItemTest.php @@ -13,10 +13,9 @@ use AtolOnline\{Constants\PaymentMethods, Entities\Item, Exceptions\AtolNameTooLongException, Exceptions\AtolPriceTooHighException, - Exceptions\AtolQuantityTooHighException, + Exceptions\AtolTooManyException, Exceptions\AtolUnitTooLongException, - Exceptions\AtolUserdataTooLongException -}; + Exceptions\AtolUserdataTooLongException}; /** * Class ItemTest @@ -28,7 +27,7 @@ class ItemTest extends BasicTestCase * * @throws AtolOnline\Exceptions\AtolNameTooLongException * @throws AtolOnline\Exceptions\AtolPriceTooHighException - * @throws AtolOnline\Exceptions\AtolQuantityTooHighException + * @throws AtolOnline\Exceptions\AtolTooManyException * @throws AtolOnline\Exceptions\AtolUnitTooLongException */ public function testConstructor() @@ -57,7 +56,7 @@ class ItemTest extends BasicTestCase * * @throws AtolOnline\Exceptions\AtolNameTooLongException * @throws AtolOnline\Exceptions\AtolPriceTooHighException - * @throws AtolOnline\Exceptions\AtolQuantityTooHighException + * @throws AtolOnline\Exceptions\AtolTooManyException * @throws AtolOnline\Exceptions\AtolUnitTooLongException * @throws AtolOnline\Exceptions\AtolUserdataTooLongException */ @@ -88,7 +87,7 @@ class ItemTest extends BasicTestCase * * @throws AtolOnline\Exceptions\AtolNameTooLongException * @throws AtolOnline\Exceptions\AtolPriceTooHighException - * @throws AtolOnline\Exceptions\AtolQuantityTooHighException + * @throws AtolOnline\Exceptions\AtolTooManyException * @throws AtolOnline\Exceptions\AtolUnitTooLongException */ public function testSetVat() @@ -105,7 +104,7 @@ class ItemTest extends BasicTestCase * * @throws AtolOnline\Exceptions\AtolNameTooLongException * @throws AtolOnline\Exceptions\AtolPriceTooHighException - * @throws AtolOnline\Exceptions\AtolQuantityTooHighException + * @throws AtolOnline\Exceptions\AtolTooManyException * @throws AtolOnline\Exceptions\AtolUnitTooLongException */ public function testAtolNameTooLongException() @@ -119,14 +118,14 @@ class ItemTest extends BasicTestCase * Тестирует исключение о слишком высоком количестве * * @throws AtolOnline\Exceptions\AtolNameTooLongException - * @throws AtolOnline\Exceptions\AtolQuantityTooHighException + * @throws AtolOnline\Exceptions\AtolTooManyException * @throws AtolOnline\Exceptions\AtolPriceTooHighException * @throws AtolOnline\Exceptions\AtolUnitTooLongException */ public function testAtolQuantityTooHighException() { $item = new Item(); - $this->expectException(AtolQuantityTooHighException::class); + $this->expectException(AtolTooManyException::class); $item->setQuantity(100000.1); } @@ -135,7 +134,7 @@ class ItemTest extends BasicTestCase * * @throws AtolOnline\Exceptions\AtolPriceTooHighException * @throws AtolOnline\Exceptions\AtolNameTooLongException - * @throws AtolOnline\Exceptions\AtolQuantityTooHighException + * @throws AtolOnline\Exceptions\AtolTooManyException * @throws AtolOnline\Exceptions\AtolUnitTooLongException */ public function testAtolPriceTooHighException() @@ -151,7 +150,7 @@ class ItemTest extends BasicTestCase * @throws AtolOnline\Exceptions\AtolUserdataTooLongException * @throws AtolOnline\Exceptions\AtolPriceTooHighException * @throws AtolOnline\Exceptions\AtolNameTooLongException - * @throws AtolOnline\Exceptions\AtolQuantityTooHighException + * @throws AtolOnline\Exceptions\AtolTooManyException * @throws AtolOnline\Exceptions\AtolUnitTooLongException */ public function testAtolUserdataTooLongException() @@ -166,7 +165,7 @@ class ItemTest extends BasicTestCase * * @throws AtolOnline\Exceptions\AtolNameTooLongException * @throws AtolOnline\Exceptions\AtolPriceTooHighException - * @throws AtolOnline\Exceptions\AtolQuantityTooHighException + * @throws AtolOnline\Exceptions\AtolTooManyException * @throws AtolOnline\Exceptions\AtolUnitTooLongException */ public function testAtolUnitTooLongException() From a619b06a48be70cf1e83edfbf2efee107c885722 Mon Sep 17 00:00:00 2001 From: AnthonyAxenov Date: Thu, 28 May 2020 00:38:16 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D1=80=D0=B0=D1=81=D1=87=D1=91=D1=82=20=D0=9D?= =?UTF-8?q?=D0=94=D0=A1=20=D0=B4=D0=BB=D1=8F=20=D0=B4=D0=BE=D0=BA=D1=83?= =?UTF-8?q?=D0=BC=D0=B5=D0=BD=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/AtolOnline/Entities/Document.php | 18 ++---------------- src/AtolOnline/Entities/Vat.php | 11 ++++++----- src/AtolOnline/Entities/VatArray.php | 6 +++++- 3 files changed, 13 insertions(+), 22 deletions(-) diff --git a/src/AtolOnline/Entities/Document.php b/src/AtolOnline/Entities/Document.php index db0c651..1a6d71b 100644 --- a/src/AtolOnline/Entities/Document.php +++ b/src/AtolOnline/Entities/Document.php @@ -60,10 +60,6 @@ class Document extends Entity /** * Document constructor. - * - * @throws \AtolOnline\Exceptions\AtolTooManyItemsException Слишком много предметов расчёта - * @throws \AtolOnline\Exceptions\AtolTooManyPaymentsException Слишком много оплат - * @throws \AtolOnline\Exceptions\AtolTooManyVatsException Слишком много ставок НДС */ public function __construct() { @@ -82,10 +78,6 @@ class Document extends Entity public function clearVats() { $this->setVats([]); - foreach ($this->getItems() as &$item) { - $item->setVatType(null); - } - $this->calcTotal(); return $this; } @@ -98,11 +90,7 @@ class Document extends Entity */ public function addVat(Vat $vat) { - if (count($this->getVats()) == 0 && !$vat->getSum()) { - $vat->setSum($this->calcTotal()); - } $this->vats->add($vat); - $this->calcTotal(); return $this; } @@ -127,7 +115,6 @@ class Document extends Entity public function setVats(array $vats) { $this->vats->set($vats); - $this->calcTotal(); return $this; } @@ -309,11 +296,10 @@ class Document extends Entity public function calcTotal() { $sum = 0; + $this->clearVats(); foreach ($this->items->get() as $item) { $sum += $item->calcSum(); - } - foreach ($this->vats->get() as $vat) { - $vat->setSum($sum); + $this->addVat(new Vat($item->getVat()->getType(), $item->getSum())); } return $this->total = round($sum, 2); } diff --git a/src/AtolOnline/Entities/Vat.php b/src/AtolOnline/Entities/Vat.php index 6b1780c..29d81c6 100644 --- a/src/AtolOnline/Entities/Vat.php +++ b/src/AtolOnline/Entities/Vat.php @@ -26,12 +26,12 @@ class Vat extends Entity private $type; /** - * @var int Сумма в копейках, от которой пересчитывается размер налога + * @var int Сумма в копейках, от которой пересчитывается размер НДС */ private $sum_original = 0; /** - * @var int Сумма налога в копейках + * @var int Сумма НДС в копейках */ private $sum_final = 0; @@ -66,18 +66,19 @@ class Vat extends Entity case VatTypes::VAT0: return 0; case VatTypes::VAT10: - return $kopeks * 10 / 100; + //return $kopeks * 10 / 100; case VatTypes::VAT110: return $kopeks * 10 / 110; case VatTypes::VAT18: - return $kopeks * 18 / 100; + //return $kopeks * 18 / 100; case VatTypes::VAT118: return $kopeks * 18 / 118; case VatTypes::VAT20: - return $kopeks * 20 / 100; + //return $kopeks * 20 / 100; case VatTypes::VAT120: return $kopeks * 20 / 120; } + return 0; } /** diff --git a/src/AtolOnline/Entities/VatArray.php b/src/AtolOnline/Entities/VatArray.php index 3ad156c..8669121 100644 --- a/src/AtolOnline/Entities/VatArray.php +++ b/src/AtolOnline/Entities/VatArray.php @@ -62,7 +62,11 @@ class VatArray extends Entity public function add(Vat $vat) { if ($this->validateCount()) { - $this->vats[] = $vat; + if (isset($this->vats[$vat->getType()])) { + $this->vats[$vat->getType()]->addSum($vat->getSum()); + } else { + $this->vats[$vat->getType()] = $vat; + } } return $this; } From 954843e3adf418d57085d2810731019215c50972 Mon Sep 17 00:00:00 2001 From: AnthonyAxenov Date: Thu, 28 May 2020 00:38:48 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=D0=A4=D0=B8=D0=BA=D1=81=20=D0=B8=D1=81?= =?UTF-8?q?=D0=BA=D0=BB=D1=8E=D1=87=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=BF=D1=80?= =?UTF-8?q?=D0=B5=D0=B2=D1=8B=D1=88=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=BA=D0=BE?= =?UTF-8?q?=D0=BB=D0=B8=D1=87=D0=B5=D1=81=D1=82=D0=B2=D0=B0=20=D1=81=D1=82?= =?UTF-8?q?=D0=B0=D0=B2=D0=BE=D0=BA=20=D0=9D=D0=94=D0=A1=20=D0=B2=20=D0=BC?= =?UTF-8?q?=D0=B0=D1=81=D1=81=D0=B8=D0=B2=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/AtolOnline/Entities/VatArray.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AtolOnline/Entities/VatArray.php b/src/AtolOnline/Entities/VatArray.php index 8669121..391fe4a 100644 --- a/src/AtolOnline/Entities/VatArray.php +++ b/src/AtolOnline/Entities/VatArray.php @@ -105,7 +105,7 @@ class VatArray extends Entity { $max_items = SellSchema::get()->properties->receipt->properties->vats->maxItems; if ((!empty($vats) && count($vats) >= $max_items) || count($this->vats) >= $max_items) { - throw new AtolTooManyVatsException($max_items); + throw new AtolTooManyVatsException(count($vats), $max_items); } return true; } From 2d29ff3994f037a265801ac0fc8ff39a7bf6a8d3 Mon Sep 17 00:00:00 2001 From: AnthonyAxenov Date: Thu, 28 May 2020 00:39:42 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=D0=A4=D0=B8=D0=BA=D1=81=20=D0=B8=D1=81?= =?UTF-8?q?=D0=BA=D0=BB=D1=8E=D1=87=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=BD=D0=B5?= =?UTF-8?q?=D0=BA=D0=BE=D1=80=D1=80=D0=B5=D0=BA=D1=82=D0=BD=D0=BE=D0=B3?= =?UTF-8?q?=D0=BE=20UUID=20=D0=B2=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D0=B5?= =?UTF-8?q?=20Kkt::getDocumentStatus()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/AtolOnline/Api/Kkt.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/AtolOnline/Api/Kkt.php b/src/AtolOnline/Api/Kkt.php index 2ab02ae..979e464 100644 --- a/src/AtolOnline/Api/Kkt.php +++ b/src/AtolOnline/Api/Kkt.php @@ -11,10 +11,10 @@ namespace AtolOnline\Api; use AtolOnline\{Entities\Document, Exceptions\AtolCorrectionInfoException, + Exceptions\AtolInvalidUuidException, Exceptions\AtolKktLoginEmptyException, Exceptions\AtolKktLoginTooLongException, Exceptions\AtolKktPasswordEmptyException, - Exceptions\AtolUuidValidateException, Exceptions\AtolWrongDocumentTypeException }; use GuzzleHttp\Client; @@ -327,13 +327,13 @@ class Kkt extends Client * * @param string $uuid UUID регистрации * @return \AtolOnline\Api\KktResponse - * @throws \AtolOnline\Exceptions\AtolUuidValidateException Некорректный UUID документа + * @throws \AtolOnline\Exceptions\AtolInvalidUuidException Некорректный UUID документа */ public function getDocumentStatus(string $uuid) { $uuid = trim($uuid); if (!Uuid::isValid($uuid)) { - throw new AtolUuidValidateException($uuid); + throw new AtolInvalidUuidException($uuid); } $this->auth(); return $this->sendAtolRequest('GET', 'report/'.$uuid); From 4fccf7809d3b471c48c9e16b1f4d5afe9ac33a70 Mon Sep 17 00:00:00 2001 From: AnthonyAxenov Date: Thu, 28 May 2020 00:41:01 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=D0=9D=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C=D1=88?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B8=20=D0=BF?= =?UTF-8?q?=D0=BE=20Item=20(=D1=83=D0=BB=D1=83=D1=87=D1=88=D0=B5=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=B4=D0=BB=D0=B8=D0=BD=D1=8B=20=D0=B5=D0=B4.=20=D0=B8=D0=B7?= =?UTF-8?q?=D0=BC.)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/AtolOnline/Entities/Item.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/AtolOnline/Entities/Item.php b/src/AtolOnline/Entities/Item.php index 4d7c026..f421336 100644 --- a/src/AtolOnline/Entities/Item.php +++ b/src/AtolOnline/Entities/Item.php @@ -100,17 +100,17 @@ class Item extends Entity if ($price) { $this->setPrice($price); } - if ($payment_object) { - $this->setPaymentObject($payment_object); - } if ($quantity) { $this->setQuantity($quantity); } + if ($measurement_unit) { + $this->setMeasurementUnit($measurement_unit); + } if ($vat_type) { $this->setVatType($vat_type); } - if ($measurement_unit) { - $this->setMeasurementUnit($measurement_unit); + if ($payment_object) { + $this->setPaymentObject($payment_object); } if ($payment_method) { $this->setPaymentMethod($payment_method); @@ -225,7 +225,7 @@ class Item extends Entity public function setMeasurementUnit(string $measurement_unit) { $measurement_unit = trim($measurement_unit); - if (strlen($measurement_unit) > 16) { + if ((function_exists('mb_strlen') ? mb_strlen($measurement_unit) : strlen($measurement_unit)) > 16) { throw new AtolUnitTooLongException($measurement_unit, 16); } $this->measurement_unit = $measurement_unit; From 600a505c83bb363b9d59f9e6c70b9ddef3c6a247 Mon Sep 17 00:00:00 2001 From: AnthonyAxenov Date: Thu, 28 May 2020 00:41:58 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=D0=93=D0=B5=D0=BD=D0=B5=D1=80=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=BE=D0=B1=D1=8A=D0=B5=D0=BA=D1=82=D0=B0=20?= =?UTF-8?q?=D0=B4=D0=BE=D0=BA=D1=83=D0=BC=D0=B5=D0=BD=D1=82=D0=B0=20=D0=BF?= =?UTF-8?q?=D0=BE=20=D1=81=D1=8B=D1=80=D0=BE=D0=B9=20json-=D1=81=D1=82?= =?UTF-8?q?=D1=80=D0=BE=D0=BA=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/AtolOnline/Entities/Document.php | 86 ++++++++++++++++++- .../Exceptions/AtolInvalidJsonException.php | 32 +++++++ 2 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 src/AtolOnline/Exceptions/AtolInvalidJsonException.php diff --git a/src/AtolOnline/Entities/Document.php b/src/AtolOnline/Entities/Document.php index 1a6d71b..302812a 100644 --- a/src/AtolOnline/Entities/Document.php +++ b/src/AtolOnline/Entities/Document.php @@ -10,6 +10,8 @@ namespace AtolOnline\Entities; use AtolOnline\Exceptions\AtolCashierTooLongException; +use AtolOnline\Exceptions\AtolException; +use AtolOnline\Exceptions\AtolInvalidJsonException; /** * Класс, описывающий документ @@ -315,7 +317,89 @@ class Document extends Entity } /** - * @inheritDoc + * Собирает объект документа из сырой json-строки + * + * @param string $json + * @return \AtolOnline\Entities\Document + * @throws \AtolOnline\Exceptions\AtolEmailTooLongException + * @throws \AtolOnline\Exceptions\AtolEmailValidateException + * @throws \AtolOnline\Exceptions\AtolException + * @throws \AtolOnline\Exceptions\AtolInnWrongLengthException + * @throws \AtolOnline\Exceptions\AtolInvalidJsonException + * @throws \AtolOnline\Exceptions\AtolNameTooLongException + * @throws \AtolOnline\Exceptions\AtolPaymentAddressTooLongException + * @throws \AtolOnline\Exceptions\AtolPhoneTooLongException + * @throws \AtolOnline\Exceptions\AtolPriceTooHighException + * @throws \AtolOnline\Exceptions\AtolTooManyException + * @throws \AtolOnline\Exceptions\AtolTooManyItemsException + * @throws \AtolOnline\Exceptions\AtolTooManyPaymentsException + * @throws \AtolOnline\Exceptions\AtolUnitTooLongException + * @throws \AtolOnline\Exceptions\AtolUserdataTooLongException + */ + public static function fromRaw(string $json) + { + $object = json_decode($json); + if (json_last_error() !== JSON_ERROR_NONE) { + throw new AtolInvalidJsonException(); + } + $doc = new self(); + if ($object->company) { + $doc->setCompany(new Company( + $object->company->sno ?? null, + $object->company->inn ?? null, + $object->company->payment_address ?? null, + $object->company->email ?? null + )); + } + if ($object->client) { + $doc->setClient(new Client( + $object->client->name ?? null, + $object->client->phone ?? null, + $object->client->email ?? null, + $object->client->inn ?? null + )); + } + if ($object->items) { + foreach ($object->items as $obj_item) { + $item = new Item( + $obj_item->name ?? null, + $obj_item->price ?? null, + $obj_item->quantity ?? null, + $obj_item->measurement_unit ?? null, + $obj_item->vat->type ?? null, + $obj_item->payment_object ?? null, + $obj_item->payment_method ?? null + ); + if (!empty($obj_item->user_data)) { + $item->setUserData($obj_item->user_data ?? null); + } + $doc->addItem($item); + } + } + if ($object->payments) { + foreach ($object->payments as $obj_payment) { + $doc->payments->add(new Payment( + $obj_payment->type, + $obj_payment->sum + )); + } + } + //if ($object->vats) { + // foreach ($object->vats as $obj_vat) { + // $doc->vats->add(new Vat( + // $obj_vat->type + // )); + // } + //} + if ($object->total != $doc->calcTotal()) { + throw new AtolException('Real total sum not equals to provided in JSON one'); + } + return $doc; + } + + /** + * Возвращает массив для кодирования в json + * * @throws \Exception */ public function jsonSerialize() diff --git a/src/AtolOnline/Exceptions/AtolInvalidJsonException.php b/src/AtolOnline/Exceptions/AtolInvalidJsonException.php new file mode 100644 index 0000000..4c53a5a --- /dev/null +++ b/src/AtolOnline/Exceptions/AtolInvalidJsonException.php @@ -0,0 +1,32 @@ +