Advertisement
mr_therabbit

Подбор вклада

Dec 1st, 2014
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(function(){
  2.    
  3.     var deposits = new Backbone.Collection;
  4.     var depositsFind = new Backbone.Collection;
  5.  
  6.     deposits.add(arrPrograms);
  7.  
  8.     var objFilter = Backbone.Model.extend({
  9.         defaults: function() {
  10.             return {
  11.                 'sum':  0,
  12.                 'currency': '',
  13.                 'period': 0,
  14.                 'period_day': 0,
  15.                 'periodInterest': 0,
  16.                 'sumExt': 0,
  17.                 'isAddSum': false,
  18.                 'isPartialDelivery': false,
  19.                 'isMulti': false,
  20.                 'isRetirees': false,
  21.                 'isLifeInsurance': false
  22.             };
  23.         },
  24.         validate: function(attrs) {
  25.             var regExpNum = /^[0-9]+$/;
  26.  
  27.             var arrError = [];
  28.             if (!this.has('sum') || !attrs.sum.length)
  29.                 arrError.push({ field: '#f_sum', error: 'Необходимо указать сумму вклада'});
  30.             else if (this.has('sum') && attrs.sum.length > 0 && !regExpNum.test(attrs.sum))
  31.                 arrError.push({ field: '#f_sum', error: 'В сумме вклада можно указывать только цифры'});
  32.                
  33.             if (!(attrs.currency == 'rub' || attrs.currency == 'usd'))
  34.                 arrError.push({ field: '#f_currency', error: 'Необходимо указать валюту вклада'});
  35.                
  36.             if (!this.has('period') || !regExpNum.test(attrs.period))
  37.                 arrError.push({ field: '#f_period', error: 'Необходимо указать срок вклада'});
  38.            
  39.             if (this.has('period') && attrs.period == 99 && (!this.has('period_day') || !regExpNum.test(attrs.period_day)))
  40.                 arrError.push({ field: '#f_period_day', error: 'Необходимо указать срок вклада в днях'});
  41.                
  42.             if (!this.has('periodInterest') || attrs.periodInterest == 0 || !regExpNum.test(attrs.periodInterest))
  43.                 arrError.push({ field: '#f_period_interest', error: 'Необходимо указать выплату процентов'});
  44.  
  45.                
  46.             if (attrs.isAddSum == true && this.has('sumExt') && attrs.sumExt.length > 0 && !regExpNum.test(attrs.sumExt))
  47.                 arrError.push({ field: '#f_sum_ext', error: 'В сумме дополнительных взносов можно указывать только цифры'});
  48.            
  49.             if (arrError.length > 0) return arrError;
  50.         },
  51.         clear: function() {
  52.             this.set(this.defaults(), {silent: true});
  53.         }
  54.        
  55.     });
  56.  
  57.     var TableView = Backbone.View.extend({
  58.         el: $('#table tbody'),
  59.         initialize: function() {
  60.             this.$el.empty();
  61.         },
  62.         calc: function(dep){
  63.             var objResult = {};
  64.            
  65.             var strCurr = App.model.get('currency');
  66.             objResult.currency = strCurr;
  67.            
  68.             var intMonth = 0;
  69.  
  70.             var intCountMonth = parseInt(App.model.get('period'));
  71.             var intOstatokDay = 0;
  72.            
  73.             if (intCountMonth == 999) {
  74.                 intCountMonth = parseInt(App.model.get('period_day'));
  75.             }//\\ if
  76.  
  77.             _.each(dep.get('month'), function(numM, key){
  78.                 if (intCountMonth >= numM) intMonth = key;
  79.             });
  80.  
  81.             var intRange = 0;
  82.             _.each(dep.get('programm')[strCurr].range, function(numSum, key){
  83.                 if (parseInt(App.model.get('sum')) >= numSum) intRange = key;
  84.             });
  85.             objResult.precent = dep.get('programm')[strCurr].percent[intRange][intMonth];
  86.  
  87.             var intSlider = 0;
  88.             var intBeginSumM1 = intBeginSumM2 = 0;
  89.             if (dep.get('xid') == 'biznes_klass'){
  90.                 _.each(dep.get('programm')[strCurr].slider, function(numSum, key){
  91.                     if (parseInt(App.model.get('sum')) >= numSum) {
  92.                         intSlider = key;
  93.                         intBeginSumM1 = objResult.minSum = numSum;
  94.                         intBeginSumM2 = parseInt(App.model.get('sum')) - intBeginSumM1;
  95.                     }
  96.                 });
  97.                 objResult.precentExt = dep.get('programm')[strCurr].percent_ext[intSlider][intMonth];
  98.             }//\\ if
  99.            
  100.  
  101.             var intBeginSum = parseFloat(App.model.get('sum'));
  102.             var intAddMoney = parseFloat(App.model.get('sumExt'));
  103.             var intCountDayInMount = 1;
  104.             objResult.income = 0;
  105.             var intAddProcentFinal = 0;
  106.            
  107.             for (var intMonth = 1; intMonth <= intCountMonth; intMonth++) {
  108.                 arrT = {};
  109.                 arrT.begin_summ = intBeginSum;
  110.                 arrT.add_money = 0;
  111.                 arrT.add_procent = 0;
  112.                 if ((intMonth / 30) > 1 && (intMonth % 30 == 0) && intAddMoney > 0) {
  113.                     arrT.add_money = intAddMoney;
  114.                 }
  115.        
  116.        
  117.                 if (dep.get('xid') != 'biznes_klass'){
  118.                     arrT.add_procent = (intBeginSum / 100) * (objResult.precent / 365);
  119.                 } else {
  120.                     arrT.add_procent = (intBeginSumM1 / 100) * (objResult.precent / 365);
  121.                     arrT.add_procent += ((intBeginSumM2 + arrT.add_money) / 100) * (objResult.precentExt / 365);
  122.                 }//\\ if
  123.        
  124.                 if (strCurr != 'rub' && objResult.precent > 9){
  125.                     // Получим процент при 9%
  126.                     var intTT = ((arrT.begin_summ + arrT.add_money) / 100) * (9 / 365);
  127.                     intTT = arrT.add_procent - intTT;
  128.                     if (intTT > 0) arrT.add_procent = arrT.add_procent - (intTT / 100 * 35);
  129.                 }//\\ if
  130.        
  131.                 objResult.income += arrT.add_procent;
  132.        
  133.                 if (dep.get('capitalization') == true) {
  134.                     intAddProcentFinal = intAddProcentFinal  + arrT.add_money + arrT.add_procent;
  135.                     if (intMonth % (dep.get('capitalization_count_month')*30) == 0 || intMonth == intCountMonth) {
  136.                         intBeginSum = intBeginSum + intAddProcentFinal;
  137.                         intAddProcentFinal = 0;
  138.                     }//\\ if
  139.                 } else intBeginSum = intBeginSum + arrT.add_money;
  140.        
  141.                 if (intMonth == intCountMonth && dep.get('capitalization') == false) intBeginSum += objResult.income;
  142.             }//\\ for
  143.            
  144.             objResult.income = objResult.income.toFixed(2);
  145.            
  146.             return objResult;
  147.         },
  148.         render: function() {
  149.             var html_table = '';
  150.             var that = this;
  151.             depositsFind.forEach(function(dep){
  152.                 var resultCalc = that.calc(dep);
  153.                 var strPaymentPercent = 'N/A';
  154.                 if (dep.get('payment_percent_end_of_month')) strPaymentPercent = 'В конце месяца';
  155.                 if (dep.get('payment_percent_end_of_deposit')) strPaymentPercent = 'В конце срока';
  156.                 if (!dep.get('payment_percent_end_of_month') && !dep.get('payment_percent_end_of_deposit') && dep.get('capitalization')) {
  157.                     if (dep.get('capitalization_count_month') == 1)
  158.                         strPaymentPercent = 'Капитализация каждые '+dep.get('capitalization_count_month')+' месяц';
  159.                     else if (dep.get('capitalization_count_month') > 1 && dep.get('capitalization_count_month') < 5)
  160.                         strPaymentPercent = 'Капитализация каждые '+dep.get('capitalization_count_month')+' месяца';
  161.                     else if (dep.get('capitalization_count_month') >= 5)
  162.                         strPaymentPercent = 'Капитализация каждые '+dep.get('capitalization_count_month')+' месяцев';
  163.                 }//\\ if
  164.                
  165.                 var strCurr = strTitle = strPrecent = '';
  166.                 if (resultCalc.currency == 'rub') strCurr = 'руб.';
  167.                 else if (resultCalc.currency == 'usd') strCurr = 'USD';
  168.                
  169.                 if (dep.get('xid') == 'biznes_klass') {
  170.                     strTitle = '<a href="'+dep.get('link')+'">"'+dep.get('title')+'"</a> Минимальная сумма вклада '+accounting.formatNumber(resultCalc.minSum, 0, ' ')+' '+strCurr+'<br />Более подробный расчет можно произвести в <a href="http://sobinbank.ru/ru/quotient_client/contributions/calculator/">калькуляторе</a>.';
  171.                     strPrecent = resultCalc.precent+'% на мин. сумму вклада; '+resultCalc.precentExt+'% на сумму сверх мин. суммы вклада';
  172.                 } else {
  173.                     strTitle = '<a href="'+dep.get('link')+'">"'+dep.get('title')+'"</a>';
  174.                     strPrecent = resultCalc.precent+'%';
  175.                 }
  176.                
  177.                 html_table += '<tr>';
  178.                 html_table += '<td>'+strTitle+'</td>';
  179.                 html_table += '<td>'+strPrecent+'</td>';
  180.                 html_table += '<td>'+accounting.formatNumber(resultCalc.income, 2, ' ')+' '+strCurr+'</td>';
  181.                 html_table += '<td>'+strPaymentPercent+'</td>';
  182.                 html_table += '</tr>';
  183.             });
  184.             if (html_table.length == 0) html_table += '<tr><td colspan="4">Не найдено</td></tr>';
  185.             this.$el.html(html_table);
  186.             return this;
  187.         }
  188.     });
  189.  
  190.  
  191.     var AppView = Backbone.View.extend({
  192.         model: new objFilter,
  193.         el: $('.csFilterDeposit'),
  194.         events: {
  195.             'click .csSearchDeposit': 'onSearch',
  196.             'click .csClearForm': 'onClear',
  197.             'change #f_opt_add_sum': 'onAddSum',
  198.             'change #f_period': 'onShowPeriodDay'
  199.         },
  200.         resultTable: new TableView(),
  201.         initialize: function() {
  202.             this.model.on('error', function(m, error){
  203.                 $.each(error, function(index, er){
  204.                     App.showError(er.field, er.error);
  205.                 })
  206.             });
  207.             this.clearError();
  208.             this.onShowPeriodDay();
  209.             this.onAddSum();
  210.         },
  211.         render: function() {
  212.            
  213.             this.resultTable.render();
  214.            
  215.             return this;
  216.         },
  217.         onSearch: function() {
  218.             this.clearError();
  219.             this.model.clear();
  220.             this.model.set({
  221.                 'sum': this.$('#f_sum').val(),
  222.                 'currency': this.$('#f_currency').val(),
  223.                 'period': parseInt(this.$('#f_period').val()),
  224.                 'period_day': parseInt(this.$('#f_period_day').val()),
  225.                 'periodInterest': parseInt(this.$('#f_period_interest').val()),
  226.                 'sumExt': this.$('#f_sum_ext').val(),
  227.                 'isAddSum': this.$('#f_opt_add_sum').is(':checked'),
  228.                 'isPartialDelivery': this.$('#f_opt_partial_delivery').is(':checked'),
  229.                 'isMulti': this.$('#f_opt_multicurrency').is(':checked'),
  230.                 'isRetirees': this.$('#f_opt_for_retirees').is(':checked'),
  231.                 'isLifeInsurance': this.$('#f_opt_for_life_insurance').is(':checked')
  232.             });
  233.             if (this.model.isValid()) {
  234.                 depositsFind.reset();
  235.                 deposits.forEach(function(dep){
  236.                     var isFind = false;
  237.                    
  238.                     var indexCur = _.indexOf(dep.get('currency'), App.model.get('currency'));
  239.                     var intMinSum = 0;
  240.                     if (dep.get('min_sum') != null)
  241.                         intMinSum = dep.get('min_sum')[indexCur];
  242.                     var intCountMonth = parseInt(App.model.get('period'));
  243.                    
  244.                     if (intCountMonth == 999) {
  245.                         intCountMonth = App.model.get('period_day');
  246.                     }//\\ if                   
  247.                    
  248.                     if (parseInt(App.model.get('sum')) >= intMinSum) {
  249.                         if (
  250.                             (dep.get('case_only_month') == true && (_.indexOf(dep.get('month'), intCountMonth) != -1 || (intCountMonth == 272 && _.indexOf(dep.get('month'), 271) != -1) || (intCountMonth == 183 && _.indexOf(dep.get('month'), 182) != -1))) ||
  251.                             (dep.get('case_only_month') == false && dep.get('prolongation') == true && intCountMonth >= _.min(dep.get('month'))) ||
  252.                             (dep.get('case_only_month') == false && dep.get('prolongation') == false && intCountMonth >= _.min(dep.get('month')) && intCountMonth <= _.max(dep.get('month')))
  253.                         ) {
  254.                             if (
  255.                                 (App.model.get('periodInterest') == 1 && dep.get('payment_percent_end_of_month') == true) ||
  256.                                 (App.model.get('periodInterest') == 2 && dep.get('payment_percent_end_of_deposit') == true) ||
  257.                                 (App.model.get('periodInterest') == 3 && dep.get('capitalization') == true) ||
  258.                                 (App.model.get('periodInterest') == 4)
  259.                             ) {
  260.                                 if ((App.model.get('sumExt') == '' || parseInt(App.model.get('sumExt')) == 0) || (
  261.                                     parseInt(App.model.get('sumExt')) >= dep.get('min_add_cont')[indexCur]
  262.                                 )) {
  263.                                     isFind = true;
  264.                                     if (App.model.get('isAddSum') == true && !(dep.get('min_add_cont')[indexCur] > 0)) isFind = false;
  265.                                     if (App.model.get('isPartialDelivery') == true && dep.get('partial_delivery') == false) isFind = false;
  266.                                     if (App.model.get('isMulti') == true && dep.get('xid') != 'multi') isFind = false;
  267.                                     if (App.model.get('isRetirees') == true && (dep.get('xid') != 'pensionary' && dep.get('xid') != 'all2')) isFind = false;
  268.                                     if (App.model.get('isLifeInsurance') == true && (dep.get('xid') != 'so_strahovkoi1' && dep.get('xid') != 'so_strahovkoi2' && dep.get('xid') != 'so_strahovkoi3')) isFind = false;
  269.                                    
  270.                                 }//\\ if
  271.                             }//\\ if
  272.                         }//\\ if
  273.                     }//\\ if
  274.                     if (isFind) depositsFind.add(dep);
  275.                 });
  276.             } else {
  277.                 depositsFind.reset();
  278.                 this.model.clear();
  279.             }
  280.             this.render();
  281.         },
  282.         onClear: function() {
  283.             this.model.clear();
  284.             depositsFind.reset();
  285.             this.clearError();
  286.             this.$('#f_sum, #f_currency, #f_period, #f_period_interest, #f_sum_ext').val('');
  287.             this.$('#f_opt_add_sum, #f_opt_partial_delivery, #f_opt_multicurrency, #f_opt_for_retirees').removeAttr('checked');
  288.             this.onAddSum();
  289.             this.onShowPeriodDay();
  290.             this.render();
  291.         },
  292.         onAddSum: function() {
  293.             if (this.$('#f_opt_add_sum').is(':checked')) this.$('#tr_add_sum').show();
  294.             else {
  295.                 this.$('#f_sum_ext').val('');
  296.                 this.$('#tr_add_sum').hide();
  297.             }//\\ if
  298.         },
  299.         onShowPeriodDay: function() {
  300.             if (this.$('#f_period').val() == 999) this.$('#tr_period_day').show();
  301.             else {
  302.                 this.$('#f_period_day').val('');
  303.                 this.$('#tr_period_day').hide();
  304.             }//\\ if
  305.         },
  306.         showError: function(strEl, strTextError) {
  307.             this.$(strEl).next().show().html(strTextError);
  308.         },
  309.         clearError: function() {
  310.             $('.error_form').empty().hide();
  311.         }
  312.     });
  313.     var App = new AppView();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement