Skip to content

Commit

Permalink
balance sheet Accumulate complete
Browse files Browse the repository at this point in the history
  • Loading branch information
TinyMurky committed Oct 11, 2024
1 parent 9246b82 commit 4f39f67
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "iSunFA",
"version": "0.8.2+42",
"version": "0.8.2+43",
"private": false,
"scripts": {
"dev": "next dev",
Expand Down
12 changes: 12 additions & 0 deletions src/constants/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 5 additions & 2 deletions src/lib/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
84 changes: 74 additions & 10 deletions src/lib/utils/report/balance_sheet_generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
Expand All @@ -48,14 +62,29 @@ export default class BalanceSheetGenerator extends FinancialReportGenerator {
private async closeAccountFromIncomeStatement(
curPeriod: boolean
): Promise<ILineItemIncludeAccount[]> {
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;

Expand All @@ -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
);
Expand All @@ -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
);
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 4f39f67

Please sign in to comment.