Skip to content

Commit

Permalink
Removing automatic credit invoice generation on refund (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
aldavigdis authored Sep 9, 2024
1 parent a5fdf4e commit 0bdf750
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 148 deletions.
17 changes: 0 additions & 17 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -639,23 +639,6 @@ public static function set_delete_inactive_products( bool $value ): bool {
);
}

/**
* Get wether to make a credit invoice when an order is labelled as refunded
*/
public static function get_make_credit_invoice(): bool {
return (bool) get_option( '1984_woo_dk_make_credit_invoice', false );
}

/**
* Set wether to make a credit invoice when an order is labelled as refunded
*
* @param bool $value True to enable credit invoices,
* false to disable it.
*/
public static function set_make_credit_invoice( bool $value ): bool {
return update_option( '1984_woo_dk_make_credit_invoice', (int) $value );
}

/**
* Get the ledger code for domestic customers
*
Expand Down
102 changes: 0 additions & 102 deletions src/Hooks/WooOrderStatusChanges.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,6 @@ public function __construct() {
10,
1
);

add_action(
'woocommerce_order_status_refunded',
array( __CLASS__, 'maybe_send_credit_invoice_on_refund' ),
10,
1
);
}

/**
Expand Down Expand Up @@ -176,99 +169,4 @@ public static function maybe_send_invoice_on_payment(
);
}
}

/**
* Send a credit invoice after an order has been fully refunded
*
* Used for the `woocommerce_order_status_refunded` hook and creates a
* credit invoice for an order in DK, finally sending it to the customer, if
* a credit invoice has not yet been created.
*
* @param int $order_id The WooCommerce order ID.
*/
public static function maybe_send_credit_invoice_on_refund( int $order_id ): void {
if ( ! Config::get_make_credit_invoice() ) {
return;
}

$wc_order = new WC_Order( $order_id );

if (
! empty(
ExportInvoice::get_dk_credit_invoice_number( $wc_order )
)
) {
return;
}

if (
! empty(
$wc_order->get_meta(
'1984_dk_woo_credit_invoice_creation_error',
true
)
)
) {
return;
}

$credit_invoice_number = ExportInvoice::reverse_in_dk( $wc_order );

if ( is_string( $credit_invoice_number ) ) {
$wc_order->add_order_note(
sprintf(
// Translators: %1$s is a placeholder for the invoice number generated in DK.
__(
'A credit invoice for the refund has been created in DK. The invoice number is %1$s.',
'1984-dk-woo'
),
$credit_invoice_number
)
);

if ( Config::get_email_invoice() ) {
if ( ExportInvoice::email_in_dk( $wc_order, 'credit' ) === true ) {
$wc_order->add_order_note(
__(
'An email containing the credit invoice as a PDF attachment was sent to the customer.',
'1984-dk-woo'
)
);
} else {
$wc_order->add_order_note(
__(
'It was not possible to send an email to the customer containing the invoice as a PDF attachment.',
'1984-dk-woo'
)
);
}
}
} elseif ( $credit_invoice_number instanceof WP_Error ) {
$wc_order->update_meta_data(
'1984_dk_woo_credit_invoice_creation_error',
$credit_invoice_number->get_error_code()
);
$wc_order->update_meta_data(
'1984_dk_woo_credit_invoice_creation_error_message',
$credit_invoice_number->get_error_message()
);
$wc_order->update_meta_data(
'1984_dk_woo_credit_invoice_creation_error_data',
$credit_invoice_number->get_error_data()
);
$wc_order->add_order_note(
__(
'Unable to create an invoice in DK: ',
'1984-dk-woo'
) . $credit_invoice_number->get_error_code()
);
} else {
$wc_order->add_order_note(
__(
'A credit invoice could not be created in DK due to an unhandled error.',
'1984-dk-woo'
)
);
}
}
}
38 changes: 9 additions & 29 deletions views/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,17 @@ class="regular-text api-key-input"

<section class="section">
<h2><?php esc_html_e( 'Invoices', '1984-dk-woo' ); ?></h2>
<p>
<?php
esc_html_e(
'Invoices may be made in DK upon successful checkout, or manually from the WooCommerce Order Editor. This can based on wether the customer supplies a kennitala, and a kennitala field can be enabled as well.',
'1984-dk-woo'
);
?>
</p>
<table id="dk-invoices-table" class="form-table">
<tbody>
<tr>
<tr>
<th scope="row" class="column-title column-primary">
<label for="default_kennitala_field">
<?php
Expand Down Expand Up @@ -498,34 +506,6 @@ class="regular-text api-key-input"
</p>
</td>
</tr>
<tr>
<th scope="row" class="column-title column-primary">
</th>
<td>
<input
id="make_credit_invoice_field"
name="make_credit_invoice"
type="checkbox"
<?php echo esc_attr( Config::get_make_credit_invoice() ? 'checked' : '' ); ?>
/>
<label for="make_credit_invoice_field">
<?php
esc_html_e(
'Create a credit invoice when an order is labelled as refunded',
'1984-dk-woo'
);
?>
</label>
<p class="description">
<?php
esc_html_e(
'If enabled, a credit invoice is automatically created when an order is labelled as refunded in WooCommerce.',
'1984-dk-woo'
);
?>
</p>
</td>
</tr>
</tbody>
</table>
<h3><?php esc_html_e( 'Service SKUs', '1984-dk-woo' ); ?></h3>
Expand Down

0 comments on commit 0bdf750

Please sign in to comment.