@fullqueso/mcp-bc-gastos 1.10.0 → 1.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -25,6 +25,16 @@ export const COGS_ACCOUNTS = {
25
25
  57020: { name: 'Costos Diversos', nameEn: 'Diverse Costs' },
26
26
  };
27
27
 
28
+ // Income group labels for breakdown (by 100-prefix: 40110 → 40100)
29
+ export const INCOME_GROUP_LABELS = {
30
+ 40100: '40100_ingreso_ventas',
31
+ 40300: '40300_facturacion_directa',
32
+ 40400: '40400_ingreso_servicios',
33
+ 40500: '40500_otros_ingresos',
34
+ 40600: '40600_ingreso_alquiler',
35
+ 40900: '40900_extraordinarios',
36
+ };
37
+
28
38
  // Ranges
29
39
  export const REVENUE_RANGE = { min: 40000, max: 49999 };
30
40
  export const COGS_RANGE = { min: 50000, max: 59999 };
package/lib/bc-client.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { logger } from '../utils/logger.js';
2
2
  import { getExpenseCategory } from '../config/expense-accounts.js';
3
+ import { INCOME_GROUP_LABELS } from '../config/income-accounts.js';
3
4
  import { resolveStores } from '../config/company-config.js';
4
5
 
5
6
  export class BCClient {
@@ -308,6 +309,26 @@ export class BCClient {
308
309
  0
309
310
  );
310
311
 
312
+ // Income breakdown by account group (401xx, 405xx, etc.)
313
+ const groupTotals = {};
314
+ for (const e of revenueEntries) {
315
+ const prefix = Math.floor(parseInt(e.accountNumber, 10) / 100) * 100;
316
+ groupTotals[prefix] = (groupTotals[prefix] || 0) + ((e.creditAmount || 0) - (e.debitAmount || 0));
317
+ }
318
+ const incomeBreakdown = {};
319
+ let breakdownSum = 0;
320
+ for (const [prefix, amount] of Object.entries(groupTotals)) {
321
+ const label = INCOME_GROUP_LABELS[prefix] || `${prefix}_other`;
322
+ incomeBreakdown[label] = Math.round(amount * 100) / 100;
323
+ breakdownSum += amount;
324
+ }
325
+ // Catch rounding drift vs totalRevenue
326
+ const drift = Math.round((totalRevenue - breakdownSum) * 100) / 100;
327
+ if (Math.abs(drift) > 0.005) {
328
+ incomeBreakdown.unclassified = drift;
329
+ }
330
+ incomeBreakdown.total = Math.round(totalRevenue * 100) / 100;
331
+
311
332
  // COGS (50000-59999): debit balance accounts, cost = debit - credit
312
333
  const totalCOGS = cogsEntries.reduce(
313
334
  (sum, e) => sum + ((e.debitAmount || 0) - (e.creditAmount || 0)),
@@ -341,6 +362,7 @@ export class BCClient {
341
362
  period: { start: startDate, end: endDate },
342
363
  expenses: categorizedExpenses,
343
364
  totalRevenue,
365
+ incomeBreakdown,
344
366
  totalCOGS,
345
367
  grossMargin,
346
368
  grossMarginPct,
@@ -85,6 +85,7 @@ export function analyzeExpenses(storeData) {
85
85
  store_code: storeData.storeCode,
86
86
  period: storeData.period,
87
87
  total_revenue: round2(totalRevenue || totalIncome),
88
+ income_breakdown: storeData.incomeBreakdown || null,
88
89
  total_cogs: round2(totalCOGS || 0),
89
90
  gross_margin: round2(grossMargin || totalIncome),
90
91
  gross_margin_pct: round2(grossMarginPct || 0),
@@ -90,6 +90,7 @@ export function calculateRatios(storeData) {
90
90
  store_code: storeData.storeCode,
91
91
  period: storeData.period,
92
92
  total_income: round2(totalIncome),
93
+ income_breakdown: storeData.incomeBreakdown || null,
93
94
  total_expenses: round2(totalExpenses),
94
95
  main_ratios: ratios,
95
96
  key_ratios: keyRatios,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fullqueso/mcp-bc-gastos",
3
- "version": "1.10.0",
3
+ "version": "1.11.1",
4
4
  "description": "MCP server for Business Central operational expense analysis, bank reconciliation, POS reconciliation, accounts receivable/payable, and multi-payment draft visibility - Full Queso franchise stores",
5
5
  "main": "server.js",
6
6
  "bin": {
package/server.js CHANGED
@@ -32,6 +32,7 @@ import { expenseDetailsTool, handleExpenseDetails } from './tools/expense-detail
32
32
  import { accountTransactionsTool, handleAccountTransactions } from './tools/account-transactions.js';
33
33
  import { vendorTransactionsTool, handleVendorTransactions } from './tools/vendor-transactions.js';
34
34
  import { listVendorsTool, handleListVendors } from './tools/list-vendors.js';
35
+ import { exchangeRateTool, handleGetExchangeRate } from './tools/get-exchange-rate.js';
35
36
 
36
37
  // Auditoria tools (bank reconciliation)
37
38
  import { listBankAccountsTool, handleListBankAccounts } from './tools/auditoria/list-bank-accounts.js';
@@ -86,6 +87,7 @@ server.setRequestHandler(ListToolsRequestSchema, async () => {
86
87
  accountTransactionsTool,
87
88
  vendorTransactionsTool,
88
89
  listVendorsTool,
90
+ exchangeRateTool,
89
91
  // Auditoria (bank reconciliation)
90
92
  listBankAccountsTool,
91
93
  reconciliationStatusTool,
@@ -145,6 +147,9 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
145
147
  case 'list_vendors':
146
148
  result = await handleListVendors(bcClient, args);
147
149
  break;
150
+ case 'get_exchange_rate':
151
+ result = await handleGetExchangeRate(bcClient, args);
152
+ break;
148
153
  // Auditoria (bank reconciliation)
149
154
  case 'list_bank_accounts':
150
155
  result = await handleListBankAccounts(bcClient, args);
@@ -0,0 +1,112 @@
1
+ import { resolveStores } from '../config/company-config.js';
2
+ import { round2 } from '../utils/currency-converter.js';
3
+
4
+ export const exchangeRateTool = {
5
+ name: 'get_exchange_rate',
6
+ description:
7
+ 'Obtiene la tasa de cambio USD → VES desde Business Central. Modo fecha: retorna la tasa vigente para una fecha específica. Modo rango: retorna el historial de tasas en un período.',
8
+ inputSchema: {
9
+ type: 'object',
10
+ properties: {
11
+ store: {
12
+ type: 'string',
13
+ enum: ['FQ01', 'FQ28', 'FQ88'],
14
+ description: 'Tienda a consultar.',
15
+ },
16
+ date: {
17
+ type: 'string',
18
+ description:
19
+ 'Fecha específica (YYYY-MM-DD). Retorna la tasa vigente (más reciente en o antes de esa fecha). Si no se indica fecha ni rango, usa hoy.',
20
+ },
21
+ start_date: {
22
+ type: 'string',
23
+ description:
24
+ 'Inicio del rango (YYYY-MM-DD). Usar junto con end_date para obtener historial de tasas.',
25
+ },
26
+ end_date: {
27
+ type: 'string',
28
+ description:
29
+ 'Fin del rango (YYYY-MM-DD). Usar junto con start_date para obtener historial de tasas.',
30
+ },
31
+ },
32
+ required: ['store'],
33
+ },
34
+ };
35
+
36
+ export async function handleGetExchangeRate(bcClient, args) {
37
+ const store = args.store;
38
+ if (!store) throw new Error('Parámetro requerido: store');
39
+
40
+ const stores = resolveStores([store]);
41
+ const storeInfo = stores[0];
42
+
43
+ const rates = await bcClient.getExchangeRates(store);
44
+
45
+ if (!rates || rates.length === 0) {
46
+ return {
47
+ store,
48
+ store_name: storeInfo.name,
49
+ error: 'No se encontraron tasas de cambio VES en Business Central.',
50
+ };
51
+ }
52
+
53
+ // Range mode
54
+ if (args.start_date && args.end_date) {
55
+ const filtered = rates.filter(
56
+ (r) => r.startingDate >= args.start_date && r.startingDate <= args.end_date
57
+ );
58
+
59
+ const rateList = filtered.map((r) => {
60
+ const rate = round2(r.exchangeRateAmount / r.relationalExchangeRateAmount);
61
+ return {
62
+ starting_date: r.startingDate,
63
+ rate,
64
+ label: `1 USD = ${rate} VES`,
65
+ };
66
+ });
67
+
68
+ // Also get the rate applicable at start_date (may be before the range)
69
+ const rateAtStart = bcClient.getExchangeRateForDate(rates, args.start_date);
70
+
71
+ return {
72
+ store,
73
+ store_name: storeInfo.name,
74
+ start_date: args.start_date,
75
+ end_date: args.end_date,
76
+ rate_at_start: rateAtStart
77
+ ? {
78
+ rate: round2(rateAtStart.rate),
79
+ starting_date: rateAtStart.startingDate,
80
+ label: `1 USD = ${round2(rateAtStart.rate)} VES`,
81
+ }
82
+ : null,
83
+ rates_in_range: rateList,
84
+ count: rateList.length,
85
+ };
86
+ }
87
+
88
+ // Single date mode (default: today)
89
+ const targetDate = args.date || new Date().toISOString().slice(0, 10);
90
+ const found = bcClient.getExchangeRateForDate(rates, targetDate);
91
+
92
+ if (!found) {
93
+ return {
94
+ store,
95
+ store_name: storeInfo.name,
96
+ date: targetDate,
97
+ exchange_rate: null,
98
+ message: 'No se encontró tasa de cambio para la fecha indicada.',
99
+ };
100
+ }
101
+
102
+ return {
103
+ store,
104
+ store_name: storeInfo.name,
105
+ date: targetDate,
106
+ exchange_rate: {
107
+ rate: round2(found.rate),
108
+ starting_date: found.startingDate,
109
+ label: `1 USD = ${round2(found.rate)} VES`,
110
+ },
111
+ };
112
+ }