$versions{'UPS_legacy_ship-lib.pl'} = '06.5.00.0000';
#######################################################################
#
#
# Contains the subroutines/functions for legacy UPS shipment ratings.
# Only works for US based shipments and the accuracy is not valid, but
# better than nothing.
#
#######################################################################
#
# 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.
#
#######################################################################
#                    calc_ups
#######################################################################

sub calc_ups {

    my ( $method, $origin, $orgincountry, $dest, $destcountry, $shipping_total,
        $weight_data )
      = @_;
    my ( $err_printed, $err, $temp_zip_thingy ) = q{};
    my (
        $product,         $alt_origin, $alt_stateprov,
        $alt_origin_temp, $weight,     $ship_cost,
        $junk,            $thezone,    $errmsg,
        $foundit,         $item_info,  $item_wt,
        $item_qty,        $item_cost,  $box_total_value,
        @ship_list,       $new_weight_data
    );
    $sc_shipping_thing = q{};
    $ship_cost         = 0;    # handling charge added in define_shipping_logic
    my (%zip_list);

    @ship_list = split( /\|/, $weight_data );

    if ( ( $destcountry =~ /united states/i ) || ( $destcountry =~ /pr/ ) ) {
        $destcountry = 'US';
    }
    elsif ( ( $destcountry =~ /cananda/i ) || ( $destcountry =~ /ca/ ) ) {
        $destcountry = 'CA';
    }
    elsif ( ( $destcountry =~ /puerto rico/i ) || ( $destcountry =~ /pr/ ) ) {
        $destcountry = 'PR';
    }

#new stuff for alt origination. use even without alt origination. Added by Mister Ed @ AgoraCart.com
    foreach $ship_list (@ship_list) {
        ( $item_qty, $item_wt, $item_val, $alt_origin, $junk ) =
          split( /\*/, $ship_list, 5 );
        if ( $alt_origin ) {
            if ( $zip_list{$alt_origin} eq '' ) {
                $zip_list{$alt_origin} = $ship_list;
            }
            else {
                $temp_zip_thingy = "$zip_list{$alt_origin}";
                $temp_zip_thingy .= '|' . "$ship_list";
                $zip_list{$alt_origin} = "$temp_zip_thingy";
            }
        }
        else {
            if ( $zip_list{$origin} eq '' ) {
                $zip_list{$origin} = $ship_list;
            }
            else {
                $temp_zip_thingy = "$zip_list{$origin}";
                $temp_zip_thingy .= '|' . "$ship_list";
                $zip_list{$origin} = "$temp_zip_thingy";
            }
        }
    }

    # runs each origination. Added by Mister Ed @ AgoraCart.com
    foreach $origin_product_list ( sort ( keys %zip_list ) ) {

        $weight_data = $zip_list{$origin_product_list};

        # Split the order out into boxes using logic of ship_put_in_boxes

        ( $new_weight_data, $ship_ins ) = ship_put_in_boxes(
            $weight_data,         $weight_data,
            $origin_product_list, $sc_UPS_max_wt
        );

        $sc_shipping_thing .= $new_weight_data;
        $sc_verify_boxes_max_wt = $sc_UPS_max_wt;

        #  Now setup the proper UPS PRODUCT code based on web page selection
        $product = 'GNDRES';    #default
        $foundit = 'no';

        if ( $foundit eq $sc_no ) {    #Look for code: value ="UPS xyz (PRODUCT)"
            ( $junk,  $stuff ) = split( /\(/, $method, 2 );
            ( $stuff, $junk )  = split( /\)/, $stuff,  2 );
            if ( $stuff ) {    #assume we have a valid product code
                $product = $stuff;
                $foundit = 'yes';
            }
        }

        # calculate the shipping for each box  individually.
        @ship_list = split( /\|/, $new_weight_data );
        foreach $item_info (@ship_list) {

            (
                $item_qty,      $item_wt, $box_total_value, $alt_origin,
                $alt_stateprov, $length,  $width,           $height,
                $girth,         $junk
            ) = split( /\*/, $item_info, 10 );
            if ( $item_wt <= 1 ) {    # set min value
                $item_wt = 1;
            }

            # alt origin checking by Mister Ed Feb 9, 2005
            if ( ( $alt_origin ) && ( $sc_alt_origin_enabled =~ /yes/i ) )
            {
                ( $item_cost, $thezone, $errmsg ) =
                  getUPS_simple( $product, $alt_origin, $orgincountry, $dest,
                    $destcountry, $item_wt );
            }
            else {
                ( $item_cost, $thezone, $errmsg ) = getUPS_simple(
                    $product, $origin,      $orgincountry,
                    $dest,    $destcountry, $item_wt
                );
            }

            if ( $errmsg ) {

                #sleep(4);
                ( $item_cost, $thezone, $errmsg ) = getUPS_simple(
                    $product, $origin,      $orgincountry,
                    $dest,    $destcountry, $item_wt
                );
            }

            if ( $err_printed eq '' ) {

                # chect to make sure outside of USA and shipping method matches
                # added by Mister Ed Oct- 24-2003
                if (   $destcountry !~ /US/
                    && $destcountry !~ /united states/i
                    && $product ne 'XPR'
                    && $product ne 'XDM'
                    && $product ne 'XPD'
                    && $product ne 'STD' )
                {
                    $err_printed               = 'yes';
                    $order_error_do_not_finish = 'yes';

                    # for Canadian destinations
                    # added by Mister Ed Oct- 24-2003
                    if ( ( $destcountry =~ /CA/ || $destcountry =~ /canada/i )
                        && $product ne 'STD' )
                    {
                        print "<br/>$agora_shiplib_message09<br/>\n";
                    }

# for all other destination countries outside the USA.  Make sure international shipping methods are chosen.
# added by Mister Ed Oct- 24-2003
                    if (   $destcountry ne 'US'
                        && $destcountry ne 'CA'
                        && $destcountry !~ /canada/i
                        && $destcountry !~ /united states/i
                        && $product ne 'XPR'
                        && $product ne 'XDM'
                        && $product ne 'XPD' )
                    {
                        print "<br/>$agora_shiplib_message10<br/>\n";
                    }

                  # standard error if something goes wrong with UPS calculations
                }
                elsif ( $item_cost == 0 && $err_printed eq '' ) {
                    $err_printed               = 'yes';
                    $order_error_do_not_finish = 'yes';
                    print "$agora_shiplib_message11 ",
                      "<br/>$agora_shiplib_message12<br/><br/>",
                      "$errmsg<br/>\n";
                }
            }

            $ship_cost += ( $item_qty * $item_cost );

        }
    }

    # incase we need to call sumtin before final cost returned
    codehook( 'UPS_cost_return' );

    return $ship_cost;
}

#######################################################################
# Codes for product may be found at:
#  http://theory.uwinnipeg.ca/CPAN/data/Business-UPS/UPS.html
#  Ex: 1DA - Next Day, 2DA 2nd Day, GNDCOM Ground Commercial
#  GNDRES Ground Residential, XPR Worldwide Express

#-- START OF CODE MODIFIED FROM UPS.pm

# new lines to work string added by Mister Ed Jan 2005.  Play with it as needed.
# may or may not work on this older quoting system

sub getUPS_simple {
    my ( $product, $origin, $orgincountry, $dest, $destcountry, $weight ) = @_;
    my ( $length, $width, $height, $oversized, $cod );
    my ($RateChart) = $sc_UPS_RateChart;
    my ( $answer, $workString );
    my $ups_prot   = 'http://';
    my $ups_site   = 'www.ups.com';
    my $ups_cgi    = '/using/services/rave/qcostcgi.cgi';
    my $workString = '?';
    #filter/fix RateChart for changes at UPS Jan 1, 2011 - By Mister Ed with AgoraCart.com
    $RateChart =~ s/Daily Pickup/Regular Daily Pickup/;
    $workString .= 'accept_UPS_license_agreement=yes&';
    $workString .= "10_action=3&";
    $workString .= "13_product=" . $product . "&";
    $workString .= '14_origCountry=' . $orgincountry . "&";
    $workString .= '15_origPostal=' . $origin . "&";
    $workString .= '19_destPostal=' . $dest . "&";

    #    $workString .= "20_destCity=" . $destCity . "&";
    $workString .= '23_weight=' . $weight;

#    $workString .= "&billToUPS=" . $ if $; # default is yes. set to no if not UPS account
    $workString .= '&22_destCountry=' . $destcountry if $destcountry;

#    $workString .= "&customValue=" . $ if $; #customs on international shipments
#    $workString .= "&24_value=" . $ if $; # insurance purposes
    $workString .= '&25_length=' . $length         if $length;
    $workString .= '&26_width=' . $width           if $width;
    $workString .= '&27_height=' . $height         if $height;
    $workString .= '&29_oversized=1'               if $oversized;
    $workString .= '&47_rate_chart=' . $RateChart if $RateChart;
    $workString .= '&30_cod=1'                     if $cod;
    $workString .= '&48_container=00';
    $doworkString = "${ups_prot}${ups_site}${ups_cgi}${workString}";

    #defaults to LWP::Simple as of March 1, 2012 by Mister Ed
    $http_lib_loaded = 'yes';

    my @ret = split( '%', get($doworkString) );

    if ( !$ret[5] ) {
        return ( undef, undef, $ret[1] );
    }
    else {
        my $total_shipping = $ret[10];
        my $ups_zone  = $ret[6];

# adjust for old pricing and add in fuel surcharges for next day and one time pickups
# added by Mister Ed Oct- 24-2003
# edited June 30, 2006 for admin control by Mister Ed
        if ( $total_shipping > 0 && $sc_ups_non_xml_adjust =~ /yes/i ) {
            $total_shipping = ( $total_shipping * $sc_ups_non_xml_adjust_rate );
        }
        return ( $total_shipping, $ups_zone, undef );
    }
}

sub Error {
    my $error = shift;
    print STDERR "$error\n";
    exit(1);
}

#-- End of code from UPS.pm

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

1;
