Skip to content

Commit

Permalink
Merge pull request #4 from giansalex/feature/ubl-21
Browse files Browse the repository at this point in the history
Validator for UBL 2.1
  • Loading branch information
giansalex authored May 18, 2019
2 parents 73a9f25 + df31551 commit 46d377e
Show file tree
Hide file tree
Showing 18 changed files with 685 additions and 10 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Travis-CI](https://img.shields.io/travis/giansalex/greenter-validator.svg?label=travis-ci&branch=master&style=flat-square)](https://travis-ci.org/giansalex/greenter-validator)
[![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)
Symfony Validator for [Greenter](https://github.com/giansalex/greenter) (UBL v2.0, v2.1)

## Install
Using composer from [packagist.org](https://packagist.org/packages/greenter/validator)
Expand All @@ -18,6 +18,8 @@ use Greenter\Validator\SymfonyValidator;
$invoice = createInvoice();

$validator = new SymfonyValidator();
// Para UBL 2.1
// $validator = new SymfonyValidator(null, '2.1');
$errors = $validator->validate($invoice);

var_dump($errors);
Expand Down
1 change: 1 addition & 0 deletions src/Validator/Loader/AddressLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ public function load(ClassMetadata $metadata)
$metadata->addPropertyConstraint('distrito', new Assert\Length(['max' => 100]));
$metadata->addPropertyConstraint('urbanizacion', new Assert\Length(['max' => 25]));
$metadata->addPropertyConstraint('direccion', new Assert\Length(['max' => 100]));
$metadata->addPropertyConstraint('codLocal', new Assert\NotBlank());
}
}
1 change: 0 additions & 1 deletion src/Validator/Loader/SalePerceptionLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public function load(ClassMetadata $metadata)
'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());
Expand Down
11 changes: 9 additions & 2 deletions src/Validator/Loader/SummaryDetailLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,15 @@ 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());
$metadata->addPropertyConstraints('total', [
new Assert\NotBlank(),
new Assert\Type(['type' => 'numeric']),
]);
$metadata->addPropertyConstraints('mtoIGV', [
new Assert\NotBlank(),
new Assert\Type(['type' => 'numeric']),
]);
$metadata->addPropertyConstraint('mtoIvap', new Assert\Type(['type' => 'numeric']));
$metadata->addConstraint(new Assert\Callback([$this, 'validate']));
}

Expand Down
8 changes: 7 additions & 1 deletion src/Validator/Loader/SummaryLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@

namespace Greenter\Validator\Loader;

use DateTime;
use Greenter\Model\Summary\Summary;
use Greenter\Validator\Metadata\LoaderMetadataInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Greenter\Validator\Constraint as MyAssert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Mapping\ClassMetadata;

Expand Down Expand Up @@ -39,13 +41,17 @@ public function load(ClassMetadata $metadata)
new Assert\Count(['min' => 1, 'max' => 500, 'maxMessage' => 'Solo se permite un maximo de 500 items']),
new Assert\Valid(),
]);
$metadata->addPropertyConstraints('moneda', [
new Assert\NotBlank(),
new MyAssert\Currency(),
]);
$metadata->addConstraint(new Assert\Callback([$this, 'validate']));
}

public function validate($object, ExecutionContextInterface $context)
{
/**@var $object Summary */
if ($object->getFecResumen() > new \DateTime()) {
if ($object->getFecResumen() > new DateTime()) {
$context->buildViolation('2236')
->atPath('fecResumen')
->addViolation();
Expand Down
27 changes: 27 additions & 0 deletions src/Validator/Loader/v21/ChargeLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Greenter\Validator\Loader\v21;

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

class ChargeLoader implements LoaderMetadataInterface
{
public function load(ClassMetadata $metadata)
{
$metadata->addPropertyConstraints('factor', [
new Assert\NotBlank(),
new Assert\Type(['type' => 'numeric']),
]);
$metadata->addPropertyConstraints('monto', [
new Assert\NotBlank(),
new Assert\Type(['type' => 'numeric']),
]);
$metadata->addPropertyConstraints('montoBase', [
new Assert\NotBlank(),
new Assert\Type(['type' => 'numeric']),
]);
$metadata->addPropertyConstraint('codTipo', new Assert\NotBlank());
}
}
20 changes: 20 additions & 0 deletions src/Validator/Loader/v21/DetailAttributeLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php


namespace Greenter\Validator\Loader\v21;

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

class DetailAttributeLoader implements LoaderMetadataInterface
{
public function load(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('name', new Assert\NotBlank());
$metadata->addPropertyConstraint('code', new Assert\NotBlank());
$metadata->addPropertyConstraint('fecInicio', new Assert\DateTime());
$metadata->addPropertyConstraint('fecFin', new Assert\DateTime());
$metadata->addPropertyConstraint('duracion', new Assert\Type(['type' => 'int']));
}
}
25 changes: 25 additions & 0 deletions src/Validator/Loader/v21/DetractionLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Greenter\Validator\Loader\v21;

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

class DetractionLoader implements LoaderMetadataInterface
{
public function load(ClassMetadata $metadata)
{
$metadata->addPropertyConstraints('percent', [
new Assert\NotBlank(),
new Assert\Type(['type' => 'numeric']),
]);
$metadata->addPropertyConstraints('mount', [
new Assert\NotBlank(),
new Assert\Type(['type' => 'numeric']),
]);
$metadata->addPropertyConstraint('codMedioPago', new Assert\NotBlank());
$metadata->addPropertyConstraint('ctaBanco', new Assert\NotBlank());
$metadata->addPropertyConstraint('codBienDetraccion', new Assert\NotBlank());
}
}
91 changes: 91 additions & 0 deletions src/Validator/Loader/v21/InvoiceLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace Greenter\Validator\Loader\v21;

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

class InvoiceLoader implements LoaderMetadataInterface
{
public function load(ClassMetadata $metadata)
{
$metadata->addPropertyConstraints('tipoOperacion', [
new Assert\NotBlank(),
]);
$metadata->addPropertyConstraints('tipoDoc', [
new Assert\NotBlank(),
]);
$metadata->addPropertyConstraints('serie', [
new Assert\NotBlank(),
new Assert\Length(['max' => 4]),
]);
$metadata->addPropertyConstraints('correlativo', [
new Assert\NotBlank(),
new Assert\Length(['max' => 8]),
]);
$metadata->addPropertyConstraints('fechaEmision', [
new Assert\NotBlank(),
new Assert\DateTime(),
]);
$metadata->addPropertyConstraint('fecVencimiento', new Assert\DateTime());
$metadata->addPropertyConstraints('tipoMoneda', [
new Assert\NotBlank(),
new MyAssert\Currency(),
]);
$metadata->addPropertyConstraints('mtoOperGravadas', [
new Assert\Type(['type' => 'numeric']),
]);
$metadata->addPropertyConstraints('mtoOperInafectas', [
new Assert\Type(['type' => 'numeric']),
]);
$metadata->addPropertyConstraints('mtoOperExoneradas', [
new Assert\Type(['type' => 'numeric']),
]);
$metadata->addPropertyConstraints('mtoOperGratuitas', [
new Assert\Type(['type' => 'numeric']),
]);
$metadata->addPropertyConstraints('mtoOperExportacion', [
new Assert\Type(['type' => 'numeric']),
]);
$metadata->addPropertyConstraint('mtoIGV', new Assert\Type(['type' => 'numeric']));
$metadata->addPropertyConstraint('mtoISC', new Assert\Type(['type' => 'numeric']));
$metadata->addPropertyConstraint('mtoBaseIsc', new Assert\Type(['type' => 'numeric']));
$metadata->addPropertyConstraint('mtoOtrosTributos', new Assert\Type(['type' => 'numeric']));
$metadata->addPropertyConstraint('mtoBaseOth', new Assert\Type(['type' => 'numeric']));
$metadata->addPropertyConstraint('mtoDescuentos', new Assert\Type(['type' => 'numeric']));
$metadata->addPropertyConstraint('sumOtrosCargos', new Assert\Type(['type' => 'numeric']));
$metadata->addPropertyConstraint('totalAnticipos', new Assert\Type(['type' => 'numeric']));
$metadata->addPropertyConstraints('totalImpuestos', [
new Assert\NotBlank(),
new Assert\Type(['type' => 'numeric']),
]);
$metadata->addPropertyConstraints('valorVenta', [
new Assert\NotBlank(),
new Assert\Type(['type' => 'numeric']),
]);
$metadata->addPropertyConstraints('mtoImpVenta', [
new Assert\NotBlank(),
new Assert\Type(['type' => 'numeric']),
]);
$metadata->addPropertyConstraints('client', [
new Assert\NotBlank(),
new Assert\Valid(),
]);
$metadata->addPropertyConstraints('company', [
new Assert\NotBlank(),
new Assert\Valid(),
]);
$metadata->addPropertyConstraint('seller', new Assert\Valid());
$metadata->addPropertyConstraint('details', new Assert\Valid());
$metadata->addPropertyConstraint('legends', new Assert\Valid());
$metadata->addPropertyConstraint('guias', new Assert\Valid());
$metadata->addPropertyConstraint('anticipos', new Assert\Valid());
$metadata->addPropertyConstraint('detraccion', new Assert\Valid());
$metadata->addPropertyConstraint('relDocs', new Assert\Valid());
$metadata->addPropertyConstraint('perception', new Assert\Valid());
$metadata->addPropertyConstraint('cargos', new Assert\Valid());
$metadata->addPropertyConstraint('descuentos', new Assert\Valid());
}
}
89 changes: 89 additions & 0 deletions src/Validator/Loader/v21/NoteLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
/**
* Created by PhpStorm.
* User: Giansalex
* Date: 18/07/2017
* Time: 22:27.
*/

namespace Greenter\Validator\Loader\v21;

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

class NoteLoader implements LoaderMetadataInterface
{
public function load(ClassMetadata $metadata)
{
$metadata->addPropertyConstraints('tipoDoc', [
new Assert\NotBlank(),
]);
$metadata->addPropertyConstraints('serie', [
new Assert\NotBlank(),
new Assert\Length(['max' => 4]),
]);
$metadata->addPropertyConstraints('correlativo', [
new Assert\NotBlank(),
new Assert\Length(['max' => 8]),
]);
$metadata->addPropertyConstraints('fechaEmision', [
new Assert\NotBlank(),
new Assert\DateTime(),
]);
$metadata->addPropertyConstraints('tipoMoneda', [
new Assert\NotBlank(),
new MyAssert\Currency(),
]);
$metadata->addPropertyConstraint('codMotivo', new Assert\NotBlank());
$metadata->addPropertyConstraint('tipDocAfectado', new Assert\NotBlank());
$metadata->addPropertyConstraint('numDocfectado', new Assert\NotBlank());
$metadata->addPropertyConstraints('desMotivo', [
new Assert\NotBlank(),
new Assert\Length(['max' => 500]),
]);
$metadata->addPropertyConstraints('mtoOperGravadas', [
new Assert\Type(['type' => 'numeric']),
]);
$metadata->addPropertyConstraints('mtoOperInafectas', [
new Assert\Type(['type' => 'numeric']),
]);
$metadata->addPropertyConstraints('mtoOperExoneradas', [
new Assert\Type(['type' => 'numeric']),
]);
$metadata->addPropertyConstraints('mtoOperGratuitas', [
new Assert\Type(['type' => 'numeric']),
]);
$metadata->addPropertyConstraints('mtoOperExportacion', [
new Assert\Type(['type' => 'numeric']),
]);
$metadata->addPropertyConstraints('totalImpuestos', [
new Assert\NotBlank(),
new Assert\Type(['type' => 'numeric']),
]);
$metadata->addPropertyConstraints('mtoImpVenta', [
new Assert\NotBlank(),
new Assert\Type(['type' => 'numeric']),
]);
$metadata->addPropertyConstraints('client', [
new Assert\NotBlank(),
new Assert\Valid(),
]);
$metadata->addPropertyConstraints('company', [
new Assert\NotBlank(),
new Assert\Valid(),
]);
$metadata->addPropertyConstraint('mtoIGV', new Assert\Type(['type' => 'numeric']));
$metadata->addPropertyConstraint('mtoISC', new Assert\Type(['type' => 'numeric']));
$metadata->addPropertyConstraint('mtoBaseIsc', new Assert\Type(['type' => 'numeric']));
$metadata->addPropertyConstraint('mtoOtrosTributos', new Assert\Type(['type' => 'numeric']));
$metadata->addPropertyConstraint('mtoBaseOth', new Assert\Type(['type' => 'numeric']));
$metadata->addPropertyConstraint('sumOtrosCargos', new Assert\Type(['type' => 'numeric']));
$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());
}
}
Loading

0 comments on commit 46d377e

Please sign in to comment.