@zauru-sdk/utils 6.16.1 → 6.18.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.
@@ -1,5 +1,14 @@
1
- import { handlePossibleAxiosErrors } from "@zauru-sdk/common";
1
+ import { formatAutomaticRejectionRuleHistoryDescription, handlePossibleAxiosErrors, REJECTION_PERCENTAGE_RULE_HISTORY_TYPE, } from "@zauru-sdk/common";
2
2
  import { create4pinosPoDiscountHistory, getPurchaseOrder, getVariablesByName, update4pinosPoDiscountHistory, } from "@zauru-sdk/services";
3
+ export const mapRejectionRuleStepsToDiscountHistory = (steps, meta) => steps.map((step) => ({
4
+ agency_id: meta.agency_id,
5
+ employee_id: meta.employee_id ?? 0,
6
+ employee_name: "Sistema",
7
+ created_at: meta.created_at ?? new Date().toISOString(),
8
+ description: formatAutomaticRejectionRuleHistoryDescription(step),
9
+ type: REJECTION_PERCENTAGE_RULE_HISTORY_TYPE,
10
+ discount: step.appliedAmount,
11
+ }));
3
12
  export const add4pinosPoDiscountsHistory = async (session, headers, purchaseOrderId, discounts) => {
4
13
  return handlePossibleAxiosErrors(async () => {
5
14
  const { historial_porcentajes_de_rechazo_webapp_table_id } = await getVariablesByName(headers, session, [
@@ -1,4 +1,4 @@
1
- import { CURRENCY_PREFIX, calculatePurchaseOrderFinancials, getNewDateByFormat, getStringFullDate, getZauruDateByText, handlePossibleAxiosErrors, mergeJsonMemo, } from "@zauru-sdk/common";
1
+ import { CURRENCY_PREFIX, calculatePurchaseOrderFinancials, getNewlyAppliedRejectionRuleSteps, getNewDateByFormat, getStringFullDate, getZauruDateByText, handlePossibleAxiosErrors, mergeJsonMemo, parseJsonMemo, } from "@zauru-sdk/common";
2
2
  import { commitSession, createNewPurchaseOrder, deleteDelivery, deletePurchaseOrder, deleteReception, getLotesWithPurchaseFormated, getPurchase, getPurchaseOrder, getPurchasesListDataTables, getVariablesByName, inactivarLote, updateReceivedPurchaseOrder, } from "@zauru-sdk/services";
3
3
  import { getActiveRejectionPercentageAdjustmentRules } from "./rejectionPercentageAdjustmentRules.utils.js";
4
4
  /**
@@ -210,17 +210,19 @@ const extractPurchaseOrderItemIds = (purchase) => {
210
210
  * updateOchAndDis
211
211
  * Updates rejectionPercentage in purchase_order.memo and the monetary
212
212
  * purchase_orders.discount (qty × unit_cost × % / 100). Optionally other_charges.
213
+ * Prefer `rejectionApplication` (delta + mode) so successive layers are preserved.
213
214
  * `data.discount` is a legacy alias for the rejection percentage, not money.
214
215
  */
215
216
  export const updateOchAndDis = async (headers, data, purchase_id, session) => {
216
217
  return handlePossibleAxiosErrors(async () => {
217
- const rejectionPercentage = Number(data?.rejectionPercentage ?? data?.discount ?? 0);
218
218
  let currentMemo = data.memo;
219
+ let liveMemo;
219
220
  let details = [];
220
221
  let loadedPurchase;
221
222
  const restPurchaseResponse = await getPurchase(headers, purchase_id);
222
223
  if (!restPurchaseResponse.error && restPurchaseResponse.data) {
223
224
  loadedPurchase = restPurchaseResponse.data;
225
+ liveMemo = restPurchaseResponse.data.memo;
224
226
  if (currentMemo === undefined) {
225
227
  currentMemo = restPurchaseResponse.data.memo;
226
228
  }
@@ -236,6 +238,9 @@ export const updateOchAndDis = async (headers, data, purchase_id, session) => {
236
238
  throw new Error(purchaseOrderResponse.userMsg ||
237
239
  "No se pudo obtener la orden de compra para actualizar el porcentaje de rechazo");
238
240
  }
241
+ if (liveMemo === undefined) {
242
+ liveMemo = purchaseOrderResponse.data.memo;
243
+ }
239
244
  if (currentMemo === undefined) {
240
245
  currentMemo = purchaseOrderResponse.data.memo;
241
246
  }
@@ -258,10 +263,22 @@ export const updateOchAndDis = async (headers, data, purchase_id, session) => {
258
263
  const payee = purchaseRecord?.payee;
259
264
  const itemIds = extractPurchaseOrderItemIds(loadedPurchase);
260
265
  const tipo = purchaseRecord?.reference;
266
+ const memoForOrigin = liveMemo ?? currentMemo;
267
+ const previousCalculations = parseJsonMemo(memoForOrigin).rejectionCalculations;
268
+ const hasExplicitLayers = data.rejectionLayers != null;
269
+ const hasApplication = data.rejectionApplication != null;
261
270
  const financials = calculatePurchaseOrderFinancials({
262
271
  details,
263
- rejectionPercentage,
264
- memo: currentMemo,
272
+ memo: memoForOrigin,
273
+ ...(hasExplicitLayers ? { rejectionLayers: data.rejectionLayers } : {}),
274
+ ...(hasApplication
275
+ ? { rejectionApplication: data.rejectionApplication }
276
+ : {}),
277
+ ...(!hasExplicitLayers && !hasApplication
278
+ ? {
279
+ rejectionPercentage: Number(data?.rejectionPercentage ?? data?.discount ?? 0),
280
+ }
281
+ : {}),
265
282
  rules,
266
283
  ctx: {
267
284
  itemIds,
@@ -271,11 +288,13 @@ export const updateOchAndDis = async (headers, data, purchase_id, session) => {
271
288
  ?.payee_category_id,
272
289
  },
273
290
  });
291
+ const newlyAppliedSteps = getNewlyAppliedRejectionRuleSteps(previousCalculations, financials.rejectionCalculations);
274
292
  const body = {
275
293
  purchase_order: {
276
- memo: mergeJsonMemo(currentMemo, {
294
+ memo: mergeJsonMemo(memoForOrigin, {
277
295
  rejectionPercentage: financials.rejectionPercentage,
278
296
  rejectionCalculations: financials.rejectionCalculations,
297
+ rejectionLayers: financials.rejectionLayers,
279
298
  }),
280
299
  discount: financials.discount,
281
300
  },
@@ -287,7 +306,12 @@ export const updateOchAndDis = async (headers, data, purchase_id, session) => {
287
306
  if (responseUpdate.error || responseUpdate.data === null) {
288
307
  throw new Error(responseUpdate.userMsg);
289
308
  }
290
- return true;
309
+ return {
310
+ rejectionPercentage: financials.rejectionPercentage,
311
+ rejectionLayers: financials.rejectionLayers,
312
+ rejectionCalculations: financials.rejectionCalculations,
313
+ newlyAppliedSteps,
314
+ };
291
315
  });
292
316
  };
293
317
  /**
@@ -1,6 +1,6 @@
1
1
  import { filterActiveRejectionPercentageAdjustmentRules, handlePossibleAxiosErrors, REJECTION_PERCENTAGE_ADJUSTMENT_RULES_TABLE_VAR, } from "@zauru-sdk/common";
2
2
  import { createWebAppTableRegister, getVariables, getVariablesByName, getWebAppTableRegisters, getWebAppTableRegistersRest, updateWebAppTableRegister, } from "@zauru-sdk/services";
3
- export { applyRejectionPercentageAdjustmentRules, filterActiveRejectionPercentageAdjustmentRules, formatRejectionPercentageAdjustmentDescription, rejectionPercentageAdjustmentRuleMatches, resolveRejectionPercentageBase, resolveRejectionPercentageOrigin, REJECTION_PERCENTAGE_ADJUSTMENT_RULES_TABLE_VAR, } from "@zauru-sdk/common";
3
+ export { applyRejectionPercentageAdjustmentRules, applyRejectionPercentageLayers, computeEffectiveRejectionPercentage, filterActiveRejectionPercentageAdjustmentRules, formatAutomaticRejectionRuleHistoryDescription, formatRejectionPercentageAdjustmentDescription, getNewlyAppliedRejectionRuleSteps, hasSuccessiveRejectionLayers, isAutomaticRejectionRuleHistoryType, normalizeRejectionPercentageLayers, rejectionPercentageAdjustmentRuleMatches, resolveRejectionPercentageBase, resolveRejectionPercentageOrigin, REJECTION_PERCENTAGE_ADJUSTMENT_RULES_TABLE_VAR, REJECTION_PERCENTAGE_RULE_HISTORY_TYPE, } from "@zauru-sdk/common";
4
4
  const getTableId = async (headers, session) => {
5
5
  const vars = await getVariablesByName(headers, session, [
6
6
  REJECTION_PERCENTAGE_ADJUSTMENT_RULES_TABLE_VAR,
@@ -5,6 +5,7 @@ import { RemixServer } from "@remix-run/react";
5
5
  import { renderToPipeableStream } from "react-dom/server";
6
6
  import { handleError } from "./cloudwatch-remix.server.js";
7
7
  const ABORT_DELAY = 5_000;
8
+ const RemixServerComponent = RemixServer;
8
9
  function isBotRequest(userAgent) {
9
10
  if (!userAgent)
10
11
  return false;
@@ -29,7 +30,7 @@ export function handleRequest(request, responseStatusCode, responseHeaders, remi
29
30
  function handleBotRequest(request, responseStatusCode, responseHeaders, remixContext) {
30
31
  return new Promise((resolve, reject) => {
31
32
  let shellRendered = false;
32
- const { pipe, abort } = renderToPipeableStream(createElement(RemixServer, {
33
+ const { pipe, abort } = renderToPipeableStream(createElement(RemixServerComponent, {
33
34
  context: remixContext,
34
35
  url: request.url,
35
36
  abortDelay: ABORT_DELAY,
@@ -61,7 +62,7 @@ function handleBotRequest(request, responseStatusCode, responseHeaders, remixCon
61
62
  function handleBrowserRequest(request, responseStatusCode, responseHeaders, remixContext) {
62
63
  return new Promise((resolve, reject) => {
63
64
  let shellRendered = false;
64
- const { pipe, abort } = renderToPipeableStream(createElement(RemixServer, {
65
+ const { pipe, abort } = renderToPipeableStream(createElement(RemixServerComponent, {
65
66
  context: remixContext,
66
67
  url: request.url,
67
68
  abortDelay: ABORT_DELAY,
@@ -1,3 +1,8 @@
1
1
  import { Session } from "@remix-run/node";
2
- import { PoDiscountHistory } from "@zauru-sdk/types";
2
+ import { PoDiscountHistory, PoDiscountHistoryEntry, RejectionPercentageAdjustmentStep } from "@zauru-sdk/types";
3
+ export declare const mapRejectionRuleStepsToDiscountHistory: (steps: RejectionPercentageAdjustmentStep[], meta: {
4
+ agency_id: number;
5
+ employee_id?: number;
6
+ created_at?: string;
7
+ }) => PoDiscountHistoryEntry[];
3
8
  export declare const add4pinosPoDiscountsHistory: (session: Session, headers: any, purchaseOrderId: number | string, discounts: PoDiscountHistory["discounts"]) => Promise<import("@zauru-sdk/types").AxiosUtilsResponse<void>>;
@@ -1,5 +1,5 @@
1
1
  import type { Session } from "@remix-run/node";
2
- import { AxiosUtilsResponse, LoteWithPurchaseFormatedSchema, PurchaseOrderGraphQL, PurchasesDataTableListFormatedSchema, PurchasesListResponseSchema } from "@zauru-sdk/types";
2
+ import { AxiosUtilsResponse, LoteWithPurchaseFormatedSchema, PurchaseOrderGraphQL, PurchasesDataTableListFormatedSchema, PurchasesListResponseSchema, RejectionPercentageApplication, RejectionPercentageLayers, UpdateOchAndDisResult } from "@zauru-sdk/types";
3
3
  /**
4
4
  * Obtiene el listado de ordenes de compra, formateado especialmente para armar la tabla de edición de porcentajes y tolerancia
5
5
  * @param headers
@@ -40,14 +40,17 @@ export declare const updatePurchaseItemPrice: (headers: any, data: {
40
40
  * updateOchAndDis
41
41
  * Updates rejectionPercentage in purchase_order.memo and the monetary
42
42
  * purchase_orders.discount (qty × unit_cost × % / 100). Optionally other_charges.
43
+ * Prefer `rejectionApplication` (delta + mode) so successive layers are preserved.
43
44
  * `data.discount` is a legacy alias for the rejection percentage, not money.
44
45
  */
45
46
  export declare const updateOchAndDis: (headers: any, data: {
47
+ rejectionApplication?: RejectionPercentageApplication;
48
+ rejectionLayers?: RejectionPercentageLayers;
46
49
  rejectionPercentage?: number | string;
47
50
  discount?: number | string;
48
51
  other_charges?: number | string;
49
52
  memo?: string | object;
50
- }, purchase_id: number, session?: Session) => Promise<AxiosUtilsResponse<boolean>>;
53
+ }, purchase_id: number, session?: Session) => Promise<AxiosUtilsResponse<UpdateOchAndDisResult>>;
51
54
  /**
52
55
  * Obtengo los id's de las órdenes de compra en un arreglo numérico
53
56
  * @param headers
@@ -1,6 +1,6 @@
1
1
  import type { Session } from "@remix-run/node";
2
2
  import { AxiosUtilsResponse, RejectionPercentageAdjustmentRule, WebAppRowGraphQL, WebAppTableUpdateResponse } from "@zauru-sdk/types";
3
- export { applyRejectionPercentageAdjustmentRules, filterActiveRejectionPercentageAdjustmentRules, formatRejectionPercentageAdjustmentDescription, rejectionPercentageAdjustmentRuleMatches, resolveRejectionPercentageBase, resolveRejectionPercentageOrigin, REJECTION_PERCENTAGE_ADJUSTMENT_RULES_TABLE_VAR, } from "@zauru-sdk/common";
3
+ export { applyRejectionPercentageAdjustmentRules, applyRejectionPercentageLayers, computeEffectiveRejectionPercentage, filterActiveRejectionPercentageAdjustmentRules, formatAutomaticRejectionRuleHistoryDescription, formatRejectionPercentageAdjustmentDescription, getNewlyAppliedRejectionRuleSteps, hasSuccessiveRejectionLayers, isAutomaticRejectionRuleHistoryType, normalizeRejectionPercentageLayers, rejectionPercentageAdjustmentRuleMatches, resolveRejectionPercentageBase, resolveRejectionPercentageOrigin, REJECTION_PERCENTAGE_ADJUSTMENT_RULES_TABLE_VAR, REJECTION_PERCENTAGE_RULE_HISTORY_TYPE, } from "@zauru-sdk/common";
4
4
  export declare const getRejectionPercentageAdjustmentRules: (headers: any, session: Session) => Promise<AxiosUtilsResponse<WebAppRowGraphQL<RejectionPercentageAdjustmentRule>[]>>;
5
5
  /**
6
6
  * Same listing without a Remix session: resolves the table id over REST, for
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zauru-sdk/utils",
3
- "version": "6.16.1",
3
+ "version": "6.18.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",
@@ -71,12 +71,12 @@
71
71
  "dependencies": {
72
72
  "@aws-sdk/client-cloudwatch-logs": "^3.1123.0",
73
73
  "@remix-run/node": "^2.8.1",
74
- "@zauru-sdk/common": "^6.6.0",
74
+ "@zauru-sdk/common": "^6.8.0",
75
75
  "@zauru-sdk/config": "^6.0.0",
76
76
  "@zauru-sdk/graphql": "^6.7.0",
77
- "@zauru-sdk/services": "^6.11.0",
78
- "@zauru-sdk/types": "^6.16.0",
77
+ "@zauru-sdk/services": "^6.11.2",
78
+ "@zauru-sdk/types": "^6.18.0",
79
79
  "moment": "^2.30.1"
80
80
  },
81
- "gitHead": "99cc7bef84a3d3c919d336a95eebe82cce2e8c08"
81
+ "gitHead": "c5b785baa29fdd2ab7f42a9077ba5cbe5c7c9707"
82
82
  }