Facebook
From Bistre Hornbill, 7 Years ago, written in JavaScript.
Embed
Download Paste or View Raw
Hits: 293
  1. //funkcja zawsze musi cos robic, zawierac w nazwie rzeczownik
  2. //z taka nazwa funkcji te wszystkie parametry poki co sa jedna czarna magia, nie wiadomo od czego ktory jest
  3. function PMT(rate, periods, present, future, type) {
  4.   // jezeli type bedzie null lub undefined to zwrocona zostanie druga wartosc, czyli 0
  5.   // type = type || 0
  6.   var type = (typeof type === 'undefined') ? 0 : type;
  7.  
  8.         //jakis hack jak skurwol ktorego nawet nie rozumiem :D eval sluzy do kompilacji i uruchamiania kodu JSowego at runtime
  9.   rate = eval(rate);
  10.  
  11.         //czo to za result, resultRate? finalRate?
  12.   var result;
  13.   if (rate === 0) {
  14.     result = (present + future) / periods;
  15.   } else {
  16.     var term = Math.pow(1 + rate, periods);
  17.     if (type === 1) {
  18.       result = (future * rate / (term - 1) + present * rate / (1 - 1 / term)) / (1 + rate);
  19.     } else {
  20.       result = future * rate / (term - 1) + present * rate / (1 - 1 / term);
  21.     }
  22.   }
  23.  
  24.   return -result;
  25. }
  26.  
  27. //to samo co wyzej
  28. function FV(rate, periods, payment, value, type) {
  29.  
  30.   var type = (typeof type === 'undefined') ? 0 : type;
  31.  
  32.   rate = eval(rate);
  33.  
  34.   var result;
  35.   if (rate === 0) {
  36.     result = value + payment * periods;
  37.   } else {
  38.     var term = Math.pow(1 + rate, periods);
  39.     if (type === 1) {
  40.       result = value * term + payment * (1 + rate) * (term - 1.0) / rate;
  41.     } else {
  42.       result = value * term + payment * (term - 1) / rate;
  43.     }
  44.   }
  45.  
  46.   return -result;
  47. }
  48.  
  49. //to samo co wyzej
  50. function IPMT(rate, period, periods, present, future, type) {
  51.   var type = (typeof type === 'undefined') ? 0 : type;
  52.  
  53.   rate = eval(rate);
  54.   periods = eval(periods);
  55.  
  56.   var payment = PMT(rate, periods, present, future, type);
  57.  
  58.   var interest;
  59.   if (period === 1) {
  60.     if (type === 1) {
  61.       interest = 0;
  62.     } else {
  63.       interest = -present;
  64.     }
  65.   } else {
  66.     if (type === 1) {
  67.       interest = FV(rate, period - 2, payment, present, 1) - payment;
  68.     } else {
  69.       interest = FV(rate, period - 1, payment, present, 0);
  70.     }
  71.   }
  72.  
  73.   return interest * rate;
  74. }
  75.  
  76. function ViewModel() {
  77.     var self = this;  
  78.     self.carAge = ko.observable("0");
  79.     self.loanPeriod = ko.observable("");
  80.     self.carPrice = ko.observable("");
  81.     self.selfPayment = ko.observable("");
  82.     self.insurance = ko.observable("");
  83.     self.otherNeeds = ko.observable("");
  84.     self.brokerage = ko.observable("");
  85.     self.groupInsurancePercent = ko.observable("");
  86.     self.commissionPercent = ko.observable("");
  87.     self.increaseInterest = ko.observable("");
  88.    
  89.     //to samo co nizej
  90.     self.creditAmount = ko.computed(function() {
  91.                 //nie wiadomo o co chodzi z tym 1
  92.         var creditAmount1 = (parseFloat(self.carPrice()) - parseFloat(self.selfPayment())) + parseFloat(self.insurance()) + parseFloat(self.otherNeeds()) + parseFloat(self.brokerage()) + 0;
  93.         //nie wiadomo o co chodzi z tym 2
  94.                                 var creditAmount2 = (100 - (parseFloat(self.commissionPercent()) + 0) - parseFloat(self.groupInsurancePercent()));
  95.                                 var creditAmount = ((creditAmount1 / creditAmount2) * 100).toFixed(2);
  96.      
  97.         if(!isNaN(creditAmount)) {
  98.                         return creditAmount;
  99.         }
  100.     });
  101.    
  102.     //to samo co nizej
  103.     self.groupInsurancePrice = ko.computed(function() {
  104.                 var total = 0;
  105.         total = ((parseFloat(self.creditAmount()) * parseFloat(self.groupInsurancePercent())) / 100).toFixed(2);
  106.        
  107.         if(!isNaN(total)) {
  108.                         return total;
  109.         }
  110.     });
  111.                
  112.     //to samo co nizej
  113.     self.commissionPrice = ko.computed(function() {
  114.                 var total = 0;
  115.         total = ((parseFloat(self.creditAmount()) * (parseFloat(self.commissionPercent()) + 0)) / 100).toFixed(2);
  116.        
  117.         if(!isNaN(total)) {
  118.                         return total;
  119.         }
  120.     });
  121.  
  122. //to samo co nizej
  123.     self.finalInterest = ko.computed(function () {
  124.         var total = 0;
  125.                 total = parseFloat(self.carAge()) + parseFloat(self.increaseInterest());
  126.        
  127.         if(!isNaN(total)) {
  128.                         return total;
  129.         }
  130.     });
  131.                 /*
  132.    
  133.     Jak ktos wpisal gnoj w formularzu to ta metoda powinna sie wyjebac, nie powinno byc takich sprawdzen tutaj
  134.     self.creditedCarPrice = ko.pureComputed(function() {
  135.         return parseFloat(self.carPrice()) - parseFloat(self.selfPayment());
  136.     });
  137.     */
  138.     self.creditedCarPrice = ko.computed(function() {
  139.                 var total = 0;
  140.         total = parseFloat(self.carPrice()) - parseFloat(self.selfPayment());
  141.        
  142.         if(!isNaN(total)) {
  143.                         return total;
  144.         }
  145.     });
  146.    
  147.     //ponizsce mogloby wygladac
  148.     /*
  149.     self.income = ko.pureComputed(function(){
  150.         return self.loanPeriod() <= minimumLoanPeriod ? 0 : calculateIncome(numberOfMonths, self.carAge(), self.loanPeriod(), self.creditedPrice())
  151.     });
  152.     */
  153.    
  154.     self.income = ko.computed(function() {
  155.                 var income = 0;
  156.        
  157.         if(self.loanPeriod() <= 24) { income = 0; }
  158.                                 else {
  159.                                         //if(loanPeriod != '' && baseInterest != '' && creditedCarPrice != '' && finalInterest != '') {
  160.                                                 //var resultIPMTbase = 0;
  161.                                                 //var resultIPMTfinal = 0;
  162.  
  163.                                                  //for (var i = 1; i <= 24; i++) {
  164.                                                   //resultIPMTbase += -IPMT((self.carAge() / 100)/12, i, self.loanPeriod(), self.creditedCarPrice(), 0, 0);
  165.                                                 //}
  166.            
  167.             //generuje tablice z 24 elementami [null, null, null, null, ...], potem kazdy z elementow tablicy mapuje, przerabia na inny obiekt, w tym przypadku przerabia go na liczbe ktora jest jego indeksem w tablicy (tablica wyglada teraz: [0, 1, 2, 3, ..., 23]), potem reduce - funkcja agregujaca, laczy wszystkie wartosci tablicy w jedna wartosc, funkcja wewnatrz reduce przyjmuje aktualna agregowana wartosc, oraz i - aktualn element tablicym to 0 jako drugi parametr funkcji reduce to wartosc poczatkowa calego agregatu (odpowiednik resultIMPTbase=0)
  168.            
  169.             //warto wyniesc cala to linijke do odrebnej funkcji
  170.             var resultIPMTbase = array.apply(null, {length: 24}).map(function(_, index){return index+1;}).reduce(function(current, i){
  171.             return current - IPMT((self.carAge() / 100)/12, i, self.loanPeriod(), self.creditedCarPrice(), 0, 0);
  172.             }, 0).toFixed(2);
  173.            
  174.                                                 //resultIPMTbase = resultIPMTbase.toFixed(2);
  175.             //alert(resultIPMTbase);
  176.  
  177.                                                 //warto wyniesc cala to linijke do odrebnej funkcji
  178.                                                 var resultIPMTfinal = array.apply(null, {length: 24}).map(function(_, index){return index+1;}).reduce(function(current, i){
  179.             return current - IPMT((self.finalInterest() / 100)/12, i, self.loanPeriod(), self.creditedCarPrice(), 0, 0);
  180.             }).toFixed(2);
  181.  
  182.                                                 //for (var i = 1; i <= 24; i++) {
  183.                                                 //      resultIPMTfinal += -IPMT((self.finalInterest() / 100)/12, i, self.loanPeriod(), self.creditedCarPrice(), 0, 0);
  184.                                                 //}
  185.                                                 //resultIPMTfinal = resultIPMTfinal.toFixed(2);
  186.  
  187.                                                 income = resultIPMTfinal - resultIPMTbase;
  188.                                                 //income = income.toFixed(2);
  189.                                         //}
  190.           // w inpucie gdzie jest teraz wartosc 2.64158.. powinno byc tak naprawde 8153.34
  191.           // aktualna wersja tego kalkulatora opiera sie o jQuery, tam liczy poprawnie, ale w porownaniu do tego KO to niebo a ziemia, wiec wolalbym zeby sie to udalo przepisac, ale na tych funkcjach sie zatrzymalem. Ogolnie nazwa tej funkcji IPMT jest mocno zaciemniona, kompletnie nie wiadoo co to ma robic, nie wiadomo tez co oznacza carPrice() / 100 / 12, latwiej byloby gdyby bylo napisane np. carPrice() / pricePerMonth / numberOfMonths, co oznacza to 24 wewnatrz for'a, dlaczego akurat od jedynki, latwiej byloby np for (var i = startMonth, i<=lastMongth, ....) to juz takie kwestie ogolnie z czystoscia kodu, nie ko samym sobie, 24 pierwsze miesiace, te funckej sa znalezione na necie, bo nikt w banku nie potrafil mi wyjasnic krok po kroku jak sie to liczy, oni tylko wyslali jakiegos podupconego Excela z ktorego tak sie nic nie dowiesz, myslalem ze po prostu ten for w KO nie dziala, bo chujowu wynik pokazuje, carPrice() to tak na prawde oprocentowanie poczatkowe, a finalInterst kocowe, po ewentualnej zwyżce, kierwa zle wpisana zmienna...
  192.                                 }
  193.        
  194.         if(!isNaN(income)) {
  195.                         return income;
  196.         }
  197.     });
  198. }
  199.  
  200. ko.applyBindings(new ViewModel());