Commit 6c1365db authored by Alexander.Trofimov's avatar Alexander.Trofimov

cell/model/FormulaObjects/dateandtimeFunctions to function-closure

parent e80e7a7a
/*
*
* (c) Copyright Ascensio System Limited 2010-2016
*
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
* In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that
* Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
*
* THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
*
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
*
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
*
* Pursuant to Section 7  3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
* relevant author attributions when distributing the software. If the display of the logo in its graphic
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
* in every copy of the program you distribute.
* Pursuant to Section 7  3(e) we decline to grant you any rights under trademark law for use of our trademarks.
*
*/
/*
*
* (c) Copyright Ascensio System Limited 2010-2016
*
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
* In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that
* Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
*
* THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
*
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
*
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
*
* Pursuant to Section 7  3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
* relevant author attributions when distributing the software. If the display of the logo in its graphic
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
* in every copy of the program you distribute.
* Pursuant to Section 7  3(e) we decline to grant you any rights under trademark law for use of our trademarks.
*
*/
$( function () {
function toFixed( n ) {
......@@ -3888,7 +3888,7 @@ $( function () {
if( settlement >= maturity || pr <= 0 || redemption <= 0 || basis < 0 || basis > 4 )
return "#NUM!"
return ( 1.0 - pr / redemption ) / yearFrac( settlement, maturity, basis );
return ( 1.0 - pr / redemption ) / AscCommonExcel.yearFrac( settlement, maturity, basis );
}
......@@ -3967,7 +3967,7 @@ $( function () {
if( settlement >= maturity || investment <= 0 || discount <= 0 || basis < 0 || basis > 4 )
return "#NUM!"
return investment / ( 1 - ( discount * yearFrac( settlement, maturity, basis) ) )
return investment / ( 1 - ( discount * AscCommonExcel.yearFrac( settlement, maturity, basis) ) )
}
......@@ -4084,7 +4084,7 @@ $( function () {
if( settlement >= maturity || investment <= 0 || redemption <= 0 || basis < 0 || basis > 4 )
return "#NUM!"
return ( ( redemption / investment ) - 1 ) / yearFrac( settlement, maturity, basis )
return ( ( redemption / investment ) - 1 ) / AscCommonExcel.yearFrac( settlement, maturity, basis )
}
......@@ -4128,7 +4128,7 @@ $( function () {
var d1 = settlement
var d2 = maturity
var fFraction = yearFrac(d1, d2, 0);
var fFraction = AscCommonExcel.yearFrac(d1, d2, 0);
if( fFraction - Math.floor( fFraction ) == 0 )
return "#NUM!"
......
......@@ -23,7 +23,12 @@
*
*/
"use strict";
(
/**
* @param {Window} window
* @param {undefined} undefined
*/
function (window, undefined) {
var DayCountBasis = {
// US 30/360
UsPsa30_360:0,
......@@ -1909,4 +1914,13 @@ cYEARFRAC.prototype.getInfo = function () {
name:this.name,
args:"( start-date , end-date [ , basis ] )"
};
}
\ No newline at end of file
}
//----------------------------------------------------------export----------------------------------------------------
window['AscCommonExcel'] = window['AscCommonExcel'] || {};
window['AscCommonExcel'].DayCountBasis = DayCountBasis;
window['AscCommonExcel'].yearFrac = yearFrac;
window['AscCommonExcel'].diffDate = diffDate;
window['AscCommonExcel'].days360 = days360;
window['AscCommonExcel'].daysInYear = daysInYear;
})(window);
......@@ -217,17 +217,17 @@ function lcl_GetCoupncd( settl, matur, freq ) {
function getcoupdaybs( settl, matur, frequency, basis ) {
var n = lcl_GetCouppcd( settl, matur, frequency );
return diffDate( n, settl, basis );
return AscCommonExcel.diffDate( n, settl, basis );
}
function getcoupdays( settl, matur, frequency, basis ) {
if ( basis == DayCountBasis.ActualActual ) {
if ( basis == AscCommonExcel.DayCountBasis.ActualActual ) {
var m = lcl_GetCouppcd( settl, matur, frequency ),
n = new Date( m );
n.addMonths( 12 / frequency );
return diffDate( m, n, basis );
return AscCommonExcel.diffDate( m, n, basis );
}
return new cNumber( daysInYear( 0, basis ) / frequency );
return new cNumber( AscCommonExcel.daysInYear( 0, basis ) / frequency );
}
function getcoupnum( settl, matur, frequency ) {
......@@ -241,7 +241,7 @@ function getcoupdaysnc( settl, matur, frequency, basis ) {
if ( (basis !== 0) && (basis !== 4) ) {
lcl_GetCoupncd( settl, matur, frequency );
return diffDate( settl, matur, basis );
return AscCommonExcel.diffDate( settl, matur, basis );
}
......@@ -324,9 +324,9 @@ function getYield( settle, mat, coup, price, redemp, freq, base ) {
function getyieldmat( settle, mat, issue, rate, price, base ) {
var issMat = yearFrac( issue, mat, base );
var issSet = yearFrac( issue, settle, base );
var setMat = yearFrac( settle, mat, base );
var issMat = AscCommonExcel.yearFrac( issue, mat, base );
var issSet = AscCommonExcel.yearFrac( issue, settle, base );
var setMat = AscCommonExcel.yearFrac( settle, mat, base );
var y = (1 + issMat * rate) / (price / 100 + issSet * rate) - 1;
y /= setMat;
......@@ -368,7 +368,7 @@ function getduration( settlement, maturity, coupon, yld, frequency, basis ) {
function oddFPrice( settl, matur, iss, firstCoup, rate, yld, redemption, frequency, basis ) {
function positiveDaysBetween( d1, d2, b ) {
var res = diffDate( d1, d2, b ).getValue();
var res = AscCommonExcel.diffDate( d1, d2, b ).getValue();
return res > 0 ? res : 0;
}
......@@ -431,7 +431,7 @@ function oddFPrice( settl, matur, iss, firstCoup, rate, yld, redemption, frequen
for ( var index = nc; index >= 1; index-- ) {
earlyCoupon = addMonth( lateCoupon, numMonthsNeg, false );
NLi = basis == DayCountBasis.ActualActual ? positiveDaysBetween( earlyCoupon, lateCoupon, basis ) : E;
NLi = basis == AscCommonExcel.DayCountBasis.ActualActual ? positiveDaysBetween( earlyCoupon, lateCoupon, basis ) : E;
DCi = index > 1 ? NLi : positiveDaysBetween( iss, lateCoupon, basis );
startDate = iss > earlyCoupon ? iss : earlyCoupon;
endDate = settl < lateCoupon ? settl : lateCoupon;
......@@ -441,11 +441,11 @@ function oddFPrice( settl, matur, iss, firstCoup, rate, yld, redemption, frequen
}
if ( basis == DayCountBasis.Actual360 || basis == DayCountBasis.Actual365 ) {
if ( basis == AscCommonExcel.DayCountBasis.Actual360 || basis == AscCommonExcel.DayCountBasis.Actual365 ) {
DSC = positiveDaysBetween( settl, getcoupncd( settl, firstCoup, frequency ), basis );
}
else {
DSC = E - diffDate( lcl_GetCouppcd( settl, firstCoup, frequency ), settl, basis );
DSC = E - AscCommonExcel.diffDate( lcl_GetCouppcd( settl, firstCoup, frequency ), settl, basis );
}
var Nq = coupNumber( firstCoup, settl, numMonths, true );
......@@ -669,7 +669,7 @@ cACCRINT.prototype.Calculate = function ( arg ) {
}
firstDate = new Date( iss > coupPCD ? iss : coupPCD );
days = days360( firstDate, settl, basis );
days = AscCommonExcel.days360( firstDate, settl, basis );
coupDays = getcoupdays( coupPCD, fInter, frequency, basis ).getValue();
res = days / coupDays;
startDate = new Date( coupPCD );
......@@ -679,13 +679,13 @@ cACCRINT.prototype.Calculate = function ( arg ) {
endDate = startDate;
startDate = addMonth( startDate, numMonthsNeg, endMonth );
firstDate = iss > startDate ? iss : startDate;
if ( basis == DayCountBasis.UsPsa30_360 ) {
days = days360( firstDate, endDate, !( iss > startDate ) );
if ( basis == AscCommonExcel.DayCountBasis.UsPsa30_360 ) {
days = AscCommonExcel.days360( firstDate, endDate, !( iss > startDate ) );
coupDays = getcoupdays( startDate, endDate, frequency, basis ).getValue();
}
else {
days = diffDate( firstDate, endDate, basis ).getValue();
coupDays = ( basis == DayCountBasis.Actual365 ) ? ( 365 / frequency ) : diffDate( startDate, endDate, basis ).getValue();
days = AscCommonExcel.diffDate( firstDate, endDate, basis ).getValue();
coupDays = ( basis == AscCommonExcel.DayCountBasis.Actual365 ) ? ( 365 / frequency ) : AscCommonExcel.diffDate( startDate, endDate, basis ).getValue();
}
res += (iss <= startDate) ? calcMethod : days / coupDays;
......@@ -782,7 +782,7 @@ cACCRINTM.prototype.Calculate = function ( arg ) {
return this.value = new cError( cErrorType.not_numeric );
}
var res = yearFrac( Date.prototype.getDateFromExcel( issue ), Date.prototype.getDateFromExcel( settlement ), basis );
var res = AscCommonExcel.yearFrac( Date.prototype.getDateFromExcel( issue ), Date.prototype.getDateFromExcel( settlement ), basis );
res *= rate * par;
......@@ -939,7 +939,7 @@ cAMORDEGRC.prototype.Calculate = function ( arg ) {
function firstDeprLinc( cost, datePurch, firstP, salvage, rate, per, basis){
function fix29February (d){
if ( (basis == DayCountBasis.ActualActual || basis == DayCountBasis.Actual365) && d.isLeapYear() && d.getUTCMonth() == 2 && d.getUTCDate() >= 28){
if ( (basis == AscCommonExcel.DayCountBasis.ActualActual || basis == AscCommonExcel.DayCountBasis.Actual365) && d.isLeapYear() && d.getUTCMonth() == 2 && d.getUTCDate() >= 28){
return new Date(d.getUTCFullYear(), d.getUTCMonth(), 28);
}
else{
......@@ -947,8 +947,8 @@ cAMORDEGRC.prototype.Calculate = function ( arg ) {
}
}
var firstLen = diffDate( fix29February (datePurch), fix29February(firstP), basis),
firstDeprTemp = firstLen / daysInYear( datePurch, basis ) * rate * cost,
var firstLen = AscCommonExcel.diffDate( fix29February (datePurch), fix29February(firstP), basis),
firstDeprTemp = firstLen / AscCommonExcel.daysInYear( datePurch, basis ) * rate * cost,
firstDepr = firstDeprTemp == 0 ? cost * rate : firstDeprTemp,
period = firstDeprTemp == 0 ? per : per + 1,
availDepr = cost - salvage;
......@@ -1111,7 +1111,7 @@ cAMORLINC.prototype.Calculate = function ( arg ) {
return this.value = new cError( cErrorType.not_numeric );
}
var fDepTime = yearFrac( val0, val1, basis ).getValue() * rate * cost,
var fDepTime = AscCommonExcel.yearFrac( val0, val1, basis ).getValue() * rate * cost,
fDep, depr = rate * cost, availDepr, availDeprTemp,
countedPeriod = 1, c = 0, maxIter = 10000;
......@@ -2210,7 +2210,7 @@ cDISC.prototype.Calculate = function ( arg ) {
return this.value = new cError( cErrorType.not_numeric );
}
var res = ( 1 - pr / redemption ) / yearFrac( Date.prototype.getDateFromExcel( settlement ), Date.prototype.getDateFromExcel( maturity ), basis );
var res = ( 1 - pr / redemption ) / AscCommonExcel.yearFrac( Date.prototype.getDateFromExcel( settlement ), Date.prototype.getDateFromExcel( maturity ), basis );
this.value = new cNumber( res );
// this.value.numFormat = 9;
......@@ -2769,7 +2769,7 @@ cINTRATE.prototype.Calculate = function ( arg ) {
return this.value = new cError( cErrorType.not_numeric );
}
var res = ( ( redemption / investment ) - 1 ) / yearFrac( Date.prototype.getDateFromExcel( settlement ), Date.prototype.getDateFromExcel( maturity ), basis );
var res = ( ( redemption / investment ) - 1 ) / AscCommonExcel.yearFrac( Date.prototype.getDateFromExcel( settlement ), Date.prototype.getDateFromExcel( maturity ), basis );
this.value = new cNumber( res );
this.value.numFormat = 10;
......@@ -3863,7 +3863,7 @@ cODDFYIELD.prototype.Calculate = function ( arg ) {
iss = Date.prototype.getDateFromExcel( issue ),
firstCoup = Date.prototype.getDateFromExcel( first_coupon );
var years = diffDate( settl, matur, basis ),
var years = AscCommonExcel.diffDate( settl, matur, basis ),
px = pr - 100,
num = rate * years * 100 - px,
denum = px * 0.25 * ( 1 + 2 * years ) + years * 100,
......@@ -4071,9 +4071,9 @@ cODDLPRICE.prototype.Calculate = function ( arg ) {
matur = Date.prototype.getDateFromExcel( maturity ),
lastInt = Date.prototype.getDateFromExcel( last_interest );
var fDCi = yearFrac( lastInt, matur, basis ) * frequency;
var fDSCi = yearFrac( settl, matur, basis ) * frequency;
var fAi = yearFrac( lastInt, settl, basis ) * frequency;
var fDCi = AscCommonExcel.yearFrac( lastInt, matur, basis ) * frequency;
var fDSCi = AscCommonExcel.yearFrac( settl, matur, basis ) * frequency;
var fAi = AscCommonExcel.yearFrac( lastInt, settl, basis ) * frequency;
var res = redemption + fDCi * 100 * rate / frequency;
res /= fDSCi * yld / frequency + 1;
......@@ -4214,9 +4214,9 @@ cODDLYIELD.prototype.Calculate = function ( arg ) {
matur = Date.prototype.getDateFromExcel( maturity ),
lastInt = Date.prototype.getDateFromExcel( last_interest );
var fDCi = yearFrac( lastInt, matur, basis ) * frequency;
var fDSCi = yearFrac( settl, matur, basis ) * frequency;
var fAi = yearFrac( lastInt, settl, basis ) * frequency;
var fDCi = AscCommonExcel.yearFrac( lastInt, matur, basis ) * frequency;
var fDSCi = AscCommonExcel.yearFrac( settl, matur, basis ) * frequency;
var fAi = AscCommonExcel.yearFrac( lastInt, settl, basis ) * frequency;
var res = redemption + fDCi * 100 * rate / frequency;
res /= pr + fAi * 100 * rate / frequency;
......@@ -4646,7 +4646,7 @@ cPRICEDISC.prototype.Calculate = function ( arg ) {
var settl = Date.prototype.getDateFromExcel( settlement ),
matur = Date.prototype.getDateFromExcel( maturity );
var res = redemption * ( 1 - discount * yearFrac( settl, matur, basis ) );
var res = redemption * ( 1 - discount * AscCommonExcel.yearFrac( settl, matur, basis ) );
return this.value = new cNumber( res );
......@@ -4759,9 +4759,9 @@ cPRICEMAT.prototype.Calculate = function ( arg ) {
matur = Date.prototype.getDateFromExcel( maturity ),
iss = Date.prototype.getDateFromExcel( issue );
var fIssMat = yearFrac( new Date( iss ), new Date( matur ), basis );
var fIssSet = yearFrac( new Date( iss ), new Date( settl ), basis );
var fSetMat = yearFrac( new Date( settl ), new Date( matur ), basis );
var fIssMat = AscCommonExcel.yearFrac( new Date( iss ), new Date( matur ), basis );
var fIssSet = AscCommonExcel.yearFrac( new Date( iss ), new Date( settl ), basis );
var fSetMat = AscCommonExcel.yearFrac( new Date( settl ), new Date( matur ), basis );
var res = 1 + fIssMat * rate;
res /= 1 + fSetMat * yld;
......@@ -5054,7 +5054,7 @@ cRECEIVED.prototype.Calculate = function ( arg ) {
return this.value = new cError( cErrorType.not_numeric );
}
var res = investment / ( 1 - ( discount * yearFrac( Date.prototype.getDateFromExcel( settlement ), Date.prototype.getDateFromExcel( maturity ), basis ) ) );
var res = investment / ( 1 - ( discount * AscCommonExcel.yearFrac( Date.prototype.getDateFromExcel( settlement ), Date.prototype.getDateFromExcel( maturity ), basis ) ) );
this.value = res >= 0 ? new cNumber( res ) : new cError( cErrorType.not_numeric );
// this.value.numFormat = 9;
......@@ -5380,7 +5380,7 @@ cTBILLPRICE.prototype.Calculate = function ( arg ) {
return this.value = new cError( cErrorType.not_numeric );
}
discount *= diffDate( d1, d2, DayCountBasis.ActualActual );
discount *= AscCommonExcel.diffDate( d1, d2, AscCommonExcel.DayCountBasis.ActualActual );
this.value = new cNumber( 100 * ( 1 - discount / 360 ) );
return this.value;
......@@ -6306,7 +6306,7 @@ cYIELDDISC.prototype.Calculate = function ( arg ) {
matur = Date.prototype.getDateFromExcel( maturity );
var fRet = ( redemption / pr ) - 1;
fRet /= yearFrac( settl, matur, basis );
fRet /= AscCommonExcel.yearFrac( settl, matur, basis );
this.value = new cNumber( fRet );
this.value.numFormat = 10;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment