@zauru-sdk/services 1.0.6 → 1.0.7
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 +8 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +20 -0
- package/dist/zauru/zauru-currencies.server.d.ts +6 -0
- package/dist/zauru/zauru-currencies.server.js +20 -0
- package/dist/zauru/zauru-deliveries.server.d.ts +22 -0
- package/dist/zauru/zauru-deliveries.server.js +44 -0
- package/dist/zauru/zauru-discharges.server.d.ts +26 -0
- package/dist/zauru/zauru-discharges.server.js +46 -0
- package/dist/zauru/zauru-employees.server.d.ts +6 -0
- package/dist/zauru/zauru-employees.server.js +23 -0
- package/dist/zauru/zauru-forms.server.d.ts +96 -0
- package/dist/zauru/zauru-forms.server.js +343 -0
- package/dist/zauru/zauru-invoices.server.d.ts +26 -0
- package/dist/zauru/zauru-invoices.server.js +94 -0
- package/dist/zauru/zauru-items.server.d.ts +91 -0
- package/dist/zauru/zauru-items.server.js +238 -0
- package/dist/zauru/zauru-lote-record.server.d.ts +8 -0
- package/dist/zauru/zauru-lote-record.server.js +26 -0
- package/dist/zauru/zauru-lotes.server.d.ts +91 -0
- package/dist/zauru/zauru-lotes.server.js +294 -0
- package/dist/zauru/zauru-motivos-rechazo.server.d.ts +9 -0
- package/dist/zauru/zauru-motivos-rechazo.server.js +49 -0
- package/dist/zauru/zauru-payees.server.d.ts +97 -0
- package/dist/zauru/zauru-payees.server.js +227 -0
- package/dist/zauru/zauru-payment-terms.server.d.ts +20 -0
- package/dist/zauru/zauru-payment-terms.server.js +65 -0
- package/dist/zauru/zauru-price-lists.server.d.ts +22 -0
- package/dist/zauru/zauru-price-lists.server.js +40 -0
- package/dist/zauru/zauru-purchase-orders.server.d.ts +160 -0
- package/dist/zauru/zauru-purchase-orders.server.js +414 -0
- package/dist/zauru/zauru-receptions.server.d.ts +33 -0
- package/dist/zauru/zauru-receptions.server.js +94 -0
- package/dist/zauru/zauru-shipments.server.d.ts +20 -0
- package/dist/zauru/zauru-shipments.server.js +53 -0
- package/dist/zauru/zauru-suggested-prices.server.d.ts +30 -0
- package/dist/zauru/zauru-suggested-prices.server.js +83 -0
- package/dist/zauru/zauru-tags.server.d.ts +7 -0
- package/dist/zauru/zauru-tags.server.js +13 -0
- package/dist/zauru/zauru-templates.server.d.ts +7 -0
- package/dist/zauru/zauru-templates.server.js +15 -0
- package/dist/zauru/zauru-web-app-tables.server.d.ts +48 -0
- package/dist/zauru/zauru-web-app-tables.server.js +120 -0
- package/package.json +6 -6
- package/src/index.ts +20 -0
- package/src/zauru/zauru-currencies.server.ts +39 -0
- package/src/zauru/zauru-deliveries.server.ts +70 -0
- package/src/zauru/zauru-discharges.server.ts +90 -0
- package/src/zauru/zauru-employees.server.ts +43 -0
- package/src/zauru/zauru-forms.server.ts +618 -0
- package/src/zauru/zauru-invoices.server.ts +142 -0
- package/src/zauru/zauru-items.server.ts +427 -0
- package/src/zauru/zauru-lote-record.server.ts +77 -0
- package/src/zauru/zauru-lotes.server.ts +484 -0
- package/src/zauru/zauru-motivos-rechazo.server.ts +121 -0
- package/src/zauru/zauru-payees.server.ts +398 -0
- package/src/zauru/zauru-payment-terms.server.ts +126 -0
- package/src/zauru/zauru-price-lists.server.ts +68 -0
- package/src/zauru/zauru-purchase-orders.server.ts +701 -0
- package/src/zauru/zauru-receptions.server.ts +148 -0
- package/src/zauru/zauru-shipments.server.ts +88 -0
- package/src/zauru/zauru-suggested-prices.server.ts +134 -0
- package/src/zauru/zauru-tags.server.ts +22 -0
- package/src/zauru/zauru-templates.server.ts +24 -0
- package/src/zauru/zauru-web-app-tables.server.ts +200 -0
|
@@ -0,0 +1,701 @@
|
|
|
1
|
+
import type { Session } from "@remix-run/node";
|
|
2
|
+
import {
|
|
3
|
+
arrayToObject,
|
|
4
|
+
convertToFormData,
|
|
5
|
+
formatDateToUTC,
|
|
6
|
+
getBasketsSchema,
|
|
7
|
+
handlePossibleAxiosErrors,
|
|
8
|
+
reduceAdd,
|
|
9
|
+
} from "@zauru-sdk/common";
|
|
10
|
+
import {
|
|
11
|
+
AxiosUtilsResponse,
|
|
12
|
+
BasketSchema,
|
|
13
|
+
CreateNewPurchaseOrderBody,
|
|
14
|
+
DataTablesFilterBody,
|
|
15
|
+
HTMLPurchasesListSchema,
|
|
16
|
+
NewPurchaseOrderResponse,
|
|
17
|
+
PurchaseOrderGraphQL,
|
|
18
|
+
PurchasesListResponseSchema,
|
|
19
|
+
SelectFieldOption,
|
|
20
|
+
UpdatePurchaseOrderBody,
|
|
21
|
+
} from "@zauru-sdk/types";
|
|
22
|
+
import {
|
|
23
|
+
createNewLotPurchaseOrderReception,
|
|
24
|
+
createNewPurchaseOrderReception,
|
|
25
|
+
} from "./zauru-receptions.server.js";
|
|
26
|
+
import httpZauru from "./httpZauru.server.js";
|
|
27
|
+
import { getGraphQLAPIHeaders, getVariablesByName } from "~/common.server.js";
|
|
28
|
+
import httpGraphQLAPI from "./httpGraphQL.server.js";
|
|
29
|
+
import {
|
|
30
|
+
getLast100ReceptionsStringQuery,
|
|
31
|
+
getPurchaseOrderByIdNumberStringQuery,
|
|
32
|
+
getPurchaseOrderStringQuery,
|
|
33
|
+
getPurchaseOrdersBetweenDatesStringQuery,
|
|
34
|
+
} from "@zauru-sdk/graphql";
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* markAsReceivePurchaseOrder
|
|
38
|
+
* Esta función sólo se utiliza cuando se van a marcar como recibida toda la órden de compra, se recibe todo lo que se envío
|
|
39
|
+
* si se quiere recibir parcialmente, utilizar el endpoint de /receptions
|
|
40
|
+
* @param headers
|
|
41
|
+
* @param body
|
|
42
|
+
* @returns
|
|
43
|
+
*/
|
|
44
|
+
export async function markAsReceivePurchaseOrder(
|
|
45
|
+
headers: any,
|
|
46
|
+
body: Partial<PurchaseOrderGraphQL> & { fechaVencimiento?: string }
|
|
47
|
+
): Promise<AxiosUtilsResponse<boolean>> {
|
|
48
|
+
return handlePossibleAxiosErrors(async () => {
|
|
49
|
+
const sendBody = { ...body };
|
|
50
|
+
|
|
51
|
+
if (sendBody.fechaVencimiento) {
|
|
52
|
+
await receiveLotPurchaseOrder(headers, sendBody as any);
|
|
53
|
+
} else {
|
|
54
|
+
await receivePurchaseOrder(headers, sendBody as any);
|
|
55
|
+
}
|
|
56
|
+
return true;
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* markAsReceivePartialPurchaseOrder
|
|
62
|
+
* @param headers
|
|
63
|
+
* @param body
|
|
64
|
+
* @returns
|
|
65
|
+
*/
|
|
66
|
+
export async function markAsReceivePartialPurchaseOrder(
|
|
67
|
+
headers: any,
|
|
68
|
+
session: Session,
|
|
69
|
+
body: Partial<PurchaseOrderGraphQL> & { fechaVencimiento?: string }
|
|
70
|
+
): Promise<AxiosUtilsResponse<boolean>> {
|
|
71
|
+
return handlePossibleAxiosErrors(async () => {
|
|
72
|
+
const sendBody = { ...body };
|
|
73
|
+
|
|
74
|
+
if (sendBody.fechaVencimiento) {
|
|
75
|
+
await createNewLotPurchaseOrderReception(
|
|
76
|
+
headers,
|
|
77
|
+
session,
|
|
78
|
+
sendBody as any
|
|
79
|
+
);
|
|
80
|
+
} else {
|
|
81
|
+
await createNewPurchaseOrderReception(headers, session, body);
|
|
82
|
+
}
|
|
83
|
+
return true;
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
*
|
|
89
|
+
* @param headers
|
|
90
|
+
* @param body
|
|
91
|
+
* @returns
|
|
92
|
+
*/
|
|
93
|
+
export async function createNewPurchaseOrder(
|
|
94
|
+
headers: any,
|
|
95
|
+
body: Partial<PurchaseOrderGraphQL>
|
|
96
|
+
): Promise<AxiosUtilsResponse<PurchaseOrderGraphQL>> {
|
|
97
|
+
return handlePossibleAxiosErrors(async () => {
|
|
98
|
+
let sendBody = {
|
|
99
|
+
...body,
|
|
100
|
+
purchase_order_details_attributes: arrayToObject(
|
|
101
|
+
body?.purchase_order_details
|
|
102
|
+
),
|
|
103
|
+
tag_ids: [
|
|
104
|
+
"",
|
|
105
|
+
...(body?.taggings?.map((x) => x.tag_id?.toString()) ?? []),
|
|
106
|
+
],
|
|
107
|
+
} as any;
|
|
108
|
+
|
|
109
|
+
delete headers["Content-type"];
|
|
110
|
+
delete sendBody.__rvfInternalFormId;
|
|
111
|
+
delete sendBody.taggings;
|
|
112
|
+
delete sendBody.purchase_order_details;
|
|
113
|
+
|
|
114
|
+
if (!(sendBody?.pdf instanceof File) || sendBody.pdf?.size <= 0) {
|
|
115
|
+
delete sendBody.pdf;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
sendBody = convertToFormData({
|
|
119
|
+
purchase_order: sendBody,
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
const response = await httpZauru.post<PurchaseOrderGraphQL>(
|
|
123
|
+
`/purchases/purchase_orders.json`,
|
|
124
|
+
sendBody,
|
|
125
|
+
{ headers }
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
return response.data;
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
*
|
|
134
|
+
* @param headers
|
|
135
|
+
* @param body
|
|
136
|
+
* @returns
|
|
137
|
+
*/
|
|
138
|
+
export async function createNewAuthorizedPurchaseOrder(
|
|
139
|
+
headers: any,
|
|
140
|
+
body: CreateNewPurchaseOrderBody,
|
|
141
|
+
withReceive: boolean = true
|
|
142
|
+
): Promise<AxiosUtilsResponse<PurchaseOrderGraphQL>> {
|
|
143
|
+
return handlePossibleAxiosErrors(async () => {
|
|
144
|
+
const sendBody = {
|
|
145
|
+
...body,
|
|
146
|
+
purchase_order_details_attributes: arrayToObject(
|
|
147
|
+
body?.purchase_order_details
|
|
148
|
+
),
|
|
149
|
+
} as any;
|
|
150
|
+
|
|
151
|
+
const response = await httpZauru.post<PurchaseOrderGraphQL>(
|
|
152
|
+
`/purchases/purchase_orders.json`,
|
|
153
|
+
{ purchase_order: sendBody },
|
|
154
|
+
{ headers }
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
await httpZauru.get<any>(
|
|
158
|
+
`/purchases/purchase_orders/${response.data.id}/authorize.json`,
|
|
159
|
+
{ headers }
|
|
160
|
+
);
|
|
161
|
+
|
|
162
|
+
if (withReceive) {
|
|
163
|
+
if (sendBody.fechaVencimiento) {
|
|
164
|
+
await receiveLotPurchaseOrder(headers, response.data as any);
|
|
165
|
+
} else {
|
|
166
|
+
await receivePurchaseOrder(headers, response.data);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return response.data;
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* receiveLotPurchaseOrder
|
|
176
|
+
* @param headers
|
|
177
|
+
* @param body
|
|
178
|
+
* @returns
|
|
179
|
+
*/
|
|
180
|
+
export async function receiveLotPurchaseOrder(
|
|
181
|
+
headers: any,
|
|
182
|
+
body: PurchaseOrderGraphQL & { fechaVencimiento: string }
|
|
183
|
+
): Promise<AxiosUtilsResponse<boolean>> {
|
|
184
|
+
return handlePossibleAxiosErrors(async () => {
|
|
185
|
+
const sendBody = {
|
|
186
|
+
agency_id: body.agency_id,
|
|
187
|
+
delivery_date: body.issue_date,
|
|
188
|
+
id: body.id,
|
|
189
|
+
needs_transit: false,
|
|
190
|
+
purchase_order_details_attributes: arrayToObject(
|
|
191
|
+
body.purchase_order_details?.map((x: any) => {
|
|
192
|
+
const newDetail = {
|
|
193
|
+
id: x.id,
|
|
194
|
+
booked_quantity: x.booked_quantity,
|
|
195
|
+
item_id: x.item_id,
|
|
196
|
+
lot_delivered_quantity: [x.delivered_quantity],
|
|
197
|
+
lot_name: [body.id_number],
|
|
198
|
+
lot_expire: [body.fechaVencimiento],
|
|
199
|
+
} as any;
|
|
200
|
+
return newDetail;
|
|
201
|
+
})
|
|
202
|
+
),
|
|
203
|
+
};
|
|
204
|
+
await httpZauru.patch<any>(
|
|
205
|
+
`/purchases/purchase_orders/${body.id}/receive_action`,
|
|
206
|
+
{ purchase_order: sendBody },
|
|
207
|
+
{ headers }
|
|
208
|
+
);
|
|
209
|
+
return true;
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* receivePurchaseOrder
|
|
215
|
+
* @param headers
|
|
216
|
+
* @param body
|
|
217
|
+
* @returns
|
|
218
|
+
*/
|
|
219
|
+
export async function receivePurchaseOrder(
|
|
220
|
+
headers: any,
|
|
221
|
+
body: PurchaseOrderGraphQL
|
|
222
|
+
): Promise<AxiosUtilsResponse<boolean>> {
|
|
223
|
+
return handlePossibleAxiosErrors(async () => {
|
|
224
|
+
const sendBody = {
|
|
225
|
+
agency_id: body.agency_id,
|
|
226
|
+
delivery_date: body.issue_date,
|
|
227
|
+
id: body.id,
|
|
228
|
+
needs_transit: false,
|
|
229
|
+
purchase_order_details_attributes: arrayToObject(
|
|
230
|
+
body.purchase_order_details?.map((x: any) => {
|
|
231
|
+
const newDetail = {
|
|
232
|
+
id: x.id,
|
|
233
|
+
delivered_quantity: x.delivered_quantity,
|
|
234
|
+
booked_quantity: x.booked_quantity,
|
|
235
|
+
item_id: x.item_id,
|
|
236
|
+
} as any;
|
|
237
|
+
return newDetail;
|
|
238
|
+
})
|
|
239
|
+
),
|
|
240
|
+
};
|
|
241
|
+
await httpZauru.patch<any>(
|
|
242
|
+
`/purchases/purchase_orders/${body.id}/receive_action`,
|
|
243
|
+
{ purchase_order: sendBody },
|
|
244
|
+
{ headers }
|
|
245
|
+
);
|
|
246
|
+
return true;
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
*
|
|
252
|
+
* @param headers
|
|
253
|
+
* @returns
|
|
254
|
+
*/
|
|
255
|
+
export async function getNewPurchaseOrderInfo(
|
|
256
|
+
headers: any
|
|
257
|
+
): Promise<AxiosUtilsResponse<NewPurchaseOrderResponse>> {
|
|
258
|
+
return handlePossibleAxiosErrors(async () => {
|
|
259
|
+
const response = await httpZauru.get<NewPurchaseOrderResponse>(
|
|
260
|
+
"/purchases/purchase_orders/new.json",
|
|
261
|
+
{ headers }
|
|
262
|
+
);
|
|
263
|
+
|
|
264
|
+
const purchasersList: SelectFieldOption[] = [];
|
|
265
|
+
response.data?.purchasers?.forEach((purchaser: any) => {
|
|
266
|
+
if (purchaser?.id) {
|
|
267
|
+
purchasersList.push({
|
|
268
|
+
value: purchaser?.id,
|
|
269
|
+
label: purchaser?.name,
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
return { ...response.data, purchasersList } as NewPurchaseOrderResponse;
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* getPurchasesListDataTables Function for get all zauru orden-compras
|
|
280
|
+
* @param headers
|
|
281
|
+
* @returns
|
|
282
|
+
*/
|
|
283
|
+
export async function getPurchasesListDataTables(
|
|
284
|
+
headers: any,
|
|
285
|
+
body: DataTablesFilterBody
|
|
286
|
+
): Promise<
|
|
287
|
+
AxiosUtilsResponse<PurchasesListResponseSchema<HTMLPurchasesListSchema>>
|
|
288
|
+
> {
|
|
289
|
+
return handlePossibleAxiosErrors(async () => {
|
|
290
|
+
const response = await httpZauru.post<
|
|
291
|
+
PurchasesListResponseSchema<HTMLPurchasesListSchema>
|
|
292
|
+
>(`/purchases/purchase_orders/datatables.json`, body, { headers });
|
|
293
|
+
|
|
294
|
+
return response.data;
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* getPurchasesList Function for get all zauru orden-compras
|
|
300
|
+
* @param headers
|
|
301
|
+
* @param params
|
|
302
|
+
* @returns
|
|
303
|
+
*/
|
|
304
|
+
export async function getPurchasesList(
|
|
305
|
+
headers: any,
|
|
306
|
+
session: Session,
|
|
307
|
+
params: {
|
|
308
|
+
fechaInicio?: string;
|
|
309
|
+
fechaFin?: string;
|
|
310
|
+
item?: string | number;
|
|
311
|
+
payeeCategory?: string | number;
|
|
312
|
+
agency_id?: string | number;
|
|
313
|
+
} = {},
|
|
314
|
+
config?: { fromProduction?: boolean }
|
|
315
|
+
): Promise<AxiosUtilsResponse<PurchaseOrderGraphQL[]>> {
|
|
316
|
+
return handlePossibleAxiosErrors(async () => {
|
|
317
|
+
//Si quiere obtener los de producción, obtengo primero su agencia producción
|
|
318
|
+
if (config?.fromProduction) {
|
|
319
|
+
const { production_agency_id: temp_production_agency_id } =
|
|
320
|
+
await getVariablesByName(headers, session, ["production_agency_id"]);
|
|
321
|
+
const hashProductionAgencyId = JSON.parse(
|
|
322
|
+
temp_production_agency_id ?? "{}"
|
|
323
|
+
);
|
|
324
|
+
const production_agency_id =
|
|
325
|
+
hashProductionAgencyId[session.get("agency_id")];
|
|
326
|
+
params.agency_id = production_agency_id;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
const response = await httpZauru.get<PurchaseOrderGraphQL[]>(
|
|
330
|
+
`/purchases/purchase_orders/export.json`,
|
|
331
|
+
{
|
|
332
|
+
headers,
|
|
333
|
+
params,
|
|
334
|
+
}
|
|
335
|
+
);
|
|
336
|
+
|
|
337
|
+
return response.data;
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* getPurchase Function for get an especific purchase order
|
|
343
|
+
* @param headers
|
|
344
|
+
* @returns
|
|
345
|
+
*/
|
|
346
|
+
export async function getPurchase(
|
|
347
|
+
headers: any,
|
|
348
|
+
purchase_order_id: number | string
|
|
349
|
+
): Promise<
|
|
350
|
+
AxiosUtilsResponse<
|
|
351
|
+
PurchaseOrderGraphQL & {
|
|
352
|
+
baskets_memo: BasketSchema[];
|
|
353
|
+
baskets_memo_quantity: number;
|
|
354
|
+
}
|
|
355
|
+
>
|
|
356
|
+
> {
|
|
357
|
+
return handlePossibleAxiosErrors(async () => {
|
|
358
|
+
const response = await httpZauru<PurchaseOrderGraphQL>(
|
|
359
|
+
`/purchases/purchase_orders/${purchase_order_id}.json`,
|
|
360
|
+
{ method: "GET", headers }
|
|
361
|
+
);
|
|
362
|
+
|
|
363
|
+
const baskets_memo = getBasketsSchema(response.data.memo);
|
|
364
|
+
const baskets_memo_quantity = baskets_memo
|
|
365
|
+
.map((basket) => basket.total)
|
|
366
|
+
.reduce(reduceAdd, 0);
|
|
367
|
+
|
|
368
|
+
return {
|
|
369
|
+
...response.data,
|
|
370
|
+
baskets_memo,
|
|
371
|
+
baskets_memo_quantity,
|
|
372
|
+
};
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* enablePurchase Enable a purchase order
|
|
378
|
+
* @param headers
|
|
379
|
+
* @param purchase_order_id
|
|
380
|
+
* @param reception_id
|
|
381
|
+
* @returns
|
|
382
|
+
*/
|
|
383
|
+
export async function enablePurchase(
|
|
384
|
+
headers: any,
|
|
385
|
+
purchase_order_id: number,
|
|
386
|
+
reception_id: number
|
|
387
|
+
): Promise<AxiosUtilsResponse<any>> {
|
|
388
|
+
return handlePossibleAxiosErrors(async () => {
|
|
389
|
+
const response = await httpZauru.get<any>(
|
|
390
|
+
`/purchases/receptions/${reception_id}/rebound.json?purchase_order_id=${purchase_order_id}`,
|
|
391
|
+
{ headers }
|
|
392
|
+
);
|
|
393
|
+
|
|
394
|
+
return response.data;
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* updatePurchaseOrder
|
|
400
|
+
* @param headers
|
|
401
|
+
* @param body
|
|
402
|
+
* @param purchase_order_id
|
|
403
|
+
* @returns
|
|
404
|
+
*/
|
|
405
|
+
export async function updatePurchaseOrder(
|
|
406
|
+
headers: any,
|
|
407
|
+
body: UpdatePurchaseOrderBody,
|
|
408
|
+
purchase_order_id: number
|
|
409
|
+
): Promise<AxiosUtilsResponse<any>> {
|
|
410
|
+
return handlePossibleAxiosErrors(async () => {
|
|
411
|
+
const response = await httpZauru.patch<any>(
|
|
412
|
+
`/purchases/purchase_orders/${purchase_order_id}.json`,
|
|
413
|
+
body,
|
|
414
|
+
{ headers }
|
|
415
|
+
);
|
|
416
|
+
|
|
417
|
+
return response.data;
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
* updatePurchaseOrder
|
|
423
|
+
* @param headers
|
|
424
|
+
* @param body
|
|
425
|
+
* @param purchase_order_id
|
|
426
|
+
* @returns
|
|
427
|
+
*/
|
|
428
|
+
export async function updateReceivedPurchaseOrder(
|
|
429
|
+
headers: any,
|
|
430
|
+
body: UpdatePurchaseOrderBody,
|
|
431
|
+
purchase_order_id: number
|
|
432
|
+
): Promise<AxiosUtilsResponse<any>> {
|
|
433
|
+
return handlePossibleAxiosErrors(async () => {
|
|
434
|
+
const response = await httpZauru.patch<any>(
|
|
435
|
+
`/purchases/purchase_orders/${purchase_order_id}/update_received.json`,
|
|
436
|
+
body,
|
|
437
|
+
{ headers }
|
|
438
|
+
);
|
|
439
|
+
|
|
440
|
+
return response.data;
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* getLast100Receptions
|
|
446
|
+
* @param headers
|
|
447
|
+
* @returns
|
|
448
|
+
*/
|
|
449
|
+
export async function getLast100Receptions(
|
|
450
|
+
session: Session,
|
|
451
|
+
agency_id?: number | string
|
|
452
|
+
): Promise<AxiosUtilsResponse<PurchaseOrderGraphQL[]>> {
|
|
453
|
+
return handlePossibleAxiosErrors(async () => {
|
|
454
|
+
const headers = await getGraphQLAPIHeaders(session);
|
|
455
|
+
|
|
456
|
+
const response = await httpGraphQLAPI.post<{
|
|
457
|
+
data: { purchase_orders: PurchaseOrderGraphQL[] };
|
|
458
|
+
errors?: {
|
|
459
|
+
message: string;
|
|
460
|
+
extensions: { path: string; code: string };
|
|
461
|
+
}[];
|
|
462
|
+
}>(
|
|
463
|
+
"",
|
|
464
|
+
{
|
|
465
|
+
query: getLast100ReceptionsStringQuery,
|
|
466
|
+
variables: {
|
|
467
|
+
agencyId: agency_id ?? session.get("agency_id"),
|
|
468
|
+
},
|
|
469
|
+
},
|
|
470
|
+
{ headers }
|
|
471
|
+
);
|
|
472
|
+
|
|
473
|
+
if (response.data.errors) {
|
|
474
|
+
throw new Error(response.data.errors.map((x) => x.message).join(";"));
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
return response.data?.data?.purchase_orders;
|
|
478
|
+
});
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
/**
|
|
482
|
+
* getPurchaseOrder
|
|
483
|
+
* @param headers
|
|
484
|
+
* @returns
|
|
485
|
+
*/
|
|
486
|
+
export async function getPurchaseOrder(
|
|
487
|
+
session: Session,
|
|
488
|
+
poId: string | number,
|
|
489
|
+
config: { withLotStocksToMyAgency: boolean } = {
|
|
490
|
+
withLotStocksToMyAgency: false,
|
|
491
|
+
}
|
|
492
|
+
): Promise<AxiosUtilsResponse<PurchaseOrderGraphQL>> {
|
|
493
|
+
return handlePossibleAxiosErrors(async () => {
|
|
494
|
+
const headers = await getGraphQLAPIHeaders(session);
|
|
495
|
+
|
|
496
|
+
const response = await httpGraphQLAPI.post<{
|
|
497
|
+
data: { purchase_orders: PurchaseOrderGraphQL[] };
|
|
498
|
+
errors?: {
|
|
499
|
+
message: string;
|
|
500
|
+
extensions: { path: string; code: string };
|
|
501
|
+
}[];
|
|
502
|
+
}>(
|
|
503
|
+
"",
|
|
504
|
+
{
|
|
505
|
+
query: getPurchaseOrderStringQuery({
|
|
506
|
+
withLotStocks: config.withLotStocksToMyAgency,
|
|
507
|
+
}),
|
|
508
|
+
variables: {
|
|
509
|
+
id: poId,
|
|
510
|
+
},
|
|
511
|
+
},
|
|
512
|
+
{ headers }
|
|
513
|
+
);
|
|
514
|
+
|
|
515
|
+
if (response.data.errors) {
|
|
516
|
+
throw new Error(response.data.errors.map((x) => x.message).join(";"));
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
const tempResponse = response.data?.data?.purchase_orders[0];
|
|
520
|
+
|
|
521
|
+
if (config.withLotStocksToMyAgency) {
|
|
522
|
+
tempResponse.lots[0].lot_stocks = tempResponse.lots[0].lot_stocks.filter(
|
|
523
|
+
(x) => x.agency_id === Number(session.get("agency_id"))
|
|
524
|
+
);
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
return tempResponse;
|
|
528
|
+
});
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
/**
|
|
532
|
+
* getPurchaseOrder
|
|
533
|
+
* @param headers
|
|
534
|
+
* @returns
|
|
535
|
+
*/
|
|
536
|
+
export async function getGraphQLPurchaseOrderBetweenDates(
|
|
537
|
+
session: Session,
|
|
538
|
+
dates: {
|
|
539
|
+
startDate: string;
|
|
540
|
+
endDate: string;
|
|
541
|
+
},
|
|
542
|
+
config: {
|
|
543
|
+
agencyFilter: boolean;
|
|
544
|
+
agencyId?: number;
|
|
545
|
+
useProductionAgencyId?: boolean;
|
|
546
|
+
consolidateIdFilter?: boolean;
|
|
547
|
+
lotItemIdExclusion?: number;
|
|
548
|
+
poDetailTagId?: number;
|
|
549
|
+
withShipmentToMyAgency?: boolean;
|
|
550
|
+
withLotStocksToMyAgency?: boolean;
|
|
551
|
+
itemId?: number | string;
|
|
552
|
+
payeeCategoryId?: number | string;
|
|
553
|
+
} = {
|
|
554
|
+
agencyFilter: true,
|
|
555
|
+
consolidateIdFilter: false,
|
|
556
|
+
useProductionAgencyId: false,
|
|
557
|
+
withShipmentToMyAgency: false,
|
|
558
|
+
withLotStocksToMyAgency: false,
|
|
559
|
+
}
|
|
560
|
+
): Promise<AxiosUtilsResponse<PurchaseOrderGraphQL[]>> {
|
|
561
|
+
return handlePossibleAxiosErrors(async () => {
|
|
562
|
+
const headers = await getGraphQLAPIHeaders(session);
|
|
563
|
+
|
|
564
|
+
let agency_id = config.agencyId;
|
|
565
|
+
|
|
566
|
+
if (config.useProductionAgencyId) {
|
|
567
|
+
const { production_agency_id /**{5693: 5696} */ } =
|
|
568
|
+
await getVariablesByName(headers, session, ["production_agency_id"]);
|
|
569
|
+
const hashAgencyId = JSON.parse(production_agency_id ?? "{}");
|
|
570
|
+
agency_id = hashAgencyId[session.get("agency_id")];
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
const query = getPurchaseOrdersBetweenDatesStringQuery({
|
|
574
|
+
agencyFilter: config.agencyFilter,
|
|
575
|
+
consolidateIdFilter: config.consolidateIdFilter,
|
|
576
|
+
lotItemIdExclusion: config.lotItemIdExclusion,
|
|
577
|
+
poDetailTagId: config.poDetailTagId,
|
|
578
|
+
withLotStocks: config.withLotStocksToMyAgency,
|
|
579
|
+
//itemId: config.itemId,
|
|
580
|
+
//payeeCategoryId: config.payeeCategoryId,
|
|
581
|
+
});
|
|
582
|
+
|
|
583
|
+
const variables = {
|
|
584
|
+
...(config.agencyFilter
|
|
585
|
+
? { agencyId: agency_id ?? session.get("agency_id") }
|
|
586
|
+
: {}),
|
|
587
|
+
...(config.lotItemIdExclusion
|
|
588
|
+
? { lotItemIdExclusion: config.lotItemIdExclusion }
|
|
589
|
+
: {}),
|
|
590
|
+
...(config.poDetailTagId ? { poDetailTagId: config.poDetailTagId } : {}),
|
|
591
|
+
startDate: formatDateToUTC(dates.startDate),
|
|
592
|
+
endDate: formatDateToUTC(dates.endDate),
|
|
593
|
+
};
|
|
594
|
+
|
|
595
|
+
const response = await httpGraphQLAPI.post<{
|
|
596
|
+
data: { purchase_orders: PurchaseOrderGraphQL[] };
|
|
597
|
+
errors?: {
|
|
598
|
+
message: string;
|
|
599
|
+
extensions: { path: string; code: string };
|
|
600
|
+
}[];
|
|
601
|
+
}>(
|
|
602
|
+
"",
|
|
603
|
+
{
|
|
604
|
+
query,
|
|
605
|
+
variables,
|
|
606
|
+
},
|
|
607
|
+
{ headers }
|
|
608
|
+
);
|
|
609
|
+
|
|
610
|
+
if (response.data.errors) {
|
|
611
|
+
throw new Error(response.data.errors.map((x) => x.message).join(";"));
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
//============ Aplicación de filtros
|
|
615
|
+
let responseData: PurchaseOrderGraphQL[] = [];
|
|
616
|
+
|
|
617
|
+
if (config.withShipmentToMyAgency) {
|
|
618
|
+
responseData = response.data?.data?.purchase_orders.filter((x) =>
|
|
619
|
+
x.shipment_purchase_orders.some(
|
|
620
|
+
(y) => y.shipment.agency_to_id?.toString() == session.get("agency_id")
|
|
621
|
+
)
|
|
622
|
+
);
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
if (config.withLotStocksToMyAgency) {
|
|
626
|
+
responseData = responseData.map((x) => {
|
|
627
|
+
x.lots = x.lots.map((y) => {
|
|
628
|
+
y.lot_stocks = y.lot_stocks.filter(
|
|
629
|
+
(z) => z.agency_id == session.get("agency_id")
|
|
630
|
+
);
|
|
631
|
+
return y;
|
|
632
|
+
});
|
|
633
|
+
return x;
|
|
634
|
+
});
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
return responseData;
|
|
638
|
+
});
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
/**
|
|
642
|
+
* deletePurchaseOrder
|
|
643
|
+
* @param headers
|
|
644
|
+
* @param id
|
|
645
|
+
* @returns
|
|
646
|
+
*/
|
|
647
|
+
export async function deletePurchaseOrder(
|
|
648
|
+
headers: any,
|
|
649
|
+
id: string | number
|
|
650
|
+
): Promise<AxiosUtilsResponse<boolean>> {
|
|
651
|
+
return handlePossibleAxiosErrors(async () => {
|
|
652
|
+
await httpZauru.delete(
|
|
653
|
+
`/purchases/purchase_orders/${id}.json?destroy=true`,
|
|
654
|
+
{ headers }
|
|
655
|
+
);
|
|
656
|
+
|
|
657
|
+
return true;
|
|
658
|
+
});
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
/**
|
|
662
|
+
* getPurchaseOrderByIdNumber
|
|
663
|
+
* @param session
|
|
664
|
+
* @param idNumber
|
|
665
|
+
* @returns
|
|
666
|
+
*/
|
|
667
|
+
export async function getPurchasesOrderByIdNumber(
|
|
668
|
+
session: Session,
|
|
669
|
+
id_number: string
|
|
670
|
+
): Promise<AxiosUtilsResponse<PurchaseOrderGraphQL[]>> {
|
|
671
|
+
return handlePossibleAxiosErrors(async () => {
|
|
672
|
+
const headers = await getGraphQLAPIHeaders(session);
|
|
673
|
+
|
|
674
|
+
const response = await httpGraphQLAPI.post<{
|
|
675
|
+
data: { purchase_orders: PurchaseOrderGraphQL[] };
|
|
676
|
+
errors?: {
|
|
677
|
+
message: string;
|
|
678
|
+
extensions: { path: string; code: string };
|
|
679
|
+
}[];
|
|
680
|
+
}>(
|
|
681
|
+
"",
|
|
682
|
+
{
|
|
683
|
+
query: getPurchaseOrderByIdNumberStringQuery,
|
|
684
|
+
variables: {
|
|
685
|
+
id_number,
|
|
686
|
+
},
|
|
687
|
+
},
|
|
688
|
+
{ headers }
|
|
689
|
+
);
|
|
690
|
+
|
|
691
|
+
if (response.data.errors) {
|
|
692
|
+
throw new Error(response.data.errors.map((x) => x.message).join(";"));
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
if (!response?.data?.data.purchase_orders) {
|
|
696
|
+
return [];
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
return response.data?.data?.purchase_orders;
|
|
700
|
+
});
|
|
701
|
+
}
|