import { useCallback, useEffect, useRef, useState } from 'react'
import { Card, Badge, Button, Field } from '../ui'
import KundenansichtButton from '../customer/KundenansichtButton'
import { applyCredit, calcCredit, decideCredit, getCredit } from '../../lib/teamApi'
import { applyMessage, BILLING_MONTHS, calcLine, checkGlyph, checkTone, creditStatus, decisionConfirm, decisionError, decisionMessage, fmtBytes, historyLabel } from '../../lib/creditFormat'
import { fmtDate, fmtDateTime, formatCents, productLabelOf } from '../../lib/format'
import { storageLabel } from '../../lib/storageFormat'
const staffOf = (by) => (by && by !== 'customer' && by !== 'system' ? ` · ${by}` : '')
const toCents = (value) => Math.round(Number(value) * 100)
/**
* Antrag auf Uebernahmegutschrift: Pruefung und Entscheidung wie im frueheren Admin-Panel.
* Waehrend einer Entscheidung sind alle Knoepfe gesperrt (busyRef verhindert auch Doppelklicks vor dem Neuzeichnen).
*/
export default function CreditDetail({ id, onClose, onChanged, onOpen }) {
const [d, setD] = useState(null)
const [error, setError] = useState(null)
const [message, setMessage] = useState('')
const [form, setForm] = useState(null)
const [calcText, setCalcText] = useState('')
const [actionError, setActionError] = useState('')
const [busy, setBusy] = useState(false)
const [busyAction, setBusyAction] = useState('')
const busyRef = useRef(false)
const amountTouched = useRef(false)
const calcTimer = useRef(null)
const load = useCallback(async (msg = '') => {
setError(null)
try {
const detail = await getCredit(id)
const r = detail.request
setD(detail)
setMessage(msg)
setActionError('')
amountTouched.current = false
setForm({
checks: Object.fromEntries(detail.requiredChecks.map((k) => [k.key, false])),
invoiceAmount: (r.invoiceAmountCents / 100).toFixed(2),
billingMonths: r.billingMonths,
paidUntil: r.paidUntilDate || '',
takeoverFrom: r.takeoverFromDate || '',
amount: ((detail.calc?.suggestedCents || 0) / 100).toFixed(2),
note: '',
})
setCalcText(detail.calc ? calcLine(detail.calc) : 'Angaben unvollständig')
} catch (err) {
setError(err.message || 'Antrag konnte nicht geladen werden.')
}
}, [id])
useEffect(() => {
setD(null)
load()
return () => clearTimeout(calcTimer.current)
}, [load])
const figures = (f) => ({ invoiceAmountCents: toCents(f.invoiceAmount), billingMonths: Number(f.billingMonths), paidUntil: f.paidUntil, takeoverFrom: f.takeoverFrom })
const setFigure = (key, value) => {
const next = { ...form, [key]: value }
setForm(next)
clearTimeout(calcTimer.current)
calcTimer.current = setTimeout(async () => {
try {
const { calc } = await calcCredit(figures(next))
setCalcText(calcLine(calc))
if (!amountTouched.current) setForm((f) => ({ ...f, amount: (calc.suggestedCents / 100).toFixed(2) }))
} catch (err) {
setCalcText(err.message)
}
}, 300)
}
const decide = async (action) => {
if (busyRef.current) return
setActionError('')
const problem = decisionError(action, form.note)
if (problem) { setActionError(problem); return }
const amountCents = toCents(form.amount)
if (!window.confirm(decisionConfirm(action, amountCents, d.customer?.name))) return
if (busyRef.current) return
busyRef.current = true
setBusy(true)
setBusyAction(action)
try {
const result = await decideCredit(id, { action, note: form.note.trim(), amountCents, checks: form.checks, ...figures(form) })
onChanged?.()
await load(decisionMessage(action, result))
} catch (err) {
setActionError(err.message)
} finally {
busyRef.current = false
setBusy(false)
setBusyAction('')
}
}
const apply = async () => {
if (busyRef.current) return
busyRef.current = true
setBusy(true)
setBusyAction('apply')
try {
const result = await applyCredit(id)
onChanged?.()
await load(applyMessage(result))
} catch (err) {
setActionError(err.message)
} finally {
busyRef.current = false
setBusy(false)
setBusyAction('')
}
}
if (error) return {error}
{actionError}
: null return (eingegangen {fmtDateTime(r.createdAt)}
{message}
}{r.message}
Keine Nachweise (mehr) vorhanden.
}{h.text}
} {(h.files || []).length > 0 &&{calcText}
Werte weichen von der Rechnung ab? Hier korrigieren – die Korrektur wird beim Genehmigen im Verlauf vermerkt. „Übernahme ab“ ist der Leistungsbeginn des Abos.
Genehmigen geht, sobald alle Punkte abgehakt sind. Der Kunde bekommt jede Entscheidung per E-Mail. {!s?.active && Achtung: Der Kunde hat kein laufendes Abo.}
{errorLine}