Skip to content

Commit

Permalink
Generación de nota de credito emergencia
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaime Cruz committed Dec 12, 2018
1 parent e445362 commit e6813ba
Show file tree
Hide file tree
Showing 18 changed files with 3,019 additions and 80 deletions.
25 changes: 22 additions & 3 deletions cdefaults/tpls/nota-credito.html
Expand Up @@ -39,12 +39,22 @@
vertical-align: top;
height: 100px;
}
.head-title{
margin: 10px 0 0;
padding-bottom: 2px;
border-bottom: solid 2px;
font-weight: bold;
}
.head-section {
width: 100%;
margin: 10px 0;
margin: 0;
}
.head-section td {
vertical-align: top;
padding: 5px 0 2px;
}
.bottom-bordered{
border-bottom: solid 1px;
}
.detail-table {
border: solid 1px;
Expand All @@ -54,7 +64,7 @@
}
.detail-table th {
font-weight: normal;
padding:3px 4px;
padding: 3px 4px;
vertical-align: middle;
border: solid 1px;
}
Expand Down Expand Up @@ -128,10 +138,15 @@
NOTA DE CRÉDITO ELECTRÓNICA<br>
N° {{documentSeries}}-{{documentNumber}}
</div>
<div class="head-title">DOCUMENTO QUE MODIFICA:</div>
<table class="head-section">
<tbody>
<tr>
<td width="50">SEÑOR(ES)</td>
<td colspan="2">{{affectedDocumentOficialName}} ELECTRÓNICA: <b>{{affectedDocumentId}}</b></td>
<td colspan="2">ANULACIÓN DE LA OPERACIÓN</td>
</tr>
<tr>
<td>SEÑOR(ES)</td>
<td><div class="one-line">: {{customerRegName}}</div></td>
<td width="60">FEC. EMISIÓN</td>
<td width="70">: {{issueDate}}</td>
Expand All @@ -147,6 +162,10 @@
<td><div class="one-line">: {{customerAddress}}</div></td>
<td colspan="2"></td>
</tr>
<tr>
<td>OBSERVACIÓN</td>
<td colspan="3">: {{note}}</td>
</tr>
</tbody>
</table>
<table class="detail-table dim-full">
Expand Down
149 changes: 113 additions & 36 deletions src/Sunat/Catalogo.php
Expand Up @@ -19,44 +19,47 @@

class Catalogo {

const CAT_TAX_DOCUMENT_TYPE = 1;
const CAT_CURRENCY_TYPE = 2;
const CAT_MEASUREMENT_UNIT = 3;
const CAT_COUNTRY_CODE = 4;
const CAT_TAX_TYPE = 5;
const CAT_IDENT_DOCUMENT_TYPE = 6;
const CAT_TAX_DOCUMENT_TYPE = 1;
const CAT_CURRENCY_TYPE = 2;
const CAT_MEASUREMENT_UNIT = 3;
const CAT_COUNTRY_CODE = 4;
const CAT_TAX_TYPE = 5;
const CAT_IDENT_DOCUMENT_TYPE = 6;
const CAT_IGV_AFFECTATION_TYPE = 7;
const CAT_ISC_CALC_SYSTEM_TYPE = 8;
const CAT_NOTA_CREDITO_TYPE = 9;
const CAT_NOTA_DEBITO_TYPE = 10;
const CAT_FACTURA_TYPE = 51;
const CAT_NOTA_CREDITO_TYPE = 9;
const CAT_NOTA_DEBITO_TYPE = 10;
const CAT_FACTURA_TYPE = 51;

/** @CAT1 Código tipo de documento */
const DOCTYPE_FACTURA = '01';
const DOCTYPE_BOLETA = '03';
const DOCTYPE_FACTURA = '01';
const DOCTYPE_BOLETA = '03';
const DOCTYPE_NOTA_CREDITO = '07';
const DOCTYPE_NOTA_DEBITO = '08';
const DOCTYPE_VOUCHER = '12';
const DOCTYPE_SC_FACTURA = 'FAC';
const DOCTYPE_SC_BOLETA = 'BOL';
const DOCTYPE_NOTA_DEBITO = '08';
const DOCTYPE_VOUCHER = '12';
const DOCTYPE_SC_FACTURA = 'FAC';
const DOCTYPE_SC_BOLETA = 'BOL';
const DOCTYPE_SC_NOTA_CREDITO = 'NCR';
const DOCTYPE_SC_NOTA_DEBITO = 'NDE';
/** @CAT5 Tipo de impuesto*/
const CAT5_IGV = '1000';
const CAT5_IVAP = '1016';
const CAT5_ISC = '2000';
const CAT5_EXP = '9995';
const CAT5_GRA = '9996';
const CAT5_EXO = '9997';
const CAT5_INA = '9998';
const DOCTYPE_SC_NOTA_DEBITO = 'NDE';

/** @CAT5 Tipo de impuesto */
const CAT5_IGV = '1000';
const CAT5_IVAP = '1016';
const CAT5_ISC = '2000';
const CAT5_EXP = '9995';
const CAT5_GRA = '9996';
const CAT5_EXO = '9997';
const CAT5_INA = '9998';
const CAT5_OTROS = '9999';

/** @CAT7 Tipo de afectación del IGV */
const CAT7_GRA_IGV = '10';
const CAT7_GRA_IGV = '10';

/** @CAT16 Tipo de precio */
const CAT16_UNITARY_PRICE = '01';
const CAT16_REF_VALUE = '02';
const CAT16_REF_VALUE = '02';

/** @CAT6*/
/** @CAT6 */
const IDENTIFICATION_DOC_DNI = '1';
const IDENTIFICATION_DOC_RUC = '6';

Expand Down Expand Up @@ -95,10 +98,10 @@ public static function getDocumentSeriesPrefix($documentType, $affectedDocumentT
*/
public static function getOfficialDocumentName($documentType) {
switch ($documentType) {
case self::DOCTYPE_FACTURA : return 'FACTURA';
case self::DOCTYPE_BOLETA : return 'BOLETA DE VENTA';
case self::DOCTYPE_FACTURA : return 'FACTURA';
case self::DOCTYPE_BOLETA : return 'BOLETA DE VENTA';
case self::DOCTYPE_NOTA_CREDITO : return 'NOTA DE CRÉDITO';
case self::DOCTYPE_NOTA_DEBITO : return 'NOTA DE DÉBITO';
case self::DOCTYPE_NOTA_DEBITO : return 'NOTA DE DÉBITO';
}
throw new InvalidArgumentException("Error: $documentType isn't a valid document type");
}
Expand All @@ -110,10 +113,10 @@ public static function getOfficialDocumentName($documentType) {
*/
public static function getDocumentType($shortCode) {
switch ($shortCode) {
case self::DOCTYPE_SC_FACTURA: return self::DOCTYPE_FACTURA ;
case self::DOCTYPE_SC_BOLETA: return self::DOCTYPE_BOLETA;
case self::DOCTYPE_SC_FACTURA: return self::DOCTYPE_FACTURA;
case self::DOCTYPE_SC_BOLETA: return self::DOCTYPE_BOLETA;
case self::DOCTYPE_SC_NOTA_CREDITO: return self::DOCTYPE_NOTA_CREDITO;
case self::DOCTYPE_SC_NOTA_DEBITO: return self::DOCTYPE_NOTA_DEBITO;
case self::DOCTYPE_SC_NOTA_DEBITO: return self::DOCTYPE_NOTA_DEBITO;
}
throw new InvalidArgumentException("Error: $shortCode isn't valid short code");
}
Expand All @@ -122,7 +125,7 @@ public static function itemExist($catNumber, $itemID) {
$items = self::getCatItems($catNumber);
return key_exists($itemID, $items);
}

public static function getCatItem($catNumber, $itemID, $key = 'id') {
$items = self::getCatItems($catNumber);
foreach ($items as $item) {
Expand Down Expand Up @@ -191,11 +194,12 @@ public static function getUnitName($unitCode) {

public static function getCustomListItem($listName, $itemId) {
$customList = self::getCustomList($listName);
if(isset($customList[$itemId])){
if (isset($customList[$itemId])) {
return $customList[$itemId];
}
throw new ConfigException("El codigó de item $itemId no existe en la lista $listName");
}

public static function getCustomList($listName) {
// returns from cache
if (isset(self::$_LIST['LIST_' . $listName])) {
Expand All @@ -204,7 +208,7 @@ public static function getCustomList($listName) {
// Company customization
$customListsPath = Company::getListsPath();
$fileName = "$customListsPath/$listName.php";
if(file_exists($fileName)){
if (file_exists($fileName)) {
$customListsPath = Company::getListsPath();
$list = require $fileName;
// Cache
Expand All @@ -219,6 +223,26 @@ public static function getCustomList($listName) {
self::$_CAT['LIST_' . $listName] = $list;
return $list;
}

public static function getAllCatNumbers() {
$catNumbers = [];
for ($i = 1; $i <= 3; $i++) {
$catNumbers[] = $i;
}
for ($i = 5; $i <= 9; $i++) {
$catNumbers[] = $i;
}
for ($i = 10; $i <= 11; $i++) {
$catNumbers[] = $i;
}
for ($i = 13; $i <= 27; $i++) {
$catNumbers[] = $i;
}
for ($i = 51; $i <= 59; $i++) {
$catNumbers[] = $i;
}
return $catNumbers;
}

public static function catItemsToPhpArray($catNumber, $resultPath) {
$items = Catalogo::getCatItems($catNumber, true);
Expand All @@ -233,6 +257,7 @@ public static function catItemsToPhpArray($catNumber, $resultPath) {
$line[] = "'id' => '$id'";
}
if ($key != 'id') {
$val = addslashes($val);
$line[] = "'$key' => '$val'";
}
$k++;
Expand All @@ -255,4 +280,56 @@ public static function catItemsToPhpArray($catNumber, $resultPath) {
file_put_contents($resultPath, $result);
}

public static function catsToJsFile($catNumbers, $resultPath) {
$cats = [];
$ENTER = chr(13) . chr(10);
foreach ($catNumbers as $catNumber) {
$catNumber = str_pad($catNumber, 2, '0', STR_PAD_LEFT);
$cats[] = self::getCatJsItem($catNumber);
}
$joinedCats = implode(',' . $ENTER, $cats);
$result = <<<FILE
({
cats: {
$joinedCats
}
});
FILE;
file_put_contents($resultPath, $result);
}

public static function getCatJsItem($catNumber) {
$items = Catalogo::getCatItems($catNumber, true);
$lines = [];
$ENTER = chr(13) . chr(10);
foreach ($items as $item) {
$line = [];
$k = 0;
foreach ($item as $key => $val) {
if (!$k) {
$id = $item['id'];
$line[] = "id: '$id'";
}
if ($key != 'id') {
$val = addslashes($val);
$line[] = "$key: '$val'";
}
$k++;
}
$lineS = implode(', ', $line);
$id = $item['id'];
$lines[] = " {{$lineS}}";
}
$joinedLines = implode(',' . $ENTER, $lines);
$result = <<<TPL
N$catNumber: {
items: [
$joinedLines
]
}
TPL;
return $result;
}

}
57 changes: 57 additions & 0 deletions src/Sunat/Document/NotaCredito.php
Expand Up @@ -12,7 +12,12 @@

use F72X\Company;
use F72X\Tools\TemplateMgr;
use F72X\Tools\LogoMgr;
use F72X\Tools\QrGenerator;
use F72X\Sunat\DataMap;
use F72X\Sunat\SunatVars;
use F72X\Sunat\Operations;
use F72X\Sunat\Catalogo;
use F72X\UblComponent\SchemaNS;
use F72X\UblComponent\CreditNote;
use Sabre\Xml\Writer;
Expand Down Expand Up @@ -93,5 +98,57 @@ public function xmlSerialize(Writer $writer) {
]);
}
}
public function getReadyToPrintData() {
$dataMap = $this->getDataMap();
$currency = Catalogo::getCurrencyPlural($dataMap->getCurrencyCode());
$payableAmount = $dataMap->getPayableAmount();
$payableInWords = Operations::getAmountInWords($payableAmount, $currency);
return [
'companyRuc' => Company::getRUC(),
'companyAddress' => Company::getAddress(),
'companyCity' => Company::getCity(),
'companyContactInfo' => Company::getContactInfo(),
'documentSeries' => $dataMap->getDocumentSeries(),
'documentNumber' => $dataMap->getDocumentNumber(),
'officialDocumentName' => $dataMap->getOfficialDocumentName(),
'currency' => $currency,
'customerRegName' => $dataMap->getCustomerRegName(),
'customerDocNumber' => $dataMap->getCustomerDocNumber(),
'customerAddress' => $dataMap->getCustomerAddress(),
'issueDate' => $dataMap->getIssueDate()->format('d-m-Y'),
'igvPercent' => SunatVars::IGV_PERCENT,
'logo' => LogoMgr::getLogoString(),
'qr' => QrGenerator::getQrString($dataMap), // QR Code
'taxableOperations' => Operations::formatAmount($dataMap->getTotalTaxableOperations()), // Total operaciones gravadas
'freeOperations' => Operations::formatAmount($dataMap->getTotalFreeOperations()), // Total operaciones gratuitas
'unaffectedOperations' => Operations::formatAmount($dataMap->getTotalUnaffectedOperations()), // Total operaciones inafectas
'exemptedOperations' => Operations::formatAmount($dataMap->getTotalExemptedOperations()), // Total operaciones exoneradas
'totalAllowances' => Operations::formatAmount($dataMap->getTotalAllowances()), // Total operaciones exoneradas
'igvAmount' => Operations::formatAmount($dataMap->getIGV()), // Total a pagar
'payableAmount' => Operations::formatAmount($payableAmount), // Total a pagar
'payableInWords' => $payableInWords, // Monto en palabras
'items' => self::getReadyToPrintDataItems($dataMap), // Items
'noteType' => $dataMap->getNoteType(),
'affectedDocumentId' => $dataMap->getNoteAffectedDocId(),
'affectedDocumentOficialName' => Catalogo::getOfficialDocumentName($dataMap->getNoteAffectedDocType()),
'note' => $dataMap->getNoteDescription()
];
}

private static function getReadyToPrintDataItems(DataMap $inv) {
$Items = $inv->getItems();
$ln = $Items->getCount();
$items2 = [];
for ($i = 0; $i < $ln; $i++) {
$items2[]= [
'productCode' => $Items->getProductCode($i),
'quantity' => $Items->getQunatity($i),
'unitName' => Catalogo::getUnitName($Items->getUnitCode($i)),
'unitBillableValue' => Operations::formatAmount($Items->getUnitBillableValue($i)),
'itemPayableAmount' => Operations::formatAmount($Items->getPayableAmount($i)),
'description' => $Items->getDescription($i)
];
}
return $items2;
}
}
2 changes: 1 addition & 1 deletion src/Sunat/catalogo/CAT_01.php
@@ -1,6 +1,6 @@
<?php

//Catálogo N° 1:
//Catálogo N° 01:

return [
'01' => ['id' => '01', 'value' => 'Factura'],
Expand Down
2 changes: 1 addition & 1 deletion src/Sunat/catalogo/CAT_02.php
@@ -1,6 +1,6 @@
<?php

//Catálogo N° 2:
//Catálogo N° 02:

return [
'AFN' => ['id' => 'AFN', 'value' => 'Afghani'],
Expand Down
2 changes: 1 addition & 1 deletion src/Sunat/catalogo/CAT_03.php
@@ -1,6 +1,6 @@
<?php

//Catálogo N° 3:
//Catálogo N° 03:

return [
'NIU' => ['id' => 'NIU', 'value' => 'UNIDAD (BIENES)'],
Expand Down
2 changes: 1 addition & 1 deletion src/Sunat/catalogo/CAT_05.php
@@ -1,6 +1,6 @@
<?php

//Catálogo N° 5:
//Catálogo N° 05:

return [
'1000' => ['id' => '1000', 'value' => 'IGV IMPUESTO GENERAL A LAS VENTAS', 'UN_ECE_5153' => 'VAT', 'categoria' => 'S', 'name' => 'IGV'],
Expand Down
2 changes: 1 addition & 1 deletion src/Sunat/catalogo/CAT_06.php
@@ -1,6 +1,6 @@
<?php

//Catálogo N° 6:
//Catálogo N° 06:

return [
'0' => ['id' => '0', 'value' => 'DOC.TRIB.NO.DOM.SIN.RUC'],
Expand Down

0 comments on commit e6813ba

Please sign in to comment.