Skip to content

Commit 6b41fac

Browse files
authored
Invices and Results are typed objects now (#46)
1 parent 0bc3003 commit 6b41fac

25 files changed

+951
-796
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
"global": { "lines": 40, "functions": 29 },
9292
"/src/Models": { "functions": 25 },
9393
"/src/WSAA": { "functions": 0 },
94-
"/src/WSFE": { "functions": 56 },
94+
"/src/WSFE": { "functions": 46 },
9595
"/src/WSMTXCA": { "functions": 0 },
9696
"/src/WSPN3": { "functions": 81 }
9797
}

composer.lock

Lines changed: 369 additions & 354 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/rules/phpstan.src.neon

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,3 @@ parameters:
1414
- '#Access to an undefined property .*\\Wsaa::\$#'
1515
- '#Call to an undefined method .*\\Wsaa::#'
1616
- '#Parameter \#1 ...\$rule of static method Respect\\Validation\\Validator\:\:allOf\(\) expects#'
17-
- '#Cannot call method getMessage\(\) on stdClass.#'

src/AfipInvoices/AfipDetail.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
use Multinexo\AfipValues\IvaConditionCodes;
1414
use Multinexo\AfipValues\UnitSizeCodes;
1515

16+
/**
17+
* @deprecated
18+
*/
1619
class AfipDetail
1720
{
1821
/**

src/AfipInvoices/AfipInvoice.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
use Multinexo\WSMTXCA\AfipResult;
1717
use Multinexo\WSMTXCA\Wsmtxca;
1818

19+
/**
20+
* @deprecated
21+
*/
1922
class AfipInvoice
2023
{
2124
/** @var int */

src/AfipInvoices/AfipInvoiceTranslator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ private function getDataArray(): array
7979
'fechaServicioDesde' => null,
8080
'fechaServicioHasta' => null,
8181
'fechaVencimientoPago' => null,
82-
'arrayComprobantesAsociados' => null,
82+
'comprobantesAsociados' => null,
8383
];
8484
}
8585

src/Exceptions/ManejadorResultados.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,32 @@ public function obtenerObservaciones(stdClass $path, string $name): ?array
3030
}
3131

3232
// Recupera información de errores detectados lanzandolo en una excepción.
33-
public function procesar(stdClass $resultado): void
33+
34+
/**
35+
* @param \SoapFault|stdClass $resultado
36+
*
37+
* @throws WsException
38+
*/
39+
public function procesar($resultado): void
3440
{
41+
$errores = '';
3542
if (isset($resultado->Errors)) {
3643
$errores = reset($resultado->Errors)->Msg;
37-
} else {
38-
//Porque el error viene de otra forma si existe message
39-
if (!property_exists($resultado, 'message')) {
40-
$errores = isset($resultado->arrayErrores) ?
41-
(isset($resultado->arrayErrores->codigoDescripcion) ?
42-
$resultado->arrayErrores->codigoDescripcion->descripcion
43-
: $resultado->arrayErrores)
44-
: null;
45-
} else {
46-
$errores = $resultado->getMessage();
44+
} elseif (isset($resultado->arrayErrores)) {
45+
if (isset($resultado->arrayErrores->codigoDescripcion->descripcion)) {
46+
$resultado->arrayErrores->codigoDescripcion = [$resultado->arrayErrores->codigoDescripcion];
47+
}
48+
foreach ($resultado->arrayErrores->codigoDescripcion as $err) {
49+
$errores .= $err->descripcion . ' (' . $err->codigo . ') ';
4750
}
51+
} elseif ($resultado instanceof \SoapFault) {
52+
$errores = $resultado->getMessage();
4853
}
4954

50-
if (!empty($errores)) {
51-
throw new WsException($errores);
55+
if (empty($errores)) {
56+
return;
5257
}
58+
59+
throw new WsException($errores);
5360
}
5461
}

src/Models/InvoiceInterface.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@
1010

1111
namespace Multinexo\Models;
1212

13+
use Multinexo\Objects\InvoiceResultObject;
1314
use stdClass;
1415

1516
interface InvoiceInterface
1617
{
17-
public function createInvoice(): stdClass;
18+
public function createInvoice(): InvoiceResultObject;
1819

19-
public function getCAEA(): stdClass;
20+
public function getCAEA(stdClass $data): stdClass;
2021

21-
public function requestCAEA(): stdClass;
22+
public function requestCAEA(stdClass $datos): stdClass;
2223

2324
public function getInvoice(): stdClass;
2425
}

src/Models/Invoice.php renamed to src/Models/InvoiceWebService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Multinexo\Exceptions\ManejadorResultados;
1616
use Multinexo\Objects\InvoiceObject;
1717

18-
abstract class Invoice implements InvoiceInterface, AfipWebServiceInterface
18+
abstract class InvoiceWebService implements InvoiceInterface, AfipWebServiceInterface
1919
{
2020
/** @var Authentication */
2121
public $service;

src/Models/Validaciones.php

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
namespace Multinexo\Models;
1212

1313
use Multinexo\Exceptions\ValidationException;
14+
use Multinexo\Objects\InvoiceObject;
15+
use Multinexo\Objects\WebServiceEnum;
1416
use Multinexo\WSMTXCA\Wsmtxca;
1517
use Multinexo\WSMTXCA\WsParametros;
1618
use Respect\Validation\Exceptions\NestedValidationException;
@@ -104,15 +106,13 @@ private function getRulesForElectronicInvoice(): array
104106
{
105107
$wsReglas = [];
106108
$codComprobantes = $this->codComprobantes();
107-
$puntosVenta = $this->getAvailablePosNumbers();
108109
$codDocumento = $this->codDocumento();
109110
$codMonedas = $this->codMonedas();
110111

111112
$reglasFeGenerales = [
112113
'periodo' => v::notEmpty()->date('Ym'),
113114
'orden' => v::notEmpty()->intVal()->between(1, 2)->length(1, 1),
114115
'codigoComprobante' => v::in($codComprobantes),
115-
'puntoVenta' => v::in($puntosVenta), // fe s/item?
116116
'cantidadRegistros' => v::notEmpty()->intVal()->between(1, 9999),
117117
'codigoConcepto' => v::in(['1', '2', '3']),
118118
'codigoDocumento' => v::in($codDocumento),
@@ -125,43 +125,35 @@ private function getRulesForElectronicInvoice(): array
125125
'importeIVA' => v::floatVal()->between(0, 9999999999999.99),
126126
'importeTotal' => v::floatVal()->between(0, 9999999999999.99),
127127
'caea' => v::intVal()->length(14, 14),
128+
'cotizacionMoneda' => v::notEmpty()->between(0, 9999999999.999999),
129+
'numeroComprobante' => v::optional(v::notEmpty()->intVal()->length(1, 8)),
130+
'fechaEmision' => v::optional(v::date('Y-m-d')),
131+
'fechaServicioDesde' => v::optional(v::date('Y-m-d')),
132+
'fechaServicioHasta' => v::optional(v::date('Y-m-d')),
133+
'fechaVencimientoPago' => v::optional(v::date('Y-m-d')),
134+
'comprobantesAsociados' => v::optional(v::arrayType()),
135+
'importeOtrosTributos' => v::optional(v::floatVal()->between(0, 9999999999999.99)),
136+
'arrayOtrosTributos' => v::optional(v::arrayType()),
137+
'arraySubtotalesIVA' => v::optional(v::objectType()),
128138
];
129-
130-
if ($this->ws === 'wsfe') {
131-
$wsReglas = [
132-
'cotizacionMoneda' => v::notEmpty()->between(0, 9999999999.999999),
133-
'numeroComprobante' => v::optional(v::notEmpty()->intVal()->length(1, 8)),
134-
'fechaEmision' => v::optional(v::date('Ymd')),
135-
'fechaServicioDesde' => v::optional(v::date('Ymd')),
136-
'fechaServicioHasta' => v::optional(v::date('Ymd')),
137-
'fechaVencimientoPago' => v::optional(v::date('Ymd')),
138-
'arrayComprobantesAsociados' => v::optional(v::objectType()),
139-
'arrayOtrosTributos' => v::optional(v::objectType()),
140-
'arraySubtotalesIVA' => v::optional(v::objectType()),
139+
if ($this->ws === WebServiceEnum::WSFE) {
140+
$wsReglasPlus = [
141+
'puntoVenta' => v::in($this->getAvailablePosNumbers()),
141142
'arrayOpcionales' => v::optional(v::objectType()),
142-
'importeOtrosTributos' => v::optional(v::floatVal()->between(0, 9999999999999.99)),
143143
];
144-
} elseif ($this->ws === 'wsmtxca') {
145-
$wsReglas = [
146-
'cotizacionMoneda' => v::notEmpty()->between(0, 9999.999999),
147-
'numeroComprobante' => v::optional(v::notEmpty()->intVal()->length(1, 8)),
148-
'fechaEmision' => v::optional(v::date('Y-m-d')),
149-
'fechaServicioDesde' => v::optional(v::date('Y-m-d')),
150-
'fechaServicioHasta' => v::optional(v::date('Y-m-d')),
151-
'fechaVencimientoPago' => v::optional(v::date('Y-m-d')),
144+
} elseif ($this->ws === WebServiceEnum::WSMTXCA) {
145+
$wsReglasPlus = [
146+
// 'puntoVenta' => v::in($this->getAvailablePosNumbers()), not working on testing environment
147+
'puntoVenta' => v::notEmpty()->intVal()->between(1, 9999)->length(1, 4),
152148
'codigoTipoAutorizacion' => v::optional(v::in(['A', 'E'])),
153149
'observaciones' => v::optional(v::stringType()->length(0, 2000)),
154-
'importeOtrosTributos' => v::optional(v::floatVal()->between(0, 9999999999999.99)),
155150
'arrayItems' => v::notEmpty()->objectType(),
156-
'arraySubtotalesIVA' => v::optional(v::objectType()),
157-
'arrayComprobantesAsociados' => v::optional(v::objectType()),
158-
'arrayOtrosTributos' => v::optional(v::objectType()),
159151
'fechaDesde' => v::optional(v::date('Y-m-d')),
160152
'fechaHasta' => v::optional(v::date('Y-m-d')),
161153
];
162154
}
163155

164-
return array_merge($reglasFeGenerales, $wsReglas);
156+
return array_merge($reglasFeGenerales, $wsReglas, $wsReglasPlus ?? []);
165157
}
166158

167159
// Devuelve mensajes de error personalizados.
@@ -194,12 +186,8 @@ private function getErrorMessages(): array
194186
// Valida los datos de un array.
195187
private function validarDatosArray(array $array, string $regla): void
196188
{
197-
if (empty($array)) {
198-
return;
199-
}
200-
201189
// TODO: Los items tienen que ir si o si en el caso de fe wsmtxca
202-
foreach (reset($array) as $dato) {
190+
foreach ($array as $dato) {
203191
$this->validarDatos($dato, $this->getRules($regla));
204192
}
205193
}
@@ -209,30 +197,30 @@ public function validateDataInvoice(): void
209197
{
210198
$this->validarDatos($this->datos, $this->getRules('fe'));
211199

212-
if (property_exists($this->datos, 'arrayOtrosTributos')) {
213-
$this->validarDatosArray((array) $this->datos->arrayOtrosTributos, 'tributos');
214-
}
200+
$this->validarDatosArray($this->datos->arrayOtrosTributos, 'tributos');
215201

216202
/** @phpstan-ignore-next-line */
217203
if (property_exists($this->datos, 'arraySubtotalesIVA')) {
218204
$this->validarDatosArray((array) $this->datos->arraySubtotalesIVA, 'iva');
219205
}
220206

221-
if (property_exists($this->datos, 'arrayComprobantesAsociados')) {
222-
$this->validarDatosArray((array) $this->datos->arrayComprobantesAsociados, 'comprobantesAsociados');
223-
}
207+
$this->validarDatosArray($this->datos->comprobantesAsociados, 'comprobantesAsociados');
224208

225209
if ($this->ws === 'wsfe') {
226210
if (property_exists($this->datos, 'arrayOpcionales')) {
227211
$this->validarDatosArray((array) $this->datos->arrayOpcionales, 'opcionales');
228212
}
229213
} elseif ($this->ws === 'wsmtxca') {
230-
$this->validarDatosArray((array) $this->datos->arrayItems, 'items');
214+
$this->validarDatosArray($this->datos->items, 'items');
231215
}
232216
}
233217

234218
// Valida que los datos ingresados cumplan con determinadas reglas.
235-
public function validarDatos(stdClass $datos, stdClass $reglas): void
219+
220+
/**
221+
* @param InvoiceObject|stdClass $datos
222+
*/
223+
public function validarDatos($datos, stdClass $reglas): void
236224
{
237225
$validaciones = [];
238226

0 commit comments

Comments
 (0)