NL.pm

filter: Statementstmt 100% 67 / 67 Branchbran 86.0% 43 / 50 Conditioncond 66.6% 8 / 12 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 100% 12 / 12 Podpod 100% 3 / 3 totaltotal 90.1% 137 / 152 CC 32 SCAR 24.1
CC
32
Cov
91%
calculate
16.1
_generate_path
14.3
country
3.0
CRAP 11.1
↑ Summary NL/Data.pm »
1 package Business::TNTPost::NL;
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use Business::TNTPost::NL::Data qw/:ALL/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use List::Util qw/reduce/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 9 our $VERSION = 0.11;
9 9 our $ERROR = undef;
10  
11 9 ▶use base qw/Class::Accessor::Fast/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 9 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 __PACKAGE__->mk_accessors(
15 qw/cost large machine priority receipt register tracktrace weight zone/
16 );
17 }
18  
19 =pod
20  
21 =head1 NAME
22  
23 Business::TNTPost::NL - Calculate Dutch (TNT Post) shipping costs
24  
25 =head1 SYNOPSIS
26  
27 use Business::TNTPost::NL;
28  
29 my $tnt = Business::TNTPost::NL->new();
30 $tnt->country('DE');
31 $tnt->weight('534');
32 $tnt->large(1);
33 $tnt->priority(1);
34 $tnt->tracktrace(1);
35 $tnt->register(1);
36  
37 my $costs = $tnt->calculate or die $Business::TNTPost::NL::ERROR;
38  
39  
40 or
41  
42 use Business::TNTPost::NL;
43  
44 my $tnt = Business::TNTPost::NL->new();
45 my $costs = $tnt->calculate(
46 country =>'DE',
47 weight => 534,
48 large => 1,
49 tracktrace => 1,
50 register => 1,
51 ) or die $Business::TNTPost::NL::ERROR;
52  
53 =head1 DESCRIPTION
54  
55 This module calculates the shipping costs for the Dutch TNT Post,
56 based on country, weight and priority shipping (or not), etc.
57  
58 The shipping cost information is based on 'Tarieven Januari 2011'.
59  
60 It returns the shipping costs in euro or undef (which usually means
61 the parcel is heavier than the maximum allowed weight; check
62 C<$Business::TNTPost::NL::ERROR>).
63  
64 =head2 METHODS
65  
66 The following methods can be used
67  
68 =head3 new
69  
70 C<new> creates a new C<Business::TNTPost::NL> object. No more, no less.
71  
72 =cut
73  
74 19 ▶sub new {
Subroutine new: called CC=2 SCAR=7.6
Pod documented
75 19 my ( $class, %parameters ) = @_;
76 19 ▶ my $self = bless( {}, ref($class) || $class );
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0190
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
77 19 return $self;
78 }
79  
80 =pod
81  
82 =head3 country
83  
84 Sets the country (ISO 3166, 2-letter country code) and returns the
85 zone number used by TNT Post (or 0 for The Netherlands (NL)).
86  
87 This value is mandatory for the calculations.
88  
89 Note that the reserved IC has been used for the Canary Islands. This
90 has not been adopted by ISO 3166 yet. The Channel Islands are completely
91 ignored due to a lack of a code.
92  
93 =cut
94  
95 19 ▶sub country {
Subroutine country: called CC=3 SCAR=11.0
Pod documented
96 19 my ( $self, $cc ) = @_;
97  
98 19 ▶ if ($cc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cc) 19 0 19
99 19 my $zones = Business::TNTPost::NL::Data::zones();
100 19 ▶ $self->zone( defined $zones->{$cc} ? $zones->{$cc} : '4' );
Branch: 1 branch
BranchTrueFalseTotal
defined $zones->{$cc} ? : 15 4 19
101 }
102  
103 19 return $self->zone;
104 }
105  
106 =pod
107  
108 =head3 calculate
109  
110 Method to calculate the actual shipping cost based on the input (see
111 methods above). These options can also be passed straight in to this method
112 (see L<SYNOPSIS>).
113  
114 Two settings are mandatory: country and weight. The rest are given a
115 default value that will be used unless told otherwise.
116  
117 Returns the shipping costs in euro, or undef (see $Business::TNTPost::NL::ERROR
118 in that case).
119  
120 =cut
121  
122 19 ▶sub calculate {
Subroutine calculate: called CC=16 SCAR=27.8
Pod documented
123 19 my ( $self, %opt ) = @_;
124  
125 # Set the options
126 19 for (
127 qw/country weight large priority tracktrace
128 register receipt machine/
129 )
130 {
131 152 ▶ $self->$_( $opt{$_} ) if ( defined $opt{$_} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{$_} 70 82 152
132 }
133  
134 19 ▶ croak "Not enough information!"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->zone and defined $self->weight 0 19 19
Condition33%1/3
defined $self->zone and defined $self->weight
!ll&&!rl&&r
0019
MC/DC0%0/2
defined $self->zone and defined $self->weight
defined $self->zone defined $self->weight
Truth table33%1/3
A and B
A= defined $self->zoneB= defined $self->weight
ABresult
0X0
100
111
135 unless ( defined $self->zone && defined $self->weight );
136  
137 # > 2000 grams automatically means 'tracktrace'
138 19 ▶ $self->tracktrace(1) if ( $self->weight > 2000 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->weight > 2000 3 16 19
139  
140 # Zone 1..4 (with tracktrace) automagically means 'priority'
141 19 ▶ $self->priority(1) if ( $self->tracktrace );
Branch: 1 branch
BranchTrueFalseTotal
if $self->tracktrace 3 16 19
142  
143 # Zone 3,4 + small automagically means 'priority'
144 19 ▶ $self->priority(1) if( $self->zone > 3 && !$self->large );
Branch: 1 branch
BranchTrueFalseTotal
if $self->zone > 3 and not $self->large 3 16 19
Condition100%3/3
$self->zone > 3 and not $self->large
!ll&&!rl&&r
1513
MC/DC100%2/2
$self->zone > 3 and not $self->large
$self->zone > 3 not $self->large
Truth table100%3/3
A and not(B)
A= $self->zone > 3B= not $self->large
ABresult
0X0
100
111
145  
146 # All zones (above NL) are now priority by default
147 19 ▶ $self->priority(1) if $self->zone;
Branch: 1 branch
BranchTrueFalseTotal
if $self->zone 15 4 19
148  
149 # Fetch the interesting table
150 19 my $ref = _pointer_to_element( table(), $self->_generate_path );
151 19 my $table = $$ref;
152  
153 19 my $highest = 0;
154 19 foreach my $key ( keys %{$table} ) {
155 43 my ( $lo, $hi ) = split ',', $key;
156 43 ▶ $highest = $hi if ( $hi > $highest );
Branch: 1 branch
BranchTrueFalseTotal
if $hi > $highest 28 15 43
157 43 ▶ if ( $self->weight >= $lo && $self->weight <= $hi ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->weight >= $lo and $self->weight <= $hi) 18 25 43
Condition100%3/3
$self->weight >= $lo and $self->weight <= $hi
!ll&&!rl&&r
71818
MC/DC100%2/2
$self->weight >= $lo and $self->weight <= $hi
$self->weight >= $lo $self->weight <= $hi
Truth table100%3/3
A and B
A= $self->weight >= $loB= $self->weight <= $hi
ABresult
0X0
100
111
158 18 $self->cost( $table->{$key} );
159 18 last;
160 }
161 }
162 19 ▶ $ERROR = $self->weight - $highest . " grams too heavy (max: $highest gr.)"
Branch: 1 branch
BranchTrueFalseTotal
if $highest < $self->weight 1 18 19
163 if ( $highest < $self->weight );
164  
165 19 ▶ return ( $self->cost ) ? sprintf( "%0.2f", $self->cost ) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->cost ? : 18 1 19
166 }
167  
168 =pod
169  
170 =head3 _generate_path
171  
172 Internal method to create the path to walk through the pricing table.
173 Don't call this, use L<calculate> instead.
174  
175 =cut
176  
177 19 ▶sub _generate_path {
Subroutine _generate_path: called CC=14 SCAR=26.6
178 19 my $self = shift;
179  
180 19 my @p;
181  
182 19 ▶ if ( $self->zone ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->zone) { } 15 4 19
183 15 push @p, 'world'; # world
184 15 ▶ if ( $self->register ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->register) { } 7 8 15
elsif ($self->tracktrace) { } 1 7 8
185 7 ▶ push @p, 'register', # w/register
Branch: 2 branches
BranchTrueFalseTotal
$self->zone < 4 ? : 6 1 7
$self->machine ? : 1 6 7
186 ( $self->zone < 4 ) # w/register/(europe|world)
187 ? 'europe'
188 : 'world',
189 ( $self->machine ) # w/register/(e|w)/(stamp|machine)
190 ? 'machine'
191 : 'stamp';
192 }
193 elsif ( $self->tracktrace ) {
194 1 push @p, 'plus', 'zone', $self->zone; # w/plus/zone/[1..4]
195 }
196 else {
197 7 ▶ push @p, 'basic', # w/basic
Branch: 2 branches
BranchTrueFalseTotal
$self->zone < 4 ? : 5 2 7
$self->large ? : 0 7 7
198 ( $self->zone < 4 ) # w/basic/(europe|world)
199 ? 'europe'
200 : 'world',
201 ( $self->large ) # w/basic/(e|w)/(large|small)
202 ? 'large'
203 : 'small';
204  
205 7 ▶ if( !$self->large ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->large) 7 0 7
206 # w/basic/(e|w)/small/(machine|stamp)
207 7 ▶ push @p, ( $self->machine ) ? 'machine' : 'stamp';
Branch: 1 branch
BranchTrueFalseTotal
$self->machine ? : 0 7 7
208 }
209  
210 ### priority is now always the default
211 7 push @p, 'priority';
212 #push @p,
213 # ( $self->priority ) # w/basic/(e|w)/(l|s)/(m|s)?/(p|s)
214 # ? 'priority'
215 # : 'standard';
216 }
217 }
218 else {
219 4 push @p, 'netherlands'; # netherlands
220 4 ▶ if ( $self->register ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->register) { } 2 2 4
221 2 push @p, 'register'; # n/register
222 }
223 else {
224 2 ▶ push @p, ( $self->large ) # n/(large|small)
Branch: 1 branch
BranchTrueFalseTotal
$self->large ? : 2 0 2
225 ? 'large'
226 : 'small';
227 }
228 4 ▶ ( push @p, ( $self->machine ) ? 'machine' : 'stamp' )
Branch: 2 branches
BranchTrueFalseTotal
unless $self->large 2 2 4
$self->machine ? : 2 0 2
229 unless $self->large;;
230 }
231 # debug
232 # print (join " :: ", @p), "\n";
233 19 return @p;
234 }
235  
236 =pod
237  
238 =head3 _pointer_to_element
239  
240 Blame L<merlyn> for this internal method. It's using L<List::Util>
241 to "grep" the information needed.
242  
243 Don't call this, use L<calculate> instead.
244  
245 =cut
246  
247 19 ▶sub _pointer_to_element { # Thanks 'merlyn'!
Subroutine _pointer_to_element: called CC=1 SCAR=0.0
248 19 require List::Util;
249 19 ▶ return List::Util::reduce( sub { \( $$a->{$b} ) }, \shift, @_ );
Subroutine __ANON__: called CC=1 SCAR=0.0
250 }
251  
252 =pod
253  
254 =head3 weight
255  
256 Sets and/or returns the weight of the parcel in question in grams.
257  
258 This value is mandatory for the calculations.
259  
260 =head3 large
261  
262 Sets and/or returns the value of this option. Defaults to undef (meaning:
263 the package will fit through the mail slot).
264  
265 =head3 tracktrace
266  
267 Sets and/or returns the value of this options. Defaults to undef (meaning:
268 no track & trace feature wanted). When a parcel destined for abroad
269 weighs over 2 kilograms, default is 1, while over 2kg it's not even
270 optional anymore.
271  
272 =head3 register
273  
274 Sets and/or returns the value of this options. Defaults to undef (meaning:
275 parcel is not registered (Dutch: aangetekend)).
276  
277 =head3 machine
278  
279 Sets and/or returns the value of this options. Defaults to undef (meaning:
280 stamps will be used, not the machine (Dutch: frankeermachine)).
281  
282 Only interesting for destinies within NL. Note that "Pakketzegel AVP"
283 and "Easystamp" should also use this option.
284  
285 =head1 BUGS
286  
287 Please do report bugs/patches to
288 L<http://rt.cpan.org/Public/Dist/Display.html?Name=Business-TNTPost-NL>
289  
290 =head1 CAVEAT
291  
292 The Dutch postal agency (TNT Post) uses many, many, many various ways
293 for you to ship your parcels. Some of them are included in this module,
294 but a lot of them not (maybe in the future? Feel free to patch ;-)
295  
296 This module handles the following shipping ways (page numbers refer to the
297 TNT Post booklet (sorry, all in Dutch)).
298  
299 'Brieven' and 'Pakketten' either paid by 'frankeermachine'. For both,
300 'aangetekend' is optional. The pagenumbers used: 18, 19, 20, 21.
301  
302 These should be the most commom methods of shipment.
303  
304 =head1 AUTHOR
305  
306 Menno Blom,
307 E<lt>blom@cpan.orgE<gt>,
308 L<http://menno.b10m.net/perl/>
309  
310 =head1 COPYRIGHT
311  
312 This program is free software; you can redistribute
313 it and/or modify it under the same terms as Perl itself.
314  
315 The full text of the license can be found in the
316 LICENSE file included with this module.
317  
318 =head1 SEE ALSO
319  
320 L<http://www.tntpost.nl/>,
321 L<http://www.tntpost.nl/zakelijk/Images/TNP%20TB%20digitaal_2_tcm210-527873.pdf>,
322 L<http://www.iso.org/iso/en/prods-services/iso3166ma/index.html>
323  
324 =cut
325  
326 9 1;
↑ Summary NL/Data.pm »