Поддержка `correction_info`

pull/15/head
Anthony Axenov 2021-12-03 20:09:14 +08:00
parent 05fd25e810
commit d533164d1b
9 changed files with 332 additions and 142 deletions

View File

@ -142,6 +142,11 @@ final class Constraints
*/
const MAX_LENGTH_ITEM_CODE = 32;
/**
* Формат даты документа коррекции
*/
const CORRECTION_DATE_FORMAT = 'd.m.Y';
/**
* Регулярное выражение для валидации строки ИНН
*

View File

@ -162,6 +162,11 @@ final class Ffd105Tags
*/
const CORRECTION_TYPE = 1173;
/**
* Дата документа основания для коррекции
*/
const CORRECTION_DATE = 1178;
/**
* Сумма по чеку (БСО) наличными
*/

View File

@ -66,10 +66,7 @@ final class Company extends Entity
string $inn,
string $payment_address,
) {
$this->setEmail($email);
$this->setSno($sno);
$this->setInn($inn);
$this->setPaymentAddress($payment_address);
$this->setEmail($email)->setSno($sno)->setInn($inn)->setPaymentAddress($payment_address);
}
/**
@ -92,8 +89,7 @@ final class Company extends Entity
public function setSno(string $sno): self
{
$sno = trim($sno);
SnoTypes::isValid($sno);
$this->sno = $sno;
SnoTypes::isValid($sno) && $this->sno = $sno;
return $this;
}

View File

@ -0,0 +1,157 @@
<?php
/*
* Copyright (c) 2020-2021 Антон Аксенов (Anthony Axenov)
*
* This code is licensed under MIT.
* Этот код распространяется по лицензии MIT.
* https://github.com/anthonyaxenov/atol-online/blob/master/LICENSE
*/
declare(strict_types = 1);
namespace AtolOnline\Entities;
use AtolOnline\Constants\Constraints;
use AtolOnline\Enums\CorrectionTypes;
use AtolOnline\Exceptions\{
EmptyCorrectionNumberException,
InvalidCorrectionDateException,
InvalidEnumValueException
};
use DateTime;
use Exception;
use JetBrains\PhpStorm\{
ArrayShape,
Pure
};
/**
* Класс, описывающий данные коррекции
*
* @see https://online.atol.ru/files/API_atol_online_v4.pdf Документация, стр 35
*/
class CorrectionInfo extends Entity
{
/**
* @var string|null Тип коррекции (1173)
*/
protected ?string $type = null;
/**
* @var string|null Дата документа основания для коррекции (1178)
*/
protected ?string $date = null;
/**
* @var string|null Номер документа основания для коррекции (1179)
*/
protected ?string $number = null;
/**
* Конструктор
*
* @param string $type Тип коррекции
* @param string $date Дата документа
* @param string $number Номер документа
* @throws InvalidEnumValueException
* @throws InvalidCorrectionDateException
* @throws EmptyCorrectionNumberException
*/
public function __construct(string $type, string $date, string $number)
{
$this->setType($type)->setDate($date)->setNumber($number);
}
/**
* Возвращает тип коррекции
*
* @return string|null
*/
public function getType(): ?string
{
return $this->type;
}
/**
* Устанавливает тип коррекции
*
* @param string $type
* @return $this
* @throws InvalidEnumValueException
*/
public function setType(string $type): self
{
$type = trim($type);
CorrectionTypes::isValid($type) && $this->type = $type;
return $this;
}
/**
* Возвращает дату документа основания для коррекции
*
* @return string|null
*/
public function getDate(): ?string
{
return $this->date;
}
/**
* Устанавливает дату документа основания для коррекции
*
* @param DateTime|string $date Строковая дата в формате d.m.Y либо объект DateTime с датой
* @return $this
* @throws InvalidCorrectionDateException
*/
public function setDate(DateTime|string $date): self
{
try {
if (is_string($date)) {
$date = new DateTime(trim($date));
}
$this->date = $date->format(Constraints::CORRECTION_DATE_FORMAT);
} catch (Exception $e) {
throw new InvalidCorrectionDateException($date, $e->getMessage());
}
return $this;
}
/**
* Возвращает установленный номер документа основания для коррекции
*
* @return string|null
*/
public function getNumber(): ?string
{
return $this->number;
}
/**
* Устанавливает номер документа основания для коррекции
*
* @param string $number
* @return $this
* @throws EmptyCorrectionNumberException
*/
public function setNumber(string $number): self
{
$number = trim($number);
empty($number) && throw new EmptyCorrectionNumberException();
$this->number = $number;
return $this;
}
/**
* @inheritDoc
*/
#[Pure]
#[ArrayShape(['type' => 'string', 'base_date' => 'string', 'base_number' => 'string'])]
public function jsonSerialize(): array
{
return [
'type' => $this->getType(),
'base_date' => $this->getDate(),
'base_number' => $this->getNumber(),
];
}
}

View File

@ -1,134 +0,0 @@
<?php
/*
* Copyright (c) 2020-2021 Антон Аксенов (Anthony Axenov)
*
* This code is licensed under MIT.
* Этот код распространяется по лицензии MIT.
* https://github.com/anthonyaxenov/atol-online/blob/master/LICENSE
*/
declare(strict_types = 1);
namespace AtolOnline\Entities;
/**
* Класс todoCorrectionInfo, описывающий данные чек коррекции
*/
class todoCorrectionInfo extends Entity
{
/**
* @var string Тип коррекции. Тег ФФД - 1173.
*/
protected string $type;
/**
* @var string Дата документа основания для коррекции. Тег ФФД - 1178.
*/
protected string $base_date;
/**
* @var string Номер документа основания для коррекции. Тег ФФД - 1179.
*/
protected string $base_number;
/**
* todoCorrectionInfo constructor.
*
* @param string|null $type Тип коррекции
* @param string|null $base_date Дата документа
* @param string|null $base_number Номер документа
*/
public function __construct(
?string $type = null,
?string $base_date = null,
?string $base_number = null
) {
$type && $this->setType($type);
$base_date && $this->setDate($base_date);
$base_number && $this->setNumber($base_number);
}
/**
* Возвращает номер документа основания для коррекции.
* Тег ФФД - 1179.
*
* @return string|null
*/
public function getNumber(): ?string
{
return $this->base_number;
}
/**
* Устанавливает номер документа основания для коррекции.
* Тег ФФД - 1179.
*
* @param string $number
* @return $this
*/
public function setNumber(string $number): todoCorrectionInfo
{
$this->base_number = trim($number);
return $this;
}
/**
* Возвращает дату документа основания для коррекции.
* Тег ФФД - 1178.
*
* @return string|null
*/
public function getDate(): ?string
{
return $this->base_date;
}
/**
* Устанавливает дату документа основания для коррекции.
* Тег ФФД - 1178.
*
* @param string $date Строка в формате d.m.Y
* @return $this
*/
public function setDate(string $date): todoCorrectionInfo
{
$this->base_date = $date;
return $this;
}
/**
* Возвращает тип коррекции.
* Тег ФФД - 1173.
*
* @return string|null
*/
public function getType(): ?string
{
return $this->type;
}
/**
* Устанавливает тип коррекции.
* Тег ФФД - 1173.
*
* @param string $type
* @return $this
*/
public function setType(string $type): todoCorrectionInfo
{
$this->type = $type;
return $this;
}
/**
* @inheritDoc
*/
public function jsonSerialize(): array
{
return [
'type' => $this->getType() ?? '',
'base_date' => $this->getDate() ?? '',
'base_number' => $this->getNumber() ?? '',
];
}
}

View File

@ -24,8 +24,7 @@ abstract class Enum extends \MyCLabs\Enum\Enum
*/
public static function isValid($value)
{
return parent::isValid($value)
?: throw new InvalidEnumValueException(static::class, $value);
return parent::isValid($value) ?: throw new InvalidEnumValueException(static::class, $value);
}
/**

View File

@ -0,0 +1,23 @@
<?php
/*
* Copyright (c) 2020-2021 Антон Аксенов (Anthony Axenov)
*
* This code is licensed under MIT.
* Этот код распространяется по лицензии MIT.
* https://github.com/anthonyaxenov/atol-online/blob/master/LICENSE
*/
declare(strict_types = 1);
namespace AtolOnline\Exceptions;
use AtolOnline\Constants\Ffd105Tags;
/**
* Исключение, возникающее при пустом номере документа коррекции
*/
class EmptyCorrectionNumberException extends AtolException
{
protected $message = 'Номер документа коррекции не может быть пустым';
protected array $ffd_tags = [Ffd105Tags::CORRECTION_DATE];
}

View File

@ -0,0 +1,33 @@
<?php
/*
* Copyright (c) 2020-2021 Антон Аксенов (Anthony Axenov)
*
* This code is licensed under MIT.
* Этот код распространяется по лицензии MIT.
* https://github.com/anthonyaxenov/atol-online/blob/master/LICENSE
*/
declare(strict_types = 1);
namespace AtolOnline\Exceptions;
use AtolOnline\Constants\Ffd105Tags;
/**
* Исключение, возникающее при попытке указать некорректную дату коррекции
*/
class InvalidCorrectionDateException extends AtolException
{
protected array $ffd_tags = [Ffd105Tags::CORRECTION_DATE];
/**
* Конструктор
*
* @param string $date
* @param string $message
*/
public function __construct(string $date = '', string $message = '')
{
parent::__construct("Ошибка даты документа коррекции '$date': " . $message);
}
}

View File

@ -0,0 +1,106 @@
<?php
/*
* Copyright (c) 2020-2021 Антон Аксенов (Anthony Axenov)
*
* This code is licensed under MIT.
* Этот код распространяется по лицензии MIT.
* https://github.com/anthonyaxenov/atol-online/blob/master/LICENSE
*/
namespace AtolOnline\Tests\Entities;
use AtolOnline\{
Entities\CorrectionInfo,
Enums\CorrectionTypes,
Exceptions\EmptyCorrectionNumberException,
Exceptions\InvalidCorrectionDateException,
Exceptions\InvalidEnumValueException,
Helpers,
Tests\BasicTestCase
};
/**
* Набор тестов для проверки работы класса данных коррекции
*/
class CorrectionInfoTest extends BasicTestCase
{
/**
* Тестирует конструктор
*
* @covers \AtolOnline\Entities\CorrectionInfo
* @covers \AtolOnline\Entities\CorrectionInfo::setType
* @covers \AtolOnline\Entities\CorrectionInfo::getType
* @covers \AtolOnline\Entities\CorrectionInfo::setDate
* @covers \AtolOnline\Entities\CorrectionInfo::getDate
* @covers \AtolOnline\Entities\CorrectionInfo::setNumber
* @covers \AtolOnline\Entities\CorrectionInfo::getNumber
* @covers \AtolOnline\Entities\CorrectionInfo::jsonSerialize
* @return void
* @throws InvalidEnumValueException
* @throws InvalidCorrectionDateException
* @throws EmptyCorrectionNumberException
*/
public function testConstructor(): void
{
$this->assertAtolable(
new CorrectionInfo(CorrectionTypes::SELF, '01.01.2021', $number = Helpers::randomStr()),
[
'type' => CorrectionTypes::SELF,
'base_date' => '01.01.2021',
'base_number' => $number,
]
);
}
/**
* Тестирует исключение при некорректном типе
*
* @covers \AtolOnline\Entities\CorrectionInfo
* @covers \AtolOnline\Entities\CorrectionInfo::setType
* @covers \AtolOnline\Enums\CorrectionTypes::isValid
* @covers \AtolOnline\Exceptions\InvalidEnumValueException
* @return void
* @throws EmptyCorrectionNumberException
* @throws InvalidCorrectionDateException
* @throws InvalidEnumValueException
*/
public function testInvalidEnumValueException(): void
{
$this->expectException(InvalidEnumValueException::class);
$this->expectExceptionMessage('Некорректное значение AtolOnline\Enums\CorrectionTypes::wrong_value');
new CorrectionInfo('wrong_value', '01.01.2021', Helpers::randomStr());
}
/**
* Тестирует исключение при некорректной дате
*
* @covers \AtolOnline\Entities\CorrectionInfo
* @covers \AtolOnline\Entities\CorrectionInfo::setDate
* @covers \AtolOnline\Enums\CorrectionTypes::isValid
* @covers \AtolOnline\Exceptions\InvalidCorrectionDateException
* @return void
* @throws EmptyCorrectionNumberException
* @throws InvalidCorrectionDateException
* @throws InvalidEnumValueException
*/
public function testInvalidCorrectionDateException(): void
{
$this->expectException(InvalidCorrectionDateException::class);
new CorrectionInfo(CorrectionTypes::SELF, Helpers::randomStr(), Helpers::randomStr());
}
/**
* Тестирует исключение при некорректной дате
*
* @covers \AtolOnline\Entities\CorrectionInfo
* @covers \AtolOnline\Entities\CorrectionInfo::setNumber
* @covers \AtolOnline\Enums\CorrectionTypes::isValid
* @covers \AtolOnline\Exceptions\EmptyCorrectionNumberException
* @return void
*/
public function testEmptyCorrectionNumberException(): void
{
$this->expectException(EmptyCorrectionNumberException::class);
new CorrectionInfo(CorrectionTypes::SELF, '01.01.2021', "\n\r\t\0");
}
}