$(document).ready(function() {
// if browser supports session storage
if(typeof(window.localStorage) !== "undefined") {
// pre-load values from local storage
$("#textClientId").val(window.localStorage.getItem("clientId"));
$("#textClientSecret").val(window.localStorage.getItem("clientSecret"));
$("#textUserId").val(window.localStorage.getItem("userId"));
$("#textPassword").val(window.localStorage.getItem("password"));
$("#textCompanyId").val(window.localStorage.getItem("companyId"));
$(".saveToLocalStorage").show();
}
var _initialized = false;
function _initSetup() {
$.ajaxSetup({
beforeSend: function (xhr) {
var _authentication = $("#textUserId").val() + ":" + $("#textPassword").val();
// append prefix Basic (according to HTTP Basic Auth) and enocde into Base64
_authentication = "Basic " + window.btoa(_authentication);
// set your applications id & secret here to request
xhr.setRequestHeader("ClientID", $("#textClientId").val());
xhr.setRequestHeader("ClientSecret", $("#textClientSecret").val());
// set authentication to request
xhr.setRequestHeader("Authorization", _authentication);
// set Company identificator
var _companyId = $("#textCompanyId").val();
if(_companyId != "") {
xhr.setRequestHeader("CompanyID", _companyId);
}
// update values in local storage also
if($("#saveToLocalStorage").is(":checked")) {
// if browser supports session storage
if(typeof(window.localStorage) !== "undefined") {
window.localStorage.setItem("clientId", $("#textClientId").val());
window.localStorage.setItem("clientSecret", $("#textClientSecret").val());
window.localStorage.setItem("userId", $("#textUserId").val());
window.localStorage.setItem("password", $("#textPassword").val());
window.localStorage.setItem("companyId", $("#textCompanyId").val());
}
}
},
error: function (request, status, error) {
alert(request.responseText);
}
});
_initialized = true;
$(".demo-functions").show();
$("body").scrollTop(400);
// refresh companies select
_refreshCompanies();
if($("#textCompanyId").val() != '') {
// load ordners into select to help user build invoice object
$.ajax({
type: "GET",
url: "https://api.profit365.eu/1.1/catalogs/ordners?code=INO",
contentType: "application/json",
success: function (ordners) {
$("#selectOrdners").empty();
$("#selectOrdners").append("");
$.each(ordners, function() {
$("#selectOrdners").append(
$('', {
value: this.id,
html: this.localizedName
})
);
});
}
});
// load warehouses into select to help user build invoice object
$.ajax({
type: "GET",
url: "https://api.profit365.eu/1.2/catalogs/warehouses",
contentType: "application/json",
success: function (ordners) {
$("#selectWarehouses").empty();
$("#selectWarehouses").append("");
$.each(ordners, function() {
$("#selectWarehouses").append(
$('', {
value: this.id,
html: this.localizedName
})
);
});
}
});
}
}
// button SAVE default click event
$("#buttonSave").click(function(event) {
event.preventDefault();
_initSetup();
return false;
});
$(".demo-functions .menu button").click(function(event) {
event.preventDefault();
$(".demo-functions-sections > div").hide();
return false;
});
$("#buttonMenu1").click(function(event) {
$("#demo-section1").show();
});
$("#buttonMenu2").click(function(event) {
$("#demo-section2").show();
});
$("#buttonMenu3").click(function(event) {
$("#demo-section3").show();
});
$("#buttonMenu4").click(function(event) {
$("#demo-section4").show();
});
// handle change (selected item changed) event on select of ordners
$("#selectOrdners").change(function() {
// pre-select ordner id into textbox
$("#textOrdnerID").val(
// value selected in select
$(this).val()
);
});
// handle change (selected item changed) event on select of warehouses
$("#selectWarehouses").change(function() {
// pre-select ordner id into textbox
$("#textWarehouseID").val(
// value selected in select
$(this).val()
);
});
// handle change (selected item changed) event on select of warehouses
$("#selectCompanies").change(function() {
// pre-select ordner id into textbox
$("#selectedCompanyID").html(
// value selected in select
$(this).val()
);
});
// disable form submit on server (page refresh)
$("form").submit(function(event) {
event.preventDefault();
return false;
});
$("#buttonGetCompanies").click(function(event) {
event.preventDefault();
if (!_initialized) {
_initSetup();
}
// create ajax request to get list of invoices
$.ajax({
type: "GET",
url: "https://api.profit365.eu/1.2/user/companies",
contentType: "application/json",
success: function (companies) {
$("#selectCompanies").empty().show();
$("#selectCompanies").append("");
$.each(companies, function() {
$("#selectCompanies").append(
$('', {
value: this.companyID,
html: this.fullName
})
);
});
}
});
return false;
});
$("#buttonListInvoices").click(function(event) {
event.preventDefault();
if (!_initialized) {
_initSetup();
}
_getSalesInvoices(
$("#textPage").val(),
$("#textSearch").val(),
function(invoices) {
$("#invoices-result").text("Sales invoices count: " + invoices.length);
$("#invoices-result").append("
");
$("#invoices-result").append(JSON.stringify(invoices, null, '\t'));
}
);
return false;
});
$("#buttonCreateNewInvoice").click(function(event) {
event.preventDefault();
if (!_initialized) {
_initSetup();
}
// reset error message before execution
$("#newInvoiceMessage").text('');
var _invoice = {
ordnerID: $("#textOrdnerID").val(),
warehouseID: $("#textWarehouseID").val(),
dateCreated: $("#textDateCreated").val(),
partnerID: $("#textPartnerID").val(),
partnerAddress: $("#textPartnerAddress").val(),
rows: []
};
if(_invoice.dateCreated == '') {
$("#newInvoiceMessage").text('DateCreated is required. Check input values');
return;
}
try {
// append first row to invoice
_invoice.rows[0] = {
name: $("#textRow0Name").val(),
quantity: parseFloat($("#textRow0Amount").val()),
price: parseFloat($("#textRow0Price").val())
};
// append second row to invoice
_invoice.rows[1] = {
name: $("#textRow1Name").val(),
quantity: parseFloat($("#textRow1Amount").val()),
price: parseFloat($("#textRow1Price").val())
};
}
catch (e) {
$("#newInvoiceMessage").text('Failed to parse number. Check input values');
return;
}
$("#invoice-json").text("JSON sent to API: ");
$("#invoice-json").append("
");
$("#invoice-json").append(JSON.stringify(_invoice, null, '\t'));
_createSalesInvoice(
_invoice,
function (invoice) {
$("#invoice-result").text("Full invoice back from API: ");
$("#invoice-result").append("
");
$("#invoice-result").append(JSON.stringify(invoice, null, '\t'));
}
);
return false;
});
// function gets sales invoices list from api and send result to callback function
function _getSalesInvoices(page, search, callback) {
var _url = "https://api.profit365.eu/1.2/sales/invoices";
if (page != null && page != "") {
_url += "/" + page;
}
if (search != null && search != "") {
_url += "?search=" + search;
}
// create ajax request to get list of invoices
$.ajax({
type: "GET",
url: _url,
contentType: "application/json",
success: function (invoices) {
// execute callback with array of invoices
callback(invoices);
}
});
}
// function creates new sales invoice and sent it to api
function _createSalesInvoice(invoice, callback) {
var _data = JSON.stringify(invoice);
// dont forget to handle beforeSend
$.ajax({
type: "POST",
url: "https://api.profit365.eu/1.2/sales/invoices",
dataType: "json",
contentType: "application/json",
data: _data,
success: function (data) {
// full invoice data back from API
callback(data);
}
});
}
function _refreshCompanies() {
// create ajax request to get list of companies
$.ajax({
type: "GET",
url: "https://api.profit365.eu/1.3/user/companies",
contentType: "application/json",
success: function (companies) {
$("#selectCompanies").empty().show();
$("#selectCompanies").append("");
$.each(companies, function() {
$("#selectCompanies").append(
$('', {
value: this.id,
html: this.fullName
})
);
});
}
});
}
});