1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* MÓDULO DE EMISIÓN ELECTRÓNICA F72X |
5
|
|
|
* UBL 2.1 |
6
|
|
|
* Version 1.1 |
7
|
|
|
* |
8
|
|
|
* Copyright 2018, Jaime Cruz |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace F72X\Tools; |
12
|
|
|
|
13
|
|
|
use F72X\F72X; |
14
|
|
|
use F72X\Sunat\DataMap; |
15
|
|
|
use F72X\Sunat\SunatVars; |
16
|
|
|
use F72X\Sunat\Catalogo; |
17
|
|
|
use F72X\Sunat\Operations; |
18
|
|
|
use F72X\Company; |
19
|
|
|
use F72X\Repository; |
20
|
|
|
use Twig_Environment; |
21
|
|
|
use Twig_Loader_Filesystem; |
22
|
|
|
use Twig_Extensions_Extension_Intl; |
23
|
|
|
use Dompdf\Dompdf; |
24
|
|
|
use Codelint\QRCode\QRCode; |
25
|
|
|
|
26
|
|
|
class PdfGenerator { |
27
|
|
|
|
28
|
|
|
public static function generateFactura(DataMap $Invoice, $billName) { |
29
|
|
|
$dompdf = new Dompdf(); |
30
|
|
|
$html = self::getRenderedHtml($Invoice, 'factura.html'); |
31
|
|
|
// Render the HTML as PDF |
32
|
|
|
$dompdf->loadHtml($html); |
33
|
|
|
$dompdf->render(); |
34
|
|
|
$pdf = $dompdf->output(); |
35
|
|
|
Repository::savePDF($billName, $pdf); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public static function getRenderedHtml(DataMap $Invoice, $tpl) { |
39
|
|
|
$invoiceData = self::getInvoiceData($Invoice); |
40
|
|
|
$renderer = self::getRenderer(); |
41
|
|
|
return $renderer->render($tpl, $invoiceData); |
42
|
|
|
} |
43
|
|
|
private static function getInvoiceData(DataMap $inv) { |
44
|
|
|
|
45
|
|
|
$currency = Catalogo::getCurrencyPlural($inv->getCurrencyCode()); |
46
|
|
|
$payableAmount = $inv->getPayableAmount(); |
47
|
|
|
$payableInWords = Operations::getAmountInWords($payableAmount, $currency); |
48
|
|
|
return [ |
49
|
|
|
'companyRuc' => Company::getRUC(), |
50
|
|
|
'documentSeries' => $inv->getDocumentSeries(), |
51
|
|
|
'documentNumber' => $inv->getDocumentNumber(), |
52
|
|
|
'documentName' => $inv->getDocumentName(), |
53
|
|
|
'currency' => $currency, |
54
|
|
|
'customerRegName' => $inv->getCustomerRegName(), |
55
|
|
|
'customerDocNumber' => $inv->getCustomerDocNumber(), |
56
|
|
|
'customerAddress' => $inv->getCustomerAddress(), |
57
|
|
|
'issueDate' => $inv->getIssueDate()->format('d-m-Y'), |
58
|
|
|
'igvPercent' => SunatVars::IGV_PERCENT, |
59
|
|
|
'qr' => self::getQrString($inv), // QR Code |
60
|
|
|
'taxableOperations' => $inv->getTotalTaxableOperations(), // Total operaciones gravadas |
61
|
|
|
'freeOperations' => $inv->getTotalFreeOperations(), // Total operaciones gratuitas |
62
|
|
|
'unaffectedOperations' => $inv->getTotalUnaffectedOperations(), // Total operaciones inafectas |
63
|
|
|
'exemptedOperations' => $inv->getTotalExemptedOperations(), // Total operaciones exoneradas |
64
|
|
|
'totalAllowances' => $inv->getTotalAllowances(), // Total operaciones exoneradas |
65
|
|
|
'igvAmount' => $inv->getIGV(), // Total a pagar |
66
|
|
|
'payableAmount' => $payableAmount, // Total a pagar |
67
|
|
|
'payableInWords' => $payableInWords, // Monto en palabras |
68
|
|
|
'items' => self::getInvoiceDataItems($inv) // Items |
69
|
|
|
|
70
|
|
|
]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
private static function getInvoiceDataItems(DataMap $inv) { |
74
|
|
|
$Items = $inv->getItems(); |
75
|
|
|
$ln = $Items->getCount(); |
76
|
|
|
$items2 = []; |
77
|
|
|
for ($i = 0; $i < $ln; $i++) { |
78
|
|
|
$items2[]= [ |
79
|
|
|
'productCode' => $Items->getProductCode($i), |
80
|
|
|
'quantity' => $Items->getQunatity($i), |
81
|
|
|
'unitName' => Catalogo::getUnitName($Items->getUnitCode($i)), |
82
|
|
|
'unitBillableValue' => $Items->getUnitBillableValue($i), |
83
|
|
|
'itemPayableAmount' => $Items->getPayableAmount($i), |
84
|
|
|
'description' => $Items->getDescription($i) |
85
|
|
|
]; |
86
|
|
|
} |
87
|
|
|
return $items2; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
private static function getQrString(DataMap $inv) { |
91
|
|
|
$billName = $inv->getBillName(); |
92
|
|
|
$qr = new QRCode(); |
93
|
|
|
$qrContent = self::getQrContent($inv); |
94
|
|
|
$qrTempPath = F72X::getTempDir() . "/QR-$billName.png"; |
95
|
|
|
$qr->png($qrContent, $qrTempPath, 'Q', 8, 2); |
96
|
|
|
$qrs = base64_encode(file_get_contents($qrTempPath)); |
97
|
|
|
unlink($qrTempPath); |
98
|
|
|
return $qrs; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
private static function getQrContent(DataMap $inv) { |
102
|
|
|
$ruc = Company::getRUC(); |
103
|
|
|
$invoiveType = $inv->getDocumentType(); |
104
|
|
|
$documentSeries = $inv->getDocumentSeries(); |
105
|
|
|
$seriesNumber = $inv->getDocumentNumber(); |
106
|
|
|
$igv = Operations::formatAmount($inv->getIGV()); |
107
|
|
|
$payableAmount = Operations::formatAmount($inv->getPayableAmount()); |
108
|
|
|
$issueDate = $inv->getIssueDate()->format('Y-m-d'); |
109
|
|
|
$customerDocType = $inv->getCustomerDocType(); |
110
|
|
|
$customerDocNumber = $inv->getCustomerDocNumber(); |
111
|
|
|
return "$ruc|$invoiveType|$documentSeries|$seriesNumber|$igv|$payableAmount|$issueDate|$customerDocType|$customerDocNumber"; |
112
|
|
|
} |
113
|
|
|
private static function getRenderer() { |
114
|
|
|
$loader = new Twig_Loader_Filesystem(); |
115
|
|
|
$loader->addPath(Company::getTplsPath()); |
116
|
|
|
$view = new Twig_Environment($loader, ['cache' => false]); |
117
|
|
|
$view->addExtension(new Twig_Extensions_Extension_Intl()); |
118
|
|
|
return $view; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
} |
122
|
|
|
|