@zauru-sdk/utils 1.0.6 → 1.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/dist/index.d.ts +23 -2
- package/dist/index.js +23 -2
- package/dist/zauru/automatic-number.utils.d.ts +9 -0
- package/dist/zauru/automatic-number.utils.js +24 -0
- package/dist/zauru/bitacora-edicion-masiva.utils.d.ts +23 -0
- package/dist/zauru/bitacora-edicion-masiva.utils.js +50 -0
- package/dist/zauru/bundles.utils.d.ts +39 -0
- package/dist/zauru/bundles.utils.js +160 -0
- package/dist/zauru/cases.utils.d.ts +9 -0
- package/dist/zauru/cases.utils.js +17 -0
- package/dist/zauru/consolidated-history.utils.d.ts +24 -0
- package/dist/zauru/consolidated-history.utils.js +52 -0
- package/dist/zauru/costos-items.utils.d.ts +87 -0
- package/dist/zauru/costos-items.utils.js +601 -0
- package/dist/zauru/deliveries.utils.d.ts +17 -0
- package/dist/zauru/deliveries.utils.js +46 -0
- package/dist/zauru/discharges-histsory.utils.d.ts +24 -0
- package/dist/zauru/discharges-histsory.utils.js +46 -0
- package/dist/zauru/employees.utils.d.ts +16 -0
- package/dist/zauru/employees.utils.js +39 -0
- package/dist/zauru/form.utils.d.ts +34 -0
- package/dist/zauru/form.utils.js +152 -0
- package/dist/zauru/invoices.utils.d.ts +22 -0
- package/dist/zauru/invoices.utils.js +79 -0
- package/dist/zauru/items.utils.d.ts +52 -0
- package/dist/zauru/items.utils.js +152 -0
- package/dist/zauru/lotes.utils.d.ts +31 -0
- package/dist/zauru/lotes.utils.js +307 -0
- package/dist/zauru/motivos-rechazo.utils.d.ts +10 -0
- package/dist/zauru/motivos-rechazo.utils.js +18 -0
- package/dist/zauru/payees.utils.d.ts +42 -0
- package/dist/zauru/payees.utils.js +128 -0
- package/dist/zauru/profiles.utils.d.ts +15 -0
- package/dist/zauru/profiles.utils.js +37 -0
- package/dist/zauru/providers.utils.d.ts +13 -0
- package/dist/zauru/providers.utils.js +36 -0
- package/dist/zauru/purchase-orders.utils.d.ts +114 -0
- package/dist/zauru/purchase-orders.utils.js +376 -0
- package/dist/zauru/shipments.utils.d.ts +21 -0
- package/dist/zauru/shipments.utils.js +90 -0
- package/dist/zauru/specialItem.utils.d.ts +34 -0
- package/dist/zauru/specialItem.utils.js +62 -0
- package/dist/zauru/templates.utils.d.ts +49 -0
- package/dist/zauru/templates.utils.js +93 -0
- package/dist/zauru/tiposMuestra.utils.d.ts +34 -0
- package/dist/zauru/tiposMuestra.utils.js +62 -0
- package/dist/zauru/webapp-tables.utils.d.ts +16 -0
- package/dist/zauru/webapp-tables.utils.js +51 -0
- package/package.json +6 -6
- package/src/index.ts +23 -0
- package/src/zauru/bitacora-edicion-masiva.utils.ts +99 -0
- package/src/zauru/bundles.utils.ts +251 -0
- package/src/zauru/cases.utils.ts +31 -0
- package/src/zauru/consolidated-history.utils.ts +102 -0
- package/src/zauru/costos-items.utils.ts +865 -0
- package/src/zauru/deliveries.utils.ts +77 -0
- package/src/zauru/discharges-histsory.utils.ts +105 -0
- package/src/zauru/employees.utils.ts +59 -0
- package/src/zauru/form.utils.ts +243 -0
- package/src/zauru/invoices.utils.ts +114 -0
- package/src/zauru/items.utils.ts +269 -0
- package/src/zauru/lotes.utils.ts +411 -0
- package/src/zauru/motivos-rechazo.utils.ts +47 -0
- package/src/zauru/payees.utils.ts +213 -0
- package/src/zauru/profiles.utils.ts +44 -0
- package/src/zauru/providers.utils.ts +67 -0
- package/src/zauru/purchase-orders.utils.ts +594 -0
- package/src/zauru/shipments.utils.ts +139 -0
- package/src/zauru/specialItem.utils.ts +126 -0
- package/src/zauru/templates.utils.ts +199 -0
- package/src/zauru/tiposMuestra.utils.ts +136 -0
- package/src/zauru/webapp-tables.utils.ts +94 -0
- /package/src/{automatic-number.utils.ts → zauru/automatic-number.utils.ts} +0 -0
|
@@ -0,0 +1,601 @@
|
|
|
1
|
+
import { handlePossibleAxiosErrors, localDateToUSDate, } from "@zauru-sdk/common";
|
|
2
|
+
import moment from "moment";
|
|
3
|
+
import { createSpecialItem, updateSpecialItem } from "./specialItem.utils.js";
|
|
4
|
+
import { createWebAppTableRegister, getPurchasesList, getVariablesByName, getWebAppTableRegisters, updateWebAppTableRegister } from "@zauru-sdk/services";
|
|
5
|
+
import { updatePurchaseItemPrice } from "./purchase-orders.utils.js";
|
|
6
|
+
/**
|
|
7
|
+
*
|
|
8
|
+
* @param headers
|
|
9
|
+
* @param session
|
|
10
|
+
* @param configuration
|
|
11
|
+
* @param specialItems
|
|
12
|
+
*/
|
|
13
|
+
export const guardarConfiguracionProductosEspeciales = (headers, session, configuration, specialItems) => {
|
|
14
|
+
return handlePossibleAxiosErrors(async () => {
|
|
15
|
+
specialItems?.forEach(async (x) => {
|
|
16
|
+
if (!configuration.some((y) => y.id === x.id)) {
|
|
17
|
+
//Elimino los que ya no están
|
|
18
|
+
const response = await updateSpecialItem(headers, session, x.id.toString(), {
|
|
19
|
+
...x.data,
|
|
20
|
+
fechaEliminacion: moment().format("DD/MM/YYYY HH:mm:ss"),
|
|
21
|
+
});
|
|
22
|
+
if (response.error) {
|
|
23
|
+
throw new Error(`Ocurrió un error al intentar eliminar un item especial. - ${response.userMsg ?? ""}`);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
configuration.forEach(async (x) => {
|
|
28
|
+
if (typeof x.id === "string") {
|
|
29
|
+
//Creación
|
|
30
|
+
const response = await createSpecialItem(headers, session, {
|
|
31
|
+
...x,
|
|
32
|
+
});
|
|
33
|
+
if (response.error) {
|
|
34
|
+
throw new Error(`Ocurrió un error al intentar guardar la configuración de items especiales. - ${response.userMsg ?? ""}`);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
//actualización
|
|
39
|
+
const response = await updateSpecialItem(headers, session, x.id.toString(), {
|
|
40
|
+
...x,
|
|
41
|
+
});
|
|
42
|
+
if (response.error) {
|
|
43
|
+
throw new Error(`Ocurrió un error al intentar actualizar un item especial. - ${response.userMsg ?? ""}`);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
return true;
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
*
|
|
52
|
+
* @returns
|
|
53
|
+
*/
|
|
54
|
+
export const obtenerSemanaAnterior = () => {
|
|
55
|
+
const hoy = moment();
|
|
56
|
+
const semanaAnterior = hoy.clone().subtract(7, "days");
|
|
57
|
+
const fechaInicio = semanaAnterior.clone().startOf("isoWeek");
|
|
58
|
+
const fechaInicioFormateada = fechaInicio.format("YYYY/MM/DD");
|
|
59
|
+
const fechaFin = fechaInicio.clone().endOf("isoWeek");
|
|
60
|
+
const fechaFinFormateada = fechaFin.format("YYYY/MM/DD");
|
|
61
|
+
const label = "Semana anterior, del Lunes " +
|
|
62
|
+
fechaInicio.date() +
|
|
63
|
+
" al Domingo " +
|
|
64
|
+
fechaFin.date() +
|
|
65
|
+
", de " +
|
|
66
|
+
fechaInicio.format("MMMM") +
|
|
67
|
+
" de " +
|
|
68
|
+
fechaInicio.year();
|
|
69
|
+
return {
|
|
70
|
+
fechaInicio: fechaInicioFormateada,
|
|
71
|
+
fechaFin: fechaFinFormateada,
|
|
72
|
+
label: label,
|
|
73
|
+
getDay: (dayIndex) => {
|
|
74
|
+
if (dayIndex < 0 || dayIndex > 6) {
|
|
75
|
+
throw new Error("Day index must be between 0 (Monday) and 6 (Sunday).");
|
|
76
|
+
}
|
|
77
|
+
const targetDate = fechaInicio.clone().add(dayIndex, "days");
|
|
78
|
+
return targetDate.format("YYYY/MM/DD");
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
};
|
|
82
|
+
const getPriceByDate = (date, fechas, costo) => {
|
|
83
|
+
const originDate = new Date(date).setHours(0, 0, 0, 0);
|
|
84
|
+
const lunesDate = new Date(fechas.lunes).setHours(0, 0, 0, 0);
|
|
85
|
+
const martesDate = new Date(fechas.martes).setHours(0, 0, 0, 0);
|
|
86
|
+
const miercolesDate = new Date(fechas.miercoles).setHours(0, 0, 0, 0);
|
|
87
|
+
const juevesDate = new Date(fechas.jueves).setHours(0, 0, 0, 0);
|
|
88
|
+
const viernesDate = new Date(fechas.viernes).setHours(0, 0, 0, 0);
|
|
89
|
+
const sabadoDate = new Date(fechas.sabado).setHours(0, 0, 0, 0);
|
|
90
|
+
const domingoDate = new Date(fechas.domingo).setHours(0, 0, 0, 0);
|
|
91
|
+
if (originDate === lunesDate || originDate === martesDate) {
|
|
92
|
+
return costo?.lunMar ?? 1;
|
|
93
|
+
}
|
|
94
|
+
else if (originDate === miercolesDate || originDate === juevesDate) {
|
|
95
|
+
return costo?.mieJue ?? 1;
|
|
96
|
+
}
|
|
97
|
+
else if (originDate === viernesDate ||
|
|
98
|
+
originDate === sabadoDate ||
|
|
99
|
+
originDate === domingoDate) {
|
|
100
|
+
return costo?.vieSabDom ?? 1;
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
return 1;
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
const logTotal = (total) => {
|
|
107
|
+
console.log("========================================================================");
|
|
108
|
+
console.log("========================================================================");
|
|
109
|
+
console.log("========================================================================");
|
|
110
|
+
console.log("========================================================================");
|
|
111
|
+
console.log(`A PUNTO DE ACTUALIZAR: ${total} órdenes de compra`);
|
|
112
|
+
console.log("========================================================================");
|
|
113
|
+
console.log("========================================================================");
|
|
114
|
+
console.log("========================================================================");
|
|
115
|
+
console.log("========================================================================");
|
|
116
|
+
};
|
|
117
|
+
/**
|
|
118
|
+
* updateOneItemPrice
|
|
119
|
+
* @param headers
|
|
120
|
+
* @param session
|
|
121
|
+
* @param body
|
|
122
|
+
* @returns
|
|
123
|
+
*/
|
|
124
|
+
export async function updateOneItemPrice(headers, session, body) {
|
|
125
|
+
return handlePossibleAxiosErrors(async () => {
|
|
126
|
+
//PASO 1
|
|
127
|
+
//Obtengo todas las órdenes de compra dentro del rango de fechas
|
|
128
|
+
const fechaInicio = localDateToUSDate(body.fechaInicio);
|
|
129
|
+
const fechaFin = localDateToUSDate(body.fechaFin);
|
|
130
|
+
console.log(`==============================> OBTENIENDO ÓRDENES DE COMPRA EN EL RANGO ${fechaInicio} - ${fechaFin}`);
|
|
131
|
+
const allPurchases = await getPurchasesList(headers, session, {
|
|
132
|
+
fechaInicio: fechaInicio,
|
|
133
|
+
fechaFin: fechaFin,
|
|
134
|
+
});
|
|
135
|
+
console.log("ORDENES DE COMPRA OBTENIDAS: ", allPurchases.data?.length);
|
|
136
|
+
if (allPurchases.error || !allPurchases.data) {
|
|
137
|
+
//TODO: REGISTRAR EL FALLO EN LA ACTUALIZACIÓN
|
|
138
|
+
return "error";
|
|
139
|
+
}
|
|
140
|
+
//PASO 2
|
|
141
|
+
//Por cada órden de compra, voy a buscar su item para verificar el precio a colocarle en el día que fue emitida
|
|
142
|
+
//Obtengo todos los id's de las órdenes de compra en ese rango de fechas
|
|
143
|
+
const costosWithPurchaseIds = [];
|
|
144
|
+
const purchaseIterations = allPurchases.data.filter((x) => x.purchase_order_details.some((y) => y.item_id === body.item));
|
|
145
|
+
for (const purchase of purchaseIterations) {
|
|
146
|
+
const purchase_order_details = purchase.purchase_order_details.map((details) => {
|
|
147
|
+
return {
|
|
148
|
+
id: details.id,
|
|
149
|
+
item_id: details.item_id,
|
|
150
|
+
unit_cost: body.price,
|
|
151
|
+
};
|
|
152
|
+
});
|
|
153
|
+
costosWithPurchaseIds.push({
|
|
154
|
+
id: purchase.id,
|
|
155
|
+
fecha: purchase.created_at,
|
|
156
|
+
finalizado: true,
|
|
157
|
+
msgResultado: "",
|
|
158
|
+
purchase_order_details,
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
//PASO 3
|
|
162
|
+
//Editar los precios de todas las órdenes de compra
|
|
163
|
+
logTotal(costosWithPurchaseIds.length);
|
|
164
|
+
for (let i = 0; i < costosWithPurchaseIds.length; i++) {
|
|
165
|
+
const x = costosWithPurchaseIds[i];
|
|
166
|
+
console.log(`==============================> ACTUALIZANDO ÓRDEN DE COMPRA: ID: ${x.id} PRICE: ${x.purchase_order_details
|
|
167
|
+
.map((x) => x.unit_cost)
|
|
168
|
+
.join(",")} ID_ITEM: ${x.purchase_order_details
|
|
169
|
+
.map((x) => x.item_id)
|
|
170
|
+
.join(",")}`);
|
|
171
|
+
const updateResponse = await updatePurchaseItemPrice(headers, {
|
|
172
|
+
purchase_order_details_attributes: {
|
|
173
|
+
...x.purchase_order_details
|
|
174
|
+
.map((detail, index) => {
|
|
175
|
+
return {
|
|
176
|
+
[`${index}`]: {
|
|
177
|
+
id: detail.id,
|
|
178
|
+
item_id: detail.item_id,
|
|
179
|
+
unit_cost: detail.unit_cost,
|
|
180
|
+
},
|
|
181
|
+
};
|
|
182
|
+
})
|
|
183
|
+
.reduce((acc, curr) => Object.assign(acc, curr), {}),
|
|
184
|
+
},
|
|
185
|
+
}, x.id);
|
|
186
|
+
if (updateResponse.error) {
|
|
187
|
+
// Si hay error
|
|
188
|
+
costosWithPurchaseIds[i].finalizado = false;
|
|
189
|
+
costosWithPurchaseIds[i].msgResultado =
|
|
190
|
+
updateResponse.userMsg ?? JSON.stringify(updateResponse);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
//PASO 4
|
|
194
|
+
//Guardo la bitácora de cambios
|
|
195
|
+
console.log(`==============================> GUARDANDO BITÁCORA DE CAMBIOS`);
|
|
196
|
+
const response = await saveBitacoraCostos(headers, session, {
|
|
197
|
+
costosEspeciales: [
|
|
198
|
+
{ specialItemId: body.item, lunMar: 1, mieJue: 1, vieSabDom: 1 },
|
|
199
|
+
],
|
|
200
|
+
accion: "Costos individualmente",
|
|
201
|
+
fechaCreacion: moment().format("DD/MM/YYYY HH:mm:ss"),
|
|
202
|
+
fechas: {
|
|
203
|
+
lunes: "",
|
|
204
|
+
martes: "",
|
|
205
|
+
miercoles: "",
|
|
206
|
+
jueves: "",
|
|
207
|
+
viernes: "",
|
|
208
|
+
sabado: "",
|
|
209
|
+
domingo: "",
|
|
210
|
+
},
|
|
211
|
+
modificadoPor: session.get("name"),
|
|
212
|
+
costos: [{ item: body.item, lunMar: 1, mieJue: 1, vieSabDom: 1 }],
|
|
213
|
+
costosActualizados: costosWithPurchaseIds,
|
|
214
|
+
costosEspecialesActualizados: [],
|
|
215
|
+
});
|
|
216
|
+
if (response.error || !response.data) {
|
|
217
|
+
throw new Error(response.userMsg ??
|
|
218
|
+
"Respuesta vacía de saveBitacoraCostos en updateOneItemPrice");
|
|
219
|
+
}
|
|
220
|
+
return response.data;
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* changePricesInit
|
|
225
|
+
* @param headers
|
|
226
|
+
* @param session
|
|
227
|
+
* @param specialItems
|
|
228
|
+
* @param body
|
|
229
|
+
* @returns
|
|
230
|
+
*/
|
|
231
|
+
export async function changePricesInit(headers, session, specialItems, body) {
|
|
232
|
+
return handlePossibleAxiosErrors(async () => {
|
|
233
|
+
//PASO 1
|
|
234
|
+
//Obtengo todas las órdenes de compra dentro del rango de fechas
|
|
235
|
+
console.log("=======================> FECHAS: ", body.fechas);
|
|
236
|
+
const fechaInicio = body.fechas.lunes;
|
|
237
|
+
const fechaFin = body.fechas.domingo;
|
|
238
|
+
console.log(`==============================> OBTENIENDO ÓRDENES DE COMPRA EN EL RANGO ${fechaInicio} - ${fechaFin}`);
|
|
239
|
+
const allPurchases = await getPurchasesList(headers, session, {
|
|
240
|
+
fechaInicio: fechaInicio,
|
|
241
|
+
fechaFin: fechaFin,
|
|
242
|
+
});
|
|
243
|
+
if (allPurchases.error || !allPurchases.data) {
|
|
244
|
+
throw new Error(`Error al ubicar las órdenes de compra en el rango de fechas establecido: ${allPurchases.userMsg}`);
|
|
245
|
+
}
|
|
246
|
+
//PASO 2
|
|
247
|
+
//Por cada órden de compra, voy a buscar su item para verificar el precio a colocarle en el día que fue emitida
|
|
248
|
+
//Obtengo todos los id's de las órdenes de compra en ese rango de fechas
|
|
249
|
+
const costosWithPurchaseIds = [];
|
|
250
|
+
const costosEspecialesWithPurchaseIds = []; //Para las verduras especiales
|
|
251
|
+
ForPrincipal: for (const purchase of allPurchases.data) {
|
|
252
|
+
//Pregunto si la órden de compra coincide con alguno de los casos especiales
|
|
253
|
+
for (const costoEspecial of body.costosEspeciales) {
|
|
254
|
+
//Ubico la configuración de item especial.
|
|
255
|
+
const specialItem = specialItems.find((x) => x.id === costoEspecial.specialItemId);
|
|
256
|
+
for (const details of purchase.purchase_order_details) {
|
|
257
|
+
//Tengo que preguntar por proveedor, region, categoría de proveedores, tipo e item.
|
|
258
|
+
if (specialItem?.data.region === purchase.incoterm_destination &&
|
|
259
|
+
specialItem?.data?.tipo === purchase.reference &&
|
|
260
|
+
specialItem.data.item === details.item_id) {
|
|
261
|
+
const costo = body.costosEspeciales.find((x) => x.specialItemId === specialItem.id);
|
|
262
|
+
const price = getPriceByDate(purchase.created_at, body.fechas, costo);
|
|
263
|
+
costosWithPurchaseIds.push({
|
|
264
|
+
id: purchase.id,
|
|
265
|
+
fecha: purchase.created_at,
|
|
266
|
+
purchase_order_details: [
|
|
267
|
+
{
|
|
268
|
+
id: details.id,
|
|
269
|
+
item_id: details.item_id,
|
|
270
|
+
unit_cost: price,
|
|
271
|
+
},
|
|
272
|
+
],
|
|
273
|
+
});
|
|
274
|
+
continue ForPrincipal;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
//SI SIGUE AQUÍ, ES POR QUE NO ES VERDURA ESPECIAL
|
|
279
|
+
const purchase_order_details = purchase.purchase_order_details.map((details) => {
|
|
280
|
+
const costo = body.costos.find((x) => x.item === details.item_id);
|
|
281
|
+
const price = getPriceByDate(purchase.created_at, body.fechas, costo);
|
|
282
|
+
return { id: details.id, item_id: details.item_id, unit_cost: price };
|
|
283
|
+
});
|
|
284
|
+
costosWithPurchaseIds.push({
|
|
285
|
+
id: purchase.id,
|
|
286
|
+
fecha: purchase.created_at,
|
|
287
|
+
purchase_order_details,
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
//PASO 5
|
|
291
|
+
//Guardo la bitácora de cambios
|
|
292
|
+
console.log(`==============================> GUARDANDO BITÁCORA DE CAMBIOS`);
|
|
293
|
+
const response = await saveBitacoraCostos(headers, session, {
|
|
294
|
+
...body,
|
|
295
|
+
costosActualizados: costosWithPurchaseIds,
|
|
296
|
+
costosEspecialesActualizados: costosEspecialesWithPurchaseIds,
|
|
297
|
+
});
|
|
298
|
+
if (response.error || !response.data) {
|
|
299
|
+
const errorMsg = `Error al guardar la bitácora de costos ${response.userMsg}` ??
|
|
300
|
+
"Respuesta vacía de saveBitacoraCostos en changePrices";
|
|
301
|
+
console.error(errorMsg);
|
|
302
|
+
throw new Error(errorMsg);
|
|
303
|
+
}
|
|
304
|
+
return response.data;
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* changePrice
|
|
309
|
+
* @param headers
|
|
310
|
+
* @param session
|
|
311
|
+
* @param body
|
|
312
|
+
* @returns
|
|
313
|
+
*/
|
|
314
|
+
export async function changePrice(headers, costo) {
|
|
315
|
+
const x = costo;
|
|
316
|
+
const msgActualizando = `==============================> ACTUALIZANDO ÓRDEN DE COMPRA: ID: ${x.id} PRICE: ${x.purchase_order_details
|
|
317
|
+
.map((x) => x.unit_cost)
|
|
318
|
+
.join(",")} ID_ITEM: ${x.purchase_order_details
|
|
319
|
+
.map((x) => x.item_id)
|
|
320
|
+
.join(",")}`;
|
|
321
|
+
const body = {
|
|
322
|
+
purchase_order_details_attributes: {
|
|
323
|
+
...x.purchase_order_details
|
|
324
|
+
.map((detail, index) => {
|
|
325
|
+
return {
|
|
326
|
+
[`${index}`]: {
|
|
327
|
+
id: detail.id,
|
|
328
|
+
item_id: detail.item_id,
|
|
329
|
+
unit_cost: detail.unit_cost,
|
|
330
|
+
},
|
|
331
|
+
};
|
|
332
|
+
})
|
|
333
|
+
.reduce((acc, curr) => Object.assign(acc, curr), {}),
|
|
334
|
+
},
|
|
335
|
+
};
|
|
336
|
+
console.log(msgActualizando);
|
|
337
|
+
const updateResponse = await updatePurchaseItemPrice(headers, body, x.id);
|
|
338
|
+
if (updateResponse.error) {
|
|
339
|
+
// Si hay error
|
|
340
|
+
costo.finalizado = false;
|
|
341
|
+
costo.msgResultado =
|
|
342
|
+
updateResponse.userMsg ??
|
|
343
|
+
`Ocurrió un error al editar la órden de compra ${JSON.stringify(updateResponse)}`;
|
|
344
|
+
}
|
|
345
|
+
else {
|
|
346
|
+
costo.finalizado = true;
|
|
347
|
+
}
|
|
348
|
+
return costo;
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* changePrices
|
|
352
|
+
* @param headers
|
|
353
|
+
* @param session
|
|
354
|
+
* @param body
|
|
355
|
+
* @returns
|
|
356
|
+
*/
|
|
357
|
+
export async function changePrices(headers, session, bitacora, id_bitacora) {
|
|
358
|
+
return handlePossibleAxiosErrors(async () => {
|
|
359
|
+
const costosWithPurchaseIds = bitacora.costosActualizados;
|
|
360
|
+
const costosEspecialesWithPurchaseIds = bitacora.costosEspecialesActualizados;
|
|
361
|
+
//PASO 3
|
|
362
|
+
//Editar los precios de todas las órdenes de compra
|
|
363
|
+
logTotal(costosWithPurchaseIds.length);
|
|
364
|
+
for (let i = 0; i < costosWithPurchaseIds.length; i++) {
|
|
365
|
+
const x = costosWithPurchaseIds[i];
|
|
366
|
+
const msgActualizando = `==============================> ACTUALIZANDO ÓRDEN DE COMPRA: ID: ${x.id} PRICE: ${x.purchase_order_details
|
|
367
|
+
.map((x) => x.unit_cost)
|
|
368
|
+
.join(",")} ID_ITEM: ${x.purchase_order_details
|
|
369
|
+
.map((x) => x.item_id)
|
|
370
|
+
.join(",")}`;
|
|
371
|
+
console.log(msgActualizando);
|
|
372
|
+
const updateResponse = await updatePurchaseItemPrice(headers, {
|
|
373
|
+
purchase_order_details_attributes: {
|
|
374
|
+
...x.purchase_order_details
|
|
375
|
+
.map((detail, index) => {
|
|
376
|
+
return {
|
|
377
|
+
[`${index}`]: {
|
|
378
|
+
id: detail.id,
|
|
379
|
+
item_id: detail.item_id,
|
|
380
|
+
unit_cost: detail.unit_cost,
|
|
381
|
+
},
|
|
382
|
+
};
|
|
383
|
+
})
|
|
384
|
+
.reduce((acc, curr) => Object.assign(acc, curr), {}),
|
|
385
|
+
},
|
|
386
|
+
}, x.id);
|
|
387
|
+
if (updateResponse.error) {
|
|
388
|
+
// Si hay error
|
|
389
|
+
costosWithPurchaseIds[i].finalizado = false;
|
|
390
|
+
costosWithPurchaseIds[i].msgResultado =
|
|
391
|
+
updateResponse.userMsg ?? JSON.stringify(updateResponse);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
//PASO 4
|
|
395
|
+
//EDITO LAS ORDENES DE COMPRA PARA ITEMS ESPECIALES
|
|
396
|
+
logTotal(costosEspecialesWithPurchaseIds.length);
|
|
397
|
+
for (let i = 0; i < costosEspecialesWithPurchaseIds.length; i++) {
|
|
398
|
+
const x = costosEspecialesWithPurchaseIds[i];
|
|
399
|
+
console.log(`==============================> ACTUALIZANDO ÓRDEN DE COMPRA (ESPECIAL): ID: ${x.id} PRICE: ${x.purchase_order_details
|
|
400
|
+
.map((x) => x.unit_cost)
|
|
401
|
+
.join(",")} ID_ITEM: ${x.purchase_order_details
|
|
402
|
+
.map((x) => x.item_id)
|
|
403
|
+
.join(",")}`);
|
|
404
|
+
const updateResponse = await updatePurchaseItemPrice(headers, {
|
|
405
|
+
purchase_order_details_attributes: {
|
|
406
|
+
...x.purchase_order_details
|
|
407
|
+
.map((detail, index) => {
|
|
408
|
+
return {
|
|
409
|
+
[`${index}`]: {
|
|
410
|
+
id: detail.id,
|
|
411
|
+
item_id: detail.item_id,
|
|
412
|
+
unit_cost: detail.unit_cost,
|
|
413
|
+
},
|
|
414
|
+
};
|
|
415
|
+
})
|
|
416
|
+
.reduce((acc, curr) => Object.assign(acc, curr), {}),
|
|
417
|
+
},
|
|
418
|
+
}, x.id);
|
|
419
|
+
if (updateResponse.error) {
|
|
420
|
+
// Si hay error
|
|
421
|
+
costosEspecialesWithPurchaseIds[i].finalizado = false;
|
|
422
|
+
costosEspecialesWithPurchaseIds[i].msgResultado =
|
|
423
|
+
updateResponse.userMsg ?? JSON.stringify(updateResponse);
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
//PASO 5
|
|
427
|
+
//Actualizo la bitácora de cambios
|
|
428
|
+
console.log(`==============================> GUARDANDO BITÁCORA DE CAMBIOS`);
|
|
429
|
+
const response = await updateBitacoraCostos(headers, session, {
|
|
430
|
+
...bitacora,
|
|
431
|
+
costosActualizados: costosWithPurchaseIds,
|
|
432
|
+
costosEspecialesActualizados: costosEspecialesWithPurchaseIds,
|
|
433
|
+
}, id_bitacora);
|
|
434
|
+
if (response.error || !response.data) {
|
|
435
|
+
const errorMsg = response.userMsg ?? "Respuesta vacía de updateBitacora en changePrices";
|
|
436
|
+
console.error(errorMsg);
|
|
437
|
+
throw new Error(errorMsg);
|
|
438
|
+
}
|
|
439
|
+
return response.data;
|
|
440
|
+
});
|
|
441
|
+
}
|
|
442
|
+
/**
|
|
443
|
+
* corregirCostos
|
|
444
|
+
* @param headers
|
|
445
|
+
* @param session
|
|
446
|
+
* @param costos
|
|
447
|
+
* @returns
|
|
448
|
+
*/
|
|
449
|
+
export async function corregirCostos(headers, session, costos, costosEspeciales) {
|
|
450
|
+
return handlePossibleAxiosErrors(async () => {
|
|
451
|
+
//PASO 1
|
|
452
|
+
//Hago una copia
|
|
453
|
+
const costosTemp = costos;
|
|
454
|
+
const costosEspecialesTemp = costosEspeciales;
|
|
455
|
+
//PASO 2
|
|
456
|
+
//Editar los precios de todas las órdenes de compra
|
|
457
|
+
logTotal(costosTemp.length);
|
|
458
|
+
for (let i = 0; i < costosTemp.length; i++) {
|
|
459
|
+
const x = costosTemp[i];
|
|
460
|
+
console.log(`==============================> ACTUALIZANDO ÓRDEN DE COMPRA: ID: ${x.id} PRICE: ${x.purchase_order_details
|
|
461
|
+
.map((x) => x.unit_cost)
|
|
462
|
+
.join(",")} ID_ITEM: ${x.purchase_order_details
|
|
463
|
+
.map((x) => x.item_id)
|
|
464
|
+
.join(",")}`);
|
|
465
|
+
const updateResponse = await updatePurchaseItemPrice(headers, {
|
|
466
|
+
purchase_order_details_attributes: {
|
|
467
|
+
...x.purchase_order_details
|
|
468
|
+
.map((detail, index) => {
|
|
469
|
+
return {
|
|
470
|
+
[`${index}`]: {
|
|
471
|
+
id: detail.id,
|
|
472
|
+
item_id: detail.item_id,
|
|
473
|
+
unit_cost: detail.unit_cost,
|
|
474
|
+
},
|
|
475
|
+
};
|
|
476
|
+
})
|
|
477
|
+
.reduce((acc, curr) => Object.assign(acc, curr), {}),
|
|
478
|
+
},
|
|
479
|
+
}, x.id);
|
|
480
|
+
if (!updateResponse.error) {
|
|
481
|
+
// Si hay error
|
|
482
|
+
costosTemp[i].finalizado = true;
|
|
483
|
+
}
|
|
484
|
+
else {
|
|
485
|
+
costosTemp[i].finalizado = false;
|
|
486
|
+
costosTemp[i].msgResultado =
|
|
487
|
+
updateResponse.userMsg ?? JSON.stringify(updateResponse);
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
//PASO 3
|
|
491
|
+
//Editar los precios de todas las órdenes de compra de precios especiales
|
|
492
|
+
logTotal(costosEspecialesTemp.length);
|
|
493
|
+
for (let i = 0; i < costosEspecialesTemp.length; i++) {
|
|
494
|
+
const x = costosEspecialesTemp[i];
|
|
495
|
+
console.log(`==============================> ACTUALIZANDO ÓRDEN DE COMPRA (ESPECIAL): ID: ${x.id} PRICE: ${x.purchase_order_details
|
|
496
|
+
.map((x) => x.unit_cost)
|
|
497
|
+
.join(",")} ID_ITEM: ${x.purchase_order_details
|
|
498
|
+
.map((x) => x.item_id)
|
|
499
|
+
.join(",")}`);
|
|
500
|
+
const updateResponse = await updatePurchaseItemPrice(headers, {
|
|
501
|
+
purchase_order_details_attributes: {
|
|
502
|
+
...x.purchase_order_details
|
|
503
|
+
.map((detail, index) => {
|
|
504
|
+
return {
|
|
505
|
+
[`${index}`]: {
|
|
506
|
+
id: detail.id,
|
|
507
|
+
item_id: detail.item_id,
|
|
508
|
+
unit_cost: detail.unit_cost,
|
|
509
|
+
},
|
|
510
|
+
};
|
|
511
|
+
})
|
|
512
|
+
.reduce((acc, curr) => Object.assign(acc, curr), {}),
|
|
513
|
+
},
|
|
514
|
+
}, x.id);
|
|
515
|
+
if (!updateResponse.error) {
|
|
516
|
+
// Si hay error
|
|
517
|
+
costosEspecialesTemp[i].finalizado = true;
|
|
518
|
+
}
|
|
519
|
+
else {
|
|
520
|
+
costosEspecialesTemp[i].finalizado = false;
|
|
521
|
+
costosEspecialesTemp[i].msgResultado =
|
|
522
|
+
updateResponse.userMsg ?? JSON.stringify(updateResponse);
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
//PASO 3
|
|
526
|
+
//Guardo la bitácora de cambios
|
|
527
|
+
console.log(`==============================> GUARDANDO BITÁCORA DE CAMBIOS`);
|
|
528
|
+
const response = await saveBitacoraCostos(headers, session, {
|
|
529
|
+
accion: "Reintento de asignación de costos",
|
|
530
|
+
costos: [],
|
|
531
|
+
costosEspeciales: [],
|
|
532
|
+
fechaCreacion: moment().format("DD/MM/YYYY HH:mm:ss"),
|
|
533
|
+
fechas: {
|
|
534
|
+
domingo: "",
|
|
535
|
+
jueves: "",
|
|
536
|
+
lunes: "",
|
|
537
|
+
martes: "",
|
|
538
|
+
miercoles: "",
|
|
539
|
+
sabado: "",
|
|
540
|
+
viernes: "",
|
|
541
|
+
},
|
|
542
|
+
modificadoPor: session.get("name"),
|
|
543
|
+
costosActualizados: costosTemp,
|
|
544
|
+
costosEspecialesActualizados: costosEspecialesTemp,
|
|
545
|
+
});
|
|
546
|
+
if (response.error || !response.data) {
|
|
547
|
+
throw new Error(response.userMsg ??
|
|
548
|
+
"Respuesta vacía de saveBitacoraCostos en changePrices");
|
|
549
|
+
}
|
|
550
|
+
return response.data;
|
|
551
|
+
});
|
|
552
|
+
}
|
|
553
|
+
/**
|
|
554
|
+
* Get saveBitacoraCostos from the web app table.
|
|
555
|
+
* @param headers Request headers.
|
|
556
|
+
* @param session Session object.
|
|
557
|
+
* @returns A Promise of AxiosUtilsResponse<WebAppRowGraphQL<BitacoraCostosItems>[]>.
|
|
558
|
+
*/
|
|
559
|
+
export async function saveBitacoraCostos(headers, session, body) {
|
|
560
|
+
return handlePossibleAxiosErrors(async () => {
|
|
561
|
+
const { bitacora_costos_verduras_web_app_table_id } = await getVariablesByName(headers, session, [
|
|
562
|
+
"bitacora_costos_verduras_web_app_table_id",
|
|
563
|
+
]);
|
|
564
|
+
const response = await createWebAppTableRegister(headers, bitacora_costos_verduras_web_app_table_id, { ...body, modificadoPor: session.get("name") });
|
|
565
|
+
return response;
|
|
566
|
+
});
|
|
567
|
+
}
|
|
568
|
+
/**
|
|
569
|
+
* Put updateBitacoraCostos from the web app table.
|
|
570
|
+
* @param headers Request headers.
|
|
571
|
+
* @param session Session object.
|
|
572
|
+
* @returns A Promise of AxiosUtilsResponse<WebAppRowGraphQL<BitacoraCostosItems>[]>.
|
|
573
|
+
*/
|
|
574
|
+
export async function updateBitacoraCostos(headers, session, body, id_registro) {
|
|
575
|
+
return handlePossibleAxiosErrors(async () => {
|
|
576
|
+
const { bitacora_costos_verduras_web_app_table_id } = await getVariablesByName(headers, session, [
|
|
577
|
+
"bitacora_costos_verduras_web_app_table_id",
|
|
578
|
+
]);
|
|
579
|
+
const response = await updateWebAppTableRegister(headers, bitacora_costos_verduras_web_app_table_id, id_registro, { ...body, modificadoPor: session.get("name") });
|
|
580
|
+
return response;
|
|
581
|
+
});
|
|
582
|
+
}
|
|
583
|
+
/**
|
|
584
|
+
* Get getCostosBitacora from the web app table.
|
|
585
|
+
* @param headers Request headers.
|
|
586
|
+
* @param session Session object.
|
|
587
|
+
* @returns A Promise of AxiosUtilsResponse<WebAppRowGraphQL<BitacoraCostosItems>[]>.
|
|
588
|
+
*/
|
|
589
|
+
export async function getCostosBitacora(headers, session) {
|
|
590
|
+
return handlePossibleAxiosErrors(async () => {
|
|
591
|
+
const { bitacora_costos_verduras_web_app_table_id } = await getVariablesByName(headers, session, [
|
|
592
|
+
"bitacora_costos_verduras_web_app_table_id",
|
|
593
|
+
]);
|
|
594
|
+
const response = await getWebAppTableRegisters(session, bitacora_costos_verduras_web_app_table_id);
|
|
595
|
+
if (response.error) {
|
|
596
|
+
throw new Error(`Ocurrió un error al tratar de obtener la bitácora de costos: ${response.userMsg}`);
|
|
597
|
+
}
|
|
598
|
+
const bitacora = response.data ?? [];
|
|
599
|
+
return bitacora;
|
|
600
|
+
});
|
|
601
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Session } from "@remix-run/node";
|
|
2
|
+
import { AxiosUtilsResponse } from "@zauru-sdk/types";
|
|
3
|
+
export type DeliveryDataTable = {
|
|
4
|
+
zid: string;
|
|
5
|
+
referencia: string;
|
|
6
|
+
fechaEntregaEstimada: string;
|
|
7
|
+
fechaEntrega: string;
|
|
8
|
+
origen: string;
|
|
9
|
+
destino: string;
|
|
10
|
+
deliveryId: string;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* getDeliveriesFormated
|
|
14
|
+
* @param headers
|
|
15
|
+
* @param session
|
|
16
|
+
*/
|
|
17
|
+
export declare const getDeliveriesDataTableFormated: (headers: any, session: Session) => Promise<AxiosUtilsResponse<DeliveryDataTable[]>>;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { extractValueBetweenTags } from "@zauru-sdk/common";
|
|
2
|
+
import { getDeliveriesDataTables } from "@zauru-sdk/services";
|
|
3
|
+
/**
|
|
4
|
+
* getDeliveriesFormated
|
|
5
|
+
* @param headers
|
|
6
|
+
* @param session
|
|
7
|
+
*/
|
|
8
|
+
export const getDeliveriesDataTableFormated = async (headers, session) => {
|
|
9
|
+
const body = {
|
|
10
|
+
order: {
|
|
11
|
+
"0": {
|
|
12
|
+
column: "0",
|
|
13
|
+
dir: "desc",
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
start: 0,
|
|
17
|
+
length: 40,
|
|
18
|
+
search: {
|
|
19
|
+
value: "",
|
|
20
|
+
regex: false,
|
|
21
|
+
},
|
|
22
|
+
agency: session.get("agency_id"),
|
|
23
|
+
};
|
|
24
|
+
const response = await getDeliveriesDataTables(headers, body);
|
|
25
|
+
if (response.error) {
|
|
26
|
+
return { ...response, data: [] };
|
|
27
|
+
}
|
|
28
|
+
const deliveries = response.data?.data?.map((x) => formatHTMLDeliveryList(x)) ?? [];
|
|
29
|
+
return { ...response, data: deliveries };
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* formatHTMLDeliveryList
|
|
33
|
+
* @param delivery
|
|
34
|
+
* @returns
|
|
35
|
+
*/
|
|
36
|
+
const formatHTMLDeliveryList = (delivery) => {
|
|
37
|
+
return {
|
|
38
|
+
zid: extractValueBetweenTags(delivery.zid, "a"),
|
|
39
|
+
referencia: extractValueBetweenTags(delivery.ref, "a"),
|
|
40
|
+
fechaEntrega: delivery.pd,
|
|
41
|
+
fechaEntregaEstimada: delivery.dt,
|
|
42
|
+
destino: delivery.at,
|
|
43
|
+
origen: delivery.af,
|
|
44
|
+
deliveryId: delivery.DT_RowId.replace("inventories-delivery-", ""),
|
|
45
|
+
};
|
|
46
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Session } from "@remix-run/node";
|
|
2
|
+
import { AxiosUtilsResponse, WebAppRowGraphQL, DischargeHistory, WebAppTableUpdateResponse } from "@zauru-sdk/types";
|
|
3
|
+
/**
|
|
4
|
+
* Get getDischargeHistories from the web app table.
|
|
5
|
+
* @param headers Request headers.
|
|
6
|
+
* @param session Session object.
|
|
7
|
+
* @returns A Promise of AxiosUtilsResponse<DischargeHistory[]>.
|
|
8
|
+
*/
|
|
9
|
+
export declare function getDischargeHistories(headers: any, session: Session): Promise<AxiosUtilsResponse<WebAppRowGraphQL<DischargeHistory>[]>>;
|
|
10
|
+
/**
|
|
11
|
+
* Create a template in the web app table.
|
|
12
|
+
* @param headers Request headers.
|
|
13
|
+
* @param session Session object.
|
|
14
|
+
* @param body DischargeHistory data to be created.
|
|
15
|
+
* @returns A Promise of AxiosUtilsResponse<DischargeHistory>.
|
|
16
|
+
*/
|
|
17
|
+
export declare function createDischargeHistory(headers: any, session: Session, body: DischargeHistory): Promise<AxiosUtilsResponse<WebAppTableUpdateResponse>>;
|
|
18
|
+
/**
|
|
19
|
+
* Put updateDischargeHistory from the web app table.
|
|
20
|
+
* @param headers Request headers.
|
|
21
|
+
* @param session Session object.
|
|
22
|
+
* @returns A Promise of AxiosUtilsResponse<DischargeHistory[]>.
|
|
23
|
+
*/
|
|
24
|
+
export declare function updateDischargeHistory(headers: any, session: Session, body: DischargeHistory, id_registro: string | number): Promise<AxiosUtilsResponse<WebAppTableUpdateResponse>>;
|