UblHelper::addAllowancesCharges()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 4
dl 0
loc 6
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.0
7
 * 
8
 * Copyright 2019, Jaime Cruz
9
 */
10
11
namespace F72X\Tools;
12
13
use F72X\Sunat\Catalogo;
14
use F72X\Sunat\Document\SunatInvoice;
15
use F72X\UblComponent\TaxTotal;
16
use F72X\UblComponent\TaxSubTotal;
17
use F72X\UblComponent\TaxCategory;
18
use F72X\UblComponent\TaxScheme;
19
use F72X\UblComponent\InvoiceLine;
20
use F72X\UblComponent\AllowanceCharge;
21
22
class UblHelper {
23
24
    /**
25
     * 
26
     * @param SunatInvoice|InvoiceLine $target
27
     * @param array $allowancesCharges
28
     * @param float $baseAmount
29
     * @param string $currencyCode
30
     */
31
    public static function addAllowancesCharges($target, array $allowancesCharges, $baseAmount, $currencyCode) {
32
        foreach ($allowancesCharges as $item) {
33
            $k = $item['multiplierFactor'];
34
            $amount = $baseAmount * $k;
35
            $chargeIndicator = $item['isCharge'] ? 'true' : 'false';
36
            self::addAllowanceCharge($target, $currencyCode, $chargeIndicator, $item['reasonCode'], $item['multiplierFactor'], $amount, $baseAmount);
37
        }
38
    }
39
40
    /**
41
     * 
42
     * @param SunatInvoice|InvoiceLine $target
43
     * @param string $currencyID
44
     * @param string $ChargeIndicator
45
     * @param string $AllowanceChargeReasonCode
46
     * @param float $Amount
47
     * @param float $BaseAmount
48
     */
49
    public static function addAllowanceCharge($target, $currencyID, $ChargeIndicator, $AllowanceChargeReasonCode, $MultiplierFactorNumeric, $Amount, $BaseAmount) {
50
        $AllowanceCharge = new AllowanceCharge();
51
        $AllowanceCharge
52
                ->setCurrencyID($currencyID)
53
                ->setChargeIndicator($ChargeIndicator)
54
                ->setAllowanceChargeReasonCode($AllowanceChargeReasonCode)
55
                ->setMultiplierFactorNumeric($MultiplierFactorNumeric)
56
                ->setAmount($Amount)
57
                ->setBaseAmount($BaseAmount);
58
        // Add AllowanceCharge
59
        $target
60
                ->addAllowanceCharge($AllowanceCharge);
61
    }
62
63
    public static function addTaxSubtotal(TaxTotal $TaxTotal, $currencyID, $taxAmount, $taxableAmount, $taxTypeCode) {
64
        // XML nodes
65
        $TaxSubTotal = new TaxSubTotal();
66
        $TaxCategory = new TaxCategory();
67
        $TaxCategory->setElementAttributes('ID', [
68
            'schemeID'         => 'UN/ECE 5305',
69
            'schemeName'       => 'Tax Category Identifier',
70
            'schemeAgencyName' => 'United Nations Economic Commission for Europe'
71
        ]);
72
        $TaxScheme = new TaxScheme();
73
        $TaxScheme->setElementAttributes('ID', [
74
            'schemeID'       => 'UN/ECE 5153',
75
            'schemeAgencyID' => '6']);
76
77
        $cat5Item = Catalogo::getCatItem(5, $taxTypeCode);
78
79
        $TaxSubTotal
80
                ->setCurrencyID($currencyID)
81
                ->setTaxAmount($taxAmount)
82
                ->setTaxableAmount($taxableAmount)
83
                ->setTaxCategory($TaxCategory
84
                        ->setID($cat5Item['categoria'])
85
                        ->setTaxScheme($TaxScheme
86
                                ->setID($taxTypeCode)
87
                                ->setName($cat5Item['name'])
88
                                ->setTaxTypeCode($cat5Item['UN_ECE_5153'])));
89
        // Add item
90
        $TaxTotal->addTaxSubTotal($TaxSubTotal);
91
    }
92
93
}
94