@zauru-sdk/utils 6.9.2 → 6.11.0

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/dist/esm/index.js CHANGED
@@ -19,6 +19,7 @@ export * from "./zauru/purchase-orders.utils.js";
19
19
  export * from "./zauru/shipments.utils.js";
20
20
  export * from "./zauru/specialItem.utils.js";
21
21
  export * from "./zauru/priceAdjustmentRules.utils.js";
22
+ export * from "./zauru/rejectionPercentageAdjustmentRules.utils.js";
22
23
  export * from "./zauru/weeklyItemPrice.utils.js";
23
24
  export * from "./zauru/templates.utils.js";
24
25
  export * from "./zauru/tiposMuestra.utils.js";
@@ -1,5 +1,6 @@
1
- import { CURRENCY_PREFIX, calculatePurchaseOrderFinancials, getNewDateByFormat, getStringFullDate, getZauruDateByText, handlePossibleAxiosErrors, setRejectionPercentage, } from "@zauru-sdk/common";
1
+ import { CURRENCY_PREFIX, applyRejectionPercentageRulesToFinancials, calculatePurchaseOrderFinancials, getNewDateByFormat, getStringFullDate, getZauruDateByText, handlePossibleAxiosErrors, setRejectionPercentage, } from "@zauru-sdk/common";
2
2
  import { commitSession, createNewPurchaseOrder, deleteDelivery, deletePurchaseOrder, deleteReception, getLotesWithPurchaseFormated, getPurchase, getPurchaseOrder, getPurchasesListDataTables, getVariablesByName, inactivarLote, updateReceivedPurchaseOrder, } from "@zauru-sdk/services";
3
+ import { getActiveRejectionPercentageAdjustmentRules } from "./rejectionPercentageAdjustmentRules.utils.js";
3
4
  /**
4
5
  * Obtiene el listado de ordenes de compra, formateado especialmente para armar la tabla de edición de porcentajes y tolerancia
5
6
  * @param headers
@@ -188,6 +189,23 @@ const extractPurchaseOrderFinancialDetails = (purchase) => {
188
189
  }
189
190
  return [];
190
191
  };
192
+ const extractPurchaseOrderItemIds = (purchase) => {
193
+ if (purchase == null || typeof purchase !== "object")
194
+ return [];
195
+ const record = purchase;
196
+ const raw = record.purchase_order_details ??
197
+ record.purchase_order_details_attributes ??
198
+ record.details ??
199
+ [];
200
+ const list = Array.isArray(raw)
201
+ ? raw
202
+ : raw && typeof raw === "object"
203
+ ? Object.values(raw)
204
+ : [];
205
+ return list
206
+ .map((detail) => Number(detail.item_id))
207
+ .filter((itemId) => Number.isFinite(itemId));
208
+ };
191
209
  /**
192
210
  * updateOchAndDis
193
211
  * Updates rejectionPercentage in purchase_order.memo and the monetary
@@ -199,8 +217,10 @@ export const updateOchAndDis = async (headers, data, purchase_id, session) => {
199
217
  const rejectionPercentage = Number(data?.rejectionPercentage ?? data?.discount ?? 0);
200
218
  let currentMemo = data.memo;
201
219
  let details = [];
220
+ let loadedPurchase;
202
221
  const restPurchaseResponse = await getPurchase(headers, purchase_id);
203
222
  if (!restPurchaseResponse.error && restPurchaseResponse.data) {
223
+ loadedPurchase = restPurchaseResponse.data;
204
224
  if (currentMemo === undefined) {
205
225
  currentMemo = restPurchaseResponse.data.memo;
206
226
  }
@@ -209,7 +229,7 @@ export const updateOchAndDis = async (headers, data, purchase_id, session) => {
209
229
  if ((currentMemo === undefined || details.length === 0) && session) {
210
230
  const purchaseOrderResponse = await getPurchaseOrder(session, purchase_id, {
211
231
  withLotStocksToMyAgency: false,
212
- withPayee: false,
232
+ withPayee: true,
213
233
  withReceptions: false,
214
234
  });
215
235
  if (purchaseOrderResponse.error || !purchaseOrderResponse.data) {
@@ -222,6 +242,9 @@ export const updateOchAndDis = async (headers, data, purchase_id, session) => {
222
242
  if (details.length === 0) {
223
243
  details = extractPurchaseOrderFinancialDetails(purchaseOrderResponse.data);
224
244
  }
245
+ if (loadedPurchase == null) {
246
+ loadedPurchase = purchaseOrderResponse.data;
247
+ }
225
248
  }
226
249
  if (currentMemo === undefined) {
227
250
  throw new Error("memo or session is required to update rejectionPercentage");
@@ -230,14 +253,37 @@ export const updateOchAndDis = async (headers, data, purchase_id, session) => {
230
253
  throw new Error(restPurchaseResponse.userMsg ||
231
254
  "No se pudieron obtener los detalles de la orden de compra para calcular el descuento");
232
255
  }
233
- const { discount } = calculatePurchaseOrderFinancials({
234
- details,
235
- rejectionPercentage,
236
- });
256
+ const rules = await getActiveRejectionPercentageAdjustmentRules(headers, session);
257
+ const purchaseRecord = loadedPurchase;
258
+ const payee = purchaseRecord?.payee;
259
+ const itemIds = extractPurchaseOrderItemIds(loadedPurchase);
260
+ const tipo = purchaseRecord?.reference;
261
+ const adjusted = rules.length > 0
262
+ ? applyRejectionPercentageRulesToFinancials({
263
+ memo: currentMemo,
264
+ originPercentage: rejectionPercentage,
265
+ details,
266
+ rules,
267
+ ctx: {
268
+ itemIds,
269
+ tipo,
270
+ providerCategoryId: payee?.payee_category_id ??
271
+ purchaseRecord
272
+ ?.payee_category_id,
273
+ },
274
+ })
275
+ : {
276
+ memo: setRejectionPercentage(currentMemo, rejectionPercentage),
277
+ discount: calculatePurchaseOrderFinancials({
278
+ details,
279
+ rejectionPercentage,
280
+ }).discount,
281
+ finalPercentage: rejectionPercentage,
282
+ };
237
283
  const body = {
238
284
  purchase_order: {
239
- memo: setRejectionPercentage(currentMemo, rejectionPercentage),
240
- discount,
285
+ memo: adjusted.memo,
286
+ discount: adjusted.discount,
241
287
  },
242
288
  };
243
289
  if (data.other_charges !== undefined && data.other_charges !== null) {
@@ -0,0 +1,51 @@
1
+ import { handlePossibleAxiosErrors } from "@zauru-sdk/common";
2
+ import { createWebAppTableRegister, getRejectionPercentageAdjustmentRulesByHeaders, getVariablesByName, getWebAppTableRegisters, updateWebAppTableRegister, } from "@zauru-sdk/services";
3
+ export { applyRejectionPercentageAdjustmentRules, applyRejectionPercentageRulesToFinancials, filterActiveRejectionPercentageAdjustmentRules, formatRejectionPercentageAdjustmentDescription, rejectionPercentageAdjustmentRuleMatches, resolveRejectionPercentageBase, resolveRejectionPercentageOrigin, REJECTION_PERCENTAGE_ADJUSTMENT_RULES_TABLE_VAR, } from "@zauru-sdk/common";
4
+ import { filterActiveRejectionPercentageAdjustmentRules, REJECTION_PERCENTAGE_ADJUSTMENT_RULES_TABLE_VAR, } from "@zauru-sdk/common";
5
+ const getTableId = async (headers, session) => {
6
+ const vars = await getVariablesByName(headers, session, [
7
+ REJECTION_PERCENTAGE_ADJUSTMENT_RULES_TABLE_VAR,
8
+ ]);
9
+ return vars[REJECTION_PERCENTAGE_ADJUSTMENT_RULES_TABLE_VAR];
10
+ };
11
+ export const getRejectionPercentageAdjustmentRules = (headers, session) => {
12
+ return handlePossibleAxiosErrors(async () => {
13
+ const tableId = await getTableId(headers, session);
14
+ const response = await getWebAppTableRegisters(session, tableId);
15
+ if (response.error) {
16
+ throw new Error(`Ocurrió un error al consultar las reglas de ajuste de porcentaje de rechazo: ${response.userMsg}`);
17
+ }
18
+ return response.data ?? [];
19
+ });
20
+ };
21
+ export const getActiveRejectionPercentageAdjustmentRules = async (headers, session) => {
22
+ try {
23
+ if (session) {
24
+ const response = await getRejectionPercentageAdjustmentRules(headers, session);
25
+ if (response.error) {
26
+ return [];
27
+ }
28
+ return filterActiveRejectionPercentageAdjustmentRules(response.data);
29
+ }
30
+ const response = await getRejectionPercentageAdjustmentRulesByHeaders(headers);
31
+ if (response.error) {
32
+ return [];
33
+ }
34
+ return filterActiveRejectionPercentageAdjustmentRules(response.data);
35
+ }
36
+ catch {
37
+ return [];
38
+ }
39
+ };
40
+ export const createRejectionPercentageAdjustmentRule = (headers, session, body) => {
41
+ return handlePossibleAxiosErrors(async () => {
42
+ const tableId = await getTableId(headers, session);
43
+ return createWebAppTableRegister(headers, tableId, body);
44
+ });
45
+ };
46
+ export const updateRejectionPercentageAdjustmentRule = (headers, session, id, body) => {
47
+ return handlePossibleAxiosErrors(async () => {
48
+ const tableId = await getTableId(headers, session);
49
+ return updateWebAppTableRegister(headers, tableId, Number(id), body);
50
+ });
51
+ };
package/dist/index.d.ts CHANGED
@@ -19,6 +19,7 @@ export * from "./zauru/purchase-orders.utils.js";
19
19
  export * from "./zauru/shipments.utils.js";
20
20
  export * from "./zauru/specialItem.utils.js";
21
21
  export * from "./zauru/priceAdjustmentRules.utils.js";
22
+ export * from "./zauru/rejectionPercentageAdjustmentRules.utils.js";
22
23
  export * from "./zauru/weeklyItemPrice.utils.js";
23
24
  export * from "./zauru/templates.utils.js";
24
25
  export * from "./zauru/tiposMuestra.utils.js";
@@ -0,0 +1,7 @@
1
+ import type { Session } from "@remix-run/node";
2
+ import { AxiosUtilsResponse, RejectionPercentageAdjustmentRule, WebAppRowGraphQL, WebAppTableUpdateResponse } from "@zauru-sdk/types";
3
+ export { applyRejectionPercentageAdjustmentRules, applyRejectionPercentageRulesToFinancials, filterActiveRejectionPercentageAdjustmentRules, formatRejectionPercentageAdjustmentDescription, rejectionPercentageAdjustmentRuleMatches, resolveRejectionPercentageBase, resolveRejectionPercentageOrigin, REJECTION_PERCENTAGE_ADJUSTMENT_RULES_TABLE_VAR, } from "@zauru-sdk/common";
4
+ export declare const getRejectionPercentageAdjustmentRules: (headers: any, session: Session) => Promise<AxiosUtilsResponse<WebAppRowGraphQL<RejectionPercentageAdjustmentRule>[]>>;
5
+ export declare const getActiveRejectionPercentageAdjustmentRules: (headers: any, session?: Session) => Promise<WebAppRowGraphQL<RejectionPercentageAdjustmentRule>[]>;
6
+ export declare const createRejectionPercentageAdjustmentRule: (headers: any, session: Session, body: RejectionPercentageAdjustmentRule) => Promise<AxiosUtilsResponse<WebAppTableUpdateResponse>>;
7
+ export declare const updateRejectionPercentageAdjustmentRule: (headers: any, session: Session, id: string, body: Partial<RejectionPercentageAdjustmentRule>) => Promise<AxiosUtilsResponse<WebAppTableUpdateResponse>>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zauru-sdk/utils",
3
- "version": "6.9.2",
3
+ "version": "6.11.0",
4
4
  "description": "Utilidades, parseo de request y demás para desarrollo de webapps de Zauru",
5
5
  "main": "./dist/esm/index.js",
6
6
  "module": "./dist/esm/index.js",
@@ -25,12 +25,12 @@
25
25
  },
26
26
  "dependencies": {
27
27
  "@remix-run/node": "^2.8.1",
28
- "@zauru-sdk/common": "^6.4.0",
28
+ "@zauru-sdk/common": "^6.5.0",
29
29
  "@zauru-sdk/config": "^6.0.0",
30
30
  "@zauru-sdk/graphql": "^6.7.0",
31
- "@zauru-sdk/services": "^6.9.0",
32
- "@zauru-sdk/types": "^6.14.0",
31
+ "@zauru-sdk/services": "^6.10.0",
32
+ "@zauru-sdk/types": "^6.15.0",
33
33
  "moment": "^2.30.1"
34
34
  },
35
- "gitHead": "e779da25a020897d79e667a4f30f747b69fb807f"
35
+ "gitHead": "2dceb781cae3f4c45ceecb915977c6d142bca6b9"
36
36
  }