|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace PhpCfdi\SatCatalogos\CFDI40; |
|
6
|
|
|
|
|
7
|
|
|
use PhpCfdi\SatCatalogos\Common\BaseCatalog; |
|
8
|
|
|
use PhpCfdi\SatCatalogos\Common\BaseCatalogTrait; |
|
9
|
|
|
use PhpCfdi\SatCatalogos\Exceptions\SatCatalogosLogicException; |
|
10
|
|
|
use PhpCfdi\SatCatalogos\Repository; |
|
11
|
|
|
|
|
12
|
|
|
class ReglasTasaCuota implements BaseCatalog |
|
13
|
|
|
{ |
|
14
|
|
|
use BaseCatalogTrait; |
|
15
|
|
|
|
|
16
|
|
|
public const FACTOR_TASA = 'Tasa'; |
|
17
|
|
|
|
|
18
|
|
|
public const FACTOR_CUOTA = 'Cuota'; |
|
19
|
|
|
|
|
20
|
|
|
public const IMPUESTO_IEPS = 'IEPS'; |
|
21
|
|
|
|
|
22
|
|
|
public const IMPUESTO_IVA = 'IVA'; |
|
23
|
|
|
|
|
24
|
|
|
public const IMPUESTO_ISR = 'ISR'; |
|
25
|
|
|
|
|
26
|
|
|
public const USO_TRASLADO = 'traslado'; |
|
27
|
|
|
|
|
28
|
|
|
public const USO_RETENCION = 'retencion'; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param string $impuesto |
|
32
|
|
|
* @param string $factor |
|
33
|
|
|
* @param string $uso |
|
34
|
|
|
* @return ReglaTasaCuota[] |
|
35
|
|
|
*/ |
|
36
|
5 |
|
public function obtainRules(string $impuesto, string $factor, string $uso): array |
|
37
|
|
|
{ |
|
38
|
5 |
|
if (self::USO_TRASLADO !== $uso && self::USO_RETENCION !== $uso) { |
|
39
|
1 |
|
throw new SatCatalogosLogicException('El campo uso no tiene uno de los valores permitidos'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$filters = [ |
|
43
|
|
|
'impuesto' => $impuesto, |
|
44
|
|
|
'factor' => $factor, |
|
45
|
|
|
$uso => true, |
|
46
|
|
|
]; |
|
47
|
|
|
|
|
48
|
4 |
|
return array_map( |
|
49
|
4 |
|
function (array $data): ReglaTasaCuota { |
|
50
|
4 |
|
return $this->createRule($data); |
|
51
|
|
|
}, |
|
52
|
4 |
|
$this->repository()->queryRowsByFields(Repository::CFDI_40_REGLAS_TASA_CUOTA, $filters) |
|
53
|
|
|
); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
3 |
|
public function findMatchingRule(string $impuesto, string $factor, string $uso, string $valor): ?ReglaTasaCuota |
|
57
|
|
|
{ |
|
58
|
3 |
|
$rules = $this->obtainRules($impuesto, $factor, $uso); |
|
59
|
|
|
|
|
60
|
3 |
|
foreach ($rules as $rule) { |
|
61
|
3 |
|
if ($rule->valorIsValid($valor)) { |
|
62
|
3 |
|
return $rule; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
3 |
|
return null; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
2 |
|
public function hasMatchingRule(string $impuesto, string $factor, string $uso, string $valor): bool |
|
70
|
|
|
{ |
|
71
|
2 |
|
return (null !== $this->findMatchingRule($impuesto, $factor, $uso, $valor)); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Create a ReglaTasaCuota based on the array values |
|
76
|
|
|
* |
|
77
|
|
|
* @param array<string, mixed> $data |
|
78
|
|
|
* @return ReglaTasaCuota |
|
79
|
|
|
*/ |
|
80
|
4 |
|
public function createRule(array $data): ReglaTasaCuota |
|
81
|
|
|
{ |
|
82
|
4 |
|
return new ReglaTasaCuota( |
|
83
|
4 |
|
$data['tipo'], |
|
84
|
4 |
|
$data['impuesto'], |
|
85
|
4 |
|
$data['factor'], |
|
86
|
4 |
|
(bool) $data['traslado'], |
|
87
|
4 |
|
(bool) $data['retencion'], |
|
88
|
4 |
|
$data['minimo'], |
|
89
|
4 |
|
$data['valor'], |
|
90
|
4 |
|
($data['vigencia_desde']) ? strtotime($data['vigencia_desde']) : 0, |
|
91
|
4 |
|
($data['vigencia_hasta']) ? strtotime($data['vigencia_hasta']) : 0 |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|