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

Fix preview issue #833

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
12 changes: 2 additions & 10 deletions includes/Documents/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,7 @@ public function get_shipping_address_title(): string {
public function init() {
// save settings
$this->save_settings();

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->initiate_date();
$this->initiate_number();

do_action( 'wpo_wcpdf_init_document', $this );
Expand Down Expand Up @@ -319,7 +311,7 @@ public function get_pdf_settings_fields( $option_name ) {
'id' => 'display_date',
'options' => array(
'' => __( 'No' , 'woocommerce-pdf-invoices-packing-slips' ),
'invoice_date' => __( 'Invoice Date' , 'woocommerce-pdf-invoices-packing-slips' ),
'document_date' => __( 'Invoice Date' , 'woocommerce-pdf-invoices-packing-slips' ),
'order_date' => __( 'Order Date' , 'woocommerce-pdf-invoices-packing-slips' ),
),
)
Expand Down
105 changes: 72 additions & 33 deletions includes/Documents/OrderDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,18 +135,18 @@ public function init_settings() {
return;
}

public function init_settings_data() {
// don't override/save settings on Preview requests
if ( isset( $_REQUEST['action'] ) && 'wpo_wcpdf_preview' === $_REQUEST['action'] ) {
return;
}

public function init_settings_data(): void {
// order
$this->order_settings = $this->get_order_settings();
// pdf
$this->settings = $this->get_settings();
$this->latest_settings = $this->get_settings( true );

// don't override/save settings on Preview requests
if ( isset( $_REQUEST['action'] ) && 'wpo_wcpdf_preview' === $_REQUEST['action'] ) {
return;
}

// save settings
$this->save_settings( $this->maybe_use_latest_settings() );
}
Expand Down Expand Up @@ -218,7 +218,7 @@ public function save_settings( $latest = false ) {
if ( isset( $settings['display_date'] ) && 'order_date' === $settings['display_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->update_meta_data( "_wcpdf_{$this->slug}_display_date", 'document_date' );
}
}

Expand All @@ -227,7 +227,29 @@ public function save_settings( $latest = false ) {
}
}

public function initiate_number( $force_new_number = false ) {
/**
* Initiate and set document date and display_date.
*
* @return void
*/
public function initiate_date(): void {
if ( isset( $this->settings['display_date'] ) && 'order_date' === $this->settings['display_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( 'document_date' );
}
}

/**
* Initiate and set document number.
*
* @param bool $force_new_number
*
* @return mixed
*/
public function initiate_number( bool $force_new_number = false ) {
$lock = new Semaphore( "initiate_{$this->slug}_number" );
$document_number = $this->exists() ? $this->get_data( 'number' ) : null;
$document_number = ! empty( $document_number ) && $force_new_number ? null : $document_number;
Expand All @@ -236,35 +258,12 @@ public function initiate_number( $force_new_number = false ) {
$lock->log( "Lock acquired for the {$this->slug} number init.", 'info' );

try {
// If a third-party plugin claims to generate document numbers, trigger this instead
if ( apply_filters( "woocommerce_{$this->slug}_number_by_plugin", false ) || apply_filters( "wpo_wcpdf_external_{$this->slug}_number_enabled", false, $this ) ) {
$document_number = apply_filters( "woocommerce_generate_{$this->slug}_number", $document_number, $this->order ); // legacy (backwards compatibility)
$document_number = apply_filters( "woocommerce_{$this->slug}_number", $document_number, $this->order->get_id() ); // legacy (backwards compatibility)
$document_number = apply_filters( "wpo_wcpdf_external_{$this->slug}_number", $document_number, $this );
} elseif ( isset( $this->settings['display_number'] ) && 'order_number' === $this->settings['display_number'] && ! empty( $this->order ) ) {
$document_number = $this->order->get_order_number();
}

if ( ! empty( $document_number ) ) { // overridden by plugin or set to order number
if ( ! is_numeric( $document_number ) && ! ( $document_number instanceof DocumentNumber ) ) {
// document number is not numeric, treat as formatted
// try to extract meaningful number data
$formatted_number = $document_number;
$number = (int) preg_replace( '/\D/', '', $document_number );
$document_number = compact( 'number', 'formatted_number' );
}
} else {
$number_store = $this->get_sequential_number_store();
$document_number = $number_store->increment( intval( $this->order_id ), $this->get_date()->date_i18n( 'Y-m-d H:i:s' ) );
}
$document_number = $this->generate_document_number( true );

if ( ! is_null( $document_number ) ) {
$this->set_number( $document_number );
}

} catch ( \Exception $e ) {
$lock->log( $e, 'critical' );
} catch ( \Error $e ) {
} catch ( \Exception|\Error $e ) {
$lock->log( $e, 'critical' );
}

Expand All @@ -279,6 +278,46 @@ public function initiate_number( $force_new_number = false ) {
return $document_number;
}

/**
* Generate the document number.
*
* @param bool $increment
*
* @return mixed
*/
public function generate_document_number( bool $increment = false ) {
$document_number = null;

// If a third-party plugin claims to generate document numbers, trigger this instead
if ( apply_filters( "woocommerce_{$this->slug}_number_by_plugin", false ) || apply_filters( "wpo_wcpdf_external_{$this->slug}_number_enabled", false, $this ) ) {
$document_number = apply_filters( "woocommerce_generate_{$this->slug}_number", $document_number, $this->order ); // legacy (backwards compatibility)
$document_number = apply_filters( "woocommerce_{$this->slug}_number", $document_number, $this->order->get_id() ); // legacy (backwards compatibility)
$document_number = apply_filters( "wpo_wcpdf_external_{$this->slug}_number", $document_number, $this );
} elseif ( isset( $this->settings['display_number'] ) && 'order_number' === $this->settings['display_number'] && ! empty( $this->order ) ) {
$document_number = $this->order->get_order_number();
}

if ( ! empty( $document_number ) ) { // overridden by plugin or set to order number
if ( ! is_numeric( $document_number ) && ! ( $document_number instanceof DocumentNumber ) ) {
// document number is not numeric, treat as formatted
// try to extract meaningful number data
$formatted_number = $document_number;
$number = (int) preg_replace( '/\D/', '', $document_number );
$document_number = compact( 'number', 'formatted_number' );
}
} else {
$number_store = $this->get_sequential_number_store();

if ( $increment ) {
$document_number = $number_store->increment( intval( $this->order_id ), $this->get_date()->date_i18n( 'Y-m-d H:i:s' ) );
} else {
$document_number = $number_store->get_next();
}
}

return $document_number;
}

public function maybe_use_latest_settings() {
return ! $this->use_historical_settings();
}
Expand Down
38 changes: 18 additions & 20 deletions includes/Documents/OrderDocumentMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -1276,36 +1276,34 @@ public function document_notes(): void {
}
}

public function document_display_date() {
public function document_display_date(): string {
$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 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';
}
$document_settings = $this->settings;
$document_display_date = $document_settings['display_date'] ?? 'document_date';
}

$formatted_value = $this->get_display_date_label( $document_display_date );
return $formatted_value;
}
// Convert the old `invoice_date` slug to the new `document_date` slug.
if ( 'invoice_date' === $document_display_date ) {
$document_display_date = 'document_date';
}

public function get_display_date_label( $date_string ) {
return $this->get_display_date_label( $document_display_date );
}

public function get_display_date_label( string $date_string ): string {
$date_labels = array(
'invoice_date' => __( 'Invoice Date' , 'woocommerce-pdf-invoices-packing-slips' ),
'order_date' => __( 'Order Date' , 'woocommerce-pdf-invoices-packing-slips' ),
'document_date' => sprintf(
/* translators: Document title */
__( '%s Date', 'woocommerce-pdf-invoices-packing-slips' ),
$this->title
),
'order_date' => __( 'Order Date', 'woocommerce-pdf-invoices-packing-slips' ),
);
if( isset( $date_labels[$date_string] ) ) {
return $date_labels[ $date_string ];
} else {
return '';
}

return $date_labels[ $date_string ] ?? '';
}

/**
Expand Down
18 changes: 15 additions & 3 deletions includes/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,11 +299,23 @@ public function ajax_preview() {
$document->set_number( $number_store->get_next() );
}

// apply document number formatting
if ( $document_number = $document->get_number( $document->get_type() ) ) {
// Update document date.
$document->initiate_date();

// Update document number.
$document_number = $document->generate_document_number();

if ( ! empty( $document_number ) ) {
$document->set_number( $document_number );
}

$document_number = $document->get_number( $document->get_type() );

// Apply document number formatting.
if ( $document_number ) {
if ( ! empty( $document->settings['number_format'] ) ) {
foreach ( $document->settings['number_format'] as $key => $value ) {
$document_number->$key = $document->settings['number_format'][$key];
$document_number->$key = $document->settings['number_format'][ $key ];
}
}
$document_number->apply_formatting( $document, $order );
Expand Down
6 changes: 3 additions & 3 deletions views/setup-wizard/display-options.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@
<select id="display-date" name="wcpdf_settings[wpo_wcpdf_documents_settings_invoice][display_date]">
<?php
$options = array(
'' => __( 'No' , 'woocommerce-pdf-invoices-packing-slips' ),
'invoice_date' => __( 'Invoice Date' , 'woocommerce-pdf-invoices-packing-slips' ),
'order_date' => __( 'Order Date' , 'woocommerce-pdf-invoices-packing-slips' ),
'' => __( 'No', 'woocommerce-pdf-invoices-packing-slips' ),
'document_date' => __( 'Invoice Date', 'woocommerce-pdf-invoices-packing-slips' ),
'order_date' => __( 'Order Date', 'woocommerce-pdf-invoices-packing-slips' ),
);
foreach ( $options as $slug => $name ) {
$selected = ( ! empty( $current_settings['display_date'] ) && $current_settings['display_date'] == $slug ) ? 'selected' : '';
Expand Down
Loading