ciao,
ho un codice che genera un flusso di cassa e ne calcola il totale.
ho esigenza di inserire un campo input che ospiti il prezzo scontato inserito dall'operatore.
a questo punto il totale dovrebbe essere aggiornato in base al nuovo prezzo.
Il tutto funziona già molto bene, ma il totale ricalcolato sullo sconto funziona solo su una singola riga, credo perche le righe sono create dinamicamente in JSON...credo.
posto il codice per chiunque abbia voglia di dare una mano a risolvere:
file index.php
codice:
<?php
session_start();
$products = null;
if (!empty($_SESSION['products'])) {
$products = $_SESSION['products'];
}
?>
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8">
<title>BarCode</title>
<meta http-equiv="imagetoolbar" content="no" />
<link href="css/core.css" rel="stylesheet" type="text/css" />
<!--[if lt IE 9]>
<script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link rel="shortcut icon" href="logo.png">
<link href="css/style.css" rel="stylesheet" type="text/css" />
<script src="js/jquery-1.6.2.min.js" type="text/javascript"></script>
<script src="js/core.js" type="text/javascript"></script>
</head>
<body>
<div id="wrapper">
<form action="" method="post" id="form_products">
<input type="text" id="name" name="name"
class="field field_medium"
placeholder="Codice Prodotto" value="" />
Cerca
Conferma e Paga
</form>
<div id="table_wrapper">
Inserisci un codice prodotto per iniziare la transazione.</p>
</div>
<input id="totale" value="" />
<div id="totaldiv">
</div>
</div>
</body>
</html>
file core.js
codice:
$(document).ready(function() {
$('.remove').live('click', function() {
var trigger = $(this);
var id = $(this).attr('rel');
var price_degree = $(this).attr('id');
$.getJSON('mod/remove.php?id='+id, function(data) {
var rows = $('.tbl_repeat tr').length;
if (rows > 2) {
trigger.closest('tr').fadeOut(300, function() {
$(this).remove();
var tot=($('#totale').val());
$('#totale').val(parseFloat(tot)-parseFloat(price_degree));
var total=($('#totale').val());
$('#totaldiv').html('
TOTALE: '+total+' €</p>');
});
} else {
$('.tbl_repeat').fadeOut(300, function() {
var msg = '
Inserisci un codice prodotto per iniziare la transazione.</p>';
$('#table_wrapper').hide().html(msg).fadeIn(300);
var tot=($('#totale').val());
$('#totale').val(parseFloat(tot)-parseFloat(price_degree));
var total=($('#totale').val());
$('#totaldiv').html('
TOTALE: '+total+' €</p>');
$('#totaldiv').html($('#pi').hide().fadeIn(300));
});
}
});
return false;
});
$('#name').keypress(function(event){
var keycode = (event.keyCode ? event.keyCode : event.which);
if(keycode == '13'){
var arr = $(this).closest('form').serializeArray();
var tbl = $('.tbl_repeat').length;
$.post('mod/row.php?tbl='+tbl, arr, function(data) {
if (tbl === 0) {
$('#table_wrapper').html(
$(data.row).hide().fadeIn(300)
);
} else {
$('.tbl_repeat tr:last').after(
$(data.row).hide().fadeIn(300)
.css('display', 'table-row')
);
}
if (tbl === 0) {
$('#totaldiv').html('<p id="pi">TOTALE: '+data.price+' €</p>');
$('#totale').val(data.price);
}
else {
var tot=($('#totale').val());
$('#totale').val(parseFloat(tot)+parseFloat(data.price));
var total=($('#totale').val());
$('#totaldiv').html('<p id="pi">TOTALE: '+total+' €</p>');
}
}, 'json');
$("#name").val("");
return false;
}
event.stopPropagation();
});
function sconto() {
if($('.sconto').val() == "") {
$(this).focus();
return false;
}
else {
var stot=($('#totale').val());
var scon=($('.sconto').val());
var price=($('.sconto').attr('placeholder'));
$('#totale').val(parseFloat(stot)-parseFloat(price)+parseFloat(scon));
var stotal=($('#totale').val());
$('.sconto').attr('placeholder', scon);
$('#totaldiv').html('<p id="pi">TOTALE: '+stotal+' €</p>');
}
}
file row.php
codice:
[...connessione al DB e select]
if(mysql_num_rows($rs) > 0) {
while($row = mysql_fetch_array($rs)) {
$name_[$row['ean']] = $row['name'];
$count[$row['ean']] = $row['quantity'];
$price[$row['ean']] = $row['price'];
}
}
else {
exit;
}
$tbl = !empty($_GET['tbl']) ? $_GET['tbl'] : 0;
$name = !empty($_POST['name']) ? $_POST['name'] : '';
if (!isset($_SESSION['max_index']) or !isset($_SESSION['products']) or empty($_SESSION['products']))
$index = 1;
else
$index = $_SESSION['max_index'] + 1;
$_SESSION['max_index'] = $index;
$_SESSION['products'][$index] = array(
'name' => $name
);
$row = '<tr>';
$row .= '<td>'.$name_[$_POST['name']].'</td>';
$row .= '<td class="ta_r">'.$name.'</td>';
$row .= '<td class="ta_r">'.sprintf("%.2f",$price[$_POST['name']]).' €</td>';
$row .= '<td class="ta_r"><input style="padding: 3px; border: solid 1px #aaa; width: 60px;" type="text" size="4" placeholder="'.sprintf("%.2f",$price[$_POST['name']]).'" onkeypress="validate(event)" onkeyup="sconto()" class="sconto"></td>';
$row .= '<td class="ta_r">1 / '.$count[$_POST['name']].'</td>';
$row .= '<td class="ta_r">Elimina</td>';
$row .= '</tr>';
if (empty($tbl)) {
$out = '<table cellpadding="0" cellspacing="0" ';
$out .= 'border="0" class="tbl_repeat">';
$out .= '<tr>';
$out .= '<th>Nome Prodotto</th>';
$out .= '<th class="col_1 ta_r">Codice</th>';
$out .= '<th class="col_1 ta_r">Prezzo</th>';
$out .= '<th class="col_1 ta_r" style="width: 140px;">Prezzo Scontato</th>';
$out .= '<th class="col_1 ta_r">Qtà / Disp.</th>';
$out .= '<th class="col_1 ta_r"></th>';
$out .= '</tr>';
$out .= $row;
$out .= '</table>';
$row = $out;
}
echo json_encode(array('row' => $row, 'check' => $check, 'price' => sprintf("%.2f",$price[$_POST['name']])));
la funzione incriminata è la funzione sconto in core.js
grazie anticipatamente