(function() {
/**
* Main function to get the estimates, receives the itemid, zipcode and the country.
* @param {[type]} item [description]
* @param {[type]} zip [description]
* @param {[type]} country [description]
* @return {[type]} [description]
*/
"use strict";
function getEstimates(itemid, quantity, zip, country) {
var promise = initRequests(item, itemOptions, quantity);
return promise.then(function() {
return setZipAndCountry(zip, itemOptions,country);
}).then(function() {
return getShippingMethods(item, quantity, itemOptions);
})
;}
/**
*
* @return {[type]} [description]
*/
function initRequests(item, itemOptions, qty) {
if (!itemOptions) {
itemOptions = "";
}
return jq_1_11.ajax({
url: '/c.560765/estimate_shipping/services/addItem.ss?itemid=' + item + '&qty=' + qty + "&options=" + JSON.stringify(itemOptions),
type: 'get',
contentType: 'application/json',
success: function(data) {
console.log('We set the item.');
}
});
}
/**
* Consumes a service to get the available shipping methods to present to the costumer,
* @param {[type]} item_id [description]
* @return {[type]} [description]
*/
function getShippingMethods(item_id, quantity, itemOptions) {
if (!itemOptions) {
itemOptions = "";
}
return jq_1_11.ajax({
url: "/c.560765/estimate_shipping/services/get_estimates.ss?options=" + JSON.stringify(itemOptions),
type: 'get',
data: {
'itemid': item_id,
'qty': quantity
},
success: function(data) {
console.log('We get the data.');
}
});
}
/**
* Consumes a service to set the zipcode and the country for the order.
* @param {[type]} zip [description]
* @param {[type]} country [description]
*/
function setZipAndCountry(zip, country) {
var data = genAddressWithZipCountry(zip, country);
return jq_1_11.ajax({
url: '/estimate_shipping/services/live-order.ss?internalid=cart&c=560765&n=1',
type: 'put',
contentType: 'application/json',
data: JSON.stringify(data),
success: function(data) {
console.log('Load was performed.');
}
});
}
/**
* Generates an address with the zipcode and country passed as paremmeter.
* @param {[type]} zip [description]
* @param {[type]} country [description]
* @return {[type]} [description]
*/
function genAddressWithZipCountry(zip, country) {
var zip_country = zip + "-" + country + "-null";
var data = {
"internalid": "cart",
"addresses": [{
"internalid": zip_country,
"zip": zip,
"country": country
}],
"shipaddress": zip_country,
};
return data;
}
/**
* Presents the estimates to the frontend user.
* Generates a table with the available shipping methods and its rates.
* @param {[type]} data [description]
* @param {[type]} modal [description]
* @return {[type]} [description]
*/
function presentEstimates(data, modal) {
var content = "";
if (data.shipping_methods !== undefined) {
content += '<div class="table-pop">';
content += '<div class="table-pop-title">';
content += '<div class="pop-left">Shipping Method</div>';
content += '<div class="pop-right">Rate</div>';
content += '</div>';
content += '<div class="table-pop-content">';
for (var i in data.shipping_methods) {
var shipping = data.shipping_methods[i];
var row = '<div class="pop-left-cont">' + shipping.name + '</div>';
row += '<div class="pop-right-cont">' + shipping.rate + '</div>';
content += row;
}
content += '</div>';
content += '</div>';
modal.options.content = content;
} else {
alert("Sorry something went wrong we couldn't estimate this item for you, try on \"my cart\" page.");
}
}
/**
* Bind the button to trigger the functionality to present the estimates to the frontend users.
* @return {[type]} [description]
*/
function bindEstimateFunctionality() {
var myModal = new Modal({});
var triggerButton = document.getElementById('trigger');
if (triggerButton !== null) {
triggerButton.addEventListener('click', function(event) {
event.preventDefault();
// get the itemid
var itemid = this.getAttribute("data-itemid");
var checkMandatory = eval('checkmandatory' + itemid + '()');
if (checkMandatory === false) {
var loader = "<div style='text-align:center; margin:0 auto;'><h3>We are processing your request, please wait.</h3><img style='max-height:200px;' src='/c.560765/estimate_shipping/frontend/img/loader.gif'></div>";
myModal.options.content = loader;
myModal.open();
// get zipcode
var zipcode = document.getElementById('zipcode').value;
// get the quantity
var quantity = document.getElementById('quantity').value;
// deberÃas tomar el id de un hijo cuando corresponda, esto obviamente para el caso de matrix items <-- no se puede.
// obtener las item options
var itemOptions = getItemOptions();
getEstimates(itemid, itemOptions, quantity, zipcode, 'US', myModal).then(function(data) {
var jsonData = JSON.parse(data);
presentEstimates(jsonData, myModal);
myModal.redraw();
});
}
});
}
}
/**
* For testing only
* @return {[type]} [description]
*/
function test() {
var modal = new Modal({});
var loader = "<div style='text-align:center; margin:0 auto;'><h3>We are processing your request, please wait.</h3><img style='max-height:200px;' src='/c.560765/estimate_shipping/frontend/img/loader.gif'></div>";
modal.options.content = loader;
modal.open();
getEstimates(984, 1, '33166', 'US', modal).then(function(data) {
var jsonData = JSON.parse(data);
presentEstimates(jsonData, modal);
modal.redraw();
});
}
/**
* For testing only
* @return {[type]} [description]
*/
function testImproved() {
var modal = new Modal({});
improvedGetEstimates(988, 1, '11109', 'US', modal).then(function(data) {
var jsonData = JSON.parse(data);
presentEstimates(jsonData, modal);
modal.open();
});
}
// debo obtener un json con las item options que se hayan seleccionado,
// pueden ser varias, en el caso que tengan talle, color, etc.
function getItemOptions() {
var jsonData = {};
$('.inputreq').each(function(i, k) {
jsonData[$(k).attr("name")] = $(k).val();
});
return jsonData;
}
// Main entry point
jq_1_11(function() {
// bind the estimate shipping funcitonality.
bindEstimateFunctionality();
});
})();