Passed
Branch develop (5fc816)
by JAIME ELMER
01:58
created

DocumentGenerator::validateData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 5
rs 10
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.1
7
 * 
8
 * Copyright 2018, Jaime Cruz
9
 */
10
11
namespace F72X\Sunat;
12
13
use F72X\Tools\XmlService;
14
use F72X\Tools\XmlDSig;
15
use F72X\Tools\FileService;
16
use F72X\Company;
17
use F72X\Repository;
18
use F72X\Sunat\Document\SunatDocument;
19
use F72X\Sunat\Document\Factura;
20
use F72X\Sunat\Document\Boleta;
21
use F72X\Exception\InvalidInputException;
22
23
class DocumentGenerator {
24
25
    /**
26
     * Generar Factura
27
     * 
28
     * Produce los documentos electronicos listos para ser enviados a SUNAT.
29
     * 
30
     * @param array $data
31
     * @param string $currencyType
32
     */
33
    public static function generateFactura(array $data, $currencyType = 'PEN') {
34
        // Validate input
35
        self::validateData($data, Catalogo::CAT1_FACTURA);
36
        // Core invoice
37
        $Invoice = new InvoiceDocument($data, Catalogo::CAT1_FACTURA, $currencyType);
38
        // Documento XML para la factura
39
        $XmlDoc = new Factura($Invoice);
40
        self::processSutatDoc($XmlDoc);
41
        return $XmlDoc;
42
    }
43
44
    /**
45
     * Generar Boleta
46
     * 
47
     * Produce los documentos electronicos listos para ser enviados a SUNAT.
48
     * 
49
     * @param array $data
50
     * @param string $currencyType
51
     */
52
    public static function generateBoleta(array $data, $currencyType = 'PEN') {
53
        // Validate input
54
        self::validateData($data, Catalogo::CAT1_BOLETA);
55
        // Core invoice
56
        $Invoice = new InvoiceDocument($data, Catalogo::CAT1_BOLETA, $currencyType);
57
        // Documento XML para la factura
58
        $XmlDoc = new Boleta($Invoice);
59
        self::processSutatDoc($XmlDoc);
60
        return $XmlDoc;
61
    }
62
63
    private static function validateData(array $data, $type) {
64
        $validator = new InputValidator($data, $type);
65
        // Input validation
66
        if (!$validator->isValid()) {
67
            throw new InvalidInputException($validator->getErrors());
68
        }
69
    }
70
71
    private static function processSutatDoc(SunatDocument $XmlDoc) {
72
        // Save Document
73
        self::saveInvoice($XmlDoc);
74
        // Sign Document
75
        self::singInvoice($XmlDoc);
76
        // Compress Document
77
        self::zipInvoice($XmlDoc);
78
    }
79
80
    private static function singInvoice(SunatDocument $Document) {
81
        $billName = $Document->getBillName();
82
        XmlDSig::sign($billName);
83
    }
84
85
    private static function zipInvoice(SunatDocument $Document) {
86
        $billName = $Document->getBillName();
87
        FileService::doZip("$billName.xml");
88
    }
89
90
    private static function saveInvoice(SunatDocument $invoice) {
91
        $repository = Company::getRepositoryPath();
0 ignored issues
show
Unused Code introduced by
The assignment to $repository is dead and can be removed.
Loading history...
92
        $xmlService = new XmlService('1.0', 'ISO-8859-1');
93
94
        $xmlService->namespaceMap = [
95
            "urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"                        => '',
96
            "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"      => 'cac',
97
            "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"          => 'cbc',
98
            "urn:un:unece:uncefact:documentation:2"                                         => 'ccts',
99
            "http://www.w3.org/2000/09/xmldsig#"                                            => 'ds',
100
            "urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"      => 'ext',
101
            "urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2"             => 'qdt',
102
            "urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2"   => 'udt',
103
            "http://www.w3.org/2001/XMLSchema-instance"                                     => 'xsi'
104
        ];
105
        $billName = $invoice->getBillName();
106
        $billContent = $xmlService->write('Invoice', $invoice);
107
        Repository::saveBill($billName, $billContent);
108
    }
109
110
}
111