Native PHP plugin (no Composer) that syncs: - Product stock and pricing from BC to WooCommerce (scheduled cron) - Orders from WooCommerce to BC (on payment received) - Auto-creates customers in BC from WooCommerce billing data Product matching: WooCommerce SKU → BC Item Number, fallback to GTIN (EAN). OAuth2 client credentials auth with encrypted secret storage. Admin settings page with connection test, manual sync, and log viewer. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
240 lines
7.6 KiB
JavaScript
240 lines
7.6 KiB
JavaScript
/**
|
|
* WooCommerce Business Central - Admin JavaScript
|
|
*/
|
|
|
|
(function($) {
|
|
'use strict';
|
|
|
|
var WBC_Admin = {
|
|
/**
|
|
* Initialize
|
|
*/
|
|
init: function() {
|
|
this.bindEvents();
|
|
},
|
|
|
|
/**
|
|
* Bind event handlers
|
|
*/
|
|
bindEvents: function() {
|
|
// Test connection
|
|
$('#wbc-test-connection').on('click', this.testConnection);
|
|
|
|
// Manual sync
|
|
$('#wbc-manual-sync').on('click', this.manualSync);
|
|
|
|
// Clear logs
|
|
$('#wbc-clear-logs').on('click', this.clearLogs);
|
|
|
|
// Load companies
|
|
$('#wbc-load-companies').on('click', this.loadCompanies);
|
|
|
|
// Select company
|
|
$('#wbc-company-select').on('change', this.selectCompany);
|
|
|
|
// Toggle log data
|
|
$(document).on('click', '.wbc-toggle-data', this.toggleLogData);
|
|
},
|
|
|
|
/**
|
|
* Test connection to Business Central
|
|
*/
|
|
testConnection: function(e) {
|
|
e.preventDefault();
|
|
|
|
var $btn = $(this);
|
|
var $status = $('#wbc-connection-status');
|
|
|
|
$btn.prop('disabled', true);
|
|
$status.removeClass('success error').addClass('loading').text(wbc_admin.strings.testing);
|
|
|
|
$.ajax({
|
|
url: wbc_admin.ajax_url,
|
|
type: 'POST',
|
|
data: {
|
|
action: 'wbc_test_connection',
|
|
nonce: wbc_admin.nonce
|
|
},
|
|
success: function(response) {
|
|
$btn.prop('disabled', false);
|
|
$status.removeClass('loading');
|
|
|
|
if (response.success) {
|
|
$status.addClass('success').text(response.data.message);
|
|
} else {
|
|
$status.addClass('error').text(response.data.message || 'Connection failed');
|
|
}
|
|
},
|
|
error: function(xhr, status, error) {
|
|
$btn.prop('disabled', false);
|
|
$status.removeClass('loading').addClass('error').text('Request failed: ' + error);
|
|
}
|
|
});
|
|
},
|
|
|
|
/**
|
|
* Run manual sync
|
|
*/
|
|
manualSync: function(e) {
|
|
e.preventDefault();
|
|
|
|
var $btn = $(this);
|
|
var $status = $('#wbc-sync-status');
|
|
|
|
$btn.prop('disabled', true);
|
|
$status.removeClass('success error').addClass('loading').text(wbc_admin.strings.syncing);
|
|
|
|
$.ajax({
|
|
url: wbc_admin.ajax_url,
|
|
type: 'POST',
|
|
data: {
|
|
action: 'wbc_manual_sync',
|
|
nonce: wbc_admin.nonce
|
|
},
|
|
success: function(response) {
|
|
$btn.prop('disabled', false);
|
|
$status.removeClass('loading');
|
|
|
|
if (response.success) {
|
|
$status.addClass('success').text(response.data.message);
|
|
} else {
|
|
$status.addClass('error').text(response.data.message || 'Sync failed');
|
|
}
|
|
},
|
|
error: function(xhr, status, error) {
|
|
$btn.prop('disabled', false);
|
|
$status.removeClass('loading').addClass('error').text('Request failed: ' + error);
|
|
}
|
|
});
|
|
},
|
|
|
|
/**
|
|
* Clear all logs
|
|
*/
|
|
clearLogs: function(e) {
|
|
e.preventDefault();
|
|
|
|
if (!confirm(wbc_admin.strings.confirm_clear)) {
|
|
return;
|
|
}
|
|
|
|
var $btn = $(this);
|
|
var originalText = $btn.text();
|
|
|
|
$btn.prop('disabled', true).text(wbc_admin.strings.clearing);
|
|
|
|
$.ajax({
|
|
url: wbc_admin.ajax_url,
|
|
type: 'POST',
|
|
data: {
|
|
action: 'wbc_clear_logs',
|
|
nonce: wbc_admin.nonce
|
|
},
|
|
success: function(response) {
|
|
if (response.success) {
|
|
// Reload the page to show empty logs
|
|
location.reload();
|
|
} else {
|
|
alert(response.data.message || 'Failed to clear logs');
|
|
$btn.prop('disabled', false).text(originalText);
|
|
}
|
|
},
|
|
error: function(xhr, status, error) {
|
|
alert('Request failed: ' + error);
|
|
$btn.prop('disabled', false).text(originalText);
|
|
}
|
|
});
|
|
},
|
|
|
|
/**
|
|
* Load companies from Business Central
|
|
*/
|
|
loadCompanies: function(e) {
|
|
e.preventDefault();
|
|
|
|
var $btn = $(this);
|
|
var $select = $('#wbc-company-select');
|
|
var $list = $('#wbc-companies-list');
|
|
var originalText = $btn.text();
|
|
|
|
$btn.prop('disabled', true).text(wbc_admin.strings.loading);
|
|
|
|
$.ajax({
|
|
url: wbc_admin.ajax_url,
|
|
type: 'POST',
|
|
data: {
|
|
action: 'wbc_get_companies',
|
|
nonce: wbc_admin.nonce
|
|
},
|
|
success: function(response) {
|
|
$btn.prop('disabled', false).text(originalText);
|
|
|
|
if (response.success && response.data.companies) {
|
|
// Clear existing options except the first one
|
|
$select.find('option:not(:first)').remove();
|
|
|
|
// Add companies
|
|
$.each(response.data.companies, function(i, company) {
|
|
$select.append(
|
|
$('<option>', {
|
|
value: company.id,
|
|
text: company.displayName + ' (' + company.id.substring(0, 8) + '...)'
|
|
})
|
|
);
|
|
});
|
|
|
|
// Show the select
|
|
$list.slideDown();
|
|
|
|
// Pre-select current company if set
|
|
var currentCompany = $('#wbc_company_id').val();
|
|
if (currentCompany) {
|
|
$select.val(currentCompany);
|
|
}
|
|
} else {
|
|
alert(response.data.message || 'Failed to load companies. Make sure credentials are saved and correct.');
|
|
}
|
|
},
|
|
error: function(xhr, status, error) {
|
|
$btn.prop('disabled', false).text(originalText);
|
|
alert('Request failed: ' + error);
|
|
}
|
|
});
|
|
},
|
|
|
|
/**
|
|
* Select company from dropdown
|
|
*/
|
|
selectCompany: function() {
|
|
var companyId = $(this).val();
|
|
if (companyId) {
|
|
$('#wbc_company_id').val(companyId);
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Toggle log data visibility
|
|
*/
|
|
toggleLogData: function(e) {
|
|
e.preventDefault();
|
|
|
|
var $btn = $(this);
|
|
var $data = $btn.siblings('.wbc-log-data');
|
|
|
|
$data.slideToggle(200, function() {
|
|
if ($data.is(':visible')) {
|
|
$btn.text($btn.data('hide-text') || 'Hide data');
|
|
} else {
|
|
$btn.text($btn.data('show-text') || 'Show data');
|
|
}
|
|
});
|
|
}
|
|
};
|
|
|
|
// Initialize on document ready
|
|
$(document).ready(function() {
|
|
WBC_Admin.init();
|
|
});
|
|
|
|
})(jQuery);
|