var payolutionXDate=function(g,m,A,p){function f(){var a=this instanceof f?this:new f,c=arguments,b=c.length,d;typeof c[b-1]=="boolean"&&(d=c[--b],c=q(c,0,b));if(b)if(b==1)if(b=c[0],b instanceof g||typeof b=="number")a[0]=new g(+b);else if(b instanceof f){var c=a,h=new g(+b[0]);if(l(b))h.toString=w;c[0]=h}else{if(typeof b=="string"){a[0]=new g(0);a:{for(var c=b,b=d||!1,h=f.parsers,r=0,e;r=b;h--)d/=G[h],d+=j(c,!1,h)-j(a,!1,h);b==1&&(d+=(c.getFullYear()-a.getFullYear())*12)}else b==2?(b=a.toDate().setUTCHours(0,0,0,0),d=c.toDate().setUTCHours(0,0,0,0),d=m.round((d-b)/864E5)+(c-d-(a-b))/864E5):d=(c-a)/[36E5,6E4,1E3,1][b-3];return d}function t(a){var c=a(0),b=a(1),a=a(2),b=new g(n(c,b,a)),d=u(c),a=d;b=c&&(a=c));return m.floor(m.round((b-a)/864E5)/7)+1}function u(a){a=new g(n(a,0,4));a.setUTCDate(a.getUTCDate()-(a.getUTCDay()+6)%7);return a}function I(a,c,b,d){var h=k(j,a,d),e=k(D,a,d),b=u(b===p?h(0):b);d||(b=s(b));a.setTime(+b);e(2,[h(2)+(c-1)*7])}function J(a,c,b,d,e){var r=f.locales,g=r[f.defaultLocale]||{},i=k(j,a,e),b=(typeof b=="string"?r[b]:b)||{};return x(a,c,function(a){if(d)for(var b=(a==7?2:a)-1;b>=0;b--)d.push(i(b));return i(a)},function(a){return b[a]||g[a]},e)}function x(a,c,b,d,e){for(var f,g,i="";f=c.match(M);){i+=c.substr(0,f.index);if(f[1]){g=i;for(var i=a,j=f[1],l=b,m=d,n=e,k=j.length,o=void 0,q="";k>0;)o=N(i,j.substr(0,k),l,m,n),o!==p?(q+=o,j=j.substr(k),k=j.length):k--;i=g+(q+j)}else f[3]?(g=x(a,f[4],b,d,e),parseInt(g.replace(/\D/g,""),10)&&(i+=g)):i+=f[7]||"'";c=c.substr(f.index+f[0].length)}return i+c}function N(a,c,b,d,e){var g=f.formatters[c];if(typeof g=="string")return x(a,g,b,d,e);else if(typeof g=="function")return g(a,e||!1,d);switch(c){case"fff":return i(b(6),3);case"s":return b(5);case"ss":return i(b(5));case"m":return b(4);case"mm":return i(b(4));case"h":return b(3)%12||12;case"hh":return i(b(3)%12||12);case"H":return b(3);case"HH":return i(b(3));case"d":return b(2);case"dd":return i(b(2));case"ddd":return d("dayNamesShort")[b(7)]||"";case"dddd":return d("dayNames")[b(7)]||"";case"M":return b(1)+1;case"MM":return i(b(1)+1);case"MMM":return d("monthNamesShort")[b(1)]||"";case"MMMM":return d("monthNames")[b(1)]||"";case"yy":return(b(0)+"").substring(2);case"yyyy":return b(0);case"t":return v(b,d).substr(0,1).toLowerCase();case"tt":return v(b,d).toLowerCase();case"T":return v(b,d).substr(0,1);case"TT":return v(b,d);case"z":case"zz":case"zzz":return e?c="Z":(d=a.getTimezoneOffset(),a=d<0?"+":"-",b=m.floor(m.abs(d)/60),d=m.abs(d)%60,e=b,c=="zz"?e=i(b):c=="zzz"&&(e=i(b)+":"+i(d)),c=a+e),c;case"w":return t(b);case"ww":return i(t(b));case"S":return c=b(2),c>10&&c<20?"th":["st","nd","rd"][c%10-1]||"th"}}function v(a,c){return a(3)<12?c("amDesignator"):c("pmDesignator")}function y(a){return!isNaN(+a[0])}function j(a,c,b){return a["get"+(c?"UTC":"")+o[b]]()}function D(a,c,b,d){a["set"+(c?"UTC":"")+o[b]].apply(a,d)}function s(a){return new g(a.getUTCFullYear(),a.getUTCMonth(),a.getUTCDate(),a.getUTCHours(),a.getUTCMinutes(),a.getUTCSeconds(),a.getUTCMilliseconds())}function E(a,c){return 32-(new g(n(a,c,32))).getUTCDate()}function z(a){return function(){return a.apply(p,[this].concat(q(arguments)))}}function k(a){var c=q(arguments,1);return function(){return a.apply(p,c.concat(q(arguments)))}}function q(a,c,b){return A.prototype.slice.call(a,c||0,b===p?a.length:b)}function K(a,c){for(var b=0;bthis.defaultDueDayOfMonth){this.firstDueDate.addMonths(1,true);} var monthDays=payolutionXDate.getDaysInMonth(this.firstDueDate.getFullYear(),this.firstDueDate.getMonth());this.firstDueDate.setDate(Math.min(this.defaultDueDayOfMonth,monthDays));} InstallmentCalculator.prototype.setDefaultDueDayOfMonth=function(){var day=this.firstDueDate.getDate();if(day>5&&day<=20){this.defaultDueDayOfMonth=20;}else{this.defaultDueDayOfMonth=5;}} InstallmentCalculator.prototype.getNextDue=function(monthOffset){if(monthOffset==0){return this.firstDueDate.clone().toDate();} var nextDueDate=this.firstDueDate.clone().addMonths(monthOffset,true);return nextDueDate.toDate();} InstallmentCalculator.prototype.calculateBinarySearch=function(lowerBound,upperBound,result,resultScale,getErrorFunction){var error=getErrorFunction(result,this);for(var i=0;i<1000&&((Math.abs(error)-this.accuracy)>0);i++){if(error>0){upperBound=result;}else{lowerBound=result;} result=upperBound-((upperBound-lowerBound)*0.5);error=getErrorFunction(result,this);} return+result.toFixed(resultScale);} InstallmentCalculator.prototype.getInstallmentAmountError=function(result,calculator){var current=calculator.originalAmount;for(var i=0;i0){return installmentAmount;} var total=installmentAmount*this.duration;var roundingDiff=total-this.originalAmount;return installmentAmount-roundingDiff;} InstallmentCalculator.prototype.calculateEffectiveInterest=function(){if(this.interestRate<=0){this.effectiveInterest=0;return;} var effectiveInterest=this.calculateBinarySearch(this.lowerBound,this.upperBound,this.interestRate*0.01,4,this.getEffectiveInterestError);this.effectiveInterest=effectiveInterest*100;this.effectiveInterest=+this.effectiveInterest.toFixed(2);} InstallmentCalculator.prototype.calculateFinalAmount=function(){this.finalAmount=0;for(var i=0;iMAXIMUM_AMOUNT){throw"Amount is too high. It has to be lower than "+MAXIMUM_AMOUNT;} if(!contains(AVAILABLE_INTEREST_RATES,interestRate)){throw"Unsupported interestRate "+interestRate+". Supported interest rates are: "+AVAILABLE_INTEREST_RATES;} if(!contains(PRICE_MATRIX[interestRate],duration)){throw"Unsupported duration "+duration+" supported durations are: "+PRICE_MATRIX[interestRate];} if(isNullOrUndefined(fulfillmentStart)){throw"FulfillmentStart required";} if(isNullOrUndefined(fulfillmentEnd)){throw"FulfillmentEnd required";} return new InstallmentCalculator(amount,duration,interestRate,currency,new payolutionXDate(getReferenceDay(fulfillmentStart,fulfillmentEnd))).calculate();} global.Payolution={getMinimumAmount:function(){return MINIMUM_AMOUNT;},getMaximumAmount:function(){return MAXIMUM_AMOUNT;},getAvailableDurations:function(){return AVAILABLE_DURATIONS;},getAvailableInterestRates:function(){return AVAILABLE_INTEREST_RATES;},getPriceMatrix:function(){return PRICE_MATRIX;},getAvailableDurationsForInterestRate:function(interestRate){interestRate=Number(interestRate);if(!contains(AVAILABLE_INTEREST_RATES,interestRate)){return[];} return PRICE_MATRIX[interestRate];},getDefaultInterestRate:function(interestRate){if(INTEREST_STRATEGY=="DURATION_BASED"){return null;} return DEFAULT_INTEREST_RATE;},calculateInstallment:function(amount,duration){return calculate(amount,duration,getDefaultInterestRate(duration),null,DEFAULT_FULFILLMENT_DATE,DEFAULT_FULFILLMENT_DATE);},calculateInstallmentWithReferenceDay:function(amount,duration,fulfillmentDate){return calculate(amount,duration,getDefaultInterestRate(duration),null,fulfillmentDate,fulfillmentDate);},calculateInstallmentWithFulfillmentInfo:function(amount,duration,fulfillmentStart,fulfillmentEnd){return calculate(amount,duration,getDefaultInterestRate(duration),null,fulfillmentStart,fulfillmentEnd);},calculateInstallmentForInterestRate:function(amount,duration,interestRate){return calculate(amount,duration,interestRate,null,DEFAULT_FULFILLMENT_DATE,DEFAULT_FULFILLMENT_DATE);},calculateInstallmentForInterestRateWithReferenceDay:function(amount,duration,interestRate,fulfillmentDate){return calculate(amount,duration,interestRate,null,fulfillmentDate,fulfillmentDate);},calculateInstallmentForInterestRateWithFulfillmentInfo:function(amount,duration,interestRate,fulfillmentStart,fulfillmentEnd){return calculate(amount,duration,interestRate,null,fulfillmentStart,fulfillmentEnd);},calculateInstallmentFixedCurrency:function(amount,duration,currency){return calculate(amount,duration,getDefaultInterestRate(duration),currency,DEFAULT_FULFILLMENT_DATE,DEFAULT_FULFILLMENT_DATE);},calculateInstallmentFixedCurrencyWithReferenceDay:function(amount,duration,currency,fulfillmentDate){return calculate(amount,duration,getDefaultInterestRate(duration),currency,fulfillmentDate,fulfillmentDate);},calculateInstallmentFixedCurrencyWithFulfillmentInfo:function(amount,duration,currency,fulfillmentStart,fulfillmentEnd){return calculate(amount,duration,getDefaultInterestRate(duration),currency,fulfillmentStart,fulfillmentEnd);},calculateInstallmentFixedCurrencyWithReferenceDayAndInterestRate:function(amount,duration,interestRate,currency,fulfillmentDate){return calculate(amount,duration,interestRate,currency,fulfillmentDate,fulfillmentDate);},calculateInstallmentFixedCurrencyWithFulfillmentInfoAndInterestRate:function(amount,duration,interestRate,currency,fulfillmentStart,fulfillmentEnd){return calculate(amount,duration,interestRate,currency,fulfillmentStart,fulfillmentEnd);}}})(this);