Merge branch 'usps'

This commit is contained in:
Colin Kuskie 2009-10-11 19:10:01 -07:00
commit d73bc15581
4 changed files with 253 additions and 37 deletions

View file

@ -151,6 +151,7 @@ sub calculate {
}
##Summarize costs from returned data
$cost = $self->_calculateFromXML($xmlData, @shippableUnits);
$cost += $self->_calculateInsurance(@shippableUnits);
return $cost;
}
@ -208,6 +209,74 @@ sub _calculateFromXML {
#-------------------------------------------------------------------
=head2 _calculateInsurance ( @shippableUnits )
Takes data from the USPS and returns the calculated shipping price.
=head3 @shippableUnits
The set of shippable units, which are required to do quantity and cost lookups.
=cut
sub _calculateInsurance {
my ($self, @shippableUnits) = @_;
my $insuranceCost = 0;
return $insuranceCost unless $self->get('addInsurance') && $self->get('insuranceRates');
my @insuranceTable = _parseInsuranceRates($self->get('insuranceRates'));
##Sort by decreasing value for easy post processing
@insuranceTable = sort { $a->[0] <=> $b->[0] } @insuranceTable;
foreach my $package (@shippableUnits) {
my $value = 0;
ITEM: foreach my $item (@{ $package }) {
$value += $item->getSku->getPrice() * $item->get('quantity');
}
my $pricePoint;
POINT: foreach my $point (@insuranceTable) {
if ($value < $point->[0]) {
$pricePoint = $point;
last POINT;
}
}
if (!defined $pricePoint) {
$pricePoint = $insuranceTable[-1];
}
$insuranceCost += $pricePoint->[1];
}
return $insuranceCost;
}
#-------------------------------------------------------------------
=head2 _parseInsuranceRates ( $rates )
Take the user entered data, a string, and turn it into an array.
=head3 $rates
The rate data entered by the user. One set of data per line. Each line has the value of
shipment, a colon, and the cost of insuring a shipment of that value.
=cut
sub _parseInsuranceRates {
my $rates = shift;
$rates =~ tr/\r//d;
my $number = qr/\d+(?:\.\d+)?/;
my $rate = qr{ \s* $number \s* : \s* $number \s* }x;
return () if ($rates !~ m{ \A (?: $rate \r?\n )* $rate (?:\r\n)? \Z }x);
my @lines = split /\n/, $rates;
my @table = ();
foreach my $line (@lines) {
$line =~ s/\s+//g;
my ($value, $cost) = split /:/, $line;
push @table, [ $value, $cost ];
}
return @table;
}
#-------------------------------------------------------------------
=head2 definition ( $session )
This subroutine returns an arrayref of hashrefs, used to validate data put into
@ -262,6 +331,18 @@ sub definition {
options => \%shippingTypes,
defaultValue => 'PARCEL',
},
addInsurance => {
fieldType => 'yesNo',
label => $i18n->get('add insurance'),
hoverHelp => $i18n->get('add insurance help'),
defaultValue => 0,
},
insuranceRates => {
fieldType => 'textarea',
label => $i18n->get('insurance rates'),
hoverHelp => $i18n->get('insurance rates help'),
defaultValue => "50:1.75\n100:2.25",
},
##Note, if a flat fee is added to this driver, then according to the license
##terms the website must display a note to the user (shop customer) that additional
##fees have been added.

View file

@ -94,6 +94,30 @@ our $I18N = {
context => q|Label for a type of shipping from the USPS.|,
},
'add insurance' => {
message => q|Ship with insurance?|,
lastUpdated => 1253988886,
context => q|Label for the edit screen.|,
},
'add insurance help' => {
message => q|If set to yes, the shipping plugin will ask the USPS for the cost of insuring this shipment. The cost will be added to the total cost of shipping. If insurance is not available, then the option to use this driver will not be presented to the user.|,
lastUpdated => 1253988884,
context => q|Label for a type of shipping from the USPS.|,
},
'insurance rates' => {
message => q|Insurance Rate Table|,
lastUpdated => 1253988886,
context => q|Label for the edit screen.|,
},
'insurance rates help' => {
message => q|Enter in one field per line with the format, value:cost.<br />value is the value of the contents.<br />cost is the cost of insurance at that value.<br />value and cost should look like numbers with a decimal point, like 0.50 or 1.00|,
lastUpdated => 1253988884,
context => q|Help for the insurance rate field.|,
},
};
1;