Skip to content

Commit

Permalink
add perception validator + summary detail validations
Browse files Browse the repository at this point in the history
  • Loading branch information
giansalex committed Jan 24, 2018
1 parent d1680c0 commit e08c574
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 64 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
1 change: 1 addition & 0 deletions src/Validator/Loader/InvoiceLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]),
Expand Down
1 change: 1 addition & 0 deletions src/Validator/Loader/NoteLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', [
Expand Down
5 changes: 2 additions & 3 deletions src/Validator/Loader/SalePerceptionLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
30 changes: 22 additions & 8 deletions src/Validator/Loader/SummaryDetailLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', [
Expand All @@ -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;
}
Expand Down
47 changes: 0 additions & 47 deletions src/Validator/Loader/SummaryDetailV2Loader.php

This file was deleted.

33 changes: 33 additions & 0 deletions src/Validator/Loader/SummaryPerceptionLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Created by PhpStorm.
* User: Administrador
* Date: 24/01/2018
* Time: 11:34 AM
*/

namespace Greenter\Validator\Loader;

use Greenter\Validator\LoaderMetadataInterface;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints as Assert;

/**
* Class SummaryPerceptionLoader
*/
class SummaryPerceptionLoader implements LoaderMetadataInterface
{
public function load(ClassMetadata $metadata)
{
$metadata->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());
}
}
10 changes: 6 additions & 4 deletions tests/Validator/FeInvoiceValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit e08c574

Please sign in to comment.