From c4177b9e0cdffae795d6e64ef8c1b67ff5e17ab7 Mon Sep 17 00:00:00 2001 From: Aparna Gawade Date: Fri, 24 Mar 2023 20:54:26 +0530 Subject: [PATCH 1/6] Add invoice display date in order meta --- .../abstract-wcpdf-order-document.php | 24 +++++++++++++++++-- includes/documents/class-wcpdf-invoice.php | 5 +++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/includes/documents/abstract-wcpdf-order-document.php b/includes/documents/abstract-wcpdf-order-document.php index f94181c7f..2e055ad2d 100644 --- a/includes/documents/abstract-wcpdf-order-document.php +++ b/includes/documents/abstract-wcpdf-order-document.php @@ -294,9 +294,10 @@ public function save( $order = null ) { $order->delete_meta_data( "_wcpdf_{$this->slug}_{$key}_data" ); // deleting the number = deleting the document, so also delete document settings $order->delete_meta_data( "_wcpdf_{$this->slug}_settings" ); - } elseif ( $key == 'notes' ) { + } elseif ( $key == 'notes' || $key == 'display_date') { $order->delete_meta_data( "_wcpdf_{$this->slug}_{$key}" ); } + } else { if ( $key == 'date' ) { // store dates as timestamp and formatted as mysql time @@ -306,10 +307,11 @@ public function save( $order = null ) { // store both formatted number and number data $order->update_meta_data( "_wcpdf_{$this->slug}_{$key}", $value->formatted_number ); $order->update_meta_data( "_wcpdf_{$this->slug}_{$key}_data", $value->to_array() ); - } elseif ( $key == 'notes' ) { + } elseif ( $key == 'notes' || $key == 'display_date' ) { // store notes $order->update_meta_data( "_wcpdf_{$this->slug}_{$key}", $value ); } + } } @@ -331,6 +333,7 @@ public function delete( $order = null ) { 'number', 'number_data', 'notes', + 'display_date', ), $this ); foreach ( $data_to_remove as $data_key ) { $order->delete_meta_data( "_wcpdf_{$this->slug}_{$data_key}" ); @@ -568,6 +571,23 @@ public function set_notes( $value, $order = null ) { } } + public function set_display_date( $value, $order = null ) { + $order = empty( $order ) ? $this->order : $order; + + try { + if ( empty( $value ) ) { + $this->data['display_date'] = null; + return; + } + + $this->data['display_date'] = $value; + } catch ( \Exception $e ) { + wcpdf_log_error( $e->getMessage() ); + } catch ( \Error $e ) { + wcpdf_log_error( $e->getMessage() ); + } + } + /* |-------------------------------------------------------------------------- | Settings getters / outputters diff --git a/includes/documents/class-wcpdf-invoice.php b/includes/documents/class-wcpdf-invoice.php index daf0e3797..e81302bf3 100644 --- a/includes/documents/class-wcpdf-invoice.php +++ b/includes/documents/class-wcpdf-invoice.php @@ -27,7 +27,7 @@ public function __construct( $order = 0 ) { $this->type = 'invoice'; $this->title = __( 'Invoice', 'woocommerce-pdf-invoices-packing-slips' ); $this->icon = WPO_WCPDF()->plugin_url() . "/assets/images/invoice.svg"; - + $this->date_preference = ''; // Call parent constructor parent::__construct( $order ); } @@ -58,8 +58,11 @@ public function init() { if ( isset( $this->settings['display_date'] ) && $this->settings['display_date'] == 'order_date' && !empty( $this->order ) ) { $this->set_date( $this->order->get_date_created() ); + $this->set_display_date( 'Order Date' ); + } elseif( empty( $this->get_date() ) ) { $this->set_date( current_time( 'timestamp', true ) ); + $this->set_display_date( 'Invoice Date' ); } $this->init_number(); From 87b6ee11bde97705770300c3de724faf5ff03105 Mon Sep 17 00:00:00 2001 From: Aparna Gawade Date: Fri, 24 Mar 2023 20:56:43 +0530 Subject: [PATCH 2/6] removed unwanted code --- includes/documents/class-wcpdf-invoice.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/documents/class-wcpdf-invoice.php b/includes/documents/class-wcpdf-invoice.php index e81302bf3..f6ad3ece0 100644 --- a/includes/documents/class-wcpdf-invoice.php +++ b/includes/documents/class-wcpdf-invoice.php @@ -27,7 +27,7 @@ public function __construct( $order = 0 ) { $this->type = 'invoice'; $this->title = __( 'Invoice', 'woocommerce-pdf-invoices-packing-slips' ); $this->icon = WPO_WCPDF()->plugin_url() . "/assets/images/invoice.svg"; - $this->date_preference = ''; + // Call parent constructor parent::__construct( $order ); } From 0a83558e59424c3d6e33c1ad536a51a9abcc5342 Mon Sep 17 00:00:00 2001 From: Aparna Gawade Date: Thu, 30 Mar 2023 22:33:31 +0530 Subject: [PATCH 3/6] changed option strings --- includes/documents/class-wcpdf-invoice.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/includes/documents/class-wcpdf-invoice.php b/includes/documents/class-wcpdf-invoice.php index f6ad3ece0..22d50194e 100644 --- a/includes/documents/class-wcpdf-invoice.php +++ b/includes/documents/class-wcpdf-invoice.php @@ -58,13 +58,13 @@ public function init() { if ( isset( $this->settings['display_date'] ) && $this->settings['display_date'] == 'order_date' && !empty( $this->order ) ) { $this->set_date( $this->order->get_date_created() ); - $this->set_display_date( 'Order Date' ); - + $this->set_display_date( 'order_date' ); } elseif( empty( $this->get_date() ) ) { $this->set_date( current_time( 'timestamp', true ) ); - $this->set_display_date( 'Invoice Date' ); + $this->set_display_date( 'invoice_date' ); } + $this->init_number(); do_action( 'wpo_wcpdf_init_document', $this ); From 7b10bc82d4009f233d2c64147840919bf028f49f Mon Sep 17 00:00:00 2001 From: Aparna Gawade Date: Mon, 3 Apr 2023 08:25:38 +0530 Subject: [PATCH 4/6] Display display date option order edit page --- includes/class-wcpdf-admin.php | 23 ++++++++++++++ .../abstract-wcpdf-order-document-methods.php | 31 +++++++++++++++++++ .../abstract-wcpdf-order-document.php | 5 +++ 3 files changed, 59 insertions(+) diff --git a/includes/class-wcpdf-admin.php b/includes/class-wcpdf-admin.php index 05e9256d0..3ddb90e97 100644 --- a/includes/class-wcpdf-admin.php +++ b/includes/class-wcpdf-admin.php @@ -472,9 +472,13 @@ public function data_input_box_content( $post_or_order_object ) { 'date' => array( 'label' => __( 'Invoice Date:', 'woocommerce-pdf-invoices-packing-slips' ), ), + 'display_date' => array( + 'label' => __( 'Display Date:', 'woocommerce-pdf-invoices-packing-slips' ), + ), 'notes' => array( 'label' => __( 'Notes (printed in the invoice):', 'woocommerce-pdf-invoices-packing-slips' ), ), + ); // output $this->output_number_date_edit_fields( $invoice, $data ); @@ -507,6 +511,13 @@ public function get_current_values_for_document( $document, $data ) { ); } + if ( !empty( $data['display_date'] ) ) { + $current['display_date'] = array( + 'value' => $document->document_display_date(), + 'name' =>"_wcpdf_{$document->slug}_display_date", + ); + } + foreach ( $data as $key => $value ) { if ( isset( $current[$key] ) ) { $data[$key] = array_merge( $current[$key], $value ); @@ -519,6 +530,7 @@ public function get_current_values_for_document( $document, $data ) { public function output_number_date_edit_fields( $document, $data ) { if( empty( $document ) || empty( $data ) ) return; $data = $this->get_current_values_for_document( $document, $data ); + ?>
@@ -555,6 +567,17 @@ public function output_number_date_edit_fields( $document, $data ) {

+ +
+

+

+ + +

+

+
+ + order ); ?> diff --git a/includes/documents/abstract-wcpdf-order-document-methods.php b/includes/documents/abstract-wcpdf-order-document-methods.php index bccb18314..432c37204 100644 --- a/includes/documents/abstract-wcpdf-order-document-methods.php +++ b/includes/documents/abstract-wcpdf-order-document-methods.php @@ -1184,7 +1184,38 @@ public function document_notes() { } } + + public function document_display_date() { + $document_display_date = $this->get_display_date( $this->get_type() ); + + //If display date data is not available in order meta (for older orders), get the display date information from document settings order meta. + if ( empty( $document_display_date ) ) { + $document_settings = $this->settings; + if( isset( $document_settings['display_date'] ) ) { + $document_display_date = $document_settings['display_date']; + } + else { + $document_display_date = 'invoice_date'; + } + } + $formatted_value = $this->get_display_date_label( $document_display_date ); + return $formatted_value; + } + + public function get_display_date_label( $date_string ) { + + $date_labels = array( + 'invoice_date' => __( 'Invoice Date' , 'woocommerce-pdf-invoices-packing-slips' ), + 'order_date' => __( 'Order Date' , 'woocommerce-pdf-invoices-packing-slips' ), + ); + if( isset( $date_labels[$date_string] ) ) { + return $date_labels[ $date_string ]; + } else { + return ''; + } + + } } endif; // class_exists \ No newline at end of file diff --git a/includes/documents/abstract-wcpdf-order-document.php b/includes/documents/abstract-wcpdf-order-document.php index 2e055ad2d..43284c917 100644 --- a/includes/documents/abstract-wcpdf-order-document.php +++ b/includes/documents/abstract-wcpdf-order-document.php @@ -266,6 +266,7 @@ public function read_data( $order ) { 'date' => $order->get_meta( "_wcpdf_{$this->slug}_date" ), 'number' => $number, 'notes' => $order->get_meta( "_wcpdf_{$this->slug}_notes" ), + 'display_date' => $order->get_meta( "_wcpdf_{$this->slug}_display_date" ), ), $order ); return; @@ -447,6 +448,10 @@ public function get_notes( $document_type = '', $order = null, $context = 'view' return $this->get_data( 'notes', $document_type, $order, $context ); } + public function get_display_date( $document_type = '', $order = null, $context = 'view' ) { + return $this->get_data( 'display_date', $document_type, $order, $context ); + } + public function get_title() { return apply_filters( "wpo_wcpdf_{$this->slug}_title", $this->title, $this ); } From 95761d7f4147fabdde658d50973f7cf6657a9f6e Mon Sep 17 00:00:00 2001 From: Aparna Gawade Date: Mon, 3 Apr 2023 20:02:10 +0530 Subject: [PATCH 5/6] Fixed display_date issue when the document is regenerated --- includes/documents/abstract-wcpdf-order-document.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/includes/documents/abstract-wcpdf-order-document.php b/includes/documents/abstract-wcpdf-order-document.php index ed2740ad0..837a8f1c9 100644 --- a/includes/documents/abstract-wcpdf-order-document.php +++ b/includes/documents/abstract-wcpdf-order-document.php @@ -195,6 +195,16 @@ public function save_settings( $latest = false ) { // in both cases, we store the document settings // exclude non historical settings from being saved in order meta $this->order->update_meta_data( "_wcpdf_{$this->slug}_settings", array_diff_key( $settings, array_flip( $this->get_non_historical_settings() ) ) ); + + if( 'invoice' === $this->slug ) { + + if ( isset( $settings['display_date'] ) && $settings['display_date'] == 'order_date' ) { + $this->order->update_meta_data( "_wcpdf_{$this->slug}_display_date", 'order_date' ); + } else { + $this->order->update_meta_data( "_wcpdf_{$this->slug}_display_date", 'invoice_date' ); + } + } + $this->order->save_meta_data(); } } From f6f85b727bb487190f21f574c81b366229a79836 Mon Sep 17 00:00:00 2001 From: Alexandre Faustino Date: Mon, 3 Apr 2023 16:09:06 +0100 Subject: [PATCH 6/6] Update abstract-wcpdf-order-document.php --- includes/documents/abstract-wcpdf-order-document.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/includes/documents/abstract-wcpdf-order-document.php b/includes/documents/abstract-wcpdf-order-document.php index 837a8f1c9..a1d995dfa 100644 --- a/includes/documents/abstract-wcpdf-order-document.php +++ b/includes/documents/abstract-wcpdf-order-document.php @@ -196,8 +196,7 @@ public function save_settings( $latest = false ) { // exclude non historical settings from being saved in order meta $this->order->update_meta_data( "_wcpdf_{$this->slug}_settings", array_diff_key( $settings, array_flip( $this->get_non_historical_settings() ) ) ); - if( 'invoice' === $this->slug ) { - + if ( 'invoice' == $this->slug ) { if ( isset( $settings['display_date'] ) && $settings['display_date'] == 'order_date' ) { $this->order->update_meta_data( "_wcpdf_{$this->slug}_display_date", 'order_date' ); } else {