cmc-sales/webroot/js/add_costing.js

171 lines
4.9 KiB
JavaScript
Raw Normal View History

2010-02-04 08:30:49 -08:00
/*
* add_costing.js
*
* Do the costing buildup. Calculate GP etc.
*
* Copyright Karl Cordes 2010
*/
$(document).ready(function () {
$(":input").change(buildup)
});
2010-01-30 23:58:22 -08:00
function buildup() {
2010-02-04 08:30:49 -08:00
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());
2010-02-04 17:43:42 -08:00
var grossSellPrice = parseFloat($("#CostingGrossSellPrice").val());
var saleDiscountPercent = parseFloat($("#CostingSaleDiscountPercent").val());
2010-02-04 08:30:49 -08:00
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);
2010-02-04 17:43:42 -08:00
grossSellPrice = checkNaN(grossSellPrice);
saleDiscountPercent = checkNaN(saleDiscountPercent);
2010-02-04 08:30:49 -08:00
var discountAmount = (ourDiscountPercent / 100) * quantity * unitCostPrice;
var fobcountryofexport = ( (quantity * unitCostPrice) - discountAmount) + packing;
//Update the FOB Country of Export value
$("#CostingFobCountryOfExport").val(fobcountryofexport.toFixed(2));
2010-02-04 17:43:42 -08:00
//Update the Exchange Conversion
$("#exchangeConversion").text("1 "+$("#CostingSaleCurrencyId :selected").text()+" = "+exchangeRate+" "+$("#CostingPurchaseCurrencyId :selected").text());
2010-02-04 08:30:49 -08:00
2010-02-04 17:43:42 -08:00
//Calculate cost in Sale Currency
var fobSaleCurrency = fobcountryofexport / exchangeRate;
fobSaleCurrency = fobSaleCurrency.toFixed(2);
$("#CostingFobSaleCurrency").val(fobSaleCurrency);
2010-02-04 08:30:49 -08:00
2010-02-04 17:43:42 -08:00
//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.
2010-02-04 08:30:49 -08:00
2010-02-04 17:43:42 -08:00
var financeAmount = (financePercent/100)*fobSaleCurrency;
financeAmount = financeAmount.toFixed(2);
$("#CostingFinanceAmount").val(financeAmount);
2010-02-04 08:30:49 -08:00
2010-02-04 17:43:42 -08:00
//Calculate the Total Landed Cost
var totalLandedCost = +(fobSaleCurrency) + +(customs) + +(shippingCost) + +(dutyAmount) + +(financeAmount) + +(miscCosts);
2010-02-04 08:30:49 -08:00
2010-02-04 17:43:42 -08:00
totalLandedCost = totalLandedCost.toFixed(2);
$("#CostingTotalLandedCost").val(totalLandedCost);
2010-02-04 08:30:49 -08:00
2010-02-04 17:43:42 -08:00
//Calculate the Sale Discount amount.
var saleDiscountAmount = (saleDiscountPercent/100) * grossSellPrice;
saleDiscountAmount = saleDiscountAmount.toFixed(2);
$("#CostingSaleDiscountAmount").val(saleDiscountAmount);
2010-02-04 08:30:49 -08:00
2010-02-04 17:43:42 -08:00
//Calculate the Gross GP prior to discount
2010-02-04 08:30:49 -08:00
2010-02-04 17:43:42 -08:00
var grossGP = grossSellPrice - totalLandedCost;
grossGP = grossGP.toFixed(2);
$("#CostingGrossGpPriorToDiscount").val(grossGP);
2010-02-04 08:30:49 -08:00
2010-02-04 17:43:42 -08:00
//Calculate the Net Selling Price
var netSellPrice = grossSellPrice - saleDiscountAmount;
$("#CostingNetSellPrice").val(netSellPrice);
2010-02-04 08:30:49 -08:00
2010-02-04 17:43:42 -08:00
//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);
2010-02-04 08:30:49 -08:00
2010-02-04 17:43:42 -08:00
//Calulate Gross/Net Sell Prices
2010-02-04 08:30:49 -08:00
2010-02-04 17:43:42 -08:00
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);
});
2010-02-04 08:30:49 -08:00
2010-01-30 23:58:22 -08:00
}
2010-02-04 08:30:49 -08:00
function checkNaN(value) {
if( isNaN(value) == true) {
return 0;
}
else {
return value;
}
}