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