Basket-Calc

 view release on metacpan or  search on metacpan

lib/Basket/Calc.pm  view on Meta::CPAN

        return;
    }

    foreach my $key ('type', 'value') {
        unless (exists $discount->{$key} and $discount->{$key}) {
            carp "'$key' missing";
            return;
        }
    }

    unless ($discount->{type} =~ m/^(percent|amount)$/x) {
        carp "'type' has to be either percent, or amount";
        return;
    }

    unless (looks_like_number($discount->{value})) {
        carp "'value' is not a number";
        return;
    }

    given ($discount->{type}) {
        when ('percent') {
            if ($discount->{value} <= 0 or $discount->{value} > 1) {
                carp "'percent' has to be a decimal between 0 and 1";
                return;
            }
        }
        when ('amount') {
            $discount->{currency} = $self->currency
                unless exists $discount->{currency};

            # convert currency if needed
            if ($discount->{currency} ne $self->currency) {
                my $amount = Finance::Currency::Convert::Yahoo::convert(

lib/Basket/Calc.pm  view on Meta::CPAN

    }

    my $original_net = $total->{net};

    # apply discounts
    if ($self->discount) {
        print __PACKAGE__ . ' discount: ' . join(' ', %{ $self->discount }) . $/
            if $self->debug;

        given ($self->discount->{type}) {
            when ('percent') {
                $total->{net} *= (1 - $self->discount->{value});
            }
            when ('amount') {
                $total->{net} = $total->{net} - $self->discount->{value};
                $total->{net} = 0 if $total->{net} < 0;
            }
        }
    }

    # calculate tax

lib/Basket/Calc.pm  view on Meta::CPAN

Perhaps a little code snippet.

    use Basket::Calc;
    use Data::Dump 'dump';

    my $basket = Basket::Calc->new(debug => 1, currency => 'NZD', tax => .15);
    $basket->add_item({ price => 14.90, currency => 'USD', quantity => 2 });
    $basket->add_item({ price => 59, currency => 'EUR'});
    $basket->add_item({ price => 119.15, currency => 'JPY' });
    
    $basket->add_discount({ type => 'percent', value => .2 });
    # or
    $basket->add_discount({ type => 'amount', value => 15, currency => 'HKD' });
    
    print dump $basket->calculate;

=head1 ATTRIBUTES

=head2 debug

=head2 currency

t/01-example.t  view on Meta::CPAN

        currency   => 'NZD',
        value      => 51.41,
        net        => 44.70,
        tax_amount => 6.71,
        discount   => 0,
    },
    'calculate totals',
);

# add 20% discount
$result = $basket->add_discount({ type => 'percent', value => .2 });
is_deeply(
    $result, {
        type  => 'percent',
        value => .2,
    },
    'add percent discount',
);

# calculate totals (percent discount)
$basket->add_item({ price => 14.90 });
$basket->add_item({ price => 14.90, quantity => 2 });
$result = $basket->calculate;
is_deeply(
    $result, {
        currency   => 'NZD',
        value      => 41.12,
        net        => 35.76,
        tax_amount => 5.36,
        discount   => 8.94,
    },
    'calculate totals (percent discount)',
);

# add fixed currency amount discount
$result = $basket->add_discount({ type => 'amount', value => 15 });
is_deeply(
    $result, {
        type     => 'amount',
        value    => 15,
        currency => 'NZD',
    },



( run in 0.352 second using v1.01-cache-2.11-cpan-709fd43a63f )