id !== $order_screen && 'woocommerce_page_wc-orders' !== $screen->id ) ) { return; } $net = esc_js( __( 'Network error.', 'factella' ) ); $js = "jQuery(function($){ $(document).on('click','.factella-pdp-send',function(){var b=$(this);var box=b.closest('.factella-pdp-box');" . "b.prop('disabled',true);box.find('.factella-pdp-msg').css('color','#646970').text('…');" . "$.post(ajaxurl,{action:'factella_pdp_send_now',nonce:b.data('nonce'),order_id:b.data('order'),refund_id:b.data('refund')||0},function(res){" . "b.prop('disabled',false);var m=res&&res.data&&res.data.message?res.data.message:'';" . "if(res&&res.data&&res.data.badge){box.find('.factella-pdp-badge').html(res.data.badge);}" . "box.find('.factella-pdp-msg').css('color',res&&res.success?'#1a7f37':'#d63638').text(m);" . "}).fail(function(){b.prop('disabled',false);box.find('.factella-pdp-msg').css('color','#d63638').text('$net');});});});"; wp_add_inline_script( 'jquery-core', $js ); } /** * Register the meta box on whichever order screen is active. */ public static function add_meta_box() { $screen = function_exists( 'wc_get_page_screen_id' ) ? wc_get_page_screen_id( 'shop-order' ) : 'shop_order'; add_meta_box( 'efacture-fr-invoice', __( 'Invoice (Factella)', 'factella' ), array( __CLASS__, 'render_meta_box' ), $screen, 'side', 'high' ); } /** * Render the meta box. * * @param WP_Post|WC_Order $post_or_order Post (legacy) or order (HPOS). */ public static function render_meta_box( $post_or_order ) { $order = $post_or_order instanceof WC_Order ? $post_or_order : wc_get_order( $post_or_order->ID ); if ( ! $order ) { return; } $number = (string) $order->get_meta( EFacture_FR_Invoice_Manager::META_NUMBER ); $buyer = EFacture_FR_Checkout_Fields::get_buyer_ids( $order ); if ( '' === $number ) { echo '

' . esc_html__( 'No invoice yet. It will be created automatically when the order reaches the configured status.', 'factella' ) . '

'; } else { echo '

' . esc_html__( 'Number:', 'factella' ) . ' ' . esc_html( $number ) . '
'; echo '' . esc_html__( 'Date:', 'factella' ) . ' ' . esc_html( (string) $order->get_meta( EFacture_FR_Invoice_Manager::META_DATE ) ) . '
'; echo '' . esc_html__( 'Type:', 'factella' ) . ' ' . esc_html( (string) $order->get_meta( EFacture_FR_Invoice_Manager::META_TYPE ) ) . '

'; // Factur-X download (only when the generator library is bundled). if ( class_exists( 'EFacture_FR_Facturx' ) && EFacture_FR_Facturx::is_available() ) { echo '

' . esc_html__( 'Download Factur-X PDF', 'factella' ) . '

'; } } // SUPER PDP transmission status + manual send. if ( '' !== $number && class_exists( 'EFacture_FR_Transmission' ) ) { $type = (string) $order->get_meta( EFacture_FR_Invoice_Manager::META_TYPE ); echo '
'; echo '

' . esc_html__( 'SUPER PDP:', 'factella' ) . ' '; echo EFacture_FR_Transmission::badge_html( $order ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- self-escaped badge markup. echo '

'; if ( 'B2C' === $type ) { echo '

' . esc_html__( 'B2C — covered by e-reporting, not transmitted.', 'factella' ) . '

'; } else { $nonce = wp_create_nonce( 'factella_pdp_send_now' ); echo '

'; } echo '
'; } if ( '' !== $buyer['siret'] || '' !== $buyer['vat'] ) { echo '

'; if ( '' !== $buyer['siret'] ) { echo 'SIRET: ' . esc_html( $buyer['siret'] ) . '
'; echo 'SIREN: ' . esc_html( $buyer['siren'] ) . '
'; } if ( '' !== $buyer['vat'] ) { echo '' . esc_html__( 'VAT:', 'factella' ) . ' ' . esc_html( $buyer['vat'] ); } echo '

'; } $facturx = class_exists( 'EFacture_FR_Facturx' ) && EFacture_FR_Facturx::is_available(); foreach ( $order->get_refunds() as $refund ) { $credit = (string) $refund->get_meta( EFacture_FR_Invoice_Manager::META_CREDIT_NUMBER ); if ( '' !== $credit ) { /* translators: %s: credit note number */ echo '

' . esc_html( sprintf( __( 'Credit note: %s', 'factella' ), $credit ) ); if ( $facturx ) { echo '
' . esc_html__( 'Download credit note PDF', 'factella' ) . ''; } echo '

'; if ( class_exists( 'EFacture_FR_Transmission' ) && 'B2C' !== (string) $order->get_meta( EFacture_FR_Invoice_Manager::META_TYPE ) ) { $rnonce = wp_create_nonce( 'factella_pdp_send_now' ); echo '
'; echo ''; echo EFacture_FR_Transmission::badge_html( $refund ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- self-escaped badge markup. echo '
'; } } } } /** * Add the invoice column after the order number. * * @param array $columns Columns. * @return array */ public static function add_column( $columns ) { $result = array(); foreach ( $columns as $key => $label ) { $result[ $key ] = $label; if ( 'order_number' === $key ) { $result['efacture_fr_invoice'] = __( 'Invoice', 'factella' ); } } if ( ! isset( $result['efacture_fr_invoice'] ) ) { $result['efacture_fr_invoice'] = __( 'Invoice', 'factella' ); } return $result; } /** * Column content for HPOS. * * @param string $column Column key. * @param WC_Order $order Order. */ public static function render_column_hpos( $column, $order ) { if ( 'efacture_fr_invoice' === $column ) { self::print_column( $order ); } } /** * Column content for legacy post storage. * * @param string $column Column key. * @param int $post_id Order ID. */ public static function render_column_legacy( $column, $post_id ) { if ( 'efacture_fr_invoice' === $column ) { self::print_column( wc_get_order( $post_id ) ); } } /** * Print "number (type)" or a dash. * * @param WC_Order|false $order Order. */ private static function print_column( $order ) { $number = $order ? (string) $order->get_meta( EFacture_FR_Invoice_Manager::META_NUMBER ) : ''; if ( '' === $number ) { echo '–'; return; } echo esc_html( $number . ' (' . $order->get_meta( EFacture_FR_Invoice_Manager::META_TYPE ) . ')' ); if ( class_exists( 'EFacture_FR_Transmission' ) ) { $badge = EFacture_FR_Transmission::badge_html( $order ); if ( '' !== $badge ) { echo '
' . $badge; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- self-escaped badge markup. } } } }