Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
/**
* Copyright 2023-present DreamNum Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { getDateSerialNumberByObject, getTwoDateDaysByBasis } from '../../../basics/date';
import { ErrorType } from '../../../basics/error-type';
import { checkVariantsErrorIsArrayOrBoolean } from '../../../engine/utils/check-variant-error';
import { ErrorValueObject } from '../../../engine/value-object/base-value-object';
import { NumberValueObject } from '../../../engine/value-object/primitive-object';
import { BaseFunction } from '../../base-function';
export class Disc extends BaseFunction {
override minParams = 4;
override maxParams = 5;
override calculate(settlement: BaseValueObject, maturity: BaseValueObject, pr: BaseValueObject, redemption: BaseValueObject, basis?: BaseValueObject): BaseValueObject {
const _basis = basis ?? NumberValueObject.create(0);
const { isError, errorObject, variants } = checkVariantsErrorIsArrayOrBoolean(settlement, maturity, pr, redemption, _basis);
if (isError) {
return errorObject as ErrorValueObject;
}
const [settlementObject, maturityObject, prObject, redemptionObject, basisObject] = variants as BaseValueObject[];
const settlementSerialNumber = getDateSerialNumberByObject(settlementObject, this.getDateSystem());
if (typeof settlementSerialNumber !== 'number') {
return settlementSerialNumber;
}
const maturitySerialNumber = getDateSerialNumberByObject(maturityObject, this.getDateSystem());
if (typeof maturitySerialNumber !== 'number') {
return maturitySerialNumber;
}
const prValue = +prObject.getValue();
const redemptionValue = +redemptionObject.getValue();
const basisValue = Math.floor(+basisObject.getValue());
if (Number.isNaN(prValue) || Number.isNaN(redemptionValue) || Number.isNaN(basisValue)) {
return ErrorValueObject.create(ErrorType.VALUE);
}
if (
prValue <= 0 ||
redemptionValue <= 0 ||
basisValue < 0 ||
basisValue > 4 ||
Math.floor(settlementSerialNumber) >= Math.floor(maturitySerialNumber)
) {
return ErrorValueObject.create(ErrorType.NUM);
}
const { days, yearDays } = getTwoDateDaysByBasis(settlementSerialNumber, maturitySerialNumber, basisValue, this.getDateSystem());
const result = ((redemptionValue - prValue) / redemptionValue) * (yearDays / days);
return NumberValueObject.create(result);
}
}