Commit b4b7fed5 authored by Dmitry.Shahtanov's avatar Dmitry.Shahtanov Committed by Alexander.Trofimov

fixed: Bug 24266 - Не учитывается необязательный аргумент Calc_method при...

fixed: Bug 24266 - Не учитывается необязательный аргумент Calc_method при вычислении функции ACCRINT

git-svn-id: svn://192.168.3.15/activex/AVS/Sources/TeamlabOffice/trunk/OfficeWeb@56411 954022d7-b5bf-4e40-9824-e11837661b57
parent ff9634af
...@@ -36,7 +36,7 @@ function yearFrac( d1, d2, mode ) { ...@@ -36,7 +36,7 @@ function yearFrac( d1, d2, mode ) {
switch ( mode ) { switch ( mode ) {
case DayCountBasis.UsPsa30_360: case DayCountBasis.UsPsa30_360:
return new cNumber( Math.abs( GetDiffDate360( date1, month1, year1, d1.isLeapYear(), date2, month2, year2, true ) ) / 360 ); return new cNumber( Math.abs( GetDiffDate360( date1, month1, year1, date2, month2, year2, true ) ) / 360 );
case DayCountBasis.ActualActual: case DayCountBasis.ActualActual:
var yc = /*Math.abs*/( year2 - year1 ), var yc = /*Math.abs*/( year2 - year1 ),
sd = year1 > year2 ? new Date( d2 ) : new Date( d1 ), sd = year1 > year2 ? new Date( d2 ) : new Date( d1 ),
...@@ -57,7 +57,7 @@ function yearFrac( d1, d2, mode ) { ...@@ -57,7 +57,7 @@ function yearFrac( d1, d2, mode ) {
dayDiff /= (365 * c_msPerDay); dayDiff /= (365 * c_msPerDay);
return new cNumber( dayDiff ); return new cNumber( dayDiff );
case DayCountBasis.Europ30_360: case DayCountBasis.Europ30_360:
return new cNumber( Math.abs( GetDiffDate360( date1, month1, year1, d1.isLeapYear(), date2, month2, year2, false ) ) / 360 ); return new cNumber( Math.abs( GetDiffDate360( date1, month1, year1, date2, month2, year2, false ) ) / 360 );
default: default:
return new cError( cErrorType.not_numeric ); return new cError( cErrorType.not_numeric );
} }
...@@ -73,7 +73,7 @@ function diffDate( d1, d2, mode ) { ...@@ -73,7 +73,7 @@ function diffDate( d1, d2, mode ) {
switch ( mode ) { switch ( mode ) {
case DayCountBasis.UsPsa30_360: case DayCountBasis.UsPsa30_360:
return new cNumber( GetDiffDate360( date1, month1, year1, d1.isLeapYear(), date2, month2, year2, true ) ); return new cNumber( GetDiffDate360( date1, month1, year1, date2, month2, year2, true ) );
case DayCountBasis.ActualActual: case DayCountBasis.ActualActual:
var yc = /*Math.abs*/( year2 - year1 ), var yc = /*Math.abs*/( year2 - year1 ),
sd = year1 > year2 ? new Date( d2 ) : new Date( d1 ), sd = year1 > year2 ? new Date( d2 ) : new Date( d1 ),
...@@ -94,7 +94,7 @@ function diffDate( d1, d2, mode ) { ...@@ -94,7 +94,7 @@ function diffDate( d1, d2, mode ) {
dayDiff /= c_msPerDay; dayDiff /= c_msPerDay;
return new cNumber( dayDiff ); return new cNumber( dayDiff );
case DayCountBasis.Europ30_360: case DayCountBasis.Europ30_360:
return new cNumber( GetDiffDate360( date1, month1, year1, d1.isLeapYear(), date2, month2, year2, false ) ); return new cNumber( GetDiffDate360( date1, month1, year1, date2, month2, year2, false ) );
default: default:
return new cError( cErrorType.not_numeric ); return new cError( cErrorType.not_numeric );
} }
...@@ -114,7 +114,7 @@ function diffDate2( d1, d2, mode ) { ...@@ -114,7 +114,7 @@ function diffDate2( d1, d2, mode ) {
case DayCountBasis.UsPsa30_360: case DayCountBasis.UsPsa30_360:
nDaysInYear = 360; nDaysInYear = 360;
nYears = year1 - year2; nYears = year1 - year2;
nDayDiff = Math.abs( GetDiffDate360( date1, month1 + 1, year1, d1.isLeapYear(), date2, month2 + 1, year2, true ) ) - nYears * nDaysInYear; nDayDiff = Math.abs( GetDiffDate360( date1, month1 + 1, year1, date2, month2 + 1, year2, true ) ) - nYears * nDaysInYear;
return new cNumber( nYears + nDayDiff / nDaysInYear ); return new cNumber( nYears + nDayDiff / nDaysInYear );
case DayCountBasis.ActualActual: case DayCountBasis.ActualActual:
nYears = year2 - year1; nYears = year2 - year1;
...@@ -148,7 +148,7 @@ function diffDate2( d1, d2, mode ) { ...@@ -148,7 +148,7 @@ function diffDate2( d1, d2, mode ) {
case DayCountBasis.Europ30_360: case DayCountBasis.Europ30_360:
nDaysInYear = 360; nDaysInYear = 360;
nYears = year1 - year2; nYears = year1 - year2;
nDayDiff = Math.abs( GetDiffDate360( date1, month1 + 1, year1, d1.isLeapYear(), date2, month2 + 1, year2, false ) ) - nYears * nDaysInYear; nDayDiff = Math.abs( GetDiffDate360( date1, month1 + 1, year1, date2, month2 + 1, year2, false ) ) - nYears * nDaysInYear;
return new cNumber( nYears + nDayDiff / nDaysInYear ); return new cNumber( nYears + nDayDiff / nDaysInYear );
default: default:
return new cError( cErrorType.not_numeric ); return new cError( cErrorType.not_numeric );
...@@ -156,7 +156,7 @@ function diffDate2( d1, d2, mode ) { ...@@ -156,7 +156,7 @@ function diffDate2( d1, d2, mode ) {
} }
function GetDiffDate( d1, d2, nMode ) { function GetDiffDate( d1, d2, nMode ) {
var bNeg = d1 > d2; var bNeg = d1 > d2, nRet, pOptDaysIn1stYear;
if ( bNeg ) { if ( bNeg ) {
var n = d2; var n = d2;
...@@ -164,8 +164,6 @@ function GetDiffDate( d1, d2, nMode ) { ...@@ -164,8 +164,6 @@ function GetDiffDate( d1, d2, nMode ) {
d1 = n; d1 = n;
} }
var nRet, pOptDaysIn1stYear
var nD1 = d1.getUTCDate(), var nD1 = d1.getUTCDate(),
nM1 = d1.getUTCMonth(), nM1 = d1.getUTCMonth(),
nY1 = d1.getUTCFullYear(), nY1 = d1.getUTCFullYear(),
......
...@@ -201,7 +201,7 @@ function getcoupdays( settl, matur, frequency, basis ) { ...@@ -201,7 +201,7 @@ function getcoupdays( settl, matur, frequency, basis ) {
n.addMonths( 12 / frequency ); n.addMonths( 12 / frequency );
return diffDate( m, n, basis ); return diffDate( m, n, basis );
} }
return daysInYear( 0, basis ) / frequency; return new cNumber( daysInYear( 0, basis ) / frequency );
} }
function getcoupnum( settl, matur, frequency ) { function getcoupnum( settl, matur, frequency ) {
...@@ -347,16 +347,16 @@ function oddFPrice(settl,matur,iss,firstCoup,rate,yld,redemption,frequency,basis ...@@ -347,16 +347,16 @@ function oddFPrice(settl,matur,iss,firstCoup,rate,yld,redemption,frequency,basis
return newDate; return newDate;
} }
function coupNumber( mat, settl, numMonths, isWholeNumber ) { function coupNumber( startDate, endDate, countMonths, isWholeNumber ) {
var my = mat.getUTCFullYear(), mm = mat.getUTCMonth(), md = mat.getUTCDate(), var my = startDate.getUTCFullYear(), mm = startDate.getUTCMonth(), md = startDate.getUTCDate(),
endOfMonthTemp = mat.lastDayOfMonth(), endOfMonthTemp = startDate.lastDayOfMonth(),
endOfMonth = (!endOfMonthTemp && mm != 1 && md > 28 && md < new Date( my, mm ).getDaysInMonth()) ? settl.lastDayOfMonth() : endOfMonthTemp, endOfMonth = (!endOfMonthTemp && mm != 1 && md > 28 && md < new Date( my, mm ).getDaysInMonth()) ? endDate.lastDayOfMonth() : endOfMonthTemp,
startDate = addMonth( settl, 0, endOfMonth ), startDate = addMonth( endDate, 0, endOfMonth ),
coupons = (isWholeNumber - 0) + (settl < startDate), coupons = (isWholeNumber - 0) + (endDate < startDate),
frontDate = addMonth( startDate, numMonths, endOfMonth ); frontDate = addMonth( startDate, countMonths, endOfMonth );
while ( !(numMonths > 0 ? frontDate >= endDate : frontDate <= endDate) ) { while ( !(countMonths > 0 ? frontDate >= endDate : frontDate <= endDate) ) {
frontDate = addMonth( frontDate, numMonths, endOfMonth ); frontDate = addMonth( frontDate, countMonths, endOfMonth );
coupons++; coupons++;
} }
...@@ -366,7 +366,7 @@ function oddFPrice(settl,matur,iss,firstCoup,rate,yld,redemption,frequency,basis ...@@ -366,7 +366,7 @@ function oddFPrice(settl,matur,iss,firstCoup,rate,yld,redemption,frequency,basis
var res = 0, DSC, var res = 0, DSC,
numMonths = 12 / frequency, numMonths = 12 / frequency,
numMonthsNeg = -numMonths, numMonthsNeg = - numMonths,
E = getcoupdays( settl, new Date( firstCoup ), frequency, basis ).getValue(), E = getcoupdays( settl, new Date( firstCoup ), frequency, basis ).getValue(),
coupNums = getcoupnum( settl, new Date( matur ), frequency ), coupNums = getcoupnum( settl, new Date( matur ), frequency ),
dfc = positiveDaysBetween( new Date( iss ), new Date( firstCoup ), basis ); dfc = positiveDaysBetween( new Date( iss ), new Date( firstCoup ), basis );
...@@ -598,26 +598,72 @@ cACCRINT.prototype.Calculate = function ( arg ) { ...@@ -598,26 +598,72 @@ cACCRINT.prototype.Calculate = function ( arg ) {
if ( basis instanceof cError ) return this.value = basis; if ( basis instanceof cError ) return this.value = basis;
if ( calcMethod instanceof cError ) return this.value = calcMethod; if ( calcMethod instanceof cError ) return this.value = calcMethod;
var _arg5 = frequency.getValue(); issue = issue.getValue();
firstInterest = firstInterest.getValue();
settlement = settlement.getValue();
rate = rate.getValue();
par = par.getValue();
frequency = frequency.getValue();
basis = basis.getValue();
calcMethod = calcMethod.toBool();
if ( issue.getValue() >= settlement.getValue() || rate.getValue() <= 0 || par.getValue() <= 0 || basis.getValue() < 0 || basis.getValue() > 4 || (_arg5 != 1 && _arg5 != 2 && _arg5 != 4) ) { if ( issue >= settlement || rate <= 0 || par <= 0 ||
basis < 0 || basis > 4 || (frequency != 1 && frequency != 2 && frequency != 4) ) {
return this.value = new cError( cErrorType.not_numeric ); return this.value = new cError( cErrorType.not_numeric );
} }
var res; function addMonth( orgDate, numMonths, returnLastDay ) {
/*if ( settlement.getValue()>firstInterest.getValue() ){ var newDate = new Date( orgDate );
res = calcMethod.toBool() ? newDate.addMonths( numMonths );
yearFrac( Date.prototype.getDateFromExcel( firstInterest.getValue() ), Date.prototype.getDateFromExcel( settlement.getValue() ), basis.getValue() ): if ( returnLastDay ) {
yearFrac( Date.prototype.getDateFromExcel( issue.getValue() ), Date.prototype.getDateFromExcel( settlement.getValue() ), basis.getValue() ); newDate.setUTCDate( newDate.getDaysInMonth() );
}
} return newDate;
else*/{
res = yearFrac( Date.prototype.getDateFromExcel( issue.getValue() ), Date.prototype.getDateFromExcel( settlement.getValue() ), basis.getValue() );
} }
var iss = Date.prototype.getDateFromExcel( issue ),
fInter = Date.prototype.getDateFromExcel( firstInterest ),
settl = Date.prototype.getDateFromExcel( settlement ),
numMonths = 12 / frequency, numMonthsNeg = - numMonths, endMonth = fInter.lastDayOfMonth(),
coupPCD, firstDate, startDate, endDate, res, days, coupDays;
if(settl > fInter && calcMethod){
coupPCD = new Date( fInter );
endDate = new Date( settl );
while ( !(numMonths > 0 ? coupPCD >= endDate : coupPCD <= endDate) ) {
endDate = coupPCD;
startDate = addMonth( coupPCD, numMonths, endMonth );
}
res *= par.getValue() * rate.getValue(); }
else{
coupPCD = addMonth( fInter, numMonthsNeg, endMonth );
}
firstDate = iss > coupPCD ? iss : coupPCD;
days = days360( firstDate, settl, basis );
coupDays = getcoupdays( coupPCD, fInter, frequency, basis ).getValue();
res = days / coupDays;
startDate = coupPCD;
endDate = iss;
while( !( numMonthsNeg > 0 ? startDate >= iss : startDate <= iss ) ) {
endDate = startDate;
startDate = addMonth( startDate, numMonthsNeg, endMonth );
firstDate = iss > startDate ? iss : startDate;
if( basis == DayCountBasis.UsPsa30_360 ){
days = 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();
}
res += (iss <= startDate) ? calcMethod : days / coupDays;
}
res *= par * rate / frequency;
return this.value = new cNumber( res ); return this.value = new cNumber( res );
}; };
cACCRINT.prototype.getInfo = function () { cACCRINT.prototype.getInfo = function () {
...@@ -4982,7 +5028,7 @@ cTBILLEQ.prototype.Calculate = function ( arg ) { ...@@ -4982,7 +5028,7 @@ cTBILLEQ.prototype.Calculate = function ( arg ) {
var date1 = d1.getUTCDate(), month1 = d1.getUTCMonth(), year1 = d1.getUTCFullYear(), var date1 = d1.getUTCDate(), month1 = d1.getUTCMonth(), year1 = d1.getUTCFullYear(),
date2 = d2.getUTCDate(), month2 = d2.getUTCMonth(), year2 = d2.getUTCFullYear(); date2 = d2.getUTCDate(), month2 = d2.getUTCMonth(), year2 = d2.getUTCFullYear();
var nDiff = GetDiffDate360( date1, month1, year1, d1.isLeapYear(), date2, month2, year2, true ); var nDiff = GetDiffDate360( date1, month1, year1, date2, month2, year2, true );
if ( settlement.getValue() >= maturity.getValue() || discount.getValue() <= 0 || nDiff > 360 ) if ( settlement.getValue() >= maturity.getValue() || discount.getValue() <= 0 || nDiff > 360 )
return this.value = new cError( cErrorType.not_numeric ); return this.value = new cError( cErrorType.not_numeric );
...@@ -5138,7 +5184,7 @@ cTBILLYIELD.prototype.Calculate = function ( arg ) { ...@@ -5138,7 +5184,7 @@ cTBILLYIELD.prototype.Calculate = function ( arg ) {
var date1 = d1.getUTCDate(), month1 = d1.getUTCMonth(), year1 = d1.getUTCFullYear(), var date1 = d1.getUTCDate(), month1 = d1.getUTCMonth(), year1 = d1.getUTCFullYear(),
date2 = d2.getUTCDate(), month2 = d2.getUTCMonth(), year2 = d2.getUTCFullYear(); date2 = d2.getUTCDate(), month2 = d2.getUTCMonth(), year2 = d2.getUTCFullYear();
var nDiff = GetDiffDate360( date1, month1, year1, d1.isLeapYear(), date2, month2, year2, true ); var nDiff = GetDiffDate360( date1, month1, year1, date2, month2, year2, true );
nDiff++; nDiff++;
if ( settlement.getValue() >= maturity.getValue() || pr.getValue() <= 0 || nDiff > 360 ) if ( settlement.getValue() >= maturity.getValue() || pr.getValue() <= 0 || nDiff > 360 )
return this.value = new cError( cErrorType.not_numeric ); return this.value = new cError( cErrorType.not_numeric );
......
...@@ -102,7 +102,14 @@ Date.prototype.addYears = function ( counts ) { ...@@ -102,7 +102,14 @@ Date.prototype.addYears = function ( counts ) {
}; };
Date.prototype.addMonths = function ( counts ) { Date.prototype.addMonths = function ( counts ) {
this.setUTCMonth( this.getUTCMonth() + counts ); if( this.lastDayOfMonth() ){
this.setUTCDate(1);
this.setUTCMonth( this.getUTCMonth() + counts );
this.setUTCDate(this.getDaysInMonth());
}
else{
this.setUTCMonth( this.getUTCMonth() + counts );
}
}; };
Date.prototype.addDays = function ( counts ) { Date.prototype.addDays = function ( counts ) {
...@@ -3875,7 +3882,7 @@ function matching( x, y, oper ) { ...@@ -3875,7 +3882,7 @@ function matching( x, y, oper ) {
return res; return res;
} }
function GetDiffDate360( nDay1, nMonth1, nYear1, bLeapYear1, nDay2, nMonth2, nYear2, bUSAMethod ) { function GetDiffDate360( nDay1, nMonth1, nYear1, nDay2, nMonth2, nYear2, bUSAMethod ) {
var nDayDiff; var nDayDiff;
var startTime = new Date( nYear1, nMonth1-1, nDay1 ), var startTime = new Date( nYear1, nMonth1-1, nDay1 ),
endTime = new Date( nYear2, nMonth2-1, nDay2 ), endTime = new Date( nYear2, nMonth2-1, nDay2 ),
...@@ -3902,7 +3909,7 @@ function GetDiffDate360( nDay1, nMonth1, nYear1, bLeapYear1, nDay2, nMonth2, nYe ...@@ -3902,7 +3909,7 @@ function GetDiffDate360( nDay1, nMonth1, nYear1, bLeapYear1, nDay2, nMonth2, nYe
} }
} }
} }
nDayDiff = ( nYear2 - nYear1 ) * 360 + ( nMonth2 - nMonth1 ) * 30 + ( nDay2 - nDay1 ); // nDayDiff = ( nYear2 - nYear1 ) * 360 + ( nMonth2 - nMonth1 ) * 30 + ( nDay2 - nDay1 );
} }
else { else {
if ( nDay1 == 31 ) { if ( nDay1 == 31 ) {
...@@ -3911,8 +3918,8 @@ function GetDiffDate360( nDay1, nMonth1, nYear1, bLeapYear1, nDay2, nMonth2, nYe ...@@ -3911,8 +3918,8 @@ function GetDiffDate360( nDay1, nMonth1, nYear1, bLeapYear1, nDay2, nMonth2, nYe
if ( nDay2 == 31 ) { if ( nDay2 == 31 ) {
nDay2--; nDay2--;
} }
nDayDiff = ( nYear2 - nYear1 ) * 360 + ( nMonth2 - nMonth1 ) * 30 + ( nDay2 - nDay1 );
} }
nDayDiff = ( nYear2 - nYear1 ) * 360 + ( nMonth2 - nMonth1 ) * 30 + ( nDay2 - nDay1 );
return nDayDiff; return nDayDiff;
} }
......
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