@zauru-sdk/utils 6.9.2 → 6.10.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";
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { getRejectionPercentage, handlePossibleAxiosErrors, parseJsonMemo, } from "@zauru-sdk/common";
|
|
2
|
+
import { createWebAppTableRegister, getVariablesByName, getWebAppTableRegisters, updateWebAppTableRegister, } from "@zauru-sdk/services";
|
|
3
|
+
import { defaultPriceAdjustmentFilters, priceAdjustmentRuleMatches, } from "./priceAdjustmentRules.utils.js";
|
|
4
|
+
const TABLE_VAR = "rejection_percentage_adjustment_rules_web_app_table_id";
|
|
5
|
+
const asPriceAdjustmentRule = (rule) => ({
|
|
6
|
+
...rule,
|
|
7
|
+
tipoValor: "percentage",
|
|
8
|
+
});
|
|
9
|
+
export const filterActiveRejectionPercentageAdjustmentRules = (rules) => (rules ?? [])
|
|
10
|
+
.filter((rule) => !rule.data?.fechaEliminacion && rule.data?.activa !== false)
|
|
11
|
+
.sort((a, b) => (a.data?.prioridad ?? 0) - (b.data?.prioridad ?? 0) || a.id - b.id);
|
|
12
|
+
const getTableId = async (headers, session) => {
|
|
13
|
+
const vars = await getVariablesByName(headers, session, [TABLE_VAR]);
|
|
14
|
+
return vars[TABLE_VAR];
|
|
15
|
+
};
|
|
16
|
+
export const getRejectionPercentageAdjustmentRules = (headers, session) => {
|
|
17
|
+
return handlePossibleAxiosErrors(async () => {
|
|
18
|
+
const tableId = await getTableId(headers, session);
|
|
19
|
+
const response = await getWebAppTableRegisters(session, tableId);
|
|
20
|
+
if (response.error) {
|
|
21
|
+
throw new Error(`Ocurrió un error al consultar las reglas de ajuste de porcentaje de rechazo: ${response.userMsg}`);
|
|
22
|
+
}
|
|
23
|
+
return response.data ?? [];
|
|
24
|
+
});
|
|
25
|
+
};
|
|
26
|
+
export const createRejectionPercentageAdjustmentRule = (headers, session, body) => {
|
|
27
|
+
return handlePossibleAxiosErrors(async () => {
|
|
28
|
+
const tableId = await getTableId(headers, session);
|
|
29
|
+
return createWebAppTableRegister(headers, tableId, body);
|
|
30
|
+
});
|
|
31
|
+
};
|
|
32
|
+
export const updateRejectionPercentageAdjustmentRule = (headers, session, id, body) => {
|
|
33
|
+
return handlePossibleAxiosErrors(async () => {
|
|
34
|
+
const tableId = await getTableId(headers, session);
|
|
35
|
+
return updateWebAppTableRegister(headers, tableId, Number(id), body);
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
export const rejectionPercentageAdjustmentRuleMatches = (rule, ctx) => {
|
|
39
|
+
const filtros = rule.filtros ?? defaultPriceAdjustmentFilters();
|
|
40
|
+
const priceRule = asPriceAdjustmentRule(rule);
|
|
41
|
+
const itemIds = ctx.itemIds ?? [];
|
|
42
|
+
if (filtros.itemMode === "all") {
|
|
43
|
+
return priceAdjustmentRuleMatches(priceRule, {
|
|
44
|
+
itemId: 0,
|
|
45
|
+
tipo: ctx.tipo,
|
|
46
|
+
providerCategoryId: ctx.providerCategoryId,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
if (itemIds.length === 0) {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
return itemIds.some((itemId) => priceAdjustmentRuleMatches(priceRule, {
|
|
53
|
+
itemId,
|
|
54
|
+
tipo: ctx.tipo,
|
|
55
|
+
providerCategoryId: ctx.providerCategoryId,
|
|
56
|
+
}));
|
|
57
|
+
};
|
|
58
|
+
const roundPercentage = (value, digits = 4) => {
|
|
59
|
+
const factor = 10 ** digits;
|
|
60
|
+
return Math.round((value + Number.EPSILON) * factor) / factor;
|
|
61
|
+
};
|
|
62
|
+
const clampPercentage = (value) => Math.min(100, Math.max(0, value));
|
|
63
|
+
const formatPercentage = (value) => Number.isInteger(value) ? String(value) : String(roundPercentage(value));
|
|
64
|
+
const formatFinalPercentage = (value) => roundPercentage(value, 2).toFixed(2);
|
|
65
|
+
const formatRuleEffect = (step) => {
|
|
66
|
+
const sign = step.operation === "subtract" ? "-" : "+";
|
|
67
|
+
return `${sign} ${formatPercentage(step.value)}% (${step.ruleName})`;
|
|
68
|
+
};
|
|
69
|
+
export const formatRejectionPercentageAdjustmentDescription = (result, baseLabel = "origen") => {
|
|
70
|
+
const base = `${formatPercentage(result.basePercentage)} (${baseLabel})`;
|
|
71
|
+
if (result.steps.length === 0) {
|
|
72
|
+
return base;
|
|
73
|
+
}
|
|
74
|
+
return `${base} ${result.steps.map(formatRuleEffect).join(" ")} = ${formatFinalPercentage(result.finalPercentage)}`;
|
|
75
|
+
};
|
|
76
|
+
export const resolveRejectionPercentageBase = (memo) => {
|
|
77
|
+
const parsed = parseJsonMemo(memo);
|
|
78
|
+
const current = clampPercentage(getRejectionPercentage(parsed));
|
|
79
|
+
const calc = parsed.rejectionCalculations;
|
|
80
|
+
if (calc &&
|
|
81
|
+
Number.isFinite(Number(calc.finalPercentage)) &&
|
|
82
|
+
roundPercentage(current, 2) ===
|
|
83
|
+
roundPercentage(Number(calc.finalPercentage), 2)) {
|
|
84
|
+
const base = Number(calc.basePercentage);
|
|
85
|
+
return Number.isFinite(base) ? clampPercentage(base) : current;
|
|
86
|
+
}
|
|
87
|
+
return current;
|
|
88
|
+
};
|
|
89
|
+
export const applyRejectionPercentageAdjustmentRules = (basePercentage, rules, ctx) => {
|
|
90
|
+
const start = Number(basePercentage);
|
|
91
|
+
const safeBase = Number.isFinite(start) ? clampPercentage(start) : 0;
|
|
92
|
+
let runningTotal = safeBase;
|
|
93
|
+
const steps = [];
|
|
94
|
+
const normalized = rules
|
|
95
|
+
.map((rule) => ("data" in rule ? rule.data : rule))
|
|
96
|
+
.filter((rule) => Boolean(rule))
|
|
97
|
+
.filter((rule) => !rule.fechaEliminacion && rule.activa !== false)
|
|
98
|
+
.sort((a, b) => (a.prioridad ?? 0) - (b.prioridad ?? 0));
|
|
99
|
+
for (const rule of normalized) {
|
|
100
|
+
if (!rejectionPercentageAdjustmentRuleMatches(rule, ctx)) {
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
const value = Number(rule.valor) || 0;
|
|
104
|
+
const previous = runningTotal;
|
|
105
|
+
runningTotal =
|
|
106
|
+
rule.operacion === "subtract" ? previous - value : previous + value;
|
|
107
|
+
runningTotal = clampPercentage(roundPercentage(runningTotal));
|
|
108
|
+
steps.push({
|
|
109
|
+
ruleName: rule.nombre,
|
|
110
|
+
operation: rule.operacion,
|
|
111
|
+
value,
|
|
112
|
+
appliedAmount: roundPercentage(runningTotal - previous),
|
|
113
|
+
runningTotal,
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
const result = {
|
|
117
|
+
basePercentage: roundPercentage(safeBase),
|
|
118
|
+
finalPercentage: roundPercentage(runningTotal, 2),
|
|
119
|
+
description: "",
|
|
120
|
+
steps,
|
|
121
|
+
};
|
|
122
|
+
result.description = formatRejectionPercentageAdjustmentDescription(result);
|
|
123
|
+
return result;
|
|
124
|
+
};
|
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,10 @@
|
|
|
1
|
+
import type { Session } from "@remix-run/node";
|
|
2
|
+
import { AxiosUtilsResponse, RejectionPercentageAdjustmentContext, RejectionPercentageAdjustmentResult, RejectionPercentageAdjustmentRule, WebAppRowGraphQL, WebAppTableUpdateResponse } from "@zauru-sdk/types";
|
|
3
|
+
export declare const filterActiveRejectionPercentageAdjustmentRules: (rules?: WebAppRowGraphQL<RejectionPercentageAdjustmentRule>[]) => WebAppRowGraphQL<RejectionPercentageAdjustmentRule>[];
|
|
4
|
+
export declare const getRejectionPercentageAdjustmentRules: (headers: any, session: Session) => Promise<AxiosUtilsResponse<WebAppRowGraphQL<RejectionPercentageAdjustmentRule>[]>>;
|
|
5
|
+
export declare const createRejectionPercentageAdjustmentRule: (headers: any, session: Session, body: RejectionPercentageAdjustmentRule) => Promise<AxiosUtilsResponse<WebAppTableUpdateResponse>>;
|
|
6
|
+
export declare const updateRejectionPercentageAdjustmentRule: (headers: any, session: Session, id: string, body: Partial<RejectionPercentageAdjustmentRule>) => Promise<AxiosUtilsResponse<WebAppTableUpdateResponse>>;
|
|
7
|
+
export declare const rejectionPercentageAdjustmentRuleMatches: (rule: RejectionPercentageAdjustmentRule, ctx: RejectionPercentageAdjustmentContext) => boolean;
|
|
8
|
+
export declare const formatRejectionPercentageAdjustmentDescription: (result: Pick<RejectionPercentageAdjustmentResult, "basePercentage" | "finalPercentage" | "steps">, baseLabel?: string) => string;
|
|
9
|
+
export declare const resolveRejectionPercentageBase: (memo?: string | object) => number;
|
|
10
|
+
export declare const applyRejectionPercentageAdjustmentRules: (basePercentage: number, rules: Array<RejectionPercentageAdjustmentRule | WebAppRowGraphQL<RejectionPercentageAdjustmentRule>>, ctx: RejectionPercentageAdjustmentContext) => RejectionPercentageAdjustmentResult;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zauru-sdk/utils",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.10.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.
|
|
28
|
+
"@zauru-sdk/common": "^6.4.1",
|
|
29
29
|
"@zauru-sdk/config": "^6.0.0",
|
|
30
30
|
"@zauru-sdk/graphql": "^6.7.0",
|
|
31
|
-
"@zauru-sdk/services": "^6.9.
|
|
32
|
-
"@zauru-sdk/types": "^6.
|
|
31
|
+
"@zauru-sdk/services": "^6.9.1",
|
|
32
|
+
"@zauru-sdk/types": "^6.15.0",
|
|
33
33
|
"moment": "^2.30.1"
|
|
34
34
|
},
|
|
35
|
-
"gitHead": "
|
|
35
|
+
"gitHead": "ab1ff7197a4688c0e887942b4e7a1f6b729a510e"
|
|
36
36
|
}
|