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

NotaCredito::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\CreditNote;
23
use Sabre\Xml\Writer;
24
25
class NotaCredito extends CreditNote {
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('CreditNoteLine');
51
        // Impuestos
52
        $this->addDocumentTaxes();
53
        // Totales
54
        $this->addInvoiceLegalMonetaryTotal();
55
    }
56
    public function xmlSerialize(Writer $writer) {
57
        $companyRUC = Company::getRUC();
58
        $companyName = Company::getCompanyName();
59
        // SchemaNS::EXT . 'UBLExtensions'
60
        $UBLExtensions = TemplateMgr::getTpl('UBLExtensions.xml');
61
        $Signature     = TemplateMgr::getTpl('Signature.xml', [
62
                    'ruc'         => $companyRUC,
63
                    'companyName' => $companyName
64
        ]);
65
        $this->writeLineJump($writer);
66
        $writer->writeRaw($UBLExtensions);
67
68
        $writer->write([
69
            SchemaNS::CBC . 'UBLVersionID'         => $this->UBLVersionID,
70
            SchemaNS::CBC . 'CustomizationID'      => $this->CustomizationID,
71
            SchemaNS::CBC . 'ID'                   => $this->ID,
72
            SchemaNS::CBC . 'IssueDate'            => $this->IssueDate->format('Y-m-d'),
73
            SchemaNS::CBC . 'IssueTime'            => $this->IssueDate->format('H:i:s'),
74
            SchemaNS::CBC . 'DocumentCurrencyCode' => $this->DocumentCurrencyCode,
75
            SchemaNS::CAC . 'DiscrepancyResponse'  => $this->DiscrepancyResponse,
76
            SchemaNS::CAC . 'BillingReference'     => $this->BillingReference
77
        ]);
78
79
        // Despatch Document Reference
80
        if ($this->DespatchDocumentReference) {
81
            $writer->write([
82
                SchemaNS::CAC . 'DespatchDocumentReference' => $this->DespatchDocumentReference
83
            ]);
84
        }
85
        // cac:Signature
86
        $writer->writeRaw($Signature);
87
        $writer->write([
88
            SchemaNS::CAC . 'AccountingSupplierParty' => $this->AccountingSupplierParty,
89
            SchemaNS::CAC . 'AccountingCustomerParty' => $this->AccountingCustomerParty,
90
            SchemaNS::CAC . 'TaxTotal'                => $this->TaxTotal,
91
            SchemaNS::CAC . 'LegalMonetaryTotal'      => $this->LegalMonetaryTotal
92
        ]);
93
94
        // Detalle
95
        foreach ($this->CreditNoteLines as $Line) {
96
            $writer->write([
97
                SchemaNS::CAC . 'CreditNoteLine' => $Line
98
            ]);
99
        }
100
    }
101
    public function getReadyToPrintData() {
102
        $dataMap = $this->getDataMap();
103
        $currency = Catalogo::getCurrencyPlural($dataMap->getCurrencyCode());
104
        $payableAmount = $dataMap->getPayableAmount();
105
        $payableInWords = Operations::getAmountInWords($payableAmount, $currency);
106
        return [
107
            'companyRuc'           => Company::getRUC(),
108
            'companyAddress'       => Company::getAddress(),
109
            'companyCity'          => Company::getCity(),
110
            'edocHeaderContent'    => Company::getEdocHeaderContent(),
111
            'edocFooterContent'    => Company::getEdocFooterContent(),
112
            'documentSeries'       => $dataMap->getDocumentSeries(),
113
            'documentNumber'       => $dataMap->getDocumentNumber(),
114
            'officialDocumentName' => $dataMap->getOfficialDocumentName(),
115
            'currency'             => $currency,
116
            'customerRegName'      => $dataMap->getCustomerRegName(),
117
            'customerDocNumber'    => $dataMap->getCustomerDocNumber(),
118
            'customerAddress'      => $dataMap->getCustomerAddress(),
119
            'issueDate'            => $dataMap->getIssueDate()->format('d-m-Y'),
120
            'igvPercent'           => SunatVars::IGV_PERCENT,
121
            'logo'                 => LogoMgr::getLogoString(),
122
            'qr'                   => QrGenerator::getQrString($dataMap), // QR Code
123
            'taxableOperations'    => Operations::formatAmount($dataMap->getTotalTaxableOperations()),    // Total operaciones gravadas
124
            'freeOperations'       => Operations::formatAmount($dataMap->getTotalFreeOperations()),       // Total operaciones gratuitas
125
            'unaffectedOperations' => Operations::formatAmount($dataMap->getTotalUnaffectedOperations()), // Total operaciones inafectas
126
            'exemptedOperations'   => Operations::formatAmount($dataMap->getTotalExemptedOperations()),   // Total operaciones exoneradas
127
            'totalAllowances'      => Operations::formatAmount($dataMap->getTotalAllowances()),           // Total operaciones exoneradas
128
            'igvAmount'            => Operations::formatAmount($dataMap->getIGV()),                       // Total a pagar
129
            'payableAmount'        => Operations::formatAmount($payableAmount),                           // Total a pagar
130
            'payableInWords'       => $payableInWords,                          // Monto en palabras
131
            'items'                => self::getReadyToPrintDataItems($dataMap),  // Items
132
            'noteType'                    => $dataMap->getNoteType(),
133
            'affectedDocumentId'          => $dataMap->getNoteAffectedDocId(),
134
            'affectedDocumentOficialName' => Catalogo::getOfficialDocumentName($dataMap->getNoteAffectedDocType()),
135
            'note'                        => $dataMap->getNoteDescription()
136
        ];
137
    }
138
139
    private static function getReadyToPrintDataItems(DataMap $inv) {
140
        $Items = $inv->getItems();
141
        $ln = $Items->getCount();
142
        $items2 = [];
143
        for ($i = 0; $i < $ln; $i++) {
144
            $items2[]= [
145
                'productCode'       => $Items->getProductCode($i),
146
                'quantity'          => $Items->getQunatity($i),
147
                'unitName'          => Catalogo::getUnitName($Items->getUnitCode($i)),
148
                'unitBillableValue' => Operations::formatAmount($Items->getUnitBillableValue($i)),
149
                'itemPayableAmount' => Operations::formatAmount($Items->getPayableAmount($i)),
150
                'description'       => $Items->getDescription($i)
151
            ];
152
        }
153
        return $items2;
154
    }
155
}
156