/** Преобразование объектов QuickBooks в строки таблиц qbo_* (миграция 032). Полный объект всегда сохраняется в raw. */ type Obj = Record; export type Row = Record; const ref = (r: Obj | undefined): { id: string | null; name: string | null } => ({ id: r?.value != null ? String(r.value) : null, name: r?.name ?? null }); const money = (v: unknown): number | null => (v === undefined || v === null || v === "" ? null : Number(v)); const ts = (v: unknown): string | null => (typeof v === "string" && v ? v : null); const date = (v: unknown): string | null => (typeof v === "string" && /^\d{4}-\d{2}-\d{2}/.test(v) ? v.slice(0, 10) : null); export interface EntitySpec { /** Имя сущности в QuickBooks (запрос `select * from `). */ entity: string; table: string; /** Колонки основной таблицы без realm_id и synced_at. */ columns: string[]; map(o: Obj): Row; /** Строки дочерней таблицы (строки документа), если есть. */ lines?: { table: string; parentColumn: string; columns: string[]; map(o: Obj, parentId: string): Row[] }; } const lineRows = (o: Obj, parentId: string, parentColumn: string): Row[] => { const out: Row[] = []; (o.Line ?? []).forEach((l: Obj, i: number) => { const d = l.AccountBasedExpenseLineDetail ?? l.ItemBasedExpenseLineDetail; if (!d) return; // итоговые и прочие служебные строки в разбивку не входят const acc = ref(d.AccountRef), cust = ref(d.CustomerRef); out.push({ [parentColumn]: parentId, line_no: i, amount: money(l.Amount), description: l.Description ?? null, account_id: acc.id, account_name: acc.name, customer_id: cust.id, customer_name: cust.name, class_id: ref(d.ClassRef).id, billable_status: d.BillableStatus ?? null, }); }); return out; }; const LINE_COLUMNS = ["line_no", "amount", "description", "account_id", "account_name", "customer_id", "customer_name", "class_id", "billable_status"]; export const SPECS: EntitySpec[] = [ { entity: "Account", table: "qbo_account", columns: ["id", "acct_num", "name", "fully_qualified_name", "account_type", "account_sub_type", "classification", "parent_id", "active", "current_balance", "sync_token", "meta_updated", "raw"], map: (o) => ({ id: String(o.Id), acct_num: o.AcctNum ?? null, name: o.Name, fully_qualified_name: o.FullyQualifiedName ?? null, account_type: o.AccountType ?? null, account_sub_type: o.AccountSubType ?? null, classification: o.Classification ?? null, parent_id: ref(o.ParentRef).id, active: o.Active !== false, current_balance: money(o.CurrentBalance), sync_token: o.SyncToken ?? null, meta_updated: ts(o.MetaData?.LastUpdatedTime), raw: o, }), }, { entity: "Vendor", table: "qbo_vendor", columns: ["id", "display_name", "company_name", "email", "phone", "active", "balance", "sync_token", "meta_updated", "raw"], map: (o) => ({ id: String(o.Id), display_name: o.DisplayName, company_name: o.CompanyName ?? null, email: o.PrimaryEmailAddr?.Address ?? null, phone: o.PrimaryPhone?.FreeFormNumber ?? null, active: o.Active !== false, balance: money(o.Balance), sync_token: o.SyncToken ?? null, meta_updated: ts(o.MetaData?.LastUpdatedTime), raw: o, }), }, { entity: "Customer", table: "qbo_customer", columns: ["id", "display_name", "fully_qualified_name", "parent_id", "is_job", "active", "balance", "sync_token", "meta_updated", "raw"], map: (o) => ({ id: String(o.Id), display_name: o.DisplayName, fully_qualified_name: o.FullyQualifiedName ?? null, parent_id: ref(o.ParentRef).id, is_job: o.Job === true, active: o.Active !== false, balance: money(o.Balance), sync_token: o.SyncToken ?? null, meta_updated: ts(o.MetaData?.LastUpdatedTime), raw: o, }), }, { entity: "Purchase", table: "qbo_purchase", columns: ["id", "txn_date", "payment_type", "account_id", "account_name", "entity_id", "entity_name", "total_amt", "doc_number", "private_note", "sync_token", "meta_created", "meta_updated", "raw"], map: (o) => { const acc = ref(o.AccountRef), ent = ref(o.EntityRef); return { id: String(o.Id), txn_date: date(o.TxnDate), payment_type: o.PaymentType ?? null, account_id: acc.id, account_name: acc.name, entity_id: ent.id, entity_name: ent.name, total_amt: money(o.TotalAmt), doc_number: o.DocNumber ?? null, private_note: o.PrivateNote ?? null, sync_token: o.SyncToken ?? null, meta_created: ts(o.MetaData?.CreateTime), meta_updated: ts(o.MetaData?.LastUpdatedTime), raw: o, }; }, lines: { table: "qbo_purchase_line", parentColumn: "purchase_id", columns: LINE_COLUMNS, map: (o, id) => lineRows(o, id, "purchase_id") }, }, { entity: "Bill", table: "qbo_bill", columns: ["id", "txn_date", "due_date", "vendor_id", "vendor_name", "total_amt", "balance", "doc_number", "private_note", "sync_token", "meta_created", "meta_updated", "raw"], map: (o) => { const v = ref(o.VendorRef); return { id: String(o.Id), txn_date: date(o.TxnDate), due_date: date(o.DueDate), vendor_id: v.id, vendor_name: v.name, total_amt: money(o.TotalAmt), balance: money(o.Balance), doc_number: o.DocNumber ?? null, private_note: o.PrivateNote ?? null, sync_token: o.SyncToken ?? null, meta_created: ts(o.MetaData?.CreateTime), meta_updated: ts(o.MetaData?.LastUpdatedTime), raw: o, }; }, lines: { table: "qbo_bill_line", parentColumn: "bill_id", columns: LINE_COLUMNS, map: (o, id) => lineRows(o, id, "bill_id") }, }, { entity: "Attachable", table: "qbo_attachable", columns: ["id", "file_name", "content_type", "size", "note", "refs", "sync_token", "meta_created", "raw"], map: (o) => ({ id: String(o.Id), file_name: o.FileName ?? null, content_type: o.ContentType ?? null, size: o.Size != null ? Number(o.Size) : null, note: o.Note ?? null, refs: JSON.stringify((o.AttachableRef ?? []).map((r: Obj) => ({ type: r.EntityRef?.type ?? null, id: r.EntityRef?.value != null ? String(r.EntityRef.value) : null }))), sync_token: o.SyncToken ?? null, meta_created: ts(o.MetaData?.CreateTime), raw: o, }), }, ];