I am Computer Engineer, Write articles about python.

Full width home advertisement

Learning by Doing

Learn Python


Post Page Advertisement [Top]

 $(document).ready(function(){

    var stockCalculatorModal = $("#stock-calculator-modal");
    var stockCalculatorResultModal = $("#stock-calculator-result-modal");
    var btnCalculator = $("input[name='stockCalculator']", stockCalculatorResultModal);
    var btnCalculate = $("input[name='calculate']", stockCalculatorModal);
    var radioType = $("input[name='transaction']", stockCalculatorModal);
    var saleElements = $(".sale-element", stockCalculatorModal);

    var buyResult = $(".buy-result", stockCalculatorResultModal);
    var sellResult = $(".sale-result", stockCalculatorResultModal);

    var buyRadio = $("#buy", stockCalculatorModal);
    var sellRadio = $("#sale", stockCalculatorModal);

    var inputBuyRate = $("#buying-rate", stockCalculatorModal);
    var inputSellRate = $("#selling-rate", stockCalculatorModal);
    var inputQty = $("#qty", stockCalculatorModal);
    var inputCert = $("#certificates", stockCalculatorModal);
    var inputIpo = $("#ipo", stockCalculatorModal);

    var profitLossResult = $("#profit-loss-result", stockCalculatorResultModal);
    var profitLossInd = $("#profit-loss", stockCalculatorResultModal);

    var buyRate;
    var sellRate;
    var qty;
    var cert;

    var total_cost_price;
    var sebo_buy_commission;
    var broker_buy_commission;
    var name_transfer_buy_cost;
    var net_cost_price;

    var total_selling_price;
    var sebo_sell_commission;
    var broker_sell_commission;
    var name_transfer_sell_cost;
    var net_selling_price;
    var capital_gain;
    var ipo;

    var profit_Loss;

    var sebo_percent = 0.00015
    var name_tranfer_unit_cost = 25;
    var capital_gain_percent = 0.05;

    btnCalculate.click(function(e){
        if(validateCalculator()){
            e.preventDefault();
            setCalculatorValue();
            calculateShare();
            insertToModal();
            adjustMode();
            stockCalculatorModal.modal('hide');
            stockCalculatorResultModal.modal('show');
        }
    });

    btnCalculator.click(function(){
        stockCalculatorResultModal.modal('hide');
        stockCalculatorModal.modal('show');
    });

    radioType.change(function(){
        if(buyRadio.prop('checked')){
            saleElements.fadeOut();
        }
        else{
            saleElements.fadeIn();
        }
    });

    function validateCalculator(){
        var buyRate = inputBuyRate.val();
        if(isEmpty(buyRate) || !isDeciNumber(buyRate)){
            return false;
        }
        if(sellRadio.prop('checked')){
            var sellRate = inputSellRate.val();
            if(isEmpty(sellRate) || !isDeciNumber(sellRate)){
                return false;
            }
        }

        var qty = inputQty.val();
        if(isEmpty(qty) || !isNumeric(qty)){
            return false;
        }

        /*var cert = inputCert.val();
        if(isEmpty(cert) || !isNumeric(cert)){
            return false;
        }*/
        return true;
    }

    function setCalculatorValue(){
        buyRate = inputBuyRate.val();
        sellRate = inputSellRate.val();
        qty = inputQty.val();
        cert = 1; //inputCert.val();
        if(inputIpo.prop('checked')){
            ipo = true;
        }
        else{
            ipo = false;
        }
    }

    function calculateShare(){
        total_cost_price = qty * buyRate;
        if(!ipo){
            sebo_buy_commission = Math.round(total_cost_price * sebo_percent * 100)/100;
            broker_buy_commission = Math.round(total_cost_price * get_broker_percent(total_cost_price)*100)/100;
            name_transfer_buy_cost = Math.round(cert * name_tranfer_unit_cost*100)/100;
        }
        else{
            sebo_buy_commission = 0;
            broker_buy_commission = 0;
            name_transfer_buy_cost = 0;
        }
        net_cost_price = Math.round((total_cost_price + sebo_buy_commission + broker_buy_commission + name_transfer_buy_cost)*100)/100;

        if(sellRadio.prop('checked')){
            total_selling_price = qty * sellRate;
            sebo_sell_commission = Math.round(total_selling_price * sebo_percent*100)/100;
            broker_sell_commission = Math.round(total_selling_price * get_broker_percent(total_selling_price)*100)/100;
            name_transfer_sell_cost = Math.round(cert * name_tranfer_unit_cost * 100)/100;
            net_selling_price = Math.round((total_selling_price - sebo_sell_commission - broker_sell_commission - name_transfer_sell_cost)*100)/100;

            profit_Loss = Math.round((net_selling_price - net_cost_price)*100)/100;

            capital_gain = 0;

            if(profit_Loss > 0){
                capital_gain = Math.round(profit_Loss * capital_gain_percent * 100)/100;
                net_selling_price = net_selling_price - capital_gain;
                profit_Loss = Math.round((profit_Loss - capital_gain) * 100)/100;
            }
        }

    }

    function insertToModal(){
        setModalValue('no_of_shares', qty);
        setModalValue('per_unit_cost_price', buyRate);
        setModalValue('total_cost_price', total_cost_price);
        setModalValue('sebo_buy_commission', sebo_buy_commission);
        setModalValue('broker_buy_commission', broker_buy_commission);
        setModalValue('name_transfer_buy_cost', name_transfer_buy_cost);
        setModalValue('net_cost_price', net_cost_price);
        if(sellRadio.prop('checked')){
            setModalValue('per_unit_selling_price', sellRate);
            setModalValue('total_selling_price', total_selling_price);
            setModalValue('sebo_sell_commission', sebo_sell_commission);
            setModalValue('broker_sell_commission', broker_sell_commission);
            setModalValue('name_transfer_sell_cost', name_transfer_sell_cost);
            setModalValue('capital_gain', capital_gain);
            setModalValue('net_selling_price', net_selling_price);
            profitLossInd.text(profit_Loss);
            if(profit_Loss < 0){
                if(!profitLossInd.hasClass("warning")){
                    profitLossInd.removeClass("primary");
                    profitLossInd.addClass("warning");
                }
            }
            else{
                if(!profitLossInd.hasClass("primary")){
                    profitLossInd.removeClass("warning");
                    profitLossInd.addClass("primary");
                }
            }
        }

    }

    function adjustMode(){
        if(!sellRadio.prop('checked')){
            if(!buyResult.hasClass('col-sm-12')){
                buyResult.removeClass('col-sm-6').addClass('col-sm-12');
                sellResult.hide();
                profitLossResult.hide();
            }
        }
        else{
            if(!buyResult.hasClass('col-sm-6')){
                buyResult.removeClass('col-sm-12').addClass('col-sm-6');
                sellResult.show();
                profitLossResult.show();
            }
        }
    }

    function setModalValue(eClass, value){
        $("." + eClass, stockCalculatorResultModal).text(value);
    }


//Double sebon = 0.00015, dpfee = 25.0, ltfiftyk = 0.0060 , ltfivelakh = 0.0055, lttwentylakh = 0.0050, ltonecrore =  0.0045 , gtonecrore = 0.0040;
		
    function get_broker_percent(amount){
        var broker_percent;
        if(amount < 50000){
            broker_percent = 0.0060;
        }
        else if(amount < 500000){
            broker_percent = 0.0055;
        }
        else if(amount < 2000000){
        broker_percent = 0.0050
        }
        else if(amount < 10000000 ){
            broker_percent = 0.0045;
        }
        else {
            broker_percent = 0.0040;
        }
        return broker_percent;
    }

    function isEmpty(val){
        if(val == null || val==""){
            return true;
        }
        return false;
    }

    function isDeciNumber(val){
        var regex = '^[+]?[0-9]{1,9}(?:\.[0-9]{1,9})?$';
        if(val.match(regex)){
            return true;
        }
        else{
            return false;
        }
    }

    function isNumeric(val){
        var regex = '^[+]?[0-9]{1,9}$';
        if(val.match(regex)){
            return true;
        }
        else{
            return false;
        }
    }

});

No comments:

Post a Comment

Bottom Ad [Post Page]