PHP - COMO AGREGAR ITEM CON COMBO Y QUE CALCULE TOTALES DE VARIOS ARTICULOS

 
Vista:
sin imagen de perfil
Val: 13
Ha aumentado su posición en 16 puestos en PHP (en relación al último mes)
Gráfica de PHP

COMO AGREGAR ITEM CON COMBO Y QUE CALCULE TOTALES DE VARIOS ARTICULOS

Publicado por elizabeth laguna almaguer (9 intervenciones) el 20/08/2019 05:56:57
QUE TAL BUENAS NOCHES MI DUDA ES LA SIGUIENTE. TENGO UN PROBLEMA EN MI CODIGO YA QUE QUIERO QUE SE AGREGUEN VARIOS COMBOS CUANDO PRESIONO EL BOTON DE AGREGA NUEVO ITEM PERO TAMBIEN MI DUDA ES SI PUEDO HACER QUE HAGA LA SUMATORIA DE TODOS LOS ARTICULOS QUE YO AGREGUE Y ADEMAS TAMBIÉN QUE APARTE DEL IVA ME CALCULE UN DESCUENTO. ALGUNA IDEA DE COMO PODRÍA QUEDAR MI CÓDIGO HACIENDO ESO QUE NECESITO?

ESTE ES EL FORMULARIO DONDE AGREGUE LOS COMBOS Y AQUI HACE LA OPERACION DE SACAR TOTALES

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
        session_start();
        include('header.php');
        include 'Invoice.php';
        $invoice = new Invoice();
        $invoice->checkLoggedIn();
        if(!empty($_POST['companyName']) && $_POST['companyName']) {
                $invoice->saveInvoice($_POST);
                header("Location:invoice_list.php");
        }
        ?>
        <title>baulphp : Sistema facturación PHP & MySQL</title>
        <script src="js/invoice.js"></script>
        <link href="css/style.css" rel="stylesheet">
        <?php include('container.php');?>
        <div class="container content-invoice">
        <form action="" id="invoice-form" method="post" class="invoice-form" role="form" novalidate>
        <div class="load-animate animated fadeInUp">
        <div class="row">
        <div class="col-xs-8 col-sm-8 col-md-8 col-lg-8">
            <h2 class="title">Sistema de Facturación PHP</h2>
            <?php include('menu.php');?>
        </div>
        </div>
        <input id="currency" type="hidden" value="$">
        <div class="row">
        <div class="col-xs-12 col-sm-4 col-md-4 col-lg-4">
            <h3>De,</h3>
            <?php echo $_SESSION['user']; ?><br>
            <?php echo $_SESSION['address']; ?><br>
            <?php echo $_SESSION['mobile']; ?><br>
            <?php echo $_SESSION['email']; ?><br>
        </div>
 
            <script>
 
    		$(function(){
				// Clona la fila oculta que tiene los campos base, y la agrega al final de la tabla
				$("#addRows").on('click', function(){
					$("#invoiceItem tbody tr:eq(0)").clone().removeClass('fila-fija').appendTo("#invoiceItem");
				});
 
				// Evento que selecciona la fila y la elimina 
				$(document).on("click",".eliminar",function(){
					var parent = $(this).parents().get(0);
					$(parent).remove();
				});
			});
 
 
		</script>
 
 
 
        <div class="col-xs-12 col-sm-4 col-md-4 col-lg-4 pull-right">
            <h3>Para,</h3>
            <div class="form-group">
                <input type="text" class="form-control" name="companyName" id="companyName" placeholder="Nombre de Empresa" autocomplete="off">
            </div>
            <div class="form-group">
                <textarea class="form-control" rows="3" name="address" id="address" placeholder="Su dirección"></textarea>
            </div>
 
        </div>
        </div>
        <div class="row">
        <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
            <table class="table table-bordered table-hover" id="invoiceItem">
                <tr class="fila-fija">
 
 
                    <td><input class="itemRow" type="checkbox"></td>
                    <td><select type="text" required name="productCode[]" id="productCode_1" class="form-control" placeholder="Cod. Producto"></select></td>
                    <td><select type="text" required name="productName[]" id="productName_1" class="form-control" placeholder="Nombre Producto"></select></td>
                    <td><select type="number" required name="quantity[]" id="quantity_1" class="form-control quantity" placeholder="Cantidad"></select></td>
                    <td><select type="number" required name="price[]" id="price_1" class="form-control price" placeholder="Precio"></select></td>
                    <td><select type="number" required name="total[]" id="total_1" class="form-control total" placeholder="Total"></select></td>
                </tr>
            </table>
        </div>
        </div>
        <div class="row">
        <div class="col-xs-12 col-sm-3 col-md-3 col-lg-3">
            <button class="btn btn-danger delete" id="eliminar" type="button">- Borrar</button>
            <button class="btn btn-success" id="addRows" type="button">+ Agregar Más</button>
        </div>
        </div>
        <div class="row">
        <div class="col-xs-12 col-sm-8 col-md-8 col-lg-8">
            <h3>Notas: </h3>
            <div class="form-group">
                <textarea class="form-control txt" rows="5" name="notes" id="notes" placeholder="Notas"></textarea>
            </div>
            <br>
            <div class="form-group">
                <input type="hidden" value="<?php echo $_SESSION['userid']; ?>" class="form-control" name="userId">
                <input data-loading-text="Guardando factura..." type="submit" name="invoice_btn" value="Guardar Factura" class="btn btn-success submit_btn invoice-save-btm">
            </div>
 
        </div>
        <div class="col-xs-12 col-sm-4 col-md-4 col-lg-4">
            <span class="form-inline">
                <div class="form-group">
                    <label>Subtotal: &nbsp;</label>
                    <div class="input-group">
                        <div class="input-group-addon currency">$</div>
                        <input value="" type="number" class="form-control" name="subTotal" id="subTotal" placeholder="Subtotal">
                    </div>
                </div>
                <div class="form-group">
                    <label>Tasa Impuesto: &nbsp;</label>
                    <div class="input-group">
                        <input value="" type="number" class="form-control" name="taxRate" id="taxRate" placeholder="Tasa de Impuestos">
                        <div class="input-group-addon">%</div>
                    </div>
                </div>
                <div class="form-group">
                    <label>Monto Inpuesto: &nbsp;</label>
                    <div class="input-group">
                        <div class="input-group-addon currency">$</div>
                        <input value="" type="number" class="form-control" name="taxAmount" id="taxAmount" placeholder="Monto de Impuesto">
                    </div>
                </div>
                <div class="form-group">
                    <label>Total: &nbsp;</label>
                    <div class="input-group">
                        <div class="input-group-addon currency">$</div>
                        <input value="" type="number" class="form-control" name="totalAftertax" id="totalAftertax" placeholder="Total">
                    </div>
                </div>
                <div class="form-group">
                    <label>Cantidad pagada: &nbsp;</label>
                    <div class="input-group">
                        <div class="input-group-addon currency">$</div>
                        <input value="" type="number" class="form-control" name="amountPaid" id="amountPaid" placeholder="Cantidad pagada">
                    </div>
                </div>
                <div class="form-group">
                    <label>Cantidad debida: &nbsp;</label>
                    <div class="input-group">
                        <div class="input-group-addon currency">$</div>
                        <input value="" type="number" class="form-control" name="amountDue" id="amountDue" placeholder="Cantidad debida">
                    </div>
                </div>
            </span>
        </div>
        </div>
        <div class="clearfix"></div>
        </div>
        </form>
        </div>
        </div>
        <?php include('footer.php');?>


AQUI LAS FUNCIONES HACEN TODO EL TRABAJO PARA QUE SEA POSIBLE OBTENER LA SUMA DE SUBTOTALES Y DESCUENTOS PERO YO NECESITO QUE EN ESTE CODIGO ME CLONE LOS MISMOS COMBOS AL MOMENTO DE AGREGAR NUEVOS ITEMS Y QUE ME HAGA LA SUMA DE TODO ELLOS


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
$(function(){
				// Clona la fila oculta que tiene los campos base, y la agrega al final de la tabla
				$("#addRows").on('click', function(){
					$("#invoiceItem").clone().removeClass('fila-fija').appendTo("#invoiceItem");
				});
 
				// Evento que selecciona la fila y la elimina 
				$(document).on("click",".eliminar",function(){
					var parent = $(this).parents().get(0);
					$(parent).remove();
				});
			});
 
 
 
 $(document).ready(function(){
	$(document).on('click', '#checkAll', function() {
		$(".itemRow").prop("checked", this.checked);
	});
	$(document).on('click', '.itemRow', function() {
		if ($('.itemRow:checked').length == $('.itemRow').length) {
			$('#checkAll').prop('checked', true);
		} else {
			$('#checkAll').prop('checked', false);
		}
	});
	var count = $(".itemRow").length;
	$(document).on('click', '#addRows', function() {
		count++;
		var htmlRows = '';
		htmlRows += '<tr class="fila-fija">';
		htmlRows += '<td><input class="itemRow" type="checkbox"></td>';
		htmlRows += '<td><select type="text" required name="productCode[]" id="productCode_'+count+'" class="form-control" autocomplete="off"></select></td>';
		htmlRows += '<td><select type="text" required name="productName[]" id="productName_'+count+'" class="form-control" autocomplete="off"></select></td>';
		htmlRows += '<td><select type="number" required name="quantity[]" id="quantity_'+count+'" class="form-control quantity" autocomplete="off"></select></td>';
		htmlRows += '<td><select type="number" required name="price[]" id="price_'+count+'" class="form-control price" autocomplete="off"></select></td>';
		htmlRows += '<td><select type="number" required name="total[]" id="total_'+count+'" class="form-control total" autocomplete="off"></select></td>';
		htmlRows += '</tr>';
		$('#invoiceItem').append(htmlRows);
	});
	$(document).on('click', '#removeRows', function(){
		$(".itemRow:checked").each(function() {
			$(this).closest('tr').remove();
		});
		$('#checkAll').prop('checked', false);
		calculateTotal();
	});
	$(document).on('blur', "[id^=quantity_]", function(){
		calculateTotal();
	});
	$(document).on('blur', "[id^=price_]", function(){
		calculateTotal();
	});
	$(document).on('blur', "#taxRate", function(){
		calculateTotal();
	});
	$(document).on('blur', "#amountPaid", function(){
		var amountPaid = $(this).val();
		var totalAftertax = $('#totalAftertax').val();
		if(amountPaid && totalAftertax) {
			totalAftertax = totalAftertax-amountPaid;
			$('#amountDue').val(totalAftertax);
		} else {
			$('#amountDue').val(totalAftertax);
		}
	});
	$(document).on('click', '.deleteInvoice', function(){
		var id = $(this).attr("id");
		if(confirm("Are you sure you want to remove this?")){
			$.ajax({
				url:"action.php",
				method:"POST",
				dataType: "json",
				data:{id:id, action:'delete_invoice'},
				success:function(response) {
					if(response.status == 1) {
						$('#'+id).closest("tr").remove();
					}
				}
			});
		} else {
			return false;
		}
	});
});
function calculateTotal(){
	var totalAmount = 0;
	$("[id^='price_']").each(function() {
		var id = $(this).attr('id');
		id = id.replace("price_",'');
		var price = $('#price_'+id).val();
		var quantity  = $('#quantity_'+id).val();
		if(!quantity) {
			quantity = 1;
		}
		var total = price*quantity;
		$('#total_'+id).val(parseFloat(total));
		totalAmount += total;
	});
	$('#subTotal').val(parseFloat(totalAmount));
	var taxRate = $("#taxRate").val();
	var subTotal = $('#subTotal').val();
	if(subTotal) {
		var taxAmount = subTotal*taxRate/100;
		$('#taxAmount').val(taxAmount);
		subTotal = parseFloat(subTotal)+parseFloat(taxAmount);
		$('#totalAftertax').val(subTotal);
		var amountPaid = $('#amountPaid').val();
		var totalAftertax = $('#totalAftertax').val();
		if(amountPaid && totalAftertax) {
			totalAftertax = totalAftertax-amountPaid;
			$('#amountDue').val(totalAftertax);
		} else {
			$('#amountDue').val(subTotal);
		}
	}
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder