1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CfdiUtils\Elements\Cfdi33\Helpers; |
4
|
|
|
|
5
|
|
|
use CfdiUtils\Elements\Cfdi33\Comprobante; |
6
|
|
|
use CfdiUtils\SumasConceptos\SumasConceptos; |
7
|
|
|
|
8
|
|
|
class SumasConceptosWriter |
9
|
|
|
{ |
10
|
|
|
/** @var Comprobante */ |
11
|
|
|
private $comprobante; |
12
|
|
|
|
13
|
|
|
/** @var \CfdiUtils\SumasConceptos\SumasConceptos */ |
14
|
|
|
private $sumas; |
15
|
|
|
|
16
|
|
|
/** @var int */ |
17
|
|
|
private $precision; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Writer constructor. |
21
|
|
|
* @param Comprobante $comprobante |
22
|
|
|
* @param \CfdiUtils\SumasConceptos\SumasConceptos $sumas |
23
|
|
|
* @param int $precision |
24
|
|
|
*/ |
25
|
15 |
|
public function __construct(Comprobante $comprobante, SumasConceptos $sumas, int $precision = 6) |
26
|
|
|
{ |
27
|
15 |
|
$this->comprobante = $comprobante; |
28
|
15 |
|
$this->sumas = $sumas; |
29
|
15 |
|
$this->precision = $precision; |
30
|
15 |
|
} |
31
|
|
|
|
32
|
13 |
|
public function put() |
33
|
|
|
{ |
34
|
13 |
|
$this->putComprobanteSumas(); |
35
|
13 |
|
$this->putImpuestosNode(); |
36
|
13 |
|
$this->putComplementoImpuestoLocalSumas(); |
37
|
13 |
|
} |
38
|
|
|
|
39
|
13 |
|
private function putComprobanteSumas() |
40
|
|
|
{ |
41
|
13 |
|
$this->comprobante['SubTotal'] = $this->format($this->sumas->getSubTotal()); |
42
|
13 |
|
$this->comprobante['Total'] = $this->format($this->sumas->getTotal()); |
43
|
13 |
|
$this->comprobante['Descuento'] = $this->format($this->sumas->getDescuento()); |
44
|
13 |
|
if (! $this->sumas->foundAnyConceptWithDiscount() |
45
|
13 |
|
&& ! $this->valueGreaterThanZero($this->sumas->getDescuento())) { |
46
|
8 |
|
unset($this->comprobante['Descuento']); |
47
|
|
|
} |
48
|
13 |
|
} |
49
|
|
|
|
50
|
13 |
|
private function putImpuestosNode() |
51
|
|
|
{ |
52
|
|
|
// obtain node reference |
53
|
13 |
|
$impuestos = $this->comprobante->getImpuestos(); |
54
|
|
|
// if there is nothing to write then remove the children and exit |
55
|
13 |
|
if (! $this->sumas->hasTraslados() && ! $this->sumas->hasRetenciones()) { |
56
|
7 |
|
$this->comprobante->children()->remove($impuestos); |
57
|
7 |
|
return; |
58
|
|
|
} |
59
|
|
|
// clear previous values |
60
|
6 |
|
$impuestos->clear(); |
61
|
|
|
// add traslados when needed |
62
|
6 |
|
if ($this->sumas->hasTraslados()) { |
63
|
5 |
|
$impuestos['TotalImpuestosTrasladados'] = $this->format($this->sumas->getImpuestosTrasladados()); |
64
|
5 |
|
$impuestos->getTraslados()->multiTraslado( |
65
|
5 |
|
...$this->getImpuestosContents($this->sumas->getTraslados()) |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
// add retenciones when needed |
69
|
6 |
|
if ($this->sumas->hasRetenciones()) { |
70
|
1 |
|
$impuestos['TotalImpuestosRetenidos'] = $this->format($this->sumas->getImpuestosRetenidos()); |
71
|
1 |
|
$impuestos->getRetenciones()->multiRetencion( |
72
|
1 |
|
...$this->getImpuestosContents($this->sumas->getRetenciones()) |
73
|
|
|
); |
74
|
|
|
} |
75
|
6 |
|
} |
76
|
|
|
|
77
|
13 |
|
private function putComplementoImpuestoLocalSumas() |
78
|
|
|
{ |
79
|
|
|
// search for implocal node |
80
|
13 |
|
$impLocal = $this->comprobante->searchNode('cfdi:Complemento', 'implocal:ImpuestosLocales'); |
81
|
13 |
|
if (! $impLocal) { |
82
|
10 |
|
return; |
83
|
|
|
} |
84
|
3 |
|
if (! $this->sumas->hasLocalesTraslados() && ! $this->sumas->hasLocalesRetenciones()) { |
85
|
2 |
|
$complemento = $this->comprobante->getComplemento(); |
86
|
2 |
|
$complemento->children()->remove($impLocal); |
87
|
2 |
|
if (0 === $complemento->count()) { |
88
|
1 |
|
$this->comprobante->children()->remove($complemento); |
89
|
|
|
} |
90
|
2 |
|
return; |
91
|
|
|
} |
92
|
1 |
|
$impLocal->attributes()->set('TotaldeRetenciones', $this->format($this->sumas->getLocalesImpuestosRetenidos())); |
93
|
1 |
|
$impLocal->attributes()->set('TotaldeTraslados', $this->format($this->sumas->getLocalesImpuestosTrasladados())); |
94
|
1 |
|
} |
95
|
|
|
|
96
|
6 |
|
private function getImpuestosContents(array $impuestos): array |
97
|
|
|
{ |
98
|
6 |
|
$return = []; |
99
|
6 |
|
foreach ($impuestos as $impuesto) { |
100
|
6 |
|
$impuesto['Importe'] = $this->format($impuesto['Importe']); |
101
|
6 |
|
$return[] = $impuesto; |
102
|
|
|
} |
103
|
6 |
|
return $return; |
104
|
|
|
} |
105
|
|
|
|
106
|
8 |
|
private function valueGreaterThanZero(float $value) |
107
|
|
|
{ |
108
|
8 |
|
return (round($value, $this->precision) > 0); |
109
|
|
|
} |
110
|
|
|
|
111
|
14 |
|
public function format(float $number): string |
112
|
|
|
{ |
113
|
14 |
|
return number_format($number, $this->precision, '.', ''); |
114
|
|
|
} |
115
|
|
|
|
116
|
1 |
|
public function getComprobante(): Comprobante |
117
|
|
|
{ |
118
|
1 |
|
return $this->comprobante; |
119
|
|
|
} |
120
|
|
|
|
121
|
1 |
|
public function getSumasConceptos(): SumasConceptos |
122
|
|
|
{ |
123
|
1 |
|
return $this->sumas; |
124
|
|
|
} |
125
|
|
|
|
126
|
1 |
|
public function getPrecision(): int |
127
|
|
|
{ |
128
|
1 |
|
return $this->precision; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|