Files
basic-erp/apps/web/src/app/features/documents/document-form.page.ts
T

345 lines
15 KiB
TypeScript
Raw Normal View History

import { HttpErrorResponse, httpResource } from '@angular/common/http';
import { ChangeDetectionStrategy, Component, computed, inject, linkedSignal, signal } from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
import { ActivatedRoute, Router, RouterLink } from '@angular/router';
import type { SlListResponse } from '@basic-erp/sap-types';
import { map } from 'rxjs';
import { API_URL, apiErrorMessage } from '../../core/api';
import { formatNumber } from '../../core/format';
import { SlDocumentApi } from '../../core/sl/sl-document';
import { DateField, LookupField, NumberField, TextField, TextareaField } from '../../shared/form';
import type { GridLine } from '../../shared/grid/grid.types';
import { LinesGrid } from '../../shared/grid/lines-grid';
import type { VhLookup } from '../../shared/value-help/value-help.types';
import { Icon, MenuButton, type MenuItem, SboWindow, Toolbar, type StatusMessage, type WindowBadge } from '../../shared/window';
import { ValueHelp } from '../../shared/value-help/value-help';
import type { ListRow } from '../../shared/list/list.types';
import {
type DocRecord,
type NavTarget,
computeTotals,
createPayload,
isBlankLine,
isDocumentReadOnly,
taxRateMap,
updatePayload,
validateDocument,
} from './document-model';
import { type CopyOrigin, copyFromDocument, copySources, copyTargets } from './document-copy';
import { DOCUMENT_SCREENS, type DocumentScreen, docNum, documentStatus, menuLabel } from './document-screens';
import { ITEMS_MULTI_ADD, lineColumnsFor, newDocumentLine, recalcLine } from './line-columns';
/** Campos de cabecera que se editan en el formulario (el resto lo calcula SAP). */
const HEADER_KEYS = ['CardCode', 'NumAtCard', 'DocDate', 'DocDueDate', 'DiscountPercent', 'Comments'] as const;
const today = (): string => new Date().toISOString().slice(0, 10);
/** SAP devuelve las fechas como `2026-01-05T00:00:00Z`; el campo de fecha y el guardado usan `2026-01-05`. */
const dateOnly = (v: unknown): string => (typeof v === 'string' ? v.slice(0, 10) : '');
const pickHeader = (doc: DocRecord | undefined): DocRecord => {
if (!doc) return { DocDate: today(), DocDueDate: today(), DiscountPercent: 0 };
return Object.fromEntries(
HEADER_KEYS.map((k) => [
k,
k === 'DocDate' || k === 'DocDueDate' ? dateOnly(doc[k]) : (doc[k] ?? (k === 'DiscountPercent' ? 0 : '')),
]),
);
};
const text = (v: unknown): string => (v === null || v === undefined ? '' : String(v));
/**
* Formulario de un documento de ventas o compras: `nuevo` (vacío) o `:docEntry` (ver / editar).
* Los cerrados y cancelados quedan en solo lectura. Al modificar se envía el `odata.etag` leído
* (`If-Match`): si otro usuario cambió el documento, SAP responde 412 y se avisa sin pisarlo.
*/
@Component({
selector: 'sbo-document-form-page',
changeDetection: ChangeDetectionStrategy.OnPush,
imports: [DateField, Icon, LinesGrid, LookupField, MenuButton, NumberField, RouterLink, SboWindow, TextField, TextareaField, Toolbar, ValueHelp],
templateUrl: './document-form.page.html',
styles: `
h1 { margin: 0 0 6px; font-size: 18px; font-weight: 600; }
p { margin: 0; color: var(--quiet); }
a { color: var(--focus); }
.totals { display: grid; gap: 2px; }
`,
})
export class DocumentFormPage {
private readonly route = inject(ActivatedRoute);
private readonly router = inject(Router);
private readonly api = inject(SlDocumentApi);
protected readonly title = this.route.snapshot.data['title'] as string;
protected readonly screen = this.route.snapshot.data['screen'] as DocumentScreen;
protected readonly partner = this.screen.partner;
protected readonly docEntry = toSignal(
this.route.paramMap.pipe(map((p) => (p.has('docEntry') ? Number(p.get('docEntry')) : null))),
{ initialValue: this.route.snapshot.paramMap.has('docEntry') ? Number(this.route.snapshot.paramMap.get('docEntry')) : null },
);
protected readonly isNew = computed(() => this.docEntry() === null);
// --- datos ---------------------------------------------------------------------------------
private readonly docResource = httpResource<DocRecord>(() => {
const id = this.docEntry();
return id === null ? undefined : `${API_URL}/sl/${this.screen.resource}/${id}`;
});
private readonly taxResource = httpResource<SlListResponse<DocRecord>>(() =>
this.partner
? { url: `${API_URL}/sl/${this.partner.taxResource}`, params: { $select: 'Code,Rate', $top: 500 } }
: undefined,
);
protected readonly doc = computed(() => (this.docResource.hasValue() ? this.docResource.value() : undefined));
protected readonly loading = computed(() => this.docResource.isLoading());
protected readonly loadError = computed(() => {
const err = this.docResource.error();
return err ? (err instanceof HttpErrorResponse && err.status === 404 ? 'El documento no existe.' : apiErrorMessage(err)) : '';
});
/** Valores editables; se reinician al cargar o recargar el documento. */
protected readonly header = linkedSignal<DocRecord>(() => pickHeader(this.doc()));
protected readonly lines = linkedSignal<readonly GridLine[]>(() =>
((this.doc()?.['DocumentLines'] as GridLine[] | undefined) ?? []).map((l) => ({ ...l })),
);
protected readonly partnerName = linkedSignal(() => text(this.doc()?.['CardName']));
// --- estado --------------------------------------------------------------------------------
protected readonly readOnly = computed(() => isDocumentReadOnly(this.doc()));
protected readonly saving = signal(false);
private readonly message = signal<StatusMessage | null>(
((): StatusMessage | null => {
const created = (this.router.currentNavigation()?.extras.state as { created?: string } | undefined)?.created;
return created ? { kind: 'ok', text: `Documento ${created} creado.` } : null;
})(),
);
protected readonly conflict = signal(false);
protected readonly status = computed<StatusMessage>(() => {
if (this.loading()) return { kind: 'info', text: 'Cargando…' };
if (this.loadError()) return { kind: 'err', text: this.loadError() };
const message = this.message();
if (message) return message;
if (this.readOnly()) return { kind: 'info', text: 'Documento cerrado o cancelado: solo lectura.' };
if (this.isNew()) return { kind: 'info', text: `Indique el ${this.partner?.label.toLowerCase() ?? 'socio'} y las líneas.` };
return { kind: 'info', text: this.dirty() ? 'Hay cambios sin guardar.' : '' };
});
protected readonly badge = computed<WindowBadge | undefined>(() => {
if (this.isNew()) return { label: 'Nueva', tone: 'info' };
const d = this.doc();
return d ? documentStatus(d) : undefined;
});
protected readonly windowTitle = computed(() => {
const num = this.doc()?.['DocNum'];
return num === undefined ? this.title : `${this.title} · ${text(num)}`;
});
protected readonly dirty = computed(() => {
const d = this.doc();
if (!d) return text(this.header()['CardCode']) !== '' || this.lines().some((l) => !isBlankLine(l));
const original = pickHeader(d);
const { body } = updatePayload(original, this.header(), (d['DocumentLines'] as GridLine[]) ?? [], this.lines());
return Object.keys(body).length > 0;
});
// --- totales -------------------------------------------------------------------------------
private readonly rates = computed(() =>
taxRateMap(this.taxResource.hasValue() ? this.taxResource.value().value : []),
);
protected readonly totals = computed(() =>
computeTotals(this.lines(), this.rates(), Number(this.header()['DiscountPercent'] ?? 0)),
);
protected readonly fmt = (n: number): string => formatNumber(n, 2);
// --- copiar de / a ----------------------------------------------------------------------------
private readonly copyOrigin = (this.router.currentNavigation()?.extras.state as { copyFrom?: CopyOrigin } | undefined)?.copyFrom;
protected readonly copyListFor = signal<string | null>(null);
/** "Copiar a": solo desde un documento guardado, abierto y sin cambios pendientes. */
protected readonly copyToItems = computed<MenuItem[]>(() => {
const blocked = this.isNew() || this.readOnly() || this.dirty();
return copyTargets(this.screen.path).map((p) => ({ id: p, label: menuLabel(p), disabled: blocked }));
});
protected readonly copyFromItems = computed<MenuItem[]>(() =>
copySources(this.screen.path).map((p) => ({ id: p, label: menuLabel(p), disabled: !this.isNew() })),
);
protected readonly copyListScreen = computed(() => DOCUMENT_SCREENS.find((s) => s.path === this.copyListFor()));
/** Documentos abiertos del origen elegido, del mismo socio si ya se indicó. */
protected readonly copyListFilter = computed(() => {
const card = text(this.header()['CardCode']);
return [`DocumentStatus eq 'bost_Open'`, `Cancelled eq 'tNO'`, ...(card ? [`CardCode eq '${card.replace(/'/g, "''")}'`] : [])].join(' and ');
});
protected readonly copyListColumns = [
docNum,
{ key: 'CardCode', title: this.partner?.label ?? 'Socio', width: '120px', mono: true },
{ key: 'CardName', title: 'Nombre' },
{ key: 'DocDate', title: 'Fecha', type: 'date', align: 'center', width: '105px' },
] as const;
protected copyTo(path: string): void {
const docEntry = this.docEntry();
if (docEntry === null) return;
const origin: CopyOrigin = { path: this.screen.path, docEntry };
void this.router.navigateByUrl(`${path}/nuevo`, { state: { copyFrom: origin } });
}
protected copyFrom(path: string): void {
this.copyListFor.set(path);
}
protected async copyPicked(rows: ListRow[]): Promise<void> {
const path = this.copyListFor();
this.copyListFor.set(null);
const docEntry = rows[0]?.['DocEntry'];
if (path && typeof docEntry === 'number') await this.loadCopy({ path, docEntry });
}
/** Trae el documento origen y vuelca sus datos y líneas abiertas en el formulario. */
private async loadCopy(origin: CopyOrigin): Promise<void> {
const source = DOCUMENT_SCREENS.find((s) => s.path === origin.path);
if (!source) return;
try {
const doc = await this.api.get(source.resource, origin.docEntry);
const copied = copyFromDocument(doc, origin.path, menuLabel(origin.path));
const card = text(this.header()['CardCode']);
if (card && card !== text(copied.header['CardCode'])) {
this.message.set({ kind: 'warn', text: `El documento es de ${text(copied.header['CardCode'])}; este es de ${card}.` });
return;
}
if (!copied.lines.length) {
this.message.set({ kind: 'warn', text: 'El documento no tiene líneas abiertas para copiar.' });
return;
}
if (!card) {
this.header.update((h) => ({ ...h, ...copied.header }));
this.partnerName.set(text(doc['CardName']));
}
this.lines.update((ls) => [...ls.filter((l) => !isBlankLine(l)), ...copied.lines]);
this.message.set({
kind: 'ok',
text: `Copiado de ${menuLabel(origin.path).toLowerCase()} ${text(doc['DocNum'])}${copied.skipped ? ` (${copied.skipped} línea(s) cerradas no se copian)` : ''}. Revise y pulse Crear.`,
});
} catch (e) {
this.message.set({ kind: 'err', text: apiErrorMessage(e) });
}
}
// --- configuración de la vista --------------------------------------------------------------
protected readonly columns = lineColumnsFor(this.partner?.taxResource ?? 'SalesTaxCodes');
protected readonly newLine = newDocumentLine;
protected readonly recalc = recalcLine;
protected readonly multi = ITEMS_MULTI_ADD;
protected readonly partnerLookup: VhLookup = {
title: this.partner?.type === 'cSupplier' ? 'Proveedores' : 'Clientes',
resource: 'BusinessPartners',
baseFilter: `CardType eq '${this.partner?.type ?? 'cCustomer'}'`,
orderby: 'CardCode',
columns: [
{ key: 'CardCode', title: 'Código', width: '130px', mono: true },
{ key: 'CardName', title: 'Nombre' },
],
};
protected str = text;
constructor() {
if (this.copyOrigin) void this.loadCopy(this.copyOrigin);
}
protected setHeader(key: string, value: unknown): void {
this.header.update((h) => ({ ...h, [key]: value }));
this.message.set(null);
}
/** Al escribir otro código el nombre anterior ya no vale; se completa al salir del campo o al elegir de la lista. */
protected onPartnerText(code: string): void {
if (code === text(this.header()['CardCode'])) return;
this.setHeader('CardCode', code);
this.partnerName.set('');
}
protected notify(textMessage: string): void {
this.message.set({ kind: 'warn', text: textMessage });
}
protected onPartner(row: Record<string, unknown>): void {
this.partnerName.set(text(row['CardName']));
}
protected setLines(lines: readonly GridLine[]): void {
this.lines.set(lines);
this.message.set(null);
}
// --- acciones ------------------------------------------------------------------------------
protected async go(target: NavTarget): Promise<void> {
try {
const entry = await this.api.neighbour(this.screen.resource, target, this.docEntry());
if (entry === null) {
this.message.set({ kind: 'warn', text: 'No hay más documentos en esa dirección.' });
return;
}
this.message.set(null);
await this.router.navigate(['..', entry], { relativeTo: this.route });
} catch (e) {
this.message.set({ kind: 'err', text: apiErrorMessage(e) });
}
}
protected async save(): Promise<void> {
if (this.saving() || this.readOnly()) return;
const errors = validateDocument(this.header(), this.lines(), this.partner?.label ?? 'Socio de negocio');
if (errors.length) {
this.message.set({ kind: 'err', text: errors[0] });
return;
}
this.saving.set(true);
this.conflict.set(false);
try {
const d = this.doc();
if (!d) {
const created = await this.api.create(this.screen.resource, createPayload(this.header(), this.lines()));
await this.router.navigate(['..', created['DocEntry']], {
relativeTo: this.route,
state: { created: text(created['DocNum']) },
});
return;
}
const { body, replaceLines } = updatePayload(
pickHeader(d),
this.header(),
(d['DocumentLines'] as GridLine[]) ?? [],
this.lines(),
);
if (!Object.keys(body).length) {
this.message.set({ kind: 'info', text: 'No hay cambios que guardar.' });
return;
}
await this.api.update(this.screen.resource, this.docEntry()!, body, text(d['odata.etag']) || undefined, replaceLines);
this.message.set({ kind: 'ok', text: 'Documento actualizado.' });
this.docResource.reload();
} catch (e) {
if (e instanceof HttpErrorResponse && e.status === 412) {
this.conflict.set(true);
this.message.set({
kind: 'warn',
text: 'Otro usuario modificó este documento. Recárguelo para ver sus cambios; los suyos no se guardaron.',
});
} else {
this.message.set({ kind: 'err', text: apiErrorMessage(e) });
}
} finally {
this.saving.set(false);
}
}
protected reload(): void {
this.conflict.set(false);
this.message.set(null);
this.docResource.reload();
}
}