Skip to content

Commit 5ffe7fe

Browse files
feat: gestione calcolo sconto combinato in fase di modifica riga nei documenti
1 parent f82ff36 commit 5ffe7fe

4 files changed

Lines changed: 142 additions & 1 deletion

File tree

ajax.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,5 +153,23 @@
153153

154154
echo json_encode($results);
155155

156+
break;
157+
158+
case 'calcola_sconto_combinato':
159+
$prc_combinato = post('prc_combinato');
160+
161+
if (!empty($prc_combinato)) {
162+
$sconto = parseScontoCombinato($prc_combinato);
163+
echo json_encode([
164+
'success' => true,
165+
'sconto' => $sconto,
166+
]);
167+
} else {
168+
echo json_encode([
169+
'success' => false,
170+
'error' => tr('Sconto combinato non valido'),
171+
]);
172+
}
173+
156174
break;
157175
}

include/common/riga.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,9 @@ function aggiorna_guadagno() {
211211
// Sconto unitario
212212
echo '
213213
<div class="col-md-'.$width.'">
214+
<button type="button" class="btn btn-info btn-xs pull-right tip" title="'.tr('Calcola sconto combinato').'" id="btn-sconto-combinato" '.($result['tipo_sconto'] !== 'PRC' ? 'disabled' : '').'>
215+
<i class="fa fa-calculator"></i>
216+
</button>
214217
{[ "type": "number", "label": "'.tr('Sconto unitario').'", "name": "sconto", "value": "'.($result['sconto_percentuale'] ?: $result['sconto_unitario_corrente']).'", "icon-after": "choice|untprc|'.$result['tipo_sconto'].'", "help": "'.tr('Il valore positivo indica uno sconto. Per applicare una maggiorazione inserire un valore negativo.').'" ]}
215218
</div>
216219
</div>';
@@ -426,5 +429,30 @@ function controlla_sconto() {
426429
}
427430
}
428431
});
432+
433+
// Abilita/disabilita il pulsante in base al tipo di sconto
434+
var updateButtonState = function() {
435+
var tipo_sconto_select = $("#modals select[id^=\'tipo_sconto\']");
436+
var btn = $("#btn-sconto-combinato");
437+
438+
if (tipo_sconto_select.length) {
439+
var tipo = tipo_sconto_select.val();
440+
if (tipo === "PRC") {
441+
btn.prop("disabled", false);
442+
} else {
443+
btn.prop("disabled", true);
444+
}
445+
}
446+
};
447+
448+
// Aggiorna lo stato al cambio del tipo di sconto
449+
$(document).on("change", "#modals select[id^=\'tipo_sconto\']", updateButtonState);
450+
451+
// Pulsante calcola sconto combinato
452+
$("#btn-sconto-combinato").click(function() {
453+
if (!$(this).prop("disabled")) {
454+
openModal("'.tr('Calcola sconto combinato').'", globals.rootdir + "/modules/fatture/modals/sconto_combinato.php");
455+
}
456+
});
429457
});
430458
</script>';

include/common/sconto.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
if ($options['action'] == 'add') {
3434
echo '
3535
<div class="col-md-4">
36+
<button type="button" class="btn btn-info btn-xs pull-right tip" title="'.tr('Calcola sconto combinato').'" id="btn-sconto-combinato">
37+
<i class="fa fa-calculator"></i>
38+
</button>
3639
{[ "type": "number", "label": "'.tr('Sconto/maggiorazione percentuale').'", "name": "sconto_percentuale", "icon-after": "%", "help": "'.tr('Il valore positivo indica uno sconto: per applicare una maggiorazione inserire un valore negativo').'" ]}
3740
</div>';
3841
}
@@ -105,4 +108,9 @@ function aggiorna_sconto_unitario(){
105108
106109
sconto_percentuale.keyup(aggiorna_sconto_percentuale);
107110
sconto_unitario.keyup(aggiorna_sconto_unitario);
108-
</script>';
111+
112+
// Gestione pulsante sconto combinato
113+
$("#btn-sconto-combinato").click(function() {
114+
openModal("'.tr('Calcola sconto combinato').'", globals.rootdir + "/modules/fatture/modals/sconto_combinato.php");
115+
});
116+
</script>';
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
/*
4+
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
5+
* Copyright (C) DevCode s.r.l.
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
*/
20+
21+
include_once __DIR__.'/../../../core.php';
22+
23+
// Contenuto del modal per il calcolo dello sconto combinato
24+
echo '
25+
<form id="form-sconto-combinato">
26+
<div class="row">
27+
<div class="offset-md-4 col-md-4">
28+
{[ "type": "text", "label": "'.tr('Sconto/magg. combinato').'", "name": "prc_combinato", "icon-after": "%", "class": "math-mask text-right", "help": "'.tr('Esempio: 2+2+2 viene convertito in 2% di sconto con 2% aggiuntivo sul totale scontato e 2% di maggiorazione sul totale finale').'. '.tr('Sono ammessi i segni + e -').'" ]}
29+
</div>
30+
</div>
31+
</form>
32+
33+
<div class="row">
34+
<div class="col-md-12 text-right">
35+
<button type="button" class="btn btn-success" id="btn-confirm-sconto"><i class="fa fa-check"></i> '.tr('Conferma').'</button>
36+
</div>
37+
</div>
38+
39+
<script>
40+
$(document).ready(function() {
41+
var modal = $("#modals > div:last");
42+
43+
// Determina quale campo sconto è presente (sconto_percentuale o sconto)
44+
var sconto_field = $("#sconto_percentuale").length ? $("#sconto_percentuale") : $("#sconto");
45+
46+
// Reinizializza i componenti del form quando il modal si apre
47+
modal.on("shown.bs.modal", function() {
48+
restart_inputs();
49+
});
50+
51+
// Gestisci il click del pulsante di conferma
52+
$("#form-sconto-combinato").on("submit", function(e) {
53+
e.preventDefault();
54+
var prc_combinato = $("#prc_combinato").val();
55+
56+
if (prc_combinato) {
57+
// Calcola lo sconto combinato
58+
$.ajax({
59+
url: globals.rootdir + "/ajax.php",
60+
type: "POST",
61+
data: {
62+
op: "calcola_sconto_combinato",
63+
prc_combinato: prc_combinato
64+
},
65+
dataType: "json",
66+
success: function(response) {
67+
if (response.success) {
68+
sconto_field.val(response.sconto.toLocale());
69+
sconto_field.trigger("keyup");
70+
modal.modal("hide");
71+
} else {
72+
alert(response.error);
73+
}
74+
}
75+
});
76+
} else {
77+
alert("'.tr('Inserire uno sconto').'");
78+
}
79+
});
80+
81+
// Gestisci il click del pulsante di conferma
82+
modal.find("#btn-confirm-sconto").click(function() {
83+
$("#form-sconto-combinato").submit();
84+
});
85+
});
86+
</script>';
87+

0 commit comments

Comments
 (0)