"use client"; import { useState } from "react"; import { CustomerPicker } from "@/components/CustomerPicker"; import { createMovement } from "@/lib/api"; import type { CreateMovementInput, Currency, Facet, LedgerCurrency, TransactionDomain, } from "@/lib/types"; const DOMAINS: { key: TransactionDomain; label: string }[] = [ { key: "UTILITY", label: "Servicios" }, { key: "INSURANCE", label: "Seguros" }, { key: "TRUST", label: "Fideicomiso" }, ]; type Direction = "charge" | "credit"; function today(): string { return new Date().toISOString().slice(0, 10); } function s(v: string): string | undefined { const t = v.trim(); return t === "" ? undefined : t; } /** Capture form for one ledger movement. `defaultCustomer` pre-fills the picker * when opening from a customer's statement. `concepts` is the list of * transaction-type facets from the billing module. */ export function MovementForm({ concepts, defaultCurrency, defaultCustomer, defaultDomain, onSaved, onCancel, }: { concepts: Facet[]; defaultCurrency?: LedgerCurrency; defaultCustomer?: { id: string; name: string }; defaultDomain?: TransactionDomain; onSaved: () => void; onCancel: () => void; }) { const [customerId, setCustomerId] = useState(defaultCustomer?.id ?? ""); const [customerName, setCustomerName] = useState(defaultCustomer?.name ?? ""); const [domain, setDomain] = useState( defaultDomain ?? "UTILITY", ); const [direction, setDirection] = useState("charge"); const [amount, setAmount] = useState(""); const [transactionDate, setTransactionDate] = useState(today()); const [currency, setCurrency] = useState( defaultCurrency ?? "MXN", ); const [typeId, setTypeId] = useState(""); const [period, setPeriod] = useState(""); const [reference, setReference] = useState(""); const [checkNumber, setCheckNumber] = useState(""); const [message, setMessage] = useState(""); const [saving, setSaving] = useState(false); const [error, setError] = useState(null); async function submit(e: React.FormEvent) { e.preventDefault(); if (!customerId) { setError("Selecciona un cliente."); return; } const abs = Number(amount); if (!Number.isFinite(abs) || abs === 0) { setError("El monto debe ser un número distinto de cero."); return; } const signed = direction === "charge" ? -Math.abs(abs) : Math.abs(abs); const payload: CreateMovementInput = { customerId, domain, amount: signed, transactionDate, currency: currency as Currency, typeId: s(typeId), period: s(period), reference: s(reference), checkNumber: s(checkNumber), message: s(message), }; setSaving(true); setError(null); try { await createMovement(payload); onSaved(); } catch (e2) { setError((e2 as Error)?.message ?? "No se pudo guardar el movimiento."); setSaving(false); } } return (
{error &&
{error}
}

Cliente

{ setCustomerId(id); setCustomerName(name); }} />

Movimiento

setTransactionDate(e.target.value)} /> setAmount(e.target.value)} placeholder="0.00" />

Detalles

setPeriod(e.target.value)} placeholder="Ej. 2025-01" /> setReference(e.target.value)} /> setCheckNumber(e.target.value)} />