Skip to content

Commit 5dd26f0

Browse files
committed
feat: #1049 Modifica aliquota IVA righe da bulk
1 parent d2fd9a0 commit 5dd26f0

18 files changed

Lines changed: 1901 additions & 47 deletions

File tree

modules/contratti/actions.php

Lines changed: 78 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -366,35 +366,35 @@
366366
// eliminazione contratto
367367
case 'delete':
368368
// Fatture o interventi collegati a questo contratto
369-
$elementi = $dbo->fetchArray('SELECT
370-
0 AS `codice`,
371-
`co_documenti`.`id` AS `id`,
372-
`co_documenti`.`numero` AS `numero`,
373-
`co_documenti`.`numero_esterno` AS `numero_esterno`,
374-
`co_documenti`.`data`,
375-
`co_tipidocumento_lang`.`title` AS `tipo_documento`,
376-
`co_tipidocumento`.`dir` AS `dir`
377-
FROM
378-
`co_documenti`
379-
INNER JOIN `co_tipidocumento` ON `co_tipidocumento`.`id` = `co_documenti`.`idtipodocumento`
380-
LEFT JOIN `co_tipidocumento_lang` ON (`co_tipidocumento_lang`.`id_record` = `co_tipidocumento`.`id` AND `co_tipidocumento_lang`.`id_lang` = '.prepare(Models\Locale::getDefault()->id).')
381-
INNER JOIN co_righe_documenti ON `co_righe_documenti`.`iddocumento` = `co_documenti`.`id`
382-
WHERE
369+
$elementi = $dbo->fetchArray('SELECT
370+
0 AS `codice`,
371+
`co_documenti`.`id` AS `id`,
372+
`co_documenti`.`numero` AS `numero`,
373+
`co_documenti`.`numero_esterno` AS `numero_esterno`,
374+
`co_documenti`.`data`,
375+
`co_tipidocumento_lang`.`title` AS `tipo_documento`,
376+
`co_tipidocumento`.`dir` AS `dir`
377+
FROM
378+
`co_documenti`
379+
INNER JOIN `co_tipidocumento` ON `co_tipidocumento`.`id` = `co_documenti`.`idtipodocumento`
380+
LEFT JOIN `co_tipidocumento_lang` ON (`co_tipidocumento_lang`.`id_record` = `co_tipidocumento`.`id` AND `co_tipidocumento_lang`.`id_lang` = '.prepare(Models\Locale::getDefault()->id).')
381+
INNER JOIN co_righe_documenti ON `co_righe_documenti`.`iddocumento` = `co_documenti`.`id`
382+
WHERE
383383
`co_righe_documenti`.`idcontratto` = '.prepare($id_record).'
384384
UNION
385-
SELECT
386-
`in_interventi`.`codice` AS `codice`,
387-
`in_interventi`.`id` AS `id`,
388-
0 AS `numero`,
389-
0 AS `numero_esterno`,
390-
`in_interventi`.`data_richiesta` AS `data`,
391-
0 AS `tipo_documento`,
392-
0 AS `dir`
393-
FROM
394-
`in_interventi`
395-
WHERE
396-
`in_interventi`.`id_contratto` = '.prepare($id_record).'
397-
ORDER BY
385+
SELECT
386+
`in_interventi`.`codice` AS `codice`,
387+
`in_interventi`.`id` AS `id`,
388+
0 AS `numero`,
389+
0 AS `numero_esterno`,
390+
`in_interventi`.`data_richiesta` AS `data`,
391+
0 AS `tipo_documento`,
392+
0 AS `dir`
393+
FROM
394+
`in_interventi`
395+
WHERE
396+
`in_interventi`.`id_contratto` = '.prepare($id_record).'
397+
ORDER BY
398398
`data` ');
399399

400400
if (empty($elementi)) {
@@ -783,5 +783,56 @@
783783
$serials = (array) post('serial');
784784
$articolo->serials = $serials;
785785

786+
break;
787+
788+
case 'update_iva':
789+
$id_riga = post('riga_id');
790+
$id_iva = post('iva_id');
791+
792+
$riga = $riga ?: Riga::find($id_riga);
793+
$riga = $riga ?: Articolo::find($id_riga);
794+
$riga = $riga ?: Sconto::find($id_riga);
795+
796+
if (!empty($riga)) {
797+
// Aggiorna l'IVA mantenendo lo stesso prezzo unitario
798+
$prezzo_unitario = $riga->prezzo_unitario;
799+
$riga->setPrezzoUnitario($prezzo_unitario, $id_iva);
800+
$riga->save();
801+
802+
flash()->info(tr('IVA aggiornata!'));
803+
}
804+
805+
break;
806+
807+
case 'update_iva_multiple':
808+
$id_righe = (array) post('righe');
809+
$id_iva = post('iva_id');
810+
$numero_totale = 0;
811+
812+
foreach ($id_righe as $id_riga) {
813+
$riga = Articolo::find($id_riga) ?: Riga::find($id_riga);
814+
$riga = $riga ?: Sconto::find($id_riga);
815+
816+
if (!empty($riga)) {
817+
// Aggiorna l'IVA mantenendo lo stesso prezzo unitario
818+
$prezzo_unitario = $riga->prezzo_unitario;
819+
$riga->setPrezzoUnitario($prezzo_unitario, $id_iva);
820+
$riga->save();
821+
++$numero_totale;
822+
}
823+
}
824+
825+
if ($numero_totale > 1) {
826+
flash()->info(tr('_NUM_ aliquote IVA modificate!', [
827+
'_NUM_' => $numero_totale,
828+
]));
829+
} elseif ($numero_totale == 1) {
830+
flash()->info(tr('_NUM_ aliquota IVA modificata!', [
831+
'_NUM_' => $numero_totale,
832+
]));
833+
} else {
834+
flash()->warning(tr('Nessuna aliquota IVA modificata!'));
835+
}
836+
786837
break;
787838
}
Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
<?php
2+
/*
3+
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
4+
* Copyright (C) DevCode s.r.l.
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
include_once __DIR__.'/../../../core.php';
21+
22+
use Modules\Contratti\Components\Articolo;
23+
use Modules\Contratti\Components\Riga;
24+
use Modules\Contratti\Components\Sconto;
25+
26+
// Recupero le righe selezionate
27+
$riga_id = get('riga_id');
28+
$righe_ids = $_GET['righe'];
29+
30+
// Array per memorizzare le aliquote IVA distinte
31+
$aliquote_iva = [];
32+
// Array per memorizzare le righe senza aliquota IVA o con aliquota non valida
33+
$righe_senza_iva = [];
34+
// Contatore per le righe totali
35+
$righe_totali = 0;
36+
37+
if (!empty($riga_id)) {
38+
// Caso singola riga
39+
$riga = Riga::find($riga_id) ?: Articolo::find($riga_id);
40+
$riga = $riga ?: Sconto::find($riga_id);
41+
$righe_totali++;
42+
43+
if (!empty($riga) && !empty($riga->idiva) && !empty($riga->aliquota)) {
44+
$aliquote_iva[$riga->idiva] = [
45+
'id' => $riga->idiva,
46+
'codice' => $riga->aliquota->codice,
47+
'descrizione' => $riga->aliquota->getTranslation('title'),
48+
'percentuale' => $riga->aliquota->percentuale,
49+
'count' => 1
50+
];
51+
} else {
52+
// Riga senza aliquota IVA o con aliquota non valida
53+
$righe_senza_iva[] = [
54+
'id' => $riga->id,
55+
'descrizione' => $riga->descrizione,
56+
'idiva' => $riga->idiva ?: 'N/D'
57+
];
58+
}
59+
} elseif (!empty($righe_ids)) {
60+
// Caso multiple righe
61+
$righe_array = explode(',', $righe_ids);
62+
$righe_totali = count($righe_array);
63+
64+
foreach ($righe_array as $id_riga) {
65+
$riga = Riga::find($id_riga) ?: Articolo::find($id_riga);
66+
$riga = $riga ?: Sconto::find($id_riga);
67+
68+
if (!empty($riga) && !empty($riga->idiva) && !empty($riga->aliquota)) {
69+
if (!isset($aliquote_iva[$riga->idiva])) {
70+
$aliquote_iva[$riga->idiva] = [
71+
'id' => $riga->idiva,
72+
'codice' => $riga->aliquota->codice,
73+
'descrizione' => $riga->aliquota->getTranslation('title'),
74+
'percentuale' => $riga->aliquota->percentuale,
75+
'count' => 0
76+
];
77+
}
78+
$aliquote_iva[$riga->idiva]['count']++;
79+
} elseif (!empty($riga)) {
80+
// Riga senza aliquota IVA o con aliquota non valida
81+
$righe_senza_iva[] = [
82+
'id' => $riga->id,
83+
'descrizione' => $riga->descrizione,
84+
'idiva' => $riga->idiva ?: 'N/D'
85+
];
86+
}
87+
}
88+
}
89+
90+
// Se non ci sono aliquote IVA, ma ci sono righe selezionate, mostriamo comunque il form
91+
$show_form = count($aliquote_iva) > 0 || count($righe_senza_iva) > 0 || (!empty($righe_ids) && !empty($righe_array));
92+
?>
93+
94+
<form id="modifica-iva-form">
95+
<?php if (count($aliquote_iva) > 0): ?>
96+
<div class="row">
97+
<div class="col-md-5">
98+
<div class="alert alert-info">
99+
<p><strong><?= tr('Aliquote IVA attuali') ?></strong></p>
100+
<ul class="list-unstyled">
101+
<?php foreach ($aliquote_iva as $aliquota): ?>
102+
<li>
103+
<strong><?= $aliquota['codice'] ?></strong> - <?= $aliquota['descrizione'] ?> (<?= $aliquota['percentuale'] ?>%)
104+
<?php if (count($aliquote_iva) > 1): ?>
105+
<span class="badge"><?= $aliquota['count'] ?> <?= tr('righe') ?></span>
106+
<?php endif; ?>
107+
</li>
108+
<?php endforeach; ?>
109+
</ul>
110+
</div>
111+
</div>
112+
113+
<div class="col-md-2 text-center" style="padding-top: 30px;">
114+
<i class="fa fa-arrow-right fa-3x text-muted"></i>
115+
</div>
116+
117+
<div class="col-md-5">
118+
<div class="panel panel-success">
119+
<div class="panel-heading">
120+
<h4 class="panel-title"><?= tr('Aliquota da applicare') ?></h4>
121+
</div>
122+
<div class="panel-body">
123+
{[ "type": "select", "label": "", "name": "iva_id", "required": 1, "values": "query=SELECT `co_iva`.`id`, IF(`deleted_at` IS NOT NULL, CONCAT(`codice`, ' - ', `title`, ' (<?= tr('eliminata') ?>)'), CONCAT(`codice`, ' - ', `title`)) AS `descrizione` FROM `co_iva` LEFT JOIN `co_iva_lang` ON (`co_iva`.`id` = `co_iva_lang`.`id_record` AND `co_iva_lang`.`id_lang` = <?= prepare(Models\Locale::getDefault()->id) ?>) ORDER BY `title` ASC" ]}
124+
<input type="hidden" name="riga_id" value="<?= get('riga_id') ?>">
125+
<input type="hidden" name="righe" value="<?= $_GET['righe'] ?>">
126+
</div>
127+
</div>
128+
</div>
129+
</div>
130+
<?php elseif ($show_form): ?>
131+
<div class="row">
132+
<div class="col-md-5">
133+
<div class="alert alert-info">
134+
<p><strong><?= tr('Aliquote IVA attuali') ?></strong></p>
135+
136+
<?php if (count($aliquote_iva) > 0): ?>
137+
<ul class="list-unstyled">
138+
<?php foreach ($aliquote_iva as $aliquota): ?>
139+
<li>
140+
<strong><?= $aliquota['codice'] ?></strong> - <?= $aliquota['descrizione'] ?> (<?= $aliquota['percentuale'] ?>%)
141+
<span class="badge"><?= $aliquota['count'] ?> <?= tr('righe') ?></span>
142+
</li>
143+
<?php endforeach; ?>
144+
</ul>
145+
<?php endif; ?>
146+
147+
<?php if (count($righe_senza_iva) > 0): ?>
148+
<p><strong><?= tr('Righe senza aliquota IVA valida') ?>:</strong> <span class="badge"><?= count($righe_senza_iva) ?></span></p>
149+
<?php endif; ?>
150+
151+
<p class="text-muted"><?= tr('Totale righe selezionate') ?>: <?= $righe_totali ?></p>
152+
</div>
153+
</div>
154+
155+
<div class="col-md-2 text-center" style="padding-top: 30px;">
156+
<i class="fa fa-arrow-right fa-3x text-muted"></i>
157+
</div>
158+
159+
<div class="col-md-5">
160+
<div class="panel panel-success">
161+
<div class="panel-heading">
162+
<h4 class="panel-title"><?= tr('Aliquota da applicare') ?></h4>
163+
</div>
164+
<div class="panel-body">
165+
{[ "type": "select", "label": "", "name": "iva_id", "required": 1, "values": "query=SELECT `co_iva`.`id`, IF(`deleted_at` IS NOT NULL, CONCAT(`codice`, ' - ', `title`, ' (<?= tr('eliminata') ?>)'), CONCAT(`codice`, ' - ', `title`)) AS `descrizione` FROM `co_iva` LEFT JOIN `co_iva_lang` ON (`co_iva`.`id` = `co_iva_lang`.`id_record` AND `co_iva_lang`.`id_lang` = <?= prepare(Models\Locale::getDefault()->id) ?>) ORDER BY `title` ASC" ]}
166+
<input type="hidden" name="riga_id" value="<?= get('riga_id') ?>">
167+
<input type="hidden" name="righe" value="<?= $_GET['righe'] ?>">
168+
</div>
169+
</div>
170+
</div>
171+
</div>
172+
<?php else: ?>
173+
<div class="row">
174+
<div class="col-md-12">
175+
<div class="alert alert-warning">
176+
<p><?= tr('Nessuna riga selezionata') ?></p>
177+
</div>
178+
</div>
179+
</div>
180+
<?php endif; ?>
181+
182+
<!-- PULSANTI -->
183+
<div class="row">
184+
<div class="col-md-12 text-right">
185+
<button type="button" class="btn btn-primary" onclick="salvaIva()">
186+
<i class="fa fa-save"></i> <?= tr('Salva') ?>
187+
</button>
188+
</div>
189+
</div>
190+
</form>
191+
192+
<script>
193+
function salvaIva() {
194+
var riga_id = $('#modifica-iva-form input[name=riga_id]').val();
195+
var righe = $('#modifica-iva-form input[name=righe]').val();
196+
var iva_id = input('iva_id').get();
197+
198+
// Se ci sono righe multiple selezionate
199+
if (righe) {
200+
// Converto la stringa in array
201+
var righe_array = righe.split(',');
202+
203+
$.ajax({
204+
url: globals.rootdir + "/actions.php",
205+
type: "POST",
206+
data: {
207+
id_module: globals.id_module,
208+
id_record: globals.id_record,
209+
op: "update_iva_multiple",
210+
righe: righe_array,
211+
iva_id: iva_id
212+
},
213+
success: function(response) {
214+
$('#modals > div').modal('hide');
215+
caricaRighe(null);
216+
renderMessages();
217+
},
218+
error: function() {
219+
alert(<?= json_encode(tr('Errore durante il salvataggio')) ?>);
220+
}
221+
});
222+
} else {
223+
// Singola riga
224+
$.ajax({
225+
url: globals.rootdir + "/actions.php",
226+
type: "POST",
227+
data: {
228+
id_module: globals.id_module,
229+
id_record: globals.id_record,
230+
op: "update_iva",
231+
riga_id: riga_id,
232+
iva_id: iva_id
233+
},
234+
success: function(response) {
235+
$('#modals > div').modal('hide');
236+
caricaRighe(riga_id);
237+
renderMessages();
238+
},
239+
error: function() {
240+
alert(<?= json_encode(tr('Errore durante il salvataggio')) ?>);
241+
}
242+
});
243+
}
244+
}
245+
</script>

0 commit comments

Comments
 (0)