id, array( 'edit-shop_order', 'shop_order', 'woocommerce_page_wc-orders' ), true ); if ( $ours || $orders ) { wp_enqueue_style( 'ebgi-admin', EBGI_URL . 'assets/css/ebgi-admin.css', array(), EBGI_VERSION ); } } /** * Register the settings option with a strict sanitiser. */ public static function register_settings() { register_setting( 'ebgi_settings_group', EBGI_Settings::OPTION, array( 'type' => 'array', 'sanitize_callback' => array( __CLASS__, 'sanitize_settings' ), 'default' => EBGI_Settings::defaults(), ) ); } /** * Sanitise every setting; invalid values fall back to the previous value with an error. * * @param mixed $input Raw input. * @return array */ public static function sanitize_settings( $input ) { $old = EBGI_Settings::all(); $input = is_array( $input ) ? $input : array(); $out = $old; $get = function ( $key ) use ( $input ) { return isset( $input[ $key ] ) ? (string) $input[ $key ] : ''; }; $gstin = EBGI_GSTIN_Validator::normalize( $get( 'seller_gstin' ) ); if ( '' === $gstin || EBGI_GSTIN_Validator::is_valid( $gstin ) ) { $out['seller_gstin'] = $gstin; } else { self::settings_error( 'ebgi_gstin', __( 'Seller GSTIN is invalid (format or check digit) and was not saved.', '8-bit-gst-invoice-for-woocommerce' ) ); } $out['legal_name'] = sanitize_text_field( $get( 'legal_name' ) ); $out['trade_name'] = sanitize_text_field( $get( 'trade_name' ) ); $out['address'] = sanitize_textarea_field( $get( 'address' ) ); $pan = strtoupper( preg_replace( '/\s+/', '', $get( 'pan' ) ) ); $out['pan'] = preg_match( '/^[A-Z]{5}[0-9]{4}[A-Z]$/', $pan ) ? $pan : EBGI_GSTIN_Validator::pan_from_gstin( $out['seller_gstin'] ); $state = strtoupper( sanitize_text_field( $get( 'store_state' ) ) ); if ( '' === EBGI_GSTIN_Validator::gst_code_from_wc_state( $state ) && '' !== $out['seller_gstin'] ) { $state = EBGI_GSTIN_Validator::wc_state_from_gst_code( substr( $out['seller_gstin'], 0, 2 ) ); } $out['store_state'] = '' !== EBGI_GSTIN_Validator::gst_code_from_wc_state( $state ) ? $state : ''; if ( '' !== $out['seller_gstin'] && '' !== $out['store_state'] && ! EBGI_GSTIN_Validator::state_matches( $out['seller_gstin'], $out['store_state'] ) ) { self::settings_error( 'ebgi_state', __( 'Warning: the Store State does not match the state code in the Seller GSTIN.', '8-bit-gst-invoice-for-woocommerce' ), 'warning' ); } $prefix = substr( preg_replace( '#[^A-Za-z0-9/\-]#', '', $get( 'invoice_prefix' ) ), 0, 10 ); $out['invoice_prefix'] = $prefix; $out['tax_source'] = in_array( $get( 'tax_source' ), array( 'inclusive', 'woocommerce' ), true ) ? $get( 'tax_source' ) : 'inclusive'; $out['shipping_rate_mode'] = in_array( $get( 'shipping_rate_mode' ), array( 'principal', 'fixed' ), true ) ? $get( 'shipping_rate_mode' ) : 'principal'; foreach ( array( 'default_gst_slab', 'shipping_gst_slab' ) as $k ) { $out[ $k ] = EBGI_GSTIN_Validator::is_valid_slab( $get( $k ) ) ? EBGI_GSTIN_Validator::slab_key( $get( $k ) ) : $old[ $k ]; } $sac = preg_replace( '/\s+/', '', $get( 'shipping_sac' ) ); $out['shipping_sac'] = EBGI_GSTIN_Validator::is_valid_hsn( $sac ) ? $sac : ''; $out['default_uqc'] = in_array( $get( 'default_uqc' ), self::UQC, true ) ? $get( 'default_uqc' ) : 'NOS-NUMBERS'; $out['enforce_state_match'] = 'yes' === $get( 'enforce_state_match' ) ? 'yes' : 'no'; $out['sync_tax_class'] = 'yes' === $get( 'sync_tax_class' ) ? 'yes' : 'no'; $threshold = absint( $get( 'b2cl_threshold' ) ); $out['b2cl_threshold'] = (string) ( $threshold > 0 ? $threshold : 100000 ); $out['signatory_name'] = sanitize_text_field( $get( 'signatory_name' ) ); $out['invoice_footer'] = sanitize_textarea_field( $get( 'invoice_footer' ) ); return $out; } /** * add_settings_error() only exists in wp-admin; ignore elsewhere (e.g. programmatic update_option()). * * @param string $code Code. * @param string $msg Message. * @param string $type Type. */ private static function settings_error( $code, $msg, $type = 'error' ) { if ( function_exists( 'add_settings_error' ) ) { add_settings_error( EBGI_Settings::OPTION, $code, $msg, $type ); } } /** * Redirect back to a tab with a one-time notice. * * @param string $tab Tab. * @param string $msg Message. * @param string $type success | error | warning | info. */ public static function redirect_with_notice( $tab, $msg, $type = 'success' ) { set_transient( 'ebgi_notice_' . get_current_user_id(), array( 'msg' => $msg, 'type' => $type, ), 60 ); wp_safe_redirect( admin_url( 'admin.php?page=' . self::PAGE . '&tab=' . rawurlencode( $tab ) ) ); exit; } /** * Capability check shared by the admin-post handlers. */ private static function require_capability() { if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', '8-bit-gst-invoice-for-woocommerce' ), 403 ); } } /** * generate tax classes + rates. */ public static function handle_generate_rates() { self::require_capability(); check_admin_referer( 'ebgi_generate_tax_rates' ); $result = EBGI_Tax_Rates::generate( EBGI_Settings::seller_state() ); if ( is_wp_error( $result ) ) { self::redirect_with_notice( 'settings', $result->get_error_message(), 'error' ); } /* translators: 1: classes created, 2: rates written */ self::redirect_with_notice( 'settings', sprintf( __( 'GST tax rates ready: %1$d new tax classes, %2$d CGST/SGST/IGST rates written. Assign the "GST n%%" tax class to your products (or enable auto-sync).', '8-bit-gst-invoice-for-woocommerce' ), $result['classes'], $result['rates'] ) ); } /** * category → HSN. */ public static function handle_bulk_category() { self::require_capability(); check_admin_referer( 'ebgi_bulk_category' ); $term = isset( $_POST['ebgi_cat'] ) ? absint( $_POST['ebgi_cat'] ) : 0; $hsn = isset( $_POST['ebgi_hsn'] ) ? preg_replace( '/\s+/', '', sanitize_text_field( wp_unslash( $_POST['ebgi_hsn'] ) ) ) : ''; $slab = isset( $_POST['ebgi_slab'] ) ? sanitize_text_field( wp_unslash( $_POST['ebgi_slab'] ) ) : ''; $overwrite = ! empty( $_POST['ebgi_overwrite'] ); $result = EBGI_Product_HSN::apply_to_category( $term, $hsn, $slab, $overwrite ); if ( is_wp_error( $result ) ) { self::redirect_with_notice( 'bulk', $result->get_error_message(), 'error' ); } /* translators: 1: updated, 2: skipped */ self::redirect_with_notice( 'bulk', sprintf( __( 'HSN applied: %1$d products updated, %2$d skipped (already had an HSN).', '8-bit-gst-invoice-for-woocommerce' ), $result['updated'], $result['skipped'] ) ); } /** * CSV → HSN. */ public static function handle_bulk_csv() { self::require_capability(); check_admin_referer( 'ebgi_bulk_csv' ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- tmp_name is validated with is_uploaded_file(). $tmp = isset( $_FILES['ebgi_csv']['tmp_name'] ) ? (string) $_FILES['ebgi_csv']['tmp_name'] : ''; if ( '' === $tmp || ! is_uploaded_file( $tmp ) ) { self::redirect_with_notice( 'bulk', __( 'Please choose a CSV file.', '8-bit-gst-invoice-for-woocommerce' ), 'error' ); } $result = EBGI_Product_HSN::apply_csv( $tmp ); if ( is_wp_error( $result ) ) { self::redirect_with_notice( 'bulk', $result->get_error_message(), 'error' ); } /* translators: 1: updated, 2: not found, 3: invalid */ self::redirect_with_notice( 'bulk', sprintf( __( 'CSV import: %1$d products updated, %2$d SKUs/IDs not found, %3$d rows with invalid HSN or rate.', '8-bit-gst-invoice-for-woocommerce' ), $result['updated'], $result['not_found'], $result['invalid'] ) ); } /** * Page shell + tabs. */ public static function render() { if ( ! current_user_can( 'manage_woocommerce' ) ) { return; } $tabs = array( 'settings' => __( 'Store GST Settings', '8-bit-gst-invoice-for-woocommerce' ), 'bulk' => __( 'Bulk HSN Codes', '8-bit-gst-invoice-for-woocommerce' ), 'gstr1' => __( 'GSTR-1 Export', '8-bit-gst-invoice-for-woocommerce' ), ); $tab = isset( $_GET['tab'] ) ? sanitize_key( wp_unslash( $_GET['tab'] ) ) : 'settings'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- navigation only. $tab = isset( $tabs[ $tab ] ) ? $tab : 'settings'; echo '
' . esc_html( $notice['msg'] ) . '
' . esc_html__( 'Creates "GST n%" tax classes for every slab with CGST + SGST for your store state and IGST for all other states. Only rates named CGST/SGST/IGST inside these GST classes are replaced; your Standard rates are never touched.', '8-bit-gst-invoice-for-woocommerce' ) . '
'; echo ''; } /** * Bulk HSN tab. */ private static function tab_bulk() { echo '' . esc_html__( 'Columns: SKU (or product ID), HSN code, GST rate (optional). A header row is allowed.', '8-bit-gst-invoice-for-woocommerce' ) . ' SKU-001,61091000,5
' . esc_html__( 'Paid orders (Processing + Completed) are exported in the GST offline-tool CSV format. Refunds / credit notes (CDNR) and exports are not included — add them separately.', '8-bit-gst-invoice-for-woocommerce' ) . '
'; // The requested month is used only with a valid nonce; otherwise the preview says so and shows last month. $last_month = current_datetime()->modify( 'first day of last month' )->format( 'Y-m' ); $month = $last_month; $nonce = isset( $_GET['ebgi_preview_nonce'] ) ? sanitize_text_field( wp_unslash( $_GET['ebgi_preview_nonce'] ) ) : ''; if ( wp_verify_nonce( $nonce, 'ebgi_gstr1_preview' ) ) { $requested = isset( $_GET['ebgi_month'] ) ? sanitize_text_field( wp_unslash( $_GET['ebgi_month'] ) ) : ''; $month = EBGI_GSTR1_Export::is_valid_month( $requested ) ? $requested : $last_month; } elseif ( isset( $_GET['ebgi_month'] ) ) { echo '' . esc_html__( 'This preview link has expired, so last month is shown. Please choose the month again.', '8-bit-gst-invoice-for-woocommerce' ) . '
| ' . esc_html( $label ) . ' | ' . esc_html( (string) $value ) . ' |
|---|
' . esc_html( sprintf( __( '%d invoice lines have no HSN/SAC code. Add HSN codes to those products before filing.', '8-bit-gst-invoice-for-woocommerce' ), $sum['missing_hsn'] ) ) . '
'; foreach ( $sections as $key => $label ) { echo '
'; } echo ''; } /** * "18%". * * @param string $slab Slab. * @return string */ public static function pct( $slab ) { return $slab . '%'; } /** * Text input row. * * @param string $name Option name. * @param string $key Key. * @param string $label Label. * @param string $value Value. * @param string $desc Description. * @param string $class Extra CSS class. */ private static function text_row( $name, $key, $label, $value, $desc = '', $class = '' ) { echo '' . esc_html( $desc ) . '
'; } echo '' . esc_html( $desc ) . '
'; } echo '