From e08c574af4aef4d9c3184047a2cbbbd42fe73e8c Mon Sep 17 00:00:00 2001 From: giansalex Date: Wed, 24 Jan 2018 12:03:32 -0500 Subject: [PATCH] add perception validator + summary detail validations --- README.md | 4 +- src/Validator/Loader/InvoiceLoader.php | 1 + src/Validator/Loader/NoteLoader.php | 1 + src/Validator/Loader/SalePerceptionLoader.php | 5 +- src/Validator/Loader/SummaryDetailLoader.php | 30 ++++++++---- .../Loader/SummaryDetailV2Loader.php | 47 ------------------- .../Loader/SummaryPerceptionLoader.php | 33 +++++++++++++ tests/Validator/FeInvoiceValidatorTest.php | 10 ++-- 8 files changed, 67 insertions(+), 64 deletions(-) delete mode 100644 src/Validator/Loader/SummaryDetailV2Loader.php create mode 100644 src/Validator/Loader/SummaryPerceptionLoader.php diff --git a/README.md b/README.md index 8e30360..71e0b2d 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,8 @@ [![Codacy Badge](https://api.codacy.com/project/badge/Grade/b237d274d88d47fbab43ddac252d73a9)](https://www.codacy.com/app/giansalex/greenter-validator?utm_source=github.com&utm_medium=referral&utm_content=giansalex/greenter-validator&utm_campaign=Badge_Grade) Symfony Validator for [Greenter](https://github.com/giansalex/greenter) -## Composer - +## Install +Using composer from [packagist.org](https://packagist.org/packages/greenter/validator) ```bash composer require greenter/validator ``` diff --git a/src/Validator/Loader/InvoiceLoader.php b/src/Validator/Loader/InvoiceLoader.php index 4db8033..08d173e 100644 --- a/src/Validator/Loader/InvoiceLoader.php +++ b/src/Validator/Loader/InvoiceLoader.php @@ -35,6 +35,7 @@ public function load(ClassMetadata $metadata) new Assert\NotBlank(), new Assert\Date(), ]); + $metadata->addPropertyConstraint('fecVencimiento', new Assert\Date()); $metadata->addPropertyConstraints('tipoMoneda', [ new Assert\NotBlank(), new Assert\Length(['max' => 3]), diff --git a/src/Validator/Loader/NoteLoader.php b/src/Validator/Loader/NoteLoader.php index 3bb2235..cebe26e 100644 --- a/src/Validator/Loader/NoteLoader.php +++ b/src/Validator/Loader/NoteLoader.php @@ -65,6 +65,7 @@ public function load(ClassMetadata $metadata) ]); $metadata->addPropertyConstraint('details', new Assert\Valid()); $metadata->addPropertyConstraint('legends', new Assert\Valid()); + $metadata->addPropertyConstraint('guias', new Assert\Valid()); $metadata->addPropertyConstraint('relDocs', new Assert\Valid()); $metadata->addPropertyConstraint('perception', new Assert\Valid()); $metadata->addPropertyConstraints('codMotivo', [ diff --git a/src/Validator/Loader/SalePerceptionLoader.php b/src/Validator/Loader/SalePerceptionLoader.php index 4fc8ab1..4624727 100644 --- a/src/Validator/Loader/SalePerceptionLoader.php +++ b/src/Validator/Loader/SalePerceptionLoader.php @@ -18,9 +18,8 @@ public function load(ClassMetadata $metadata) { $metadata->addPropertyConstraints('codReg', [ new Assert\NotBlank(), - new Assert\Length([ - 'min' => 2, - 'max' => 2, + new Assert\Choice([ + 'choices' => ['01', '02', '03'], ]), ]); // $metadata->addPropertyConstraint('tasa', new Assert\NotBlank()); diff --git a/src/Validator/Loader/SummaryDetailLoader.php b/src/Validator/Loader/SummaryDetailLoader.php index 5a7dd7d..53c5c9b 100644 --- a/src/Validator/Loader/SummaryDetailLoader.php +++ b/src/Validator/Loader/SummaryDetailLoader.php @@ -21,14 +21,7 @@ public function load(ClassMetadata $metadata) $metadata->addPropertyConstraints('tipoDoc', [ new Assert\NotBlank(), new Assert\Choice([ - 'choices' => ['03', '07', '08'], - ]), - ]); - $metadata->addPropertyConstraints('serieNro', [ - new Assert\NotBlank(), - new Assert\Regex([ - 'pattern' => '/^[B][A-Z0-9]{3}-[0-9]{1,8}$/', - 'message' => 'La serie no cumple el formato BXXX', + 'choices' => ['03', '07', '08', '12'], ]), ]); $metadata->addPropertyConstraints('clienteTipo', [ @@ -48,11 +41,32 @@ public function load(ClassMetadata $metadata) ]), ]); $metadata->addPropertyConstraint('docReferencia', new Assert\Valid()); + $metadata->addPropertyConstraint('percepcion', new Assert\Valid()); $metadata->addPropertyConstraint('total', new Assert\NotBlank()); $metadata->addPropertyConstraint('mtoIGV', new Assert\NotBlank()); $callback = function ($object, ExecutionContextInterface $context) { /** @var $object SummaryDetail */ + $pattern = $object->getTipoDoc() == '12' ? "/^[a-zA-Z0-9]{1,20}(-[0-9]{1,20})$/" : "/^[B][A-Z0-9]{3}-[0-9]{1,8}$/"; + if (!preg_match($pattern, $object->getSerieNro())) { + $context->buildViolation('Nro de documento no cumple con el formato para tipo doc. '.$object->getTipoDoc()) + ->atPath('serieNro') + ->addViolation(); + } + + if ($object->getTipoDoc() == '07' || $object->getTipoDoc() == '08') { + if (empty($object->getDocReferencia())) { + $context->buildViolation('Necesita un documento de referencia para tipo doc. '.$object->getTipoDoc()) + ->atPath('docReferencia') + ->addViolation(); + } else if (!in_array($object->getDocReferencia()->getTipoDoc(), ['03', '12'])) { + $context->buildViolation('Documento de referencia solo puede ser Ticket(12) o Boleta(03)') + ->atPath('docReferencia') + ->addViolation(); + } + + } + if (!($object->getTotal() > 750)) { return; } diff --git a/src/Validator/Loader/SummaryDetailV2Loader.php b/src/Validator/Loader/SummaryDetailV2Loader.php deleted file mode 100644 index 13559c0..0000000 --- a/src/Validator/Loader/SummaryDetailV2Loader.php +++ /dev/null @@ -1,47 +0,0 @@ -addPropertyConstraints('tipoDoc', [ - new Assert\NotBlank(), - new Assert\Length([ - 'min' => 2, - 'max' => 2, - ]), - ]); - $metadata->addPropertyConstraints('serieNro', [ - new Assert\NotBlank(), - new Assert\Length(['max' => 13]), - ]); - $metadata->addPropertyConstraints('clienteTipo', [ - new Assert\NotBlank(), - new Assert\Length(['max' => 1]), - ]); - $metadata->addPropertyConstraints('clienteNro', [ - new Assert\NotBlank(), - new Assert\Length(['max' => 20]), - ]); - $metadata->addPropertyConstraints('estado', [ - new Assert\NotBlank(), - new Assert\Length(['max' => 1]), - ]); - $metadata->addPropertyConstraint('docReferencia', new Assert\Valid()); - $metadata->addPropertyConstraint('total', new Assert\NotBlank()); - $metadata->addPropertyConstraint('mtoIGV', new Assert\NotBlank()); - $metadata->addPropertyConstraint('mtoISC', new Assert\NotBlank()); - } -} diff --git a/src/Validator/Loader/SummaryPerceptionLoader.php b/src/Validator/Loader/SummaryPerceptionLoader.php new file mode 100644 index 0000000..6ee2bf1 --- /dev/null +++ b/src/Validator/Loader/SummaryPerceptionLoader.php @@ -0,0 +1,33 @@ +addPropertyConstraints('codReg', [ + new Assert\NotBlank(), + new Assert\Choice([ + 'choices' => ['01', '02', '03'], + ]), + ]); + $metadata->addPropertyConstraint('tasa', new Assert\NotBlank()); + $metadata->addPropertyConstraint('mtoBase', new Assert\NotBlank()); + $metadata->addPropertyConstraint('mto', new Assert\NotBlank()); + $metadata->addPropertyConstraint('mtoTotal', new Assert\NotBlank()); + } +} \ No newline at end of file diff --git a/tests/Validator/FeInvoiceValidatorTest.php b/tests/Validator/FeInvoiceValidatorTest.php index 5edad50..ab7004f 100644 --- a/tests/Validator/FeInvoiceValidatorTest.php +++ b/tests/Validator/FeInvoiceValidatorTest.php @@ -55,15 +55,13 @@ private function getInvoice() ->setSumDsctoGlobal(12) ->setMtoDescuentos(23) ->setTipoOperacion('2') + ->setFecVencimiento(new \DateTime()) ->setPerception((new SalePerception()) ->setCodReg('01') ->setMto(2) ->setMtoBase(3) ->setMtoTotal(4) - )->setGuias([(new Document()) - ->setTipoDoc('09') - ->setNroDoc('T001-1') - ])->setCompra('001-12112') + )->setCompra('001-12112') ->setDetraccion((new Detraction()) ->setMount(2228.3) ->setPercent(9) @@ -94,6 +92,10 @@ private function getInvoice() ->setTipoDoc('01') ->setNroDoc('F001-123') ]) + ->setGuias([(new Document()) + ->setTipoDoc('09') + ->setNroDoc('T001-1') + ]) ->setTipoDoc('01') ->setSerie('F001') ->setCorrelativo('123')