diff --git a/assets/js/admin-script.js b/assets/js/admin-script.js index f7feece13..e8fc15a2e 100644 --- a/assets/js/admin-script.js +++ b/assets/js/admin-script.js @@ -105,6 +105,28 @@ jQuery( function( $ ) { 'delay': 200 } ); + function toggle_due_date_custom_days_field() { + const due_date_value = $( '#due_date' ).val(); + const due_date_custom_days = $( '#due_date_custom_days' ); + const due_date_base_date = $( '#due_date_base_date' ); + + if ( '' === due_date_value ) { + due_date_custom_days.closest( 'tr' ).hide(); + due_date_base_date.closest( 'tr' ).hide(); + } + else if ( 'custom' === due_date_value ) { + due_date_custom_days.closest( 'tr' ).show(); + due_date_base_date.closest( 'tr' ).show(); + } else { + due_date_custom_days.closest( 'tr' ).hide(); + due_date_base_date.closest( 'tr' ).show(); + } + } + + toggle_due_date_custom_days_field(); + + $( '#due_date' ).change( toggle_due_date_custom_days_field ); + //----------> Preview <----------// // objects let $previewWrapper = $( '#wpo-wcpdf-preview-wrapper' ); diff --git a/assets/js/admin-script.min.js b/assets/js/admin-script.min.js index 35e9817f6..653956e88 100644 --- a/assets/js/admin-script.min.js +++ b/assets/js/admin-script.min.js @@ -1 +1 @@ -jQuery(function(a){function b(){n=x.val(),o=y.val(),p=z.val(),q=A.val(),r=B.serialize()}function c(){y.val(y.data("default")).trigger("change")}function d(){x.val("").trigger("change")}function e(){!1==v.attr("data-preview-states-lock")&&(1200>=a(this).width()&&(1200=u||a(this).width()==u)&&("full"==v.attr("data-preview-state")?(v.find(".preview-document").show(),v.find(".sidebar").hide(),v.find(".slide-left").hide(),v.find(".slide-right").show(),v.attr("data-preview-states",3),v.attr("data-preview-state","full"),v.attr("data-from-preview-state","sidebar"),v.addClass("static")):"closed"==v.attr("data-preview-state")&&a(this).width()!==u?(v.find(".preview-document").hide(),v.find(".sidebar").show(),v.find(".slide-left").show(),v.find(".slide-right").hide(),v.attr("data-preview-states",3),v.attr("data-preview-state","closed"),v.attr("data-from-preview-state",""),v.removeClass("static")):(v.find(".preview-document, .sidebar").show(),v.find(".slide-left, .slide-right").show(),v.attr("data-preview-states",3),v.attr("data-preview-state","sidebar"),v.attr("data-from-preview-state",""),v.removeClass("static")))),u=a(this).width()}function f(a){window.scrollTo(0,0);let b=a;setTimeout(function(){b.addClass("static")},300)}function g(b,c){h();let d=a(b.target);if(!j(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}i(c)}}function h(b){a(".preview-data-wrapper .save-settings p").css("margin-right","0")}function i(a){a="number"==typeof a?a:0,b(),clearTimeout(s),s=setTimeout(function(){k()},a)}function j(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 k(){let b=wpo_wcpdf_admin.pdfjs_worker,c="preview-canvas",d={action:"wpo_wcpdf_preview",security:q,order_id:n,document_type:o,output_format:p,data:r};w.children(".notice").remove(),w.block({message:null,overlayCSS:{background:"#fff",opacity:.6}}),C=a.ajax({type:"POST",url:wpo_wcpdf_admin.ajaxurl,data:d,beforeSend:function(a,b){null!=C&&C.abort()},success:function(d,e,f){if(d.data.error)a("#"+c).remove(),w.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":w.append(""),l(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,"
");w.html("
"+e+"
")}w.unblock()},error:function(b,d,e){if("abort"!=d){let d=b.status+": "+b.statusText;a("#"+c).remove(),w.append("

"+d+"

"),w.unblock()}}})}function l(a,b,c){c=window.atob(c);let d=window["pdfjs-dist/build/pdf"];d.GlobalWorkerOptions.workerSrc=a;let e=d.getDocument({data:c});e.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 m(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:o};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});let n,o,p,q,r,s,t,u,v=a("#wpo-wcpdf-preview-wrapper"),w=a("#wpo-wcpdf-preview-wrapper .preview"),x=a("#wpo-wcpdf-preview-wrapper input[name=\"order_id\"]"),y=a("#wpo-wcpdf-preview-wrapper input[name=\"document_type\"]"),z=a("#wpo-wcpdf-preview-wrapper input[name=\"output_format\"]"),A=a("#wpo-wcpdf-preview-wrapper input[name=\"nonce\"]"),B=a("#wpo-wcpdf-settings"),C=null;a(document).ready(function(){c(),d(),b(),u=a(window).width(),e()}),a(window).on("resize",e),a(".slide-left").on("click",function(){let a=v.attr("data-preview-states"),b=v.attr("data-preview-state");v.find(".preview-data-wrapper ul").removeClass("active"),3==a?"closed"==b?(v.find(".preview-document").show(),v.find(".slide-right").show(),v.attr("data-preview-state","sidebar"),v.attr("data-from-preview-state","closed")):(v.find(".slide-left").hide(),v.find(".sidebar").delay(300).hide(0),v.attr("data-preview-state","full"),v.attr("data-from-preview-state","sidebar"),f(v)):(v.find(".preview-document").show(),v.find(".slide-left").hide(),v.find(".slide-right").show(),v.attr("data-preview-state","full"),v.attr("data-from-preview-state","closed"),f(v))}),a(".slide-right").on("click",function(){let a=v.attr("data-preview-states"),b=v.attr("data-preview-state");v.find(".preview-data-wrapper ul").removeClass("active"),3==a?"full"==b?(v.find(".slide-left").delay(400).show(0),v.find(".sidebar").show(),v.attr("data-preview-state","sidebar"),v.attr("data-from-preview-state","full")):(v.find(".preview-document").hide(300),v.find(".slide-right").hide(),v.attr("data-preview-state","closed"),v.attr("data-from-preview-state","sidebar")):(v.find(".preview-document").hide(300),v.find(".slide-left").show(),v.find(".slide-right").hide(),v.attr("data-preview-state","closed"),v.attr("data-from-preview-state","full")),v.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(),d(),i())}),a(document).ready(i()),a(document).on("wpo-wcpdf-settings-changed",function(a,b){h(),i(b)}),a(document).on("wpo-wcpdf-refresh-preview wpo_wcpdf_refresh_preview",function(a,b){i(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")),x.val(a(this).data("order_id")).change(),a(this).closest("div").hide(),a(this).closest("div").children("a").remove(),i()}),a(document).on("keyup paste","#wpo-wcpdf-settings input, #wpo-wcpdf-settings textarea",g),a(document).on("change","#wpo-wcpdf-settings input[type=\"checkbox\"], #wpo-wcpdf-settings input[type=\"radio\"], #wpo-wcpdf-settings select",function(a){a.isTrigger||g(a)}),a(document).on("select2:select select2:unselect","#wpo-wcpdf-settings select.wc-enhanced-select",g),a(document.body).on("wpo-wcpdf-media-upload-setting-updated",g),a(document).on("click",".wpo_remove_image_button, #wpo-wcpdf-settings .remove-requirement",g),a(document).on("click",".preview-data-wrapper .save-settings p input",function(b){a("#wpo-wcpdf-settings input#submit").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")}),y.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()),i()}}).trigger("change"),x.on("change",function(){i()}).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(t),t=setTimeout(function(){m(d)},e)})}); \ No newline at end of file +jQuery(function(a){function b(){const b=a("#due_date").val(),c=a("#due_date_custom_days"),d=a("#due_date_base_date");""===b?(c.closest("tr").hide(),d.closest("tr").hide()):"custom"===b?(c.closest("tr").show(),d.closest("tr").show()):(c.closest("tr").hide(),d.closest("tr").show())}function c(){o=y.val(),p=z.val(),q=A.val(),r=B.val(),s=C.serialize()}function d(){z.val(z.data("default")).trigger("change")}function e(){y.val("").trigger("change")}function f(){!1==w.attr("data-preview-states-lock")&&(1200>=a(this).width()&&(1200=v||a(this).width()==v)&&("full"==w.attr("data-preview-state")?(w.find(".preview-document").show(),w.find(".sidebar").hide(),w.find(".slide-left").hide(),w.find(".slide-right").show(),w.attr("data-preview-states",3),w.attr("data-preview-state","full"),w.attr("data-from-preview-state","sidebar"),w.addClass("static")):"closed"==w.attr("data-preview-state")&&a(this).width()!==v?(w.find(".preview-document").hide(),w.find(".sidebar").show(),w.find(".slide-left").show(),w.find(".slide-right").hide(),w.attr("data-preview-states",3),w.attr("data-preview-state","closed"),w.attr("data-from-preview-state",""),w.removeClass("static")):(w.find(".preview-document, .sidebar").show(),w.find(".slide-left, .slide-right").show(),w.attr("data-preview-states",3),w.attr("data-preview-state","sidebar"),w.attr("data-from-preview-state",""),w.removeClass("static")))),v=a(this).width()}function g(a){window.scrollTo(0,0);let b=a;setTimeout(function(){b.addClass("static")},300)}function h(b,c){i();let d=a(b.target);if(!k(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}j(c)}}function i(b){a(".preview-data-wrapper .save-settings p").css("margin-right","0")}function j(a){a="number"==typeof a?a:0,c(),clearTimeout(t),t=setTimeout(function(){l()},a)}function k(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 l(){let b=wpo_wcpdf_admin.pdfjs_worker,c="preview-canvas",d={action:"wpo_wcpdf_preview",security:r,order_id:o,document_type:p,output_format:q,data:s};x.children(".notice").remove(),x.block({message:null,overlayCSS:{background:"#fff",opacity:.6}}),D=a.ajax({type:"POST",url:wpo_wcpdf_admin.ajaxurl,data:d,beforeSend:function(a,b){null!=D&&D.abort()},success:function(d,e,f){if(d.data.error)a("#"+c).remove(),x.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":x.append(""),m(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,"
");x.html("
"+e+"
")}x.unblock()},error:function(b,d,e){if("abort"!=d){let d=b.status+": "+b.statusText;a("#"+c).remove(),x.append("

"+d+"

"),x.unblock()}}})}function m(a,b,c){c=window.atob(c);let d=window["pdfjs-dist/build/pdf"];d.GlobalWorkerOptions.workerSrc=a;let e=d.getDocument({data:c});e.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 n(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:p};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}),b(),a("#due_date").change(b);let o,p,q,r,s,t,u,v,w=a("#wpo-wcpdf-preview-wrapper"),x=a("#wpo-wcpdf-preview-wrapper .preview"),y=a("#wpo-wcpdf-preview-wrapper input[name=\"order_id\"]"),z=a("#wpo-wcpdf-preview-wrapper input[name=\"document_type\"]"),A=a("#wpo-wcpdf-preview-wrapper input[name=\"output_format\"]"),B=a("#wpo-wcpdf-preview-wrapper input[name=\"nonce\"]"),C=a("#wpo-wcpdf-settings"),D=null;a(document).ready(function(){d(),e(),c(),v=a(window).width(),f()}),a(window).on("resize",f),a(".slide-left").on("click",function(){let a=w.attr("data-preview-states"),b=w.attr("data-preview-state");w.find(".preview-data-wrapper ul").removeClass("active"),3==a?"closed"==b?(w.find(".preview-document").show(),w.find(".slide-right").show(),w.attr("data-preview-state","sidebar"),w.attr("data-from-preview-state","closed")):(w.find(".slide-left").hide(),w.find(".sidebar").delay(300).hide(0),w.attr("data-preview-state","full"),w.attr("data-from-preview-state","sidebar"),g(w)):(w.find(".preview-document").show(),w.find(".slide-left").hide(),w.find(".slide-right").show(),w.attr("data-preview-state","full"),w.attr("data-from-preview-state","closed"),g(w))}),a(".slide-right").on("click",function(){let a=w.attr("data-preview-states"),b=w.attr("data-preview-state");w.find(".preview-data-wrapper ul").removeClass("active"),3==a?"full"==b?(w.find(".slide-left").delay(400).show(0),w.find(".sidebar").show(),w.attr("data-preview-state","sidebar"),w.attr("data-from-preview-state","full")):(w.find(".preview-document").hide(300),w.find(".slide-right").hide(),w.attr("data-preview-state","closed"),w.attr("data-from-preview-state","sidebar")):(w.find(".preview-document").hide(300),w.find(".slide-left").show(),w.find(".slide-right").hide(),w.attr("data-preview-state","closed"),w.attr("data-from-preview-state","full")),w.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(),e(),j())}),a(document).ready(j()),a(document).on("wpo-wcpdf-settings-changed",function(a,b){i(),j(b)}),a(document).on("wpo-wcpdf-refresh-preview wpo_wcpdf_refresh_preview",function(a,b){j(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")),y.val(a(this).data("order_id")).change(),a(this).closest("div").hide(),a(this).closest("div").children("a").remove(),j()}),a(document).on("keyup paste","#wpo-wcpdf-settings input, #wpo-wcpdf-settings textarea",h),a(document).on("change","#wpo-wcpdf-settings input[type=\"checkbox\"], #wpo-wcpdf-settings input[type=\"radio\"], #wpo-wcpdf-settings select",function(a){a.isTrigger||h(a)}),a(document).on("select2:select select2:unselect","#wpo-wcpdf-settings select.wc-enhanced-select",h),a(document.body).on("wpo-wcpdf-media-upload-setting-updated",h),a(document).on("click",".wpo_remove_image_button, #wpo-wcpdf-settings .remove-requirement",h),a(document).on("click",".preview-data-wrapper .save-settings p input",function(b){a("#wpo-wcpdf-settings input#submit").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")}),z.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()),j()}}).trigger("change"),y.on("change",function(){j()}).trigger("change"),a("#preview-order-search").on("keyup paste",function(b){let d=a(this);d.addClass("ajax-waiting");let e="keyup"==b.type?1e3:0;c(),clearTimeout(u),u=setTimeout(function(){n(d)},e)})}); \ No newline at end of file diff --git a/includes/class-wcpdf-main.php b/includes/class-wcpdf-main.php index 56754df69..c5abf97c0 100644 --- a/includes/class-wcpdf-main.php +++ b/includes/class-wcpdf-main.php @@ -74,6 +74,8 @@ public function __construct() { add_filter( 'woocommerce_valid_webhook_events', array( $this, 'wc_webhook_topic_events' ) ); add_filter( 'woocommerce_webhook_topics', array( $this, 'wc_webhook_topics' ) ); add_action( 'wpo_wcpdf_save_document', array( $this, 'wc_webhook_trigger' ), 10, 2 ); + + add_action( 'wpo_wcpdf_after_order_data', array( $this, 'display_due_date' ), 10, 2 ); } /** @@ -1572,7 +1574,39 @@ public function wc_webhook_topics( $topics ) { public function wc_webhook_trigger( $document, $order ) { do_action( "wpo_wcpdf_webhook_order_{$document->slug}_saved", $order->get_id() ); } - + + /** + * @param string $document_type + * @param \WC_Order|\WC_Order_Refund $order + * + * @return void + */ + public function display_due_date( string $document_type, $order ): void { + if ( 'invoice' !== $document_type || empty( $order ) ) { + return; + } + + $invoice = wcpdf_get_invoice( $order ); + + if ( ! $invoice ) { + return; + } + + $due_date_timestamp = $invoice->get_due_date(); + + if ( 0 >= $due_date_timestamp ) { + return; + } + + $due_date = apply_filters( 'wpo_wcpdf_invoice_due_date_display', date( wcpdf_date_format( $this, 'due_date' ), $due_date_timestamp ), $due_date_timestamp, $order ); + + if ( ! empty( $due_date ) ) { + echo ' + ', __( 'Due Date:', 'woocommerce-pdf-invoices-packing-slips' ), ' + ', $due_date, ' + '; + } + } } endif; // class_exists diff --git a/includes/documents/class-wcpdf-invoice.php b/includes/documents/class-wcpdf-invoice.php index 187920173..1a1810cf4 100644 --- a/includes/documents/class-wcpdf-invoice.php +++ b/includes/documents/class-wcpdf-invoice.php @@ -333,6 +333,54 @@ public function get_pdf_settings_fields( $option_name ) { ), ) ), + array( + 'type' => 'setting', + 'id' => 'due_date', + 'title' => __( 'Display due date', 'woocommerce-pdf-invoices-packing-slips' ), + 'callback' => 'select', + 'section' => $this->type, + 'args' => array( + 'option_name' => $option_name, + 'id' => 'due_date', + 'options' => apply_filters( 'wpo_wcpdf_due_date_options', array( + '' => __( 'No', 'wpo_wcpdf_pro' ), + '1' => __( '1 day', 'wpo_wcpdf_pro' ), + '7' => __( '7 days', 'wpo_wcpdf_pro' ), + '30' => __( '30 days', 'wpo_wcpdf_pro' ), + 'custom' => __( 'Custom days', 'wpo_wcpdf_pro' ), + ) ), + 'description' => __( 'Displays a due date below the order data.', 'woocommerce-pdf-invoices-packing-slips' ), + ) + ), + array( + 'type' => 'setting', + 'id' => 'due_date_custom_days', + 'title' => __( 'Custom due date (days)', 'woocommerce-pdf-invoices-packing-slips' ), + 'callback' => 'text_input', + 'section' => $this->type, + 'args' => array( + 'option_name' => $option_name, + 'id' => 'due_date_custom_days', + 'type' => 'number', + 'description' => __( 'Enter the number of days for the due date.', 'woocommerce-pdf-invoices-packing-slips' ), + ) + ), + array( + 'type' => 'setting', + 'id' => 'due_date_base_date', + 'title' => __( 'Due date base', 'woocommerce-pdf-invoices-packing-slips' ), + 'callback' => 'select', + 'section' => $this->type, + 'args' => array( + 'option_name' => $option_name, + 'id' => 'due_date_base_date', + 'options' => apply_filters( 'wpo_wcpdf_due_date_base_date_options', array( + 'order_date' => __( 'Order date', 'woocommerce-pdf-invoices-packing-slips' ), + 'invoice_date' => __( 'Invoice date', 'woocommerce-pdf-invoices-packing-slips' ), + ) ), + 'description' => __( 'Choose the initial date from which the due date will be calculated.', 'woocommerce-pdf-invoices-packing-slips' ) + ) + ), array( 'type' => 'setting', 'id' => 'display_number', @@ -650,6 +698,37 @@ public function get_date_title() { return apply_filters( "wpo_wcpdf_{$this->slug}_date_title", $date_title, $this ); } + /** + * Returns the due date timestamp. + * + * @return int + */ + public function get_due_date(): int { + if ( empty( $this->order ) || empty( $this->settings['due_date'] ) ) { + return 0; + } + + $due_date_days = ( 'custom' === $this->settings['due_date'] ) ? intval( $this->settings['due_date_custom_days'] ?? 0 ) : absint( $this->settings['due_date'] ); + $due_date_days = apply_filters( "wpo_wcpdf_{$this->slug}_due_date_days", $due_date_days, $this ); + + if ( 0 >= $due_date_days ) { + return 0; + } + + if ( isset( $this->settings['due_date_base_date'] ) && 'invoice_date' === $this->settings['due_date_base_date'] && $this->exists() ) { + // The `get_date()` method has been used to get the date as a WC_DateTime object. + $base_date = $this->get_date( 'invoice' ); + } + + if ( empty( $base_date ) ) { + $base_date = $this->order->get_date_created(); + } + + $base_date = apply_filters( "wpo_wcpdf_{$this->slug}_due_date_base_date", $base_date, $this ); + $due_date_datetime = $base_date->modify( "+$due_date_days days" ); + + return apply_filters( "wpo_wcpdf_{$this->slug}_due_date", $due_date_datetime->getTimestamp() ?? 0, $this ); + } } endif; // class_exists