Shipment/Sendle.pm

filter: Statementstmt 21.7% 32 / 147 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 41 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 44.4% 8 / 18 Podpod 100% 3 / 3 totaltotal 15.9% 43 / 269 CC 38 SCAR 45.0
CC
38
Cov
14%
ship
182.0
rate
156.0
_build_services
56.0
CRAP 89.9
« Shipment/CanadaPost.pm ↑ Summary Shipment/Purolator/WSDL/Interfaces/EstimatingService/EstimatingServiceEndpoint.pm »
1 package Shipment::Sendle;
2 1 $Shipment::Sendle::VERSION = '3.11';
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Shipment::Sendle::API;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Shipment::Label;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use MooX::Types::MooseLike::Base qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 extends 'Shipment::Base';
16  
17  
18 1 has 'user' => (
19 is => 'rw',
20 isa => Str,
21 );
22  
23 1 has 'api_key' => (
24 is => 'rw',
25 isa => Str,
26 );
27  
28  
29 1 has 'mode' => (
30 is => 'rw',
31 isa => Enum [
32 qw(
33 sandbox
34 live
35 )
36 ],
37 default => 'sandbox',
38 );
39  
40  
41 has 'api' => (
42 is => 'rw',
43 isa => InstanceOf ['Shipment::Sendle::API'],
44 lazy => 1,
45 0 ▶ default => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
46 0 my $self = shift;
47  
48 0 my $api = Shipment::Sendle::API->new(
49 user => $self->user,
50 api_key => $self->api_key,
51 mode => $self->mode
52 );
53 0 ▶ $api->debug(1) if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
54  
55 0 $api;
56 },
57 1 );
58  
59 0 ▶sub _api_response_error {
Subroutine _api_response_error: not called CC=6 SCAR=37.4
60 0 my ($self, $response) = @_;
61  
62 0 $self->error('');
63  
64 0 ▶ if ($response->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->{"error"}) 0 0 0
65 0 $self->error($response->{error});
66 }
67  
68 0 ▶ if (ref $response->{content} eq 'HASH' && $response->{content}->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $response->{"content"} eq "HASH" and $response->{"content"}{"error"}) 0 0 0
Condition0%0/3
ref $response->{"content"} eq "HASH" and $response->{"content"}{"error"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $response->{"content"} eq "HASH" and $response->{"content"}{"error"}
ref $response->{"content"} eq "HASH" $response->{"content"}{"error"}
Truth table0%0/3
A and B
A= ref $response->{"content"} eq "HASH"B= $response->{"content"}{"error"}
ABresult
0X0
100
111
69 0 my $error = '';
70 0 foreach my $key (keys %{$response->{content}->{messages}}) {
71 $error .= $key . ': '
72 0 . join(',', @{$response->{content}->{messages}->{$key}}) . "\n";
73 }
74 0 $self->error($error);
75 }
76  
77 0 ▶ return 1 if $self->error;
Branch: 1 branch
BranchTrueFalseTotal
if $self->error 0 0 0
78  
79 0 return;
80 }
81  
82 1 has '+printer_type' => (
83 isa => Enum [qw( letter cropped )],
84 default => 'letter',
85 );
86  
87  
88 0 ▶sub _build_services {
Subroutine _build_services: not called CC=7 SCAR=40.3
89 0 my $self = shift;
90  
91 0 my %services;
92  
93 0 ▶ if ($self->from_address() && $self->to_address()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->from_address and $self->to_address) { } 0 0 0
Condition0%0/3
$self->from_address and $self->to_address
!ll&&!rl&&r
000
MC/DC0%0/2
$self->from_address and $self->to_address
$self->from_address $self->to_address
Truth table0%0/3
A and B
A= $self->from_addressB= $self->to_address
ABresult
0X0
100
111
94  
95 0 my $response = $self->api->quote(
96 pickup_postcode => $self->from_address()->postal_code,
97 pickup_suburb => $self->from_address()->city,
98 pickup_country => $self->from_address()->country_code,
99 delivery_postcode => $self->to_address()->postal_code,
100 delivery_suburb => $self->to_address()->city,
101 delivery_country => $self->to_address()->country_code,
102 weight_units => 'lb',
103 weight_value => 1,
104 );
105  
106 0 ▶ if ($self->_api_response_error($response)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_api_response_error($response)) 0 0 0
107 0 return {};
108 }
109  
110 0 my $quote = $response->{content}->[0];
111 0 my ($y, $m, $d) = split('-', $quote->{eta}->{date_range}->[1]);
112 0 my $eta;
113 0 ▶ if ($y && $m && $d) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y and $m and $d) 0 0 0
Condition0%0/6
$y and $m and $d
!ll&&!rl&&r
000
$y and $m
!ll&&!rl&&r
000
MC/DC0%0/3
$y and $m and $d
$y $m $d
Truth table0%0/4
A and B and C
A= $yB= $mC= $d
ABCresult
0XX0
10X0
1100
1111
114 0 $eta = {year => $y, month => $m, day => $d};
115 }
116 $services{ground} = $services{$quote->{plan_name}} =
117 Shipment::Service->new(
118 id => $quote->{plan_name},
119 name => $quote->{route}->{description},
120 cost => Data::Currency->new(
121 $quote->{quote}->{gross}->{amount},
122 $quote->{quote}->{gross}->{currency}
123 ),
124 base_cost => Data::Currency->new(
125 $quote->{quote}->{net}->{amount},
126 $quote->{quote}->{net}->{currency}
127 ),
128 tax => Data::Currency->new(
129 $quote->{quote}->{tax}->{amount},
130 $quote->{quote}->{tax}->{currency}
131 ),
132 0 etd => $quote->{eta}->{days_range}->[1],
133 eta => $eta,
134 );
135  
136 }
137 else {
138 # from and to address required
139 0 $self->error("Both from and to address are required.");
140 0 return {};
141 }
142  
143 0 \%services;
144 }
145  
146  
147 0 ▶sub rate {
Subroutine rate: not called CC=12 SCAR=50.5
Pod documented
148 0 my ($self, $service_id) = @_;
149  
150 0 ▶ $service_id ||= 'ground';
Condition0%0/2
$service_id ||= "ground"
l!l
00
MC/DC0%0/1
$service_id ||= "ground"
$service_id
Truth table0%0/2
A
A= $service_id
Aresult
11
00
151  
152 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
153 0 $service_id = $self->services->{$service_id}->id;
154 }
155 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
156 0 $self->error("service ($service_id) not available");
157 0 $service_id = '';
158 0 };
159 0 ▶ return unless $service_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $service_id 0 0 0
160  
161 0 ▶ if ( $self->from_address()
Branch: 1 branch
BranchTrueFalseTotal
if ($self->from_address and $self->to_address and $self->count_packages > 0) { } 0 0 0
Condition0%0/6
$self->from_address and $self->to_address and $self->count_packages > 0
!ll&&!rl&&r
000
$self->from_address and $self->to_address
!ll&&!rl&&r
000
MC/DC0%0/3
$self->from_address and $self->to_address and $self->count_packages > 0
$self->from_address $self->to_address $self->count_packages > 0
Truth table0%0/4
A and B and C
A= $self->from_addressB= $self->to_addressC= $self->count_packages > 0
ABCresult
0XX0
10X0
1100
1111
162 && $self->to_address()
163 && $self->count_packages > 0)
164 {
165  
166 0 my ($cost, $base_cost, $tax) = (0, 0, 0);
167 0 my $etd = 0;
168 0 my $eta;
169 0 my $currency = $self->currency;
170  
171 0 foreach my $package (@{$self->packages}) {
172  
173 0 my $volume =
174 ($package->length * $package->width * $package->height);
175 0 my $volume_unit = 'in3';
176 0 ▶ if ($self->dim_unit eq 'cm') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dim_unit eq "cm") 0 0 0
177 0 $volume *= 0.000001;
178 0 $volume_unit = 'm3';
179 }
180  
181 0 my $response = $self->api->quote(
182 pickup_address_line1 => $self->from_address()->address1,
183 pickup_address_line2 => $self->from_address()->address2,
184 pickup_postcode => $self->from_address()->postal_code,
185 pickup_suburb => $self->from_address()->city,
186 pickup_country => $self->from_address()->country_code,
187 delivery_address_line1 => $self->to_address()->address1,
188 delivery_address_line2 => $self->to_address()->address2,
189 delivery_postcode => $self->to_address()->postal_code,
190 delivery_suburb => $self->to_address()->city,
191 delivery_country => $self->to_address()->country_code,
192 weight_units => $self->weight_unit,
193 weight_value => $package->weight,
194 volume_units => $volume_unit,
195 volume_value => $volume,
196 );
197  
198 0 ▶ if ($self->_api_response_error($response)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_api_response_error($response)) 0 0 0
199 0 return;
200 }
201  
202 0 my $quote = $response->{content}->[0];
203  
204 0 $cost += $quote->{quote}->{gross}->{amount};
205 0 $base_cost += $quote->{quote}->{net}->{amount};
206 0 $tax += $quote->{quote}->{tax}->{amount};
207 0 $currency = $quote->{quote}->{gross}->{currency};
208 0 $etd = $quote->{eta}->{days_range}->[1];
209 0 my ($y, $m, $d) = split('-', $quote->{eta}->{date_range}->[1]);
210 0 ▶ if ($y && $m && $d) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y and $m and $d) 0 0 0
Condition0%0/6
$y and $m and $d
!ll&&!rl&&r
000
$y and $m
!ll&&!rl&&r
000
MC/DC0%0/3
$y and $m and $d
$y $m $d
Truth table0%0/4
A and B and C
A= $yB= $mC= $d
ABCresult
0XX0
10X0
1100
1111
211 0 $eta = {year => $y, month => $m, day => $d};
212 }
213 }
214  
215 $self->service(
216 Shipment::Service->new(
217 id => $service_id,
218 0 name => $self->services->{$service_id}->name,
219 cost => Data::Currency->new($cost, $currency),
220 base_cost => Data::Currency->new($base_cost, $currency),
221 tax => Data::Currency->new($tax, $currency),
222 etd => $etd,
223 eta => $eta,
224 )
225 );
226 }
227 else {
228 # from and to address, packages required
229 0 $self->error(
230 "Both from and to address and at least 1 package are required for rating."
231 );
232 }
233  
234 0 return;
235 }
236  
237  
238 0 ▶sub ship {
Subroutine ship: not called CC=13 SCAR=52.0
Pod documented
239 0 my ($self, $service_id) = @_;
240  
241 0 ▶ $service_id ||= 'ground';
Condition0%0/2
$service_id ||= "ground"
l!l
00
MC/DC0%0/1
$service_id ||= "ground"
$service_id
Truth table0%0/2
A
A= $service_id
Aresult
11
00
242  
243 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
244 0 $service_id = $self->services->{$service_id}->id;
245 }
246 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
247 0 $self->error("service ($service_id) not available");
248 0 $service_id = '';
249 0 };
250 0 ▶ return unless $service_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $service_id 0 0 0
251  
252 0 ▶ if ( $self->from_address()
Branch: 1 branch
BranchTrueFalseTotal
if ($self->from_address and $self->to_address and $self->count_packages > 0) { } 0 0 0
Condition0%0/6
$self->from_address and $self->to_address and $self->count_packages > 0
!ll&&!rl&&r
000
$self->from_address and $self->to_address
!ll&&!rl&&r
000
MC/DC0%0/3
$self->from_address and $self->to_address and $self->count_packages > 0
$self->from_address $self->to_address $self->count_packages > 0
Truth table0%0/4
A and B and C
A= $self->from_addressB= $self->to_addressC= $self->count_packages > 0
ABCresult
0XX0
10X0
1100
1111
253 && $self->to_address()
254 && $self->count_packages > 0)
255 {
256  
257 0 my $shipment_id;
258 0 my ($cost, $base_cost, $tax) = (0, 0, 0);
259 0 my $currency = $self->currency;
260 0 my $package_index = 0;
261  
262 0 foreach my $package (@{$self->packages}) {
263  
264 0 my $volume =
265 ($package->length * $package->width * $package->height);
266 0 my $volume_unit = 'in3';
267 0 ▶ if ($self->dim_unit eq 'cm') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dim_unit eq "cm") 0 0 0
268 0 $volume *= 0.000001;
269 0 $volume_unit = 'm3';
270 }
271  
272 0 ▶ my $response = $self->api->create_order(
Condition0%0/4
$self->get_reference(0) || ''
l!l
00
$self->special_instructions || 'n/a'
l!l
00
MC/DC0%0/1
$self->get_reference(0) || ''
$self->get_reference(0)
MC/DC0%0/1
$self->special_instructions || 'n/a'
$self->special_instructions
Truth table0%0/2
A
A= $self->get_reference(0)
Aresult
11
00
Truth table0%0/2
A
A= $self->special_instructions
Aresult
11
00
273 description => $package->notes,
274 weight =>
275 {value => $package->weight, units => $self->weight_unit},
276 volume => {value => $volume, units => $volume_unit},
277 customer_reference => $self->get_reference(0) || '',
278 sender => {
279 contact => {
280 name => $self->from_address()->name,
281 phone => $self->from_address()->phone,
282 email => $self->from_address()->email,
283 company => $self->from_address()->company,
284 },
285 address => {
286 address_line1 => $self->from_address()->address1,
287 address_line2 => $self->from_address()->address2,
288 suburb => $self->from_address()->city,
289 state_name => $self->from_address()->state_code,
290 postcode => $self->from_address()->postal_code,
291 country => $self->from_address()->country,
292 }
293 },
294 receiver => {
295 instructions => $self->special_instructions || 'n/a',
296 contact => {
297 name => $self->to_address()->name,
298 phone => $self->to_address()->phone,
299 email => $self->to_address()->email,
300 company => $self->to_address()->company,
301 },
302 address => {
303 address_line1 => $self->to_address()->address1,
304 address_line2 => $self->to_address()->address2,
305 suburb => $self->to_address()->city,
306 state_name => $self->to_address()->state_code,
307 postcode => $self->to_address()->postal_code,
308 country => $self->to_address()->country,
309 }
310 },
311 );
312  
313 0 ▶ if ($self->_api_response_error($response)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_api_response_error($response)) 0 0 0
314 0 return;
315 }
316  
317 0 my $order = $response->{content};
318  
319 0 $cost += $order->{price}->{gross}->{amount};
320 0 $base_cost += $order->{price}->{net}->{amount};
321 0 $tax += $order->{price}->{tax}->{amount};
322 0 $currency = $order->{price}->{gross}->{currency};
323  
324 0 my $tracking_id = $order->{sendle_reference};
325 0 ▶ $shipment_id ||= $tracking_id;
Condition0%0/3
$shipment_id ||= $tracking_id
l!l&&r!l&&!r
000
MC/DC0%0/2
$shipment_id ||= $tracking_id
$shipment_id $tracking_id
Truth table0%0/3
A ||= B
A= $shipment_idB= $tracking_id
ABresult
1X1
011
000
326  
327 0 $self->get_package($package_index)->id($order->{order_id});
328 0 $self->get_package($package_index)->tracking_id($tracking_id);
329 $self->get_package($package_index)->cost(
330 Data::Currency->new(
331 $order->{price}->{gross}->{amount},
332 $order->{price}->{gross}->{currency}
333 )
334 0 );
335  
336 $response = $self->api->label(
337 id => $order->{order_id},
338 0 type => $self->printer_type . '.pdf'
339 );
340  
341 0 ▶ if ($self->_api_response_error($response)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_api_response_error($response)) 0 0 0
342 $self->error(
343 0 'failed to get label for order: ' . $order->{order_id});
344 }
345  
346 $self->get_package($package_index)->label(
347 Shipment::Label->new(
348 { tracking_id => $tracking_id,
349 content_type => 'application/'
350 . $self->printer_type . '-pdf',
351 data => $response->{raw},
352 0 file_name => $tracking_id . '.pdf',
353 },
354 )
355 );
356  
357 0 $package_index++;
358  
359 }
360  
361 0 $self->tracking_id($shipment_id);
362 $self->service(
363 Shipment::Service->new(
364 id => $service_id,
365 0 name => $self->services->{$service_id}->name,
366 cost => Data::Currency->new($cost, $currency),
367 base_cost => Data::Currency->new($base_cost, $currency),
368 tax => Data::Currency->new($tax, $currency),
369 )
370 );
371  
372 }
373 else {
374 # from and to address required
375 0 $self->error(
376 "Both from and to address, and at least 1 package are required for shipping."
377 );
378 }
379  
380 0 return;
381  
382 }
383  
384  
385 0 ▶sub track {
Subroutine track: not called CC=3 SCAR=24.8
Pod documented
386 0 my ($self) = @_;
387  
388 0 ▶ if (!$self->tracking_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->tracking_id) 0 0 0
389 0 $self->error('no tracking id provided');
390 0 return;
391 }
392  
393 0 my $response = $self->api->track_order(ref => $self->tracking_id);
394  
395 0 ▶ if ($self->_api_response_error($response)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_api_response_error($response)) 0 0 0
396 0 return;
397 }
398  
399 0 return $response->{content}->{state};
400  
401 }
402  
403  
404 1 1;
405  
406 __END__
« Shipment/CanadaPost.pm ↑ Summary Shipment/Purolator/WSDL/Interfaces/EstimatingService/EstimatingServiceEndpoint.pm »