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