Skip to content

Commit

Permalink
first prototype revenue-profit.calculators.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Nov 5, 2024
1 parent 09a1ac4 commit 064f89c
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 1 deletion.
8 changes: 8 additions & 0 deletions api/src/modules/calculations/conservation-cost.calculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { BaseSize } from '@shared/entities/base-size.entity';
import { SequestrationRatesCalculator } from '@api/modules/calculations/sequestration-rate.calculator';
import { RESTORATION_ACTIVITY_SUBTYPE } from '@shared/entities/projects.entity';
import { ACTIVITY } from '@shared/entities/activity.enum';
import { RevenueProfitCalculator } from '@api/modules/calculations/revenue-profit.calculators';

export class ConservationCostCalculator extends CostCalculator {
project: ConservationProject;
Expand All @@ -29,6 +30,7 @@ export class ConservationCostCalculator extends CostCalculator {
baseIncrease: BaseIncrease;
baseSize: BaseSize;
sequestrationCreditsCalculator: SequestrationRatesCalculator;
revenueProfitCalculator: RevenueProfitCalculator;
constructor(
project: ConservationProject,
baseIncrease: BaseIncrease,
Expand Down Expand Up @@ -63,6 +65,12 @@ export class ConservationCostCalculator extends CostCalculator {
ACTIVITY.CONSERVATION,
RESTORATION_ACTIVITY_SUBTYPE.PLANTING,
);
this.revenueProfitCalculator = new RevenueProfitCalculator(
this.project,
this.conservationProjectLength,
this.defaultProjectLength,
this.sequestrationCreditsCalculator,
);
}

private initializeCostPlan(): { [year: number]: number } {
Expand Down
115 changes: 114 additions & 1 deletion api/src/modules/calculations/revenue-profit.calculators.ts
Original file line number Diff line number Diff line change
@@ -1 +1,114 @@
export class RevenueProfitCalculator {}
import { ConservationProject } from '@api/modules/custom-projects/conservation.project';
import { SequestrationRatesCalculator } from '@api/modules/calculations/sequestration-rate.calculator';

export class RevenueProfitCalculator {
private project: ConservationProject;
private sequestrationCreditsCalculator: SequestrationRatesCalculator;
private projectLength: number;
private defaultProjectLength: number;

constructor(
project: ConservationProject,
projectLength: number,
defaultProjectLength: number,
sequestrationCreditsCalculator: SequestrationRatesCalculator,
) {
this.project = project;
this.sequestrationCreditsCalculator = sequestrationCreditsCalculator;
this.projectLength = projectLength;
this.defaultProjectLength = defaultProjectLength;
}

public calculateEstimatedRevenue(): { [year: number]: number } {
const estimatedRevenuePlan: { [year: number]: number } = {};

for (let year = -4; year <= this.defaultProjectLength; year++) {
if (year !== 0) {
estimatedRevenuePlan[year] = 0;
}
}

const estimatedCreditsIssued =
this.sequestrationCreditsCalculator.calculateEstimatedCreditsIssued();

for (const year in estimatedRevenuePlan) {
const yearNum = Number(year);
if (yearNum <= this.projectLength) {
if (yearNum < -1) {
estimatedRevenuePlan[yearNum] = 0;
} else {
estimatedRevenuePlan[yearNum] =
estimatedCreditsIssued[yearNum] *
this.project.carbonPrice *
Math.pow(1 + this.project.carbonPriceIncrease, yearNum);
}
} else {
estimatedRevenuePlan[yearNum] = 0;
}
}

return estimatedRevenuePlan;
}

public calculateAnnualNetCashFlow(
capexTotalCostPlan: { [year: number]: number },
opexTotalCostPlan: { [year: number]: number },
): { [year: number]: number } {
const estimatedRevenue = this.calculateEstimatedRevenue();
const costPlans = {
capexTotal: { ...capexTotalCostPlan },
opexTotal: { ...opexTotalCostPlan },
};

for (const key in costPlans) {
for (const year in costPlans[key]) {
costPlans[key][year] = -costPlans[key][year];
}
}

const totalCostPlan: { [year: number]: number } = {};
for (const year of new Set([
...Object.keys(costPlans.capexTotal),
...Object.keys(costPlans.opexTotal),
])) {
const yearNum = Number(year);
totalCostPlan[yearNum] =
(costPlans.capexTotal[yearNum] || 0) +
(costPlans.opexTotal[yearNum] || 0);
}

const annualNetCashFlow: { [year: number]: number } = {};
for (let year = -4; year <= this.projectLength; year++) {
if (year !== 0) {
annualNetCashFlow[year] =
estimatedRevenue[year] + (totalCostPlan[year] || 0);
}
}

return annualNetCashFlow;
}

public calculateAnnualNetIncome(opexTotalCostPlan: {
[year: number]: number;
}): { [year: number]: number } {
const costPlans = {
opexTotal: { ...opexTotalCostPlan },
};

for (const year in costPlans.opexTotal) {
costPlans.opexTotal[year] = -costPlans.opexTotal[year];
}

const estimatedRevenue = this.calculateEstimatedRevenue();

const annualNetIncome: { [year: number]: number } = {};
for (let year = -4; year <= this.projectLength; year++) {
if (year !== 0) {
annualNetIncome[year] =
estimatedRevenue[year] + (costPlans.opexTotal[year] || 0);
}
}

return annualNetIncome;
}
}

0 comments on commit 064f89c

Please sign in to comment.