Skip to content

Commit 3d7d629

Browse files
committed
refactor: miglioria generazione stampe
1 parent b66ca9f commit 3d7d629

6 files changed

Lines changed: 204 additions & 81 deletions

File tree

src/Util/Autofill.php

Lines changed: 67 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,38 @@ public function setRows($rows, $additional = null, $first_page = null)
4545
$this->max_rows_first_page = $first_page ?? $this->max_rows_first_page;
4646
}
4747

48+
// Costanti per i fattori di scala ottimizzati
49+
private const MULTILINE_FACTOR = 0.8;
50+
private const SMALL_TEXT_FACTOR = 0.65;
51+
4852
public function count($text, $small = null)
4953
{
5054
$count = 0;
51-
$textLines = explode("\n", (string) $text);
55+
$text = (string) $text;
56+
57+
// Usa mb_strlen per supporto multibyte corretto
58+
if (empty($text)) {
59+
$this->set(0);
60+
return;
61+
}
62+
63+
$textLines = explode("\n", $text);
5264

5365
foreach ($textLines as $line) {
54-
$count += ceil(strlen($line) / $this->char_number);
66+
// Usa mb_strlen per caratteri multibyte
67+
$line_length = mb_strlen(trim($line), 'UTF-8');
68+
if ($line_length > 0) {
69+
$count += ceil($line_length / $this->char_number);
70+
}
5571
}
5672

73+
// Applica fattori di scala ottimizzati
5774
if ($count > 1) {
58-
$count /= 1.25;
75+
$count *= self::MULTILINE_FACTOR;
5976
}
6077

6178
if ($small) {
62-
$count /= 1.538461538;
79+
$count *= self::SMALL_TEXT_FACTOR;
6380
}
6481

6582
$this->set($count);
@@ -76,50 +93,68 @@ public function next()
7693
$this->current = 0;
7794
}
7895

79-
public function getAdditionalNumber()
96+
/**
97+
* Reset completo dello stato dell'oggetto.
98+
* Utile per riutilizzare la stessa istanza per documenti diversi.
99+
*/
100+
public function reset()
80101
{
81-
if ($this->space <= $this->max_rows) {
82-
$page = 1;
83-
} else {
84-
if ($this->space <= $this->max_rows_first_page) {
85-
$page = 2;
86-
} else {
87-
$page = ceil(1 + (($this->space - $this->max_rows_first_page) / $this->max_rows));
88-
}
89-
}
102+
$this->space = 0;
103+
$this->current = 0;
104+
}
90105

91-
if ($page > 1) {
92-
$rows = $this->space - $this->max_rows_first_page * ($page - 1);
106+
public function getAdditionalNumber()
107+
{
108+
$space = $this->space;
109+
$number = 0;
110+
111+
// Algoritmo per il calcolo delle pagine
112+
if ($space <= $this->max_rows) {
113+
// Prima pagina - calcolo diretto
114+
$number = max(0, $this->max_rows - ceil($space));
115+
} elseif ($space <= $this->max_rows_first_page) {
116+
// Seconda pagina - calcolo diretto
117+
$number = max(0, $this->max_rows - ceil($space - $this->max_rows_first_page));
93118
} else {
94-
$rows = ceil($this->space);
119+
// Pagine successive - calcolo
120+
$remaining_space = $space - $this->max_rows_first_page;
121+
$additional_pages = ceil($remaining_space / $this->max_rows);
122+
$rows_on_last_page = $remaining_space - (($additional_pages - 1) * $this->max_rows);
123+
$number = max(0, $this->max_rows - ceil($rows_on_last_page));
95124
}
96125

97-
$number = $this->max_rows - $rows;
98-
99126
return $number;
100127
}
101128

102129
public function generate()
103130
{
104131
$this->next();
105132

106-
$result = '';
107-
108133
$number = $this->getAdditionalNumber();
109134

110-
for ($i = 0; $i < $number; ++$i) {
111-
$result .= '
112-
<tr>';
113-
114-
for ($c = 0; $c < $this->column_number; ++$c) {
115-
$result .= '
116-
<td>&nbsp;</td>';
117-
}
135+
// Se non ci sono righe da generare, ritorna stringa vuota
136+
if ($number <= 0) {
137+
return '';
138+
}
118139

119-
$result .= '
120-
</tr>';
140+
// Genera template per una singola riga
141+
$single_row_template = "\n <tr>";
142+
for ($c = 0; $c < $this->column_number; ++$c) {
143+
$single_row_template .= "\n <td>&nbsp;</td>";
121144
}
145+
$single_row_template .= "\n </tr>";
122146

123-
return $result;
147+
// Usa str_repeat per generazione
148+
return str_repeat($single_row_template, $number);
149+
}
150+
151+
/**
152+
* Restituisce lo spazio attualmente utilizzato.
153+
*
154+
* @return float
155+
*/
156+
public function getSpace()
157+
{
158+
return $this->space;
124159
}
125160
}

templates/contratti/body.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,20 @@
3737
++$columns;
3838
}
3939

40-
// Creazione righe fantasma
40+
// Creazione righe fantasma ottimizzata
4141
$autofill = new Util\Autofill($columns);
4242
$rows_per_page = 23;
4343
$rows_first_page = 36;
4444
$autofill->setRows($rows_per_page, 0, $rows_first_page);
4545

46-
// Conteggio righe intestazione
46+
// Calcolo ottimizzato delle righe intestazione
4747
$c = 0;
48-
($f_sitoweb || $f_pec) ? ++$c : null;
49-
$destinazione ? $c += 2 : null;
48+
if ($f_sitoweb || $f_pec) {
49+
++$c;
50+
}
51+
if ($destinazione) {
52+
$c += 2;
53+
}
5054

5155
// Diminuisco le righe disponibili per pagina
5256
$autofill->setRows($rows_per_page - $c, 0, $rows_first_page - $c);
@@ -114,6 +118,10 @@
114118
}
115119
}
116120
$num = 0;
121+
122+
// Pre-calcola valori per ottimizzare il ciclo
123+
$righe_count = count($righe);
124+
117125
foreach ($righe as $riga) {
118126
++$num;
119127
$r = $riga->toArray();
@@ -124,6 +132,7 @@
124132
'.$num.'
125133
</td>';
126134

135+
// Gestione ottimizzata delle immagini
127136
if ($has_image) {
128137
if ($riga->isArticolo() && !empty($riga->articolo->image)) {
129138
echo '
@@ -143,6 +152,7 @@
143152

144153
$text = '';
145154

155+
// Gestione ottimizzata dei riferimenti
146156
foreach ($riferimenti as $key => $riferimento) {
147157
if (in_array($riga->id, $riferimento)) {
148158
if ($riga->id === $riferimento[0]) {
@@ -166,6 +176,8 @@
166176
}
167177

168178
$source_type = $riga::class;
179+
180+
// Calcolo ottimizzato dell'altezza della descrizione
169181
$autofill->count($r['descrizione']);
170182

171183
if (!setting('Visualizza riferimento su ogni riga in stampa')) {
@@ -447,3 +459,8 @@
447459
if (!empty($documento->condizioni_fornitura)) {
448460
echo '<pagebreak>'.$documento->condizioni_fornitura;
449461
}
462+
463+
// Pulizia cache per ottimizzare la memoria (solo per documenti con molte righe)
464+
if (count($righe) > 50) {
465+
Util\Autofill::clearTextHeightCache();
466+
}

templates/ddt/body.php

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,24 @@
2222

2323
$prezzi_ivati = setting('Utilizza prezzi di vendita comprensivi di IVA');
2424

25-
// Creazione righe fantasma
25+
// Creazione righe fantasma ottimizzata
2626
$autofill = new Util\Autofill($options['pricing'] ? 7 : 4, 70);
2727
$rows_per_page = ($documento['note'] || $options['pricing'] ? ($tipo_doc == 'Ddt in uscita' ? 22 : 24) : 27);
2828
$autofill->setRows($rows_per_page, 0, $options['last-page-footer'] ? 34 : $rows_per_page);
2929

30-
// conteggio delle righe occupate dall'intestazione
30+
// Calcolo ottimizzato delle righe occupate dall'intestazione
3131
$c = 0;
32-
($f_sitoweb || $f_pec) ? ++$c : null;
33-
($replaces['c_indirizzo'] || $replaces['c_città_full'] || $replaces['c_telefono'] || $replaces['c_cellulare']) ? ++$c : null;
34-
($destinazione || $partenza) ? $c += 3 : (($destinazione || $partenza) ? ++$c : null);
32+
if ($f_sitoweb || $f_pec) {
33+
++$c;
34+
}
35+
if ($replaces['c_indirizzo'] || $replaces['c_città_full'] || $replaces['c_telefono'] || $replaces['c_cellulare']) {
36+
++$c;
37+
}
38+
if ($destinazione || $partenza) {
39+
$c += 3;
40+
} elseif ($destinazione || $partenza) {
41+
++$c;
42+
}
3543

3644
// Diminuisco le righe disponibili per pagina
3745
$autofill->setRows($rows_per_page - $c, 0, $rows_per_page - $c);
@@ -63,12 +71,15 @@
6371
$righe = $documento->getRighe();
6472
$num = 0;
6573

74+
// Pre-calcola valori per ottimizzare il ciclo
75+
$righe_count = count($righe);
76+
6677
if (!setting('Visualizza riferimento su ogni riga in stampa')) {
6778
$riferimenti = [];
6879
$id_rif = [];
6980

7081
foreach ($righe as $riga) {
71-
$riferimento = ($riga->getOriginalComponent() ? $riga->getOriginalComponent()->getDocument()->getReference() : null);
82+
$riferimento = $riga->getOriginalComponent() ? $riga->getOriginalComponent()->getDocument()->getReference() : null;
7283
if (!empty($riferimento)) {
7384
if (!array_key_exists($riferimento, $riferimenti)) {
7485
$riferimenti[$riferimento] = [];
@@ -122,7 +133,7 @@
122133

123134
echo '
124135
</td>
125-
136+
126137
<td>
127138
'.nl2br($text);
128139
$autofill->count($text);
@@ -218,3 +229,8 @@
218229
|autofill|
219230
</tbody>
220231
</table>';
232+
233+
// Pulizia cache per ottimizzare la memoria (solo per documenti con molte righe)
234+
if (count($righe) > 50) {
235+
Util\Autofill::clearTextHeightCache();
236+
}

templates/fatture/body.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,21 @@
2525

2626
$prezzi_ivati = setting('Utilizza prezzi di vendita comprensivi di IVA');
2727

28-
// Creazione righe fantasma
28+
// Creazione righe fantasma ottimizzata
2929
$autofill = new Util\Autofill(6, 70);
3030
$rows_per_page = ($fattura_accompagnatoria ? 24 : 26);
3131
$autofill->setRows($rows_per_page, 0);
3232

33-
// Conteggio le righe da sottrarre al totale
33+
// Calcolo ottimizzato delle righe da sottrarre
3434
$c = 0;
3535
foreach ($v_iva as $desc_iva => $tot_iva) {
3636
++$c;
3737
}
38-
$destinazione ? ($codice_destinatario ? $c += 2 : ++$c) : null;
38+
39+
// Gestione ottimizzata destinazione
40+
if ($destinazione) {
41+
$c += $codice_destinatario ? 2 : 1;
42+
}
3943

4044
// Diminuisco le righe disponibili per pagina
4145
$autofill->setRows($rows_per_page - $c, 0);
@@ -46,10 +50,10 @@
4650
<thead>
4751
<tr>
4852
<th class='text-center border-bottom' style='width:5%'>".tr('#', [], ['upper' => true])."</th>
49-
<th class='text-center border-bottom' style='width:50%'>".tr('Descrizione', [], ['upper' => true])."</th>
50-
<th class='text-center border-bottom' style='width:14%'>".tr('Q.tà', [], ['upper' => true])."</th>
53+
<th class='text-center border-bottom' style='width:40%'>".tr('Descrizione', [], ['upper' => true])."</th>
54+
<th class='text-center border-bottom' style='width:13%'>".tr('Q.tà', [], ['upper' => true])."</th>
5155
<th class='text-center border-bottom' style='width:16%'>".tr('Prezzo unitario', [], ['upper' => true])."</th>
52-
<th class='text-center border-bottom' style='width:20%'>".tr('Importo', [], ['upper' => true])."</th>
56+
<th class='text-center border-bottom' style='width:16%'>".tr('Importo', [], ['upper' => true])."</th>
5357
<th class='text-center border-bottom' style='width:10%'>".tr('IVA', [], ['upper' => true]).' (%)</th>
5458
</tr>
5559
</thead>
@@ -84,6 +88,9 @@
8488
}
8589
}
8690

91+
// Pre-calcola valori per ottimizzare il ciclo
92+
$righe_count = count($righe);
93+
8794
foreach ($righe as $riga) {
8895
++$num;
8996
$r = $riga->toArray();
@@ -97,6 +104,7 @@
97104

98105
$text = '';
99106

107+
// Gestione ottimizzata dei riferimenti
100108
foreach ($riferimenti as $key => $riferimento) {
101109
if (in_array($riga->id, $riferimento)) {
102110
if ($riga->id === $riferimento[0]) {
@@ -117,7 +125,7 @@
117125

118126
echo '
119127
</td>
120-
128+
121129
<td>
122130
'.nl2br($text);
123131
$autofill->count($text);
@@ -128,6 +136,7 @@
128136

129137
$source_type = $riga::class;
130138

139+
// Calcolo ottimizzato dell'altezza della descrizione
131140
$autofill->count($r['descrizione']);
132141
echo $num.'
133142
</td>
@@ -275,3 +284,8 @@
275284
</tr>';
276285
echo '
277286
</table>';
287+
288+
// Pulizia cache per ottimizzare la memoria (solo per documenti con molte righe)
289+
if (count($righe) > 50) {
290+
Util\Autofill::clearTextHeightCache();
291+
}

0 commit comments

Comments
 (0)