@zauru-sdk/utils 6.4.2 → 6.5.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
@@ -18,6 +18,7 @@ export * from "./zauru/providers.utils.js";
18
18
  export * from "./zauru/purchase-orders.utils.js";
19
19
  export * from "./zauru/shipments.utils.js";
20
20
  export * from "./zauru/specialItem.utils.js";
21
+ export * from "./zauru/priceAdjustmentRules.utils.js";
21
22
  export * from "./zauru/templates.utils.js";
22
23
  export * from "./zauru/tiposMuestra.utils.js";
23
24
  export * from "./zauru/webapp-tables.utils.js";
@@ -0,0 +1,132 @@
1
+ import { handlePossibleAxiosErrors } from "@zauru-sdk/common";
2
+ import { createWebAppTableRegister, getVariablesByName, getWebAppTableRegisters, updateWebAppTableRegister, } from "@zauru-sdk/services";
3
+ const TABLE_VAR = "price_adjustment_rules_web_app_table_id";
4
+ const DEFAULT_FILTERS = {
5
+ itemMode: "all",
6
+ itemIds: [],
7
+ tipoMode: "all",
8
+ tipos: [],
9
+ programaMode: "all",
10
+ providerCategoryIds: [],
11
+ };
12
+ export const defaultPriceAdjustmentFilters = () => ({
13
+ ...DEFAULT_FILTERS,
14
+ itemIds: [],
15
+ tipos: [],
16
+ providerCategoryIds: [],
17
+ });
18
+ export const filterActivePriceAdjustmentRules = (rules) => (rules ?? [])
19
+ .filter((rule) => !rule.data?.fechaEliminacion && rule.data?.activa !== false)
20
+ .sort((a, b) => (a.data?.prioridad ?? 0) - (b.data?.prioridad ?? 0) || a.id - b.id);
21
+ const getTableId = async (headers, session) => {
22
+ const vars = await getVariablesByName(headers, session, [TABLE_VAR]);
23
+ return vars[TABLE_VAR];
24
+ };
25
+ export const getPriceAdjustmentRules = (headers, session) => {
26
+ return handlePossibleAxiosErrors(async () => {
27
+ const tableId = await getTableId(headers, session);
28
+ const response = await getWebAppTableRegisters(session, tableId);
29
+ if (response.error) {
30
+ throw new Error(`Ocurrió un error al consultar las reglas de ajuste de precio: ${response.userMsg}`);
31
+ }
32
+ return response.data ?? [];
33
+ });
34
+ };
35
+ export const createPriceAdjustmentRule = (headers, session, body) => {
36
+ return handlePossibleAxiosErrors(async () => {
37
+ const tableId = await getTableId(headers, session);
38
+ return createWebAppTableRegister(headers, tableId, body);
39
+ });
40
+ };
41
+ export const updatePriceAdjustmentRule = (headers, session, id, body) => {
42
+ return handlePossibleAxiosErrors(async () => {
43
+ const tableId = await getTableId(headers, session);
44
+ return updateWebAppTableRegister(headers, tableId, Number(id), body);
45
+ });
46
+ };
47
+ const matchesFilter = (mode, selected, value) => {
48
+ const normalizedMode = mode ?? "all";
49
+ const values = selected ?? [];
50
+ if (normalizedMode === "all") {
51
+ return true;
52
+ }
53
+ if (value === undefined || value === null || value === "") {
54
+ return false;
55
+ }
56
+ const asString = String(value);
57
+ const included = values.some((item) => String(item) === asString);
58
+ if (normalizedMode === "include") {
59
+ return values.length > 0 && included;
60
+ }
61
+ return !included;
62
+ };
63
+ export const priceAdjustmentRuleMatches = (rule, ctx) => {
64
+ const filtros = rule.filtros ?? defaultPriceAdjustmentFilters();
65
+ return (matchesFilter(filtros.itemMode, filtros.itemIds, ctx.itemId) &&
66
+ matchesFilter(filtros.tipoMode, filtros.tipos, ctx.tipo) &&
67
+ matchesFilter(filtros.programaMode, filtros.providerCategoryIds, ctx.providerCategoryId));
68
+ };
69
+ const roundMoney = (value) => Math.round((value + Number.EPSILON) * 10000) / 10000;
70
+ const formatMoney = (value) => Number.isInteger(value) ? String(value) : String(roundMoney(value));
71
+ const formatSignedAmount = (value) => {
72
+ const abs = formatMoney(Math.abs(value));
73
+ return value < 0 ? `- ${abs}` : `+ ${abs}`;
74
+ };
75
+ const formatRuleEffect = (step) => {
76
+ if (step.valueType === "percentage") {
77
+ const sign = step.operation === "subtract" ? "-" : "+";
78
+ return `${sign} ${formatMoney(step.value)}% (${step.ruleName})`;
79
+ }
80
+ return `${formatSignedAmount(step.appliedAmount)} (${step.ruleName})`;
81
+ };
82
+ export const formatPriceAdjustmentDescription = (result) => {
83
+ const base = `${formatMoney(result.basePrice)} (base)`;
84
+ if (result.steps.length === 0) {
85
+ return base;
86
+ }
87
+ return `${base} ${result.steps.map(formatRuleEffect).join(" ")} = ${formatMoney(result.finalPrice)}`;
88
+ };
89
+ export const applyPriceAdjustmentRules = (basePrice, rules, ctx) => {
90
+ const start = Number(basePrice);
91
+ const safeBase = Number.isFinite(start) ? 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 (!priceAdjustmentRuleMatches(rule, ctx)) {
101
+ continue;
102
+ }
103
+ const value = Number(rule.valor) || 0;
104
+ const previous = runningTotal;
105
+ if (rule.tipoValor === "percentage") {
106
+ const delta = previous * (value / 100);
107
+ runningTotal =
108
+ rule.operacion === "subtract" ? previous - delta : previous + delta;
109
+ }
110
+ else {
111
+ runningTotal =
112
+ rule.operacion === "subtract" ? previous - value : previous + value;
113
+ }
114
+ runningTotal = roundMoney(runningTotal);
115
+ steps.push({
116
+ ruleName: rule.nombre,
117
+ operation: rule.operacion,
118
+ valueType: rule.tipoValor,
119
+ value,
120
+ appliedAmount: roundMoney(runningTotal - previous),
121
+ runningTotal,
122
+ });
123
+ }
124
+ const result = {
125
+ basePrice: roundMoney(safeBase),
126
+ finalPrice: roundMoney(runningTotal),
127
+ description: "",
128
+ steps,
129
+ };
130
+ result.description = formatPriceAdjustmentDescription(result);
131
+ return result;
132
+ };
@@ -162,6 +162,7 @@ export const updatePurchaseItemPrice = async (headers, data, purchase_id) => {
162
162
  const body = {
163
163
  purchase_order: {
164
164
  purchase_order_details_attributes: data.purchase_order_details_attributes,
165
+ ...(data.memo !== undefined ? { memo: data.memo } : {}),
165
166
  },
166
167
  };
167
168
  const responseUpdate = await updateReceivedPurchaseOrder(headers, body, purchase_id);
package/dist/index.d.ts CHANGED
@@ -18,6 +18,7 @@ export * from "./zauru/providers.utils.js";
18
18
  export * from "./zauru/purchase-orders.utils.js";
19
19
  export * from "./zauru/shipments.utils.js";
20
20
  export * from "./zauru/specialItem.utils.js";
21
+ export * from "./zauru/priceAdjustmentRules.utils.js";
21
22
  export * from "./zauru/templates.utils.js";
22
23
  export * from "./zauru/tiposMuestra.utils.js";
23
24
  export * from "./zauru/webapp-tables.utils.js";
@@ -0,0 +1,10 @@
1
+ import type { Session } from "@remix-run/node";
2
+ import { AxiosUtilsResponse, PriceAdjustmentContext, PriceAdjustmentFilters, PriceAdjustmentResult, PriceAdjustmentRule, WebAppRowGraphQL, WebAppTableUpdateResponse } from "@zauru-sdk/types";
3
+ export declare const defaultPriceAdjustmentFilters: () => PriceAdjustmentFilters;
4
+ export declare const filterActivePriceAdjustmentRules: (rules?: WebAppRowGraphQL<PriceAdjustmentRule>[]) => WebAppRowGraphQL<PriceAdjustmentRule>[];
5
+ export declare const getPriceAdjustmentRules: (headers: any, session: Session) => Promise<AxiosUtilsResponse<WebAppRowGraphQL<PriceAdjustmentRule>[]>>;
6
+ export declare const createPriceAdjustmentRule: (headers: any, session: Session, body: PriceAdjustmentRule) => Promise<AxiosUtilsResponse<WebAppTableUpdateResponse>>;
7
+ export declare const updatePriceAdjustmentRule: (headers: any, session: Session, id: string, body: Partial<PriceAdjustmentRule>) => Promise<AxiosUtilsResponse<WebAppTableUpdateResponse>>;
8
+ export declare const priceAdjustmentRuleMatches: (rule: PriceAdjustmentRule, ctx: PriceAdjustmentContext) => boolean;
9
+ export declare const formatPriceAdjustmentDescription: (result: Pick<PriceAdjustmentResult, "basePrice" | "finalPrice" | "steps">) => string;
10
+ export declare const applyPriceAdjustmentRules: (basePrice: number, rules: Array<PriceAdjustmentRule | WebAppRowGraphQL<PriceAdjustmentRule>>, ctx: PriceAdjustmentContext) => PriceAdjustmentResult;
@@ -33,6 +33,7 @@ export declare const updatePurchaseItemPrice: (headers: any, data: {
33
33
  item_id: number;
34
34
  id: number;
35
35
  }>;
36
+ memo?: string;
36
37
  }, purchase_id: number) => Promise<AxiosUtilsResponse<boolean>>;
37
38
  /**
38
39
  * updateOchAndDis
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zauru-sdk/utils",
3
- "version": "6.4.2",
3
+ "version": "6.5.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.3.2",
28
+ "@zauru-sdk/common": "^6.3.3",
29
29
  "@zauru-sdk/config": "^6.0.0",
30
- "@zauru-sdk/graphql": "^6.4.0",
31
- "@zauru-sdk/services": "^6.6.4",
32
- "@zauru-sdk/types": "^6.10.0",
30
+ "@zauru-sdk/graphql": "^6.5.0",
31
+ "@zauru-sdk/services": "^6.7.1",
32
+ "@zauru-sdk/types": "^6.11.0",
33
33
  "moment": "^2.30.1"
34
34
  },
35
- "gitHead": "bde6df12ceda8636075e0adb3b79a563076d51bf"
35
+ "gitHead": "3b01e2440af4265aa61b77aba1e7dd33abb2e4fa"
36
36
  }