Skip to content

Commit

Permalink
progresss
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaime Cruz committed Jan 9, 2019
1 parent 55008d4 commit 481aabb
Show file tree
Hide file tree
Showing 17 changed files with 522 additions and 1,159 deletions.
174 changes: 174 additions & 0 deletions src/Sunat/Document/AbstractDocument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
<?php

/**
* MÓDULO DE EMISIÓN ELECTRÓNICA F72X
* UBL 2.1
* Version 1.1
*
* Copyright 2018, Jaime Cruz
*/

namespace F72X\Sunat\Document;

use F72X\F72X;
use F72X\Company;
use F72X\Repository;
use F72X\Tools\TplRenderer;

abstract class AbstractDocument implements DocumentInterface {

protected $rawData;
protected $parsedData;
protected $accountingSupplierDocType = 6; // Type: RUC
protected $accountingSupplierDocNumber;
protected $accountingSupplierRegName;
protected $documentSeries;
protected $documentNumber;
protected $documentId;
protected $documentFileName;
protected $documentLines;
protected $xmlTplName;

/**
* Use this to set the default fields for the document lines
* @var array
*/
protected $documentLineDefaults = [];

public function __construct(array $inputData) {
$this->processInput($inputData);
$this->setBodyFields();
// Datos de contribuyente: RUC, Razón Social, etc.
$this->setAccountingSupplierFields();
// Docuemnt: ID, Series, Number and filename
$this->setDocumentIdentificationFields();
}

private function processInput(array $inputData) {
$parsedData = $this->parseInput($inputData);
$this->rawData = $inputData;
$this->parsedData = $parsedData;
}
/**
* Parses the raw input data
* @param array $rawInput
* @return array The parsed data
*/
abstract protected function parseInput(array $rawInput);


public function parseInputLines(array $rawLines) {
$parsedLines = [];
foreach ($rawLines as $line) {
$line = array_merge($this->documentLineDefaults, $line);
$parsedLines[] = $this->parseInputLine($line);
}
return $parsedLines;
}

public function parseInputLine(array $rawInputLine) {
return $rawInputLine;
}

public function setAccountingSupplierFields() {
$this->accountingSupplierDocNumber = Company::getRUC();
$this->accountingSupplierRegName = Company::getCompanyName();
}

public function generateXmlFile() {
$tplRenderer = TplRenderer::getRenderer(F72X::getSrcDir() . '/templates');
$xmlContent = $tplRenderer->render($this->xmlTplName, $this->getDataForXml());
Repository::saveDocument($this->documentFileName, $xmlContent);
}

public function getRawData() {
return $this->rawData;
}

public function getParsedData() {
return $this->parsedData;
}

public function getAccountingSupplierDocType() {
return $this->accountingSupplierDocType;
}

public function getAccountingSupplierDocNumber() {
return $this->accountingSupplierDocNumber;
}

public function getAccountingSupplierRegName() {
return $this->accountingSupplierRegName;
}

public function getDocumentSeries() {
return $this->documentSeries;
}

public function getDocumentNumber() {
return $this->documentNumber;
}

public function getDocumentId() {
return $this->documentId;
}

public function getDocumentFileName() {
return $this->documentFileName;
}

public function getDocumentLines() {
return $this->documentLines;
}

public function setRawData($rawData) {
$this->rawData = $rawData;
return $this;
}

public function setParsedData($parsedData) {
$this->parsedData = $parsedData;
return $this;
}

public function setAccountingSupplierDocType($docType) {
$this->accountingSupplierDocType = $docType;
return $this;
}

public function setAccountingSupplierDocNumber($docNumber) {
$this->accountingSupplierDocNumber = $docNumber;
return $this;
}

public function setAccountingSupplierRegName($registrationName) {
$this->accountingSupplierRegName = $registrationName;
return $this;
}

public function setDocumentSeries($series) {
$this->documentSeries = $series;
return $this;
}

public function setDocumentNumber($number) {
$this->documentNumber = $number;
return $this;
}

public function setDocumentId($id) {
$this->documentId = $id;
return $this;
}

public function setDocumentFileName($documentFileName) {
$this->documentFileName = $documentFileName;
return $this;
}

public function setDocumentLines($documentLines) {
$this->documentLines = $documentLines;
return $this;
}

}
40 changes: 40 additions & 0 deletions src/Sunat/Document/AbstractSummary.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/**
* MÓDULO DE EMISIÓN ELECTRÓNICA F72X
* UBL 2.1
* Version 1.1
*
* Copyright 2018, Jaime Cruz
*/

namespace F72X\Sunat\Document;

abstract class AbstractSummary extends AbstractDocument {

protected $summaryIdPrefix;

/**
* @var DateTime
*/
protected $issueDate;

/**
* @var DateTime
*/
protected $referenceDate;

/**
* Sets: Document: Number, ID and filename
*/
public function setDocumentIdentificationFields() {
$parsedData = $this->parsedData;
// Max: 5 digits
$this->documentNumber = str_pad($parsedData['documentNumber'], 5, '0', STR_PAD_LEFT);
// <PREFIX>-<Fecha de generación del archivo YYYYMMDD>-<Número Correlativo>
$this->documentId = $this->summaryIdPrefix . '-' . $this->issueDate->format('Ymd') . '-' . $this->documentNumber;
// The file name
$this->documentFileName = $this->accountingSupplierDocNumber . '-' . $this->documentId;
}

}
68 changes: 68 additions & 0 deletions src/Sunat/Document/ComunicacionBaja.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/**
* MÓDULO DE EMISIÓN ELECTRÓNICA F72X
* UBL 2.1
* Version 1.1
*
* Copyright 2018, Jaime Cruz
*/

namespace F72X\Sunat\Document;

use DateTime;

class ComunicacionBaja extends AbstractSummary {

protected $summaryIdPrefix = 'RA';
protected $xmlTplName = 'VoidedDocuments.xml';

protected function parseInput(array $input) {
$parsedData = $input;
$parsedData['issueDate'] = new DateTime($input['issueDate']);
$parsedData['referenceDate'] = new DateTime($input['referenceDate']);
$parsedData['items'] = $this->parseInputLines($input['items']);
return $parsedData;
}

public function setBodyFields() {
$data = $this->parsedData;
$this->issueDate = $data['issueDate'];
$this->referenceDate = $data['referenceDate'];
// Lines
$this->documentLines = $data['items'];
}

public function getDataForXml() {
return [
'documentId' => $this->documentId,
'issueDate' => $this->issueDate->format('Y-m-d'),
'referenceDate' => $this->referenceDate->format('Y-m-d'),
'accountingSupplierDocType' => $this->accountingSupplierDocType,
'accountingSupplierDocNumber' => $this->accountingSupplierDocNumber,
'accountingSupplierRegName' => $this->accountingSupplierRegName,
'lines' => $this->documentLines
];
}
public function generateFiles() {
$this->generateXmlFile();
}
public function getIssueDate() {
return $this->issueDate;
}

public function getReferenceDate() {
return $this->referenceDate;
}

public function setIssueDate(DateTime $issueDate) {
$this->issueDate = $issueDate;
return $this;
}

public function setReferenceDate(DateTime $referenceDate) {
$this->referenceDate = $referenceDate;
return $this;
}

}
62 changes: 62 additions & 0 deletions src/Sunat/Document/DocumentInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/**
* MÓDULO DE EMISIÓN ELECTRÓNICA F72X
* UBL 2.1
* Version 1.1
*
* Copyright 2018, Jaime Cruz
*/

namespace F72X\Sunat\Document;

interface DocumentInterface {

/**
* Set the supplier's information such as RUC, name and others
*/
public function setAccountingSupplierFields();

/**
* Sets the Document identification fields
*/
public function setDocumentIdentificationFields();

/**
* Sets the Body fields
*/
public function setBodyFields();


/**
* Parses the raw input lines, is should be implemented using an each function
* and calling to *parseInputLine for each line in order to reduce complexity
* @param array $rawLines
* @return array The parsed lines
*/
public function parseInputLines(array $rawLines);

/**
* Parses the raw input line
* @param array $rawInputLine
* @return array The parsed line
*/
public function parseInputLine(array $rawInputLine);

/**
* Returns the an array with data ready to be placed in the document's XML template
* @param array $input
* @return array The parsed data
*/
public function getDataForXml();

/**
* Writes the document files
*/
public function generateFiles();

/**
* Writes the document XML file
*/
public function generateXmlFile();
}

0 comments on commit 481aabb

Please sign in to comment.