Skip to content

Commit

Permalink
Added Comprobante impuestos and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eclipxe13 committed Dec 12, 2017
2 parents 52ce485 + b952cec commit 8d9f942
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/CfdiUtils/Validate/Cfdi33/Standard/ComprobanteImpuestos.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
namespace CfdiUtils\Validate\Cfdi33\Standard;

use CfdiUtils\Nodes\NodeInterface;
use CfdiUtils\Validate\Asserts;
use CfdiUtils\Validate\Cfdi33\Abstracts\AbstractDiscoverableVersion33;
use CfdiUtils\Validate\Status;

/**
* ConceptoImpuestos.php
*
* Valida que:
* - COMPIMPUESTOSC01: Si existe el nodo impuestos entonces debe incluir el total de traslados
* y/o el total de retenciones
* - COMPIMPUESTOSC02: Si existe al menos un traslado entonces debe existir el total de traslados
* - COMPIMPUESTOSC03: Si existe al menos una retención entonces debe existir el total de retenciones
*/
class ComprobanteImpuestos extends AbstractDiscoverableVersion33
{
private function registerAsserts(Asserts $asserts)
{
$assertDescriptions = [
'COMPIMPUESTOSC01' => 'Si existe el nodo impuestos entonces debe incluir el total detraslados y/o'
. ' el total de retenciones',
'COMPIMPUESTOSC02' => 'Si existe al menos un traslado entonces debe existir el total de traslados',
'COMPIMPUESTOSC03' => 'Si existe al menos una retención entonces debe existir el total de retenciones',
];
foreach ($assertDescriptions as $code => $title) {
$asserts->put($code, $title);
}
}

public function validate(NodeInterface $comprobante, Asserts $asserts)
{
$this->registerAsserts($asserts);

$nodeImpuestos = $comprobante->searchNode('cfdi:Impuestos');
if (null === $nodeImpuestos) {
return;
}

$existsTotalTrasladados = isset($nodeImpuestos['TotalImpuestosTrasladados']);
$existsTotalRetenidos = isset($nodeImpuestos['TotalImpuestosRetenidos']);

$asserts->putStatus(
'COMPIMPUESTOSC01',
Status::when($existsTotalTrasladados || $existsTotalRetenidos)
);

$hasTraslados = (null !== $nodeImpuestos->searchNode('cfdi:Traslados', 'cfdi:Traslado'));
$asserts->putStatus(
'COMPIMPUESTOSC02',
Status::when(! ($hasTraslados xor $existsTotalTrasladados))
);

$hasRetenciones = (null !== $nodeImpuestos->searchNode('cfdi:Retenciones', 'cfdi:Retencion'));
$asserts->putStatus(
'COMPIMPUESTOSC03',
Status::when(! ($hasRetenciones xor $existsTotalRetenidos))
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php
namespace CfdiUtilsTests\Validate\Cfdi33\Standard;

use CfdiUtils\Nodes\Node;
use CfdiUtils\Validate\Cfdi33\Standard\ComprobanteImpuestos;
use CfdiUtils\Validate\Status;
use CfdiUtilsTests\Validate\ValidateTestCase;

class ComprobanteImpuestosTest extends ValidateTestCase
{
/** @var ComprobanteImpuestos */
protected $validator;

protected function setUp()
{
parent::setUp();
$this->validator = new ComprobanteImpuestos();
}

public function providerValidImpuestos()
{
return [
[true, false],
[false, true],
[true, true],
];
}

/**
* @param $putTraslados
* @param $putRetenciones
* @dataProvider providerValidImpuestos
*/
public function testValidImpuestos($putTraslados, $putRetenciones)
{
$nodeImpuestos = new Node('cfdi:Impuestos');
if ($putTraslados) {
$nodeImpuestos['TotalImpuestosTrasladados'] = '';
$nodeImpuestos->addChild(new Node('cfdi:Traslados', [], [new Node('cfdi:Traslado')]));
}
if ($putRetenciones) {
$nodeImpuestos['TotalImpuestosRetenidos'] = '';
$nodeImpuestos->addChild(new Node('cfdi:Retenciones', [], [new Node('cfdi:Retencion')]));
}
$this->comprobante->addChild($nodeImpuestos);

$this->runValidate();
$this->assertStatusEqualsCode(Status::ok(), 'COMPIMPUESTOSC01');
$this->assertStatusEqualsCode(Status::ok(), 'COMPIMPUESTOSC02');
$this->assertStatusEqualsCode(Status::ok(), 'COMPIMPUESTOSC03');
}

public function testInvalidWithEmptyImpuestos()
{
$this->comprobante->addChild(new Node('cfdi:Impuestos'));

$this->runValidate();
$this->assertStatusEqualsCode(Status::error(), 'COMPIMPUESTOSC01');
}

public function testInvalidTrasladosNodesWithoutTotalTraslados()
{
$this->comprobante->addChild(new Node(
'cfdi:Impuestos',
[],
[new Node('cfdi:Traslados', [], [new Node('cfdi:Traslado')])]
));

$this->runValidate();
$this->assertStatusEqualsCode(Status::error(), 'COMPIMPUESTOSC02');
}

public function testInvalidTotalTrasladosWithoutTrasladosNodes()
{
$this->comprobante->addChild(new Node(
'cfdi:Impuestos',
['TotalImpuestosTrasladados' => '']
));

$this->runValidate();
$this->assertStatusEqualsCode(Status::error(), 'COMPIMPUESTOSC02');
}

public function testInvalidRetencionesNodesWithoutTotalRetenciones()
{
$this->comprobante->addChild(new Node(
'cfdi:Impuestos',
[],
[new Node('cfdi:Retenciones', [], [new Node('cfdi:Retencion')])]
));

$this->runValidate();
$this->assertStatusEqualsCode(Status::error(), 'COMPIMPUESTOSC03');
}

public function testInvalidTotalRetencionesWithoutRetencionesNodes()
{
$this->comprobante->addChild(new Node(
'cfdi:Impuestos',
['TotalImpuestosRetenidos' => '']
));

$this->runValidate();
$this->assertStatusEqualsCode(Status::error(), 'COMPIMPUESTOSC03');
}

public function testWithoutNodeImpuestos()
{
$this->runValidate();
$this->assertStatusEqualsCode(Status::none(), 'COMPIMPUESTOSC01');
$this->assertStatusEqualsCode(Status::none(), 'COMPIMPUESTOSC02');
$this->assertStatusEqualsCode(Status::none(), 'COMPIMPUESTOSC03');
}
}

0 comments on commit 8d9f942

Please sign in to comment.