get_status(), self::ready_statuses(), true ) || '' !== (string) $order->get_meta( self::META_NUMBER ); } /** * Status hook callback. * * @param int $order_id Order ID. */ public static function assign_on_status( $order_id ) { $order = wc_get_order( $order_id ); if ( $order ) { self::ensure_number( $order ); } } /** * Assign the next sequential invoice number once (idempotent). * * @param WC_Order $order Order. * @return string Invoice number. */ public static function ensure_number( $order ) { $existing = (string) $order->get_meta( self::META_NUMBER ); if ( '' !== $existing ) { return $existing; } $prefix = EBGI_Settings::get( 'invoice_prefix' ); $number = $prefix . str_pad( (string) self::next_counter(), 5, '0', STR_PAD_LEFT ); $created = $order->get_date_created(); $order->update_meta_data( self::META_NUMBER, $number ); $order->update_meta_data( self::META_DATE, $created ? $created->date( 'Y-m-d' ) : gmdate( 'Y-m-d' ) ); $order->save(); return $number; } /** * Atomically increment the invoice counter (MySQL LAST_INSERT_ID trick; safe under concurrency). * * @return int */ private static function next_counter() { global $wpdb; add_option( self::COUNTER, '0', '', 'no' ); $next = 0; if ( ! ( defined( 'DB_ENGINE' ) && 'sqlite' === DB_ENGINE ) ) { // MySQL / MariaDB: atomic increment. Errors are suppressed because unsupported engines fall back below. $suppress = $wpdb->suppress_errors( true ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- atomic counter must bypass the object cache. $updated = $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->options} SET option_value = LAST_INSERT_ID(option_value + 1) WHERE option_name = %s", self::COUNTER ) ); $next = $updated ? (int) $wpdb->get_var( 'SELECT LAST_INSERT_ID()' ) : 0; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching $wpdb->suppress_errors( $suppress ); } wp_cache_delete( self::COUNTER, 'options' ); wp_cache_delete( 'notoptions', 'options' ); wp_cache_delete( 'alloptions', 'options' ); if ( $next < 1 ) { // Database without LAST_INSERT_ID(expr) support (e.g. SQLite): read-increment-write. $next = (int) get_option( self::COUNTER, 0 ) + 1; update_option( self::COUNTER, (string) $next, false ); } return $next; } /** * Public URL of the invoice (order key acts as the access token for customers / guests). * * @param WC_Order $order Order. * @return string */ public static function url( $order ) { return add_query_arg( array( 'action' => 'ebgi_invoice', 'order_id' => $order->get_id(), 'key' => $order->get_order_key(), ), admin_url( 'admin-post.php' ) ); } /** * Gather everything the invoice / GSTR-1 export needs, computed through EBGI_GSTIN_Validator. * * @param WC_Order $order Order. * @return array */ public static function get_data( $order ) { $s = EBGI_Settings::all(); $seller_state = EBGI_Settings::seller_state(); $seller_code = EBGI_GSTIN_Validator::gst_code_from_wc_state( $seller_state ); $default_slab = EBGI_GSTIN_Validator::slab_key( $s['default_gst_slab'] ); $inclusive = 'inclusive' === $s['tax_source']; $has_shipping = '' !== $order->get_shipping_country() && '' !== $order->get_shipping_state(); $buyer_state = $has_shipping ? $order->get_shipping_state() : $order->get_billing_state(); $buyer_country = $has_shipping ? $order->get_shipping_country() : $order->get_billing_country(); $buyer_country = '' === $buyer_country ? 'IN' : $buyer_country; $uqc = '' !== $s['default_uqc'] ? $s['default_uqc'] : 'NOS-NUMBERS'; $lines = array(); $principal = array( 'slab' => $default_slab, 'taxable' => -1, ); $make_line = function ( $name, $hsn, $qty, $taxable, $wc_tax, $slab, $line_uqc ) use ( $inclusive ) { if ( abs( $wc_tax ) >= 0.005 ) { return array( 'name' => $name, 'hsn' => $hsn, 'qty' => $qty, 'uqc' => $line_uqc, 'slab' => EBGI_GSTIN_Validator::detect_slab( $taxable, $wc_tax ), 'taxable' => $taxable, 'tax' => $wc_tax, ); } if ( $inclusive ) { $net = EBGI_GSTIN_Validator::taxable_from_inclusive( $taxable, $slab ); return array( 'name' => $name, 'hsn' => $hsn, 'qty' => $qty, 'uqc' => $line_uqc, 'slab' => $slab, 'taxable' => $net, 'tax' => ( EBGI_GSTIN_Validator::to_paise( $taxable ) - EBGI_GSTIN_Validator::to_paise( $net ) ) / 100, ); } return array( 'name' => $name, 'hsn' => $hsn, 'qty' => $qty, 'uqc' => $line_uqc, 'slab' => '0', 'taxable' => $taxable, 'tax' => 0, ); }; foreach ( $order->get_items( 'line_item' ) as $item ) { $meta = EBGI_Product_HSN::get_item_tax_meta( $item ); $slab = '' !== $meta['slab'] ? EBGI_GSTIN_Validator::slab_key( $meta['slab'] ) : $default_slab; $product = $item->get_product(); $line = $make_line( $item->get_name(), $meta['hsn'], (float) $item->get_quantity(), (float) $item->get_total(), (float) $item->get_total_tax(), $slab, $uqc ); $line['sku'] = $product ? $product->get_sku() : ''; $lines[] = $line; if ( $line['taxable'] > $principal['taxable'] ) { $principal = array( 'slab' => $line['slab'], 'taxable' => $line['taxable'], ); } } $shipping_slab = 'principal' === $s['shipping_rate_mode'] ? $principal['slab'] : EBGI_GSTIN_Validator::slab_key( $s['shipping_gst_slab'] ); foreach ( $order->get_items( 'shipping' ) as $item ) { $total = (float) $item->get_total(); $tax = (float) $item->get_total_tax(); if ( abs( $total ) < 0.005 && abs( $tax ) < 0.005 ) { continue; } /* translators: %s: shipping method name */ $lines[] = $make_line( sprintf( __( 'Shipping: %s', '8-bit-gst-invoice-for-woocommerce' ), $item->get_name() ), $s['shipping_sac'], 1, $total, $tax, $shipping_slab, 'NA' ); } foreach ( $order->get_items( 'fee' ) as $item ) { $lines[] = $make_line( $item->get_name(), '', 1, (float) $item->get_total(), (float) $item->get_total_tax(), $default_slab, 'NA' ); } $computed = EBGI_GSTIN_Validator::build_invoice( $lines, $seller_state, $buyer_state, $buyer_country, (float) $order->get_total() ); $number = (string) $order->get_meta( self::META_NUMBER ); $date = (string) $order->get_meta( self::META_DATE ); $created = $order->get_date_created(); if ( '' === $date ) { $date = $created ? $created->date( 'Y-m-d' ) : gmdate( 'Y-m-d' ); } return array( 'order_id' => $order->get_id(), 'order_number' => $order->get_order_number(), 'invoice_number' => $number, 'invoice_date' => $date, 'is_proforma' => '' === $number, 'payment_method' => $order->get_payment_method_title(), 'order_total' => (float) $order->get_total(), 'seller' => array( 'legal_name' => $s['legal_name'], 'trade_name' => $s['trade_name'], 'address' => $s['address'], 'gstin' => $s['seller_gstin'], 'pan' => '' !== $s['pan'] ? $s['pan'] : EBGI_GSTIN_Validator::pan_from_gstin( $s['seller_gstin'] ), 'state_code' => $seller_code, 'state_name' => EBGI_GSTIN_Validator::state_name( $seller_code ), 'signatory' => $s['signatory_name'], 'footer' => $s['invoice_footer'], ), 'buyer' => array( 'name' => trim( $order->get_billing_first_name() . ' ' . $order->get_billing_last_name() ), 'legal_name' => EBGI_Checkout::get_legal_name( $order ), 'gstin' => EBGI_Checkout::get_gstin( $order ), 'billing_address' => $order->get_formatted_billing_address(), 'shipping_address' => $order->get_formatted_shipping_address(), 'email' => $order->get_billing_email(), 'phone' => $order->get_billing_phone(), 'country' => $buyer_country, ), 'computed' => $computed, ); } /** * admin-post handler: render the invoice after an access check. */ public static function handle_view() { // phpcs:disable WordPress.Security.NonceVerification.Recommended -- access is gated by capability or the secret order key; guest and email links cannot carry a nonce. $order_id = isset( $_GET['order_id'] ) ? absint( $_GET['order_id'] ) : 0; $key = isset( $_GET['key'] ) ? sanitize_text_field( wp_unslash( $_GET['key'] ) ) : ''; // phpcs:enable $order = $order_id ? wc_get_order( $order_id ) : false; if ( ! $order || ! $order instanceof WC_Order ) { wp_die( esc_html__( 'Order not found.', '8-bit-gst-invoice-for-woocommerce' ), '', array( 'response' => 404 ) ); } $is_admin = current_user_can( 'edit_shop_orders' ); $is_owner = '' !== $key && hash_equals( (string) $order->get_order_key(), $key ); if ( ! $is_admin && ! $is_owner ) { wp_die( esc_html__( 'You are not allowed to view this invoice.', '8-bit-gst-invoice-for-woocommerce' ), '', array( 'response' => 403 ) ); } $is_ready = self::is_ready( $order ); if ( ! $is_ready && ! $is_admin ) { wp_die( esc_html__( 'The GST invoice will be available once the order is paid.', '8-bit-gst-invoice-for-woocommerce' ), '', array( 'response' => 403 ) ); } // Saving a number needs the secret order key, which a forged cross-site link cannot contain. // Capability alone only views the invoice and changes nothing. if ( $is_ready && $is_owner ) { self::ensure_number( $order ); } nocache_headers(); header( 'Content-Type: text/html; charset=utf-8' ); header( 'X-Robots-Tag: noindex, nofollow' ); echo self::render( self::get_data( $order ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- template escapes every value. exit; } /** * Render the invoice HTML. * * @param array $data From get_data(). * @return string */ public static function render( array $data ) { wp_register_style( 'ebgi-invoice', EBGI_URL . 'assets/css/ebgi-invoice.css', array(), EBGI_VERSION ); ob_start(); include EBGI_DIR . 'templates/invoice.php'; return (string) ob_get_clean(); } /** * Button in the admin order "Order actions" box. * * @param int $order_id Order ID. */ public static function admin_metabox_button( $order_id ) { $order = wc_get_order( $order_id ); if ( ! $order ) { return; } $label = self::is_ready( $order ) ? __( 'Print GST Invoice', '8-bit-gst-invoice-for-woocommerce' ) : __( 'Preview Proforma Invoice', '8-bit-gst-invoice-for-woocommerce' ); echo '
  • ' . esc_html( $label ) . '
  • '; } /** * Icon action in the admin orders list. * * @param array $actions Actions. * @param WC_Order $order Order. * @return array */ public static function admin_list_action( $actions, $order ) { if ( $order instanceof WC_Order && self::is_ready( $order ) ) { $actions['ebgi_invoice'] = array( 'url' => self::url( $order ), 'name' => __( 'GST Invoice', '8-bit-gst-invoice-for-woocommerce' ), 'action' => 'ebgi-invoice', ); } return $actions; } /** * "Download / Print GST Invoice" on My Account → Orders. * * @param array $actions Actions. * @param WC_Order $order Order. * @return array */ public static function my_account_action( $actions, $order ) { if ( $order instanceof WC_Order && self::is_ready( $order ) ) { $actions['ebgi_invoice'] = array( 'url' => self::url( $order ), 'name' => __( 'Download / Print GST Invoice', '8-bit-gst-invoice-for-woocommerce' ), ); } return $actions; } /** * Button under the order table (My Account → View order, thank-you page). * * @param WC_Order $order Order. */ public static function order_view_button( $order ) { if ( ! $order instanceof WC_Order || ! self::is_ready( $order ) ) { return; } echo ''; } }