erp-retail-backend-v2/src/modules/mcp/tools/financial-tools.service.ts
Adrian Flores Cortes 9de89aab5a [PROP-CORE-004] feat: Add Phase 6 modules from erp-core
Propagated modules:
- payment-terminals: MercadoPago + Clip TPV integration
- ai: Role-based AI access (ADMIN, GERENTE_TIENDA, CAJERO, CLIENTE)
- mcp: 18 ERP tools for AI assistants

71 files added. Critical for POS operations (P0).

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-25 02:43:47 -06:00

292 lines
8.0 KiB
TypeScript

import {
McpToolProvider,
McpToolDefinition,
McpToolHandler,
McpContext,
} from '../interfaces';
/**
* Financial Tools Service
* Provides MCP tools for financial reporting and analysis.
* Used by: ADMIN role only
*/
export class FinancialToolsService implements McpToolProvider {
getTools(): McpToolDefinition[] {
return [
{
name: 'get_financial_report',
description: 'Genera reporte financiero (ingresos, gastos, utilidad)',
category: 'financial',
parameters: {
type: 'object',
properties: {
type: {
type: 'string',
enum: ['income', 'expenses', 'profit', 'summary'],
description: 'Tipo de reporte',
default: 'summary',
},
start_date: {
type: 'string',
format: 'date',
description: 'Fecha inicial',
},
end_date: {
type: 'string',
format: 'date',
description: 'Fecha final',
},
branch_id: {
type: 'string',
format: 'uuid',
description: 'Filtrar por sucursal',
},
},
required: ['type'],
},
returns: { type: 'object' },
},
{
name: 'get_accounts_receivable',
description: 'Obtiene cuentas por cobrar (clientes que deben)',
category: 'financial',
parameters: {
type: 'object',
properties: {
status: {
type: 'string',
enum: ['all', 'current', 'overdue'],
default: 'all',
},
min_amount: { type: 'number' },
limit: { type: 'number', default: 50 },
},
},
returns: {
type: 'object',
properties: {
total: { type: 'number' },
count: { type: 'number' },
accounts: { type: 'array' },
},
},
},
{
name: 'get_accounts_payable',
description: 'Obtiene cuentas por pagar (deudas a proveedores)',
category: 'financial',
parameters: {
type: 'object',
properties: {
status: {
type: 'string',
enum: ['all', 'current', 'overdue'],
default: 'all',
},
due_date_before: { type: 'string', format: 'date' },
limit: { type: 'number', default: 50 },
},
},
returns: {
type: 'object',
properties: {
total: { type: 'number' },
count: { type: 'number' },
accounts: { type: 'array' },
},
},
},
{
name: 'get_cash_flow',
description: 'Analiza flujo de caja (entradas y salidas)',
category: 'financial',
parameters: {
type: 'object',
properties: {
period: {
type: 'string',
enum: ['week', 'month', 'quarter'],
default: 'month',
},
branch_id: { type: 'string', format: 'uuid' },
},
},
returns: {
type: 'object',
properties: {
inflows: { type: 'number' },
outflows: { type: 'number' },
net_flow: { type: 'number' },
by_category: { type: 'array' },
},
},
},
{
name: 'get_kpis',
description: 'Obtiene indicadores clave de desempeno (KPIs)',
category: 'financial',
parameters: {
type: 'object',
properties: {
period: {
type: 'string',
enum: ['month', 'quarter', 'year'],
default: 'month',
},
},
},
returns: {
type: 'object',
properties: {
gross_margin: { type: 'number' },
net_margin: { type: 'number' },
inventory_turnover: { type: 'number' },
avg_collection_days: { type: 'number' },
},
},
},
];
}
getHandler(toolName: string): McpToolHandler | undefined {
const handlers: Record<string, McpToolHandler> = {
get_financial_report: this.getFinancialReport.bind(this),
get_accounts_receivable: this.getAccountsReceivable.bind(this),
get_accounts_payable: this.getAccountsPayable.bind(this),
get_cash_flow: this.getCashFlow.bind(this),
get_kpis: this.getKPIs.bind(this),
};
return handlers[toolName];
}
private async getFinancialReport(
params: { type: string; start_date?: string; end_date?: string; branch_id?: string },
context: McpContext
): Promise<any> {
// TODO: Connect to FinancialService
return {
type: params.type,
period: {
start: params.start_date || new Date().toISOString().split('T')[0],
end: params.end_date || new Date().toISOString().split('T')[0],
},
income: 125000.00,
expenses: 85000.00,
gross_profit: 40000.00,
net_profit: 32000.00,
breakdown: {
sales: 120000.00,
services: 5000.00,
cost_of_goods: 65000.00,
operating_expenses: 20000.00,
taxes: 8000.00,
},
message: 'Conectar a FinancialService real',
};
}
private async getAccountsReceivable(
params: { status?: string; min_amount?: number; limit?: number },
context: McpContext
): Promise<any> {
// TODO: Connect to AccountsService
return {
total: 45000.00,
count: 12,
overdue_total: 15000.00,
overdue_count: 4,
accounts: [
{
customer: 'Cliente A',
amount: 5000.00,
due_date: '2026-01-20',
days_overdue: 5,
status: 'overdue',
},
{
customer: 'Cliente B',
amount: 8000.00,
due_date: '2026-02-01',
days_overdue: 0,
status: 'current',
},
].slice(0, params.limit || 50),
message: 'Conectar a AccountsService real',
};
}
private async getAccountsPayable(
params: { status?: string; due_date_before?: string; limit?: number },
context: McpContext
): Promise<any> {
// TODO: Connect to AccountsService
return {
total: 32000.00,
count: 8,
overdue_total: 5000.00,
overdue_count: 2,
accounts: [
{
supplier: 'Proveedor X',
amount: 12000.00,
due_date: '2026-01-28',
status: 'current',
},
{
supplier: 'Proveedor Y',
amount: 5000.00,
due_date: '2026-01-15',
days_overdue: 10,
status: 'overdue',
},
].slice(0, params.limit || 50),
message: 'Conectar a AccountsService real',
};
}
private async getCashFlow(
params: { period?: string; branch_id?: string },
context: McpContext
): Promise<any> {
// TODO: Connect to FinancialService
return {
period: params.period || 'month',
inflows: 95000.00,
outflows: 72000.00,
net_flow: 23000.00,
opening_balance: 45000.00,
closing_balance: 68000.00,
by_category: [
{ category: 'Ventas', type: 'inflow', amount: 90000.00 },
{ category: 'Cobranzas', type: 'inflow', amount: 5000.00 },
{ category: 'Compras', type: 'outflow', amount: 55000.00 },
{ category: 'Nomina', type: 'outflow', amount: 12000.00 },
{ category: 'Gastos operativos', type: 'outflow', amount: 5000.00 },
],
message: 'Conectar a FinancialService real',
};
}
private async getKPIs(
params: { period?: string },
context: McpContext
): Promise<any> {
// TODO: Connect to AnalyticsService
return {
period: params.period || 'month',
gross_margin: 32.5,
net_margin: 18.2,
inventory_turnover: 4.5,
avg_collection_days: 28,
current_ratio: 1.8,
quick_ratio: 1.2,
return_on_assets: 12.5,
trends: {
gross_margin_change: 2.1,
net_margin_change: 1.5,
},
message: 'Conectar a AnalyticsService real',
};
}
}