From 5c84eb930686242ef29315b6d762b28a22fea659 Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Wed, 7 Oct 2009 22:16:30 -0700 Subject: [PATCH] Finish building insurance calculation method. Needs tests. --- lib/WebGUI/Shop/ShipDriver/USPS.pm | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/lib/WebGUI/Shop/ShipDriver/USPS.pm b/lib/WebGUI/Shop/ShipDriver/USPS.pm index 2ed5fd888..38181777c 100644 --- a/lib/WebGUI/Shop/ShipDriver/USPS.pm +++ b/lib/WebGUI/Shop/ShipDriver/USPS.pm @@ -61,19 +61,15 @@ sub buildXML { next PACKAGE unless scalar @{ $package }; tie my %packageData, 'Tie::IxHash'; my $weight = 0; - my $cost = 0; foreach my $item (@{ $package }) { my $sku = $item->getSku; my $itemWeight = $sku->getWeight(); - my $itemCost = $sku->getPrice(); ##Items that ship separately with a quantity > 1 are rate estimated as 1 item and then the ##shipping cost is multiplied by the quantity. if (! $sku->shipsSeparately ) { $itemWeight *= $item->get('quantity'); - $itemCost *= $item->get('quantity'); } $weight += $itemWeight; - $cost += $itemCost; } my $pounds = int($weight); my $ounces = int(16 * ($weight - $pounds)); @@ -226,12 +222,23 @@ The set of shippable units, which are required to do quantity and cost lookups. sub _calculateInsurance { my ($self, @shippableUnits) = @_; my $cost = 0; - return $cost unless $self-get('addInsurance') && $self->get('insuranceRates'); - my @insuranceTable = map { my ($cost,$value) = split /:/, $_; [$cost, $value]; } + return $cost unless $self->get('addInsurance') && $self->get('insuranceRates'); + my @insuranceTable = map { my ($value,$cost) = split /:/, $_; [$value, $cost]; } split /\r?\n/, $self->get('insuranceRates'); - ##Sort by increasing value for easy post processing - my @insuranceTable = map { $_->[1] } sort { $a <=> $b } map { [ $_->[0], $_ ] } @insuranceTable; - return $cost; + ##Sort by decreasing value for easy post processing + @insuranceTable = sort { $b->[0] <=> $a->[0] } @insuranceTable; + my $insuranceCost = 0; + foreach my $package (@shippableUnits) { + ITEM: foreach my $item (@{ $package }) { + $cost += $item->getSku->getPrice() * $item->get('quantity'); + } + my $pricePoint; + POINT: foreach $pricePoint (@insuranceTable) { + last POINT if $cost <= $pricePoint->[0]; + } + $insuranceCost += $pricePoint->[1]; + } + return $insuranceCost; } #-------------------------------------------------------------------