From b952cec4daccd8c7793e622879da9999ff3eb552 Mon Sep 17 00:00:00 2001 From: David Date: Tue, 12 Dec 2017 12:54:12 -0600 Subject: [PATCH] Added Comprobante impuestos and tests --- .../Cfdi33/Standard/ComprobanteImpuestos.php | 62 ++++++++++ .../Standard/ComprobanteImpuestosTest.php | 114 ++++++++++++++++++ 2 files changed, 176 insertions(+) create mode 100644 src/CfdiUtils/Validate/Cfdi33/Standard/ComprobanteImpuestos.php create mode 100644 tests/CfdiUtilsTests/Validate/Cfdi33/Standard/ComprobanteImpuestosTest.php diff --git a/src/CfdiUtils/Validate/Cfdi33/Standard/ComprobanteImpuestos.php b/src/CfdiUtils/Validate/Cfdi33/Standard/ComprobanteImpuestos.php new file mode 100644 index 00000000..429e77b4 --- /dev/null +++ b/src/CfdiUtils/Validate/Cfdi33/Standard/ComprobanteImpuestos.php @@ -0,0 +1,62 @@ + '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)) + ); + } +} diff --git a/tests/CfdiUtilsTests/Validate/Cfdi33/Standard/ComprobanteImpuestosTest.php b/tests/CfdiUtilsTests/Validate/Cfdi33/Standard/ComprobanteImpuestosTest.php new file mode 100644 index 00000000..c34a1a62 --- /dev/null +++ b/tests/CfdiUtilsTests/Validate/Cfdi33/Standard/ComprobanteImpuestosTest.php @@ -0,0 +1,114 @@ +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'); + } +}