jQuery(function ($){
if(typeof wp==='undefined'||typeof wp.data==='undefined'){
console.error('wp.data is not loaded. Ensure WooCommerce Blocks are enqueued.');
return;
}
const { dispatch }=wp.data;
if(!dispatch){
console.error('WooCommerce Blocks are not loaded. Ensure wc-blocks-checkout is enqueued.');
}
$(document).ready(function (){
const isBlockCheckout=($('.wc-block-checkout').length > 0);
const showAllFields=typeof ajax_object!=='undefined'&&ajax_object.display_all_fields;
if(isBlockCheckout){
console.log("Detected BLOCK Checkout.");
function checkOBSInvoiceFieldsVisibilityBlock(){
let receiptType=$('#contact-obs-checkout-fields-billing_receipt_type').val();
if(receiptType==='timologio'){
updateProps('contact-obs-checkout-fields-billing_vat', true);
updateProps('contact-obs-checkout-fields-billing_tax_office', true);
updateProps('contact-obs-checkout-fields-billing_job_description', true);
updateProps('contact-obs-checkout-fields-billing_company', true);
updateProps('contact-obs-checkout-fields-billing_bs_address', true);
updateProps('contact-obs-checkout-fields-billing_bs_city', true);
updateProps('contact-obs-checkout-fields-billing_bs_zip', true);
$('.wc-block-components-address-form__obs-checkout-fields-billing_vat').slideDown('fast');
if(showAllFields){
$('.wc-block-components-select-input-obs-checkout-fields-billing_tax_office').slideDown('fast');
$('.wc-block-components-address-form__obs-checkout-fields-billing_job_description').slideDown('fast');
$('.wc-block-components-address-form__obs-checkout-fields-billing_company').slideDown('fast');
$('.wc-block-components-address-form__obs-checkout-fields-billing_bs_address').slideDown('fast');
$('.wc-block-components-address-form__obs-checkout-fields-billing_bs_city').slideDown('fast');
$('.wc-block-components-address-form__obs-checkout-fields-billing_bs_zip').slideDown('fast');
}}else{
updateProps('contact-obs-checkout-fields-billing_vat', false);
updateProps('contact-obs-checkout-fields-billing_tax_office', false);
updateProps('contact-obs-checkout-fields-billing_job_description', false);
updateProps('contact-obs-checkout-fields-billing_company', false);
updateProps('contact-obs-checkout-fields-billing_bs_address', false);
updateProps('contact-obs-checkout-fields-billing_bs_city', false);
updateProps('contact-obs-checkout-fields-billing_bs_zip', false);
$('.wc-block-components-address-form__obs-checkout-fields-billing_vat').slideUp('fast');
$('.wc-block-components-select-input-obs-checkout-fields-billing_tax_office').slideUp('fast');
$('.wc-block-components-address-form__obs-checkout-fields-billing_job_description').slideUp('fast');
$('.wc-block-components-address-form__obs-checkout-fields-billing_company').slideUp('fast');
$('.wc-block-components-address-form__obs-checkout-fields-billing_bs_address').slideUp('fast');
$('.wc-block-components-address-form__obs-checkout-fields-billing_bs_city').slideUp('fast');
$('.wc-block-components-address-form__obs-checkout-fields-billing_bs_zip').slideUp('fast');
const fieldsToClear=[
'contact-obs-checkout-fields-billing_vat',
'contact-obs-checkout-fields-billing_tax_office',
'contact-obs-checkout-fields-billing_job_description',
'contact-obs-checkout-fields-billing_company',
'contact-obs-checkout-fields-billing_bs_address',
'contact-obs-checkout-fields-billing_bs_city',
'contact-obs-checkout-fields-billing_bs_zip'
];
fieldsToClear.forEach((fieldId, index)=> {
setTimeout(()=> {
updateField(fieldId, '');
}, index * 50);
});
}
if(receiptType==='timologio'){
$('.wc-block-components-address-form__obs-checkout-fields-billing_job_description').slideDown('fast');
}}
function updateField(fieldId, value){
const inputElement=document.getElementById(fieldId);
if(inputElement){
const fiberKey=Object.keys(inputElement).find(key=> key.startsWith('__reactFiber$'));
const fiberNode=inputElement[fiberKey];
if(fiberNode&&fiberNode.return&&fiberNode.return.memoizedProps){
const children=fiberNode.return.memoizedProps.children;
if(Array.isArray(children)&&children[0]&&children[0].props&&children[0].props.onChange){
children[0].props.onChange({
target: {
id: fieldId,
value: value,
}});
}else{
children[1].props.onChange({
target: {
id: fieldId,
value: value
}});
}}else{
console.error(`React Fiber node not found for element: ${fieldId}`);
}}else{
console.error(`Element not found: ${fieldId}`);
}}
function updateProps(fieldId, toggleRequired){
if(toggleRequired){
if(fieldId==='contact-obs-checkout-fields-billing_tax_office'){
jQuery('#' + fieldId + ' option:first').text(ajax_object.select_tax_office_required);
jQuery('#' + fieldId + ' option:first').attr('data-alternate-values', ajax_object.select_tax_office_required);
jQuery('label[for="' + fieldId + '"]').text(ajax_object.tax_office_required);
}else{
oldValue=jQuery('#' + fieldId).attr('aria-label');
newValue=oldValue.replace(ajax_object.optional_text, ajax_object.required_text);
jQuery('#' + fieldId).attr('aria-label', newValue);
jQuery('label[for="' + fieldId + '"]').text(newValue);
}}else{
if(fieldId==='contact-obs-checkout-fields-billing_tax_office'){
jQuery('#' + fieldId + ' option:first').text(ajax_object.select_tax_office_optional);
jQuery('#' + fieldId + ' option:first').attr('data-alternate-values', ajax_object.select_tax_office_optional);
jQuery('label[for="' + fieldId + '"]').text(ajax_object.tax_office_optional);
}else{
oldValue=jQuery('#' + fieldId).attr('aria-label');
newValue=oldValue.replace(ajax_object.required_text, ajax_object.optional_text);
jQuery('#' + fieldId).attr('aria-label', newValue);
jQuery('label[for="' + fieldId + '"]').text(newValue);
}}
}
function initializeOBSFields(){
const waitForElements=setInterval(()=> {
const vatField=document.getElementById('contact-obs-checkout-fields-billing_vat');
const receiptTypeField=document.getElementById('contact-obs-checkout-fields-billing_receipt_type');
if(vatField&&receiptTypeField){
clearInterval(waitForElements);
const vatValue=$('#contact-obs-checkout-fields-billing_vat').val();
if(vatValue){
updateField('contact-obs-checkout-fields-billing_receipt_type', 'timologio');
updateField('contact-obs-checkout-fields-billing_vat', vatValue);
}else{
updateField('contact-obs-checkout-fields-billing_receipt_type', 'apodeixi');
checkOBSInvoiceFieldsVisibilityBlock();
const fieldsToClear=[
'contact-obs-checkout-fields-billing_vat',
'contact-obs-checkout-fields-billing_tax_office',
'contact-obs-checkout-fields-billing_job_description',
'contact-obs-checkout-fields-billing_company',
'contact-obs-checkout-fields-billing_bs_address',
'contact-obs-checkout-fields-billing_bs_city',
'contact-obs-checkout-fields-billing_bs_zip'
];
fieldsToClear.forEach((fieldId, index)=> {
setTimeout(()=> {
updateField(fieldId, '');
}, index * 50);
});
}}
}, 100);
}
initializeOBSFields();
$(document).on('change', '#contact-obs-checkout-fields-billing_receipt_type', function (){
console.log('lalalalal');
checkOBSInvoiceFieldsVisibilityBlock();
const chosen=$(this).val();
let timologio=(chosen==='timologio') ? 1:0;
$.ajax({
url: ajax_object.ajaxurl,
type: 'POST',
data: {
action: 'check_timologio',
timologio: timologio
},
success: function (){
$('body').trigger('update_checkout');
},
error: function (xhr){
console.log(xhr.responseText);
}});
});
$(document).on('input', '#contact-obs-checkout-fields-billing_vat', function (){
let vatValue=$(this).val();
if(vatValue.length===9){
validateVatBlock(vatValue);
}});
function validateVatBlock(vatNumber){
if(!showAllFields){
$('.wc-block-components-select-input-obs-checkout-fields-billing_tax_office').slideUp('fast');
$('.wc-block-components-address-form__obs-checkout-fields-billing_job_description').slideUp('fast');
$('.wc-block-components-address-form__obs-checkout-fields-billing_company').slideUp('fast');
$('.wc-block-components-address-form__obs-checkout-fields-billing_bs_address').slideUp('fast');
$('.wc-block-components-address-form__obs-checkout-fields-billing_bs_city').slideUp('fast');
$('.wc-block-components-address-form__obs-checkout-fields-billing_bs_zip').slideUp('fast');
}
$.ajax({
type: "POST",
url: ajax_object.ajaxurl,
data: {
action: "validate_vat",
vat_id: vatNumber
},
dataType: "json",
success: function (response){
if(response.status==="success"){
console.log("VAT validated successfully!");
const updateFieldsWithResponseData=(response)=> {
const fieldsToUpdate=[
{ id: 'contact-obs-checkout-fields-billing_tax_office', value: 'doy-' + response.vat.doy },
{ id: 'contact-obs-checkout-fields-billing_job_description', value: response.vat.activities[0] },
{ id: 'contact-obs-checkout-fields-billing_company', value: response.vat.onomasia },
{ id: 'contact-obs-checkout-fields-billing_bs_address', value: response.vat.postal_address + ' ' + response.vat.postal_address_no },
{ id: 'contact-obs-checkout-fields-billing_bs_city', value: response.vat.postal_area_description },
{ id: 'contact-obs-checkout-fields-billing_bs_zip', value: response.vat.postal_zip_code }
];
fieldsToUpdate.forEach((field, index)=> {
setTimeout(()=> {
updateField(field.id, field.value);
console.log(`Updated field: ${field.id} with value: ${field.value}`);
}, index * 50);
});
};
updateFieldsWithResponseData(response);
$('.wc-block-components-select-input-obs-checkout-fields-billing_tax_office').slideDown('fast');
$('.wc-block-components-address-form__obs-checkout-fields-billing_job_description').slideDown('fast');
$('.wc-block-components-address-form__obs-checkout-fields-billing_company').slideDown('fast');
$('.wc-block-components-address-form__obs-checkout-fields-billing_bs_address').slideDown('fast');
$('.wc-block-components-address-form__obs-checkout-fields-billing_bs_city').slideDown('fast');
$('.wc-block-components-address-form__obs-checkout-fields-billing_bs_zip').slideDown('fast');
}else{
const fieldsToClear=[
'contact-obs-checkout-fields-billing_tax_office',
'contact-obs-checkout-fields-billing_job_description',
'contact-obs-checkout-fields-billing_company',
'contact-obs-checkout-fields-billing_bs_address',
'contact-obs-checkout-fields-billing_bs_city',
'contact-obs-checkout-fields-billing_bs_zip'
];
fieldsToClear.forEach((fieldId, index)=> {
setTimeout(()=> {
updateField(fieldId, '');
}, index * 50);
});
if(!showAllFields){
$('.wc-block-components-select-input-obs-checkout-fields-billing_tax_office').slideUp('fast');
$('.wc-block-components-address-form__obs-checkout-fields-billing_job_description').slideUp('fast');
$('.wc-block-components-address-form__obs-checkout-fields-billing_company').slideUp('fast');
$('.wc-block-components-address-form__obs-checkout-fields-billing_bs_address').slideUp('fast');
$('.wc-block-components-address-form__obs-checkout-fields-billing_bs_city').slideUp('fast');
$('.wc-block-components-address-form__obs-checkout-fields-billing_bs_zip').slideUp('fast');
}
alert(ajax_object.invalid_vat_text);
}},
error: function (xhr){
console.log("VAT validation error:", xhr.responseText);
const fieldsToClear=[
'contact-obs-checkout-fields-billing_tax_office',
'contact-obs-checkout-fields-billing_job_description',
'contact-obs-checkout-fields-billing_company',
'contact-obs-checkout-fields-billing_bs_address',
'contact-obs-checkout-fields-billing_bs_city',
'contact-obs-checkout-fields-billing_bs_zip'
];
fieldsToClear.forEach((fieldId, index)=> {
setTimeout(()=> {
updateField(fieldId, '');
}, index * 50);
});
if(!showAllFields){
$('.wc-block-components-select-input-obs-checkout-fields-billing_tax_office').slideUp('fast');
$('.wc-block-components-address-form__obs-checkout-fields-billing_job_description').slideUp('fast');
$('.wc-block-components-address-form__obs-checkout-fields-billing_company').slideUp('fast');
$('.wc-block-components-address-form__obs-checkout-fields-billing_bs_address').slideUp('fast');
$('.wc-block-components-address-form__obs-checkout-fields-billing_bs_city').slideUp('fast');
$('.wc-block-components-address-form__obs-checkout-fields-billing_bs_zip').slideUp('fast');
}
alert(ajax_object.invalid_vat_text);
}});
}
$('.wc-block-components-checkout-place-order-button').on('click', function (e){
let button=$(this);
let receiptType=$('#contact-obs-checkout-fields-billing_receipt_type').val();
if(receiptType==='timologio'){
let requiredFields=[
'#contact-obs-checkout-fields-billing_vat',
'#contact-obs-checkout-fields-billing_tax_office',
'#contact-obs-checkout-fields-billing_job_description',
'#contact-obs-checkout-fields-billing_company',
'#contact-obs-checkout-fields-billing_bs_address',
'#contact-obs-checkout-fields-billing_bs_city',
'#contact-obs-checkout-fields-billing_bs_zip'
];
let allFieldsFilled=true;
for (let i=0; i < requiredFields.length; i++){
if(!$(requiredFields[i]).val()){
allFieldsFilled=false;
break;
}}
if(!allFieldsFilled){
alert(ajax_object.required_fields_text);
button.prop('disabled', true);
setTimeout(function (){
button.prop('disabled', false);
}, 1000);
return false;
}}
button.prop('disabled', true);
return true;
});
}else{
console.log("Detected CLASSIC Checkout.");
function checkOBSInvoiceFieldsVisibility(){
let receipt_type=$('input[name="billing_receipt_type"]:checked').val();
if(receipt_type=='timologio'){
$('#afm_field_container').slideDown('fast');
var vatValue=$('#billing_vat').val();
if(showAllFields){
$('#rest_of_fields_container').slideDown('fast');
}else if(vatValue.length==9){
validateVat(vatValue);
}else{
$('#rest_of_fields_container').slideUp('fast');
}
$('#billing_job_description').slideDown('fast');
}else{
$('#afm_field_container').slideUp('fast');
$('#rest_of_fields_container').slideUp('fast');
}}
$('input[name="billing_receipt_type"]').on('change', function (){
checkOBSInvoiceFieldsVisibility();
var selectedOption=$(this).val();
var timologio=(selectedOption==='timologio') ? 1:0;
$.ajax({
url: ajax_object.ajaxurl,
type: 'POST',
data: {
action: 'check_timologio',
timologio: timologio
},
success: function (response){
$('body').trigger('update_checkout');
},
error: function (xhr, status, error){
console.log(xhr.responseText);
}});
});
checkOBSInvoiceFieldsVisibility();
$('#billing_vat').on('input', function (){
var vatValue=$(this).val();
if(!showAllFields){
if(vatValue.length==9){
validateVat(vatValue);
}else{
$('#rest_of_fields_container').slideUp('fast');
}}else if(vatValue.length==9){
validateVat(vatValue);
}});
function validateVat(vatNumber){
$('body').trigger('update_checkout');
$.ajax({
type: "post",
url: ajax_object.ajaxurl,
data: {
'action': "validate_vat",
'vat_id': vatNumber
},
dataType: "JSON",
success: function (response){
if(response.status=="success"){
$("#billing_company").val(response.vat.commer_title);
$("#billing_tax_office").val("doy-" + response.vat.doy);
$("#billing_job_description").val(response.vat.activities[0]);
$("#billing_bs_address").val(response.vat.postal_address + ' ' + response.vat.postal_address_no);
$("#billing_bs_zip").val(response.vat.postal_zip_code);
$("#billing_bs_city").val(response.vat.postal_area_description);
$("#billing_company").val(response.vat.onomasia);
$('#rest_of_fields_container').slideDown('fast');
}else{
if(!showAllFields){
$('#rest_of_fields_container').slideUp('fast');
}
alert(ajax_object.invalid_vat_text);
}}
});
}
$(document).on('change', '#billing_39a_afm', function (){
var afm=$(this).val();
$.ajax({
url: '/wp-admin/admin-ajax.php',
type: 'POST',
data: {
action: 'validate_39a',
afm: afm
},
success: function (response){
$('body').trigger('update_checkout');
if(response!=1){
if($('.error_afm').length <=0){
$('#billing_39a_afm_field').append('<span class="error_afm">' + response + '</span>');
}}else{
$('.error_afm').remove();
}},
error: function (xhr, status, error){
console.log(xhr.responseText);
}});
});
$("body").on("click", "#place_order", function (event){
var receipt_type=$('input[name="billing_receipt_type"]:checked').val();
if(receipt_type=='timologio'){
if($("#billing_vat").val()==""||$("#billing_tax_office").val()==""||$("#billing_job_description").val()==""){
event.preventDefault();
alert(ajax_object.required_fields_text);
}}
});
if(typeof ajax_object.invoiceData!=='undefined'&&Object.keys(ajax_object.invoiceData).length > 0){
$('#billing_company').val(ajax_object.invoiceData.billing_company);
$('#billing_job_description').val(ajax_object.invoiceData.job_description);
$('#billing_vat').val(ajax_object.invoiceData.vat);
$('#billing_bs_address').val(ajax_object.invoiceData.billing_bs_address);
$('#billing_bs_city').val(ajax_object.invoiceData.billing_bs_city);
$('#billing_bs_zip').val(ajax_object.invoiceData.billing_bs_zip);
$('#billing_tax_office').val(ajax_object.invoiceData.tax_office);
$('#billing_phone').val(ajax_object.invoiceData.billing_phone);
$('input[name="billing_receipt_type"][value="timologio"]').prop('checked', true);
$('input[name="billing_receipt_type"]').trigger('change');
$('#rest_of_fields_container').slideDown('fast');
}}
});
});