Skip to content

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamadNateqi committed Nov 2, 2023
1 parent 601f8c6 commit 928a8ad
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 23 deletions.
25 changes: 5 additions & 20 deletions includes/class-wcpdf-main.php
Original file line number Diff line number Diff line change
Expand Up @@ -1586,34 +1586,19 @@ public function display_due_date( string $document_type, $order ): void {
return;
}

$invoice_settings = WPO_WCPDF()->settings->get_document_settings( 'invoice' );
$invoice = wcpdf_get_invoice( $order );

if ( empty( $invoice_settings['due_date'] ) ) {
if ( ! $invoice ) {
return;
}

$due_days = ( 'custom' === $invoice_settings['due_date'] ) ? intval( $invoice_settings['due_date_custom_days'] ?? 0 ) : absint( $invoice_settings['due_date'] );
$due_date_timestamp = $invoice->get_due_date();

if ( 0 >= $due_days ) {
if ( 0 >= $due_date_timestamp ) {
return;
}

$base_date_option = $invoice_settings['due_date_base_date'] ?? 'order_date';

if ( 'invoice_date' === $base_date_option ) {
$invoice = wcpdf_get_invoice( $order );
if ( $invoice && $invoice->exists() ) {
$base_date = $invoice->get_date( 'invoice' );
}
}

if ( empty( $base_date ) ) {
$base_date = $order->get_date_created();
}

$base_date = apply_filters( 'wpo_wcpdf_due_date_base_date', $base_date, $base_date->getTimestamp(), $order );
$due_date_datetime = $base_date->modify( "+$due_days days" );
$due_date = apply_filters( "wpo_wcpdf_invoice_due_date", date( wcpdf_date_format( $this, 'due_date' ), $due_date_datetime->getTimestamp() ), $due_date_datetime->getTimestamp(), $order );
$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 '<tr class="due-date">
Expand Down
38 changes: 35 additions & 3 deletions includes/documents/class-wcpdf-invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,11 +373,11 @@ public function get_pdf_settings_fields( $option_name ) {
'args' => array(
'option_name' => $option_name,
'id' => 'due_date_base_date',
'options' => array(
'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')
) ),
'description' => __( 'Choose the initial date from which the due date will be calculated.', 'woocommerce-pdf-invoices-packing-slips' )
)
),
array(
Expand Down Expand Up @@ -696,6 +696,38 @@ public function get_date_title() {
$date_title = __( 'Invoice Date:', 'woocommerce-pdf-invoices-packing-slips' );
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( $invoice_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

0 comments on commit 928a8ad

Please sign in to comment.