-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Comprobante impuestos and tests
- Loading branch information
Showing
2 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
src/CfdiUtils/Validate/Cfdi33/Standard/ComprobanteImpuestos.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
); | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
tests/CfdiUtilsTests/Validate/Cfdi33/Standard/ComprobanteImpuestosTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |