4 use BSE::CfgInfo qw(load_class);
6 our $VERSION = "1.003";
9 my ($class, $cfg, $wanted) = @_;
11 my @enabled = split /\s+/, $cfg->entry("shipping", "couriers");
12 push @enabled, "Null" if $cfg->entry("shipping", "quoted", 1);
15 foreach my $name (@enabled) {
16 $name = "Courier::$name";
20 load_class($name, $cfg);
21 $courier = $name->new(config => $cfg);
24 warn "Unable to load $courier: $@\n";
27 next if defined $wanted and $wanted ne $courier->name();
28 push @couriers, $courier;
33 # returns one or more parcels to be delivered by a courier
34 # currently always returns a single parcel
36 my ($class, $cfg, $order, $items) = @_;
38 require BSE::TB::Products;
43 foreach my $item (@$items) {
44 my $product = BSE::TB::Products->getByPkey($item->{productId});
45 my $number = $item->{units};
47 my $weight = $product->{weight};
51 $total_weight += $weight*$number;
53 # Calculate dimensions for the given number of items. We keep
54 # filling a stack of n*n squares with products, and measure the
58 @{$product}{qw(length width height)};
59 my ($length, $width, $height) = ($L, $W, $H);
65 if ($n == 0) { $length += $L; }
66 elsif ($n == 1) { $width += $W; }
67 elsif ($n == 2) { $height += $H; }
71 # Store the longest length and width of any group of items in
72 # the order, but keep adding up the heights. Represents
73 # something like a worst-case packing.
75 if ($length != 0 && $length > $total_length) {
76 $total_length = $length;
79 if ($width != 0 && $width > $total_width) {
80 $total_width = $width;
83 $total_height += $height;
87 or confess "package_order() may return multiple packages in the future";
89 return BSE::Shipping::Parcel->new
91 length => $total_length,
92 width => $total_width,
93 height => $total_height,
94 weight => $total_weight
98 package BSE::Shipping::Parcel;
100 use Carp qw(confess);
102 # simple wrapper around length/width/height/weight
105 my ($class, %opts) = @_;
107 defined $opts{length}
108 or confess "Missing length option";
110 or confess "Missing width option";
111 defined $opts{height}
112 or confess "Missing height option";
113 defined $opts{weight}
114 or confess "Missing weight option";
116 return bless \%opts, $class;
119 # calcuate "cubic weight" as per Australia Post
120 # as volume in cubic metres * 250
121 # length/width/height in millimetres
125 $self->length / 1000 * $self->width / 1000 * $self->height / 1000 * 250_000;