Skip to content

Commit

Permalink
Making credit invoices optional
Browse files Browse the repository at this point in the history
  • Loading branch information
aldavigdis committed Jun 26, 2024
1 parent e186093 commit a7b3176
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class NineteenEightyWoo {
make_invoice_if_kennitala_is_set: Boolean( formData.get( 'make_invoice_if_kennitala_is_set' ) ),
make_invoice_if_kennitala_is_missing: Boolean( formData.get( 'make_invoice_if_kennitala_is_missing' ) ),
email_invoice: Boolean( formData.get( 'email_invoice' ) ),
make_credit_invoice: Boolean( formData.get( 'make_credit_invoice' ) ),
fetch_products: true
}

Expand Down
17 changes: 17 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -635,4 +635,21 @@ public static function set_delete_inactive_products( bool $value ): bool {
(int) $value
);
}

/**
* 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( '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( 'make_credit_invoice', (int) $value );
}
}
4 changes: 4 additions & 0 deletions src/Hooks/WooOrderStatusChanges.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ public static function maybe_send_invoice_on_payment(
* @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 ( false === empty( ExportInvoice::get_dk_credit_invoice_number( $wc_order ) ) ) {
Expand Down
4 changes: 4 additions & 0 deletions src/Rest/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ public static function rest_api_callback(
);
}

if ( true === property_exists( $rest_json, 'email_credit_invoice' ) ) {
Config::set_make_credit_invoice( $rest_json->make_credit_invoice );
}

if ( true === property_exists( $rest_json, 'make_invoice_if_kennitala_is_set' ) ) {
Config::set_make_invoice_if_kennitala_is_set(
$rest_json->make_invoice_if_kennitala_is_set
Expand Down
28 changes: 28 additions & 0 deletions views/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,34 @@ 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 a7b3176

Please sign in to comment.