From 6787ce3ad72dac90930d99dd2cf52b07724532e1 Mon Sep 17 00:00:00 2001 From: AnthonyAxenov Date: Sat, 11 Dec 2021 15:53:57 +0800 Subject: [PATCH] =?UTF-8?q?=D0=9A=D0=BB=D0=B0=D1=81=D1=81=20=D0=B4=D0=BE?= =?UTF-8?q?=D0=BA=D1=83=D0=BC=D0=B5=D0=BD=D1=82=D0=B0=20=D0=BA=D0=BE=D1=80?= =?UTF-8?q?=D1=80=D0=B5=D0=BA=D1=86=D0=B8=D0=B8=20`Correction`=20=D1=81=20?= =?UTF-8?q?=D0=BF=D0=BE=D0=BA=D1=80=D1=8B=D1=82=D0=B8=D0=B5=D0=BC=20=D0=B8?= =?UTF-8?q?=20=D0=B2=D1=81=D1=8F=D0=BA=D0=B0=D1=8F=20=D0=BC=D0=B5=D0=BB?= =?UTF-8?q?=D0=BE=D1=87=D1=91=D0=B2=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - финализация Receipt + Payment - фиксы phpdoc --- src/Collections/EntityCollection.php | 2 +- src/Entities/Company.php | 16 +- src/Entities/Correction.php | 215 ++++++++++++++++++ src/Entities/Payment.php | 11 +- src/Entities/Receipt.php | 27 ++- src/Enums/DocumentTypes.php | 4 +- .../Tests/Entities/CorrectionTest.php | 138 +++++++++++ 7 files changed, 380 insertions(+), 33 deletions(-) create mode 100644 src/Entities/Correction.php create mode 100644 tests/AtolOnline/Tests/Entities/CorrectionTest.php diff --git a/src/Collections/EntityCollection.php b/src/Collections/EntityCollection.php index 3552583..7c92f0d 100644 --- a/src/Collections/EntityCollection.php +++ b/src/Collections/EntityCollection.php @@ -20,7 +20,7 @@ use Illuminate\Support\Collection; abstract class EntityCollection extends Collection { /** - * @return array + * @inheritDoc * @throws InvalidEntityInCollectionException */ public function jsonSerialize(): array diff --git a/src/Entities/Company.php b/src/Entities/Company.php index e5d9804..ed665ca 100644 --- a/src/Entities/Company.php +++ b/src/Entities/Company.php @@ -11,18 +11,14 @@ declare(strict_types = 1); namespace AtolOnline\Entities; -use AtolOnline\{ - Constants\Constraints, - Enums\SnoTypes, - Traits\HasEmail, - Traits\HasInn}; -use AtolOnline\Exceptions\{ - InvalidEmailException, +use AtolOnline\{Constants\Constraints, Enums\SnoTypes, Traits\HasEmail, Traits\HasInn}; +use AtolOnline\Exceptions\{InvalidEmailException, InvalidEnumValueException, InvalidInnLengthException, InvalidPaymentAddressException, TooLongEmailException, - TooLongPaymentAddressException}; + TooLongPaymentAddressException +}; use JetBrains\PhpStorm\ArrayShape; /** @@ -59,8 +55,8 @@ final class Company extends Entity * @throws TooLongPaymentAddressException */ public function __construct( - string $email, - string $sno, + string $email, //TODO сделать необязательным здесь + string $sno, //TODO сделать необязательным здесь string $inn, string $payment_address, ) { diff --git a/src/Entities/Correction.php b/src/Entities/Correction.php new file mode 100644 index 0000000..70a2193 --- /dev/null +++ b/src/Entities/Correction.php @@ -0,0 +1,215 @@ +setCompany($company)->setCorrectionInfo($correction_info)->setPayments($payments)->setVats($vats); + } + + /** + * Возвращает установленного продавца + * + * @return Company + */ + public function getCompany(): Company + { + return $this->company; + } + + /** + * Устанаваливает продавца + * + * @param Company $company + * @return $this + */ + public function setCompany(Company $company): self + { + $this->company = $company; + return $this; + } + + /** + * Возвращает установленного кассира + * + * @return string|null + */ + public function getCashier(): ?string + { + return $this->cashier; + } + + /** + * Устанаваливает кассира + * + * @param string|null $cashier + * @return $this + * @throws TooLongCashierException + */ + public function setCashier(?string $cashier): self + { + if (is_string($cashier)) { + $cashier = trim($cashier); + if (mb_strlen($cashier) > Constraints::MAX_LENGTH_CASHIER_NAME) { + throw new TooLongCashierException($cashier); + } + } + $this->cashier = $cashier ?: null; + return $this; + } + + /** + * Возвращает установленные данные коррекции + * + * @return CorrectionInfo + */ + public function getCorrectionInfo(): CorrectionInfo + { + return $this->correction_info; + } + + /** + * Устанавливает данные коррекции + * + * @param CorrectionInfo $correction_info + * @return Correction + */ + public function setCorrectionInfo(CorrectionInfo $correction_info): Correction + { + $this->correction_info = $correction_info; + return $this; + } + + /** + * Возвращает установленную коллекцию оплат + * + * @return Payments + */ + public function getPayments(): Payments + { + return $this->payments; + } + + /** + * Устанаваливает коллекцию оплат + * + * @param Payments $payments + * @return $this + * @throws InvalidEntityInCollectionException + */ + public function setPayments(Payments $payments): self + { + $payments->checkCount(); + $payments->checkItemsClasses(); + $this->payments = $payments; + return $this; + } + + /** + * Возвращает установленную коллекцию ставок НДС + * + * @return Vats|null + */ + public function getVats(): ?Vats + { + return $this->vats ?? new Vats(); + } + + /** + * Устанаваливает коллекцию ставок НДС + * + * @param Vats|null $vats + * @return $this + * @throws Exception + */ + public function setVats(?Vats $vats): self + { + $vats->checkCount(); + $vats->checkItemsClasses(); + $this->vats = $vats; + return $this; + } + + /** + * @inheritDoc + * @throws InvalidEntityInCollectionException + */ + #[ArrayShape([ + 'company' => "\AtolOnline\Entities\Company", + 'correction_info' => "\AtolOnline\Entities\CorrectionInfo", + 'payments' => "array", + 'vats' => "\AtolOnline\Collections\Vats|null", + 'cashier' => "null|string" + ])] + public function jsonSerialize(): array + { + $json = [ + 'company' => $this->getCompany(), + 'correction_info' => $this->getCorrectionInfo(), + 'payments' => $this->getPayments()->jsonSerialize(), + 'vats' => $this->getVats(), + ]; + !is_null($this->getCashier()) && $json['cashier'] = $this->getCashier(); + return $json; + } +} diff --git a/src/Entities/Payment.php b/src/Entities/Payment.php index 2982833..f10d671 100644 --- a/src/Entities/Payment.php +++ b/src/Entities/Payment.php @@ -13,20 +13,15 @@ namespace AtolOnline\Entities; use AtolOnline\Constants\Constraints; use AtolOnline\Enums\PaymentTypes; -use AtolOnline\Exceptions\{ - InvalidEnumValueException, - NegativePaymentSumException, - TooHighPaymentSumException,}; -use JetBrains\PhpStorm\{ - ArrayShape, - Pure}; +use AtolOnline\Exceptions\{InvalidEnumValueException, NegativePaymentSumException, TooHighPaymentSumException,}; +use JetBrains\PhpStorm\{ArrayShape, Pure}; /** * Класс, описывающий оплату * * @see https://online.atol.ru/files/API_atol_online_v4.pdf Документация, стр 30 */ -class Payment extends Entity +final class Payment extends Entity { /** * @var int Тип оплаты diff --git a/src/Entities/Receipt.php b/src/Entities/Receipt.php index 632d634..07b40c5 100644 --- a/src/Entities/Receipt.php +++ b/src/Entities/Receipt.php @@ -26,7 +26,7 @@ use Exception; * * @see https://online.atol.ru/files/API_atol_online_v4.pdf Документация, стр 17 */ -class Receipt extends Entity +final class Receipt extends Entity { /** * @var Client Покупатель @@ -35,6 +35,7 @@ class Receipt extends Entity /** * @var Company Продавец + * @todo вынести в трейт */ protected Company $company; @@ -55,6 +56,7 @@ class Receipt extends Entity /** * @var Payments Коллекция оплат + * @todo вынести в трейт */ protected Payments $payments; @@ -70,6 +72,7 @@ class Receipt extends Entity /** * @var string|null ФИО кассира + * @todo вынести в трейт */ protected ?string $cashier = null; @@ -112,7 +115,7 @@ class Receipt extends Entity * Устанаваливает покупателя * * @param Client $client - * @return Receipt + * @return $this */ public function setClient(Client $client): self { @@ -134,7 +137,7 @@ class Receipt extends Entity * Устанаваливает продавца * * @param Company $company - * @return Receipt + * @return $this */ public function setCompany(Company $company): self { @@ -156,7 +159,7 @@ class Receipt extends Entity * Устанаваливает агента * * @param AgentInfo|null $agent_info - * @return Receipt + * @return $this */ public function setAgentInfo(?AgentInfo $agent_info): self { @@ -178,7 +181,7 @@ class Receipt extends Entity * Поставщика * * @param Supplier|null $supplier - * @return Receipt + * @return $this */ public function setSupplier(?Supplier $supplier): self { @@ -199,12 +202,12 @@ class Receipt extends Entity /** * Устанаваливает коллекцию предметов расчёта * - * @todo исключение при пустой коллекции * @param Items $items - * @return Receipt + * @return $this * @throws EmptyItemsException * @throws InvalidEntityInCollectionException * @throws Exception + * @todo исключение при пустой коллекции */ public function setItems(Items $items): self { @@ -230,7 +233,7 @@ class Receipt extends Entity * Устанаваливает коллекцию оплат * * @param Payments $payments - * @return Receipt + * @return $this * @throws InvalidEntityInCollectionException */ public function setPayments(Payments $payments): self @@ -255,7 +258,7 @@ class Receipt extends Entity * Устанаваливает коллекцию ставок НДС * * @param Vats|null $vats - * @return Receipt + * @return $this * @throws Exception */ public function setVats(?Vats $vats): self @@ -292,7 +295,7 @@ class Receipt extends Entity * Устанаваливает кассира * * @param string|null $cashier - * @return Receipt + * @return $this * @throws TooLongCashierException */ public function setCashier(?string $cashier): self @@ -321,7 +324,7 @@ class Receipt extends Entity * Устанаваливает дополнительный реквизит чека * * @param string|null $add_check_props - * @return Receipt + * @return $this * @throws TooLongAddCheckPropException */ public function setAddCheckProps(?string $add_check_props): self @@ -350,7 +353,7 @@ class Receipt extends Entity * Устанаваливает дополнительный реквизит пользователя * * @param AdditionalUserProps|null $add_user_props - * @return Receipt + * @return $this */ public function setAddUserProps(?AdditionalUserProps $add_user_props): self { diff --git a/src/Enums/DocumentTypes.php b/src/Enums/DocumentTypes.php index 706a0d3..8cf0869 100644 --- a/src/Enums/DocumentTypes.php +++ b/src/Enums/DocumentTypes.php @@ -17,12 +17,12 @@ namespace AtolOnline\Enums; final class DocumentTypes extends Enum { /** - * Чек прихода, возврата прихода, расхода, возврата расхода + * Документ прихода, возврата прихода, расхода, возврата расхода */ const RECEIPT = 'receipt'; /** - * Чек коррекции + * Документ коррекции */ const CORRECTION = 'correction'; diff --git a/tests/AtolOnline/Tests/Entities/CorrectionTest.php b/tests/AtolOnline/Tests/Entities/CorrectionTest.php new file mode 100644 index 0000000..759cde4 --- /dev/null +++ b/tests/AtolOnline/Tests/Entities/CorrectionTest.php @@ -0,0 +1,138 @@ +newCorrection(); + $this->assertIsAtolable($correction); + } + + /** + * Тестирует установку валидного кассира + * + * @return void + * @covers \AtolOnline\Entities\Correction::setCashier + * @covers \AtolOnline\Entities\Correction::getCashier + * @covers \AtolOnline\Entities\Correction::jsonSerialize + * @throws EmptyCorrectionNumberException + * @throws InvalidCorrectionDateException + * @throws InvalidEntityInCollectionException + * @throws InvalidEnumValueException + * @throws NegativePaymentSumException + * @throws TooHighPaymentSumException + * @throws TooLongCashierException + */ + public function testCashier(): void + { + $correction = $this->newCorrection()->setCashier(Helpers::randomStr()); + $this->assertArrayHasKey('cashier', $correction->jsonSerialize()); + $this->assertEquals($correction->getCashier(), $correction->jsonSerialize()['cashier']); + } + + /** + * Тестирует обнуление кассира + * + * @param mixed $param + * @return void + * @dataProvider providerNullableStrings + * @covers \AtolOnline\Entities\Correction::setCashier + * @covers \AtolOnline\Entities\Correction::getCashier + * @throws EmptyCorrectionNumberException + * @throws InvalidCorrectionDateException + * @throws InvalidEntityInCollectionException + * @throws InvalidEnumValueException + * @throws NegativePaymentSumException + * @throws TooHighPaymentSumException + * @throws TooLongCashierException + */ + public function testNullableCashier(mixed $param): void + { + $this->assertNull($this->newCorrection()->setCashier($param)->getCashier()); + } + + /** + * Тестирует выброс исключения при установке слишком длинного кассира (лол) + * + * @return void + * @covers \AtolOnline\Entities\Correction::setCashier + * @covers \AtolOnline\Exceptions\TooLongCashierException + * @throws EmptyCorrectionNumberException + * @throws InvalidCorrectionDateException + * @throws InvalidEntityInCollectionException + * @throws InvalidEnumValueException + * @throws NegativePaymentSumException + * @throws TooHighPaymentSumException + * @throws TooLongCashierException + */ + public function testTooLongCashierException(): void + { + $this->expectException(TooLongCashierException::class); + $this->newCorrection()->setCashier(Helpers::randomStr(Constraints::MAX_LENGTH_CASHIER_NAME + 1)); + } + + /** + * Возвращает валидный тестовый объект чека + * + * @return Correction + * @throws InvalidEntityInCollectionException + * @throws InvalidEnumValueException + * @throws NegativePaymentSumException + * @throws TooHighPaymentSumException + * @throws EmptyCorrectionNumberException + * @throws InvalidCorrectionDateException + */ + protected function newCorrection(): Correction + { + return new Correction( + new Company('company@example.com', SnoTypes::OSN, '1234567890', 'https://example.com'), + new CorrectionInfo(CorrectionTypes::SELF, '01.01.2021', Helpers::randomStr()), + new Payments($this->generatePaymentObjects(2)), + new Vats($this->generateVatObjects(2)), + ); + } +}