|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* MÓDULO DE EMISIÓN ELECTRÓNICA F72X |
|
5
|
|
|
* UBL 2.1 |
|
6
|
|
|
* Version 1.0 |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright 2019, 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 Twig_Extension_Escaper; |
|
24
|
|
|
use Dompdf\Dompdf; |
|
25
|
|
|
use Dompdf\Options; |
|
26
|
|
|
|
|
27
|
|
|
class PdfGenerator { |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Generates a PDF based on the invoice data |
|
31
|
|
|
* @param DataMap $Invoice |
|
32
|
|
|
* @param type $documentName |
|
33
|
|
|
*/ |
|
34
|
|
|
public static function generatePdf(DataMap $Invoice, $documentName) { |
|
35
|
|
|
Repository::savePDF($documentName, self::buildPdf($Invoice)); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Builds a PDF and returns the stream |
|
40
|
|
|
* @param DataMap $Invoice |
|
41
|
|
|
* @return string The PDF stream |
|
42
|
|
|
*/ |
|
43
|
|
|
public static function buildPdf(DataMap $Invoice) { |
|
44
|
|
|
// Dompdf Options |
|
45
|
|
|
$options = new Options(); |
|
46
|
|
|
$options->set('tempDir', F72X::getTempDir() . '/'); |
|
47
|
|
|
// Dompdf |
|
48
|
|
|
$dompdf = new Dompdf($options); |
|
49
|
|
|
$docType = self::getTplFor($Invoice->getDocumentType()); |
|
50
|
|
|
$html = self::getRenderedHtml($Invoice, $docType); |
|
51
|
|
|
// Render the HTML as PDF |
|
52
|
|
|
$dompdf->loadHtml($html); |
|
53
|
|
|
$dompdf->render(); |
|
54
|
|
|
return $dompdf->output(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
private static function getTplFor($docType) { |
|
58
|
|
|
if ($docType == Catalogo::DOCTYPE_FACTURA) { |
|
59
|
|
|
return 'factura.html'; |
|
60
|
|
|
} |
|
61
|
|
|
if ($docType == Catalogo::DOCTYPE_BOLETA) { |
|
62
|
|
|
return 'boleta.html'; |
|
63
|
|
|
} |
|
64
|
|
|
if ($docType == Catalogo::DOCTYPE_NOTA_CREDITO) { |
|
65
|
|
|
return 'nota-credito.html'; |
|
66
|
|
|
} |
|
67
|
|
|
return 'nota-debito.html'; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Compiles the template with the input data |
|
72
|
|
|
* @param DataMap $Invoice The invoice data |
|
73
|
|
|
* @param string $tpl The template name |
|
74
|
|
|
* @return string |
|
75
|
|
|
*/ |
|
76
|
|
|
public static function getRenderedHtml(DataMap $Invoice, $tpl) { |
|
77
|
|
|
$invoiceData = self::getDocumentData($Invoice); |
|
78
|
|
|
$renderer = self::getRenderer(); |
|
79
|
|
|
return $renderer->render($tpl, $invoiceData); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
private static function getDocumentData(DataMap $inv) { |
|
83
|
|
|
$currency = Catalogo::getCurrencyPlural($inv->getCurrencyCode()); |
|
84
|
|
|
$payableAmount = $inv->getPayableAmount(); |
|
85
|
|
|
$payableInWords = Operations::getAmountInWords($payableAmount, $currency); |
|
86
|
|
|
// has dueDate = |
|
87
|
|
|
$dueDate = $inv->getDueDate(); |
|
88
|
|
|
$formOfPayment = $inv->getFormOfPayment(); |
|
89
|
|
|
$formOfPaymentSrt = ""; |
|
90
|
|
|
if($formOfPayment == Catalogo::FAC_FORM_OF_PAYMENT_CONTADO) { |
|
91
|
|
|
$formOfPaymentSrt = "CONTADO"; |
|
92
|
|
|
}elseif($formOfPayment == Catalogo::FAC_FORM_OF_PAYMENT_CREDITO) { |
|
93
|
|
|
$formOfPaymentSrt = "CRÉDITO"; |
|
94
|
|
|
} |
|
95
|
|
|
$data = [ |
|
96
|
|
|
'companyName' => Company::getCompanyName(), |
|
97
|
|
|
'companyRuc' => Company::getRUC(), |
|
98
|
|
|
'companyAddress' => Company::getAddress(), |
|
99
|
|
|
'companyCity' => Company::getCity(), |
|
100
|
|
|
'edocHeaderContent' => Company::getEdocHeaderContent(), |
|
101
|
|
|
'edocFooterContent' => Company::getEdocFooterContent(), |
|
102
|
|
|
'documentSeries' => $inv->getDocumentSeries(), |
|
103
|
|
|
'documentNumber' => $inv->getDocumentNumber(), |
|
104
|
|
|
'officialDocumentName' => $inv->getOfficialDocumentName(), |
|
105
|
|
|
'currency' => $currency, |
|
106
|
|
|
'customerRegName' => $inv->getCustomerRegName(), |
|
107
|
|
|
'customerDocNumber' => $inv->getCustomerDocNumber(), |
|
108
|
|
|
'customerAddress' => $inv->getCustomerAddress(), |
|
109
|
|
|
'issueDate' => $inv->getIssueDate()->format('d-m-Y'), |
|
110
|
|
|
'dueDate' => $dueDate ? $dueDate->format('d-m-Y') : '', |
|
111
|
|
|
'igvPercent' => SunatVars::IGV_PERCENT, |
|
112
|
|
|
'logo' => LogoMgr::getLogoString(), |
|
113
|
|
|
'qr' => QrGenerator::getQrString($inv), // QR Code |
|
114
|
|
|
'taxableOperations' => $inv->getTotalTaxableOperations(), // Total operaciones gravadas |
|
115
|
|
|
'freeOperations' => $inv->getTotalFreeOperations(), // Total operaciones gratuitas |
|
116
|
|
|
'unaffectedOperations' => $inv->getTotalUnaffectedOperations(), // Total operaciones inafectas |
|
117
|
|
|
'exemptedOperations' => $inv->getTotalExemptedOperations(), // Total operaciones exoneradas |
|
118
|
|
|
'totalAllowances' => $inv->getTotalAllowances(), // Total operaciones exoneradas |
|
119
|
|
|
'igvAmount' => $inv->getIGV(), // IGV |
|
120
|
|
|
'payableAmount' => $payableAmount, // Total a pagar |
|
121
|
|
|
'formOfPaymentStr' => $formOfPaymentSrt, // Forma de pago |
|
122
|
|
|
'pendingAmount' => $inv->getPendingAmount(), // Forma de pago |
|
123
|
|
|
'payableInWords' => $payableInWords, // Monto en palabras |
|
124
|
|
|
'items' => self::getDocumentDataItems($inv), // Items |
|
125
|
|
|
'installments' => self::getDocumentInstallments($inv), // Cuotas |
|
126
|
|
|
]; |
|
127
|
|
|
// For credit and debit notes |
|
128
|
|
|
if (in_array($inv->getDocumentType(), [Catalogo::DOCTYPE_NOTA_CREDITO, Catalogo::DOCTYPE_NOTA_DEBITO])) { |
|
129
|
|
|
$noteData = [ |
|
130
|
|
|
'noteType' => $inv->getNoteType(), |
|
131
|
|
|
'discrepancyResponseReason' => $inv->getDiscrepancyResponseReason(), |
|
132
|
|
|
'affectedDocumentId' => $inv->getNoteAffectedDocId(), |
|
133
|
|
|
'affectedDocumentOficialName' => Catalogo::getOfficialDocumentName($inv->getNoteAffectedDocType()), |
|
134
|
|
|
'note' => $inv->getNoteDescription() |
|
135
|
|
|
]; |
|
136
|
|
|
return array_merge($data, $noteData); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return $data; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
private static function getDocumentDataItems(DataMap $inv) { |
|
143
|
|
|
$Items = $inv->getItems(); |
|
144
|
|
|
$ln = $Items->getCount(); |
|
145
|
|
|
$items2 = []; |
|
146
|
|
|
for ($i = 0; $i < $ln; $i++) { |
|
147
|
|
|
$items2[]= [ |
|
148
|
|
|
'productCode' => $Items->getProductCode($i), |
|
149
|
|
|
'quantity' => $Items->getQunatity($i), |
|
150
|
|
|
'unitName' => Catalogo::getUnitName($Items->getUnitCode($i)), |
|
151
|
|
|
'unitBillableValue' => $Items->getUnitBillableValue($i), |
|
152
|
|
|
'itemPayableAmount' => $Items->getPayableAmount($i), |
|
153
|
|
|
'description' => $Items->getDescription($i) |
|
154
|
|
|
]; |
|
155
|
|
|
} |
|
156
|
|
|
return $items2; |
|
157
|
|
|
} |
|
158
|
|
|
private static function getDocumentInstallments(DataMap $inv) { |
|
159
|
|
|
$installments = $inv->getInstallments(); |
|
160
|
|
|
$out = []; |
|
161
|
|
|
foreach ($installments as $installment) { |
|
162
|
|
|
$out[] = [ |
|
163
|
|
|
'amount' => $installment->getAmmount(), |
|
164
|
|
|
'paymentDueDate' => $installment->getPaymentDueDate()->format('d-m-Y'), |
|
165
|
|
|
]; |
|
166
|
|
|
} |
|
167
|
|
|
return $out; |
|
168
|
|
|
} |
|
169
|
|
|
private static function getRenderer() { |
|
170
|
|
|
$loader = new Twig_Loader_Filesystem(); |
|
171
|
|
|
// Custom |
|
172
|
|
|
$loader->addPath(Company::getPdfTemplatesPath()); |
|
173
|
|
|
// Defaults |
|
174
|
|
|
$loader->addPath(F72X::getDefaultPdfTemplatesPath()); |
|
175
|
|
|
$view = new Twig_Environment($loader, ['cache' => false]); |
|
176
|
|
|
// I18n ext |
|
177
|
|
|
$view->addExtension(new Twig_Extensions_Extension_Intl()); |
|
178
|
|
|
// Scape html ext |
|
179
|
|
|
$view->addExtension(new Twig_Extension_Escaper('html')); |
|
180
|
|
|
return $view; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
} |
|
184
|
|
|
|