$versions{'order_email_helpers.pl'} = '06.6.00.0000';

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


########################################################################
#                  replace_email_template_tokens Subroutine
########################################################################
#
# Added by Mister Ed July 2010
# replaces the tokens in the email template set for the gateway being processed.
# most tokens replaced only once within a template (only the first one encountered).
# called in each gateway prior actual logging that is default for each specific gateway.
# any custom tokens can be done at the codehook at the bottom of this subroutine

sub replace_email_template_tokens {
    local ( $zfile ) = @_;
    local ( $the_file ) = q{};
    open( MYFILE, "$sc_emailPlates_dir/$zfile" );

    while (<MYFILE>) {
        if ( ( $_ =~ /\-\-SP\-\-/ ) && ( $_ =~ /\[\[/ ) ) {
           ( $title, $token ) = split( /\-\-SP\-\-/, $_ );
           $title = substr( $title, 0, 16 ) . q{ };
           $the_file .= $title . $token;
       }
       else {
           $the_file .= $_;
       }
    }
    #read( MYFILE, $the_file, 204800 );
    close(MYFILE);

    $the_file =~ s/\[\[year\]\]/$orderLoggingHash{'year'}/g;   #### gateway provided
    $the_file =~ s/\[\[day\]\]/$orderLoggingHash{'day'}/g;   #### gateway provided
    $the_file =~ s/\[\[month\]\]/$orderLoggingHash{'month'}/g;   #### gateway provided
    $the_file =~ s/\[\[orderDate\]\]/$orderLoggingHash{'orderDate'}/g;   #### gateway provided
    $the_file =~ s/\[\[invoiceNumber\]\]/$orderLoggingHash{'cart_invoiceNumber'}/g;
    $the_file =~ s/\[\[cartOrderID\]\]/$orderLoggingHash{'cart_and_order_id'}/g;
    $the_file =~ s/\[\[orderStatus\]\]/$orderLoggingHash{'orderStatus'}/g;
    $the_file =~ s/\[\[shiptrackingID\]\]/$orderLoggingHash{'shiptrackingID'}/g;
    $the_file =~ s/\[\[firstName\]\]/$orderLoggingHash{'firstName'}/g;
    $the_file =~ s/\[\[lastName\]\]/$orderLoggingHash{'lastName'}/g;
    $the_file =~ s/\[\[fullName\]\]/$orderLoggingHash{'fullName'}/g;
    $the_file =~ s/\[\[companyName\]\]/$orderLoggingHash{'companyName'}/g;
    $the_file =~ s/\[\[customerPhone\]\]/$orderLoggingHash{'customerPhone'}/g;
    $the_file =~ s/\[\[faxNumber\]\]/$orderLoggingHash{'faxNumber'}/g;
    $the_file =~ s/\[\[emailAddress\]\]/$orderLoggingHash{'emailAddress'}/g;
    $the_file =~ s/\[\[orderFromAddress\]\]/$orderLoggingHash{'orderFromAddress'}/g;
    $the_file =~ s/\[\[customerAddress2\]\]/$orderLoggingHash{'customerAddress2'}/g;
    $the_file =~ s/\[\[customerAddress3\]\]/$orderLoggingHash{'customerAddress3'}/g;
    $the_file =~ s/\[\[orderFromCity\]\]/$orderLoggingHash{'orderFromCity'}/g;
    $the_file =~ s/\[\[orderFromState\]\]/$orderLoggingHash{'orderFromState'}/g;
    $the_file =~ s/\[\[orderFromPostal\]\]/$orderLoggingHash{'orderFromPostal'}/g;
    $the_file =~ s/\[\[orderFromCountry\]\]/$orderLoggingHash{'orderFromCountry'}/g;
    $the_file =~ s/\[\[shipToName\]\]/$orderLoggingHash{'shipToName'}/g;
    $the_file =~ s/\[\[shipToCompany\]\]/$orderLoggingHash{'shipToCompany'}/g;
    $the_file =~ s/\[\[shipToAddress\]\]/$orderLoggingHash{'shipToAddress'}/g;
    $the_file =~ s/\[\[shipToAddress2\]\]/$orderLoggingHash{'shipToAddress2'}/g;
    $the_file =~ s/\[\[shipToAddress3\]\]/$orderLoggingHash{'shipToAddress3'}/g;
    $the_file =~ s/\[\[shipToCity\]\]/$orderLoggingHash{'shipToCity'}/g;
    $the_file =~ s/\[\[shipToState\]\]/$orderLoggingHash{'shipToState'}/g;
    $the_file =~ s/\[\[shipToPostal\]\]/$orderLoggingHash{'shipToPostal'}/g;
    $the_file =~ s/\[\[shipToCountry\]\]/$orderLoggingHash{'shipToCountry'}/g;
    $the_file =~ s/\[\[shipToResidential\]\]/$orderLoggingHash{'shiptoResidential'}/g;
    $the_file =~ s/\[\[shipToInsured\]\]/$orderLoggingHash{'insureShipment'}/g;
    $the_file =~ s/\[\[shipMethod\]\]/$orderLoggingHash{'shipMethod'}/g;   #### gateway provided
    $the_file =~ s/\[\[shippingTotal\]\]/$orderLoggingHash{'shippingTotal'}/g;   #### gateway provided
    $the_file =~ s/\[\[salesTax\]\]/$orderLoggingHash{'salesTax'}/g;   #### gateway provided
    $the_file =~ s/\[\[tax1\]\]/$orderLoggingHash{'tax1'}/g;   #### gateway provided
    $the_file =~ s/\[\[tax2\]\]/$orderLoggingHash{'tax2'}/g;   #### gateway provided
    $the_file =~ s/\[\[tax3\]\]/$orderLoggingHash{'tax3'}/g;   #### gateway provided
    $the_file =~ s/\[\[discounts\]\]/$orderLoggingHash{'discounts'}/g;   #### gateway provided
    $the_file =~ s/\[\[subTotal\]\]/$orderLoggingHash{'subTotal'}/g;   #### gateway provided
    $the_file =~ s/\[\[orderTotal\]\]/$orderLoggingHash{'orderTotal'}/g;   #### gateway provided
    $the_file =~ s/\[\[user1\]\]/$orderLoggingHash{'user1'}/g;
    $the_file =~ s/\[\[user2\]\]/$orderLoggingHash{'user2'}/g;
    $the_file =~ s/\[\[user3\]\]/$orderLoggingHash{'user3'}/g;
    $the_file =~ s/\[\[user4\]\]/$orderLoggingHash{'user4'}/g;
    $the_file =~ s/\[\[user5\]\]/$orderLoggingHash{'user5'}/g;
    $the_file =~ s/\[\[buysafecost\]\]/$orderLoggingHash{'buySafe'}/g;   #### gateway provided
    $the_file =~ s/\[\[adminMessages\]\]/$orderLoggingHash{'adminMessages'}/g;   #### gateway provided
    $the_file =~ s/\[\[GatewayUsed\]\]/$orderLoggingHash{'GatewayUsed'}/g;   #### gateway provided
    $the_file =~ s/\[\[shippingMessages\]\]/$orderLoggingHash{'shippingMessages'}/g;   #### gateway provided
    $the_file =~ s/\[\[money_symbol\]\]/$sc_money_symbol/g;
    $the_file =~ s/\[\[xcomments\]\]/$orderLoggingHash{'xcomments'}/g;   #### gateway provided
    $the_file =~ s/\[\[termsOfService\]\]/$orderLoggingHash{'termsOfService'}/g;
    $the_file =~ s/\[\[discountCode\]\]/$orderLoggingHash{'discountCode'}/g;
    $the_file =~ s/\[\[user6\]\]/$orderLoggingHash{'user6'}/g;
    $the_file =~ s/\[\[user7\]\]/$orderLoggingHash{'user7'}/g;
    $the_file =~ s/\[\[user8\]\]/$orderLoggingHash{'user8'}/g;
    $the_file =~ s/\[\[user9\]\]/$orderLoggingHash{'user9'}/g;
    $the_file =~ s/\[\[user10\]\]/$orderLoggingHash{'user10'}/g;
    $the_file =~ s/\[\[order_payment_type_user1\]\]/$orderLoggingHash{'order_payment_type_user1'}/g;
    $the_file =~ s/\[\[order_payment_type_user2\]\]/$orderLoggingHash{'order_payment_type_user2'}/g;
    $the_file =~ s/\[\[GiftCard_number\]\]/$orderLoggingHash{'GiftCard_number'}/g;
    $the_file =~ s/\[\[GiftCard_amount_used\]\]/$orderLoggingHash{'GiftCard_amount_used'}/g;
    # $the_file =~ s/\[\[internal_company_notes1\]\]/$orderLoggingHash{'internal_company_notes1'}/g;
    $the_file =~ s/\[\[customer_order_notes1\]\]/$orderLoggingHash{'customer_order_notes1'}/g;
    $the_file =~ s/\[\[customer_order_notes2\]\]/$orderLoggingHash{'customer_order_notes2'}/g;
    $the_file =~ s/\[\[customer_order_notes3\]\]/$orderLoggingHash{'customer_order_notes3'}/g;
    $the_file =~ s/\[\[customer_order_notesDLtext\]\]/$orderLoggingHash{'customer_order_notesDLtext'}/g;
    $the_file =~ s/\[\[PP_pending_reason\]\]/$form_data{'pending_reason'}/g;
    $the_file =~ s/\[\[PP_reason_code\]\]/$form_data{'reason_code'}/g;
    $the_file =~ s/\[\[PP_payment_status\]\]/$form_data{'payment_status'}/g;

    ####### will need toggles for remailing after payment confirmed  ######
    if ( ( $orderLoggingHash{'GatewayUsed'} ne 'Offline' ) && ( $orderLoggingHash{'GatewayUsed'} ne 'PayPal' ) && ( $zfile =~ /template_email_html/ ) ) {
        $the_file =~ s/\[\[customer_order_notesDLhtml\]\]/$orderLoggingHash{'customer_order_notesDLhtml'}/g;
        $the_file =~ s/\[\[customer_order_notesDLtext\]\]//g;
    }
    elsif ( ( $orderLoggingHash{'GatewayUsed'} ne 'Offline' ) && ( $orderLoggingHash{'GatewayUsed'} ne 'PayPal' ) && ( $zfile =~ /template_email_text/ ) ) {
        $the_file =~ s/\[\[customer_order_notesDLtext\]\]/$orderLoggingHash{'customer_order_notesDLtext'}/g;
        $the_file =~ s/\[\[customer_order_notesDLhtml\]\]//g;
    }
    elsif ( $zfile =~ /template_admin_email/ ) {
        $the_file =~ s/\[\[customer_order_notesDLtext\]\]/$orderLoggingHash{'customer_order_notesDLtext'}/g;
        $the_file =~ s/\[\[customer_order_notesDLhtml\]\]/$messages{'admin_error_DL_html_tokens_used'} /g;
    }
    else {
        $the_file =~ s/\[\[customer_order_notesDLtext\]\]//g;
        $the_file =~ s/\[\[customer_order_notesDLhtml\]\]//g;
    }
    #######
    $the_file =~ s/\[\[mailinglist_subscribe\]\]/$orderLoggingHash{'mailinglist_subscribe'}/g;
    $the_file =~ s/\[\[wishlist_subscribe\]\]/$orderLoggingHash{'wishlist_subscribe'}/g;
    $the_file =~ s/\[\[insurance_cost\]\]/$orderLoggingHash{'insurance_cost'}/g;
    $the_file =~ s/\[\[trade_in_allowance\]\]/$orderLoggingHash{'trade_in_allowance'}/g;
    $the_file =~ s/\[\[rma_number\]\]/$orderLoggingHash{'rma_number'}/g;
    $the_file =~ s/\[\[customer_contact_notes1\]\]/$orderLoggingHash{'customer_contact_notes1'}/g;
    $the_file =~ s/\[\[customer_contact_notes2\]\]/$orderLoggingHash{'customer_contact_notes2'}/g;
    $the_file =~ s/\[\[account_number\]\]/$orderLoggingHash{'account_number'}/g;
    $the_file =~ s/\[\[sales_rep\]\]/$orderLoggingHash{'sales_rep'}/g;
    # $the_file =~ s/\[\[sales_rep_notes1\]\]/$orderLoggingHash{'sales_rep_notes1'}/g;
    $the_file =~ s/\[\[how_did_you_find_us\]\]/$orderLoggingHash{'how_did_you_find_us'}/g;
    $the_file =~ s/\[\[suggestion_box\]\]/$orderLoggingHash{'suggestion_box'}/g;
    $the_file =~ s/\[\[preferrred_shipping_date\]\]/$orderLoggingHash{'preferrred_shipping_date'}/g;
    $the_file =~ s/\[\[ship_order_items_as_available\]\]/$orderLoggingHash{'ship_order_items_as_available'}/g;
    $the_file =~ s/\[\netProfit\]\]/$orderLoggingHash{'netProfit'}/g;
    $the_file =~ s/\[\[affiliateID\]\]/$orderLoggingHash{'affiliateID'}/g;
    $the_file =~ s/\[\[affiliateMisc\]\]/$orderLoggingHash{'affiliateMisc'}/g;
    $the_file =~ s/\[\[member\]\]/$orderLoggingHash{'member'}/g;    #added July 2010
    $the_file =~ s/\[\[pricingLevel\]\]/$orderLoggingHash{'pricingLevel'}/g;    #added July 2010
     # at time of order only - for email templates
     $the_file =~ s/\[\[cart_contents_text\]\]/$text_of_cart/g;
     $the_file =~ s/\[\[cart_contents_html\]\]/$html_of_cart/g;
     $the_file =~ s/\[\[cart_contents_html_header\]\]/$html_of_cart_header/g;
     $the_file =~ s/\[\[xcomments_html\]\]/$xcomments_html/g;
     $the_file =~ s/\[\[nl\]\]/\n/g;

    #change things here for custom tokens in email templates
    # NEW CODE HOOK
    codehook( 'replace_email_template_tokens_bottom' );

    return( $the_file );
}

########################################################################
#                  cart_textinfo Subroutine
########################################################################

sub cart_textinfo {
    local (*cart_fields) = @_;
    local ( @my_row );

    # set some local variables in case virtual fields want/need them
    local $cart_line_id  = $cart_fields[ $cart{'unique_cart_line_id'} ];
    local $quantity      = $cart_fields[ $cart{'quantity'} ];
    local $p_id          = $cart_fields[ $cart{'product_id'} ];
    local $product       = $cart_fields[ $cart{'product'} ];
    local $product_name  = $cart_fields[ $cart{'name'} ];
    local $options       = $cart_fields[ $cart{'options'} ];
    local $product_price = $cart_fields[ $cart{'price'} ];

    if ( !( $sc_db_lib_was_loaded =~ /yes/i ) )  {
        require_supporting_libraries( __FILE__, __LINE__, "$sc_db_lib_path" );
    }

    #read in the database row
    local $query_result = check_db_with_product_id( $p_id, *my_row );
    local $result       = q{};
    local $inx          = 0;

    # mandatory fields
    $result =
        "$agora_cart_text_info01$quantity\n$agora_cart_text_info02"
      . $product_name . ' ('
      . display_price($product_price)
      . "$agora_cart_text_info03)\n";

    foreach $field_name (@sc_textcol_name) {
        my $display_index = $cart{$field_name};
        my $title = $sc_textcart_display_fields[$inx] . $agora_cart_text_info04;
        my $mydata = q{};
        $title = substr( $title, 0, 14 ) . q{ };
        if ( $display_index < 0 ) {    #virtual field ... take from db
            $mydata = $my_row[ ( 0 - $display_index ) ];
        }
        else {       #normal, in the cart
            $mydata =
              vf_get_data( 'CART', $field_name, $cart_line_id, @cart_fields );
        }
        if ( $sc_textcart_display_factor[$inx] =~ /yes/i ) {   # mult by qty!
            $mydata = $mydata * $cart_fields[ $cart{'quantity'} ];
        }
        if ( $sc_textcart_display_format[$inx] =~ /2-Decimal/i )   {   # 2 decimal places
            $mydata = format_price($mydata);
        }
        if ( $sc_textcart_display_format[$inx] =~ /2-D Price/i ) {   # is a price
            $mydata = display_price($mydata);
        }
        $result .= $title . $mydata . "\n";
        $inx++;
    }

    $result .= "\n";
    return $result;

}

########################################################################
#                  cart_html_textinfo Subroutine
########################################################################

sub cart_html_textinfo {
    local (*cart_fields) = @_;
    local ( @my_row );

    # set some local variables in case virtual fields want/need them
    local $cart_line_id  = $cart_fields[ $cart{'unique_cart_line_id'} ];
    local $quantity      = $cart_fields[ $cart{'quantity'} ];
    local $p_id          = $cart_fields[ $cart{'product_id'} ];
    local $product       = $cart_fields[ $cart{'product'} ];
    local $product_name  = $cart_fields[ $cart{'name'} ];
    local $options       = $cart_fields[ $cart{'options'} ];
    local $product_price = $cart_fields[ $cart{'price'} ];

    if ( !( $sc_db_lib_was_loaded =~ /yes/i ) )   {
        require_supporting_libraries( __FILE__, __LINE__, "$sc_db_lib_path" );
    }

    #read in the database row
    local $query_result = check_db_with_product_id( $p_id, *my_row );
    local $result       = q{};
    local $inx          = 0;

    # mandatory fields
    $result =
        "$cart_html_textinfo_item_row_start $cart_html_textinfo_item_cell_start $quantity $cart_html_textinfo_item_cell_end $cart_html_textinfo_item_cell_start $product_name ("
       . display_price($product_price)
       . "$agora_cart_text_info03) $cart_html_textinfo_item_cell_end";

    foreach my $field_name (@sc_textcol_name) {
        my $display_index = $cart{$field_name};
        if ( $display_index < 0 ) {    #virtual field ... take from db
            $mydata = $my_row[ ( 0 - $display_index ) ];
        }
        else {                         #normal, in the cart
            $mydata =  vf_get_data( 'CART', $field_name, $cart_line_id, @cart_fields );
        }
        $mydata =~ s/\n/<br>/g;
        if ( $sc_textcart_display_factor[$inx] =~ /yes/i ) {    # mult by qty!
            $mydata = $mydata * $cart_fields[ $cart{'quantity'} ];
        }
        if ( $sc_textcart_display_format[$inx] =~ /2-Decimal/i )  {   # 2 decimal places
            $mydata = format_price($mydata);
        }
        if ( $sc_textcart_display_format[$inx] =~ /2-D Price/i ) {  # is a price
            $mydata = display_price($mydata);
        }
        $result .= "$cart_html_textinfo_item_cell_start $mydata $cart_html_textinfo_item_cell_end";
        $inx++;
    }

    $result .= $cart_html_textinfo_item_row_end;
    return $result;

}

########################################################################
#                  cart_html_headerinfo Subroutine
########################################################################

sub cart_html_headerinfo {
    my $result       = q{};

    foreach my $field_name (@sc_textcart_display_fields) {
        $result .= "$cart_html_textinfo_header_cell_start $field_name $cart_html_textinfo_item_cell_end";
    }

    return $result;

}

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

1;
