@zauru-sdk/utils 6.6.2 → 6.7.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,5 @@
1
- import { CURRENCY_PREFIX, getNewDateByFormat, getStringFullDate, getZauruDateByText, handlePossibleAxiosErrors, setRejectionPercentage, } from "@zauru-sdk/common";
2
- import { commitSession, createNewPurchaseOrder, deleteDelivery, deletePurchaseOrder, deleteReception, getLotesWithPurchaseFormated, getPurchaseOrder, getPurchasesListDataTables, getVariablesByName, inactivarLote, updateReceivedPurchaseOrder, } from "@zauru-sdk/services";
1
+ import { CURRENCY_PREFIX, calculatePurchaseOrderFinancials, getNewDateByFormat, getStringFullDate, getZauruDateByText, handlePossibleAxiosErrors, setRejectionPercentage, } from "@zauru-sdk/common";
2
+ import { commitSession, createNewPurchaseOrder, deleteDelivery, deletePurchaseOrder, deleteReception, getLotesWithPurchaseFormated, getPurchase, getPurchaseOrder, getPurchasesListDataTables, getVariablesByName, inactivarLote, updateReceivedPurchaseOrder, } from "@zauru-sdk/services";
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
@@ -163,6 +163,7 @@ export const updatePurchaseItemPrice = async (headers, data, purchase_id) => {
163
163
  purchase_order: {
164
164
  purchase_order_details_attributes: data.purchase_order_details_attributes,
165
165
  ...(data.memo !== undefined ? { memo: data.memo } : {}),
166
+ ...(data.discount !== undefined ? { discount: data.discount } : {}),
166
167
  },
167
168
  };
168
169
  const responseUpdate = await updateReceivedPurchaseOrder(headers, body, purchase_id);
@@ -172,16 +173,40 @@ export const updatePurchaseItemPrice = async (headers, data, purchase_id) => {
172
173
  return true;
173
174
  });
174
175
  };
176
+ const extractPurchaseOrderFinancialDetails = (purchase) => {
177
+ if (purchase == null || typeof purchase !== "object")
178
+ return [];
179
+ const record = purchase;
180
+ const raw = record.purchase_order_details ??
181
+ record.purchase_order_details_attributes ??
182
+ record.details ??
183
+ [];
184
+ if (Array.isArray(raw))
185
+ return raw;
186
+ if (raw && typeof raw === "object") {
187
+ return Object.values(raw);
188
+ }
189
+ return [];
190
+ };
175
191
  /**
176
192
  * updateOchAndDis
177
- * Updates rejectionPercentage in purchase_order.memo (not the financial discount column)
178
- * and optionally other_charges (tolerancia).
193
+ * Updates rejectionPercentage in purchase_order.memo and the monetary
194
+ * purchase_orders.discount (qty × unit_cost × % / 100). Optionally other_charges.
195
+ * `data.discount` is a legacy alias for the rejection percentage, not money.
179
196
  */
180
197
  export const updateOchAndDis = async (headers, data, purchase_id, session) => {
181
198
  return handlePossibleAxiosErrors(async () => {
182
199
  const rejectionPercentage = Number(data?.rejectionPercentage ?? data?.discount ?? 0);
183
200
  let currentMemo = data.memo;
184
- if (currentMemo === undefined && session) {
201
+ let details = [];
202
+ const restPurchaseResponse = await getPurchase(headers, purchase_id);
203
+ if (!restPurchaseResponse.error && restPurchaseResponse.data) {
204
+ if (currentMemo === undefined) {
205
+ currentMemo = restPurchaseResponse.data.memo;
206
+ }
207
+ details = extractPurchaseOrderFinancialDetails(restPurchaseResponse.data);
208
+ }
209
+ if ((currentMemo === undefined || details.length === 0) && session) {
185
210
  const purchaseOrderResponse = await getPurchaseOrder(session, purchase_id, {
186
211
  withLotStocksToMyAgency: false,
187
212
  withPayee: false,
@@ -191,14 +216,28 @@ export const updateOchAndDis = async (headers, data, purchase_id, session) => {
191
216
  throw new Error(purchaseOrderResponse.userMsg ||
192
217
  "No se pudo obtener la orden de compra para actualizar el porcentaje de rechazo");
193
218
  }
194
- currentMemo = purchaseOrderResponse.data.memo;
219
+ if (currentMemo === undefined) {
220
+ currentMemo = purchaseOrderResponse.data.memo;
221
+ }
222
+ if (details.length === 0) {
223
+ details = extractPurchaseOrderFinancialDetails(purchaseOrderResponse.data);
224
+ }
195
225
  }
196
226
  if (currentMemo === undefined) {
197
227
  throw new Error("memo or session is required to update rejectionPercentage");
198
228
  }
229
+ if (details.length === 0) {
230
+ throw new Error(restPurchaseResponse.userMsg ||
231
+ "No se pudieron obtener los detalles de la orden de compra para calcular el descuento");
232
+ }
233
+ const { discount } = calculatePurchaseOrderFinancials({
234
+ details,
235
+ rejectionPercentage,
236
+ });
199
237
  const body = {
200
238
  purchase_order: {
201
239
  memo: setRejectionPercentage(currentMemo, rejectionPercentage),
240
+ discount,
202
241
  },
203
242
  };
204
243
  if (data.other_charges !== undefined && data.other_charges !== null) {
@@ -34,11 +34,13 @@ export declare const updatePurchaseItemPrice: (headers: any, data: {
34
34
  id: number;
35
35
  }>;
36
36
  memo?: string;
37
+ discount?: number;
37
38
  }, purchase_id: number) => Promise<AxiosUtilsResponse<boolean>>;
38
39
  /**
39
40
  * updateOchAndDis
40
- * Updates rejectionPercentage in purchase_order.memo (not the financial discount column)
41
- * and optionally other_charges (tolerancia).
41
+ * Updates rejectionPercentage in purchase_order.memo and the monetary
42
+ * purchase_orders.discount (qty × unit_cost × % / 100). Optionally other_charges.
43
+ * `data.discount` is a legacy alias for the rejection percentage, not money.
42
44
  */
43
45
  export declare const updateOchAndDis: (headers: any, data: {
44
46
  rejectionPercentage?: number | string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zauru-sdk/utils",
3
- "version": "6.6.2",
3
+ "version": "6.7.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.5",
28
+ "@zauru-sdk/common": "^6.4.0",
29
29
  "@zauru-sdk/config": "^6.0.0",
30
30
  "@zauru-sdk/graphql": "^6.5.0",
31
- "@zauru-sdk/services": "^6.7.4",
32
- "@zauru-sdk/types": "^6.13.0",
31
+ "@zauru-sdk/services": "^6.7.5",
32
+ "@zauru-sdk/types": "^6.14.0",
33
33
  "moment": "^2.30.1"
34
34
  },
35
- "gitHead": "9d36380b88d7e251132cf68af896afecab65569f"
35
+ "gitHead": "66fea5adcf174e0a5e7a5f9324eb45ae0223f8b7"
36
36
  }