/*
 * add_costing.js
 *
 * Do the costing buildup. Calculate GP etc.
 *
 * Copyright Karl Cordes 2010
 */

$(document).ready(function () { 

    $(":input").change(buildup)

});


function buildup() {

    var quantity = parseFloat($("#CostingQuantity").val());
    var unitCostPrice = parseFloat($("#CostingUnitCostPrice").val());
    var exchangeRate = parseFloat($("#CostingExchangeRate").val());
    var ourDiscountPercent = parseFloat($("#CostingOurDiscountPercent").val());
    var packing = parseFloat($("#CostingPacking").val());

    var shippingCost = parseFloat($("#CostingShippingCost").val());
    var dutyPercent = parseFloat($("#CostingDutyPercent").val());
    var customs = parseFloat($("#CostingCustoms").val());
    var financePercent = parseFloat($("#CostingFinancePercent").val());
    var miscCosts = parseFloat($("#CostingMiscCosts").val());

    var grossSellPrice = parseFloat($("#CostingGrossSellPrice").val());
    var saleDiscountPercent = parseFloat($("#CostingSaleDiscountPercent").val());
    


    quantity = checkNaN(quantity);
    unitCostPrice = checkNaN(unitCostPrice);
    exchangeRate = checkNaN(exchangeRate);
    ourDiscountPercent = checkNaN(ourDiscountPercent);
    packing = checkNaN(packing);

    shippingCost = checkNaN(shippingCost);
    dutyPercent = checkNaN(dutyPercent);
    customs = checkNaN(customs);
    financePercent = checkNaN(financePercent);
    miscCosts = checkNaN(miscCosts);

    grossSellPrice = checkNaN(grossSellPrice);
    saleDiscountPercent = checkNaN(saleDiscountPercent);


    var discountAmount = (ourDiscountPercent / 100) * quantity * unitCostPrice;

    var fobcountryofexport = ( (quantity * unitCostPrice) - discountAmount) + packing;


    //Update the FOB Country of Export value
    $("#CostingFobCountryOfExport").val(fobcountryofexport.toFixed(2));




    //Update the Exchange Conversion
    $("#exchangeConversion").text("1 "+$("#CostingSaleCurrencyId :selected").text()+" = "+exchangeRate+" "+$("#CostingPurchaseCurrencyId :selected").text());


    //Calculate cost in Sale Currency
    var fobSaleCurrency = fobcountryofexport / exchangeRate;

    fobSaleCurrency = fobSaleCurrency.toFixed(2);
    $("#CostingFobSaleCurrency").val(fobSaleCurrency);


    //Calulate dutyAmount, update the hidden field.

    var dutyAmount = (dutyPercent/100)*fobSaleCurrency;

    dutyAmount = dutyAmount.toFixed(2);
    $("#CostingDutyAmount").val(dutyAmount);


    //Calculate finance amount. update the the hidden field.
    
    var financeAmount = (financePercent/100)*fobSaleCurrency;
    financeAmount = financeAmount.toFixed(2);
    $("#CostingFinanceAmount").val(financeAmount);


    //Calculate the Total Landed Cost
    var totalLandedCost = +(fobSaleCurrency) + +(customs) + +(shippingCost) + +(dutyAmount) + +(financeAmount) + +(miscCosts);

    totalLandedCost = totalLandedCost.toFixed(2);
    $("#CostingTotalLandedCost").val(totalLandedCost);

    
    //Calculate the Sale Discount amount. 
    var saleDiscountAmount = (saleDiscountPercent/100) * grossSellPrice;
    saleDiscountAmount = saleDiscountAmount.toFixed(2);
    $("#CostingSaleDiscountAmount").val(saleDiscountAmount);

    //Calculate the Gross GP prior to discount

    var grossGP = grossSellPrice - totalLandedCost;
    grossGP = grossGP.toFixed(2);
    $("#CostingGrossGpPriorToDiscount").val(grossGP);

    //Calculate the Net Selling Price
    var netSellPrice = grossSellPrice - saleDiscountAmount;
    $("#CostingNetSellPrice").val(netSellPrice);

    //Calculate net GP Amount
    var netGPamount = grossGP - saleDiscountAmount;
    netGPamount = netGPamount.toFixed(2);
    $("#CostingNetGpAmount").val(netGPamount);
    //Calculate net GP %
    var netGPpercent = (netGPamount / grossSellPrice) * 100;
    netGPpercent = netGPpercent.toFixed(2);
    $("#CostingNetGpPercent").val(netGPpercent);

    //Calulate Gross/Net Sell Prices

    var grossSellpriceEach = grossSellPrice / quantity;
    var netSellpriceEach = netSellPrice / quantity;

    $("#CostingGrossSellPriceEach").val(grossSellpriceEach);
    $("#CostingNetSellPriceEach").val(netSellpriceEach);


    //get a list of currencies in JSON format
    //update the Sale and Purchase currency classes

    $.getJSON('/currencies/jsonlist', function(data) {

        $(".saleCurrency").text(data[$("#CostingSaleCurrencyId").val()].iso4217);

        $(".purchaseCurrency").text(data[$("#CostingPurchaseCurrencyId").val()].iso4217);

        $("#dutyAmount").text("Duty Amount = "+dutyAmount+ " " +data[$("#CostingSaleCurrencyId").val()].iso4217);

        $("#financeAmount").text("Finance Amount = "+financeAmount+" "+data[$("#CostingSaleCurrencyId").val()].iso4217);

        $("#saleDiscountAmount").text("Discount Amount = "+saleDiscountAmount+" "+data[$("#CostingSaleCurrencyId").val()].iso4217);

    });
    


    

    
   






}




function checkNaN(value) {
    if( isNaN(value) == true) {
        return 0;
    }
    else {
        return value;
    }
}
