From 4f39f6707fdad3eb5f6d6d69dcab354d4e3ba610 Mon Sep 17 00:00:00 2001 From: Tiny_Murky Date: Fri, 11 Oct 2024 13:53:47 +0800 Subject: [PATCH] balance sheet Accumulate complete --- package.json | 2 +- src/constants/account.ts | 12 +++ src/lib/utils/common.ts | 7 +- .../utils/report/balance_sheet_generator.ts | 84 ++++++++++++++++--- 4 files changed, 92 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index a9c93ac60..1e5effa30 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "iSunFA", - "version": "0.8.2+42", + "version": "0.8.2+43", "private": false, "scripts": { "dev": "next dev", diff --git a/src/constants/account.ts b/src/constants/account.ts index dc46aeb94..899bfb996 100644 --- a/src/constants/account.ts +++ b/src/constants/account.ts @@ -306,6 +306,18 @@ export const SPECIAL_ACCOUNTS: { rootCode: '3350', level: 3, }, + ACCUMULATED_PROFIT_AND_LOSS: { + system: 'IFRS', + type: AccountType.EQUITY, + debit: false, + liquidity: false, + code: '3351', + name: '累積盈虧', + forUser: true, + parentCode: '3350', + rootCode: '3350', + level: 3, + }, OTHER_EQUITY_OTHER: { system: 'IFRS', type: AccountType.EQUITY, diff --git a/src/lib/utils/common.ts b/src/lib/utils/common.ts index 4f478bcd7..6211d7506 100644 --- a/src/lib/utils/common.ts +++ b/src/lib/utils/common.ts @@ -459,8 +459,11 @@ export function setTimestampToDayStart(timestamp: number) { return timestampInSeconds(date.getTime()); } -export function getTimestampOfFirstDateOfThisYear() { - const year = new Date().getFullYear(); +export function getTimestampOfFirstDateOfThisYear(currentDateInSecond?: number) { + const dateToGetYear = currentDateInSecond + ? new Date(timestampInMilliSeconds(currentDateInSecond)) + : new Date(); + const year = dateToGetYear.getFullYear(); const date = new Date(year, 0, 1); const timestamp = date.getTime(); const timestampInSecond = setTimestampToDayStart(timestamp); diff --git a/src/lib/utils/report/balance_sheet_generator.ts b/src/lib/utils/report/balance_sheet_generator.ts index 188297b88..ccedef30e 100644 --- a/src/lib/utils/report/balance_sheet_generator.ts +++ b/src/lib/utils/report/balance_sheet_generator.ts @@ -17,28 +17,42 @@ import IncomeStatementGenerator from '@/lib/utils/report/income_statement_genera import { DAY_IN_YEAR } from '@/constants/common'; import { EMPTY_I_ACCOUNT_READY_FRONTEND } from '@/constants/financial_report'; import { ASSET_CODE, SPECIAL_ACCOUNTS } from '@/constants/account'; -import { timestampToString } from '@/lib/utils/common'; +import { getTimestampOfFirstDateOfThisYear, timestampToString } from '@/lib/utils/common'; import { ILineItemIncludeAccount } from '@/interfaces/line_item'; import { findUniqueAccountByCodeInPrisma } from '@/lib/utils/repo/account.repo'; export default class BalanceSheetGenerator extends FinancialReportGenerator { + private startSecondOfYear: number; + private incomeStatementGenerator: IncomeStatementGenerator; - private incomeStatementGeneratorFromTimeZero: IncomeStatementGenerator; + private incomeStatementGeneratorFromTimeZeroToBeginOfYear: IncomeStatementGenerator; + + private incomeStatementGeneratorFromBeginOfYearToEndDate: IncomeStatementGenerator; constructor(companyId: number, startDateInSecond: number, endDateInSecond: number) { const reportSheetType = ReportSheetType.BALANCE_SHEET; super(companyId, 0, endDateInSecond, reportSheetType); + this.startSecondOfYear = getTimestampOfFirstDateOfThisYear(startDateInSecond); + this.incomeStatementGenerator = new IncomeStatementGenerator( companyId, startDateInSecond, endDateInSecond ); - this.incomeStatementGeneratorFromTimeZero = new IncomeStatementGenerator( + // Info: (20241011 - Murky) For Accumulate Profit and Loss + this.incomeStatementGeneratorFromTimeZeroToBeginOfYear = new IncomeStatementGenerator( companyId, 0, + this.startSecondOfYear + ); + + // Info: (20241011 - Murky) For NetIncome + this.incomeStatementGeneratorFromBeginOfYearToEndDate = new IncomeStatementGenerator( + companyId, + this.startSecondOfYear, endDateInSecond ); } @@ -48,14 +62,29 @@ export default class BalanceSheetGenerator extends FinancialReportGenerator { private async closeAccountFromIncomeStatement( curPeriod: boolean ): Promise { - const incomeStatementContent = - await this.incomeStatementGeneratorFromTimeZero.generateIAccountReadyForFrontendArray(); + const currentYearISContent = + await this.incomeStatementGeneratorFromBeginOfYearToEndDate.generateIAccountReadyForFrontendArray(); + const beforeISContent = + await this.incomeStatementGeneratorFromTimeZeroToBeginOfYear.generateIAccountReadyForFrontendArray(); + + // Info: (20241011 - Murky) net income 是本期範圍內的營收 const netIncome = - incomeStatementContent.find((account) => account.code === SPECIAL_ACCOUNTS.NET_INCOME.code) || + currentYearISContent.find((account) => account.code === SPECIAL_ACCOUNTS.NET_INCOME.code) || + EMPTY_I_ACCOUNT_READY_FRONTEND; + + // Info: (20241011 - Murky) Accumulate Profit and loss是本期以前的營收 + const accumulateProfitAndLoss = + beforeISContent.find((account) => account.code === SPECIAL_ACCOUNTS.NET_INCOME.code) || EMPTY_I_ACCOUNT_READY_FRONTEND; - const otherComprehensiveIncome = - incomeStatementContent.find( + + const currentOtherComprehensiveIncome = + currentYearISContent.find( + (account) => account.code === SPECIAL_ACCOUNTS.OTHER_COMPREHENSIVE_INCOME.code + ) || EMPTY_I_ACCOUNT_READY_FRONTEND; + + const beforeOtherComprehensiveIncome = + beforeISContent.find( (account) => account.code === SPECIAL_ACCOUNTS.OTHER_COMPREHENSIVE_INCOME.code ) || EMPTY_I_ACCOUNT_READY_FRONTEND; @@ -64,6 +93,7 @@ export default class BalanceSheetGenerator extends FinancialReportGenerator { const netIncomeAccount = await findUniqueAccountByCodeInPrisma( SPECIAL_ACCOUNTS.NET_INCOME.code ); + const otherComprehensiveIncomeAccount = await findUniqueAccountByCodeInPrisma( SPECIAL_ACCOUNTS.OTHER_COMPREHENSIVE_INCOME.code ); @@ -72,6 +102,10 @@ export default class BalanceSheetGenerator extends FinancialReportGenerator { SPECIAL_ACCOUNTS.NET_INCOME_IN_EQUITY.code ); + const accumulateProfitAndLossInEquity = await findUniqueAccountByCodeInPrisma( + SPECIAL_ACCOUNTS.ACCUMULATED_PROFIT_AND_LOSS.code + ); + const otherEquityOther = await findUniqueAccountByCodeInPrisma( SPECIAL_ACCOUNTS.OTHER_EQUITY_OTHER.code ); @@ -102,11 +136,41 @@ export default class BalanceSheetGenerator extends FinancialReportGenerator { }, }); + closeAccount.push({ + id: accumulateProfitAndLossInEquity?.id || -1, + amount: curPeriod + ? accumulateProfitAndLoss.curPeriodAmount + : accumulateProfitAndLoss.prePeriodAmount, + description: SPECIAL_ACCOUNTS.ACCUMULATED_PROFIT_AND_LOSS.name, + debit: SPECIAL_ACCOUNTS.ACCUMULATED_PROFIT_AND_LOSS.debit, + accountId: accumulateProfitAndLossInEquity?.id || -1, + voucherId: -1, + createdAt: 1, + updatedAt: 1, + deletedAt: null, + account: netIncomeAccount + ? { + ...netIncomeAccount, + code: SPECIAL_ACCOUNTS.ACCUMULATED_PROFIT_AND_LOSS.code, + debit: SPECIAL_ACCOUNTS.ACCUMULATED_PROFIT_AND_LOSS.debit, + } + : { + ...SPECIAL_ACCOUNTS.ACCUMULATED_PROFIT_AND_LOSS, + id: -1, + companyId: this.companyId, + createdAt: 1, + updatedAt: 1, + deletedAt: null, + }, + }); + closeAccount.push({ id: otherEquityOther?.id || -1, amount: curPeriod - ? otherComprehensiveIncome.curPeriodAmount - : otherComprehensiveIncome.prePeriodAmount, + ? currentOtherComprehensiveIncome.curPeriodAmount + + beforeOtherComprehensiveIncome.curPeriodAmount + : beforeOtherComprehensiveIncome.prePeriodAmount + + beforeOtherComprehensiveIncome.prePeriodAmount, description: SPECIAL_ACCOUNTS.OTHER_EQUITY_OTHER.name, debit: SPECIAL_ACCOUNTS.OTHER_EQUITY_OTHER.debit, accountId: otherEquityOther?.id || -1,