From 18d85caae2609d4f613d82f474afdb8245eb0461 Mon Sep 17 00:00:00 2001 From: Alexandre Faustino Date: Fri, 13 Dec 2024 10:34:44 +0000 Subject: [PATCH] New: adds support for multiple UBL formats (#927) --- assets/js/admin-script.js | 11 +++++++++++ assets/js/admin-script.min.js | 2 +- includes/Documents/Invoice.php | 25 ++++++++++++++++++++----- includes/Documents/OrderDocument.php | 11 +++++++++++ includes/Settings/SettingsGeneral.php | 2 +- ubl/Builders/SabreBuilder.php | 2 +- ubl/Documents/Document.php | 1 + ubl/Documents/UblDocument.php | 8 ++++++-- 8 files changed, 52 insertions(+), 10 deletions(-) diff --git a/assets/js/admin-script.js b/assets/js/admin-script.js index 29ab0b02f..ba4046df8 100644 --- a/assets/js/admin-script.js +++ b/assets/js/admin-script.js @@ -55,6 +55,17 @@ jQuery( function( $ ) { $( this ).closest( 'tr' ).nextAll( 'tr' ).has( 'input#next_invoice_number' ).first().show(); } } ).trigger( 'change' ); + + // disable encrypted pdf option for non UBL 2.1 formats + $( "[name='wpo_wcpdf_documents_settings_invoice_ubl[ubl_format]']" ).on( 'change', function( event ) { + let $encryptedPdfCheckbox = $( this ).closest( 'form' ).find( "[name='wpo_wcpdf_documents_settings_invoice_ubl[include_encrypted_pdf]']" ); + + if ( $( this ).val() !== 'ubl_2_1' ) { + $encryptedPdfCheckbox.prop( 'checked', false ).prop( 'disabled', true ); + } else { + $encryptedPdfCheckbox.prop( 'disabled', false ); + } + } ).trigger( 'change' ); // enable settings document switch $( '.wcpdf_document_settings_sections > h2' ).on( 'click', function() { diff --git a/assets/js/admin-script.min.js b/assets/js/admin-script.min.js index 3b7f2d1de..c43e6e076 100644 --- a/assets/js/admin-script.min.js +++ b/assets/js/admin-script.min.js @@ -1 +1 @@ -jQuery(function(a){function b(){m=w.val(),n=x.val(),o=y.val(),p=z.val(),q=A.serialize()}function c(){w.val("").trigger("change")}function d(){!1==u.attr("data-preview-states-lock")&&(1200>=a(this).width()&&(1200=t||a(this).width()==t)&&("full"==u.attr("data-preview-state")?(u.find(".preview-document").show(),u.find(".sidebar").hide(),u.find(".slide-left").hide(),u.find(".slide-right").show(),u.attr("data-preview-states",3),u.attr("data-preview-state","full"),u.attr("data-from-preview-state","sidebar"),u.addClass("static")):"closed"==u.attr("data-preview-state")&&a(this).width()!==t?(u.find(".preview-document").hide(),u.find(".sidebar").show(),u.find(".slide-left").show(),u.find(".slide-right").hide(),u.attr("data-preview-states",3),u.attr("data-preview-state","closed"),u.attr("data-from-preview-state",""),u.removeClass("static")):(u.find(".preview-document, .sidebar").show(),u.find(".slide-left, .slide-right").show(),u.attr("data-preview-states",3),u.attr("data-preview-state","sidebar"),u.attr("data-from-preview-state",""),u.removeClass("static")))),t=a(this).width()}function e(a){window.scrollTo(0,0);let b=a;setTimeout(function(){b.addClass("static")},300)}function f(b,c){g();let d=a(b.target);if(!i(d.attr("name"))){if(d.hasClass("remove-requirement")||"disable_for"==d.attr("id"))return;if(-1!==jQuery.inArray(b.type,["keyup","paste"])){if(d.is("input[type=\"checkbox\"], select"))return;c="keyup"==b.type?1e3:0}h(c)}}function g(b){a(".preview-data-wrapper .save-settings p").css("margin-right","0")}function h(a){a="number"==typeof a?a:0,b(),clearTimeout(r),r=setTimeout(function(){j()},a)}function i(b){let c=!1;if(!b)return c;let d=b.includes("[")?b.match(/\[(.*?)\]/)[1]:b;return-1!==a.inArray(d,wpo_wcpdf_admin.preview_excluded_settings)&&(c=!0),c}function j(){let b=wpo_wcpdf_admin.pdfjs_worker,c="preview-canvas",d={action:"wpo_wcpdf_preview",security:p,order_id:m,document_type:n,output_format:o,data:q};v.children(".notice").remove(),v.block({message:null,overlayCSS:{background:"#fff",opacity:.6}}),B=a.ajax({type:"POST",url:wpo_wcpdf_admin.ajaxurl,data:d,beforeSend:function(a,b){null!=B&&B.abort()},success:function(d,e,f){if(d.data.error)a("#"+c).remove(),v.append("

"+d.data.error+"

");else if(d.data.preview_data&&d.data.output_format)switch(a("#"+c).remove(),d.data.output_format){default:case"pdf":v.append(""),k(b,c,d.data.preview_data);break;case"ubl":let a=d.data.preview_data,e=a.replace(/&/g,"&").replace(//g,">").replace(/ /g," ").replace(/\n/g,"
");v.html("
"+e+"
")}v.unblock()},error:function(b,d,e){if("abort"!=d){let d=b.status+": "+b.statusText;a("#"+c).remove(),v.append("

"+d+"

"),v.unblock()}}})}function k(a,b,c){c=window.atob(c),pdfjsLib.GlobalWorkerOptions.workerSrc=a;let d=pdfjsLib.getDocument({data:c});d.promise.then(function(a){let c=1;a.getPage(1).then(function(a){let c=2,d=a.getViewport({scale:2}),e=document.getElementById(b),f=e.getContext("2d");e.height=d.height,e.width=d.width;let g={canvasContext:f,viewport:d},h=a.render(g);h.promise.then(function(){})})},function(a){console.error(a)})}function l(b){let c=b.closest(".preview-data").find("#preview-order-search-results"),d=b.val(),e=b.data("nonce"),f="wpo_wcpdf_preview_order_search",g={security:e,action:f,search:d,document_type:n};c.parent().find("img.preview-order-search-clear").hide(),c.children(".error").remove(),c.children("a").remove(),c.hide(),a.ajax({type:"POST",url:wpo_wcpdf_admin.ajaxurl,data:g,success:function(d){d.data&&(d.data.error?(c.append(""+d.data.error+""),c.show()):a.each(d.data,function(a,b){let d="#"+b.order_number+" - "+b.billing_first_name+" "+b.billing_last_name;0"+b.date_created+""+b.total+"";c.append(d+e),c.show()})),b.removeClass("ajax-waiting"),b.closest("div").find("img.preview-order-search-clear").show()}})}a(".wcpdf-extensions .more").hide(),a(".wcpdf-extensions > li").on("click",function(b){a(this).toggleClass("expanded"),a(this).find(".more").slideToggle()}),a(".edit-next-number").on("click",function(b){a(this).hide(),a(this).siblings("input").prop("disabled",!1),a(this).siblings(".save-next-number.button").show()}),a(".save-next-number").on("click",function(b){$input=a(this).siblings("input"),$input.addClass("ajax-waiting");let c=$input.val();if(0 h2").on("click",function(){a(this).parent().find("ul").toggleClass("active")}),a.each(wpo_wcpdf_admin.pointers,function(b,c){a(c.target).pointer({content:c.content,position:{edge:c.position.edge,align:c.position.align},pointerClass:c.pointer_class,pointerWidth:c.pointer_width,close:function(){jQuery.post(wpo_wcpdf_admin.ajaxurl,{pointer:b,action:"dismiss-wp-pointer"})}}),-1===a.inArray(b,wpo_wcpdf_admin.dismissed_pointers.split(","))&&a(c.target).pointer("open")}),a(".woocommerce-help-tip").tipTip({attribute:"data-tip",fadeIn:50,fadeOut:50,delay:200}),a("#wpo-wcpdf-preview-wrapper #due_date").on("change",function(){const b=a("#wpo-wcpdf-preview-wrapper #due_date"),c=a("#wpo-wcpdf-preview-wrapper #due_date_days");b.is(":checked")?c.prop("disabled",!1):c.prop("disabled",!0)}).trigger("change");let m,n,o,p,q,r,s,t,u=a("#wpo-wcpdf-preview-wrapper"),v=a("#wpo-wcpdf-preview-wrapper .preview"),w=a("#wpo-wcpdf-preview-wrapper input[name=\"order_id\"]"),x=a("#wpo-wcpdf-preview-wrapper input[name=\"document_type\"]"),y=a("#wpo-wcpdf-preview-wrapper input[name=\"output_format\"]"),z=a("#wpo-wcpdf-preview-wrapper input[name=\"nonce\"]"),A=a("#wpo-wcpdf-settings"),B=null;(function a(){x.val(x.data("default")).trigger("change")})(),c(),b(),t=a(window).width(),d(),a(window).on("resize",d),a(".slide-left").on("click",function(){let a=u.attr("data-preview-states"),b=u.attr("data-preview-state");u.find(".preview-data-wrapper ul").removeClass("active"),3==a?"closed"==b?(u.find(".preview-document").show(),u.find(".slide-right").show(),u.attr("data-preview-state","sidebar"),u.attr("data-from-preview-state","closed")):(u.find(".slide-left").hide(),u.find(".sidebar").delay(300).hide(0),u.attr("data-preview-state","full"),u.attr("data-from-preview-state","sidebar"),e(u)):(u.find(".preview-document").show(),u.find(".slide-left").hide(),u.find(".slide-right").show(),u.attr("data-preview-state","full"),u.attr("data-from-preview-state","closed"),e(u))}),a(".slide-right").on("click",function(){let a=u.attr("data-preview-states"),b=u.attr("data-preview-state");u.find(".preview-data-wrapper ul").removeClass("active"),3==a?"full"==b?(u.find(".slide-left").delay(400).show(0),u.find(".sidebar").show(),u.attr("data-preview-state","sidebar"),u.attr("data-from-preview-state","full")):(u.find(".preview-document").hide(300),u.find(".slide-right").hide(),u.attr("data-preview-state","closed"),u.attr("data-from-preview-state","sidebar")):(u.find(".preview-document").hide(300),u.find(".slide-left").show(),u.find(".slide-right").hide(),u.attr("data-preview-state","closed"),u.attr("data-from-preview-state","full")),u.removeClass("static")}),a(".preview-document .preview-data p").on("click",function(){let b=a(this).closest(".preview-data");b.siblings(".preview-data").find("ul").removeClass("active"),b.find("ul").toggleClass("active")}),a(".preview-document .preview-data ul > li").on("click",function(){let b=a(this).closest(".preview-data");b.find("ul").toggleClass("active"),a(this).hasClass("order-search")?(b.find("p.last-order").hide(),b.find("input[name=\"preview-order-search\"]").addClass("active"),b.find("p.order-search").show().find(".order-search-label").text(a(this).text())):(b.find("p.last-order").show(),b.find("p.order-search").hide(),b.find("input[name=\"preview-order-search\"]").removeClass("active").val(""),b.find("#preview-order-search-results").hide(),b.find("img.preview-order-search-clear").hide(),c(),h())}),h(),a(document).on("wpo-wcpdf-settings-changed",function(a,b){g(),h(b)}),a(document).on("wpo-wcpdf-refresh-preview wpo_wcpdf_refresh_preview",function(a,b){h(b)}),a(document).on("click","#preview-order-search-results a",function(b){b.preventDefault(),a(".preview-document .order-search-label").text("#"+a(this).data("order_id")),w.val(a(this).data("order_id")).trigger("change"),a(this).closest("div").hide(),a(this).closest("div").children("a").remove(),h()}),a(document).on("keyup paste","#wpo-wcpdf-settings input, #wpo-wcpdf-settings textarea",f),a(document).on("change","#wpo-wcpdf-settings input[type=\"checkbox\"], #wpo-wcpdf-settings input[type=\"radio\"], #wpo-wcpdf-settings select",function(a){a.isTrigger||f(a)}),a(document).on("select2:select select2:unselect","#wpo-wcpdf-settings select.wc-enhanced-select",f),a(document.body).on("wpo-wcpdf-media-upload-setting-updated",f),a(document).on("click",".wpo_remove_image_button, #wpo-wcpdf-settings .remove-requirement",f),a(document.body).on("click",".preview-data-wrapper .save-settings p input",function(b){a("#wpo-wcpdf-settings input#submit").trigger("click")}),a(document).on("click","img.preview-order-search-clear",function(b){b.preventDefault(),a(this).closest("div").find("input#preview-order-search").val(""),a(this).closest(".preview-data").find("#preview-order-search-results").children("a").remove(),a(this).closest(".preview-data").find("#preview-order-search-results").children(".error").remove(),a(this).closest(".preview-data").find("#preview-order-search-results").hide(),a(this).hide()}),a("#wpo-wcpdf-preview-wrapper ul.preview-data-option-list li").on("click",function(){let b=a(this).closest("ul").data("input-name"),c=a("#wpo-wcpdf-preview-wrapper :input[name="+b+"]");c.val(a(this).data("value")).trigger("change")}),x.on("change",function(){let b=a(this).val();if(b.length){let c=a(this).attr("name"),d=a("#wpo-wcpdf-preview-wrapper ul.preview-data-option-list[data-input-name="+c+"]"),e=d.find("li[data-value="+b+"]");d.parent().find(".current-label").text(e.text()),h()}}).trigger("change"),w.on("change",function(){h()}).trigger("change"),a("#preview-order-search").on("keyup paste",function(c){let d=a(this);d.addClass("ajax-waiting");let e="keyup"==c.type?1e3:0;b(),clearTimeout(s),s=setTimeout(function(){l(d)},e)}),function b(){a(".settings_category").not("#general").find(".form-table").hide(),a("#general > h2").addClass("active"),a(".settings_category h2").each(function(b){const c=localStorage.getItem("wcpdf_accordion_state_"+b);"true"===c&&a(this).addClass("active").next(".form-table").show()}),a(".settings_category h2").on("click",function(){const b=a(".settings_category h2").index(this);a(this).toggleClass("active").next(".form-table").slideToggle("fast",function(){const c=a(this).is(":visible");localStorage.setItem("wcpdf_accordion_state_"+b,c)})})}()}); \ No newline at end of file +jQuery(function(a){function b(){m=w.val(),n=x.val(),o=y.val(),p=z.val(),q=A.serialize()}function c(){w.val("").trigger("change")}function d(){!1==u.attr("data-preview-states-lock")&&(1200>=a(this).width()&&(1200=t||a(this).width()==t)&&("full"==u.attr("data-preview-state")?(u.find(".preview-document").show(),u.find(".sidebar").hide(),u.find(".slide-left").hide(),u.find(".slide-right").show(),u.attr("data-preview-states",3),u.attr("data-preview-state","full"),u.attr("data-from-preview-state","sidebar"),u.addClass("static")):"closed"==u.attr("data-preview-state")&&a(this).width()!==t?(u.find(".preview-document").hide(),u.find(".sidebar").show(),u.find(".slide-left").show(),u.find(".slide-right").hide(),u.attr("data-preview-states",3),u.attr("data-preview-state","closed"),u.attr("data-from-preview-state",""),u.removeClass("static")):(u.find(".preview-document, .sidebar").show(),u.find(".slide-left, .slide-right").show(),u.attr("data-preview-states",3),u.attr("data-preview-state","sidebar"),u.attr("data-from-preview-state",""),u.removeClass("static")))),t=a(this).width()}function e(a){window.scrollTo(0,0);let b=a;setTimeout(function(){b.addClass("static")},300)}function f(b,c){g();let d=a(b.target);if(!i(d.attr("name"))){if(d.hasClass("remove-requirement")||"disable_for"==d.attr("id"))return;if(-1!==jQuery.inArray(b.type,["keyup","paste"])){if(d.is("input[type=\"checkbox\"], select"))return;c="keyup"==b.type?1e3:0}h(c)}}function g(b){a(".preview-data-wrapper .save-settings p").css("margin-right","0")}function h(a){a="number"==typeof a?a:0,b(),clearTimeout(r),r=setTimeout(function(){j()},a)}function i(b){let c=!1;if(!b)return c;let d=b.includes("[")?b.match(/\[(.*?)\]/)[1]:b;return-1!==a.inArray(d,wpo_wcpdf_admin.preview_excluded_settings)&&(c=!0),c}function j(){let b=wpo_wcpdf_admin.pdfjs_worker,c="preview-canvas",d={action:"wpo_wcpdf_preview",security:p,order_id:m,document_type:n,output_format:o,data:q};v.children(".notice").remove(),v.block({message:null,overlayCSS:{background:"#fff",opacity:.6}}),B=a.ajax({type:"POST",url:wpo_wcpdf_admin.ajaxurl,data:d,beforeSend:function(a,b){null!=B&&B.abort()},success:function(d,e,f){if(d.data.error)a("#"+c).remove(),v.append("

"+d.data.error+"

");else if(d.data.preview_data&&d.data.output_format)switch(a("#"+c).remove(),d.data.output_format){default:case"pdf":v.append(""),k(b,c,d.data.preview_data);break;case"ubl":let a=d.data.preview_data,e=a.replace(/&/g,"&").replace(//g,">").replace(/ /g," ").replace(/\n/g,"
");v.html("
"+e+"
")}v.unblock()},error:function(b,d,e){if("abort"!=d){let d=b.status+": "+b.statusText;a("#"+c).remove(),v.append("

"+d+"

"),v.unblock()}}})}function k(a,b,c){c=window.atob(c),pdfjsLib.GlobalWorkerOptions.workerSrc=a;let d=pdfjsLib.getDocument({data:c});d.promise.then(function(a){let c=1;a.getPage(1).then(function(a){let c=2,d=a.getViewport({scale:2}),e=document.getElementById(b),f=e.getContext("2d");e.height=d.height,e.width=d.width;let g={canvasContext:f,viewport:d},h=a.render(g);h.promise.then(function(){})})},function(a){console.error(a)})}function l(b){let c=b.closest(".preview-data").find("#preview-order-search-results"),d=b.val(),e=b.data("nonce"),f="wpo_wcpdf_preview_order_search",g={security:e,action:f,search:d,document_type:n};c.parent().find("img.preview-order-search-clear").hide(),c.children(".error").remove(),c.children("a").remove(),c.hide(),a.ajax({type:"POST",url:wpo_wcpdf_admin.ajaxurl,data:g,success:function(d){d.data&&(d.data.error?(c.append(""+d.data.error+""),c.show()):a.each(d.data,function(a,b){let d="#"+b.order_number+" - "+b.billing_first_name+" "+b.billing_last_name;0"+b.date_created+""+b.total+"";c.append(d+e),c.show()})),b.removeClass("ajax-waiting"),b.closest("div").find("img.preview-order-search-clear").show()}})}a(".wcpdf-extensions .more").hide(),a(".wcpdf-extensions > li").on("click",function(b){a(this).toggleClass("expanded"),a(this).find(".more").slideToggle()}),a(".edit-next-number").on("click",function(b){a(this).hide(),a(this).siblings("input").prop("disabled",!1),a(this).siblings(".save-next-number.button").show()}),a(".save-next-number").on("click",function(b){$input=a(this).siblings("input"),$input.addClass("ajax-waiting");let c=$input.val();if(0 h2").on("click",function(){a(this).parent().find("ul").toggleClass("active")}),a.each(wpo_wcpdf_admin.pointers,function(b,c){a(c.target).pointer({content:c.content,position:{edge:c.position.edge,align:c.position.align},pointerClass:c.pointer_class,pointerWidth:c.pointer_width,close:function(){jQuery.post(wpo_wcpdf_admin.ajaxurl,{pointer:b,action:"dismiss-wp-pointer"})}}),-1===a.inArray(b,wpo_wcpdf_admin.dismissed_pointers.split(","))&&a(c.target).pointer("open")}),a(".woocommerce-help-tip").tipTip({attribute:"data-tip",fadeIn:50,fadeOut:50,delay:200}),a("#wpo-wcpdf-preview-wrapper #due_date").on("change",function(){const b=a("#wpo-wcpdf-preview-wrapper #due_date"),c=a("#wpo-wcpdf-preview-wrapper #due_date_days");b.is(":checked")?c.prop("disabled",!1):c.prop("disabled",!0)}).trigger("change");let m,n,o,p,q,r,s,t,u=a("#wpo-wcpdf-preview-wrapper"),v=a("#wpo-wcpdf-preview-wrapper .preview"),w=a("#wpo-wcpdf-preview-wrapper input[name=\"order_id\"]"),x=a("#wpo-wcpdf-preview-wrapper input[name=\"document_type\"]"),y=a("#wpo-wcpdf-preview-wrapper input[name=\"output_format\"]"),z=a("#wpo-wcpdf-preview-wrapper input[name=\"nonce\"]"),A=a("#wpo-wcpdf-settings"),B=null;(function a(){x.val(x.data("default")).trigger("change")})(),c(),b(),t=a(window).width(),d(),a(window).on("resize",d),a(".slide-left").on("click",function(){let a=u.attr("data-preview-states"),b=u.attr("data-preview-state");u.find(".preview-data-wrapper ul").removeClass("active"),3==a?"closed"==b?(u.find(".preview-document").show(),u.find(".slide-right").show(),u.attr("data-preview-state","sidebar"),u.attr("data-from-preview-state","closed")):(u.find(".slide-left").hide(),u.find(".sidebar").delay(300).hide(0),u.attr("data-preview-state","full"),u.attr("data-from-preview-state","sidebar"),e(u)):(u.find(".preview-document").show(),u.find(".slide-left").hide(),u.find(".slide-right").show(),u.attr("data-preview-state","full"),u.attr("data-from-preview-state","closed"),e(u))}),a(".slide-right").on("click",function(){let a=u.attr("data-preview-states"),b=u.attr("data-preview-state");u.find(".preview-data-wrapper ul").removeClass("active"),3==a?"full"==b?(u.find(".slide-left").delay(400).show(0),u.find(".sidebar").show(),u.attr("data-preview-state","sidebar"),u.attr("data-from-preview-state","full")):(u.find(".preview-document").hide(300),u.find(".slide-right").hide(),u.attr("data-preview-state","closed"),u.attr("data-from-preview-state","sidebar")):(u.find(".preview-document").hide(300),u.find(".slide-left").show(),u.find(".slide-right").hide(),u.attr("data-preview-state","closed"),u.attr("data-from-preview-state","full")),u.removeClass("static")}),a(".preview-document .preview-data p").on("click",function(){let b=a(this).closest(".preview-data");b.siblings(".preview-data").find("ul").removeClass("active"),b.find("ul").toggleClass("active")}),a(".preview-document .preview-data ul > li").on("click",function(){let b=a(this).closest(".preview-data");b.find("ul").toggleClass("active"),a(this).hasClass("order-search")?(b.find("p.last-order").hide(),b.find("input[name=\"preview-order-search\"]").addClass("active"),b.find("p.order-search").show().find(".order-search-label").text(a(this).text())):(b.find("p.last-order").show(),b.find("p.order-search").hide(),b.find("input[name=\"preview-order-search\"]").removeClass("active").val(""),b.find("#preview-order-search-results").hide(),b.find("img.preview-order-search-clear").hide(),c(),h())}),h(),a(document).on("wpo-wcpdf-settings-changed",function(a,b){g(),h(b)}),a(document).on("wpo-wcpdf-refresh-preview wpo_wcpdf_refresh_preview",function(a,b){h(b)}),a(document).on("click","#preview-order-search-results a",function(b){b.preventDefault(),a(".preview-document .order-search-label").text("#"+a(this).data("order_id")),w.val(a(this).data("order_id")).trigger("change"),a(this).closest("div").hide(),a(this).closest("div").children("a").remove(),h()}),a(document).on("keyup paste","#wpo-wcpdf-settings input, #wpo-wcpdf-settings textarea",f),a(document).on("change","#wpo-wcpdf-settings input[type=\"checkbox\"], #wpo-wcpdf-settings input[type=\"radio\"], #wpo-wcpdf-settings select",function(a){a.isTrigger||f(a)}),a(document).on("select2:select select2:unselect","#wpo-wcpdf-settings select.wc-enhanced-select",f),a(document.body).on("wpo-wcpdf-media-upload-setting-updated",f),a(document).on("click",".wpo_remove_image_button, #wpo-wcpdf-settings .remove-requirement",f),a(document.body).on("click",".preview-data-wrapper .save-settings p input",function(b){a("#wpo-wcpdf-settings input#submit").trigger("click")}),a(document).on("click","img.preview-order-search-clear",function(b){b.preventDefault(),a(this).closest("div").find("input#preview-order-search").val(""),a(this).closest(".preview-data").find("#preview-order-search-results").children("a").remove(),a(this).closest(".preview-data").find("#preview-order-search-results").children(".error").remove(),a(this).closest(".preview-data").find("#preview-order-search-results").hide(),a(this).hide()}),a("#wpo-wcpdf-preview-wrapper ul.preview-data-option-list li").on("click",function(){let b=a(this).closest("ul").data("input-name"),c=a("#wpo-wcpdf-preview-wrapper :input[name="+b+"]");c.val(a(this).data("value")).trigger("change")}),x.on("change",function(){let b=a(this).val();if(b.length){let c=a(this).attr("name"),d=a("#wpo-wcpdf-preview-wrapper ul.preview-data-option-list[data-input-name="+c+"]"),e=d.find("li[data-value="+b+"]");d.parent().find(".current-label").text(e.text()),h()}}).trigger("change"),w.on("change",function(){h()}).trigger("change"),a("#preview-order-search").on("keyup paste",function(c){let d=a(this);d.addClass("ajax-waiting");let e="keyup"==c.type?1e3:0;b(),clearTimeout(s),s=setTimeout(function(){l(d)},e)}),function b(){a(".settings_category").not("#general").find(".form-table").hide(),a("#general > h2").addClass("active"),a(".settings_category h2").each(function(b){const c=localStorage.getItem("wcpdf_accordion_state_"+b);"true"===c&&a(this).addClass("active").next(".form-table").show()}),a(".settings_category h2").on("click",function(){const b=a(".settings_category h2").index(this);a(this).toggleClass("active").next(".form-table").slideToggle("fast",function(){const c=a(this).is(":visible");localStorage.setItem("wcpdf_accordion_state_"+b,c)})})}()}); \ No newline at end of file diff --git a/includes/Documents/Invoice.php b/includes/Documents/Invoice.php index 7c3d6203f..7dd214c58 100644 --- a/includes/Documents/Invoice.php +++ b/includes/Documents/Invoice.php @@ -579,7 +579,7 @@ public function get_ubl_settings_fields( $option_name ) { $settings_fields = array( array( 'type' => 'section', - 'id' => $this->type.'_ubl', + 'id' => $this->type . '_ubl', 'title' => '', 'callback' => 'section', ), @@ -588,18 +588,32 @@ public function get_ubl_settings_fields( $option_name ) { 'id' => 'enabled', 'title' => __( 'Enable', 'woocommerce-pdf-invoices-packing-slips' ), 'callback' => 'checkbox', - 'section' => $this->type.'_ubl', + 'section' => $this->type . '_ubl', 'args' => array( 'option_name' => $option_name, 'id' => 'enabled', ) ), + array( + 'type' => 'setting', + 'id' => 'ubl_format', + 'title' => __( 'Format', 'woocommerce-pdf-invoices-packing-slips' ), + 'callback' => 'select', + 'section' => $this->type . '_ubl', + 'args' => array( + 'option_name' => $option_name, + 'id' => 'ubl_format', + 'options' => apply_filters( 'wpo_wcpdf_document_ubl_settings_formats', array( + 'ubl_2_1' => __( 'UBL 2.1' , 'woocommerce-pdf-invoices-packing-slips' ), + ), $this ), + ) + ), array( 'type' => 'setting', 'id' => 'attach_to_email_ids', 'title' => __( 'Attach to:', 'woocommerce-pdf-invoices-packing-slips' ), 'callback' => 'multiple_checkboxes', - 'section' => $this->type.'_ubl', + 'section' => $this->type . '_ubl', 'args' => array( 'option_name' => $option_name, 'id' => 'attach_to_email_ids', @@ -613,11 +627,11 @@ public function get_ubl_settings_fields( $option_name ) { 'id' => 'include_encrypted_pdf', 'title' => __( 'Include encrypted PDF:', 'woocommerce-pdf-invoices-packing-slips' ), 'callback' => 'checkbox', - 'section' => $this->type.'_ubl', + 'section' => $this->type . '_ubl', 'args' => array( 'option_name' => $option_name, 'id' => 'include_encrypted_pdf', - 'description' => __( 'Include the PDF Invoice file encrypted in the UBL file.', 'woocommerce-pdf-invoices-packing-slips' ), + 'description' => __( 'Embed the encrypted PDF invoice file within the UBL document. Note that this option may not be supported by all UBL formats.', 'woocommerce-pdf-invoices-packing-slips' ), ) ), ); @@ -687,6 +701,7 @@ public function get_settings_categories( string $output_format ): array { 'title' => __( 'General', 'woocommerce-pdf-invoices-packing-slips' ), 'members' => array( 'enabled', + 'ubl_format', 'attach_to_email_ids', 'include_encrypted_pdf', ), diff --git a/includes/Documents/OrderDocument.php b/includes/Documents/OrderDocument.php index 0ab5edcb6..2e2490f3c 100644 --- a/includes/Documents/OrderDocument.php +++ b/includes/Documents/OrderDocument.php @@ -342,6 +342,17 @@ public function is_enabled( $output_format = 'pdf' ) { return apply_filters( 'wpo_wcpdf_document_is_enabled', $is_enabled, $this->type, $output_format ); } + + /** + * Get the UBL format + * + * @return string|false + */ + public function get_ubl_format() { + $ubl_format = $this->get_setting( 'ubl_format', false, 'ubl' ); + + return apply_filters( 'wpo_wcpdf_document_ubl_format', $ubl_format, $this ); + } public function get_hook_prefix() { return 'wpo_wcpdf_' . $this->slug . '_get_'; diff --git a/includes/Settings/SettingsGeneral.php b/includes/Settings/SettingsGeneral.php index 5e80c9506..da9bab033 100644 --- a/includes/Settings/SettingsGeneral.php +++ b/includes/Settings/SettingsGeneral.php @@ -207,7 +207,7 @@ public function init_settings() { 'option_name' => $option_name, 'id' => 'shop_phone_number', 'translatable' => true, -// 'description' => __( 'Required for e-Invoice.', 'woocommerce-pdf-invoices-packing-slips' ), + 'description' => __( 'Mandatory for certain UBL formats.', 'woocommerce-pdf-invoices-packing-slips' ), ) ), array( diff --git a/ubl/Builders/SabreBuilder.php b/ubl/Builders/SabreBuilder.php index 6b8676de7..f9eda847a 100644 --- a/ubl/Builders/SabreBuilder.php +++ b/ubl/Builders/SabreBuilder.php @@ -23,7 +23,7 @@ public function build( Document $document ) { $namespaces = array_flip( $document->get_namespaces() ); $this->service->namespaceMap = $namespaces; - return $this->service->write( 'Invoice', $document->get_data() ); + return $this->service->write( $document->get_root_element(), $document->get_data() ); } } diff --git a/ubl/Documents/Document.php b/ubl/Documents/Document.php index 2ce96d446..c9b38bca9 100644 --- a/ubl/Documents/Document.php +++ b/ubl/Documents/Document.php @@ -31,6 +31,7 @@ public function set_order_document( OrderDocument $order_document ) { $this->order_document = $order_document; } + abstract public function get_root_element(); abstract public function get_format(); abstract public function get_namespaces(); abstract public function get_data(); diff --git a/ubl/Documents/UblDocument.php b/ubl/Documents/UblDocument.php index cfa85f15d..6d48cfbd8 100644 --- a/ubl/Documents/UblDocument.php +++ b/ubl/Documents/UblDocument.php @@ -9,6 +9,10 @@ } class UblDocument extends Document { + + public function get_root_element() { + return apply_filters( 'wpo_wc_ubl_document_root_element', 'Invoice', $this ); + } public function get_format() { $format = apply_filters( 'wpo_wc_ubl_document_format' , array( @@ -82,7 +86,7 @@ public function get_format() { 'enabled' => true, 'handler' => \WPO\IPS\UBL\Handlers\Invoice\InvoiceLineHandler::class, ), - ) ); + ), $this ); foreach ( $format as $key => $element ) { if ( false === $element['enabled'] ) { @@ -98,7 +102,7 @@ public function get_namespaces() { 'cac' => 'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2', 'cbc' => 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2', '' => 'urn:oasis:names:specification:ubl:schema:xsd:Invoice-2', - ) ); + ), $this ); } public function get_data() {