diff --git a/view/components/order_details_invoice_box.php b/view/components/order_details_invoice_box.php index e29e65f..110cd51 100644 --- a/view/components/order_details_invoice_box.php +++ b/view/components/order_details_invoice_box.php @@ -5,164 +5,208 @@ $order = new OblioSoftware\Order($post->ID); if ((int) get_option('oblio_gen_date') === 2) { - $date = $order->get_date_created(); - $invoiceDate = $date ? $date->format('Y-m-d') : date('Y-m-d'); + $date = $order->get_date_created(); + $invoiceDate = $date ? $date->format('Y-m-d') : date('Y-m-d'); } else { - $invoiceDate = date('Y-m-d'); + $invoiceDate = date('Y-m-d'); } $invoiceDateClass = ''; if ($order->get_data_info('oblio_invoice_link')) { - $invoiceDateClass = 'hidden'; + $invoiceDateClass = 'hidden'; } ?> get_data_info($series_name_key); - $number = $order->get_data_info($number_key); - $link = $order->get_data_info($link_key); - - $order_meta_table = OblioSoftware\Order::get_meta_table_name(); - $field_name = OblioSoftware\Order::get_meta_table_field_name(); - $sql = "SELECT pmn.{$field_name} " . - "FROM `{$order_meta_table}` pmn " . - "JOIN `{$order_meta_table}` pms ON(pmn.post_id=pms.post_id AND pmn.meta_key='{$number_key}' AND pms.meta_key='{$series_name_key}') " . - "WHERE pmn.meta_value<>'' AND pms.meta_value='{$series_name}' " . - "ORDER BY pmn.`meta_value` DESC " . - "LIMIT 1"; - $lastInvoice = !$link ? null : $wpdb->get_var($sql); - if ($link) { - echo sprintf('
', - _wp_oblio_build_url('oblio-view-' . $options['docType'], $post), sprintf(__('Vezi %s %s %d', 'woocommerce-oblio'), $options['name'], $series_name, $number)); - - parse_str(parse_url($link, PHP_URL_QUERY), $output); - $viewLink = 'https://www.oblio.eu/docs/preview/' . $options['docType'] . '/' . $output['id']; - echo sprintf('', - $viewLink, sprintf(__('Editeaza %s %s %d', 'woocommerce-oblio'), $options['name'], $series_name, $number)); - } else { - echo sprintf('', - $options['docType'], _wp_oblio_build_url('oblio-generate-' . $options['docType'] . '-stock', $post), __('Emite ' . $options['name'], 'woocommerce-oblio')); - if ($options['docType'] === 'invoice' && get_option('oblio_use_stock') === '1') { - echo sprintf('', - $options['docType'], _wp_oblio_build_url('oblio-generate-' . $options['docType'], $post), __('Emite ' . $options['name'] . ' fara Descarcare', 'woocommerce-oblio')); - } - } - if (!$link || $lastInvoice == $post->ID || $options['docType'] === 'proforma') { - $hidden = $link ? '' : 'hidden'; - echo sprintf('', - $hidden, _wp_oblio_build_url('oblio-delete-' . $options['docType'], $post), __('Sterge ' . $options['name'], 'woocommerce-oblio')); - } - if (isset($options['fn']) && is_callable($options['fn'])) { - $options['fn']([ - 'series_name' => $series_name, - 'number' => $number, - 'link' => $link, - ]); - } -?> +$displayDocument = function ( $post, $options = [] ) use ( $wpdb, $order ) { + if ( empty( $options['docType'] ) ) { + $options['docType'] = 'invoice'; + } + + switch ( $options['docType'] ) { + case 'proforma': + $series_name = get_option( 'oblio_series_name_proforma' ); + break; + default: + $series_name = get_option( 'oblio_series_name' ); + } + + $series_name_key = 'oblio_' . $options['docType'] . '_series_name'; + $number_key = 'oblio_' . $options['docType'] . '_number'; + $link_key = 'oblio_' . $options['docType'] . '_link'; + + $series_name = $order->get_data_info( $series_name_key ); + $number = $order->get_data_info( $number_key ); + $link = $order->get_data_info( $link_key ); + + // Detect if HPOS is enabled + $hpos_enabled = wc_get_container() + ->get( \Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController::class ) + ->custom_orders_table_usage_is_enabled(); + + // Set correct table and field names + $order_meta_table = $hpos_enabled ? $wpdb->prefix . "wc_orders_meta" : $wpdb->prefix . "postmeta"; + $order_id_field = $hpos_enabled ? "order_id" : "post_id"; + + // Corrected SQL query to get last invoice for a given series + $sql = $wpdb->prepare( " + SELECT pmn.meta_value + FROM `{$order_meta_table}` pmn + JOIN `{$order_meta_table}` pms + ON pmn.{$order_id_field} = pms.{$order_id_field} + AND pmn.meta_key = %s + AND pms.meta_key = %s + WHERE pmn.meta_value <> '' + AND pms.meta_value = %s + ORDER BY CAST(pmn.meta_value AS UNSIGNED) DESC + LIMIT 1 + ", $number_key, $series_name_key, $series_name ); + + $lastInvoice = $wpdb->get_var( $sql ); // Run regardless of $link + + if ( $link ) { + echo sprintf( + '', + _wp_oblio_build_url( 'oblio-view-' . $options['docType'], $post ), + sprintf( __( 'Vezi %s %s %d', 'woocommerce-oblio' ), $options['name'], $series_name, $number ) + ); + + parse_str( parse_url( $link, PHP_URL_QUERY ), $output ); + $viewLink = 'https://www.oblio.eu/docs/preview/' . $options['docType'] . '/' . $output['id']; - - + 'invoice', - 'name' => 'factura', - 'fn' => function($data) use ($displayDocument, $post) { - if (!$data['link']) { - $displayDocument($post, [ - 'docType' => 'proforma', - 'name' => 'proforma', - ]); - } - } -]); +$displayDocument( $post, [ + 'docType' => 'invoice', + 'name' => 'factura', + 'fn' => function ( $data ) use ( $displayDocument, $post ) { + if ( ! $data['link'] ) { + $displayDocument( $post, [ + 'docType' => 'proforma', + 'name' => 'proforma', + ] ); + } + } +] ); ?> \ No newline at end of file + ul.order_notes li.system-note .note_content.note_error {background:#c9356e;color:#fff;} + ul.order_notes li .note_content.note_error::after {border-color: #c9356e transparent;} + ul.order_notes li.system-note .note_content.note_success {background:#46b450;color:#fff;} + ul.order_notes li .note_content.note_success::after {border-color: #46b450 transparent;} +