$versions{'cart_formatters.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.
#
#######################################################################

#
#
# Contains the subroutines/functions for formatting pieces of
# data within the cart display and emailsfor things like pricing
# and other items.
#
#

#################################################################
#                     display_price Subroutine
#################################################################
#
# display_price is used to format the price string so that
# the store can take into account differing methods for
# displaying prices. For example, some countries use
# "$xxx.yyy".  Others may use "xx.yy UNIT".  This
# subroutine will  use the $sc_money_symbol_placement and
# the $sc_money_symbol variables defined in
# the store manager areas to format the entire price string for
# display.  The subroutine takes one argument, the price
# to be formatted, and is called with the following
# syntax:
#
# $price = &display_price(xx.yy);
#
# Where xx.yy is some number like 23.99.
#
# Note that the main routine calling this subroutine must
# prepare a variable for the returned formatted price to
# be assigned to.
#
#################################################################

sub display_price {
    local ($price) = @_;
    local ($format_price);

    # set to 2 decimal places
    $price = format_price($price);
    # Add commas US, CAN, AUS currencies fix for others later
    1 while $price =~ s/^(-?\d+)(\d\d\d)/$1,$2/;

    if ( $sc_money_symbol_placement eq "$money_symbol_position_text" ) {
        $format_price = "$sc_money_symbol$sc_money_symbol_spaces$price";
    }
    else {
        $format_price = "$price$sc_money_symbol_spaces$sc_money_symbol";
    }
    return $format_price;
}

sub display_price_nospaces {
    local ($price)  = @_;
    local ($holdme) = $sc_money_symbol_spaces;
    $sc_money_symbol_spaces = '';
    $price    = display_price($price);
    $sc_money_symbol_spaces = $holdme;
    return $price;
}

#######################################################################
#                            format_price
#######################################################################
#
# format_price is used to format prices to two decimal
# places. It takes one argument, the price to be formatted
# and is called with the following syntax:
#
# $price =&format_price(xxx.yyyyy);
#
# Notice that the main calling routine must assign the
# returned formatted price to some variable for its own
# use.
#
# Also notice that this routine takes a value even if it
# is longer than two decimal places and formats it with
# rounding.  Thus, you can utilize price calculations such
# as 12.99 * 7.985 (where 7.985 might be some tax value.
#
#######################################################################

sub format_price {

    # The incoming price is set to a local variables and a few
    # wroking local variables are defined.
    local ($unformatted_price) = @_;
    local ($formatted_price);

    # The script then uses the rounding method in EXCEL. If
    # the 3rd decimal place is > 4, then we round the 2nd
    # decimal place up 1. Otherwise, we leave the number
    # alone.  Notice that we will use the substr function to
    # pull off the last value in the three decimal place
    # number and compare it using the EXCEL logic.
    #
    # Basically, the routine uses the rounding rules of
    # sprintf.

    # The unformatted_price is rounded to
    # to two decimal places and returned to the calling
    # routine.
    $formatted_price = sprintf( "%.2f", $unformatted_price );
    return $formatted_price;

}

#######################################################################
#                       format_text_field
#######################################################################
#
# subroutine: format_text_field
#   Usage:
#       $formatted_value =
#         &format_text_field($value, [$width]);
#
#   Parameters:
#     $value = text value to format.
#     $width = optional field width. Defaults to 25.
#
#     This routine takes the value and appends enough
#     spaces so that the field width is 25 spaces.
#     in order to justify the fields that are stored
#     eventually in the $text_of_cart.
#
#   Output:
#     The formatted value
#
#######################################################################

sub format_text_field {

    local ( $value, $width ) = @_;
    $width = 25 if ( !$width );

    # Very simple. We return the value in
    # $value plus a string of 25 spaces which
    # has been truncated by the length of
    # the $value string.
    #
    # This results in a left justified
    # field of width = 25.
    #
    return ( $value . ( " " x ( $width - length($value) ) ) );

}

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

1;
