Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save invoice display date option #489

Merged
merged 7 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions includes/class-wcpdf-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -520,9 +520,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 );
Expand Down Expand Up @@ -555,6 +559,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 );
Expand All @@ -567,6 +578,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 );

?>
<div class="wcpdf-data-fields" data-document="<?= esc_attr( $document->get_type() ); ?>" data-order_id="<?php echo esc_attr( $document->order->get_id() ); ?>">
<section class="wcpdf-data-fields-section number-date">
Expand Down Expand Up @@ -603,6 +615,17 @@ public function output_number_date_edit_fields( $document, $data ) {
</p>
</div>
<?php endif; ?>
<?php if( isset( $data['display_date'] ) ) : ?>
<div class="<?= esc_attr( $document->get_type() ); ?>-number">
<p class="form-field form-field-wide">
<p>
<span><strong><?= wp_kses_post( $data['display_date']['label'] ); ?></strong></span>
<span><?= esc_attr( $data['display_date']['value'] ); ?></span>
</p>
</p>
</div>
<?php endif; ?>

<?php do_action( 'wpo_wcpdf_meta_box_after_document_data', $document, $document->order ); ?>
<?php else : ?>
<?php /* translators: document title */ ?>
Expand Down
31 changes: 31 additions & 0 deletions includes/documents/abstract-wcpdf-order-document-methods.php
Original file line number Diff line number Diff line change
Expand Up @@ -1184,7 +1184,38 @@ public function document_notes() {
}
}


public function document_display_date() {
alexmigf marked this conversation as resolved.
Show resolved Hide resolved
$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
29 changes: 27 additions & 2 deletions includes/documents/abstract-wcpdf-order-document.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -294,9 +295,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
Expand All @@ -306,10 +308,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 );
}

}
}

Expand All @@ -332,6 +335,7 @@ public function delete( $order = null ) {
'number_data',
'notes',
'printed',
'display_date',
), $this );
foreach ( $data_to_remove as $data_key ) {
$order->delete_meta_data( "_wcpdf_{$this->slug}_{$data_key}" );
Expand Down Expand Up @@ -453,6 +457,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 );
}
Expand Down Expand Up @@ -577,6 +585,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
Expand Down
5 changes: 4 additions & 1 deletion includes/documents/class-wcpdf-invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";

// Call parent constructor
parent::__construct( $order );
}
Expand Down Expand Up @@ -58,10 +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' );
} elseif( empty( $this->get_date() ) ) {
$this->set_date( current_time( 'timestamp', true ) );
$this->set_display_date( 'invoice_date' );
}


$this->init_number();

do_action( 'wpo_wcpdf_init_document', $this );
Expand Down