$versions{'tax_settings-ext_lib.pl'} = '06.6.00.0000';

# Contains all sub routines (functions) for the sales/VAT taxes, Avalara API &
# custom tax logic settings that the storefront (customer facing) functions use.

#######################################################################
#
# AgoraCart and all associated files, except where noted, are
# Copyright 2001 to Present jointly by K-Factor Technologies, Inc.
# and by C E Mayo (aka Mister Ed) at AgoraCart.com & K-Factor.net
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This copyright notice may not be removed or altered in any way.
#
#######################################################################

{
    local ($modname) = 'tax_settings';
    register_extension( $modname, 'Tax Logic', $versions{$modname} );
    add_settings_choice( 'tax settings', ' Tax Settings & Logic ',
        'tax_settings_screen' );
    register_menu( 'ChangeTaxSettings', 'action_input_tax_settings', $modname,
        "Write Tax Logic" );
    register_menu( 'tax_settings_screen', 'show_tax_settings_screen', $modname,
        'Display Tax Logic' );

    $menu_items{'avalara_log'} = 'avalara_log_display';
    $menu_items{'clear_avalara_log'} = 'clear_avalara_txn_log';
    $menu_items{'test_avalara'} = 'test_avalara_connection';

    # Avalara v1.0 API
    $sc_prod_avalara_v1_tax = 'https://avatax.avalara.net/1.0/tax/get';
    $sc_dev_avalara_v1_tax = 'https://development.avalara.net/1.0/tax/get';
    $sc_dev_avalara_v1_address = 'https://development.avalara.net/1.0/address/validate'; # using v2 API for addresses
    $avalara_cancel_url_test = 'https://development.avalara.net/1.0/tax/cancel';
    $avalara_cancel_url_prod = 'https://avatax.avalara.net/1.0/tax/cancel';
    # Avalara v2 API
    $sc_dev_avalara_v2_address = 'https://sandbox-rest.avatax.com/api/v2/addresses/resolve';
    $sc_prod_avalara_v2_address = 'https://rest.avatax.com/api/v2/addresses/resolve';
}

#######################################################################

sub show_tax_settings_screen {

    print &$manager_page_header( 'Tax Settings Manager', '', '', '', '' );

    $mgr_avatax_test_button = '';
    $is_avalara_enabled = 'no';

    if ( $mc_tax_logic_rows < 5 ) {
        $mc_tax_logic_rows = 5;
    }

    if ( $mc_tax_logic_rows > 20 ) {
        $mc_tax_logic_rows = 20;
    }

    if ( $sc_debug_mode eq '' ) {
        $sc_debug_mode = 'no';
    }

    if ( $sc_avalara_debug_req_and_response eq '' ) {
        $sc_avalara_debug_req_and_response = 'no';
    }

    if ( $sc_avalara_txn_logging eq '' ) {
        $sc_avalara_txn_logging = 'no';
    }

    if ( $sc_avalara_document_commit eq '' ) {
        $sc_avalara_document_commit = 'yes';
    }

    if ( $sc_avalara_tax_enable eq '' ) {
        $sc_avalara_tax_enable = 'no';
    }

    if ( $sc_avalara_verify_address_enable eq '' ) {
        $sc_avalara_verify_address_enable = 'no';
    }

    if ( $sc_avalara_tax_mode eq '' ) {
        $sc_avalara_tax_mode = 'Sandbox';
    }

    if ( $avalara_currency_code eq '' ) {
        $avalara_currency_code = 'USD';
    }

    if ( $sc_path_of_images_directory ) {
        $URL_of_images_directory = $sc_path_of_images_directory;
    }

    if ( $sc_sales_tax_form_variable eq '' ) {
        $sc_sales_tax_form_variable = 'Ecom_ShipTo_Postal_StateProv';
    }

    if ( $sc_sales_tax_roundat4 eq '' ) {
        $sc_sales_tax_roundat4 = 'No';
    }

    if ( ( $sc_avalara_enabled eq 'yes' ) && ( $sc_avalara_acct_number ne '' )
        && ( $sc_avalara_license_key ne '' ) && ( $avalara_origin_address_line1 ne '' )
        && ( $avalara_origin_city ne '' ) && ( $avalara_origin_country ne '' )
        && ( ( $sc_avalara_verify_address_enable eq 'yes' ) || ( $sc_avalara_tax_enable eq 'yes' ) )
        ) {
            $mgr_avatax_test_button = qq|<p><a href="manager.cgi?mgr_seshun=$mgr_seshun&test_avalara=yes" class="btn btn-success btn-block" role="button">Test Avalara Connection with Your Settings</a></p><p><br></p>|;
    }

    print <<ENDOFTEXT;
<h1 class='mgr-page-title'>Tax Settings</h1>
<p><br></p>

<p class="well">Use the tabs below to view each sub-section. You may change the settings within as many tabs/groups as needed, then press the <b><i>submit</i></b> button at the very bottom of the page after you are finished making any desired changes. Click on the <i class="fa fa-info-circle"></i> icon next to any setting for more information on the setting.</p>
<p><br></p>
ENDOFTEXT

    if ( $in{'system_edit_success'} ) {
        print <<ENDOFTEXT;
<div class="row col-xs-12 col-sm-12 col-md-12 col-lg-12 hidden-xs">
    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 bg-success">
        <h4 class="text-primary mgr-center">Tax settings have been successfully updated.</h4>
    </div>
</div>
<p><br><br><br></p>
ENDOFTEXT
    }

    make_lists_of_various_options();

    if ( $mc_mgr_plugins_enabled =~ /avalara/ ) {
        $is_avalara_enabled = 'yes';
    }

    print <<ENDOFTEXT;
<ul class="nav nav-tabs" id="tabs6" role="tablist">
    <li role="presentation" class="active"><a href="#tabs-1" aria-controls="tabs-1" role="tab" data-toggle="tab">Sales/VAT/HST/GST/PST Settings</a></li>
    <li role="presentation"><a href="#tabs-2" aria-controls="tabs-2" role="tab" data-toggle="tab">Non-Taxable Settings</a></li>
    <li role="presentation"><a href="#tabs-3" aria-controls="tabs-3" role="tab" data-toggle="tab">Custom Tax Logic</a></li>
ENDOFTEXT
    if ( $is_avalara_enabled eq 'yes' ) {
        print <<ENDOFTEXT;
        <li role="presentation"><a href="#tabs-4" aria-controls="tabs-4" role="tab" data-toggle="tab">Avalara</a></li>
ENDOFTEXT
    }

    print <<ENDOFTEXT;
</ul>
<form class="form-horizontal" action="manager.cgi" method="post">
<input type="hidden" name="mgr_seshun" value="$in{'mgr_seshun'}">
<div class="tab-content">
<div class="tab-pane active" id="tabs-1">

<p><br></p>
<h2 class="mgr-page-title"><b>Sales / VAT Tax Settings</b></h2>
<p><br></p>

<p class="well">Currently Supporting USA sales taxes, and Canadian GST / HST / PST taxes. Simple versions of European VAT taxes are supported. Additional City, State, Provincial or product taxes, can be configured in the custom tax logic tab area.<br><br>Skip this section if Avalara AvaTax is enabled. These settings are overridden by enabling Avalara AvaTax.</p>
<p><br></p>

<p><b>Select Taxing Jurisdiction: &nbsp; <a data-toggle="collapse" href="#salesTaxJuris" aria-expanded="false" aria-controls="salesTaxJuris"><i class="fa fa-info-circle"></i></a></b><span style="padding-left: 25px;"></span><select name=sales_tax_state>
<option>$sc_sales_tax_state</option>
<option></option>
<option>None</option>
<option>European Union</option>
<option>Canada</option>
<option></option>
<option>AL</option>
<option>AK</option>
<option>AZ</option>
<option>AR</option>
<option>CA</option>
<option>CO</option>
<option>CT</option>
<option>DC</option>
<option>DE</option>
<option>FL</option>
<option>GA</option>
<option>GU</option>
<option>HI</option>
<option>ID</option>
<option>IL</option>
<option>IN</option>
<option>IA</option>
<option>KS</option>
<option>KY</option>
<option>LA</option>
<option>ME</option>
<option>MD</option>
<option>MA</option>
<option>MI</option>
<option>MN</option>
<option>MS</option>
<option>MO</option>
<option>MT</option>
<option>NE</option>
<option>NV</option>
<option>NH</option>
<option>NJ</option>
<option>NM</option>
<option>NY</option>
<option>NC</option>
<option>ND</option>
<option>OH</option>
<option>OK</option>
<option>OR</option>
<option>PA</option>
<option>PR</option>
<option>RI</option>
<option>SC</option>
<option>SD</option>
<option>TN</option>
<option>TX</option>
<option>UT</option>
<option>VI</option>
<option>VT</option>
<option>VA</option>
<option>WA</option>
<option>WV</option>
<option>WI</option>
<option>WY</option>
</select></p>
<div class="collapse" id="salesTaxJuris">
    <div class="well">
        <p><b>US</b>: Select the taxing state. Customers shipping orders to the state selected here
        will be charged sales tax.</p>
        <p><b>Canada:</b> Select "Canada". HST / GST / PST rates will be used and computed.</p>
        <p><b>EU</b>: A simple version of the European Union VAT tax will be computed.</p>
    </div>
</div>
<p><hr></p>

<p><b>Enter Tax Rate: &nbsp; <a data-toggle="collapse" href="#taxRate" aria-expanded="false" aria-controls="taxRate"><i class="fa fa-info-circle"></i></a></b><span style="padding-left: 25px;"></span><input name="sales_tax" type="sales_tax" size="10" maxlength="10" value="$sc_sales_tax"></p>
<div class="collapse" id="taxRate">
    <div class="well">
          <p>Enter as a decimal number. For example, enter <b>.05</b>" for "5%", "<b>.0625</b>" for "6.25%", etc.</p>
          <p><b>US</b>: enter state tax rate.</p>
          <p><b>Canada</b>: enter HST rate.</p>
          <p><b>EU</b>: enter VAT rate.</p>
    </div>
</div>
<p><hr></p>

<p><b>Tax Rate Rounds Up at 4? &nbsp; <a data-toggle="collapse" href="#taxRateRounding4" aria-expanded="false" aria-controls="taxRateRounding4"><i class="fa fa-info-circle"></i></a></b><span style="padding-left: 25px;"></span><select name="sc_sales_tax_roundat4">
<option value="$sc_sales_tax_roundat4">$sc_sales_tax_roundat4</option>
<option value="No">No</option>
<option value="Yes">Yes</option>
</select></p>
<div class="collapse" id="taxRateRounding4">
    <div class="well">
          <p>Default is NO.</p>
          <p>Does your tax rate round up at 4? (instead of 5). In other words, does 5.4 cents become become 6 cents or does it stay as 5 cents?</p>
          <p>Some taxing authorities require fractions of cents (pennies) to be rounded up to the next penny if the computed tax rate results in a tax amount with a fraction of a cent of .40 or higher.</p>
    </div>
</div>
<p><hr></p>

<p><b>CANADA ONLY: Enter GST Tax Rate: &nbsp; <a data-toggle="collapse" href="#GSTtaxRate" aria-expanded="false" aria-controls="GSTtaxRate"><i class="fa fa-info-circle"></i></a></b><span style="padding-left: 25px;"></span><input name="sc_replyto_email" type="GSTsales_tax" size="10" maxlength="10" value="$sc_GSTsales_tax"></p>
<div class="collapse" id="GSTtaxRate">
    <div class="well">
          <p>Enter as a decimal number. Leave blank for all other countries</p>
          <p>For example, enter "<b>.05</b>" for "5%", "<b>.0625</b>" for "6.25%", etc.</p>
    </div>
</div>
<p><br></p>

<p><b>CANADA ONLY: Enter PST Tax Rate: &nbsp; <a data-toggle="collapse" href="#PSTtaxRate" aria-expanded="false" aria-controls="PSTtaxRate"><i class="fa fa-info-circle"></i></a></b><span style="padding-left: 25px;"></span><input name="sc_replyto_email" type="PSTsales_tax" size="10" maxlength="10" value="$sc_PSTsales_tax"></p>
<div class="collapse" id="PSTtaxRate">
    <div class="well">
          <p>Enter as a decimal number. Leave blank for all other countries</p>
          <p>For example, enter "<b>.05</b>" for "5%", "<b>.0625</b>" for "6.25%", etc.</p>
    </div>
</div>
<p><hr></p>

<p><b>Select Sales Tax Order Form Variable: &nbsp; <a data-toggle="collapse" href="#taxOFvariable" aria-expanded="false" aria-controls="taxOFvariable"><i class="fa fa-info-circle"></i></a></b><span style="padding-left: 25px;"></span><select name="sc_sales_tax_form_variable">
<option value="$sc_sales_tax_form_variable">$sc_sales_tax_form_variable &nbsp;</option>
<option value="Ecom_ShipTo_Postal_StateProv">Ship to State / Province &nbsp;</option>
<option value="Ecom_ShipTo_Postal_CountryCode">Ship to Country &nbsp;</option>
<option value="Ecom_ShipTo_Postal_City">Ship to City &nbsp;</option>
</select></p>
<div class="collapse" id="taxOFvariable">
    <div class="well">
          <p>Defaults; <b>US</b> - default is the Ship to State. <b>All other countries</b> - default is the Ship to Country.</p>
          <p>Select the sales tax order form item that the sales tax routines will loook for and use to determine if taxes are applicable and any rates that might apply.</p>
          <p>The most common taxing jurisdictions are States/Provinces, Countries, and even Cities. In most cases, once the tax routines encounter the selected form element, the customer's "ship to" location will be used to compute any Sales / VAT type taxes</p>
          <p></p>
    </div>
</div>

<p><br></p>
</div>

<!-- End of tab 1 -->

<div class="tab-pane" id="tabs-2">
<p><br></p>
<h2 class="mgr-page-title"><b>Non-Taxable Settings</b></h2>
<p><br></p>

<p class="well">Skip this section if Avalara AvaTax is enabled. These settings are ignored by Avalara AvaTax.</p>
<p><br></p>

<p><b>Enable Non-Taxable Products? &nbsp; <a data-toggle="collapse" href="#enableTaxFreeProds" aria-expanded="false" aria-controls="enableTaxFreeProds"><i class="fa fa-info-circle"></i></a></b><span style="padding-left: 25px;"></span><select name=sc_non_taxables_enabled>
<option>$sc_non_taxables_enabled</option>
<option>yes</option>
<option>no</option>
</select></p>
<div class="collapse" id="enableTaxFreeProds">
    <div class="well">
          <p>This is an all or nothing setting, no variable tax rates are available at this time.</p>
    </div>
</div>
<p><hr></p>

<p><b>Use Which Product Database User Field? &nbsp; <a data-toggle="collapse" href="#taxFreeUserField" aria-expanded="false" aria-controls="taxFreeUserField"><i class="fa fa-info-circle"></i></a></b><span style="padding-left: 25px;"></span><select name=sc_non_taxables_db_counter2>
<option>$sc_non_taxables_db_counter2</option>
<option value="">None</option>
<option>User 2</option>
<option>User 3</option>
<option>User 4</option>
<option>User 5</option>
$user10_thingy
$user20_thingy
</select></p>
<div class="collapse" id="taxFreeUserField">
    <div class="well">
          <p>This feature allows you to set individual products as being tax free by using one of the user fields in the product database as a tax-free indicator.</p>
          <p>Once enabled, you can enter "yes" (without the quotes) in the datafield of your choice within each product that you wish to designate as non-taxable.  Example, if you select user field "user2", then in a product your would type "yes" in the user2 box for the product in the "Add Product" or "Edit Product" manager screens.</p>
    </div>
</div>

<p><br></p>
</div>

<!-- End of tab 2 -->

<div class="tab-pane" id="tabs-3">
<p><br></p>
<h2 class="mgr-page-title"><b>Custom Sales Tax Logic</b></h2>
<p><br></p>

<p class="well">Additional City, State, Provincial or product taxes, can be configured in this custom tax logic tab.<br><br>Skip this section if Avalara AvaTax is enabled. These settings are overridden by enabling Avalara AvaTax.<br><br>This is an advanced feature that usually requires knowledge of basic coding. Please check our user forums and any user guides for more information on how to create the custom logic. You can also hire us or another expert to create the custom logic you require.</p>
<p><br></p>

<p><b>Show how many rows of custom tax logic for each box below?</b><span style="padding-left: 25px;"></span><input name="mc_tax_logic_rows" type="number" value="$mc_tax_logic_rows" size="2"></p>
<p><hr></p>


<p><b>Use Extra Tax 1 Logic? &nbsp; <a data-toggle="collapse" href="#extraTax1" aria-expanded="false" aria-controls="extraTax1"><i class="fa fa-info-circle"></i></a></b><span style="padding-left: 25px;"></span><select name='use_tax1_logic'>
<option>$sc_use_tax1_logic</option>
<option>yes</option>
<option>no</option>
</select><span style="padding-left: 25px;"></span>Display Name: <input name='extra_tax1_name' maxlength=20 size=15
type="text" value='$sc_extra_tax1_name'></p>
<p><b>Extra Tax 1 Logic</b>:<br><textarea class="form-control" name="sc_extra_tax1_logic" rows="$mc_tax_logic_rows" wrap="off">$sc_extra_tax1_logic</textarea></p>
<div class="collapse" id="extraTax1">
     <div class="well">
         <p><b>Warning</b>: This is an advanced feature that requires programming knowledge.</p>
         <p>Additional taxes can be computed on jsut about anything from single or multiple cities, additional states, or even by product IDs or categories. The possibilities are limited only by the information collected on your order forms.</p>
         <p>Display Name: is the text that is shown in the totals box. Example: if you enter display name as "New York City Taxes", then in the sales tax area of the totals, a line for "New York City Taxes" will show up in the cart totals as well as any confirmation emails to the customer.</p>
     </div>
</div>
<p><hr></p>

<p><b>Use Extra Tax 2 Logic? &nbsp; <a data-toggle="collapse" href="#extraTax2" aria-expanded="false" aria-controls="extraTax2"><i class="fa fa-info-circle"></i></a></b><span style="padding-left: 25px;"></span><select name='use_tax2_logic'>
<option>$sc_use_tax2_logic</option>
<option>yes</option>
<option>no</option>
</select><span style="padding-left: 25px;"></span>Display Name: <input name='extra_tax2_name' maxlength=20 size=15
type="text" value='$sc_extra_tax2_name'></p>
<p><b>Extra Tax 2 Logic</b>:<br><textarea class="form-control" name="sc_extra_tax2_logic" rows="$mc_tax_logic_rows" wrap="off">$sc_extra_tax2_logic</textarea></p>
<div class="collapse" id="extraTax2">
    <div class="well">
        <p><b>Warning</b>: This is an advanced feature that requires programming knowledge.</p>
        <p>Additional taxes can be computed on just about anything from single or multiple cities, additional states, or even by product IDs or categories. The possibilities are limited only by the information collected on your order forms.</p>
        <p>Display Name: is the text that is shown in the totals box. Example: if you enter display name as "New York City Taxes", then in the sales tax area of the totals, a line for "New York City Taxes" will show up in the cart totals as well as any confirmation emails to the customer.</p>
    </div>
</div>
<p><hr></p>

<p><b>Use Extra Tax 3 Logic? &nbsp; <a data-toggle="collapse" href="#extraTax3" aria-expanded="false" aria-controls="extraTax3"><i class="fa fa-info-circle"></i></a></b><span style="padding-left: 25px;"></span><select name='use_tax3_logic'>
<option>$sc_use_tax3_logic</option>
<option>yes</option>
<option>no</option>
</select><span style="padding-left: 25px;"></span>Display Name: <input name='extra_tax3_name' maxlength=20 size=15
type="text" value='$sc_extra_tax3_name'></p>
<p><b>Extra Tax 3 Logic</b>:<br><textarea class="form-control" name="sc_extra_tax3_logic" rows="$mc_tax_logic_rows" wrap="off">$sc_extra_tax3_logic</textarea></p>
<div class="collapse" id="extraTax3">
    <div class="well">
        <p><b>Warning</b>: This is an advanced feature that requires programming knowledge.</p>
        <p>Additional taxes can be computed on jsut about anything from single or multiple cities, additional states, or even by product IDs or categories. The possibilities are limited only by the information collected on your order forms.</p>
        <p>Display Name: is the text that is shown in the totals box. Example: if you enter display name as "New York City Taxes", then in the sales tax area of the totals, a line for "New York City Taxes" will show up in the cart totals as well as any confirmation emails to the customer.</p>
    </div>
</div>

<p><br></p>
</div>

<!-- End of tab 3 -->

<div class="tab-pane" id="tabs-4">
<p><br></p>

ENDOFTEXT

# display Avalara content in this tab, if enabled in addon/plugin manager
if ( $is_avalara_enabled eq 'yes' ) {
    &display_avalara_1_settings_tab_content;
}


print <<ENDOFTEXT;
<p><br><br></p>
</div>

<!-- End of tab 4 -->

</div>
<p><br><br></p>
</div>
<input type="hidden" name="system_edit_success" value="yes">
<input name="ChangeTaxSettings" type="hidden" value="Submit">
<button class="btn btn-primary btn-lg btn-block mgr-center">Submit Tax Settings Changes</button>
<div class="row col-sm-12"><p><br><br><br></p></div>

</form>
ENDOFTEXT

    print &$manager_page_footer;
}

#######################################################################

sub action_input_tax_settings {

    local ( $admin_email, $order_email, $cookieDomain, $cookiePath );
    local ($other_program_settings) = q{};
    my ( $myset, $temp_db_thingy ) = q{};
    my $is_avalara_enabled = 'no';

    if ( $mc_mgr_plugins_enabled =~ /avalara/ ) {
        $is_avalara_enabled = 'yes';
    }

    $cookieDomain = $in{'sc_store_url'};
    $cookiePath   = $in{'sc_store_url'};

    $cookieDomain =~ s/http.*:\/\///g;
    $cookieDomain =~ s/\/.*//g;
    $cookieDomain =~ s/\/agora.cgi//g;

    $cookiePath =~ s/http.*:\/\/$cookieDomain//g;
    $cookiePath =~ s/agora.cgi//g;
    chop $cookiePath;

    $order_email = $in{'email_address_for_orders'};
    $order_email =~ s/\@/\\@/;

    $admin_email = $in{'admin_email'};
    $admin_email =~ s/\@/\\@/;

    if ( $in{'sc_non_taxables_db_counter2'} ) {
        $temp_db_thingy = $in{'sc_non_taxables_db_counter2'};
        if ( $temp_db_thingy =~ /User 20/i ) {
            $sc_non_taxables_db_counter = 26;
        }
        elsif ( $temp_db_thingy =~ /User 10/i ) {
            $sc_non_taxables_db_counter = 16;
        }
        elsif ( $temp_db_thingy =~ /User 2/i ) {
            $sc_non_taxables_db_counter = " 8";
        }
        elsif ( $temp_db_thingy =~ /User 3/i ) {
            $sc_non_taxables_db_counter = " 9";
        }
        elsif ( $temp_db_thingy =~ /User 4/i ) {
            $sc_non_taxables_db_counter = 10;
        }
        elsif ( $temp_db_thingy =~ /User 5/i ) {
            $sc_non_taxables_db_counter = 11;
        }
        elsif ( $temp_db_thingy =~ /User 6/i ) {
            $sc_non_taxables_db_counter = 12;
        }
        elsif ( $temp_db_thingy =~ /User 7/i ) {
            $sc_non_taxables_db_counter = 13;
        }
        elsif ( $temp_db_thingy =~ /User 8/i ) {
            $sc_non_taxables_db_counter = 14;
        }
        elsif ( $temp_db_thingy =~ /User 9/i ) {
            $sc_non_taxables_db_counter = 15;
        }
        elsif ( $temp_db_thingy =~ /User 11/i ) {
            $sc_non_taxables_db_counter = 17;
        }
        elsif ( $temp_db_thingy =~ /User 12/i ) {
            $sc_non_taxables_db_counter = 18;
        }
        elsif ( $temp_db_thingy =~ /User 13/i ) {
            $sc_non_taxables_db_counter = 19;
        }
        elsif ( $temp_db_thingy =~ /User 14/i ) {
            $sc_non_taxables_db_counter = 20;
        }
        elsif ( $temp_db_thingy =~ /User 15/i ) {
            $sc_non_taxables_db_counter = 21;
        }
        elsif ( $temp_db_thingy =~ /User 16/i ) {
            $sc_non_taxables_db_counter = 22;
        }
        elsif ( $temp_db_thingy =~ /User 17/i ) {
            $sc_non_taxables_db_counter = 23;
        }
        elsif ( $temp_db_thingy =~ /User 18/i ) {
            $sc_non_taxables_db_counter = 24;
        }
        elsif ( $temp_db_thingy =~ /User 19/i ) {
            $sc_non_taxables_db_counter = 25;
        }
        else {
            $sc_non_taxables_db_counter = q{};
        }
    }

    $myset = q{};
    $myset .= "\$sc_non_taxables_enabled = \"$in{'sc_non_taxables_enabled'}\";\n";
    $myset .= "\$sc_non_taxables_db_counter = \"$sc_non_taxables_db_counter\";\n";
    $myset .= "\$sc_non_taxables_db_counter2 = \"$in{'sc_non_taxables_db_counter2'}\";\n";
    $myset .= "\$mc_tax_logic_rows = \"$in{'mc_tax_logic_rows'}\";\n";
    $myset .= "\$sc_sales_tax_form_variable = \"$in{'sc_sales_tax_form_variable'}\";\n";
    $myset .= "\$sc_sales_tax = \"$in{'sales_tax'}\";\n"; #also handles EU VAT and CAN HST
    $myset .= "\$sc_sales_tax_state = \"$in{'sales_tax_state'}\";\n";
    $myset .= "\$sc_sales_tax_roundat4 = \"$in{'sales_tax_roundat4'}\";\n";
    $myset .= "\$sc_GSTsales_tax = \"$in{'GSTsales_tax'}\";\n";
    $myset .= "\$sc_PSTsales_tax = \"$in{'PSTsales_tax'}\";\n";
    $myset .= "\$sc_use_tax1_logic = \"$in{'use_tax1_logic'}\";\n";
    $myset .= "\$sc_use_tax2_logic = \"$in{'use_tax2_logic'}\";\n";
    $myset .= "\$sc_use_tax3_logic = \"$in{'use_tax3_logic'}\";\n";
    $myset .= "\$sc_extra_tax1_name = \"$in{'extra_tax1_name'}\";\n";
    $myset .= "\$sc_extra_tax2_name = \"$in{'extra_tax2_name'}\";\n";
    $myset .= "\$sc_extra_tax3_name = \"$in{'extra_tax3_name'}\";\n";
    $sc_extra_tax1_logic = my_escape( $in{'sc_extra_tax1_logic'} );
    $myset .= "\$sc_extra_tax1_logic = qq`$sc_extra_tax1_logic`;\n";
    $sc_extra_tax2_logic = my_escape( $in{'sc_extra_tax2_logic'} );
    $myset .= "\$sc_extra_tax2_logic = qq`$sc_extra_tax2_logic`;\n";
    $sc_extra_tax3_logic = my_escape( $in{'sc_extra_tax3_logic'} );
    $myset .= "\$sc_extra_tax3_logic = qq`$sc_extra_tax3_logic`;\n";

    if ( $is_avalara_enabled eq 'yes' ) {
        $myset .= &save_avalara_tax_settings();
    }

    update_store_settings( 'tax', $myset );
    show_tax_settings_screen();
}


#######################################################################

1;
