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__( 'GST & B2B Invoices', '8-bit-gst-invoice-for-woocommerce' ) . '

'; $notice = get_transient( 'ebgi_notice_' . get_current_user_id() ); if ( is_array( $notice ) ) { delete_transient( 'ebgi_notice_' . get_current_user_id() ); echo '

' . esc_html( $notice['msg'] ) . '

'; } settings_errors( EBGI_Settings::OPTION ); echo '
'; call_user_func( array( __CLASS__, 'tab_' . $tab ) ); echo '
'; } /** * Settings tab. */ private static function tab_settings() { $s = EBGI_Settings::all(); $name = EBGI_Settings::OPTION; $states = EBGI_GSTIN_Validator::get_states(); $slabs = EBGI_GSTIN_Validator::GST_SLABS; echo '
'; settings_fields( 'ebgi_settings_group' ); echo '

' . esc_html__( 'Seller details (printed on every invoice)', '8-bit-gst-invoice-for-woocommerce' ) . '

'; self::text_row( $name, 'seller_gstin', __( 'Seller GSTIN', '8-bit-gst-invoice-for-woocommerce' ), $s['seller_gstin'], __( 'Validated with the official Mod-36 check digit.', '8-bit-gst-invoice-for-woocommerce' ), 'ebgi-upper' ); self::text_row( $name, 'legal_name', __( 'Legal Name', '8-bit-gst-invoice-for-woocommerce' ), $s['legal_name'] ); self::text_row( $name, 'trade_name', __( 'Trade Name', '8-bit-gst-invoice-for-woocommerce' ), $s['trade_name'] ); echo ''; self::text_row( $name, 'pan', __( 'PAN', '8-bit-gst-invoice-for-woocommerce' ), $s['pan'], __( 'Leave empty to take it from the GSTIN.', '8-bit-gst-invoice-for-woocommerce' ), 'ebgi-upper' ); echo ''; self::text_row( $name, 'signatory_name', __( 'Authorised Signatory', '8-bit-gst-invoice-for-woocommerce' ), $s['signatory_name'] ); echo ''; echo '

' . esc_html__( 'Invoice & tax', '8-bit-gst-invoice-for-woocommerce' ) . '

'; self::text_row( $name, 'invoice_prefix', __( 'Invoice Prefix', '8-bit-gst-invoice-for-woocommerce' ), $s['invoice_prefix'], __( 'Letters, digits, "-" and "/" only (GST allows max 16 characters per invoice number).', '8-bit-gst-invoice-for-woocommerce' ) ); self::select_row( $name, 'tax_source', __( 'Tax calculation', '8-bit-gst-invoice-for-woocommerce' ), $s['tax_source'], array( 'inclusive' => __( 'Product prices include GST (back-calculate tax when WooCommerce charged none)', '8-bit-gst-invoice-for-woocommerce' ), 'woocommerce' => __( 'Use only the tax WooCommerce charged (items without tax show 0%)', '8-bit-gst-invoice-for-woocommerce' ), ), __( 'When WooCommerce charged tax on an item, that exact amount is always used.', '8-bit-gst-invoice-for-woocommerce' ) ); self::select_row( $name, 'default_gst_slab', __( 'Default GST rate', '8-bit-gst-invoice-for-woocommerce' ), $s['default_gst_slab'], array_combine( $slabs, array_map( array( __CLASS__, 'pct' ), $slabs ) ), __( 'For products without their own GST rate.', '8-bit-gst-invoice-for-woocommerce' ) ); self::select_row( $name, 'shipping_rate_mode', __( 'Shipping GST rate', '8-bit-gst-invoice-for-woocommerce' ), $s['shipping_rate_mode'], array( 'principal' => __( 'Same as the main (highest value) item — composite supply', '8-bit-gst-invoice-for-woocommerce' ), 'fixed' => __( 'Fixed rate (below)', '8-bit-gst-invoice-for-woocommerce' ), ) ); self::select_row( $name, 'shipping_gst_slab', __( 'Fixed shipping rate', '8-bit-gst-invoice-for-woocommerce' ), $s['shipping_gst_slab'], array_combine( $slabs, array_map( array( __CLASS__, 'pct' ), $slabs ) ) ); self::text_row( $name, 'shipping_sac', __( 'Shipping SAC code', '8-bit-gst-invoice-for-woocommerce' ), $s['shipping_sac'] ); self::select_row( $name, 'default_uqc', __( 'Default unit (UQC)', '8-bit-gst-invoice-for-woocommerce' ), $s['default_uqc'], array_combine( self::UQC, self::UQC ), __( 'Used in the GSTR-1 HSN summary.', '8-bit-gst-invoice-for-woocommerce' ) ); self::text_row( $name, 'b2cl_threshold', __( 'B2C Large threshold (₹)', '8-bit-gst-invoice-for-woocommerce' ), $s['b2cl_threshold'], __( 'Inter-state B2C invoices above this value go to B2CL (₹1,00,000 from 1-Aug-2024).', '8-bit-gst-invoice-for-woocommerce' ) ); self::check_row( $name, 'enforce_state_match', __( 'GSTIN must match billing state', '8-bit-gst-invoice-for-woocommerce' ), $s['enforce_state_match'], __( 'Reject checkout when the GSTIN state code differs from the billing state.', '8-bit-gst-invoice-for-woocommerce' ) ); self::check_row( $name, 'sync_tax_class', __( 'Auto-assign GST tax class', '8-bit-gst-invoice-for-woocommerce' ), $s['sync_tax_class'], __( 'When saving a product with a GST rate, set its tax class to the matching "GST n%" class (create them with the button below).', '8-bit-gst-invoice-for-woocommerce' ) ); echo ''; echo ''; submit_button(); echo '
'; echo '

' . esc_html__( 'Auto-populate WooCommerce GST tax rates', '8-bit-gst-invoice-for-woocommerce' ) . '

'; echo '

' . 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 '
'; wp_nonce_field( 'ebgi_generate_tax_rates' ); submit_button( __( 'Auto-Populate WooCommerce GST Tax Rates', '8-bit-gst-invoice-for-woocommerce' ), 'secondary', 'submit', false ); echo '
'; } /** * Bulk HSN tab. */ private static function tab_bulk() { echo '

' . esc_html__( 'Assign HSN / SAC by category', '8-bit-gst-invoice-for-woocommerce' ) . '

'; echo '
'; wp_nonce_field( 'ebgi_bulk_category' ); echo ''; echo ''; echo ''; echo ''; submit_button( __( 'Apply to category', '8-bit-gst-invoice-for-woocommerce' ) ); echo '

'; echo '

' . esc_html__( 'Import HSN codes from CSV', '8-bit-gst-invoice-for-woocommerce' ) . '

' . 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

'; echo '
'; wp_nonce_field( 'ebgi_bulk_csv' ); echo ' '; submit_button( __( 'Import CSV', '8-bit-gst-invoice-for-woocommerce' ), 'secondary', 'submit', false ); echo '
'; } /** * GSTR-1 tab. */ private static function tab_gstr1() { if ( ! current_user_can( 'manage_woocommerce' ) ) { return; } echo '

' . esc_html__( '1-Click GSTR-1 export for your CA', '8-bit-gst-invoice-for-woocommerce' ) . '

'; echo '

' . 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' ) . '

'; } echo '
'; wp_nonce_field( 'ebgi_gstr1_preview', 'ebgi_preview_nonce', false ); echo ' '; submit_button( __( 'Preview', '8-bit-gst-invoice-for-woocommerce' ), 'secondary', 'submit', false ); echo '
'; $period = EBGI_GSTR1_Export::period_from_request( array( 'ebgi_month' => $month ) ); $records = EBGI_GSTR1_Export::collect( $period[0], $period[1] ); $sum = EBGI_GSTR1_Export::summary( $records, (float) EBGI_Settings::get( 'b2cl_threshold' ) ); echo ''; $rows = array( __( 'Invoices', '8-bit-gst-invoice-for-woocommerce' ) => $sum['invoices'], __( 'B2B (buyer has GSTIN)', '8-bit-gst-invoice-for-woocommerce' ) => $sum['b2b'], __( 'B2C Large (inter-state)', '8-bit-gst-invoice-for-woocommerce' ) => $sum['b2cl'], __( 'B2C Small', '8-bit-gst-invoice-for-woocommerce' ) => $sum['b2cs'], __( 'Exports (not in these CSVs)', '8-bit-gst-invoice-for-woocommerce' ) => $sum['exp'], __( 'Taxable value (₹)', '8-bit-gst-invoice-for-woocommerce' ) => EBGI_GSTIN_Validator::format_inr( $sum['taxable'] ), __( 'Total GST (₹)', '8-bit-gst-invoice-for-woocommerce' ) => EBGI_GSTIN_Validator::format_inr( $sum['tax'] ), ); foreach ( $rows as $label => $value ) { echo ''; } echo '
' . esc_html( $label ) . '' . esc_html( (string) $value ) . '
'; if ( $sum['missing_hsn'] > 0 ) { /* translators: %d: number of lines */ echo '

' . 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'] ) ) . '

'; } $sections = array( 'b2b' => __( 'B2B Invoices (b2b)', '8-bit-gst-invoice-for-woocommerce' ), 'b2cl' => __( 'B2C Large (b2cl)', '8-bit-gst-invoice-for-woocommerce' ), 'b2cs' => __( 'B2C Small Summary (b2cs)', '8-bit-gst-invoice-for-woocommerce' ), 'hsn_b2b' => __( 'HSN Summary – B2B (hsn(b2b))', '8-bit-gst-invoice-for-woocommerce' ), 'hsn_b2c' => __( 'HSN Summary – B2C (hsn(b2c))', '8-bit-gst-invoice-for-woocommerce' ), ); echo '

'; foreach ( $sections as $key => $label ) { echo '

'; wp_nonce_field( 'ebgi_gstr1_export' ); submit_button( '⬇ ' . $label, 'primary', 'submit', false ); 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 ''; if ( '' !== $desc ) { echo '

' . esc_html( $desc ) . '

'; } echo ''; } /** * Select row. * * @param string $name Option name. * @param string $key Key. * @param string $label Label. * @param string $value Value. * @param array $options value => label. * @param string $desc Description. */ private static function select_row( $name, $key, $label, $value, array $options, $desc = '' ) { echo ''; if ( '' !== $desc ) { echo '

' . esc_html( $desc ) . '

'; } echo ''; } /** * Checkbox row. * * @param string $name Option name. * @param string $key Key. * @param string $label Label. * @param string $value yes|no. * @param string $desc Description. */ private static function check_row( $name, $key, $label, $value, $desc = '' ) { echo '' . esc_html( $label ) . ''; } }