-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproveedores.js
346 lines (309 loc) · 8.98 KB
/
proveedores.js
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
const apiUrl = "http://l.iqtics.mx/api";
class ProveedorTabla {
constructor(id, represenante, razonSocial) {
this.id = id;
this.representante = represenante;
this.razonSocial = razonSocial;
this.acciones = `
<button class="btn btn-info btn-sm btn-editar" type="button" data-toggle="modal" data-client-id="${id}" data-backdrop="static" data-keyboard="false" data-target="#EditarCliente">
<i class="fas fa-user-edit"></i>
</button>
<button class="btn btn-danger btn-sm btn-eliminar" type="submit" data-client-id="${id}">
<i class="fas fa-user-times"></i>
</button>
`;
}
}
class ProveedorFiltro {
constructor(
id,
claveRegistro,
representante,
telefono,
celular,
email,
ciudad,
razonSocial,
rfc,
telefonoEmpresa,
celularEmpresa,
ciudadEmpresa,
paqueteria
) {
this.id = id;
this.claveRegistro = claveRegistro;
this.representante = representante;
this.telfono = telefono;
this.celular = celular;
this.email = email;
this.ciudad = ciudad;
this.razonSocial = razonSocial;
this.rfc = rfc;
this.telefonoEmpresa = telefonoEmpresa;
this.celularEmpresa = celularEmpresa;
this.ciudadEmpresa = ciudadEmpresa;
this.paqueteria = paqueteria;
}
}
let proveedores = [];
let proveedoresFilter = [];
let proveedorSeleccionado = {};
(() => {
document.addEventListener("DOMContentLoaded", async () => {
let formFiltros = document.querySelector("#form-filtros");
let formMostrarCliente = document.querySelector("#mostrar-proveedor");
let inputsFechaAlta = document.querySelectorAll(".input-fecha-alta");
proveedores = await GetProveedores();
let tabla = CrearTabla(TransoformarInformacion(proveedores));
formFiltros.addEventListener("keyup", () => {
let filtro = formFiltros.value;
proveedoresFilter = proveedores.filter((c) =>
FiltrarProveedores(c, filtro)
);
RecargarTabla(tabla, TransoformarInformacion(proveedoresFilter));
});
document
.getElementById("add-proveedor")
.addEventListener("submit", onSubmitAddForm);
document
.getElementById("edit-proveedor")
.addEventListener("submit", onSubmitEditForm);
document.getElementById("btn-export").addEventListener("click", exportXls);
});
})();
function onSubmitAddForm(e) {
const form = e.target;
PostProveedor(new FormData(form));
reload();
}
function onSubmitEditForm(e) {
const form = e.target;
PutProveedor(new FormData(form));
reload();
}
async function exportXls(e) {
const result = await HttpClient.get(`${apiUrl}/proveedores/export`);
console.log(result);
const url = window.URL.createObjectURL(result);
const a = document.createElement("a");
a.setAttribute("download", "Clientes.xlsx");
a.href = url;
a.click();
a.remove();
window.URL.revokeObjectURL(url);
}
async function PostProveedor(proveedor) {
const result = await HttpClient.post(`${apiUrl}/proveedores`, proveedor);
console.log(result);
}
async function PutProveedor(proveedor) {
const result = await HttpClient.put(
`${apiUrl}/proveedores/` + proveedorSeleccionado.id,
proveedor
);
console.log(result);
}
async function GetProveedores() {
const result = await HttpClient.get(`${apiUrl}/proveedores`);
return result;
}
function FindProveedoresOfIdx(id) {
let idx = proveedores.findIndex((proveedor) => proveedor.id === id);
return idx;
}
async function EliminarProveedor(IdCliente) {
let data = IdCliente;
const result = await HttpClient.delete(`${apiUrl}/proveedores/` + data);
let clienteDeleted = await result.json();
proveedores.splice(FindProveedoresOfIdx(clienteDeleted.id), 1);
}
function SeleccionarProveedor(idProveedor) {
let cliente = proveedores[FindProveedoresOfIdx(idProveedor)];
llenarForm("#mostrar-proveedor", cliente);
}
function CrearTabla(data) {
let tabla = $("#tabla").DataTable({
searching: false,
paging: false,
bInfo: false,
bScrollInfinite: true,
bScrollCollapse: true,
ordering: false,
sScrollY: "170px",
columnDefs: [{ width: "10%", targets: 2 }],
createdRow: function (row, data, dataIndex) {
$(row).attr("id", data.id);
},
columns: [
{ data: "representante" },
{ data: "razonSocial" },
{ data: "acciones" },
],
data: data,
});
SelectFila(tabla);
CargarBotones(tabla);
return tabla;
}
function SelectFila(tabla) {
let filas = [...document.querySelectorAll("#tabla tbody tr")];
filas.forEach((tr) => {
tr.addEventListener("click", () => {
if (tr.classList.contains("selected")) {
tr.classList.remove("selected");
tr.style.backgroundColor = "";
tr.style.color = "";
document.getElementById("mostrar-proveedor").reset();
} else {
tabla.$("tr.selected").removeClass("selected");
tr.classList.add("selected");
for (var i = 0; i < filas.length; i++) {
filas[i].style.backgroundColor = "";
filas[i].style.color = "";
}
tr.style.backgroundColor = "#1560ff";
tr.style.color = "#fff";
SeleccionarProveedor(tr.id);
}
});
});
}
function TransoformarInformacion(data) {
let dataResult = [];
data.forEach((proveedor) =>
dataResult.push(
new ProveedorTabla(
proveedor.id,
proveedor.representante,
proveedor.razonSocial
)
)
);
return dataResult;
}
function FiltrarProveedores(proveedor, str) {
let datosProveedor = Object.values(proveedor).join("");
return datosProveedor.includes(str);
}
function reload() {
window.location.reload();
}
async function CargarBotones(tabla) {
let btnElimniar = document.querySelectorAll(".btn-eliminar");
let btnEditar = document.querySelectorAll(".btn-editar");
btnElimniar.forEach((btn) => {
let idCliente = btn.dataset.clientId;
btn.addEventListener("click", () => {
const confirmacion = confirm("¿Desea borrar el proveedor?");
if (confirmacion) {
EliminarProveedor(idCliente);
reload();
}
});
});
btnEditar.forEach((btn) => {
btn.addEventListener("click", () => {
let id = FindProveedoresOfIdx(btn.dataset.clientId);
console.log(id);
proveedorSeleccionado = proveedores[id];
console.log(proveedorSeleccionado);
console.log(proveedorSeleccionado.id);
llenarForm("#edit-proveedor", proveedorSeleccionado);
});
});
}
function llenarForm(idForm, entity) {
const inputs = {
value: ["text", "number", "select", "date"],
check: ["checkbox"],
};
const fomInputs = [...document.querySelector(idForm).elements];
if(entity !== undefined){
const keys = Object.keys(entity);
fomInputs.forEach((input) => {
if (inputs.value.includes(input.type)) {
const key = keys.find(
(k) => k.toUpperCase() === input.name.toUpperCase()
);
input.value = entity[key];
}
if (inputs.check.includes(input.type)) {
const key = keys.find(
(k) => k.toUpperCase() === input.name.toUpperCase()
);
input.checked = entity[key];
}
});
}
}
class HttpClient {
static async get(url) {
const result = await fetch(url)
.then(async (res) => {
if (res.ok) {
const contentType = res.headers.get("content-type");
if (contentType && contentType.indexOf("application/json") !== -1) {
return await res.json();
} else if (
contentType &&
contentType.indexOf(
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
) !== -1
) {
return await res.blob();
} else {
console.log("Oops, we haven't got JSON!");
}
} else {
throw "Error";
}
})
.catch((error) => console.log(error));
return result;
}
static post(url, data) {
const result = fetch(url, {
method: "post",
body: data,
})
.then(async (res) => {
if (res.ok) {
const contentType = res.headers.get("content-type");
if (contentType && contentType.indexOf("application/json") !== -1) {
return await res.json();
} else {
console.log("Oops, we haven't got JSON!");
}
} else {
throw "Error";
}
})
.catch((err) => console.log(err));
return result;
}
static put(url, data) {
return fetch(url, {
method: "put",
body: data,
})
.then(async (res) => {
if (res.ok) {
const contentType = res.headers.get("content-type");
if (contentType && contentType.indexOf("application/json") !== -1) {
return await res.json();
} else {
console.log("Oops, we haven't got JSON!");
}
} else {
throw "Error";
}
})
.catch((err) => console.log(err));
}
static delete(url) {
return fetch(url, {
method: "delete",
});
}
}