feat: Projects + stronger LLM placeholder substitution
Backend:
- Project model with defaults: defaultClient, defaultTemplate, defaultBankAccount
- Document gets optional projectId (FK + index)
- Migration 2_projects: enum ProjectStatus + Project table + Document.projectId
- API: /api/projects CRUD, GET /api/projects/:id/documents
- documents/routes filter by projectId
LLM prompt:
- Concrete preamble example: «ООО «...», в лице директора ... , ИП ..., ОГРНИП ...»
→ {{customer.name}}, {{customer.signatoryPosition}} {{customer.signatoryName}},
{{executor.name}}, {{executor.ogrn}}
- Expanded placeholder list (signatoryName/Position для customer, ogrn etc)
- Side-role detection: «Заказчик» vs «Исполнитель» map to customer/executor
Frontend:
- /projects (list) and /projects/:id (defaults form + documents list under project)
- Nav: «Проекты» first
- DocumentEdit reads projectId from URL, presets default client from project,
saves projectId with the document
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,223 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
import { Link, useNavigate, useParams } from 'react-router-dom';
|
||||
import {
|
||||
api,
|
||||
ApiError,
|
||||
type BankAccount,
|
||||
type Client,
|
||||
type DocumentSummary,
|
||||
type DocumentTemplate,
|
||||
type Project,
|
||||
type ProjectStatus,
|
||||
} from '../api.js';
|
||||
import { Button, EmptyState, Field, Select, Textarea, formatRub } from '../components/ui.js';
|
||||
|
||||
const STATUS_LABEL: Record<ProjectStatus, string> = {
|
||||
active: 'Активный',
|
||||
completed: 'Завершён',
|
||||
cancelled: 'Отменён',
|
||||
};
|
||||
|
||||
const DOC_TYPE_LABEL: Record<string, string> = {
|
||||
contract: 'Договор',
|
||||
invoice: 'Счёт',
|
||||
act: 'Акт',
|
||||
upd: 'УПД',
|
||||
};
|
||||
|
||||
export function ProjectEditPage() {
|
||||
const { id } = useParams<{ id: string }>();
|
||||
const navigate = useNavigate();
|
||||
const [project, setProject] = useState<Project | null>(null);
|
||||
const [draft, setDraft] = useState<Partial<Project>>({});
|
||||
const [docs, setDocs] = useState<DocumentSummary[]>([]);
|
||||
const [clients, setClients] = useState<Client[]>([]);
|
||||
const [templates, setTemplates] = useState<DocumentTemplate[]>([]);
|
||||
const [bankAccounts, setBankAccounts] = useState<BankAccount[]>([]);
|
||||
const [saving, setSaving] = useState(false);
|
||||
const [savedAt, setSavedAt] = useState<Date | null>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [orgId, setOrgId] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (!id) return;
|
||||
void Promise.all([
|
||||
api.get<Project>(`/api/projects/${id}`),
|
||||
api.get<{ items: DocumentSummary[] }>(`/api/projects/${id}/documents`),
|
||||
api.get<{ items: Client[] }>('/api/clients?limit=500'),
|
||||
api.get<{ items: DocumentTemplate[] }>('/api/templates'),
|
||||
api.get<{ id: string }>('/api/active-organization'),
|
||||
])
|
||||
.then(async ([p, ds, cs, ts, ao]) => {
|
||||
setProject(p);
|
||||
setDraft(p);
|
||||
setDocs(ds.items);
|
||||
setClients(cs.items);
|
||||
setTemplates(ts.items);
|
||||
setOrgId(ao.id);
|
||||
const ba = await api.get<{ items: BankAccount[] }>(`/api/organizations/${ao.id}/bank-accounts`);
|
||||
setBankAccounts(ba.items);
|
||||
})
|
||||
.catch((e) => setError(String(e)));
|
||||
}, [id]);
|
||||
|
||||
async function save() {
|
||||
if (!id || !draft) return;
|
||||
setSaving(true);
|
||||
setError(null);
|
||||
try {
|
||||
const updated = await api.put<Project>(`/api/projects/${id}`, {
|
||||
name: draft.name ?? project?.name ?? '',
|
||||
status: draft.status ?? project?.status ?? 'active',
|
||||
defaultClientId: draft.defaultClientId ?? null,
|
||||
defaultTemplateId: draft.defaultTemplateId ?? null,
|
||||
defaultBankAccountId: draft.defaultBankAccountId ?? null,
|
||||
notes: draft.notes ?? null,
|
||||
});
|
||||
setProject(updated);
|
||||
setDraft(updated);
|
||||
setSavedAt(new Date());
|
||||
} catch (e) {
|
||||
setError(e instanceof ApiError ? e.prettyMessage() : String(e));
|
||||
} finally {
|
||||
setSaving(false);
|
||||
}
|
||||
}
|
||||
|
||||
function createDoc(docType: 'contract' | 'invoice' | 'act' | 'upd') {
|
||||
if (!project) return;
|
||||
const params = new URLSearchParams({ docType, projectId: project.id });
|
||||
if (project.defaultTemplateId) params.set('fromTemplate', project.defaultTemplateId);
|
||||
navigate(`/documents/new?${params.toString()}`);
|
||||
}
|
||||
|
||||
if (error && !project) return <main className="content"><div className="error-text">{error}</div></main>;
|
||||
if (!project) return <main className="content"><p className="hint">Загрузка…</p></main>;
|
||||
|
||||
return (
|
||||
<main className="content">
|
||||
<header className="page-head">
|
||||
<h2>{project.name}</h2>
|
||||
<Button onClick={() => navigate('/projects')}>← К проектам</Button>
|
||||
</header>
|
||||
|
||||
<section className="form-grid">
|
||||
<Field
|
||||
label="Название"
|
||||
value={draft.name ?? ''}
|
||||
onChange={(e) => setDraft((d) => ({ ...d, name: e.target.value }))}
|
||||
/>
|
||||
<Select
|
||||
label="Статус"
|
||||
value={(draft.status ?? 'active') as ProjectStatus}
|
||||
onChange={(v) => setDraft((d) => ({ ...d, status: v as ProjectStatus }))}
|
||||
options={[
|
||||
{ value: 'active', label: STATUS_LABEL.active },
|
||||
{ value: 'completed', label: STATUS_LABEL.completed },
|
||||
{ value: 'cancelled', label: STATUS_LABEL.cancelled },
|
||||
]}
|
||||
/>
|
||||
<label className="field">
|
||||
<span className="field__label">Клиент по умолчанию</span>
|
||||
<select
|
||||
className="field__input"
|
||||
value={draft.defaultClientId ?? ''}
|
||||
onChange={(e) => setDraft((d) => ({ ...d, defaultClientId: e.target.value || null }))}
|
||||
>
|
||||
<option value="">— не выбран —</option>
|
||||
{clients.map((c) => (
|
||||
<option key={c.id} value={c.id}>
|
||||
{c.name}{c.inn ? ` · ИНН ${c.inn}` : ''}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
<label className="field">
|
||||
<span className="field__label">Шаблон по умолчанию</span>
|
||||
<select
|
||||
className="field__input"
|
||||
value={draft.defaultTemplateId ?? ''}
|
||||
onChange={(e) => setDraft((d) => ({ ...d, defaultTemplateId: e.target.value || null }))}
|
||||
>
|
||||
<option value="">— не выбран —</option>
|
||||
{templates.map((t) => (
|
||||
<option key={t.id} value={t.id}>
|
||||
{DOC_TYPE_LABEL[t.docType]}: {t.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
<label className="field">
|
||||
<span className="field__label">Банк-счёт для оплаты</span>
|
||||
<select
|
||||
className="field__input"
|
||||
value={draft.defaultBankAccountId ?? ''}
|
||||
onChange={(e) => setDraft((d) => ({ ...d, defaultBankAccountId: e.target.value || null }))}
|
||||
>
|
||||
<option value="">— не выбран —</option>
|
||||
{bankAccounts.map((b) => (
|
||||
<option key={b.id} value={b.id}>
|
||||
{b.name}{b.bankName ? ` · ${b.bankName}` : ''}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
<Textarea
|
||||
label="Заметки"
|
||||
value={draft.notes ?? ''}
|
||||
onChange={(e) => setDraft((d) => ({ ...d, notes: e.target.value }))}
|
||||
rows={3}
|
||||
/>
|
||||
</section>
|
||||
|
||||
<div className="form-actions">
|
||||
<Button variant="primary" onClick={save} disabled={saving}>
|
||||
{saving ? 'Сохраняю…' : 'Сохранить'}
|
||||
</Button>
|
||||
{savedAt ? <span className="hint">Сохранено в {savedAt.toLocaleTimeString('ru-RU')}</span> : null}
|
||||
{error ? <span className="error-text">{error}</span> : null}
|
||||
</div>
|
||||
|
||||
<hr style={{ margin: '24px 0', border: 0, borderTop: '1px solid #e5e7eb' }} />
|
||||
|
||||
<header className="page-head">
|
||||
<h3>Документы проекта ({docs.length})</h3>
|
||||
<div style={{ display: 'flex', gap: 8 }}>
|
||||
<Button onClick={() => createDoc('contract')}>+ Договор</Button>
|
||||
<Button onClick={() => createDoc('invoice')}>+ Счёт</Button>
|
||||
<Button onClick={() => createDoc('act')}>+ Акт</Button>
|
||||
<Button onClick={() => createDoc('upd')}>+ УПД</Button>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
{docs.length === 0 ? (
|
||||
<EmptyState>В проекте пока нет документов. Жми кнопки выше.</EmptyState>
|
||||
) : (
|
||||
<table className="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>№</th>
|
||||
<th>Тип</th>
|
||||
<th>Дата</th>
|
||||
<th>Клиент</th>
|
||||
<th>Сумма</th>
|
||||
<th>Статус</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{docs.map((d) => (
|
||||
<tr key={d.id}>
|
||||
<td><Link to={`/documents/${d.id}`}>{d.number}</Link></td>
|
||||
<td>{DOC_TYPE_LABEL[d.docType] ?? d.docType}</td>
|
||||
<td>{d.issuedAt ? new Date(d.issuedAt).toLocaleDateString('ru-RU') : '—'}</td>
|
||||
<td>{d.client?.name ?? '—'}</td>
|
||||
<td className="num">{formatRub(d.totalCents)}</td>
|
||||
<td>{d.status}</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
)}
|
||||
</main>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user