[ name, WooCommerce state codes ]. * The first WooCommerce code is the one WooCommerce core uses today (i18n/states.php); * the rest are legacy / alternate codes still found in old orders or other plugins. * * @var array */ private static $states = array( '01' => array( 'Jammu and Kashmir', array( 'JK' ) ), '02' => array( 'Himachal Pradesh', array( 'HP' ) ), '03' => array( 'Punjab', array( 'PB' ) ), '04' => array( 'Chandigarh', array( 'CH' ) ), '05' => array( 'Uttarakhand', array( 'UK', 'UT' ) ), '06' => array( 'Haryana', array( 'HR' ) ), '07' => array( 'Delhi', array( 'DL' ) ), '08' => array( 'Rajasthan', array( 'RJ' ) ), '09' => array( 'Uttar Pradesh', array( 'UP' ) ), '10' => array( 'Bihar', array( 'BR' ) ), '11' => array( 'Sikkim', array( 'SK' ) ), '12' => array( 'Arunachal Pradesh', array( 'AR' ) ), '13' => array( 'Nagaland', array( 'NL' ) ), '14' => array( 'Manipur', array( 'MN' ) ), '15' => array( 'Mizoram', array( 'MZ' ) ), '16' => array( 'Tripura', array( 'TR' ) ), '17' => array( 'Meghalaya', array( 'ML' ) ), '18' => array( 'Assam', array( 'AS' ) ), '19' => array( 'West Bengal', array( 'WB' ) ), '20' => array( 'Jharkhand', array( 'JH' ) ), '21' => array( 'Odisha', array( 'OD', 'OR' ) ), '22' => array( 'Chhattisgarh', array( 'CT', 'CG' ) ), '23' => array( 'Madhya Pradesh', array( 'MP' ) ), '24' => array( 'Gujarat', array( 'GJ' ) ), '26' => array( 'Dadra and Nagar Haveli and Daman and Diu', array( 'DH', 'DN', 'DD' ) ), '27' => array( 'Maharashtra', array( 'MH' ) ), '29' => array( 'Karnataka', array( 'KA' ) ), '30' => array( 'Goa', array( 'GA' ) ), '31' => array( 'Lakshadweep Islands', array( 'LD' ) ), '32' => array( 'Kerala', array( 'KL' ) ), '33' => array( 'Tamil Nadu', array( 'TN' ) ), '34' => array( 'Puducherry', array( 'PY' ) ), '35' => array( 'Andaman and Nicobar Islands', array( 'AN' ) ), '36' => array( 'Telangana', array( 'TS', 'TG' ) ), '37' => array( 'Andhra Pradesh', array( 'AP' ) ), '38' => array( 'Ladakh', array( 'LA' ) ), ); /** GSTIN state codes that are valid but are not one of the 36 States/UTs. */ private static $special_codes = array( '97' => 'Other Territory', ); /** * Registry of all 36 States/UTs. * * @return array gst_code => array( 'name' => string, 'wc' => primary WC code, 'aliases' => string[] ). */ public static function get_states() { $out = array(); foreach ( self::$states as $code => $row ) { $out[ $code ] = array( 'name' => $row[0], 'wc' => $row[1][0], 'aliases' => $row[1], ); } return $out; } /** * GST state code for a WooCommerce India state code (accepts legacy aliases). * * @param string $wc_state WooCommerce state code, e.g. 'MH'. * @return string Two-digit code or '' when unknown. */ public static function gst_code_from_wc_state( $wc_state ) { $wc_state = strtoupper( trim( (string) $wc_state ) ); if ( '' === $wc_state ) { return ''; } foreach ( self::$states as $code => $row ) { if ( in_array( $wc_state, $row[1], true ) ) { return (string) $code; // PHP turns numeric keys like '27' into int 27. } } return ''; } /** * Primary WooCommerce state code for a GST state code. * * @param string $gst_code Two-digit code. * @return string */ public static function wc_state_from_gst_code( $gst_code ) { $gst_code = (string) $gst_code; return isset( self::$states[ $gst_code ] ) ? self::$states[ $gst_code ][1][0] : ''; } /** * State / UT name for a GST state code. * * @param string $gst_code Two-digit code. * @return string */ public static function state_name( $gst_code ) { $gst_code = (string) $gst_code; if ( isset( self::$states[ $gst_code ] ) ) { return self::$states[ $gst_code ][0]; } if ( isset( self::$special_codes[ $gst_code ] ) ) { return self::$special_codes[ $gst_code ]; } if ( self::OTHER_COUNTRIES_CODE === $gst_code ) { return 'Other Countries'; } return ''; } /** * Uppercase and strip spaces / separators. * * @param string $gstin Raw input. * @return string */ public static function normalize( $gstin ) { return (string) preg_replace( '/[^0-9A-Z]/', '', strtoupper( (string) $gstin ) ); } /** * Official GSTN check character for the first 14 characters. * * Each character's base-36 value is multiplied by an alternating factor (1, 2, 1, 2 ...), * the product is folded as quotient(36) + remainder(36), all folds are summed, and the * check value is (36 - sum mod 36) mod 36. * * @param string $first14 First 14 characters of a GSTIN. * @return string Single check character, or '' if input is invalid. */ public static function checksum_char( $first14 ) { $first14 = strtoupper( (string) $first14 ); if ( 14 !== strlen( $first14 ) ) { return ''; } $sum = 0; for ( $i = 0; $i < 14; $i++ ) { $value = strpos( self::CHARSET, $first14[ $i ] ); if ( false === $value ) { return ''; } $product = $value * ( ( $i % 2 ) + 1 ); $sum += intdiv( $product, 36 ) + ( $product % 36 ); } return self::CHARSET[ ( 36 - ( $sum % 36 ) ) % 36 ]; } /** * Full validation. * * @param string $gstin Raw input. * @return array { * @type bool $valid True when the GSTIN passed every check. * @type string $code ok | empty | length | format | state_code | checksum. * @type string $gstin Normalised GSTIN. * @type string $state_code First two digits (when length is right). * } */ public static function validate( $gstin ) { $g = self::normalize( $gstin ); $out = array( 'valid' => false, 'code' => 'ok', 'gstin' => $g, 'state_code' => '', ); if ( '' === $g ) { $out['code'] = 'empty'; return $out; } if ( 15 !== strlen( $g ) ) { $out['code'] = 'length'; return $out; } $out['state_code'] = substr( $g, 0, 2 ); if ( ! preg_match( self::GSTIN_REGEX, $g ) ) { $out['code'] = 'format'; return $out; } if ( ! isset( self::$states[ $out['state_code'] ] ) && ! isset( self::$special_codes[ $out['state_code'] ] ) ) { $out['code'] = 'state_code'; return $out; } if ( self::checksum_char( substr( $g, 0, 14 ) ) !== $g[14] ) { $out['code'] = 'checksum'; return $out; } $out['valid'] = true; return $out; } /** * Shortcut boolean validation. * * @param string $gstin Raw input. * @return bool */ public static function is_valid( $gstin ) { $r = self::validate( $gstin ); return $r['valid']; } /** * Does the GSTIN's state code match the buyer's WooCommerce billing state? * * @param string $gstin GSTIN. * @param string $wc_state WooCommerce state code. * @return bool */ public static function state_matches( $gstin, $wc_state ) { $g = self::normalize( $gstin ); $code = self::gst_code_from_wc_state( $wc_state ); return '' !== $code && strlen( $g ) >= 2 && substr( $g, 0, 2 ) === $code; } /** * PAN embedded in a GSTIN (characters 3-12). * * @param string $gstin GSTIN. * @return string */ public static function pan_from_gstin( $gstin ) { $g = self::normalize( $gstin ); return 15 === strlen( $g ) ? substr( $g, 2, 10 ) : ''; } /** * Is this a valid 4/6/8 digit HSN / SAC code? * * @param string $hsn Code. * @return bool */ public static function is_valid_hsn( $hsn ) { return 1 === preg_match( self::HSN_REGEX, (string) $hsn ); } /** * Is the slab one of the supported GST rates? * * @param string|float $slab Rate in percent. * @return bool */ public static function is_valid_slab( $slab ) { return in_array( self::slab_key( $slab ), self::GST_SLABS, true ); } /** * Canonical string for a slab (18.00 -> '18', 0.250 -> '0.25'). * * @param string|float $slab Rate. * @return string */ public static function slab_key( $slab ) { if ( ! is_numeric( $slab ) ) { return ''; } $s = rtrim( rtrim( number_format( (float) $slab, 2, '.', '' ), '0' ), '.' ); return '' === $s ? '0' : $s; } /** * Round half away from zero to 2 decimals, done in integer paise so PHP and JS agree. * * @param float $amount Amount. * @return float */ public static function round_money( $amount ) { return self::to_paise( $amount ) / 100; } /** * Amount -> integer paise (half away from zero). * * @param float $amount Amount in rupees. * @return int */ public static function to_paise( $amount ) { $amount = (float) $amount; $sign = $amount < 0 ? -1 : 1; return (int) ( $sign * floor( abs( $amount ) * 100 + 0.5 + 1e-9 ) ); } /** * Intra-state (CGST+SGST) or inter-state (IGST) supply? * * @param string $seller_wc_state Seller WooCommerce state code. * @param string $buyer_wc_state Buyer (place of supply) WooCommerce state code. * @param string $buyer_country Buyer country (ISO-2). Non-IN is always inter-state. * @return string INTRA | INTER */ public static function supply_type( $seller_wc_state, $buyer_wc_state, $buyer_country = 'IN' ) { if ( 'IN' !== strtoupper( (string) $buyer_country ) ) { return 'INTER'; } $seller = self::gst_code_from_wc_state( $seller_wc_state ); $buyer = self::gst_code_from_wc_state( $buyer_wc_state ); return ( '' !== $seller && $seller === $buyer ) ? 'INTRA' : 'INTER'; } /** * Place of supply for a buyer state / country. * * @param string $buyer_wc_state WooCommerce state code. * @param string $buyer_country ISO-2 country. * @return array array( 'code' => '27', 'name' => 'Maharashtra' ). */ public static function place_of_supply( $buyer_wc_state, $buyer_country = 'IN' ) { if ( 'IN' !== strtoupper( (string) $buyer_country ) ) { $code = self::OTHER_COUNTRIES_CODE; } else { $code = self::gst_code_from_wc_state( $buyer_wc_state ); } return array( 'code' => $code, 'name' => self::state_name( $code ), ); } /** * Split a GST amount into CGST/SGST or IGST. * * When $tax_amount is null the tax is computed from $taxable x $slab (CGST and SGST are each * computed at half-rate, so they are always equal). When a known tax amount is given (e.g. the * tax WooCommerce charged), it is preserved exactly: CGST gets the rounded half, SGST the rest. * * @param string $seller_wc_state Seller state (WC code). * @param string $buyer_wc_state Buyer / place-of-supply state (WC code). * @param float $taxable Taxable value. * @param float $slab GST rate in percent. * @param string $buyer_country Buyer country. * @param float|null $tax_amount Optional known total tax. * @return array */ public static function tax_split( $seller_wc_state, $buyer_wc_state, $taxable, $slab, $buyer_country = 'IN', $tax_amount = null ) { $type = self::supply_type( $seller_wc_state, $buyer_wc_state, $buyer_country ); $pos = self::place_of_supply( $buyer_wc_state, $buyer_country ); $rate = (float) $slab; $taxable_paise = self::to_paise( $taxable ); if ( null === $tax_amount ) { if ( 'INTRA' === $type ) { $half_paise = self::to_paise( $taxable_paise * $rate / 200 / 100 ); $cgst_paise = $half_paise; $sgst_paise = $half_paise; $igst_paise = 0; } else { $cgst_paise = 0; $sgst_paise = 0; $igst_paise = self::to_paise( $taxable_paise * $rate / 100 / 100 ); } } else { $tax_paise = self::to_paise( $tax_amount ); if ( 'INTRA' === $type ) { $cgst_paise = self::to_paise( $tax_paise / 2 / 100 ); $sgst_paise = $tax_paise - $cgst_paise; $igst_paise = 0; } else { $cgst_paise = 0; $sgst_paise = 0; $igst_paise = $tax_paise; } } $half_rate = 'INTRA' === $type ? $rate / 2 : 0; return array( 'supply_type' => $type, 'taxable_amount' => $taxable_paise / 100, 'gst_rate' => $rate, 'cgst_rate' => $half_rate, 'cgst_amount' => $cgst_paise / 100, 'sgst_rate' => $half_rate, 'sgst_amount' => $sgst_paise / 100, 'igst_rate' => 'INTER' === $type ? $rate : 0, 'igst_amount' => $igst_paise / 100, 'total_tax' => ( $cgst_paise + $sgst_paise + $igst_paise ) / 100, 'place_of_supply_code' => $pos['code'], 'place_of_supply_name' => $pos['name'], ); } /** * Taxable value contained in a GST-inclusive amount. * * @param float $gross Amount including GST. * @param float $slab GST rate. * @return float */ public static function taxable_from_inclusive( $gross, $slab ) { return self::to_paise( self::to_paise( $gross ) * 100 / ( 100 + (float) $slab ) / 100 ) / 100; } /** * Nearest supported slab for an effective rate (e.g. 17.98 -> '18'), or the rate itself. * * @param float $taxable Taxable value. * @param float $tax Tax charged. * @return string */ public static function detect_slab( $taxable, $tax ) { $taxable = (float) $taxable; if ( abs( $taxable ) < 0.005 ) { return '0'; } $effective = (float) $tax / $taxable * 100; $best = null; foreach ( self::GST_SLABS as $slab ) { $diff = abs( $effective - (float) $slab ); if ( $diff <= 0.5 && ( null === $best || $diff < $best[1] ) ) { $best = array( $slab, $diff ); } } return null === $best ? self::slab_key( round( $effective, 2 ) ) : $best[0]; } /** * Amount in words, Indian numbering (Lakh / Crore). * 145250.50 => "Rupees One Lakh Forty-Five Thousand Two Hundred Fifty and Fifty Paise Only". * * @param float $amount Amount. * @return string */ public static function amount_in_words( $amount ) { $paise_total = abs( self::to_paise( $amount ) ); $rupees = intdiv( $paise_total, 100 ); $paise = $paise_total % 100; $words = 0 === $rupees ? 'Zero' : self::indian_number_words( $rupees ); $out = 'Rupees ' . $words; if ( $paise > 0 ) { $out .= ' and ' . self::two_digit_words( $paise ) . ' Paise'; } return $out . ' Only'; } /** * Words for a positive integer using crore / lakh / thousand / hundred. * * @param int $n Number (> 0). * @return string */ private static function indian_number_words( $n ) { $parts = array(); $crore = intdiv( $n, 10000000 ); $n = $n % 10000000; if ( $crore > 0 ) { $parts[] = self::indian_number_words( $crore ) . ' Crore'; } $lakh = intdiv( $n, 100000 ); $n = $n % 100000; if ( $lakh > 0 ) { $parts[] = self::two_digit_words( $lakh ) . ' Lakh'; } $thousand = intdiv( $n, 1000 ); $n = $n % 1000; if ( $thousand > 0 ) { $parts[] = self::two_digit_words( $thousand ) . ' Thousand'; } $hundred = intdiv( $n, 100 ); $n = $n % 100; if ( $hundred > 0 ) { $parts[] = self::two_digit_words( $hundred ) . ' Hundred'; } if ( $n > 0 ) { $parts[] = self::two_digit_words( $n ); } return implode( ' ', $parts ); } /** * Words for 1..99. * * @param int $n Number. * @return string */ private static function two_digit_words( $n ) { $ones = array( '', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen' ); $tens = array( '', '', 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety' ); if ( $n < 20 ) { return $ones[ $n ]; } $t = intdiv( $n, 10 ); $o = $n % 10; return $tens[ $t ] . ( $o > 0 ? '-' . $ones[ $o ] : '' ); } /** * Indian digit grouping: 14525050.5 => "1,45,25,050.50". * * @param float $amount Amount. * @return string */ public static function format_inr( $amount ) { $paise = self::to_paise( $amount ); $neg = $paise < 0; $paise = abs( $paise ); $int = (string) intdiv( $paise, 100 ); $dec = str_pad( (string) ( $paise % 100 ), 2, '0', STR_PAD_LEFT ); $last3 = substr( $int, -3 ); $rest = substr( $int, 0, -3 ); $grouped = '' === $rest ? $last3 : preg_replace( '/\B(?=(\d{2})+(?!\d))/', ',', $rest ) . ',' . $last3; return ( $neg ? '-' : '' ) . $grouped . '.' . $dec; } /** * Build a complete invoice computation from plain line arrays (no WooCommerce needed). * * Each line: array( * 'name' => string, 'hsn' => string, 'qty' => float, 'slab' => string, * 'taxable' => float, 'tax' => float|null (known tax; null = compute from slab) * ). * * @param array $lines Lines. * @param string $seller_wc_state Seller state. * @param string $buyer_wc_state Place-of-supply state. * @param string $buyer_country Buyer country. * @param float $expected_total Optional order total; difference (< ₹1) is reported as round-off. * @return array */ public static function build_invoice( array $lines, $seller_wc_state, $buyer_wc_state, $buyer_country = 'IN', $expected_total = null ) { $out_lines = array(); $totals = array( 'taxable' => 0, 'cgst' => 0, 'sgst' => 0, 'igst' => 0, 'tax' => 0, 'gross' => 0, ); $hsn = array(); $supply = self::supply_type( $seller_wc_state, $buyer_wc_state, $buyer_country ); $pos = self::place_of_supply( $buyer_wc_state, $buyer_country ); foreach ( $lines as $line ) { $slab = self::slab_key( isset( $line['slab'] ) ? $line['slab'] : 0 ); $known = array_key_exists( 'tax', $line ) && null !== $line['tax'] ? (float) $line['tax'] : null; $split = self::tax_split( $seller_wc_state, $buyer_wc_state, (float) $line['taxable'], (float) $slab, $buyer_country, $known ); $gross = self::to_paise( $split['taxable_amount'] ) + self::to_paise( $split['total_tax'] ); $row = array_merge( $line, array( 'slab' => $slab, 'split' => $split, 'gross' => $gross / 100, ) ); $out_lines[] = $row; $totals['taxable'] += self::to_paise( $split['taxable_amount'] ); $totals['cgst'] += self::to_paise( $split['cgst_amount'] ); $totals['sgst'] += self::to_paise( $split['sgst_amount'] ); $totals['igst'] += self::to_paise( $split['igst_amount'] ); $totals['tax'] += self::to_paise( $split['total_tax'] ); $totals['gross'] += $gross; $code = isset( $line['hsn'] ) && '' !== $line['hsn'] ? (string) $line['hsn'] : 'NA'; $key = $code . '|' . $slab; if ( ! isset( $hsn[ $key ] ) ) { $hsn[ $key ] = array( 'hsn' => $code, 'description' => isset( $line['name'] ) ? (string) $line['name'] : '', 'uqc' => isset( $line['uqc'] ) ? (string) $line['uqc'] : 'NOS-NUMBERS', 'slab' => $slab, 'qty' => 0, 'taxable' => 0, 'cgst' => 0, 'sgst' => 0, 'igst' => 0, 'tax' => 0, 'gross' => 0, ); } $hsn[ $key ]['qty'] += isset( $line['qty'] ) ? (float) $line['qty'] : 0; $hsn[ $key ]['taxable'] += self::to_paise( $split['taxable_amount'] ); $hsn[ $key ]['cgst'] += self::to_paise( $split['cgst_amount'] ); $hsn[ $key ]['sgst'] += self::to_paise( $split['sgst_amount'] ); $hsn[ $key ]['igst'] += self::to_paise( $split['igst_amount'] ); $hsn[ $key ]['tax'] += self::to_paise( $split['total_tax'] ); $hsn[ $key ]['gross'] += $gross; } foreach ( $hsn as $k => $row ) { foreach ( array( 'taxable', 'cgst', 'sgst', 'igst', 'tax', 'gross' ) as $f ) { $hsn[ $k ][ $f ] = $row[ $f ] / 100; } } $round_off = 0; if ( null !== $expected_total ) { $diff = self::to_paise( $expected_total ) - $totals['gross']; if ( abs( $diff ) < 100 ) { $round_off = $diff; } } $grand = $totals['gross'] + $round_off; return array( 'supply_type' => $supply, 'place_of_supply_code' => $pos['code'], 'place_of_supply_name' => $pos['name'], 'lines' => $out_lines, 'hsn_summary' => array_values( $hsn ), 'total_taxable' => $totals['taxable'] / 100, 'total_cgst' => $totals['cgst'] / 100, 'total_sgst' => $totals['sgst'] / 100, 'total_igst' => $totals['igst'] / 100, 'total_tax' => $totals['tax'] / 100, 'round_off' => $round_off / 100, 'grand_total' => $grand / 100, 'amount_in_words' => self::amount_in_words( $grand / 100 ), ); } }