Test Failed
Push — master ( c55034...101a53 )
by JAIME ELMER
03:40
created

NotaDebito::getReadyToPrintData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 34
nc 1
nop 0
dl 0
loc 35
rs 9.376
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\Document;
12
13
use F72X\Company;
14
use F72X\Tools\TemplateMgr;
15
use F72X\Tools\LogoMgr;
16
use F72X\Tools\QrGenerator;
17
use F72X\Sunat\DataMap;
18
use F72X\Sunat\SunatVars;
19
use F72X\Sunat\Operations;
20
use F72X\Sunat\Catalogo;
21
use F72X\UblComponent\SchemaNS;
22
use F72X\UblComponent\DebitNote;
23
use Sabre\Xml\Writer;
24
25
class NotaDebito extends DebitNote {
26
27
    use BillMixin, NoteMixin;
28
29
    protected $UBLVersionID = '2.1';
30
    protected $CustomizationID = '2.0';
31
32
    public function __construct(DataMap $DataMap) {
33
        $this->dataMap = $DataMap;
34
        $currencyCode = $DataMap->getCurrencyCode();
35
        // ID
36
        $this->setID($DataMap->getDocumentId());
37
        // Fecha de emisión
38
        $this->setIssueDate($DataMap->getIssueDate());
39
        // Tipo de moneda
40
        $this->setDocumentCurrencyCode($currencyCode);
41
        // Motivo de emisión
42
        $this->addDiscrepancyResponse();
43
        // Motivo de emisión
44
        $this->addBillingReference();
45
        // Información de la empresa
46
        $this->addInvoiceAccountingSupplierParty();
47
        // Información del cliente
48
        $this->addInvoiceAccountingCustomerParty();
49
        // Detalle
50
        $this->addDocumentItems('DebitNoteLine');
51
        // Impuestos
52
        $this->addDocumentTaxes();
53
        // Totales
54
        $this->addRequestedMonetaryTotal();
55
    }
56
57
    public function xmlSerialize(Writer $writer) {
58
        $companyRUC = Company::getRUC();
59
        $companyName = Company::getCompanyName();
60
        // SchemaNS::EXT . 'UBLExtensions'
61
        $UBLExtensions = TemplateMgr::getTpl('UBLExtensions.xml');
62
        $Signature     = TemplateMgr::getTpl('Signature.xml', [
63
                    'ruc'         => $companyRUC,
64
                    'companyName' => $companyName
65
        ]);
66
        $this->writeLineJump($writer);
67
        $writer->writeRaw($UBLExtensions);
68
69
        $writer->write([
70
            SchemaNS::CBC . 'UBLVersionID'         => $this->UBLVersionID,
71
            SchemaNS::CBC . 'CustomizationID'      => $this->CustomizationID,
72
            SchemaNS::CBC . 'ID'                   => $this->ID,
73
            SchemaNS::CBC . 'IssueDate'            => $this->IssueDate->format('Y-m-d'),
74
            SchemaNS::CBC . 'IssueTime'            => $this->IssueDate->format('H:i:s'),
75
            SchemaNS::CBC . 'DocumentCurrencyCode' => $this->DocumentCurrencyCode,
76
            SchemaNS::CAC . 'DiscrepancyResponse'  => $this->DiscrepancyResponse,
77
            SchemaNS::CAC . 'BillingReference'     => $this->BillingReference
78
        ]);
79
80
        // Despatch Document Reference
81
        if ($this->DespatchDocumentReference) {
82
            $writer->write([
83
                SchemaNS::CAC . 'DespatchDocumentReference' => $this->DespatchDocumentReference
84
            ]);
85
        }
86
        // cac:Signature
87
        $writer->writeRaw($Signature);
88
        $writer->write([
89
            SchemaNS::CAC . 'AccountingSupplierParty' => $this->AccountingSupplierParty,
90
            SchemaNS::CAC . 'AccountingCustomerParty' => $this->AccountingCustomerParty,
91
            SchemaNS::CAC . 'TaxTotal'                => $this->TaxTotal,
92
            SchemaNS::CAC . 'RequestedMonetaryTotal'  => $this->RequestedMonetaryTotal
93
        ]);
94
95
        // Detalle
96
        foreach ($this->DebitNoteLines as $Line) {
97
            $writer->write([
98
                SchemaNS::CAC . 'DebitNoteLine' => $Line
99
            ]);
100
        }
101
    }
102
    public function getReadyToPrintData() {
103
        $dataMap = $this->getDataMap();
104
        $currency = Catalogo::getCurrencyPlural($dataMap->getCurrencyCode());
105
        $payableAmount = $dataMap->getPayableAmount();
106
        $payableInWords = Operations::getAmountInWords($payableAmount, $currency);
107
        return [
108
            'companyRuc'           => Company::getRUC(),
109
            'companyAddress'       => Company::getAddress(),
110
            'companyCity'          => Company::getCity(),
111
            'edocHeaderContent'    => Company::getEdocHeaderContent(),
112
            'edocFooterContent'    => Company::getEdocFooterContent(),
113
            'documentSeries'       => $dataMap->getDocumentSeries(),
114
            'documentNumber'       => $dataMap->getDocumentNumber(),
115
            'officialDocumentName' => $dataMap->getOfficialDocumentName(),
116
            'currency'             => $currency,
117
            'customerRegName'      => $dataMap->getCustomerRegName(),
118
            'customerDocNumber'    => $dataMap->getCustomerDocNumber(),
119
            'customerAddress'      => $dataMap->getCustomerAddress(),
120
            'issueDate'            => $dataMap->getIssueDate()->format('d-m-Y'),
121
            'igvPercent'           => SunatVars::IGV_PERCENT,
122
            'logo'                 => LogoMgr::getLogoString(),
123
            'qr'                   => QrGenerator::getQrString($dataMap), // QR Code
124
            'taxableOperations'    => Operations::formatAmount($dataMap->getTotalTaxableOperations()),    // Total operaciones gravadas
125
            'freeOperations'       => Operations::formatAmount($dataMap->getTotalFreeOperations()),       // Total operaciones gratuitas
126
            'unaffectedOperations' => Operations::formatAmount($dataMap->getTotalUnaffectedOperations()), // Total operaciones inafectas
127
            'exemptedOperations'   => Operations::formatAmount($dataMap->getTotalExemptedOperations()),   // Total operaciones exoneradas
128
            'totalAllowances'      => Operations::formatAmount($dataMap->getTotalAllowances()),           // Total operaciones exoneradas
129
            'igvAmount'            => Operations::formatAmount($dataMap->getIGV()),                       // Total a pagar
130
            'payableAmount'        => Operations::formatAmount($payableAmount),                           // Total a pagar
131
            'payableInWords'       => $payableInWords,                          // Monto en palabras
132
            'items'                => self::getReadyToPrintDataItems($dataMap),  // Items
133
            'noteType'                    => $dataMap->getNoteType(),
134
            'affectedDocumentId'          => $dataMap->getNoteAffectedDocId(),
135
            'affectedDocumentOficialName' => Catalogo::getOfficialDocumentName($dataMap->getNoteAffectedDocType()),
136
            'note'                        => $dataMap->getNoteDescription()
137
        ];
138
    }
139
140
    private static function getReadyToPrintDataItems(DataMap $inv) {
141
        $Items = $inv->getItems();
142
        $ln = $Items->getCount();
143
        $items2 = [];
144
        for ($i = 0; $i < $ln; $i++) {
145
            $items2[]= [
146
                'productCode'       => $Items->getProductCode($i),
147
                'quantity'          => $Items->getQunatity($i),
148
                'unitName'          => Catalogo::getUnitName($Items->getUnitCode($i)),
149
                'unitBillableValue' => Operations::formatAmount($Items->getUnitBillableValue($i)),
150
                'itemPayableAmount' => Operations::formatAmount($Items->getPayableAmount($i)),
151
                'description'       => $Items->getDescription($i)
152
            ];
153
        }
154
        return $items2;
155
    }
156
}
157