@asteby/metacore-runtime-react 47.1.0 → 48.0.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/CHANGELOG.md +29 -0
- package/dist/action-modal-dispatcher.d.ts.map +1 -1
- package/dist/action-modal-dispatcher.js +24 -4
- package/dist/business/catalog-product-search.d.ts +25 -0
- package/dist/business/catalog-product-search.d.ts.map +1 -0
- package/dist/business/catalog-product-search.js +57 -0
- package/dist/business/catalog.d.ts.map +1 -1
- package/dist/business/catalog.js +2 -0
- package/dist/business/document-editor-model.d.ts +107 -0
- package/dist/business/document-editor-model.d.ts.map +1 -0
- package/dist/business/document-editor-model.js +279 -0
- package/dist/business/document-editor-panels.d.ts +64 -0
- package/dist/business/document-editor-panels.d.ts.map +1 -0
- package/dist/business/document-editor-panels.js +58 -0
- package/dist/business/document-editor.d.ts +27 -0
- package/dist/business/document-editor.d.ts.map +1 -0
- package/dist/business/document-editor.js +445 -0
- package/dist/business/document-lines.d.ts.map +1 -1
- package/dist/business/document-lines.js +5 -1
- package/dist/business/index.d.ts +5 -0
- package/dist/business/index.d.ts.map +1 -1
- package/dist/business/index.js +5 -0
- package/dist/business/line-items.d.ts +7 -0
- package/dist/business/line-items.d.ts.map +1 -1
- package/dist/business/line-items.js +6 -1
- package/dist/business/payment-allocator.d.ts +19 -0
- package/dist/business/payment-allocator.d.ts.map +1 -0
- package/dist/business/payment-allocator.js +31 -0
- package/dist/business/product-search.d.ts +4 -0
- package/dist/business/product-search.d.ts.map +1 -1
- package/dist/dialogs/dynamic-record.d.ts.map +1 -1
- package/dist/dialogs/dynamic-record.js +14 -7
- package/dist/document-form-dialog.d.ts +31 -2
- package/dist/document-form-dialog.d.ts.map +1 -1
- package/dist/document-form-dialog.js +43 -11
- package/dist/dynamic-columns-helpers.d.ts +9 -0
- package/dist/dynamic-columns-helpers.d.ts.map +1 -1
- package/dist/dynamic-columns-helpers.js +19 -0
- package/dist/dynamic-columns.d.ts +8 -2
- package/dist/dynamic-columns.d.ts.map +1 -1
- package/dist/dynamic-columns.js +21 -5
- package/dist/dynamic-crud-page.d.ts +6 -0
- package/dist/dynamic-crud-page.d.ts.map +1 -1
- package/dist/dynamic-crud-page.js +14 -5
- package/dist/dynamic-form-schema.d.ts.map +1 -1
- package/dist/dynamic-form-schema.js +15 -3
- package/dist/dynamic-form.d.ts +16 -1
- package/dist/dynamic-form.d.ts.map +1 -1
- package/dist/dynamic-form.js +78 -20
- package/dist/dynamic-kanban.d.ts +9 -3
- package/dist/dynamic-kanban.d.ts.map +1 -1
- package/dist/dynamic-kanban.js +31 -12
- package/dist/dynamic-relation-helpers.d.ts.map +1 -1
- package/dist/dynamic-relation-helpers.js +8 -0
- package/dist/dynamic-relation.js +1 -1
- package/dist/index.d.ts +5 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +13 -2
- package/dist/installed-addons-context.d.ts +15 -0
- package/dist/installed-addons-context.d.ts.map +1 -0
- package/dist/installed-addons-context.js +39 -0
- package/dist/org-runtime-context.d.ts +8 -0
- package/dist/org-runtime-context.d.ts.map +1 -1
- package/dist/org-runtime-context.js +8 -0
- package/dist/org-runtime-provider.d.ts +3 -1
- package/dist/org-runtime-provider.d.ts.map +1 -1
- package/dist/org-runtime-provider.js +3 -3
- package/dist/primitives/aging.d.ts +34 -0
- package/dist/primitives/aging.d.ts.map +1 -0
- package/dist/primitives/aging.js +48 -0
- package/dist/primitives/allocation.d.ts +42 -0
- package/dist/primitives/allocation.d.ts.map +1 -0
- package/dist/primitives/allocation.js +59 -0
- package/dist/primitives/approvals.d.ts +71 -0
- package/dist/primitives/approvals.d.ts.map +1 -0
- package/dist/primitives/approvals.js +48 -0
- package/dist/primitives/contributions.d.ts +82 -0
- package/dist/primitives/contributions.d.ts.map +1 -0
- package/dist/primitives/contributions.js +126 -0
- package/dist/primitives/document-kinds.d.ts +25 -0
- package/dist/primitives/document-kinds.d.ts.map +1 -0
- package/dist/primitives/document-kinds.js +26 -0
- package/dist/primitives/index.d.ts +8 -0
- package/dist/primitives/index.d.ts.map +1 -0
- package/dist/primitives/index.js +23 -0
- package/dist/primitives/panels.d.ts +68 -0
- package/dist/primitives/panels.d.ts.map +1 -0
- package/dist/primitives/panels.js +3 -0
- package/dist/primitives/use-contributions.d.ts +15 -0
- package/dist/primitives/use-contributions.d.ts.map +1 -0
- package/dist/primitives/use-contributions.js +30 -0
- package/dist/record-prefill-bar.d.ts +11 -0
- package/dist/record-prefill-bar.d.ts.map +1 -0
- package/dist/record-prefill-bar.js +33 -0
- package/dist/types.d.ts +102 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/__tests__/catalog-product-search.test.ts +61 -0
- package/src/__tests__/document-editor-model.test.ts +167 -0
- package/src/__tests__/document-editor.test.tsx +261 -0
- package/src/__tests__/document-form-dialog.test.tsx +118 -3
- package/src/__tests__/dynamic-form-select-edit.test.tsx +155 -0
- package/src/__tests__/dynamic-kanban.test.tsx +24 -3
- package/src/__tests__/dynamic-record-extension-label.test.tsx +88 -0
- package/src/__tests__/installed-addons-context.test.tsx +50 -0
- package/src/__tests__/line-item-totals.test.ts +28 -0
- package/src/__tests__/primitives-federated.test.tsx +156 -0
- package/src/__tests__/primitives.test.ts +224 -0
- package/src/__tests__/record-prefill-bar.test.tsx +50 -0
- package/src/action-modal-dispatcher.tsx +47 -4
- package/src/business/catalog-product-search.ts +87 -0
- package/src/business/catalog.ts +2 -0
- package/src/business/document-editor-model.ts +325 -0
- package/src/business/document-editor-panels.tsx +164 -0
- package/src/business/document-editor.tsx +727 -0
- package/src/business/document-lines.ts +5 -1
- package/src/business/index.ts +5 -0
- package/src/business/line-items.ts +15 -1
- package/src/business/payment-allocator.tsx +110 -0
- package/src/business/product-search.ts +4 -0
- package/src/dialogs/dynamic-record.tsx +29 -6
- package/src/document-form-dialog.tsx +92 -12
- package/src/dynamic-columns-helpers.ts +22 -0
- package/src/dynamic-columns.tsx +23 -6
- package/src/dynamic-crud-page.tsx +22 -3
- package/src/dynamic-form-schema.ts +15 -2
- package/src/dynamic-form.tsx +117 -39
- package/src/dynamic-kanban.tsx +34 -7
- package/src/dynamic-relation-helpers.ts +9 -0
- package/src/dynamic-relation.tsx +7 -2
- package/src/index.ts +21 -1
- package/src/installed-addons-context.tsx +58 -0
- package/src/org-runtime-context.ts +10 -0
- package/src/org-runtime-provider.tsx +7 -2
- package/src/primitives/aging.ts +76 -0
- package/src/primitives/allocation.ts +104 -0
- package/src/primitives/approvals.ts +103 -0
- package/src/primitives/contributions.ts +208 -0
- package/src/primitives/document-kinds.ts +62 -0
- package/src/primitives/index.ts +23 -0
- package/src/primitives/panels.ts +59 -0
- package/src/primitives/use-contributions.ts +42 -0
- package/src/record-prefill-bar.tsx +62 -0
- package/src/row-action-stage-gate.test.ts +33 -0
- package/src/types.ts +101 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,34 @@
|
|
|
1
1
|
# @asteby/metacore-runtime-react
|
|
2
2
|
|
|
3
|
+
## 48.0.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 7532829: Primitivos de negocio para hosts y addons federados:
|
|
8
|
+
|
|
9
|
+
- `InstalledAddonsProvider` + `useAddonInstalled` / `useCapabilityProvided` / `useInstalledAddons`: una sola respuesta a «¿qué hay instalado?» (sin provider devuelven `undefined`, nunca «ausente»).
|
|
10
|
+
- `allocatePayment` / `validateAllocation` (PaymentAllocator) y `computeAging` / `bucketFor` (AgingTable) como funciones puras.
|
|
11
|
+
- Bandeja de aprobaciones: `approvalsClient`, `registerApprovalCategory`, `groupApprovals` y el tipo `ApprovalRequestDTO`.
|
|
12
|
+
- Contribuciones federadas por documento: `registerDocumentContribution` (con disposer y carga perezosa), `resolveContributions` (pura) y el hook `useDocumentContributions`.
|
|
13
|
+
- Modales federados: `registerFederatedModal` / `resolveFederatedModal`. `ActionModalDispatcher` pinta el modal registrado sin esperar al host y, si el addon del `modal` no está instalado, abre el formulario genérico de la acción de inmediato en lugar de esperar 20 s.
|
|
14
|
+
- Tipos de documento (`registerDocumentKind`, `CORE_DOCUMENT_KINDS`) y contratos de PartyCard / TotalsPanel / PreviewPanel / ValidationChecklist.
|
|
15
|
+
- Manifest `document_forms`: `lines.kind`, `lines.open_documents`, `sources`, `preview` y `submit_action` en los tipos.
|
|
16
|
+
- Ayudantes de captura por modelo: `registerRecordPrefill` (también en `registry.scope(addon)`, se retira en el `unbind`) y `RecordPrefillBar`, que `DynamicRecordDialog` monta arriba del formulario; solo aplica claves que el formulario declara.
|
|
17
|
+
|
|
18
|
+
- 830349b: `DocumentEditor`: alta de documentos en una sola pantalla por secciones cuando el tipo de `document_forms` declara `layout: "editor"` (factura, nota de crédito, cobro/REP, cotización, OC). A la vista solo lo esencial (contraparte con su tarjeta, «Cargar desde…», renglones o reparto del pago, totales); los campos fiscales (`fiscal_data.*`, catálogos con default, contribuciones `header.fields`) van en «Opciones fiscales», plegada por defecto, y la vista previa también es opcional y plegada. Una sola acción primaria. Prefill desde el documento origen (`sources`, `initialSource`, `default_from_record` vía la nueva prop `record`) y validación inline. `lines.kind: "allocation"` reparte el cobro con `PaymentAllocator` (del más vencido o manual, con parcialidad) y guarda por `submit_action`. Nuevos exports: `DocumentEditor`, `PaymentAllocator`, `PartyCard`, `TotalsPanel`, `ValidationChecklist`, `PreviewPanel`, `CollapsibleSection`, `EditorSection` y las reglas puras de `document-editor-model`. Sin `layout: "editor"` el wizard sigue igual.
|
|
19
|
+
- 7d1bdf9: Alta guiada acotada a la vista filtrada: `scopeDocumentFormsToFilter(forms, filter)` y la prop `filter` de `DynamicCRUDPage`. Si el filtro fijo de la vista (el `filter` del nav) va sobre el `type_field` de `document_forms`, «Crear» abre ese tipo directo; si no hay formulario para ese tipo, no se ofrece. Antes «Documentos fiscales → Facturas → Crear» abría el formulario del REP.
|
|
20
|
+
- 9c34b09: Crear factura (DocumentFormDialog): el paso de renglones busca en el catálogo de productos aunque el host no pase `searchProducts`. Antes solo ofrecía «Renglón libre» y la factura se guardaba en $0. Al elegir un producto, el renglón entra con precio de venta, unidad, SKU, claves de extensión (`fiscal_data`), cantidad 1 y la tasa de impuesto del producto o, si no trae, la de la organización (`OrgRuntimeProvider taxRate`, nuevo). Nuevos: `createCatalogProductSearch`, props `productModel`/`defaultTaxRate` y `lines.discount_mode` en `document_forms`. Una cantidad vacía al leer renglones ya no queda en 0.
|
|
21
|
+
|
|
22
|
+
### Patch Changes
|
|
23
|
+
|
|
24
|
+
- 3c6e8eb: Corrige que al editar un registro (p. ej. un renglón dentro de una relación) un campo select con valor se borrara al guardar sin tocarlo. `DynamicForm` siembra los valores del registro de forma síncrona, ignora el `onValueChange('')` espurio del Select cuando ya hay valor y `''` no es opción válida, y en edición no manda selects vacíos que el usuario no tocó. `deriveRelationFormFields` ahora copia `readonly` de la columna, y el modal de alta/edición de renglón limita su alto al viewport con cuerpo con scroll y botones fijos abajo.
|
|
25
|
+
- 153dff8: El formulario de alta/edición (`DynamicRecordDialog`) ya no muestra claves de catálogo crudas como etiqueta de campo (p. ej. `FISCAL_MEXICO.EXT.CUSTOMER.RFC_RECEPTOR` en los campos que un addon agrega por extensión a Clientes o Productos). Si la etiqueta llega del servidor sin resolver, se traduce con el catálogo del cliente y, si tampoco está ahí, se muestra legible (`Codigo Postal`). Las etiquetas que ya llegan en texto humano no se tocan. Nuevo helper exportado `localizeFieldLabel`.
|
|
26
|
+
- db934ed: Kanban: los carriles de etapas ya no se pintan sobre otra columna. El host sirve un solo `group_by` por modelo, el del ÚLTIMO tablero de la navegación (en Taller, `service_bay_id`). Un tablero abierto sin `?group_by=` (por ejemplo, con el cambio de vista desde la lista) agrupaba las etapas por la bahía: todas las OT caían en «Sin etapa», los conteos de cada carril pedían `f_service_bay_id=diagnosis` (422) y al soltar una tarjeta se enviaba `service_bay_id = 'diagnosis'` (422 «no es un UUID válido»). `withGroupBy` ahora agrupa por el `stage_field` cuando no hay `?group_by=`. El tablero de la columna servida conserva la máquina de etapas solo si esa columna es la de etapas.
|
|
27
|
+
- 4ed3225: Renglones de formularios de acción: cuando la columna `total` es el precio unitario (`price`, `unit_price`…) y el renglón tiene cantidad pero ninguna columna de importe, la fila de total suma cantidad × precio − descuento de cada renglón y ya no los precios sueltos. En la OT de taller, cantidad 2 × 750 mostraba 750 en el total, aunque al guardar quedaba bien (1500). Si hay columna de importe (`subtotal`, `line_total`…), el comportamiento no cambia.
|
|
28
|
+
- f2e31cf: Las acciones por fila con `requiresState` se filtran contra el `stage_field` del modelo cuando hay máquina de etapas, igual que el kernel. Antes se comparaban con `status`, y por eso la OT de taller no mostraba «Procesar», «Esperar refacciones» ni «Cancelar» en la tabla ni en el kanban. Nuevo export `lifecycleStageField(metadata)`.
|
|
29
|
+
- Updated dependencies [7532829]
|
|
30
|
+
- @asteby/metacore-sdk@3.13.0
|
|
31
|
+
|
|
3
32
|
## 47.1.0
|
|
4
33
|
|
|
5
34
|
### Minor Changes
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"action-modal-dispatcher.d.ts","sourceRoot":"","sources":["../src/action-modal-dispatcher.tsx"],"names":[],"mappings":"AAmDA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AA8B/C,OAAO,KAAK,EAAE,cAAc,EAAmC,MAAM,SAAS,CAAA;AAE9E,OAAO,EACH,KAAK,cAAc,EACnB,KAAK,gBAAgB,EAGxB,MAAM,sBAAsB,CAAA;
|
|
1
|
+
{"version":3,"file":"action-modal-dispatcher.d.ts","sourceRoot":"","sources":["../src/action-modal-dispatcher.tsx"],"names":[],"mappings":"AAmDA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AA8B/C,OAAO,KAAK,EAAE,cAAc,EAAmC,MAAM,SAAS,CAAA;AAE9E,OAAO,EACH,KAAK,cAAc,EACnB,KAAK,gBAAgB,EAGxB,MAAM,sBAAsB,CAAA;AAM7B,YAAY,EAAE,cAAc,EAAE,gBAAgB,EAAE,CAAA;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D;;;2DAG2D;AAC3D,wBAAgB,wBAAwB,CACpC,GAAG,EAAE,OAAO,EACZ,MAAM,EAAE,SAAS,cAAc,EAAE,GAAG,SAAS,EAC7C,CAAC,EAAE,SAAS,EACZ,QAAQ,CAAC,EAAE,MAAM,GAClB,MAAM,CAWR;AA+bD,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,cAAc,EAAE,EAAE,MAAM,EAAE,GAAG,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAoB7F;AAyPD,oGAAoG;AACpG,wBAAgB,WAAW,CACvB,KAAK,EAAE,cAAc,EACrB,KAAK,EAAE,GAAG,EACV,QAAQ,EAAE,CAAC,KAAK,EAAE,GAAG,KAAK,IAAI,EAK9B,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAChC,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAC5B,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,+BAwEvC"}
|
|
@@ -16,7 +16,7 @@ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-run
|
|
|
16
16
|
// The host injects its axios-like client via <ApiProvider>; we no longer
|
|
17
17
|
// depend on a bundler alias to `@/lib/api`.
|
|
18
18
|
import { emitRecordMutation } from './record-mutation-events';
|
|
19
|
-
import { useState, useCallback, useEffect, useMemo, useRef, useSyncExternalStore } from 'react';
|
|
19
|
+
import { Suspense, useState, useCallback, useEffect, useMemo, useRef, useSyncExternalStore } from 'react';
|
|
20
20
|
import { useTranslation } from 'react-i18next';
|
|
21
21
|
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription, DialogFooter, AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, Button, Input, Textarea, Select, SelectContent, SelectItem, SelectTrigger, SelectValue, Switch, } from '@asteby/metacore-ui/primitives';
|
|
22
22
|
import { Loader2 } from 'lucide-react';
|
|
@@ -46,6 +46,9 @@ import { ViewValue, objectLabel, relationSiblingValue, isLineItemsField as isRec
|
|
|
46
46
|
// Canonical registry lives in @asteby/metacore-sdk
|
|
47
47
|
import { getActionComponent, subscribeActionComponents, } from '@asteby/metacore-sdk';
|
|
48
48
|
import { requestFederatedAction } from './federated-action-loader';
|
|
49
|
+
import { useInstalledAddons } from './installed-addons-context';
|
|
50
|
+
import { federatedModalComponent, resolveFederatedModal } from './primitives/contributions';
|
|
51
|
+
import { installedPredicate, useContributionsVersion } from './primitives/use-contributions';
|
|
49
52
|
export function isPrefillSpec(v) {
|
|
50
53
|
return (typeof v === 'object' &&
|
|
51
54
|
v !== null &&
|
|
@@ -248,13 +251,26 @@ export function ActionModalDispatcher({ open, onOpenChange, action, model, recor
|
|
|
248
251
|
// render, and a memoized lookup would pin the fallback forever.
|
|
249
252
|
const readComponent = () => getActionComponent(model, action.key);
|
|
250
253
|
const CustomComponent = useSyncExternalStore(subscribeActionComponents, readComponent, readComponent);
|
|
254
|
+
// Modal federado declarativo (registerFederatedModal). Si el addon dueño
|
|
255
|
+
// del slug consta como NO instalado, el `modal` se ignora y se usa el
|
|
256
|
+
// formulario genérico de la acción de inmediato (sin esperar el timeout).
|
|
257
|
+
useContributionsVersion();
|
|
258
|
+
const installed = useInstalledAddons();
|
|
259
|
+
const slug = action.modal || '';
|
|
260
|
+
const federatedModal = slug ? resolveFederatedModal(slug, installedPredicate(installed)) : null;
|
|
261
|
+
const modalAddonMissing = !!slug && !federatedModal && !!installed && !installed.addons.has(slug.split('.')[0]);
|
|
251
262
|
if (CustomComponent) {
|
|
252
263
|
return (_jsx(CustomComponent, { open: open, onOpenChange: onOpenChange, action: action, model: model, record: record, endpoint: endpoint, onSuccess: onSuccess }));
|
|
253
264
|
}
|
|
265
|
+
if (federatedModal) {
|
|
266
|
+
const FederatedComponent = federatedModalComponent(federatedModal);
|
|
267
|
+
return (_jsx(Suspense, { fallback: _jsx(LoadingActionDialog, { open: open, onOpenChange: onOpenChange, action: action }), children: _jsx(FederatedComponent, { open: open, onOpenChange: onOpenChange, action: action, model: model, record: record, endpoint: endpoint, onSuccess: onSuccess }) }));
|
|
268
|
+
}
|
|
254
269
|
// Declarative custom slot (`modal: "addon.action"`). Wait for the remote,
|
|
255
270
|
// then a hard error — confirm/fields generics look "fine" and hide a
|
|
256
|
-
// missing remote.
|
|
257
|
-
|
|
271
|
+
// missing remote. Exception: the slug's addon is known NOT installed — the
|
|
272
|
+
// remote will never come, so fall through to the generic form now.
|
|
273
|
+
if (action.modal && !modalAddonMissing) {
|
|
258
274
|
return (_jsx(PendingCustomActionModal, { open: open, onOpenChange: onOpenChange, action: action, model: model }));
|
|
259
275
|
}
|
|
260
276
|
if (action.steps && action.steps.length > 0) {
|
|
@@ -288,7 +304,6 @@ const MODAL_SLUG_RE = /^[\w-]+(\.[\w-]+)+$/;
|
|
|
288
304
|
* override from another addon that registerAction()s over it.
|
|
289
305
|
*/
|
|
290
306
|
function PendingCustomActionModal({ open, onOpenChange, action, model, }) {
|
|
291
|
-
const { t } = useTranslation();
|
|
292
307
|
const slug = action.modal || '';
|
|
293
308
|
const [timedOut, setTimedOut] = useState(false);
|
|
294
309
|
useEffect(() => {
|
|
@@ -329,6 +344,11 @@ function PendingCustomActionModal({ open, onOpenChange, action, model, }) {
|
|
|
329
344
|
if (timedOut) {
|
|
330
345
|
return _jsx(MissingCustomActionModal, { open: open, onOpenChange: onOpenChange, action: action, model: model });
|
|
331
346
|
}
|
|
347
|
+
return _jsx(LoadingActionDialog, { open: open, onOpenChange: onOpenChange, action: action });
|
|
348
|
+
}
|
|
349
|
+
/** Diálogo «Cargando…» mientras llega el componente federado de la acción. */
|
|
350
|
+
function LoadingActionDialog({ open, onOpenChange, action, }) {
|
|
351
|
+
const { t } = useTranslation();
|
|
332
352
|
return (_jsx(Dialog, { open: open, onOpenChange: onOpenChange, children: _jsxs(DialogContent, { children: [_jsxs(DialogHeader, { children: [_jsx(DialogTitle, { children: t(action.label, { defaultValue: action.label }) }), _jsx(DialogDescription, { children: t('dynamic.action_modal_loading', { defaultValue: 'Cargando…' }) })] }), _jsx("div", { className: "flex justify-center py-6", "data-testid": "federated-action-loading", children: _jsx(Loader2, { className: "h-6 w-6 animate-spin text-muted-foreground" }) })] }) }));
|
|
333
353
|
}
|
|
334
354
|
/** Shown when the action declares `modal` but no federated component registered. */
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { ProductQuery, ProductResult } from './product-search';
|
|
2
|
+
interface ApiLike {
|
|
3
|
+
get: (url: string, cfg?: {
|
|
4
|
+
params?: Record<string, unknown>;
|
|
5
|
+
signal?: AbortSignal;
|
|
6
|
+
}) => Promise<{
|
|
7
|
+
data: any;
|
|
8
|
+
}>;
|
|
9
|
+
}
|
|
10
|
+
export interface CatalogProductSearchOptions {
|
|
11
|
+
/** Modelo de catálogo. Default `products.Product`. */
|
|
12
|
+
model?: string;
|
|
13
|
+
/** Endpoint de listado. Default `/data/<model>`. */
|
|
14
|
+
endpoint?: string;
|
|
15
|
+
limit?: number;
|
|
16
|
+
/** Tasa de impuesto cuando el producto no trae la suya (config fiscal de la org; p. ej. 0.16). */
|
|
17
|
+
defaultTaxRate?: number;
|
|
18
|
+
}
|
|
19
|
+
/** Proyección pura registro del catálogo → ProductResult (exportada para tests). */
|
|
20
|
+
export declare function catalogRecordToProduct(rec: Record<string, any>, defaultTaxRate?: number): ProductResult;
|
|
21
|
+
export declare function createCatalogProductSearch(api: ApiLike, opts?: CatalogProductSearchOptions): (query: Exclude<ProductQuery, {
|
|
22
|
+
kind: "empty";
|
|
23
|
+
}>, signal: AbortSignal) => Promise<ProductResult[]>;
|
|
24
|
+
export {};
|
|
25
|
+
//# sourceMappingURL=catalog-product-search.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"catalog-product-search.d.ts","sourceRoot":"","sources":["../../src/business/catalog-product-search.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAA;AAEnE,UAAU,OAAO;IACb,GAAG,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE;QAAE,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;QAAC,MAAM,CAAC,EAAE,WAAW,CAAA;KAAE,KAAK,OAAO,CAAC;QAAE,IAAI,EAAE,GAAG,CAAA;KAAE,CAAC,CAAA;CACjH;AAED,MAAM,WAAW,2BAA2B;IACxC,sDAAsD;IACtD,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,oDAAoD;IACpD,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,kGAAkG;IAClG,cAAc,CAAC,EAAE,MAAM,CAAA;CAC1B;AA4BD,oFAAoF;AACpF,wBAAgB,sBAAsB,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,cAAc,CAAC,EAAE,MAAM,GAAG,aAAa,CAkBvG;AAED,wBAAgB,0BAA0B,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,GAAE,2BAAgC,IAI7E,OAAO,OAAO,CAAC,YAAY,EAAE;IAAE,IAAI,EAAE,OAAO,CAAA;CAAE,CAAC,EAAE,QAAQ,WAAW,KAAG,OAAO,CAAC,aAAa,EAAE,CAAC,CAShH"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
const num = (v) => {
|
|
2
|
+
const n = typeof v === 'string' && v.trim() !== '' ? Number(v) : typeof v === 'number' ? v : NaN;
|
|
3
|
+
return Number.isFinite(n) ? n : undefined;
|
|
4
|
+
};
|
|
5
|
+
const str = (v) => (typeof v === 'string' && v.trim() ? v.trim() : undefined);
|
|
6
|
+
/** Tasa como fracción: acepta 0.16 o 16. */
|
|
7
|
+
const rate = (v) => {
|
|
8
|
+
const n = num(v);
|
|
9
|
+
if (n == null || n < 0)
|
|
10
|
+
return undefined;
|
|
11
|
+
return n > 1 ? n / 100 : n;
|
|
12
|
+
};
|
|
13
|
+
const bag = (v) => {
|
|
14
|
+
if (typeof v === 'string') {
|
|
15
|
+
try {
|
|
16
|
+
const parsed = JSON.parse(v);
|
|
17
|
+
return parsed && typeof parsed === 'object' ? parsed : {};
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
return {};
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return v && typeof v === 'object' ? v : {};
|
|
24
|
+
};
|
|
25
|
+
/** Proyección pura registro del catálogo → ProductResult (exportada para tests). */
|
|
26
|
+
export function catalogRecordToProduct(rec, defaultTaxRate) {
|
|
27
|
+
// Campos de extensión de otros addons (p. ej. claves SAT de fiscal_mexico).
|
|
28
|
+
const extensions = Object.fromEntries(Object.entries(bag(rec.fiscal_data)).filter(([, v]) => typeof v === 'string' && v !== ''));
|
|
29
|
+
const pt = String(rec.product_type ?? '');
|
|
30
|
+
return {
|
|
31
|
+
id: String(rec.id ?? rec.value ?? ''),
|
|
32
|
+
name: String(rec.name ?? rec.label ?? ''),
|
|
33
|
+
sku: str(rec.sku),
|
|
34
|
+
barcode: str(rec.barcode),
|
|
35
|
+
kind: pt === 'service' ? 'service' : 'product',
|
|
36
|
+
price: num(rec.unit_price ?? rec.sale_price ?? rec.price),
|
|
37
|
+
cost: num(rec.cost_price ?? rec.cost),
|
|
38
|
+
tax_rate: rate(rec.tax_rate) ?? defaultTaxRate,
|
|
39
|
+
unit: str(rec.unit_of_measure) ?? str(rec.unit),
|
|
40
|
+
extensions: Object.keys(extensions).length ? extensions : undefined,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
export function createCatalogProductSearch(api, opts = {}) {
|
|
44
|
+
const model = opts.model ?? 'products.Product';
|
|
45
|
+
const url = opts.endpoint ?? `/data/${model}`;
|
|
46
|
+
const limit = opts.limit ?? 20;
|
|
47
|
+
return async (query, signal) => {
|
|
48
|
+
const search = query.kind === 'barcode' ? query.barcode : query.raw.trim();
|
|
49
|
+
const res = await api.get(url, { params: { search, per_page: limit }, signal });
|
|
50
|
+
const rows = res?.data?.data;
|
|
51
|
+
if (!Array.isArray(rows))
|
|
52
|
+
return [];
|
|
53
|
+
return rows
|
|
54
|
+
.filter((r) => r && typeof r === 'object' && r.is_active !== false)
|
|
55
|
+
.map((r) => catalogRecordToProduct(r, opts.defaultTaxRate));
|
|
56
|
+
};
|
|
57
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"catalog.d.ts","sourceRoot":"","sources":["../../src/business/catalog.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,qBAAqB;IAClC,+DAA+D;IAC/D,IAAI,EAAE,MAAM,CAAA;IACZ,yCAAyC;IACzC,KAAK,EAAE,MAAM,CAAA;IACb,uCAAuC;IACvC,MAAM,EAAE,MAAM,EAAE,CAAA;IAChB,4EAA4E;IAC5E,WAAW,EAAE,MAAM,EAAE,CAAA;IACrB,kDAAkD;IAClD,MAAM,EAAE,MAAM,EAAE,CAAA;IAChB,uDAAuD;IACvD,OAAO,EAAE,MAAM,EAAE,CAAA;CACpB;AAED,eAAO,MAAM,mBAAmB,EAAE,SAAS,qBAAqB,
|
|
1
|
+
{"version":3,"file":"catalog.d.ts","sourceRoot":"","sources":["../../src/business/catalog.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,qBAAqB;IAClC,+DAA+D;IAC/D,IAAI,EAAE,MAAM,CAAA;IACZ,yCAAyC;IACzC,KAAK,EAAE,MAAM,CAAA;IACb,uCAAuC;IACvC,MAAM,EAAE,MAAM,EAAE,CAAA;IAChB,4EAA4E;IAC5E,WAAW,EAAE,MAAM,EAAE,CAAA;IACrB,kDAAkD;IAClD,MAAM,EAAE,MAAM,EAAE,CAAA;IAChB,uDAAuD;IACvD,OAAO,EAAE,MAAM,EAAE,CAAA;CACpB;AAED,eAAO,MAAM,mBAAmB,EAAE,SAAS,qBAAqB,EAwGtD,CAAA"}
|
package/dist/business/catalog.js
CHANGED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { type LineItem } from './line-items';
|
|
2
|
+
import type { Allocation, AllocationIssue, OpenDocument } from '../primitives/allocation';
|
|
3
|
+
import type { ActionFieldDef, DocumentFormLines, DocumentFormOpenDocuments, DocumentFormsManifest, DocumentFormType } from '../types';
|
|
4
|
+
export declare const EXTENSION_PREFIX = "fiscal_data.";
|
|
5
|
+
export type EditorLinesConfig = DocumentFormLines & {
|
|
6
|
+
field: string;
|
|
7
|
+
};
|
|
8
|
+
/** true si el tipo se abre en el DocumentEditor (`layout: "editor"`). */
|
|
9
|
+
export declare function isEditorLayout(type: Pick<DocumentFormType, 'layout'> | null | undefined): boolean;
|
|
10
|
+
/** Paso de renglones efectivo del tipo (o undefined si no tiene). */
|
|
11
|
+
export declare function editorLinesConfig(type: DocumentFormType, forms: Pick<DocumentFormsManifest, 'lines_field'>): EditorLinesConfig | undefined;
|
|
12
|
+
/**
|
|
13
|
+
* Divulgación progresiva: ¿el campo va plegado en «Opciones fiscales»?
|
|
14
|
+
* - campos de extensión (`fiscal_data.*`) que aportan otros addons;
|
|
15
|
+
* - `section: "fiscal" | "advanced"`;
|
|
16
|
+
* - catálogos opcionales que ya traen un default (método, serie, relación…):
|
|
17
|
+
* el usuario no necesita tocarlos para guardar.
|
|
18
|
+
* Lo obligatorio sin default, la contraparte y el monto siempre están a la vista.
|
|
19
|
+
*/
|
|
20
|
+
export declare function isAdvancedField(f: ActionFieldDef, essentialKeys?: ReadonlySet<string>): boolean;
|
|
21
|
+
export interface EditorFieldGroups {
|
|
22
|
+
/** Campo que elige la contraparte (cliente/proveedor), si el tipo la declara. */
|
|
23
|
+
party?: ActionFieldDef;
|
|
24
|
+
/** A la vista: lo esencial del encabezado. */
|
|
25
|
+
essential: ActionFieldDef[];
|
|
26
|
+
/** Plegados en «Opciones fiscales». */
|
|
27
|
+
advanced: ActionFieldDef[];
|
|
28
|
+
/** Notas largas (textarea): al final. */
|
|
29
|
+
notes: ActionFieldDef[];
|
|
30
|
+
}
|
|
31
|
+
/** Reparte los campos del tipo (más los de extensión) en grupos de pantalla. */
|
|
32
|
+
export declare function splitEditorFields(fields: readonly ActionFieldDef[], opts?: {
|
|
33
|
+
partyField?: string;
|
|
34
|
+
essentialKeys?: readonly string[];
|
|
35
|
+
extensionFields?: readonly ActionFieldDef[];
|
|
36
|
+
}): EditorFieldGroups;
|
|
37
|
+
/**
|
|
38
|
+
* Etiqueta clara con el código como dato secundario:
|
|
39
|
+
* `03 · Transferencia electrónica` → `Transferencia electrónica · 03`.
|
|
40
|
+
* Solo reordena el patrón «CÓDIGO · texto»; cualquier otra etiqueta pasa igual.
|
|
41
|
+
*/
|
|
42
|
+
export declare function friendlyOptionLabel(label: string): string;
|
|
43
|
+
/** El mismo campo con sus opciones en formato «texto · código». */
|
|
44
|
+
export declare function withFriendlyOptions(f: ActionFieldDef): ActionFieldDef;
|
|
45
|
+
/**
|
|
46
|
+
* Defaults del encabezado que salen de la contraparte: los campos de extensión
|
|
47
|
+
* con la MISMA clave en ambos modelos (p. ej. `fiscal_data.uso_cfdi` de Customer
|
|
48
|
+
* → `fiscal_data.uso_cfdi` del documento) y la moneda.
|
|
49
|
+
* Solo llena lo que está vacío: nunca pisa lo que el usuario ya capturó.
|
|
50
|
+
*/
|
|
51
|
+
export declare function partyDefaults(party: Record<string, any> | null, documentFieldKeys: readonly string[], current: Record<string, unknown>): Record<string, unknown>;
|
|
52
|
+
/** Filas de la tarjeta de contraparte: resumen declarado + extensiones (máximo `limit`). */
|
|
53
|
+
export declare function partySummaryRows(party: Record<string, any> | null, summary?: readonly string[], labels?: Record<string, string>, limit?: number): Array<{
|
|
54
|
+
key: string;
|
|
55
|
+
label: string;
|
|
56
|
+
value: string;
|
|
57
|
+
}>;
|
|
58
|
+
export interface CreditStatus {
|
|
59
|
+
limit?: number;
|
|
60
|
+
balance?: number;
|
|
61
|
+
overdue?: number;
|
|
62
|
+
available?: number;
|
|
63
|
+
holdReason?: string;
|
|
64
|
+
/** `ok` | `warn` (supera 80 % o hay vencido) | `block` (retenido o sin crédito disponible). */
|
|
65
|
+
level: 'ok' | 'warn' | 'block';
|
|
66
|
+
}
|
|
67
|
+
export declare function creditStatus(party: Record<string, any> | null, cols: {
|
|
68
|
+
limit?: string;
|
|
69
|
+
balance?: string;
|
|
70
|
+
overdue?: string;
|
|
71
|
+
hold_reason?: string;
|
|
72
|
+
} | undefined, documentTotal: number): CreditStatus | null;
|
|
73
|
+
/**
|
|
74
|
+
* Renglones de un documento origen → LineItem. `map` traduce nombres
|
|
75
|
+
* (`description: product_name`, `discount: discount_pct`). Si el origen trae
|
|
76
|
+
* `tax_amount` y `subtotal` pero no la tasa, la deriva. `credit` (NC) topa la
|
|
77
|
+
* cantidad en lo facturado (o en `max_quantity` si el origen la trae).
|
|
78
|
+
*/
|
|
79
|
+
export declare function linesFromSource(rows: unknown, map?: Record<string, string>, opts?: {
|
|
80
|
+
kind?: DocumentFormLines['kind'];
|
|
81
|
+
}): LineItem[];
|
|
82
|
+
export interface EditorIssue {
|
|
83
|
+
field?: string;
|
|
84
|
+
severity: 'error' | 'warning';
|
|
85
|
+
message: string;
|
|
86
|
+
}
|
|
87
|
+
/** Revisión local (instantánea) de los renglones. Sin ruido: solo lo que hay que mirar. */
|
|
88
|
+
export declare function localIssues(lines: LineItem[], opts?: {
|
|
89
|
+
requireLines?: boolean;
|
|
90
|
+
requireTax?: boolean;
|
|
91
|
+
}): EditorIssue[];
|
|
92
|
+
/** Documento abierto (fila de /data) → OpenDocument para PaymentAllocator. */
|
|
93
|
+
export declare function toOpenDocuments(rows: unknown, cfg: DocumentFormOpenDocuments, currency: string): OpenDocument[];
|
|
94
|
+
/** Monto actual de cada documento, para pasar a modo manual sin perder lo repartido. */
|
|
95
|
+
export declare function manualFromAllocations(allocations: readonly Allocation[]): Record<string, number>;
|
|
96
|
+
/** Renglones del payload: solo los dos campos que declara la acción (`invoice_id`, `amount`). */
|
|
97
|
+
export declare function allocationPayload(allocations: readonly Allocation[], cfg: DocumentFormOpenDocuments): Array<Record<string, unknown>>;
|
|
98
|
+
/**
|
|
99
|
+
* Campo del encabezado con el monto recibido: `amount` si existe; si no, el
|
|
100
|
+
* primer campo numérico/moneda. Convención documentada (el schema no lo nombra).
|
|
101
|
+
*/
|
|
102
|
+
export declare function allocationAmountField(fields: readonly ActionFieldDef[]): string | undefined;
|
|
103
|
+
/** AllocationIssue → mensaje en español claro (con el número del documento). */
|
|
104
|
+
export declare function allocationIssueMessages(issues: readonly AllocationIssue[], docs: readonly OpenDocument[]): EditorIssue[];
|
|
105
|
+
/** Días de atraso de un documento abierto (0 si no está vencido o no tiene vencimiento). */
|
|
106
|
+
export declare function overdueDays(doc: Pick<OpenDocument, 'due_at'>, today?: Date): number;
|
|
107
|
+
//# sourceMappingURL=document-editor-model.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"document-editor-model.d.ts","sourceRoot":"","sources":["../../src/business/document-editor-model.ts"],"names":[],"mappings":"AAMA,OAAO,EAA2B,KAAK,QAAQ,EAAE,MAAM,cAAc,CAAA;AAGrE,OAAO,KAAK,EAAE,UAAU,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAA;AACzF,OAAO,KAAK,EACR,cAAc,EACd,iBAAiB,EACjB,yBAAyB,EACzB,qBAAqB,EACrB,gBAAgB,EACnB,MAAM,UAAU,CAAA;AAEjB,eAAO,MAAM,gBAAgB,iBAAiB,CAAA;AAK9C,MAAM,MAAM,iBAAiB,GAAG,iBAAiB,GAAG;IAAE,KAAK,EAAE,MAAM,CAAA;CAAE,CAAA;AAErE,yEAAyE;AACzE,wBAAgB,cAAc,CAAC,IAAI,EAAE,IAAI,CAAC,gBAAgB,EAAE,QAAQ,CAAC,GAAG,IAAI,GAAG,SAAS,GAAG,OAAO,CAEjG;AAED,qEAAqE;AACrE,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,g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E,QAAQ,EAAE,EAAE,IAAI,GAAE;IAAE,YAAY,CAAC,EAAE,OAAO,CAAC;IAAC,UAAU,CAAC,EAAE,OAAO,CAAA;CAAO,GAAG,WAAW,EAAE,CAezH;AAID,8EAA8E;AAC9E,wBAAgB,eAAe,CAAC,IAAI,EAAE,OAAO,EAAE,GAAG,EAAE,yBAAyB,EAAE,QAAQ,EAAE,MAAM,GAAG,YAAY,EAAE,CA0B/G;AAED,wFAAwF;AACxF,wBAAgB,qBAAqB,CAAC,WAAW,EAAE,SAAS,UAAU,EAAE,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAEhG;AAED,iGAAiG;AACjG,wBAAgB,iBAAiB,CAAC,WAAW,EAAE,SAAS,UAAU,EAAE,EAAE,GAAG,EAAE,yBAAyB,GAAG,KAAK,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAEpI;AAED;;;GAGG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,SAAS,cAAc,EAAE,GAAG,MAAM,GAAG,SAAS,CAK3F;AAUD,gFAAgF;AAChF,wBAAgB,uBAAuB,CAAC,MAAM,EAAE,SAAS,eAAe,EAAE,EAAE,IAAI,EAAE,SAAS,YAAY,EAAE,GAAG,WAAW,EAAE,CAUxH;AAED,4FAA4F;AAC5F,wBAAgB,WAAW,CAAC,GAAG,EAAE,IAAI,CAAC,YAAY,EAAE,QAAQ,CAAC,EAAE,KAAK,GAAE,IAAiB,GAAG,MAAM,CAM/F"}
|
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
// Reglas puras del DocumentEditor (sin React ni host): qué se ve a primera
|
|
2
|
+
// vista y qué va en «Opciones fiscales», qué copiar de la contraparte, cómo
|
|
3
|
+
// traer renglones de un documento origen, cómo repartir un cobro entre los
|
|
4
|
+
// documentos abiertos y qué revisar antes de guardar. Genérico: factura,
|
|
5
|
+
// cotización, pedido, OC, nota de crédito y cobro/REP. El SDK no conoce CFDI.
|
|
6
|
+
import { roundMoney, toAmount } from './format';
|
|
7
|
+
import { computeTotals, makeLine } from './line-items';
|
|
8
|
+
import { optionPassesRule, getOptionFilter } from '../option-filter';
|
|
9
|
+
export const EXTENSION_PREFIX = 'fiscal_data.';
|
|
10
|
+
/** Secciones de campo que el editor pliega en «Opciones fiscales». */
|
|
11
|
+
const ADVANCED_SECTIONS = new Set(['fiscal', 'advanced']);
|
|
12
|
+
/** true si el tipo se abre en el DocumentEditor (`layout: "editor"`). */
|
|
13
|
+
export function isEditorLayout(type) {
|
|
14
|
+
return type?.layout === 'editor';
|
|
15
|
+
}
|
|
16
|
+
/** Paso de renglones efectivo del tipo (o undefined si no tiene). */
|
|
17
|
+
export function editorLinesConfig(type, forms) {
|
|
18
|
+
if (!type.lines)
|
|
19
|
+
return undefined;
|
|
20
|
+
const cfg = type.lines === true ? {} : type.lines;
|
|
21
|
+
const fallback = cfg.kind === 'allocation' ? 'allocations' : 'lines';
|
|
22
|
+
return { ...cfg, field: cfg.field ?? forms.lines_field ?? fallback };
|
|
23
|
+
}
|
|
24
|
+
/** Lee `a.b.c` de un registro (incluye `fiscal_data.x` dentro del jsonb). */
|
|
25
|
+
function readPath(rec, path) {
|
|
26
|
+
if (!rec)
|
|
27
|
+
return undefined;
|
|
28
|
+
if (path in rec)
|
|
29
|
+
return rec[path];
|
|
30
|
+
return path.split('.').reduce((acc, k) => (acc == null ? undefined : acc[k]), rec);
|
|
31
|
+
}
|
|
32
|
+
const hasValue = (v) => v !== undefined && v !== null && v !== '';
|
|
33
|
+
function declaredDefault(f) {
|
|
34
|
+
return f.defaultValue ?? f.default;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Divulgación progresiva: ¿el campo va plegado en «Opciones fiscales»?
|
|
38
|
+
* - campos de extensión (`fiscal_data.*`) que aportan otros addons;
|
|
39
|
+
* - `section: "fiscal" | "advanced"`;
|
|
40
|
+
* - catálogos opcionales que ya traen un default (método, serie, relación…):
|
|
41
|
+
* el usuario no necesita tocarlos para guardar.
|
|
42
|
+
* Lo obligatorio sin default, la contraparte y el monto siempre están a la vista.
|
|
43
|
+
*/
|
|
44
|
+
export function isAdvancedField(f, essentialKeys = new Set()) {
|
|
45
|
+
if (essentialKeys.has(f.key))
|
|
46
|
+
return false;
|
|
47
|
+
if (f.key.startsWith(EXTENSION_PREFIX))
|
|
48
|
+
return true;
|
|
49
|
+
if (f.section && ADVANCED_SECTIONS.has(f.section))
|
|
50
|
+
return true;
|
|
51
|
+
const hasOptions = Array.isArray(f.options) && f.options.length > 0;
|
|
52
|
+
return !f.required && hasOptions && hasValue(declaredDefault(f));
|
|
53
|
+
}
|
|
54
|
+
/** Reparte los campos del tipo (más los de extensión) en grupos de pantalla. */
|
|
55
|
+
export function splitEditorFields(fields, opts = {}) {
|
|
56
|
+
const essentialKeys = new Set(opts.essentialKeys ?? []);
|
|
57
|
+
const out = { essential: [], advanced: [], notes: [] };
|
|
58
|
+
const seen = new Set();
|
|
59
|
+
for (const f of [...fields, ...(opts.extensionFields ?? [])]) {
|
|
60
|
+
if (seen.has(f.key))
|
|
61
|
+
continue;
|
|
62
|
+
seen.add(f.key);
|
|
63
|
+
if (opts.partyField && f.key === opts.partyField)
|
|
64
|
+
out.party = f;
|
|
65
|
+
else if (f.type === 'textarea' || f.widget === 'textarea')
|
|
66
|
+
out.notes.push(f);
|
|
67
|
+
else if (isAdvancedField(f, essentialKeys))
|
|
68
|
+
out.advanced.push(f);
|
|
69
|
+
else
|
|
70
|
+
out.essential.push(f);
|
|
71
|
+
}
|
|
72
|
+
return out;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Etiqueta clara con el código como dato secundario:
|
|
76
|
+
* `03 · Transferencia electrónica` → `Transferencia electrónica · 03`.
|
|
77
|
+
* Solo reordena el patrón «CÓDIGO · texto»; cualquier otra etiqueta pasa igual.
|
|
78
|
+
*/
|
|
79
|
+
export function friendlyOptionLabel(label) {
|
|
80
|
+
const m = /^\s*([A-Z0-9]{1,5})\s*[·\-–]\s*(.+)$/.exec(label);
|
|
81
|
+
return m && /\d/.test(m[1]) ? `${m[2].trim()} · ${m[1]}` : label;
|
|
82
|
+
}
|
|
83
|
+
/** El mismo campo con sus opciones en formato «texto · código». */
|
|
84
|
+
export function withFriendlyOptions(f) {
|
|
85
|
+
if (!Array.isArray(f.options) || f.options.length === 0)
|
|
86
|
+
return f;
|
|
87
|
+
return { ...f, options: f.options.map((o) => ({ ...o, label: friendlyOptionLabel(String(o.label ?? o.value)) })) };
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Defaults del encabezado que salen de la contraparte: los campos de extensión
|
|
91
|
+
* con la MISMA clave en ambos modelos (p. ej. `fiscal_data.uso_cfdi` de Customer
|
|
92
|
+
* → `fiscal_data.uso_cfdi` del documento) y la moneda.
|
|
93
|
+
* Solo llena lo que está vacío: nunca pisa lo que el usuario ya capturó.
|
|
94
|
+
*/
|
|
95
|
+
export function partyDefaults(party, documentFieldKeys, current) {
|
|
96
|
+
if (!party)
|
|
97
|
+
return {};
|
|
98
|
+
const out = {};
|
|
99
|
+
for (const key of documentFieldKeys) {
|
|
100
|
+
if (!key.startsWith(EXTENSION_PREFIX) || hasValue(current[key]))
|
|
101
|
+
continue;
|
|
102
|
+
const v = readPath(party, key);
|
|
103
|
+
if (hasValue(v))
|
|
104
|
+
out[key] = v;
|
|
105
|
+
}
|
|
106
|
+
if (!hasValue(current.currency_code) && typeof party.currency_code === 'string' && party.currency_code) {
|
|
107
|
+
out.currency_code = party.currency_code;
|
|
108
|
+
}
|
|
109
|
+
return out;
|
|
110
|
+
}
|
|
111
|
+
/** Filas de la tarjeta de contraparte: resumen declarado + extensiones (máximo `limit`). */
|
|
112
|
+
export function partySummaryRows(party, summary = [], labels = {}, limit = 6) {
|
|
113
|
+
if (!party)
|
|
114
|
+
return [];
|
|
115
|
+
const keys = new Set(summary);
|
|
116
|
+
const fd = party.fiscal_data;
|
|
117
|
+
if (fd && typeof fd === 'object')
|
|
118
|
+
for (const k of Object.keys(fd))
|
|
119
|
+
keys.add(EXTENSION_PREFIX + k);
|
|
120
|
+
const rows = [];
|
|
121
|
+
for (const key of keys) {
|
|
122
|
+
const v = readPath(party, key);
|
|
123
|
+
if (!hasValue(v) || typeof v === 'object')
|
|
124
|
+
continue;
|
|
125
|
+
rows.push({ key, label: labels[key] ?? key.replace(EXTENSION_PREFIX, ''), value: String(v) });
|
|
126
|
+
}
|
|
127
|
+
return rows.slice(0, limit);
|
|
128
|
+
}
|
|
129
|
+
export function creditStatus(party, cols, documentTotal) {
|
|
130
|
+
if (!party || !cols)
|
|
131
|
+
return null;
|
|
132
|
+
const n = (k) => (k && hasValue(party[k]) ? toAmount(party[k]) : undefined);
|
|
133
|
+
const limit = n(cols.limit);
|
|
134
|
+
const balance = n(cols.balance);
|
|
135
|
+
const overdue = n(cols.overdue);
|
|
136
|
+
const holdReason = cols.hold_reason ? party[cols.hold_reason] || undefined : undefined;
|
|
137
|
+
if (limit == null && balance == null && overdue == null && !holdReason)
|
|
138
|
+
return null;
|
|
139
|
+
const available = limit != null ? roundMoney(limit - (balance ?? 0)) : undefined;
|
|
140
|
+
let level = 'ok';
|
|
141
|
+
if (holdReason || (available != null && limit > 0 && available < documentTotal))
|
|
142
|
+
level = 'block';
|
|
143
|
+
else if ((overdue ?? 0) > 0 || (limit != null && limit > 0 && (balance ?? 0) + documentTotal > 0.8 * limit))
|
|
144
|
+
level = 'warn';
|
|
145
|
+
return { limit, balance, overdue, available, holdReason, level };
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Renglones de un documento origen → LineItem. `map` traduce nombres
|
|
149
|
+
* (`description: product_name`, `discount: discount_pct`). Si el origen trae
|
|
150
|
+
* `tax_amount` y `subtotal` pero no la tasa, la deriva. `credit` (NC) topa la
|
|
151
|
+
* cantidad en lo facturado (o en `max_quantity` si el origen la trae).
|
|
152
|
+
*/
|
|
153
|
+
export function linesFromSource(rows, map = {}, opts = {}) {
|
|
154
|
+
if (!Array.isArray(rows))
|
|
155
|
+
return [];
|
|
156
|
+
return rows.map((raw) => {
|
|
157
|
+
const r = (raw ?? {});
|
|
158
|
+
const get = (k) => r[map[k] ?? k];
|
|
159
|
+
const qty = toAmount(get('quantity') ?? 1);
|
|
160
|
+
let rate = get('tax_rate');
|
|
161
|
+
if (!hasValue(rate)) {
|
|
162
|
+
const sub = toAmount(get('subtotal'));
|
|
163
|
+
const tax = toAmount(get('tax_amount'));
|
|
164
|
+
rate = sub > 0 ? Math.round((tax / sub) * 10000) / 10000 : 0;
|
|
165
|
+
}
|
|
166
|
+
const r2 = toAmount(rate);
|
|
167
|
+
const line = makeLine({
|
|
168
|
+
product_id: get('product_id') ?? undefined,
|
|
169
|
+
sku: get('sku') ?? undefined,
|
|
170
|
+
description: String(get('description') ?? get('product_name') ?? ''),
|
|
171
|
+
quantity: qty,
|
|
172
|
+
unit_price: toAmount(get('unit_price')),
|
|
173
|
+
discount: toAmount(get('discount') ?? 0),
|
|
174
|
+
tax_rate: r2 > 1 ? r2 / 100 : r2,
|
|
175
|
+
unit: get('unit') ?? undefined,
|
|
176
|
+
});
|
|
177
|
+
if (opts.kind === 'credit')
|
|
178
|
+
line.max_quantity = hasValue(get('max_quantity')) ? toAmount(get('max_quantity')) : qty;
|
|
179
|
+
return line;
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
/** Revisión local (instantánea) de los renglones. Sin ruido: solo lo que hay que mirar. */
|
|
183
|
+
export function localIssues(lines, opts = {}) {
|
|
184
|
+
const issues = [];
|
|
185
|
+
const items = lines.filter((l) => l.kind === 'item');
|
|
186
|
+
if ((opts.requireLines ?? true) && items.length === 0) {
|
|
187
|
+
issues.push({ field: 'lines', severity: 'error', message: 'Agrega al menos un renglón.' });
|
|
188
|
+
}
|
|
189
|
+
items.forEach((l, i) => {
|
|
190
|
+
if (!l.description.trim())
|
|
191
|
+
issues.push({ field: `lines.${i}`, severity: 'error', message: `Renglón ${i + 1}: falta la descripción.` });
|
|
192
|
+
if (toAmount(l.unit_price) <= 0)
|
|
193
|
+
issues.push({ field: `lines.${i}`, severity: 'warning', message: `Renglón ${i + 1}: precio en cero.` });
|
|
194
|
+
if (opts.requireTax && toAmount(l.tax_rate) === 0) {
|
|
195
|
+
issues.push({ field: `lines.${i}`, severity: 'warning', message: `Renglón ${i + 1}: sin impuesto.` });
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
if (computeTotals(lines).total < 0)
|
|
199
|
+
issues.push({ severity: 'error', message: 'El total no puede ser negativo.' });
|
|
200
|
+
return issues;
|
|
201
|
+
}
|
|
202
|
+
// ---- Cobro / REP (lines.kind = allocation) ----------------------------------
|
|
203
|
+
/** Documento abierto (fila de /data) → OpenDocument para PaymentAllocator. */
|
|
204
|
+
export function toOpenDocuments(rows, cfg, currency) {
|
|
205
|
+
if (!Array.isArray(rows))
|
|
206
|
+
return [];
|
|
207
|
+
const rules = getOptionFilter(cfg);
|
|
208
|
+
const out = [];
|
|
209
|
+
for (const raw of rows) {
|
|
210
|
+
if (!raw || typeof raw !== 'object')
|
|
211
|
+
continue;
|
|
212
|
+
const r = raw;
|
|
213
|
+
if (rules.length) {
|
|
214
|
+
const opt = { id: r.id, value: r.id, label: String(r[cfg.number_field] ?? ''), name: '', meta: r };
|
|
215
|
+
if (!rules.every((rule) => optionPassesRule(opt, rule)))
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
const balance = roundMoney(toAmount(readPath(r, cfg.balance_field)));
|
|
219
|
+
if (balance <= 0)
|
|
220
|
+
continue;
|
|
221
|
+
out.push({
|
|
222
|
+
id: String(r.id),
|
|
223
|
+
number: String(readPath(r, cfg.number_field) ?? r.id),
|
|
224
|
+
issued_at: String(readPath(r, cfg.issued_field ?? 'issued_at') ?? r.created_at ?? ''),
|
|
225
|
+
due_at: (cfg.due_field ? readPath(r, cfg.due_field) : null) ?? null,
|
|
226
|
+
total: roundMoney(toAmount(cfg.total_field ? readPath(r, cfg.total_field) : balance)),
|
|
227
|
+
balance,
|
|
228
|
+
currency: String(r.currency_code ?? r.currency ?? currency),
|
|
229
|
+
payment_method: cfg.method_field ? (readPath(r, cfg.method_field) ?? null) : null,
|
|
230
|
+
installments_paid: hasValue(r.installments_paid) ? toAmount(r.installments_paid) : undefined,
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
return out;
|
|
234
|
+
}
|
|
235
|
+
/** Monto actual de cada documento, para pasar a modo manual sin perder lo repartido. */
|
|
236
|
+
export function manualFromAllocations(allocations) {
|
|
237
|
+
return Object.fromEntries(allocations.map((a) => [a.document_id, a.amount]));
|
|
238
|
+
}
|
|
239
|
+
/** Renglones del payload: solo los dos campos que declara la acción (`invoice_id`, `amount`). */
|
|
240
|
+
export function allocationPayload(allocations, cfg) {
|
|
241
|
+
return allocations.filter((a) => a.amount > 0).map((a) => ({ [cfg.line_document_field]: a.document_id, [cfg.line_amount_field]: a.amount }));
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Campo del encabezado con el monto recibido: `amount` si existe; si no, el
|
|
245
|
+
* primer campo numérico/moneda. Convención documentada (el schema no lo nombra).
|
|
246
|
+
*/
|
|
247
|
+
export function allocationAmountField(fields) {
|
|
248
|
+
return (fields.find((f) => f.key === 'amount')?.key ??
|
|
249
|
+
fields.find((f) => ['number', 'numeric', 'decimal', 'currency', 'money'].includes(f.type) || f.widget === 'currency')?.key);
|
|
250
|
+
}
|
|
251
|
+
const ALLOCATION_MESSAGES = {
|
|
252
|
+
non_positive: 'Captura el monto recibido.',
|
|
253
|
+
over_balance: 'Se aplica más que el saldo del documento.',
|
|
254
|
+
over_amount: 'Lo aplicado supera el monto recibido.',
|
|
255
|
+
currency_mismatch: 'El documento está en otra moneda.',
|
|
256
|
+
unapplied: 'Queda monto sin aplicar.',
|
|
257
|
+
};
|
|
258
|
+
/** AllocationIssue → mensaje en español claro (con el número del documento). */
|
|
259
|
+
export function allocationIssueMessages(issues, docs) {
|
|
260
|
+
const byId = new Map(docs.map((d) => [d.id, d]));
|
|
261
|
+
return issues.map((i) => {
|
|
262
|
+
const doc = i.document_id ? byId.get(i.document_id) : undefined;
|
|
263
|
+
return {
|
|
264
|
+
field: i.code === 'non_positive' ? 'amount' : 'allocations',
|
|
265
|
+
severity: i.severity,
|
|
266
|
+
message: doc ? `${doc.number}: ${ALLOCATION_MESSAGES[i.code]}` : ALLOCATION_MESSAGES[i.code],
|
|
267
|
+
};
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
/** Días de atraso de un documento abierto (0 si no está vencido o no tiene vencimiento). */
|
|
271
|
+
export function overdueDays(doc, today = new Date()) {
|
|
272
|
+
if (!doc.due_at)
|
|
273
|
+
return 0;
|
|
274
|
+
const due = new Date(doc.due_at);
|
|
275
|
+
if (Number.isNaN(due.getTime()))
|
|
276
|
+
return 0;
|
|
277
|
+
const ms = Date.UTC(today.getFullYear(), today.getMonth(), today.getDate()) - Date.UTC(due.getUTCFullYear(), due.getUTCMonth(), due.getUTCDate());
|
|
278
|
+
return Math.max(0, Math.floor(ms / 86_400_000));
|
|
279
|
+
}
|