Test Failed
Push — master ( d3c8d8...d4fc42 )
by JAIME ELMER
02:02
created

DocumentGenerator::addTaxes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 16
nc 1
nop 1
dl 0
loc 29
rs 9.7333
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\Sunat\Document\SunatDocument;
18
use F72X\Sunat\Document\Factura;
19
use F72X\Sunat\Document\Boleta;
20
21
class DocumentGenerator {
22
23
    /**
24
     * Generar Factura
25
     * 
26
     * Produce los documentos electronicos listos para ser enviados a SUNAT.
27
     * 
28
     * @param array $data
29
     * @param string $currencyType
30
     */
31
    public static function generateFactura(array $data, $currencyType = 'PEN') {
32
        $Invoice = new InvoiceDocument($data, Catalogo::CAT1_FACTURA, $currencyType);
33
        // Documento XML para la factura
34
        $XmlDoc = new Factura($Invoice);
35
        self::processSutatDoc($XmlDoc);
36
    }
37
    /**
38
     * Generar Boleta
39
     * 
40
     * Produce los documentos electronicos listos para ser enviados a SUNAT.
41
     * 
42
     * @param array $data
43
     * @param string $currencyType
44
     */
45
    public static function generateBoleta(array $data, $currencyType = 'PEN') {
46
        $Invoice = new InvoiceDocument($data, Catalogo::CAT1_BOLETA, $currencyType);
47
        // Documento XML para la factura
48
        $XmlDoc = new Boleta($Invoice);
49
        self::processSutatDoc($XmlDoc);
50
    }
51
52
    private static function processSutatDoc(SunatDocument $XmlDoc) {
53
        // Save Document
54
        self::saveInvoice($XmlDoc);
55
        // Sign Document
56
        self::singInvoice($XmlDoc);
57
        // Compress Document
58
        self::zipInvoice($XmlDoc);
59
    }
60
61
    private static function singInvoice(SunatDocument $Document) {
62
        $xmlFile = $Document->getFileName();
63
        XmlDSig::sign($xmlFile);
64
    }
65
66
    private static function zipInvoice(SunatDocument $Document) {
67
        $xmlFile = $Document->getFileName();
68
        FileService::doZip($xmlFile);
69
    }
70
71
    public static function saveInvoice(SunatDocument $invoice) {
72
        $repository = Company::getRepositoryPath();
73
        $xmlService = new XmlService('1.0', 'ISO-8859-1');
74
75
        $xmlService->namespaceMap = [
76
            "urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"                        => '',
77
            "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"      => 'cac',
78
            "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"          => 'cbc',
79
            "urn:un:unece:uncefact:documentation:2"                                         => 'ccts',
80
            "http://www.w3.org/2000/09/xmldsig#"                                            => 'ds',
81
            "urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"      => 'ext',
82
            "urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2"             => 'qdt',
83
            "urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2"   => 'udt',
84
            "http://www.w3.org/2001/XMLSchema-instance"                                     => 'xsi'
85
        ];
86
        $fileName = $invoice->getFileName();
87
        file_put_contents("$repository/xml/$fileName", $xmlService->write('Invoice', $invoice));
88
    }
89
90
}
91