Skip to content

Commit 29048db

Browse files
author
Carlos Garcia
committed
Fix provincia normalization and null-safe numeric formatting
1 parent 9bfcce3 commit 29048db

3 files changed

Lines changed: 388 additions & 19 deletions

File tree

Lib/Txt347Export.php

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* This file is part of Modelo347 plugin for FacturaScripts
4-
* Copyright (C) 2020-2025 Carlos Garcia Gomez <carlos@facturascripts.com>
4+
* Copyright (C) 2020-2026 Carlos Garcia Gomez <carlos@facturascripts.com>
55
*
66
* This program is free software: you can redistribute it and/or modify
77
* it under the terms of the GNU Lesser General Public License as
@@ -23,7 +23,6 @@
2323
use FacturaScripts\Core\Tools;
2424
use FacturaScripts\Dinamic\Model\Ejercicio;
2525
use FacturaScripts\Dinamic\Model\Empresa;
26-
use FacturaScripts\Dinamic\Model\Pais;
2726

2827
class Txt347Export
2928
{
@@ -44,6 +43,7 @@ class Txt347Export
4443

4544
public static function export(string $codejercicio, array $customersData, array $suppliersData): string
4645
{
46+
self::$total = 0.0;
4747
self::$customersData = $customersData;
4848
self::$suppliersData = $suppliersData;
4949
self::loadExercise($codejercicio);
@@ -87,7 +87,7 @@ protected static function getNifOperadorComunitario(array $item): string
8787
return self::formatString('', 17, ' ', STR_PAD_RIGHT);
8888
}
8989

90-
protected static function formatAmount(float $amount, int $length, int $align): string
90+
public static function formatAmount(float $amount, int $length, int $align): string
9191
{
9292
$signed = ($amount < 0.00) ? 'N' : ' ';
9393
// forzamos al formato de 2 decimales sin signo y sin separador de decimales
@@ -96,19 +96,23 @@ protected static function formatAmount(float $amount, int $length, int $align):
9696
return $signed . self::formatString($amount, $length - 1, '0', $align);
9797
}
9898

99-
protected static function formatOnlyNumber(string $number): string
99+
public static function formatOnlyNumber(?string $number): string
100100
{
101+
if (empty($number)) {
102+
return '';
103+
}
104+
101105
// eliminamos cualquier carácter que no sea un número
102106
return preg_replace('/[^0-9]/', '', $number);
103107
}
104108

105-
protected static function formatString(string $string, int $length, string $charter, int $align): string
109+
public static function formatString(string $string, int $length, string $charter, int $align): string
106110
{
107111
// eliminamos los acentos y caracteres especiales
108112
$string = self::sanitize($string);
109113

110114
// pasamos el string a mayúsculas
111-
$string = strtoupper($string);
115+
$string = mb_strtoupper($string, 'UTF-8');
112116

113117
// limitamos el tamaño del string
114118
$string = self::limitString($string, $length);
@@ -137,7 +141,7 @@ protected static function getCompanyData(): string
137141
. self::formatString('', 1, ' ', STR_PAD_LEFT)
138142
. self::formatString('', 1, ' ', STR_PAD_LEFT) // DECLARACIÓN COMPLEMENTARIA O SUSTITUTIVA
139143
. self::formatString('', 13, '0', STR_PAD_RIGHT) // NÚMERO IDENTIFICATIVO DE LA DECLARACIÓN ANTERIOR
140-
. self::formatString(count(self::$customersData) + count(self::$suppliersData), 9, '0', STR_PAD_LEFT) // NÚMERO TOTAL DE PERSONAS Y ENTIDADES
144+
. self::formatString((string)(count(self::$customersData) + count(self::$suppliersData)), 9, '0', STR_PAD_LEFT) // NÚMERO TOTAL DE PERSONAS Y ENTIDADES
141145
. self::formatAmount(self::$total, 16, STR_PAD_LEFT) // IMPORTE TOTAL ANUAL DE LAS OPERACIONES
142146
. self::formatString('', 9, '0', STR_PAD_RIGHT) // NÚMERO TOTAL DE INMUEBLES
143147
. self::formatAmount(0.00, 16, STR_PAD_LEFT) // IMPORTE TOTAL ANUAL DE LAS OPERACIONES DE ARRENDAMIENTO DE LOCALES DE NEGOCIO
@@ -190,11 +194,6 @@ protected static function getCustomerData(): string
190194
return $txt;
191195
}
192196

193-
protected static function getDecimal($number): int
194-
{
195-
return ((float)$number - (int)$number) * 100;
196-
}
197-
198197
protected static function loadExercise(string $codejercicio): void
199198
{
200199
self::$exercise = new Ejercicio();
@@ -203,17 +202,24 @@ protected static function loadExercise(string $codejercicio): void
203202

204203
protected static function getPais(string $codpais): string
205204
{
206-
$paisModel = new Pais();
207-
if ($paisModel->load($codpais) && $paisModel->codiso !== 'ES') {
208-
return self::formatString($paisModel->codiso, 2, '', STR_PAD_LEFT);
205+
$pais = Paises::get($codpais);
206+
if (!empty($pais->codiso) && strtoupper($pais->codiso) !== 'ES') {
207+
return self::formatString($pais->codiso, 2, ' ', STR_PAD_LEFT);
209208
}
210209

211210
return self::formatString('', 2, ' ', STR_PAD_LEFT);
212211
}
213212

214-
protected static function getProvincia(?string $provincia): string
213+
public static function getProvincia(?string $provincia): string
215214
{
216-
switch (strtolower($provincia)) {
215+
if (empty($provincia)) {
216+
return '99';
217+
}
218+
219+
$provincia = trim(self::sanitize($provincia));
220+
$provincia = mb_strtolower($provincia, 'UTF-8');
221+
222+
switch ($provincia) {
217223
case 'araba':
218224
case 'alava':
219225
case 'álava':
@@ -444,7 +450,7 @@ protected static function limitString(string $string, int $length): string
444450
return substr($string, 0, $length);
445451
}
446452

447-
protected static function sanitize(?string $txt): string
453+
public static function sanitize(?string $txt): string
448454
{
449455
$changes = ['/à/' => 'a', '/á/' => 'a', '/â/' => 'a', '/ã/' => 'a', '/ä/' => 'a',
450456
'/å/' => 'a', '/æ/' => 'ae', '/ç/' => 'c', '/è/' => 'e', '/é/' => 'e', '/ê/' => 'e',

0 commit comments

Comments
 (0)