Skip to content

Commit

Permalink
Add Build Filename to docs + Add Last Xml + Test
Browse files Browse the repository at this point in the history
  • Loading branch information
giansalex committed Jul 30, 2017
1 parent ae3d8f6 commit c97ce90
Show file tree
Hide file tree
Showing 8 changed files with 283 additions and 77 deletions.
70 changes: 24 additions & 46 deletions src/Greenter/FeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Greenter\Model\Response\BillResult;
use Greenter\Model\Response\StatusResult;
use Greenter\Model\Response\SummaryResult;
use Greenter\Model\Sale\BaseSale;
use Greenter\Model\Sale\Note;
use Greenter\Model\Summary\Summary;
use Greenter\Model\Voided\Voided;
Expand Down Expand Up @@ -46,6 +45,12 @@ class FeFactory implements FeFactoryInterface
*/
private $sender;

/**
* Ultimo xml generado.
*
* @var string
*/
private $lastXml;
/**
* @var Company
*/
Expand All @@ -69,7 +74,7 @@ public function __construct()
public function sendInvoice(Invoice $invoice)
{
$xml = $this->builder->buildInvoice($invoice);
$filename = $this->getFilename($invoice);
$filename = $invoice->getFilename($this->company->getRuc());

return $this->getBillResult($xml, $filename);
}
Expand All @@ -83,7 +88,7 @@ public function sendInvoice(Invoice $invoice)
public function sendNote(Note $note)
{
$xml = $this->builder->buildNote($note);
$filename = $this->getFilename($note);
$filename = $note->getFilename($this->company->getRuc());

return $this->getBillResult($xml, $filename);
}
Expand All @@ -97,7 +102,7 @@ public function sendNote(Note $note)
public function sendResumen(Summary $summary)
{
$xml = $this->builder->buildSummary($summary);
$filename = $this->getFilenameSummary($summary);
$filename = $summary->getFileName($this->company->getRuc());

return $this->getSummaryResult($xml, $filename);
}
Expand All @@ -111,7 +116,7 @@ public function sendResumen(Summary $summary)
public function sendBaja(Voided $voided)
{
$xml = $this->builder->buildVoided($voided);
$filename = $this->getFilenameSummary($voided);
$filename = $voided->getFileName($this->company->getRuc());

return $this->getSummaryResult($xml, $filename);
}
Expand Down Expand Up @@ -157,6 +162,16 @@ public function setParameters($params)
}
}

/**
* Get Last XML Signed.
*
* @return string
*/
public function getLastXml()
{
return $this->lastXml;
}

/**
* @param array $ws
*/
Expand All @@ -178,9 +193,9 @@ private function setWsParams($ws)
*/
private function getBillResult($xml, $filename)
{
$xmlS = $this->getXmmlSigned($xml);
$this->lastXml = $this->getXmmlSigned($xml);

$zip = $this->zipper->compress("$filename.xml", $xmlS);
$zip = $this->zipper->compress("$filename.xml", $this->lastXml);
return $this->sender->send("$filename.zip", $zip);
}

Expand All @@ -191,9 +206,9 @@ private function getBillResult($xml, $filename)
*/
private function getSummaryResult($xml, $filename)
{
$xmlS = $this->getXmmlSigned($xml);
$this->lastXml = $this->getXmmlSigned($xml);

$zip = $this->zipper->compress("$filename.xml", $xmlS);
$zip = $this->zipper->compress("$filename.xml", $this->lastXml);
return $this->sender->sendSummary("$filename.zip", $zip);
}

Expand All @@ -205,41 +220,4 @@ private function getXmmlSigned($xml)
{
return $this->signer->sign($xml);
}

/**
* @param BaseSale $sale
* @return string
*/
private function getFilename(BaseSale $sale)
{
$parts = [
$this->company->getRuc(),
$sale->getTipoDoc(),
$sale->getSerie(),
$sale->getCorrelativo(),
];

return join('-', $parts);
}


/**
* @param Summary|Voided $object
* @return string
*/
private function getFilenameSummary($object)
{
$parts = [$this->company->getRuc()];

if ($object instanceof Summary) {
$parts[] = 'RC';
$parts[] = $object->getFecResumen()->format('Ymd');
} elseif ($object instanceof Voided) {
$parts[] = 'RA';
$parts[] = $object->getFecComunicacion()->format('Ymd');
}
$parts[] = $object->getCorrelativo();

return join('-', $parts);
}
}
7 changes: 7 additions & 0 deletions src/Greenter/FeFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ public function sendBaja(Voided $voided);
*/
public function getStatus($ticket);

/**
* Get Last XML Signed.
*
* @return string
*/
public function getLastXml();

/**
* Set Company
*
Expand Down
18 changes: 18 additions & 0 deletions src/Greenter/Model/Sale/BaseSale.php
Original file line number Diff line number Diff line change
Expand Up @@ -474,4 +474,22 @@ public function setRelDocs($relDocs)
$this->relDocs = $relDocs;
return $this;
}

/**
* Get FileName without extension.
*
* @param string $ruc Ruc de la Empresa.
* @return string
*/
public function getFileName($ruc)
{
$parts = [
$ruc,
$this->getTipoDoc(),
$this->getSerie(),
$this->getCorrelativo(),
];

return join('-', $parts);
}
}
18 changes: 18 additions & 0 deletions src/Greenter/Model/Summary/Summary.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,22 @@ public function setDetails($details)
$this->details = $details;
return $this;
}

/**
* Get FileName without extension.
*
* @param string $ruc Ruc de la Empresa.
* @return string
*/
public function getFileName($ruc)
{
$parts = [
$ruc,
'RC',
$this->getFecResumen()->format('Ymd'),
$this->getCorrelativo(),
];

return join('-', $parts);
}
}
18 changes: 18 additions & 0 deletions src/Greenter/Model/Voided/Voided.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,22 @@ public function setDetails($details)
$this->details = $details;
return $this;
}

/**
* Get FileName without extension.
*
* @param string $ruc Ruc de la Empresa.
* @return string
*/
public function getFileName($ruc)
{
$parts = [
$ruc,
'RA',
$this->getFecComunicacion()->format('Ymd'),
$this->getCorrelativo(),
];

return join('-', $parts);
}
}
81 changes: 51 additions & 30 deletions tests/Greenter/FeFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,14 @@
*/

namespace Tests\Greenter;
use Greenter\FeFactory;
use Greenter\FeFactoryInterface;
use Greenter\Ws\Services\FeSunat;

/**
* Class FeFactoryTest
* @package Tests\Greenter
*/
class FeFactoryTest extends \PHPUnit_Framework_TestCase
{
use FeFactoryTrait;

/**
* @var FeFactoryInterface
*/
private $factory;

public function setUp()
{
$factory = new FeFactory();
$factory->setParameters([
'ws' => [
'user' => '20000000001MODDATOS',
'pass' => 'moddatos',
'service' => FeSunat::BETA,
],
'xml' => [
'cache' => sys_get_temp_dir(),
],
'cert' => [
'public' => file_get_contents(__DIR__.'/Resources/certificado.cer'),
'private' => file_get_contents(__DIR__.'/Resources/certificado.key'),
]
]);
$factory->setCompany($this->getCompany());
$this->factory = $factory;
}
use FeFactoryTraitTest;

public function testInvoice()
{
Expand All @@ -58,6 +29,16 @@ public function testInvoice()
);
}

/**
* @expectedException \Greenter\Xml\Exception\ValidationException
*/
public function testInvoiceInvalid()
{
$invoice = $this->getInvoice();
$invoice->setTipoDoc('000');
$this->factory->sendInvoice($invoice);
}

public function testNotaCredito()
{
$creditNote = $this->getCreditNote();
Expand All @@ -71,6 +52,16 @@ public function testNotaCredito()
);
}

/**
* @expectedException \Greenter\Xml\Exception\ValidationException
*/
public function testCreditNoteException()
{
$creditNote = $this->getCreditNote();
$creditNote->setCodMotivo('C00');
$this->factory->sendNote($creditNote);
}

public function testNotaDebito()
{
$debitNote = $this->getDebitNote();
Expand All @@ -84,6 +75,16 @@ public function testNotaDebito()
);
}

/**
* @expectedException \Greenter\Xml\Exception\ValidationException
*/
public function testDebitNoteException()
{
$debitNote = $this->getDebitNote();
$debitNote->setCodMotivo('C00');
$this->factory->sendNote($debitNote);
}


public function testResumen()
{
Expand All @@ -95,6 +96,16 @@ public function testResumen()
$this->assertEquals('2072', $result->getError()->getCode());
}

/**
* @expectedException \Greenter\Xml\Exception\ValidationException
*/
public function testResumenException()
{
$resumen = $this->getSummary();
$resumen->setCorrelativo('1234');
$this->factory->sendResumen($resumen);
}

public function testBaja()
{
$baja = $this->getVoided();
Expand All @@ -105,6 +116,16 @@ public function testBaja()
$this->assertEquals(13, strlen($result->getTicket()));
}

/**
* @expectedException \Greenter\Xml\Exception\ValidationException
*/
public function testBajaException()
{
$baja = $this->getVoided();
$baja->getDetails()[0]->setTipoDoc('100');
$this->factory->sendBaja($baja);
}

public function testStatus()
{
$result = $this->factory->getStatus('1500523236696');
Expand Down
Loading

0 comments on commit c97ce90

Please sign in to comment.