@zauru-sdk/utils 6.26.1 → 6.28.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.
@@ -11,6 +11,7 @@ import { getProgramaciones } from "./programaciones.utils.js";
11
11
  import { getShipmentsToMyAgency } from "./shipments.utils.js";
12
12
  import { getTemplates } from "./templates.utils.js";
13
13
  import { getTipoMuestras } from "./tiposMuestra.utils.js";
14
+ import { getProviderReceptionBlockRules, getProviderReceptionExceptions, } from "./providerReceptionMatrix.utils.js";
14
15
  import { getReceptionTypes } from "./webapp-tables.utils.js";
15
16
  const missingQueryParam = (param) => {
16
17
  console.warn({
@@ -88,6 +89,8 @@ const sdkCatalogHandlers = {
88
89
  motivosRechazo: ({ headers, session }) => getMotivosRechazo(headers, session),
89
90
  ccPorcentajesDeRechazo: ({ headers, session }) => getCCPorcentajesDeRechazo(headers, session),
90
91
  programaciones4pinos: ({ headers, session }) => getProgramaciones(headers, session),
92
+ providerReceptionBlockRules: ({ headers, session }) => getProviderReceptionBlockRules(headers, session),
93
+ providerReceptionExceptions: ({ headers, session }) => getProviderReceptionExceptions(headers, session),
91
94
  solicitudesEliminacionPO: ({ headers, session }) => get4pinosSolicitudEliminacionPO(headers, session),
92
95
  pesoMaximoPorCanasta: ({ headers, session }) => get4pinosWeightLimitPerBasket(headers, session),
93
96
  poDiscountHistory: ({ headers, session }) => get4pinosPoDiscountHistory(headers, session),
@@ -1,4 +1,4 @@
1
- import { CURRENCY_PREFIX, calculatePurchaseOrderFinancials, getNewlyAppliedRejectionRuleSteps, getNewDateByFormat, getStringFullDate, getZauruDateByText, handlePossibleAxiosErrors, mergeJsonMemo, parseJsonMemo, } from "@zauru-sdk/common";
1
+ import { CURRENCY_PREFIX, buildPurchaseOrderDetailsCostAttributes, 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
  /**
@@ -152,19 +152,70 @@ export const getPurchaseOrderDataTables = async (headers, search, tag_id, agency
152
152
  const purchaseOrderDataTable = getPurchaseOrderFormated(ordersDataTables?.data[0]);
153
153
  return purchaseOrderDataTable;
154
154
  };
155
+ const unitCostOverridesFromAttributes = (attributes) => {
156
+ const unitCostByDetailId = new Map();
157
+ for (const detail of Object.values(attributes ?? {})) {
158
+ const id = Number(detail.id);
159
+ const unitCost = Number(detail.unit_cost);
160
+ if (Number.isFinite(id) && Number.isFinite(unitCost)) {
161
+ unitCostByDetailId.set(id, unitCost);
162
+ }
163
+ }
164
+ return unitCostByDetailId;
165
+ };
155
166
  /**
156
167
  * updatePurchaseItemPrice
157
- * @param headers
158
- * @param data
159
- * @param purchase_id
168
+ * Writes unit_cost and the matching line `cost` (qty × unit_cost) so Baculo
169
+ * refreshes subtotal. Recalculates monetary discount when it is not passed.
160
170
  */
161
- export const updatePurchaseItemPrice = async (headers, data, purchase_id) => {
171
+ export const updatePurchaseItemPrice = async (headers, data, purchase_id, session) => {
162
172
  return handlePossibleAxiosErrors(async () => {
173
+ const restPurchaseResponse = await getPurchase(headers, purchase_id);
174
+ if (restPurchaseResponse.error || !restPurchaseResponse.data) {
175
+ throw new Error(restPurchaseResponse.userMsg ||
176
+ "No se pudo obtener la orden de compra para actualizar el precio");
177
+ }
178
+ const loadedPurchase = restPurchaseResponse.data;
179
+ const details = extractPurchaseOrderFinancialDetails(loadedPurchase);
180
+ if (details.length === 0) {
181
+ throw new Error("No se pudieron obtener los detalles de la orden de compra para actualizar el costo");
182
+ }
183
+ const unitCostByDetailId = unitCostOverridesFromAttributes(data.purchase_order_details_attributes);
184
+ const mergedDetails = details.map((detail) => {
185
+ const id = Number(detail.id);
186
+ return {
187
+ ...detail,
188
+ unit_cost: unitCostByDetailId.get(id) ?? detail.unit_cost,
189
+ };
190
+ });
191
+ const detailsAttributes = buildPurchaseOrderDetailsCostAttributes(mergedDetails, unitCostByDetailId);
192
+ if (Object.keys(detailsAttributes).length === 0) {
193
+ throw new Error("No se pudieron armar los costos de línea de la orden de compra");
194
+ }
195
+ let discount = data.discount;
196
+ if (discount === undefined) {
197
+ const rules = await getActiveRejectionPercentageAdjustmentRules(headers, session);
198
+ const purchaseRecord = loadedPurchase;
199
+ discount = calculatePurchaseOrderFinancials({
200
+ details: mergedDetails,
201
+ memo: data.memo ?? loadedPurchase.memo,
202
+ rules,
203
+ ctx: {
204
+ itemIds: extractPurchaseOrderItemIds(loadedPurchase),
205
+ tipo: purchaseRecord.reference,
206
+ providerCategoryId: purchaseRecord.payee?.payee_category_id ??
207
+ purchaseRecord.payee_category_id,
208
+ },
209
+ }).discount;
210
+ }
163
211
  const body = {
164
212
  purchase_order: {
165
- purchase_order_details_attributes: data.purchase_order_details_attributes,
213
+ purchase_order_details_attributes: detailsAttributes,
166
214
  ...(data.memo !== undefined ? { memo: data.memo } : {}),
167
- ...(data.discount !== undefined ? { discount: data.discount } : {}),
215
+ discount,
216
+ ...(data.other_charges !== undefined && data.other_charges !== null
217
+ ? { other_charges: Number(data.other_charges) }
218
+ : {}),
168
219
  },
169
220
  };
170
221
  const responseUpdate = await updateReceivedPurchaseOrder(headers, body, purchase_id);
@@ -289,22 +340,19 @@ export const updateOchAndDis = async (headers, data, purchase_id, session) => {
289
340
  },
290
341
  });
291
342
  const newlyAppliedSteps = getNewlyAppliedRejectionRuleSteps(previousCalculations, financials.rejectionCalculations);
292
- const body = {
293
- purchase_order: {
294
- memo: mergeJsonMemo(memoForOrigin, {
295
- rejectionPercentage: financials.rejectionPercentage,
296
- rejectionCalculations: financials.rejectionCalculations,
297
- rejectionLayers: financials.rejectionLayers,
298
- }),
299
- discount: financials.discount,
300
- },
301
- };
302
- if (data.other_charges !== undefined && data.other_charges !== null) {
303
- body.purchase_order.other_charges = Number(data.other_charges);
304
- }
305
- const responseUpdate = await updateReceivedPurchaseOrder(headers, body, purchase_id);
306
- if (responseUpdate.error || responseUpdate.data === null) {
307
- throw new Error(responseUpdate.userMsg);
343
+ const persistResponse = await updatePurchaseItemPrice(headers, {
344
+ memo: mergeJsonMemo(memoForOrigin, {
345
+ rejectionPercentage: financials.rejectionPercentage,
346
+ rejectionCalculations: financials.rejectionCalculations,
347
+ rejectionLayers: financials.rejectionLayers,
348
+ }),
349
+ discount: financials.discount,
350
+ ...(data.other_charges !== undefined && data.other_charges !== null
351
+ ? { other_charges: Number(data.other_charges) }
352
+ : {}),
353
+ }, purchase_id, session);
354
+ if (persistResponse.error) {
355
+ throw new Error(persistResponse.userMsg);
308
356
  }
309
357
  return {
310
358
  rejectionPercentage: financials.rejectionPercentage,
@@ -21,21 +21,23 @@ export declare const getPurchasesDataTableListFormated: (headers: any, session:
21
21
  * @returns
22
22
  */
23
23
  export declare const getPurchaseOrderDataTables: (headers: any, search: string, tag_id?: string, agency_id?: string) => Promise<PurchasesDataTableListFormatedSchema>;
24
- /**
25
- * updatePurchaseItemPrice
26
- * @param headers
27
- * @param data
28
- * @param purchase_id
29
- */
30
- export declare const updatePurchaseItemPrice: (headers: any, data: {
31
- purchase_order_details_attributes: Record<string, {
24
+ type PurchaseOrderPriceUpdateInput = {
25
+ purchase_order_details_attributes?: Record<string, {
32
26
  unit_cost: number;
33
27
  item_id: number;
34
28
  id: number;
29
+ cost?: number;
35
30
  }>;
36
31
  memo?: string;
37
32
  discount?: number;
38
- }, purchase_id: number) => Promise<AxiosUtilsResponse<boolean>>;
33
+ other_charges?: number | string;
34
+ };
35
+ /**
36
+ * updatePurchaseItemPrice
37
+ * Writes unit_cost and the matching line `cost` (qty × unit_cost) so Baculo
38
+ * refreshes subtotal. Recalculates monetary discount when it is not passed.
39
+ */
40
+ export declare const updatePurchaseItemPrice: (headers: any, data: PurchaseOrderPriceUpdateInput, purchase_id: number, session?: Session) => Promise<AxiosUtilsResponse<boolean>>;
39
41
  /**
40
42
  * updateOchAndDis
41
43
  * Updates rejectionPercentage in purchase_order.memo and the monetary
@@ -119,3 +121,4 @@ export declare const deleteTask: (session: Session, timeStamp: number) => Promis
119
121
  export declare const createNewLabItemRequest: (headers: any, session: Session, body: Partial<PurchaseOrderGraphQL> & {
120
122
  [key: string]: any;
121
123
  }) => Promise<AxiosUtilsResponse<PurchaseOrderGraphQL>>;
124
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zauru-sdk/utils",
3
- "version": "6.26.1",
3
+ "version": "6.28.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.12.0",
74
+ "@zauru-sdk/common": "^6.13.0",
75
75
  "@zauru-sdk/config": "^6.0.0",
76
76
  "@zauru-sdk/graphql": "^6.9.1",
77
- "@zauru-sdk/services": "^6.15.1",
78
- "@zauru-sdk/types": "^6.28.0",
77
+ "@zauru-sdk/services": "^6.16.0",
78
+ "@zauru-sdk/types": "^6.29.0",
79
79
  "moment": "^2.30.1"
80
80
  },
81
- "gitHead": "862f3a72643479e1b124cfc9159d33901e5b6912"
81
+ "gitHead": "671859beecfce09a1ccf138055a4de26a8c1f059"
82
82
  }