-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathventa-formulario.php
More file actions
183 lines (169 loc) · 7.98 KB
/
Copy pathventa-formulario.php
File metadata and controls
183 lines (169 loc) · 7.98 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
include_once "config.php";
include_once "entidades/cliente.php";
include_once "entidades/producto.php";
include_once "entidades/venta.php";
$pg = "Edición de venta";
$venta = new Venta();
$venta->cargarFormulario($_REQUEST);
if ($_POST) {
if (isset($_POST["btnGuardar"])) {
if (isset($_GET["id"]) && $_GET["id"] > 0) {
$venta->actualizar();
} else {
$venta->insertar();
}
$msg["texto"] = "Guardado correctamente";
$msg["codigo"] = "alert-success";
} else if (isset($_POST["btnBorrar"])) {
$venta->eliminar();
header("Location: venta-listado.php");
}
}
if (isset($_GET["do"]) && $_GET["do"] == "obtenerProducto" && $_GET["id"] && $_GET["id"] > 0) {
$aProducto = [
"precio" => Producto::obtenerPrecio($_GET["id"])
];
echo json_encode($aProducto);
exit;
}
if (isset($_GET["id"]) && $_GET["id"] > 0) {
$venta->obtenerPorId();
}
$aClientes = Cliente::obtenerTodos();
$aProductos = Producto::obtenerTodos();
include_once "header.php";
?>
<!-- Begin Page Content -->
<div class="container-fluid">
<!-- Page Heading -->
<h1 class="h3 mb-4 text-gray-800">Venta</h1>
<?php if (isset($msg)): ?>
<div class="row">
<div class="col-12">
<div class="alert <?php echo $msg["codigo"]; ?>" role="alert">
<?php echo $msg["texto"]; ?>
</div>
</div>
</div>
<?php endif;?>
<div class="row">
<div class="col-12 mb-3">
<a href="venta-listado.php" class="btn btn-primary mr-2">Listado</a>
<a href="venta-formulario.php" class="btn btn-primary mr-2">Nuevo</a>
<button type="submit" class="btn btn-success mr-2" id="btnGuardar" name="btnGuardar">Guardar</button>
<button type="submit" class="btn btn-danger" id="btnBorrar" name="btnBorrar">Borrar</button>
</div>
</div>
<div class="row">
<div class="col-6 form-group">
<label for="txtFechaDia">Fecha y hora:</label>
<div class="row">
<div class="col-3">
<select class="form-control" name="txtFechaDia" id="txtFechaDia">
<option value="" disabled <?= $venta->fecha_hora ? "" : " selected" ?>>DD</option>
<?php for ($i=1; $i <= 31; $i++): ?>
<option value="<?= $i ?>"
<?php
if ($venta->fecha_hora && $i == date_format(date_create($venta->fecha_hora), "j")) {
echo " selected";
}
?>
><?= $i ?></option>
<?php endfor; ?>
</select>
</div>
<div class="col-3">
<select class="form-control" name="txtFechaMes" id="txtFechaMes">
<?php for ($i=1; $i <= 12; $i++): ?>
<option value="<?= $i ?>"
<?php
if ($venta->fecha_hora && $i == date_format(date_create($venta->fecha_hora), "n")) {
echo " selected";
}
?>
><?= $i ?></option>
<?php endfor; ?>
</select>
</div>
<div class="col-3">
<select class="form-control" name="txtFechaAnio" id="txtFechaAnio">
<?php for ($i=date('Y'); $i >= 1900; $i--): ?>
<option value="<?= $i ?>"
<?php
if ($venta->fecha_hora && $i == date_format(date_create($venta->fecha_hora), "Y")) {
echo " selected";
}
?>
><?= $i ?></option>
<?php endfor; ?>
</select>
</div>
<div class="col-3">
<input type="time" class="form-control" name="txtHora" id="txtHora" value="<?= $venta->fecha_hora ? date_format(date_create($venta->fecha_hora), "H:i") : "" ?>">
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-6 form-group">
<label for="lstCliente">Cliente:</label>
<select required name="lstCliente" id="lstCliente" class="form-control">
<option value="" selected disabled>Seleccionar</option>
<?php foreach($aClientes as $cliente): ?>
<option value="<?= $cliente->idcliente ?>"<?= $venta->fk_idcliente == $cliente->idcliente ? " selected" : "" ?>><?= $cliente->nombre ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-6 form-group">
<label for="lstProducto">Producto:</label>
<select required name="lstProducto" id="lstProducto" class="form-control" onchange="fObtenerProducto()">
<option value="" selected disabled>Seleccionar</option>
<?php foreach($aProductos as $producto): ?>
<option value="<?= $producto->idproducto ?>"<?= $venta->fk_idproducto == $producto->idproducto ? " selected" : "" ?>><?= $producto->nombre ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-6 form-group">
<label for="txtPrecioUnitario">Precio unitario:</label>
<input onchange="fCalcularTotal()" type="number" readonly class="form-control" name="txtPrecioUnitario" id="txtPrecioUnitario" value="<?= $venta->preciounitario ?>">
</div>
<div class="col-6 form-group">
<label for="txtCantidad">Cantidad:</label>
<input onchange="fCalcularTotal()" type="number" required class="form-control" name="txtCantidad" id="txtCantidad" value="<?= $venta->cantidad ?>">
</div>
<div class="col-6 form-group">
<label for="txtTotal">Total:</label>
<input type="number" required class="form-control" readonly name="txtTotal" id="txtTotal" value="<?= $venta->total ?>">
</div>
</div>
</div>
<!-- /.container-fluid -->
</div>
<!-- End of Main Content -->
<script>
function fCalcularTotal() {
let cantidad = $('#txtCantidad').val();
let precioUnitario = $('#txtPrecioUnitario').val();
if (cantidad && precioUnitario) {
$('#txtTotal').val((Number(cantidad) * Number(precioUnitario)).toFixed(2));
} else {
$('#txtTotal').val('');
}
}
function fObtenerProducto() {
idProducto = $("#lstProducto option:selected").val();
$.ajax({
type: "GET",
url: "venta-formulario.php?do=obtenerProducto",
data: { id:idProducto },
async: true,
dataType: "json",
success: function (respuesta) {
$('#txtPrecioUnitario').val((Number(respuesta.precio ?? 0)).toFixed(2));
fCalcularTotal();
}
});
}
</script>
<?php include_once "footer.php";?>