Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions app/src/main/groovy/se/alipsa/accounting/domain/LegalForm.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ import se.alipsa.accounting.support.I18n

enum LegalForm {

AKTIEBOLAG,
ENSKILD_FIRMA,
HANDELSBOLAG_KB
AKTIEBOLAG('2099'),
ENSKILD_FIRMA('2019'),
HANDELSBOLAG_KB('2019')

final String defaultResultAccount

private LegalForm(String defaultResultAccount) {
this.defaultResultAccount = defaultResultAccount
}

String getDisplayName() {
I18n.instance.getString("legalForm.${name()}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ class AccountingMcpTools {
private Map<String, Object> previewYearEnd(Map<String, Object> args) {
long companyId = requiredLong(args, 'company_id')
long fiscalYearId = requiredLong(args, 'fiscal_year_id')
String closingAccount = optionalString(args, 'closing_account', ClosingService.DEFAULT_CLOSING_ACCOUNT)
String closingAccount = optionalString(args, 'closing_account', null)
try {
long expectedCompanyId = companyService.resolveFromFiscalYear(fiscalYearId)
if (expectedCompanyId != companyId) {
Expand All @@ -812,10 +812,12 @@ class AccountingMcpTools {
next_fiscal_year_name: preview.nextFiscalYear?.name,
next_fiscal_year_will_be_created: preview.nextFiscalYearWillBeCreated,
closing_account: preview.closingAccountNumber,
result_account_count: preview.resultAccountCount,
result_accounts_with_balance_count: preview.resultAccountsWithBalanceCount,
income_total: preview.incomeTotal,
expense_total: preview.expenseTotal,
net_result: preview.netResult,
transferred_result: preview.transferredResult,
remaining_result: preview.remainingResult,
blocking_issues: preview.blockingIssues,
warnings: preview.warnings,
ready_to_close: readyToClose,
Expand All @@ -829,7 +831,7 @@ class AccountingMcpTools {
private Map<String, Object> closeFiscalYear(Map<String, Object> args) {
long companyId = requiredLong(args, 'company_id')
long fiscalYearId = requiredLong(args, 'fiscal_year_id')
String closingAccount = optionalString(args, 'closing_account', ClosingService.DEFAULT_CLOSING_ACCOUNT)
String closingAccount = optionalString(args, 'closing_account', null)
String providedToken = args.get('preview_token') as String
if (!providedToken) {
return [ok: false, errors: ['preview_token krävs — kör preview_year_end först.']]
Expand All @@ -840,6 +842,7 @@ class AccountingMcpTools {
if (expectedCompanyId != companyId) {
return [ok: false, errors: ["Fiscal year ${fiscalYearId} does not belong to company ${companyId}.".toString()]]
}
closingAccount = closingAccount == null ? closingService.defaultClosingAccount(fiscalYearId) : closingAccount
String expectedToken = yearEndPreviewToken(fiscalYearId, closingAccount)
if (providedToken != expectedToken) {
return [ok: false, errors: ['preview_token stämmer inte — kör preview_year_end igen.']]
Expand All @@ -858,10 +861,12 @@ class AccountingMcpTools {
next_fiscal_year_name: result.nextFiscalYear?.name,
closing_voucher_id: result.closingVoucher?.id,
closing_voucher_number: result.closingVoucher?.voucherNumber,
result_account_count: result.resultAccountCount,
result_accounts_with_balance_count: result.resultAccountsWithBalanceCount,
opening_balance_count: result.openingBalanceCount,
closing_entry_count: result.closingEntryCount,
net_result: result.netResult,
transferred_result: result.transferredResult,
remaining_result: result.remainingResult,
warnings: result.warnings
]
} catch (Exception exception) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package se.alipsa.accounting.mcp

import se.alipsa.accounting.domain.LegalForm

/**
* Builds MCP tool schemas exposed by the local accounting MCP server.
*/
Expand Down Expand Up @@ -195,24 +197,31 @@ final class McpToolDefinitions {
]
}

private static String closingAccountDescription() {
String defaults = LegalForm.values().collect { LegalForm form ->
"${form.name()}: ${form.defaultResultAccount}".toString()
}.join(', ')
"Optional closing account number. Defaults by legal form (${defaults}); an unset legal form uses ${LegalForm.AKTIEBOLAG.defaultResultAccount}."
}

private static List<Map<String, Object>> yearEndToolDefs() {
[
toolDef('preview_year_end',
'Runs year-end closing pre-checks. Returns blocking issues, warnings, totals, net result, and a preview_token.',
'Runs year-end closing pre-checks. Returns blocking issues, warnings, income_total and expense_total excluding 8999, net_result for the year, transferred_result via 8999, remaining_result to transfer, result_accounts_with_balance_count, and a preview_token.',
['company_id', 'fiscal_year_id'],
[
company_id: intParam('Company ID'),
fiscal_year_id: intParam('Fiscal year ID to preview'),
closing_account: optStrParam('Optional closing account number. Defaults to "2099".')
closing_account: optStrParam(closingAccountDescription())
]
),
toolDef('close_fiscal_year',
'Closes the fiscal year. Requires the preview_token returned by preview_year_end for the same fiscal year and closing account.',
'Closes the fiscal year. Requires the preview_token returned by preview_year_end for the same fiscal year and closing account. Returns the preview\'s net_result, prior transferred_result, remaining_result posted by this closing, and result_accounts_with_balance_count.',
['company_id', 'fiscal_year_id', 'preview_token'],
[
company_id: intParam('Company ID'),
fiscal_year_id: intParam('Fiscal year ID to close'),
closing_account: optStrParam('Optional closing account number. Defaults to "2099".'),
closing_account: optStrParam(closingAccountDescription()),
preview_token: strParam('Token returned by preview_year_end.')
]
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ final class YearEndClosingPreview {
FiscalYear nextFiscalYear
boolean nextFiscalYearWillBeCreated
String closingAccountNumber
int resultAccountCount
int resultAccountsWithBalanceCount
BigDecimal incomeTotal
BigDecimal expenseTotal
BigDecimal netResult
BigDecimal transferredResult
BigDecimal remainingResult
List<String> blockingIssues = []
List<String> warnings = []
}
Expand All @@ -26,10 +28,12 @@ final class YearEndClosingResult {
FiscalYear closedFiscalYear
FiscalYear nextFiscalYear
Voucher closingVoucher
int resultAccountCount
int resultAccountsWithBalanceCount
int openingBalanceCount
int closingEntryCount
BigDecimal netResult
BigDecimal transferredResult
BigDecimal remainingResult
List<String> warnings = []
}

Expand Down Expand Up @@ -68,3 +72,13 @@ final class ClosingExecution {
int openingBalanceCount
int closingEntryCount
}

@Canonical
final class ClosingResultTotals {

BigDecimal incomeTotal
BigDecimal expenseTotal
BigDecimal netResult
BigDecimal transferredResult
BigDecimal remainingResult
}
Loading
Loading