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