Test Failed
Push — master ( 1f5d45...b072a0 )
by JAIME ELMER
04:15
created

BillMixin::addInvoiceAccountingSupplierParty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 36
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 30
nc 1
nop 0
dl 0
loc 36
rs 9.44
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\Document;
12
13
use F72X\Company;
14
use F72X\Sunat\DataMap;
15
use F72X\Sunat\InvoiceItems;
16
use F72X\Sunat\Catalogo;
17
use F72X\Sunat\SunatVars;
18
use F72X\Tools\UblHelper;
19
use F72X\UblComponent\OrderReference;
20
use F72X\UblComponent\Party;
21
use F72X\UblComponent\PartyIdentification;
22
use F72X\UblComponent\PartyTaxScheme;
23
use F72X\UblComponent\RegistrationAddress;
24
use F72X\UblComponent\PartyName;
25
use F72X\UblComponent\AccountingSupplierParty;
26
use F72X\UblComponent\AccountingCustomerParty;
27
use F72X\UblComponent\PartyLegalEntity;
28
use F72X\UblComponent\TaxTotal;
29
use F72X\UblComponent\TaxSubTotal;
30
use F72X\UblComponent\TaxCategory;
31
use F72X\UblComponent\TaxScheme;
32
use F72X\UblComponent\LegalMonetaryTotal;
33
use F72X\UblComponent\InvoiceLine;
34
use F72X\UblComponent\CreditNoteLine;
35
use F72X\UblComponent\DebitNoteLine;
36
use F72X\UblComponent\PricingReference;
37
use F72X\UblComponent\AlternativeConditionPrice;
38
use F72X\UblComponent\Item;
39
use F72X\UblComponent\SellersItemIdentification;
40
use F72X\UblComponent\CommodityClassification;
41
use F72X\UblComponent\Price;
42
43
trait BillMixin {
44
45
    
46
47
    /** @var DataMap */
48
    private $dataMap;
49
50
    public function getDataMap() {
51
        return $this->dataMap;
52
    }
53
54
    /**
55
     * 
56
     * @return InvoiceItems
57
     */
58
    public function getItems() {
59
        return $this->dataMap->getItems();
60
    }
61
62
    /**
63
     * 
64
     * @param string $lineType InvoiceLine|CreditNoteLine|DebitNoteLine
65
     */
66
    private function addDocumentItems($lineType) {
67
        $ln = $this->dataMap->getTotalItems();
68
        // Loop
69
        for ($i = 0; $i < $ln; $i++) {
70
            $this->addDocumentItem($i, $lineType);
71
        }
72
    }
73
74
    private function addInvoiceOrderReference() {
75
        $orderNumer = $this->dataMap->getPurchaseOrder();
76
        if ($orderNumer) {
77
            // Xml Node
78
            $orderRef = new OrderReference();
79
            $orderRef->setID($orderNumer);
80
            // Añadir al documento
81
            parent::setOrderReference($orderRef);
82
        }
83
    }
84
85
    private function addDocumentTaxes() {
86
        $Invoice                   = $this->dataMap;
87
        $currencyID                = $Invoice->getCurrencyCode();              // Tipo de moneda
88
        $totalTaxableOperations    = $Invoice->getTotalTaxableOperations();    // Total operaciones gravadas
89
        $totalTaxes                = $Invoice->getTotalTaxes();                // Total operaciones gravadas
90
        $Igv                       = $Invoice->getIGV();                       // Total IGV
91
        $totalExemptedOperations   = $Invoice->getTotalExemptedOperations();   // Total operaciones exoneradas
92
        $totalUnaffectedOperations = $Invoice->getTotalUnaffectedOperations(); // Total operaciones inafectas
93
        $totalFreeOpertions        = $Invoice->getTotalFreeOperations();       // Total operaciones gratuitas
94
95
        // XML nodes
96
        $TaxTotal = new TaxTotal();
97
98
        // Operaciones gravadas
99
        if ($Igv) {
100
            UblHelper::addTaxSubtotal($TaxTotal, $currencyID, $Igv, $totalTaxableOperations, Catalogo::CAT5_IGV);
101
        }
102
        // Total operaciones exoneradas
103
        if ($totalExemptedOperations) {
104
            UblHelper::addTaxSubtotal($TaxTotal, $currencyID, 0, $totalExemptedOperations,   Catalogo::CAT5_EXO);
105
        }
106
        // Total operaciones inafectas
107
        if ($totalUnaffectedOperations) {
108
            UblHelper::addTaxSubtotal($TaxTotal, $currencyID, 0, $totalUnaffectedOperations, Catalogo::CAT5_INA);
109
        }
110
        // Total operaciones gratuitas solo aplica a FACTURA
111
        if ($totalFreeOpertions && $Invoice->getDocumentType() === Catalogo::DOCTYPE_FACTURA) {
112
            UblHelper::addTaxSubtotal($TaxTotal, $currencyID, 0, $totalFreeOpertions,        Catalogo::CAT5_GRA);
113
        }
114
115
        // Total impuestos
116
        $TaxTotal
117
                ->setCurrencyID($currencyID)
118
                ->setTaxAmount($totalTaxes);
119
        // Anadir al documento
120
        parent::setTaxTotal($TaxTotal);
121
    }
122
123
    /**
124
     * 
125
     * @param int $itemIndex Index del item
126
     * @param string $lineType InvoiceLine|CreditNoteLine|DebitNoteLine
127
     */
128
    
129
    private function addDocumentItem($itemIndex, $lineType) {
130
        $docLineClassName = "\F72X\UblComponent\\$lineType";
131
        // XML Nodes
132
        $DocumentLine     = new $docLineClassName();
133
        $PricingReference = new PricingReference();
134
        $TaxTotal         = new TaxTotal();
135
        $TaxSubTotal      = new TaxSubTotal();
136
        $TaxCategory      = new TaxCategory();
137
        $TaxCategory
138
                ->setElementAttributes('ID', [
139
                    'schemeID'         => 'UN/ECE 5305',
140
                    'schemeName'       => 'Tax Category Identifier',
141
                    'schemeAgencyName' => 'United Nations Economic Commission for Europe'])
142
                ->setElementAttributes('TaxExemptionReasonCode', [
143
                    'listAgencyName'   => 'PE:SUNAT',
144
                    'listName'         => 'SUNAT:Codigo de Tipo de Afectación del IGV',
145
                    'listURI'          => 'urn:pe:gob:sunat:cpe:see:gem:catalogos:catalogo07']);
146
147
        $TaxScheme = new TaxScheme();
148
        $TaxScheme
149
                ->setElementAttributes('ID', [
150
                    'schemeID'         => 'UN/ECE 5153',
151
                    'schemeName'       => 'Tax Scheme Identifier',
152
                    'schemeAgencyName' => 'United Nations Economic Commission for Europe']);
153
154
        $AlternativeConditionPrice  = new AlternativeConditionPrice();
155
        $Item                       = new Item();
156
        $SellersItemIdentification  = new SellersItemIdentification();
157
        $CommodityClassification    = new CommodityClassification();
158
        $Price                      = new Price();
159
        // Detail Operation Matrix
160
        $Items = $this->dataMap->getItems();
161
        // Vars
162
        $productCode        = $Items->getProductCode($itemIndex);
163
        $sunatProductCode   = $Items->getUNPSC($itemIndex);
164
        $unitCode           = $Items->getUnitCode($itemIndex);
165
        $quantity           = $Items->getQunatity($itemIndex);
166
        $description        = $Items->getDescription($itemIndex);
167
        $currencyCode       = $Items->getCurrencyCode($itemIndex);
168
        $unitBillableValue  = $Items->getUnitBillableValue($itemIndex);
169
        $priceTypeCode      = $Items->getPriceTypeCode($itemIndex);
170
        $taxTypeCode        = $Items->getTaxTypeCode($itemIndex);
171
        $igvAffectationType = $Items->getIgvAffectationType($itemIndex);
172
173
        $itemValue          = $Items->getItemValue($itemIndex);
174
        $ac                 = $Items->getAllowancesAndCharges($itemIndex);
175
        $itemTaxableAmount  = $Items->getTaxableAmount($itemIndex);
176
        $itemTaxAmount      = $Items->getIgv($itemIndex);
177
        $unitPrice          = $Items->getUnitTaxedValue($itemIndex);
178
179
        // Catálogo 5 Ipuesto aplicable
180
        $cat5Item = Catalogo::getCatItem(5, $taxTypeCode);
181
182
        // Descuentos y cargos
183
        UblHelper::addAllowancesCharges($DocumentLine, $ac, $itemValue, $currencyCode);
184
185
        // Config Item
186
        $Item->setDescription($description); // Descripción
187
        // Código de producto
188
        if ($productCode) {
189
            $Item->setSellersItemIdentification($SellersItemIdentification->setID($productCode));
190
        }
191
        // Código de producto SUNAT
192
        if ($sunatProductCode) {
193
            $Item->setCommodityClassification($CommodityClassification->setItemClassificationCode($sunatProductCode));
194
        }
195
        $DocumentLine
196
                ->setCurrencyID($currencyCode)                    // Tipo de moneda
197
                ->setID($itemIndex + 1)                         // Número de orden
198
                ->setUnitCode($unitCode)                        // Codigo de unidad de medida
199
                ->setLineExtensionAmount($itemTaxableAmount)    // Valor de venta del ítem, sin impuestos
200
                ->setPricingReference($PricingReference
201
                        ->setAlternativeConditionPrice($AlternativeConditionPrice
202
                                ->setCurrencyID($currencyCode)            // Tipo de moneda
203
                                ->setPriceAmount($unitPrice)            // Precio de venta unitario
204
                                ->setPriceTypeCode($priceTypeCode)))    // Price
205
                ->setTaxTotal($TaxTotal
206
                        ->setCurrencyID($currencyCode)
207
                        ->setTaxAmount($itemTaxAmount)
208
                        ->addTaxSubTotal($TaxSubTotal
209
                                ->setCurrencyID($currencyCode)            // Tipo de moneda
210
                                ->setTaxableAmount($itemTaxableAmount)  // Valor de venta del item sin impuestos
211
                                ->setTaxAmount($itemTaxAmount)          // IGV
212
                                ->setTaxCategory($TaxCategory
213
                                        ->setID($cat5Item['categoria'])                     // Codigo de categoria de immpuestos @CAT5
214
                                        ->setPercent(SunatVars::IGV_PERCENT)                // Porcentaje de IGV (18.00)
215
                                        ->setTaxExemptionReasonCode($igvAffectationType)    // Código de afectación del IGV
216
                                        ->setTaxScheme($TaxScheme
217
                                                ->setID($taxTypeCode)                       // Codigo de categoria de impuesto
218
                                                ->setName($cat5Item['name'])
219
                                                ->setTaxTypeCode($cat5Item['UN_ECE_5153'])))))
220
                ->setItem($Item)
221
                ->setPrice($Price
222
                        ->setCurrencyID($currencyCode)    // Tipo de moneda
223
                        ->setPriceAmount($unitBillableValue)    // Precio unitario del item
224
        );
225
        // Set Quantity
226
        $this->setDocumentLineQuantity($DocumentLine, $lineType, $quantity);
227
        // Añade item
228
        $this->addDocumentLine($DocumentLine, $lineType);
229
    }
230
231
    /**
232
     * 
233
     * @param InvoiceLine|CreditNoteLine|DebitNoteLine $DocumentLine
234
     * @param string $lineType InvoiceLine|CreditNoteLine|DebitNoteLine
235
     * @param int $quantity
236
     */
237
    private function addDocumentLine($DocumentLine, $lineType) {
238
        switch ($lineType) {
239
            case 'InvoiceLine' :
240
                parent::addInvoiceLine($DocumentLine);
241
                break;
242
            case 'CreditNoteLine' :
243
                parent::addCreditNoteLine($DocumentLine);
244
                break;
245
            case 'DebitNoteLine' :
246
                parent::addDebitNoteLine($DocumentLine);
247
                break;
248
        }
249
    }
250
    /**
251
     * 
252
     * @param InvoiceLine|CreditNoteLine|DebitNoteLine $DocumentLine
253
     * @param string $lineType InvoiceLine|CreditNoteLine|DebitNoteLine
254
     * @param int $quantity
255
     */
256
    private function setDocumentLineQuantity($DocumentLine, $lineType, $quantity) {
257
        switch ($lineType) {
258
            case 'InvoiceLine' :
259
                $DocumentLine->setInvoicedQuantity($quantity);
0 ignored issues
show
Bug introduced by
The method setInvoicedQuantity() does not exist on F72X\UblComponent\DebitNoteLine. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

259
                $DocumentLine->/** @scrutinizer ignore-call */ 
260
                               setInvoicedQuantity($quantity);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method setInvoicedQuantity() does not exist on F72X\UblComponent\CreditNoteLine. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

259
                $DocumentLine->/** @scrutinizer ignore-call */ 
260
                               setInvoicedQuantity($quantity);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
260
                break;
261
            case 'CreditNoteLine' :
262
                $DocumentLine->setCreditedQuantity($quantity);
0 ignored issues
show
Bug introduced by
The method setCreditedQuantity() does not exist on F72X\UblComponent\DebitNoteLine. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

262
                $DocumentLine->/** @scrutinizer ignore-call */ 
263
                               setCreditedQuantity($quantity);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method setCreditedQuantity() does not exist on F72X\UblComponent\InvoiceLine. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

262
                $DocumentLine->/** @scrutinizer ignore-call */ 
263
                               setCreditedQuantity($quantity);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
263
                break;
264
            case 'DebitNoteLine' :
265
                $DocumentLine->setDebitedQuantity($quantity);
0 ignored issues
show
Bug introduced by
The method setDebitedQuantity() does not exist on F72X\UblComponent\CreditNoteLine. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

265
                $DocumentLine->/** @scrutinizer ignore-call */ 
266
                               setDebitedQuantity($quantity);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method setDebitedQuantity() does not exist on F72X\UblComponent\InvoiceLine. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

265
                $DocumentLine->/** @scrutinizer ignore-call */ 
266
                               setDebitedQuantity($quantity);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
266
                break;
267
        }
268
    }
269
270
    private function addInvoiceLegalMonetaryTotal() {
271
        $Invoice            = $this->dataMap;
272
        $currencyID         = $this->getDocumentCurrencyCode(); // Tipo de moneda
0 ignored issues
show
Bug introduced by
It seems like getDocumentCurrencyCode() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

272
        /** @scrutinizer ignore-call */ 
273
        $currencyID         = $this->getDocumentCurrencyCode(); // Tipo de moneda
Loading history...
273
        $totalAllowances    = $Invoice->getTotalAllowances();   // Total descuentos
274
        $payableAmount      = $Invoice->getPayableAmount();     // Total a pagar
275
        $billableAmount     = $Invoice->getBillableValue();
276
        // LegalMonetaryTotal
277
        $LegalMonetaryTotal = new LegalMonetaryTotal();
278
        $LegalMonetaryTotal
279
                ->setCurrencyID($currencyID)
280
                ->setLineExtensionAmount($billableAmount)
281
                ->setTaxInclusiveAmount($payableAmount)
282
                ->setAllowanceTotalAmount($totalAllowances)
283
                ->setPayableAmount($payableAmount);
284
285
        parent::setLegalMonetaryTotal($LegalMonetaryTotal);
286
    }
287
288
    private function addInvoiceAccountingSupplierParty() {
289
        // Info
290
        $partyName  = Company::getBusinessName();
291
        $regName    = Company::getCompanyName();
292
        $docNumber  = Company::getRUC();
293
        $addressRegCode = Company::getRegAddressCode(); // Código de domicilio fiscal o anexo
294
        $docType    = Catalogo::IDENTIFICATION_DOC_RUC;
295
296
        // XML nodes
297
        $AccountingSupplierParty    = new AccountingSupplierParty();
298
        $Party                      = new Party();
299
        $PartyIdentification        = new PartyIdentification();
300
        $PartyTaxScheme             = new PartyTaxScheme();
301
        $RegistrationAddress        = new RegistrationAddress();
302
        $PartyIdentification
303
                ->setElementAttributes('ID', [
304
                    'schemeAgencyName'  => 'PE:SUNAT',
305
                    'schemeID'          => $docType,
306
                    'schemeName'        => 'Documento de Identidad',
307
                    'schemeURI'         => 'urn:pe:gob:sunat:cpe:see:gem:catalogos:catalogo06']);
308
        $PartyName                  = new PartyName();
309
        $PartyLegalEntity           = new PartyLegalEntity();
310
311
        $AccountingSupplierParty
312
                ->setParty($Party
313
                        ->setPartyIdentification($PartyIdentification
314
                                ->setID($docNumber))
315
                        ->setPartyName($PartyName
316
                                ->setName($partyName))
317
                        ->setPartyTaxScheme($PartyTaxScheme
318
                                ->setRegistrationAddress($RegistrationAddress
319
                                        ->setAddressTypeCode($addressRegCode)))
320
                        ->setPartyLegalEntity($PartyLegalEntity
321
                                ->setRegistrationName($regName)));
322
        // Add to Document
323
        parent::setAccountingSupplierParty($AccountingSupplierParty);
324
    }
325
326
    private function addInvoiceAccountingCustomerParty() {
327
        $Invoice   = $this->dataMap;
328
        // Info
329
        $regName   = $Invoice->getCustomerRegName();
330
        $docNumber = $Invoice->getCustomerDocNumber();
331
        $docType   = $Invoice->getCustomerDocType();
332
333
        // XML nodes
334
        $AccountingCustomerParty    = new AccountingCustomerParty();
335
        $Party                      = new Party();
336
        $PartyIdentification        = new PartyIdentification();
337
        $PartyLegalEntity           = new PartyLegalEntity();
338
        $PartyIdentification
339
                ->setElementAttributes('ID', [
340
                    'schemeAgencyName'  => 'PE:SUNAT',
341
                    'schemeID'          => $docType,
342
                    'schemeName'        => 'Documento de Identidad',
343
                    'schemeURI'         => 'urn:pe:gob:sunat:cpe:see:gem:catalogos:catalogo06']);
344
345
        $AccountingCustomerParty
346
                ->setParty($Party
347
                        ->setPartyIdentification($PartyIdentification
348
                                ->setID($docNumber))
349
                        ->setPartyLegalEntity($PartyLegalEntity
350
                                ->setRegistrationName($regName)));
351
        // Add to Document
352
        parent::setAccountingCustomerParty($AccountingCustomerParty);
353
    }
354
355
    /**
356
     *    
357
     * @return string Nombre del comprobante de acuerdo con las especificaciones de la SUNAT
358
     */
359
    public function getBillName() {
360
        return $this->dataMap->getBillName();
361
    }
362
363
}
364