DocumentGenerator::getNamespaceMap()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 23
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 21
nc 5
nop 1
dl 0
loc 23
rs 9.2728
c 0
b 0
f 0
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\Sunat;
12
13
use InvalidArgumentException;
14
use F72X\Tools\XmlDSig;
15
use F72X\Tools\PdfGenerator;
16
use F72X\Repository;
17
use F72X\Sunat\Catalogo;
18
use F72X\Tools\XmlService;
19
use F72X\Sunat\Document\Factura;
20
use F72X\Sunat\Document\Boleta;
21
use F72X\Sunat\Document\NotaCredito;
22
use F72X\Sunat\Document\NotaDebito;
23
use F72X\Sunat\Document\ResumenDiario;
24
use F72X\Sunat\Document\ComunicacionBaja;
25
use F72X\Sunat\Document\Percepcion;
26
use F72X\Sunat\Document\Retencion;
27
28
class DocumentGenerator {
29
30
    /**
31
     * Crear documento electrónico
32
     * 
33
     * Crea Factura, Boleta, Nota de crédito y Nota de débito.
34
     * 
35
     * Procesa la data proporcionada para el tipo de documento indicado
36
     * 
37
     * @param string $shortCode FAC|BOL|NCR|NDE
38
     * @param array $data
39
     * @param string $currencyCode
40
     */
41
    public static function createDocument($shortCode, array $data) {
42
        // Validate type
43
        if (!in_array($shortCode, ['FAC', 'BOL', 'NCR', 'NDE'])) {
44
            throw new InvalidArgumentException("F72X: El tipo '$shortCode', es invalido use FAC|BOL|NCR|NDE");
45
        }
46
        // Set Document Type Code
47
        $docType = Catalogo::getDocumentType($shortCode);
48
        $data['documentType'] = $docType;
49
        // Validate input
50
        self::validateData($data, $shortCode);
51
        // Data map
52
        $dataMap = new DataMap($data, $docType);
53
        // Generate XML
54
        if ($docType == Catalogo::DOCTYPE_FACTURA) {
55
            return new Factura($dataMap);
56
        }
57
        if ($docType == Catalogo::DOCTYPE_BOLETA) {
58
            return new Boleta($dataMap);
59
        }
60
        if ($docType == Catalogo::DOCTYPE_NOTA_CREDITO) {
61
            return new NotaCredito($dataMap);
62
        }
63
        return new NotaDebito($dataMap);
64
    }
65
66
    /**
67
     * 
68
     * @param string $documentType 01|03|07|08
69
     * @param string $affectedDocumentType 01|03
70
     * @param string $baseSeries ###|C##|D##
71
     * @return string F###|B###|FC##|FD#|#BC##|BD##
72
     */
73
    public static function buildDocumentSeries($documentType, $affectedDocumentType, $baseSeries) {
74
        return Catalogo::getDocumentSeriesPrefix($documentType, $affectedDocumentType) . $baseSeries;
75
    }
76
77
    private static function validateData(array $data, $type) {
78
        $validator = new InputValidator($data, $type);
79
        // Input validation
80
        if (!$validator->isValid()) {
81
            $validator->throwExc();
82
        }
83
    }
84
85
    /**
86
     * 
87
     * @param Factura|Boleta|NotaCredito|NotaDebito $XmlDoc
88
     */
89
    public static function generateFiles($XmlDoc) {
90
        // Save Input
91
        self::saveDocumentInput($XmlDoc);
92
        // Save Document
93
        self::saveDocument($XmlDoc);
94
        // Sign Document
95
        self::signDocument($XmlDoc);
96
        // Compress Document
97
        self::zipDocument($XmlDoc);
98
        // Generate PDF
99
        self::generatePdf($XmlDoc);
100
    }
101
102
    /**
103
     * 
104
     * @param Factura|Boleta|NotaCredito|NotaDebito $XmlDoc
105
     */
106
    public static function regenerateFiles($XmlDoc) {
107
        // Save Document
108
        self::saveDocument($XmlDoc);
109
        // Sign Document
110
        self::signDocument($XmlDoc);
111
        // Compress Document
112
        self::zipDocument($XmlDoc);
113
        // Generate PDF
114
        self::generatePdf($XmlDoc);
115
    }
116
117
    /**
118
     * 
119
     * @param array $data
120
     * @return ResumenDiario
121
     */
122
    public static function createResumenDiario($data) {
123
        return new ResumenDiario($data);
124
    }
125
126
    /**
127
     * 
128
     * @param array $data La data
129
     * @return ComunicacionBaja
130
     */
131
    public static function createComunicacionBaja($data) {
132
        return new ComunicacionBaja($data);
133
    }
134
135
    /**
136
     * 
137
     * @param array $data La data
138
     * @return Perception
139
     */
140
    public static function createPercepcion($data) {
141
        self::validateData($data, 'PER');
142
        return new Percepcion($data);
143
    }
144
145
    /**
146
     * 
147
     * @param array $data La data
148
     * @return Perception
149
     */
150
    public static function createRetencion($data) {
151
        return new Retencion($data);
152
    }
153
154
    public static function generateResumenFiles(ResumenDiario $eDocument) {
155
        $eDocument->generateFiles();
156
    }
157
158
    private static function saveDocumentInput($XmlDoc) {
159
        $documentName = $XmlDoc->getDocumentName();
160
        Repository::saveDocumentInput($documentName, json_encode($XmlDoc->getDataMap()->getRawData(), JSON_PRETTY_PRINT));
161
    }
162
163
    private static function signDocument($XmlDoc) {
164
        $documentName = $XmlDoc->getDocumentName();
165
        XmlDSig::sign($documentName);
166
    }
167
168
    private static function zipDocument($XmlDoc) {
169
        $documentName = $XmlDoc->getDocumentName();
170
        Repository::zipDocument($documentName);
171
    }
172
173
    public static function generatePdf($XmlDoc) {
174
        $documentName = $XmlDoc->getDocumentName();
175
        $Invoice = $XmlDoc->getDataMap();
176
        PdfGenerator::generatePdf($Invoice, $documentName);
177
    }
178
179
    private static function saveDocument($Bill) {
180
        $xmlService = new XmlService('1.0', 'ISO-8859-1');
181
        $documentType = $Bill->getDataMap()->getDocumentType();
182
        // Set namespaces
183
        $xmlService->namespaceMap = self::getNamespaceMap($documentType);
184
        $documentName = $Bill->getDocumentName();
185
        // Xml Root
186
        $xmlRoot = self::getXmlRoot($documentType);
187
        $billContent = $xmlService->write($xmlRoot, $Bill);
188
        Repository::saveDocument($documentName, $billContent);
189
    }
190
191
    /**
192
     * 
193
     * @param string $documentType 01|03|07|08
194
     * @return string Invoice|CreditNote|DebitNote
195
     */
196
    private static function getXmlRoot($documentType) {
197
        switch ($documentType) {
198
            case Catalogo::DOCTYPE_FACTURA :
199
            case Catalogo::DOCTYPE_BOLETA : return 'Invoice';
200
            case Catalogo::DOCTYPE_NOTA_CREDITO : return 'CreditNote';
201
            case Catalogo::DOCTYPE_NOTA_DEBITO : return 'DebitNote';
202
        }
203
    }
204
205
    /**
206
     * 
207
     * @param string $documentType 01|03|07|08
208
     * @return array
209
     */
210
    private static function getNamespaceMap($documentType) {
211
        switch ($documentType) {
212
            case Catalogo::DOCTYPE_FACTURA :
213
            case Catalogo::DOCTYPE_BOLETA :
214
                $topNamespace = 'urn:oasis:names:specification:ubl:schema:xsd:Invoice-2';
215
                break;
216
            case Catalogo::DOCTYPE_NOTA_CREDITO :
217
                $topNamespace = 'urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2';
218
                break;
219
            case Catalogo::DOCTYPE_NOTA_DEBITO :
220
                $topNamespace = 'urn:oasis:names:specification:ubl:schema:xsd:DebitNote-2';
221
                break;
222
        }
223
        return [
224
            $topNamespace => '',
225
            'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2' => 'cac',
226
            'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2' => 'cbc',
227
            'urn:un:unece:uncefact:documentation:2' => 'ccts',
228
            'http://www.w3.org/2000/09/xmldsig#' => 'ds',
229
            'urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2' => 'ext',
230
            'urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2' => 'qdt',
231
            'urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2' => 'udt',
232
            'http://www.w3.org/2001/XMLSchema-instance' => 'xsi'
233
        ];
234
    }
235
236
}
237