Operations::applyAllowancesAndCharges()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 4
nop 2
dl 0
loc 17
rs 9.8333
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;
12
13
use NumberToWords\NumberToWords;
14
15
class Operations {
16
17
    public static function formatAmount($amount, $decimals = 2) {
18
        return number_format($amount, $decimals, '.', '');
19
    }
20
21
    public static function getAmountInWords($amount, $currency = '') {
22
        $formatedNumber = self::formatAmount($amount);
23
24
        $parts = explode('.', $formatedNumber);
25
        $intPart = (int)$parts[0];
26
        $decimalPart = $parts[1];
27
        $numberTransformer = (new NumberToWords())->getNumberTransformer('es');
28
        $t1 = mb_strtoupper($numberTransformer->toWords($intPart));
29
        $t2 = $t1 . " Y $decimalPart/100";
30
        return $currency ? "$t2 $currency" : $t2;
31
    }
32
33
    /**
34
     * 
35
     * @param float $amount
36
     * @param array $items
37
     * @return float
38
     */
39
    public static function applyAllowancesAndCharges($amount, array $items = []) {
40
        if (!$amount) {
41
            return 0;
42
        }
43
        $totalAllowances = 0;
44
        $totalCharges = 0;
45
        foreach ($items as $item) {
46
            $isCharge = $item['isCharge'];
47
            $k = $item['multiplierFactor'];
48
            $r = $amount * $k;
49
            if ($isCharge) {
50
                $totalCharges += $r;
51
            } else {
52
                $totalAllowances += $r;
53
            }
54
        }
55
        return $amount - $totalAllowances + $totalCharges;
56
    }
57
58
    public static function getTotalAllowanceCharge($amount, array $items, $isCharge) {
59
        $total = 0;
60
        foreach ($items as $item) {
61
            $k = $item['multiplierFactor'];
62
            if ($item['isCharge'] == $isCharge) {
63
                $total += $amount * $k;
64
            }
65
        }
66
        return $total;
67
    }
68
69
    public static function getTotalCharges($amount, array $items) {
70
        return self::getTotalAllowanceCharge($amount, $items, true);
71
    }
72
73
    public static function getTotalAllowances($amount, array $items) {
74
        return self::getTotalAllowanceCharge($amount, $items, false);
75
    }
76
77
    /**
78
     * Calcular IGV
79
     * @param float $baseAmount
80
     * @return float
81
     */
82
    public static function calcIGV($baseAmount) {
83
        return $baseAmount * SunatVars::IGV;
84
    }
85
86
    /**
87
     * Calcular ISC
88
     * @IMP
89
     * @return float
90
     */
91
    public static function calcISC() {
92
        return 0;
93
    }
94
95
    /**
96
     * Calcular IVAP
97
     * @IMP
98
     * @return float
99
     */
100
    public static function calcIVAP() {
101
        return 0;
102
    }
103
104
    /**
105
     * Aplica IGV?
106
     * 
107
     * @param string $igvAffectCode @CAT7
108
     * @return boolean
109
     */
110
    public static function isIGVAffected($igvAffectCode) {
111
        return ($igvAffectCode === Catalogo::CAT7_GRA_IGV);
112
    }
113
114
}
115