Open-World: включить маржинальность выбранной номенклатуры

This commit is contained in:
2026-05-04 08:40:27 +03:00
parent edab736a6d
commit 7294eca381
23 changed files with 472 additions and 56 deletions
@@ -292,6 +292,7 @@ function isInventoryLifecycleHistoryIntent(intent) {
return (intent === "inventory_purchase_provenance_for_item" ||
intent === "inventory_purchase_documents_for_item" ||
intent === "inventory_sale_trace_for_item" ||
intent === "inventory_profitability_for_item" ||
intent === "inventory_purchase_to_sale_chain");
}
function shouldSuppressInventoryCounterpartyAlias(intent, counterparty, organization) {
@@ -1008,6 +1009,7 @@ function mergeFollowupFilters(current, intent, userMessage, followupContext) {
intent === "inventory_purchase_documents_for_item" ||
intent === "inventory_supplier_stock_overlap_as_of_date" ||
intent === "inventory_sale_trace_for_item" ||
intent === "inventory_profitability_for_item" ||
intent === "inventory_purchase_to_sale_chain" ||
intent === "inventory_aging_by_purchase_date" ||
intent === "payables_confirmed_as_of_date" ||
@@ -1061,6 +1063,19 @@ function mergeFollowupFilters(current, intent, userMessage, followupContext) {
reasons.push("period_from_followup_context");
}
}
if (intent === "inventory_profitability_for_item" &&
previousHasPeriod &&
hasSelectedObjectInventorySignal(userMessage) &&
!hasExplicitPeriodInMessage &&
!hasExplicitCurrentDateInMessage) {
if (previousPeriodFrom && merged.period_from !== previousPeriodFrom) {
merged.period_from = previousPeriodFrom;
}
if (previousPeriodTo && merged.period_to !== previousPeriodTo) {
merged.period_to = previousPeriodTo;
}
reasons.push("period_from_followup_context");
}
if (!currentHasPeriod &&
previousHasPeriod &&
hasFollowupSignal &&
@@ -56,6 +56,30 @@ function inventoryRequestedPartyMatches(requested, actualParties) {
function inventoryPartyListOrUnknown(parties) {
return parties.length > 0 ? parties.slice(0, 4).join("; ") : "не выделен отдельным полем";
}
function sumInventoryRowAmount(rows) {
return rows.reduce((sum, row) => sum + (typeof row.amount === "number" && Number.isFinite(row.amount) ? row.amount : 0), 0);
}
function sumInventoryRowQuantity(rows) {
return rows.reduce((sum, row) => sum + (typeof row.quantity === "number" && Number.isFinite(row.quantity) ? row.quantity : 0), 0);
}
function formatInventoryPercent(value, formatNumberWithDots) {
return value === null || !Number.isFinite(value) ? "не подтверждена" : `${formatNumberWithDots(value, 2)}%`;
}
function inventoryProfitabilityPeriodLabel(options, deps) {
const from = typeof options.periodFrom === "string" && options.periodFrom.trim().length > 0 ? options.periodFrom : null;
const to = typeof options.periodTo === "string" && options.periodTo.trim().length > 0 ? options.periodTo : null;
if (from && to) {
return `${deps.formatDateRu(from)} - ${deps.formatDateRu(to)}`;
}
if (from) {
return `с ${deps.formatDateRu(from)}`;
}
if (to) {
return `до ${deps.formatDateRu(to)}`;
}
const asOfDate = typeof options.asOfDate === "string" && options.asOfDate.trim().length > 0 ? options.asOfDate : null;
return asOfDate ? `до ${deps.formatDateRu(asOfDate)}` : "по доступной выборке";
}
function composeInventoryReply(intent, rows, options, deps) {
if (intent === "inventory_on_hand_as_of_date") {
const asOfDate = deps.resolvePayablesAsOfDate(options);
@@ -353,6 +377,71 @@ function composeInventoryReply(intent, rows, options, deps) {
? (0, replyContracts_1.buildFactualListReply)(lines, (0, replyContracts_1.buildConfirmedBalanceSemantics)(summary.counterparties.length > 0 ? "strong" : "medium", true))
: (0, replyContracts_1.buildFactualSummaryReply)(lines, (0, replyContracts_1.buildConfirmedBalanceSemantics)("medium", false));
}
if (intent === "inventory_profitability_for_item") {
const purchaseRows = rows.filter((row) => deps.isInventoryPurchaseMovement(row));
const saleRows = rows.filter((row) => deps.isInventorySaleMovement(row));
const requestedItemHint = String(options.itemHint ?? "").trim();
const excludedCounterpartyTokens = requestedItemHint ? [requestedItemHint] : [];
const purchaseSummary = deps.summarizeInventoryTraceRows(purchaseRows, excludedCounterpartyTokens);
const saleSummary = deps.summarizeInventoryTraceRows(saleRows, excludedCounterpartyTokens);
const itemLabel = requestedItemHint || purchaseSummary.item || saleSummary.item || "товар не определен";
const revenue = sumInventoryRowAmount(saleRows);
const purchaseCostProxy = sumInventoryRowAmount(purchaseRows);
const spread = revenue - purchaseCostProxy;
const marginPct = revenue > 0 ? (spread / revenue) * 100 : null;
const markupPct = purchaseCostProxy > 0 ? (spread / purchaseCostProxy) * 100 : null;
const saleQuantity = sumInventoryRowQuantity(saleRows);
const purchaseQuantity = sumInventoryRowQuantity(purchaseRows);
const periodLabel = inventoryProfitabilityPeriodLabel(options, deps);
const hasSales = saleRows.length > 0;
const hasPurchases = purchaseRows.length > 0;
const directAnswerLine = hasSales && hasPurchases
? `По товару ${itemLabel} за период ${periodLabel} подтверждена выручка продаж ${deps.formatMoneyRub(revenue)} и закупочный след ${deps.formatMoneyRub(purchaseCostProxy)}; расчетный валовый спред по доступным документам: ${deps.formatMoneyRub(spread)}. Маржинальность к выручке: ${formatInventoryPercent(marginPct, deps.formatNumberWithDots)}.`
: hasSales
? `По товару ${itemLabel} за период ${periodLabel} подтверждена выручка продаж ${deps.formatMoneyRub(revenue)}, но закупочный след в доступных строках не найден; прибыль и маржа не подтверждены.`
: hasPurchases
? `По товару ${itemLabel} за период ${periodLabel} найден закупочный след ${deps.formatMoneyRub(purchaseCostProxy)}, но продажи в доступных строках не найдены; выручка, прибыль и маржа не подтверждены.`
: `По товару ${itemLabel} за период ${periodLabel} не найдено ни продаж, ни закупочного следа в доступных строках 41.01.`;
const lines = [directAnswerLine];
(0, inventoryReplyPresentation_1.appendInventoryBulletSection)(lines, "Расчет:", [
`Строк продаж со счета 41.01: ${deps.formatNumberWithDots(saleRows.length)}.`,
`Строк закупки на счет 41.01: ${deps.formatNumberWithDots(purchaseRows.length)}.`,
`Выручка по документам продажи: ${deps.formatMoneyRub(revenue)}.`,
`Закупочная сумма по доступным документам: ${deps.formatMoneyRub(purchaseCostProxy)}.`,
`Расчетный валовый спред: ${deps.formatMoneyRub(spread)}.`,
`Маржинальность к выручке: ${formatInventoryPercent(marginPct, deps.formatNumberWithDots)}.`,
`Наценка к закупочному следу: ${formatInventoryPercent(markupPct, deps.formatNumberWithDots)}.`
]);
if (saleQuantity > 0 || purchaseQuantity > 0) {
lines.push(`- Количество в продажах: ${deps.formatNumberWithDots(saleQuantity, 3)}; количество в закупках: ${deps.formatNumberWithDots(purchaseQuantity, 3)}.`);
}
if (saleSummary.counterparties.length > 0) {
lines.push(`- Покупатели в продаже: ${saleSummary.counterparties.slice(0, 4).join("; ")}.`);
}
if (purchaseSummary.counterparties.length > 0) {
lines.push(`- Поставщики в закупочном следе: ${purchaseSummary.counterparties.slice(0, 4).join("; ")}.`);
}
(0, inventoryReplyPresentation_1.appendInventoryBulletSection)(lines, "Ограничения:", [
"Это не чистая прибыль компании и не бухгалтерский финансовый результат.",
"Закупочная сумма является proxy по найденным документам поступления; без партионного/управленческого учета нельзя доказать точную себестоимость конкретной продажи.",
"Если продажи и закупки попали в разные периоды или разные организации/склады, вывод нужно читать как документальный срез по доступной выборке, а не как полный P&L."
]);
if (saleRows.length > 0) {
(0, inventoryReplyPresentation_1.appendInventorySection)(lines, "Продажи:", [
`- Первая дата продажи: ${deps.inventoryTraceDateLabel(saleSummary.firstPeriod)}.`,
`- Последняя дата продажи: ${deps.inventoryTraceDateLabel(saleSummary.lastPeriod)}.`,
...deps.formatInventoryTraceRows(saleRows, 8, [itemLabel])
]);
}
if (purchaseRows.length > 0) {
(0, inventoryReplyPresentation_1.appendInventorySection)(lines, "Закупки:", [
`- Первая дата закупки: ${deps.inventoryTraceDateLabel(purchaseSummary.firstPeriod)}.`,
`- Последняя дата закупки: ${deps.inventoryTraceDateLabel(purchaseSummary.lastPeriod)}.`,
...deps.formatInventoryTraceRows(purchaseRows, 8, [itemLabel])
]);
}
return (0, replyContracts_1.buildFactualSummaryReply)(lines, (0, replyContracts_1.buildConfirmedBalanceSemantics)(hasSales && hasPurchases ? "strong" : hasSales || hasPurchases ? "medium" : "weak", hasSales || hasPurchases));
}
if (intent === "inventory_purchase_to_sale_chain") {
const purchaseRows = rows.filter((row) => deps.isInventoryPurchaseMovement(row));
const saleRows = rows.filter((row) => deps.isInventorySaleMovement(row));