Skip to content

Commit

Permalink
New: adds support for multiple UBL formats (#927)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmigf authored Dec 13, 2024
1 parent 0b73234 commit 18d85ca
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 10 deletions.
11 changes: 11 additions & 0 deletions assets/js/admin-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ jQuery( function( $ ) {
$( this ).closest( 'tr' ).nextAll( 'tr' ).has( 'input#next_invoice_number' ).first().show();
}
} ).trigger( 'change' );

// disable encrypted pdf option for non UBL 2.1 formats
$( "[name='wpo_wcpdf_documents_settings_invoice_ubl[ubl_format]']" ).on( 'change', function( event ) {
let $encryptedPdfCheckbox = $( this ).closest( 'form' ).find( "[name='wpo_wcpdf_documents_settings_invoice_ubl[include_encrypted_pdf]']" );

if ( $( this ).val() !== 'ubl_2_1' ) {
$encryptedPdfCheckbox.prop( 'checked', false ).prop( 'disabled', true );
} else {
$encryptedPdfCheckbox.prop( 'disabled', false );
}
} ).trigger( 'change' );

// enable settings document switch
$( '.wcpdf_document_settings_sections > h2' ).on( 'click', function() {
Expand Down
2 changes: 1 addition & 1 deletion assets/js/admin-script.min.js

Large diffs are not rendered by default.

25 changes: 20 additions & 5 deletions includes/Documents/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ public function get_ubl_settings_fields( $option_name ) {
$settings_fields = array(
array(
'type' => 'section',
'id' => $this->type.'_ubl',
'id' => $this->type . '_ubl',
'title' => '',
'callback' => 'section',
),
Expand All @@ -588,18 +588,32 @@ public function get_ubl_settings_fields( $option_name ) {
'id' => 'enabled',
'title' => __( 'Enable', 'woocommerce-pdf-invoices-packing-slips' ),
'callback' => 'checkbox',
'section' => $this->type.'_ubl',
'section' => $this->type . '_ubl',
'args' => array(
'option_name' => $option_name,
'id' => 'enabled',
)
),
array(
'type' => 'setting',
'id' => 'ubl_format',
'title' => __( 'Format', 'woocommerce-pdf-invoices-packing-slips' ),
'callback' => 'select',
'section' => $this->type . '_ubl',
'args' => array(
'option_name' => $option_name,
'id' => 'ubl_format',
'options' => apply_filters( 'wpo_wcpdf_document_ubl_settings_formats', array(
'ubl_2_1' => __( 'UBL 2.1' , 'woocommerce-pdf-invoices-packing-slips' ),
), $this ),
)
),
array(
'type' => 'setting',
'id' => 'attach_to_email_ids',
'title' => __( 'Attach to:', 'woocommerce-pdf-invoices-packing-slips' ),
'callback' => 'multiple_checkboxes',
'section' => $this->type.'_ubl',
'section' => $this->type . '_ubl',
'args' => array(
'option_name' => $option_name,
'id' => 'attach_to_email_ids',
Expand All @@ -613,11 +627,11 @@ public function get_ubl_settings_fields( $option_name ) {
'id' => 'include_encrypted_pdf',
'title' => __( 'Include encrypted PDF:', 'woocommerce-pdf-invoices-packing-slips' ),
'callback' => 'checkbox',
'section' => $this->type.'_ubl',
'section' => $this->type . '_ubl',
'args' => array(
'option_name' => $option_name,
'id' => 'include_encrypted_pdf',
'description' => __( 'Include the PDF Invoice file encrypted in the UBL file.', 'woocommerce-pdf-invoices-packing-slips' ),
'description' => __( 'Embed the encrypted PDF invoice file within the UBL document. Note that this option may not be supported by all UBL formats.', 'woocommerce-pdf-invoices-packing-slips' ),
)
),
);
Expand Down Expand Up @@ -687,6 +701,7 @@ public function get_settings_categories( string $output_format ): array {
'title' => __( 'General', 'woocommerce-pdf-invoices-packing-slips' ),
'members' => array(
'enabled',
'ubl_format',
'attach_to_email_ids',
'include_encrypted_pdf',
),
Expand Down
11 changes: 11 additions & 0 deletions includes/Documents/OrderDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,17 @@ public function is_enabled( $output_format = 'pdf' ) {

return apply_filters( 'wpo_wcpdf_document_is_enabled', $is_enabled, $this->type, $output_format );
}

/**
* Get the UBL format
*
* @return string|false
*/
public function get_ubl_format() {
$ubl_format = $this->get_setting( 'ubl_format', false, 'ubl' );

return apply_filters( 'wpo_wcpdf_document_ubl_format', $ubl_format, $this );
}

public function get_hook_prefix() {
return 'wpo_wcpdf_' . $this->slug . '_get_';
Expand Down
2 changes: 1 addition & 1 deletion includes/Settings/SettingsGeneral.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public function init_settings() {
'option_name' => $option_name,
'id' => 'shop_phone_number',
'translatable' => true,
// 'description' => __( 'Required for e-Invoice.', 'woocommerce-pdf-invoices-packing-slips' ),
'description' => __( 'Mandatory for certain UBL formats.', 'woocommerce-pdf-invoices-packing-slips' ),
)
),
array(
Expand Down
2 changes: 1 addition & 1 deletion ubl/Builders/SabreBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function build( Document $document ) {
$namespaces = array_flip( $document->get_namespaces() );
$this->service->namespaceMap = $namespaces;

return $this->service->write( 'Invoice', $document->get_data() );
return $this->service->write( $document->get_root_element(), $document->get_data() );
}

}
1 change: 1 addition & 0 deletions ubl/Documents/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function set_order_document( OrderDocument $order_document ) {
$this->order_document = $order_document;
}

abstract public function get_root_element();
abstract public function get_format();
abstract public function get_namespaces();
abstract public function get_data();
Expand Down
8 changes: 6 additions & 2 deletions ubl/Documents/UblDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
}

class UblDocument extends Document {

public function get_root_element() {
return apply_filters( 'wpo_wc_ubl_document_root_element', 'Invoice', $this );
}

public function get_format() {
$format = apply_filters( 'wpo_wc_ubl_document_format' , array(
Expand Down Expand Up @@ -82,7 +86,7 @@ public function get_format() {
'enabled' => true,
'handler' => \WPO\IPS\UBL\Handlers\Invoice\InvoiceLineHandler::class,
),
) );
), $this );

foreach ( $format as $key => $element ) {
if ( false === $element['enabled'] ) {
Expand All @@ -98,7 +102,7 @@ public function get_namespaces() {
'cac' => 'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2',
'cbc' => 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2',
'' => 'urn:oasis:names:specification:ubl:schema:xsd:Invoice-2',
) );
), $this );
}

public function get_data() {
Expand Down

0 comments on commit 18d85ca

Please sign in to comment.