# EenvoudigFactureren plugin for WooCommerce

Generate invoices or order forms in EenvoudigFactureren for WooCommerce orders.

## Description

The EenvoudigFactureren plugin for WooCommerce is an extension for your WooCommerce online shop that automatically generates invoices or order forms in EenvoudigFactureren for new WooCommerce orders.

### Key features

* Every new order automatically triggers the creation of an invoice or order form at EenvoudigFactureren.
* Customer information is synchronised to EenvoudigFactureren.
* Supports EU VAT numbers and VAT exemptions.
* Configure automatic email sending to your customers with attached PDF from EenvoudigFactureren.
* Administrator and Store Manager can directly open the generated invoice or order form from WooCommerce.

### About EenvoudigFactureren

EenvoudigFactureren is an invoicing solution for Belgian entrepreneurs. You can create a new account for free at [EenvoudigFactureren](https://eenvoudigfactureren.be/).

### Technical specifications

* Requires at least WooCommerce 3.6 version.
* Developed for single-site installations.

## Installation

### Steps of installation

1. Unzip `eenvoudigfactureren-for-woocommerce.zip` to the `/wp-content/plugins/eenvoudigfactureren-for-woocommerce/` directory
2. Activate the plugin through the 'Plugins' menu in WordPress.
3. In the WordPress admin section of your online shop configure the plugin by adding your EenvoudigFactureren credentials in the EenvoudigFactureren API Settings page.
4. That's it! By default a new invoice will be automatically created in EenvoudigFactureren on order creation.

### Required

EenvoudigFactureren account is required. You can create an account at [EenvoudigFactureren](https://eenvoudigfactureren.be/).

### Options

By default new customers in WooCommerce are added as new client in EenvoudigFacturen. To link an existing customer in WooCommerce to an existing client in EenvoudigFactureren add the WooCommerce customer ID as client number in EenvoudigFactureren. In WooCommerce you need to activate the option 'Use client number to search for existing clients' in the Settings page to enable this behaviour.
Only WooCommerce customers which have created an account can be linked to existing clients in EenvoudigFactureren.

When a VAT exemption is applied for foreign business customers the VAT exemption reason code (e.g. 'ICL') can be added as a 0% WooCommerce tax rate and should be used as the tax name. The plugin supports the legacy `is_vat_exempt` flow and also recognises the fixed EenvoudigFactureren VAT exemption codes such as `KO`, `VA`, `ICL` and `ICD` directly from 0% WooCommerce tax names.

## Developer filters

The plugin provides WordPress filters for custom integrations. Add callbacks in a custom plugin or your theme's `functions.php`. Every callback must return its first argument, after changing it if needed.

| Filter | Purpose | Parameters | Return value |
| --- | --- | --- | --- |
| `wc_eenvfact_skip_zero_total` | Change whether a zero-total order is skipped. | `$skip`, `$order_id`, `WC_Order $order` | `bool` |
| `wc_eenvfact_should_skip_generation` | Change whether an already generating or generated document is skipped. | `$skip`, `$order_id`, `WC_Order $order` | `bool` |
| `wc_eenvfact_vat_keys` | Change the order-meta keys searched for a VAT number. | `array $meta_keys` | `array` |
| `wc_eenvfact_product_description` | Change a product description sent to EF. | `$description`, `WC_Order_Item_Product $item`, `WC_Order $order` | `string` |
| `wc_eenvfact_billing_street` | Change the billing street sent to EF. | `$street`, `WC_Order $order` | `string` |
| `wc_eenvfact_shipping_street` | Change the shipping street sent to EF. | `$street`, `WC_Order $order` | `string` |
| `wc_eenvfact_client_data` | Change the complete EF client payload before it is created or updated. | `object $client`, `WC_Order $order` | `object` |
| `wc_eenvfact_document_item_data` | Change an EF product, fee or shipping line. | `object $document_item`, `$source_item`, `WC_Order $order`, `$item_type` | `object` |
| `wc_eenvfact_document_data` | Change the complete EF document payload before it is created. | `object $document`, `WC_Order $order` | `object` |

### Generation and VAT

`wc_eenvoudigfactureren_skip_zero_total` was renamed to `wc_eenvfact_skip_zero_total` in version 1.3.3. The old name still works, but WordPress marks it as deprecated; update custom code to use the new name.

```php
// Generate a document for zero-total orders as well.
add_filter( 'wc_eenvfact_skip_zero_total', function( $skip, $order_id, $order ) {
    return false;
}, 10, 3 );

// Skip generation for orders selected by custom order metadata.
add_filter( 'wc_eenvfact_should_skip_generation', function( $skip, $order_id, $order ) {
    return $skip || 'yes' === $order->get_meta( 'skip_ef_generation' );
}, 10, 3 );

// Look for a VAT number in an additional order-meta field.
add_filter( 'wc_eenvfact_vat_keys', function( $meta_keys ) {
    $meta_keys[] = 'my_vat_number';

    return $meta_keys;
} );
```

`wc_eenvfact_should_skip_generation` receives the same order ID and order as the zero-total filter. Return `false` only when intentionally allowing a document to be regenerated.

### Product descriptions

The following converts `Product X<span> - </span>Large` to `Product X - Large` and removes other HTML. The WooCommerce product and order item remain unchanged.

```php
add_filter( 'wc_eenvfact_product_description', function( $description, $item, $order ) {
    if ( ! is_string( $description ) || '' === trim( $description ) ) {
        return $description;
    }

    $description = html_entity_decode( $description, ENT_QUOTES | ENT_HTML5, 'UTF-8' );
    $description = preg_replace( '#<span[^>]*>\s*-\s*</span>#i', ' - ', $description );
    $description = wp_strip_all_tags( $description, true );

    return trim( preg_replace( '/\s+/', ' ', $description ) );
}, 10, 3 );
```

### Client addresses

These filters change only the addresses sent to EenvoudigFactureren. This example appends separate house-number metadata.

```php
add_filter( 'wc_eenvfact_billing_street', function( $street, $order ) {
    $house_number = trim( $order->get_meta( 'billing_house_number' ) );

    return '' !== $house_number && '' !== $street && false === stripos( $street, $house_number )
        ? $street . ' ' . $house_number
        : $street;
}, 10, 2 );

add_filter( 'wc_eenvfact_shipping_street', function( $street, $order ) {
    $house_number = trim( $order->get_meta( 'shipping_house_number' ) );

    if ( '' === $house_number ) {
        $house_number = trim( $order->get_meta( 'billing_house_number' ) );
    }

    if ( '' === $house_number || '' === $street ) {
        return $street;
    }

    $parts = explode( ' ', $house_number );
    $first_part = $parts[0];

    if ( preg_match( '/\\s' . preg_quote( $first_part, '/' ) . '$/', $street ) ) {
        $house_number = trim( substr( $house_number, strlen( $first_part ) ) );
    }

    return '' === $house_number ? $street : $street . ' ' . $house_number;
}, 10, 2 );
```

### EenvoudigFactureren custom fields

Add an EenvoudigFactureren custom field as a dynamic property named `custom_value.<field_name>`. The document-line filter receives `$item_type` as `product`, `fee` or `shipping`; `$source_item` is `null` for shipping because WooCommerce shipping can be combined into one EenvoudigFactureren line.

```php
add_filter( 'wc_eenvfact_client_data', function( $client, $order ) {
    $client->{'custom_value.bedrijfsnaam'} = $order->get_billing_company();

    return $client;
}, 10, 2 );

add_filter( 'wc_eenvfact_document_data', function( $document, $order ) {
    $document->{'custom_value.inkoopreferentie'} = $order->get_meta( 'purchase_order_number' );

    return $document;
}, 10, 2 );

add_filter( 'wc_eenvfact_document_item_data', function( $document_item, $source_item, $order, $item_type ) {
    if ( 'product' === $item_type && $source_item ) {
        $document_item->{'custom_value.productcode'} = $source_item->get_meta( 'product_code' );
    }

    return $document_item;
}, 10, 4 );
```

### Limitations

* Only EURO as currency is supported.
