| 1 | # ------------------------------------------------------------------------------------- | ||
| 2 | # MKDoc::ECommerce::Rules | ||
| 3 | # ------------------------------------------------------------------------------------- | ||
| 4 | # Author : Jean-Michel Hiver <jhiver@mkdoc.com>. | ||
| 5 | # Copyright : (c) MKDoc Holdings Ltd, 2003 | ||
| 6 | # | ||
| 7 | # Billing Rules can become very, very complicated and ugly. This module attempts to | ||
| 8 | # sort of sort out this ugly mess. | ||
| 9 | # ------------------------------------------------------------------------------------- | ||
| 10 | package MKDoc::ECommerce::Rules; | ||
| 11 | use MKDoc::Control_List; | ||
| 12 | use strict; | ||
| 13 | use warnings; | ||
| 14 | |||
| 15 | our @LIST = (); | ||
| 16 | our $Self = undef; | ||
| 17 | |||
| 18 | |||
| 19 | sub no_warnings { $::Rules } | ||
| 20 | |||
| 21 | |||
| 22 | sub new | ||
| 23 | { | ||
| 24 | my $class = shift; | ||
| 25 | my $self = bless { @_ }, $class; | ||
| 26 | } | ||
| 27 | |||
| 28 | |||
| 29 | # returns a list of hashes comprising three fields: | ||
| 30 | # { title => $title | ||
| 31 | # desc => $description | ||
| 32 | # amount => $new_amount } | ||
| 33 | sub explain | ||
| 34 | { | ||
| 35 | my $self = shift; | ||
| 36 | my $billing = $ENV{ECOMMERCE_BILLING_RULES} || $ENV{MKDOC_DIR} . '/conf/ecommerce.list.conf'; | ||
| 37 | |||
| 38 | # no rules specified? return the total price... | ||
| 39 | my @res = { amount => $self->{basket}->total() }; | ||
| 40 | $billing && -e $billing || return wantarray ? @res : \@res; | ||
| 41 | |||
| 42 | my $control = new MKDoc::Control_List ( file => $billing ); | ||
| 43 | $::Rules = $self; | ||
| 44 | |||
| 45 | @res = $control->process(); | ||
| 46 | |||
| 47 | my $prev = $self->{basket}->total(); | ||
| 48 | for (@res) | ||
| 49 | { | ||
| 50 | $_->{title} = $_->{title}->($prev) if ( ref $_->{title} ); | ||
| 51 | $_->{desc} = $_->{desc}->($prev) if ( ref $_->{desc} ); | ||
| 52 | $_->{amount} = $_->{amount}->($prev) if ( ref $_->{amount} ); | ||
| 53 | $prev = $_->{amount}; | ||
| 54 | } | ||
| 55 | |||
| 56 | return wantarray ? @res : \@res; | ||
| 57 | } | ||
| 58 | |||
| 59 | |||
| 60 | sub deal_price | ||
| 61 | { | ||
| 62 | my $self = shift; | ||
| 63 | my @results = reverse $self->explain(); | ||
| 64 | return $results[0]->{amount}; | ||
| 65 | } | ||
| 66 | |||
| 67 | |||
| 68 | 1; | ||
| 69 | |||
| 70 | |||
| 71 | __END__ |