Skip to content

Commit

Permalink
parse fields to float
Browse files Browse the repository at this point in the history
  • Loading branch information
giansalex committed Oct 6, 2017
1 parent 45794cf commit 54ef9c4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
22 changes: 11 additions & 11 deletions src/Greenter/Parser/InvoiceParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ public function parse($value)
$this->loadTributos($inv, $xpt);
$monetaryTotal = $xpt->query('/xt:Invoice/cac:LegalMonetaryTotal')->item(0);
$inv->setTipoOperacion($this->defValue($xpt->query('sac:SUNATTransaction/cbc:ID', $additional)))
->setSumDsctoGlobal($this->defValue($xpt->query('cbc:AllowanceTotalAmount', $monetaryTotal)))
->setTotalAnticipos($this->defValue($xpt->query('cbc:PrepaidAmount', $monetaryTotal)))
->setSumDsctoGlobal(floatval($this->defValue($xpt->query('cbc:AllowanceTotalAmount', $monetaryTotal),0)))
->setTotalAnticipos(floatval($this->defValue($xpt->query('cbc:PrepaidAmount', $monetaryTotal),0)))
->setAnticipos(iterator_to_array($this->getPrepayments($xpt)))
->setMtoOtrosTributos($this->defValue($xpt->query('cbc:ChargeTotalAmount', $monetaryTotal)))
->setMtoImpVenta(floatval($this->defValue($xpt->query('cbc:PayableAmount', $monetaryTotal))))
->setMtoOtrosTributos(floatval($this->defValue($xpt->query('cbc:ChargeTotalAmount', $monetaryTotal), 0)))
->setMtoImpVenta($this->defValue($xpt->query('cbc:PayableAmount', $monetaryTotal)))
->setDetails(iterator_to_array($this->getDetails($xpt)))
->setLegends(iterator_to_array($this->getLegends($xpt, $additional)));

Expand Down Expand Up @@ -90,7 +90,7 @@ private function loadTotals(Invoice $inv, \DOMXPath $xpt, \DOMNode $node)
foreach ($totals as $total) {
/**@var $total \DOMElement*/
$id = $this->defValue($xpt->query('cbc:ID', $total));
$val = floatval($this->defValue($xpt->query('cbc:PayableAmount', $total)));
$val = floatval($this->defValue($xpt->query('cbc:PayableAmount', $total),0));
switch ($id) {
case '1001':
$inv->setMtoOperGravadas($val);
Expand All @@ -115,7 +115,7 @@ private function loadTotals(Invoice $inv, \DOMXPath $xpt, \DOMNode $node)
$inv->setDetraccion((new Detraction())
->setMount($val)
->setPercent($this->defValue($xpt->query('cbc:Percent', $total)))
->setValueRef($this->defValue($xpt->query('sac:ReferenceAmount', $total), null)));
->setValueRef($this->defValue($xpt->query('sac:ReferenceAmount', $total))));
break;
case '2005':
$inv->setMtoDescuentos($val);
Expand All @@ -129,7 +129,7 @@ private function loadTributos(Invoice $inv, \DOMXPath $xpt)
$taxs = $xpt->query('/xt:Invoice/cac:TaxTotal');
foreach ($taxs as $tax) {
$name = $this->defValue($xpt->query('cac:TaxSubtotal/cac:TaxCategory/cac:TaxScheme/cbc:Name', $tax));
$val = floatval($this->defValue($xpt->query('cbc:TaxAmount', $tax)));
$val = floatval($this->defValue($xpt->query('cbc:TaxAmount', $tax),0));
switch ($name) {
case 'IGV':
$inv->setMtoIGV($val);
Expand All @@ -153,7 +153,7 @@ private function getPrepayments(\DOMXPath $xpt)
foreach ($nodes as $node) {
$docRel = $xpt->query('cbc:ID', $node)->item(0);
$item = (new Prepayment())
->setTotal(floatval($this->defValue($xpt->query('cbc:PaidAmount', $node))))
->setTotal(floatval($this->defValue($xpt->query('cbc:PaidAmount', $node),0)))
->setTipoDocRel($docRel->getAttribute('schemeID'))
->setNroDocRel($docRel->nodeValue);

Expand Down Expand Up @@ -209,7 +209,7 @@ private function getDetails(\DOMXPath $xpt)
foreach ($nodes as $node) {
$quant = $xpt->query('cbc:InvoicedQuantity', $node)->item(0);
$det = new SaleDetail();
$det->setCtdUnidadItem(floatval($quant->nodeValue))
$det->setCtdUnidadItem($quant->nodeValue)
->setCodUnidadMedida($quant->getAttribute('unitCode'))
->setMtoValorVenta($this->defValue($xpt->query('cbc:LineExtensionAmount', $node)))
->setMtoValorUnitario($this->defValue($xpt->query('cac:Price/cbc:PriceAmount', $node)))
Expand All @@ -219,7 +219,7 @@ private function getDetails(\DOMXPath $xpt)
$taxs = $xpt->query('cac:TaxTotal', $node);
foreach ($taxs as $tax) {
$name = $this->defValue($xpt->query('cac:TaxSubtotal/cac:TaxCategory/cac:TaxScheme/cbc:Name', $tax));
$val = floatval($this->defValue($xpt->query('cbc:TaxAmount', $tax)));
$val = floatval($this->defValue($xpt->query('cbc:TaxAmount', $tax),0));
switch ($name) {
case 'IGV':
$det->setMtoIgvItem($val);
Expand All @@ -235,7 +235,7 @@ private function getDetails(\DOMXPath $xpt)
$prices = $xpt->query('cac:PricingReference', $node);
foreach ($prices as $price) {
$code = $this->defValue($xpt->query('cac:AlternativeConditionPrice/cbc:PriceTypeCode', $price));
$value = floatval($this->defValue($xpt->query('cac:AlternativeConditionPrice/cbc:PriceAmount', $price)));
$value = floatval($this->defValue($xpt->query('cac:AlternativeConditionPrice/cbc:PriceAmount', $price),0));
$code = trim($code);

switch ($code) {
Expand Down
7 changes: 4 additions & 3 deletions src/Greenter/Parser/NoteParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function parse($value)
$this->loadTotals($inv, $additional);
$this->loadTributos($inv);
$monetaryTotal = $xml->getNode($isNcr ? 'cac:LegalMonetaryTotal': 'cac:RequestedMonetaryTotal', $root);
$inv->setMtoOtrosTributos($xml->getValue('cbc:ChargeTotalAmount','', $monetaryTotal))
$inv->setMtoOtrosTributos(floatval($xml->getValue('cbc:ChargeTotalAmount',0, $monetaryTotal)))
->setMtoImpVenta($xml->getValue('cbc:PayableAmount', 0,$monetaryTotal))
->setDetails(iterator_to_array($this->getDetails($isNcr)))
->setLegends(iterator_to_array($this->getLegends($additional)));
Expand Down Expand Up @@ -191,7 +191,7 @@ private function getDetails($isNcr)
foreach ($nodes as $node) {
$quant = $xml->getNode('cbc:'.$nameQuant, $node);
$det = new SaleDetail();
$det->setCtdUnidadItem(floatval($quant->nodeValue))
$det->setCtdUnidadItem($quant->nodeValue)
->setCodUnidadMedida($quant->getAttribute('unitCode'))
->setMtoValorVenta($xml->getValue('cbc:LineExtensionAmount','', $node))
->setMtoValorUnitario($xml->getValue('cac:Price/cbc:PriceAmount', '', $node))
Expand All @@ -217,7 +217,8 @@ private function getDetails($isNcr)
$prices = $xml->getNodes('cac:PricingReference', $node);
foreach ($prices as $price) {
$code = $xml->getValue('cac:AlternativeConditionPrice/cbc:PriceTypeCode','', $price);
$value = floatval($xml->getValue('cac:AlternativeConditionPrice/cbc:PriceAmount','', $price));
$code = trim($code);
$value = floatval($xml->getValue('cac:AlternativeConditionPrice/cbc:PriceAmount',0, $price));

switch ($code) {
case '01':
Expand Down
1 change: 1 addition & 0 deletions tests/Greenter/Parser/InvoiceParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public function testFacParseFromDoc()
$this->assertNotEmpty($obj->getFechaEmision());
$this->assertGreaterThanOrEqual(1, count($obj->getDetails()));
$this->assertGreaterThanOrEqual(1, count($obj->getLegends()));
var_dump($obj);
}

public function filenameProvider()
Expand Down

0 comments on commit 54ef9c4

Please sign in to comment.