/*
    Класс абстрагирует платежный метод
    
    id                  - id
    currencyId          - объект Currency
    code                - код платежного метода
    isActive            - признак того, что платежный метод активен
    isInput             - признак того, что платежный метод доступен на ввод
    isOutput            - признак того, что платежный метод доступен на вывод
    whoPayPsCommission  - кто платит комиссию (payer/payee)
    precision           - точность
*/
function PaymentMethod(id, currencyId, currencySymbol, code, isActive, isInput, isOutput, whoPayPsCommission, precision){
    this.id = id;
    this.currencyId = currencyId;
    this.currencySymbol = currencySymbol;
    this.code = code;
    this.isActive = isActive;
    this.isInput = isInput;
    this.isOutput = isOutput;
    this.whoPayPsCommission = whoPayPsCommission;
    this.precision = precision;
    
    this.error = '';            
}

/*
    Возвращает признак - использует ли платежный метод группы стран   

    @return bool
*/

PaymentMethod.prototype.isUseCountryGroups = function(){
    isUseCountryGroups = false;
    var i = 0;
    $each(pmCommissionData[this.id], function(countryGroup, index){
        for(i = 0; i < countryGroup.length; i++){
            if(countryGroup[i].country_group_id > 0){
                isUseCountryGroups = true;                                                        
            }   
        }                    
    });
    return isUseCountryGroups;    
}

/*
    Округление десятичной части 
    
    @param decimal amount текст ошибки     
    @param array('in', 'out') direction направление    
    @return decimal
*/
PaymentMethod.prototype.calcPrecisionAmount = function(amount, direction){
    if(direction == null || direction == 'undefined'){
        direction = 'in';
    }
    switch(direction){
        case 'in':
            amount = amount.round(this.precision);
        break;
        case 'out':
            amount = amount.round(this.precision);
        break;            
    }
    return amount;
}

/*
    Установить текст ошибки
    
    @param string error текст ошибки
    @return void
*/
PaymentMethod.prototype.setError = function(error){     
    this.error = error;
    return;
};


/*
    Установить текст ошибки
    
    @param string error текст ошибки
    @return void
*/
PaymentMethod.prototype.setError = function(error){     
    this.error = error;
    return;
};

/*
    Вернуть текст ошибки
    
    @return string error текст ошибки
*/
PaymentMethod.prototype.getError = function(){     
    return this.error;
};

/*
    Получить объект комиссии
    
    @param int countriesGroupId id группы стран
    @return CurCommission commissionObj объект для расчет комиссиий
*/
PaymentMethod.prototype.makeCommissionCalcObject = function(pmCommissionData, countriesGroupId){
    if(pmCommissionData[this.id].length == 0){
        this.setError('error: no commissions for id=' + this.id);
        return -1;        
    }
    
    if(!countriesGroupId || countriesGroupId == 'undefined')
        countriesGroupId = 0;
    
    pmCommissions = pmCommissionData[this.id][countriesGroupId];
    if(pmCommissions == null){
        pmCommissions = pmCommissionData[this.id][0];        
    }
    
    if(pmCommissions == null || pmCommissions == 'undefined'){
        this.setError('Ошибка: комиссия для данного метода и данной группы стран отсутствует');
        return false;
    }
    
    commissionCalcArr = new Array();
    for(i = 0; i < pmCommissions.length ; i++){
        pmCommission = pmCommissions[i];
        if(pmCommission.period > 0 && pmCommission.period_fix > 0){
            commissionCalcArr.push(new periodComissionObj(this.code, pmCommission.amount_from, pmCommission.amount_to, pmCommission.perc, pmCommission.fix, pmCommission.min, pmCommission.max, pmCommission.period, pmCommission.period_fix));    
        } else{
            commissionCalcArr.push(new comissionObj(this.code, pmCommission.amount_from, pmCommission.amount_to, pmCommission.perc, pmCommission.fix, pmCommission.min, pmCommission.max, pmCommission.is_calc_comm_diff));    
        }
    }
    
    if(pmCommissions.length == 1){                            
        commissionCalcObj = commissionCalcArr[0];        
    } else{                                                            
        commissionCalcObj = new complexComissionObj(commissionCalcArr, this.whoPayPsCommission == 'payee' ? 0 : 1);                
    }   
    
    return commissionCalcObj;   
};

/*
    Расчет прямой комиссии
    
    @param decimal amountIn входная сумма
    @param int countriesGroupId id группы стран
    @return decimal commission комиссия
*/
PaymentMethod.prototype.calcCommissionIn2Out = function(amountIn, pmCommissionData, countriesGroupId){
    commissionCalcObject = this.makeCommissionCalcObject(pmCommissionData, countriesGroupId);    
    if(commissionCalcObject == false){
        return false;                
    }
    var amountOut = commissionCalcObject.afterComission(amountIn, false);
    if(amountOut == 0) return false;
    var commission = amountIn - amountOut;
    return commission;
};

/*
    Расчет обратной комиссии
    
    @param decimal amountOut выходная сумма
    @param int countriesGroupId id группы стран
    @return decimal commission комиссия
*/          
PaymentMethod.prototype.calcCommissionOut2In = function(amountOut, pmCommissionData, countriesGroupId){
    commissionCalcObject = this.makeCommissionCalcObject(pmCommissionData, countriesGroupId);    
    if(commissionCalcObject == false){
        return false;                
    }    
    var amountIn = commissionCalcObject.reverseComission(amountOut, false);
    if(amountIn == 0) return false;
    var commission = amountIn - amountOut;
    return commission;        
};
