Ebay/Order.pm

filter: Statementstmt 79.8% 131 / 164 Branchbran 45.6% 21 / 46 Conditioncond 23.5% 4 / 17 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 84.6% 33 / 39 Podpod 100% 23 / 23 totaltotal 71.1% 212 / 298 CC 37 SCAR 16.3
CC
37
Cov
69%
ebay_site
30.0
can_be_imported
12.0
order_status
12.0
CRAP 5.1
« Ebay.pm ↑ Summary Ebay/Order/Address.pm »
1 package Marketplace::Ebay::Order;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use DateTime::Format::ISO8601;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use MooX::Types::MooseLike::Base qw(Str HashRef Int Object Bool);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Marketplace::Ebay::Order::Address;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use Marketplace::Ebay::Order::Item;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 NAME
16  
17 Marketplace::Ebay::Order
18  
19 =head1 DESCRIPTION
20  
21 Class to handle the xml structures found in the GetOrders call.
22  
23 L<http://developer.ebay.com/devzone/xml/docs/Reference/ebay/GetOrders.html>
24  
25 The aim is to have a consistent interface with
26 L<Amazon::MWS::XML::Order> so importing the orders can happens almost
27 transparently.
28  
29 =cut
30  
31 =head1 ACCESSORS/METHODS
32  
33 =head2 order
34  
35 The raw structure got from the XML parsing
36  
37 =head2 shop_type
38  
39 Always returns C<ebay>
40  
41 =cut
42  
43 5 has order => (is => 'ro', isa => HashRef, required => 1);
44  
45 sub shop_type {
46 1 ▶ return 'ebay';
Subroutine shop_type: called CC=1 SCAR=0.0
Pod documented
47 }
48  
49 =head2 name_from_shipping_address
50  
51 By default, lookup the name from the shipping address. Defaults to
52 true. Otherwise look it up from the first item. Prior to version 0.19,
53 the name was looked up from the first item only.
54  
55 =cut
56  
57 1 ▶has name_from_shipping_address => (is => 'ro', isa => Bool, default => sub { 1 });
Subroutine __ANON__: called CC=1 SCAR=0.0
58  
59 =head2 order_number
60  
61 read-write accessor for the (shop) order number so you can set this
62 while importing it.
63  
64 =head2 payment_status
65  
66 read-write accessor for the payment status, so the shop can set it
67 while importing it.
68  
69 =cut
70  
71 5 has order_number => (is => 'rw', isa => Str);
72 5 has payment_status => (is => 'rw', isa => Str);
73  
74 =head2 can_be_imported
75  
76 Return true if both orderstatus and checkout status are completed
77  
78 =cut
79  
80 sub can_be_imported {
81 0 ▶ my ($self) = @_;
Subroutine can_be_imported: not called CC=3 SCAR=24.8
Pod documented
82 0 my $order = $self->order;
83 0 ▶ if ($order->{OrderStatus} eq 'Completed' and
Branch: 1 branch
BranchTrueFalseTotal
if ($order->{'OrderStatus'} eq 'Completed' and $order->{'CheckoutStatus'}{'Status'} eq 'Complete') { } 0 0 0
Condition0%0/3
$order->{"OrderStatus"} eq "Completed" and $order->{"CheckoutStatus"}{"Status"} eq "Complete"
!ll&&!rl&&r
000
MC/DC0%0/2
$order->{"OrderStatus"} eq "Completed" and $order->{"CheckoutStatus"}{"Status"} eq "Complete"
$order->{"OrderStatus"} eq "Completed" $order->{"CheckoutStatus"}{"Status"} eq "Complete"
Truth table0%0/3
A and B
A= $order->{"OrderStatus"} eq "Completed"B= $order->{"CheckoutStatus"}{"Status"} eq "Complete"
ABresult
0X0
100
111
84 $order->{CheckoutStatus}->{Status} eq 'Complete') {
85 0 return 1;
86 }
87 else {
88 0 return 0;
89 }
90 }
91  
92 =head2 order_status
93  
94 Return the order status and the payment status, separated by a colon.
95  
96 =cut
97  
98  
99 sub order_status {
100 0 ▶ my ($self) = @_;
Subroutine order_status: not called CC=3 SCAR=24.8
Pod documented
101 0 my $order = $self->order;
102 0 ▶ my $order_status = $order->{OrderStatus} || 'Unknown';
Condition0%0/2
$order->{'OrderStatus'} || 'Unknown'
l!l
00
MC/DC0%0/1
$order->{'OrderStatus'} || 'Unknown'
$order->{'OrderStatus'}
Truth table0%0/2
A
A= $order->{'OrderStatus'}
Aresult
11
00
103 0 ▶ my $payment_status = $order->{CheckoutStatus}->{Status} || 'Unknown';
Condition0%0/2
$order->{'CheckoutStatus'}{'Status'} || 'Unknown'
l!l
00
MC/DC0%0/1
$order->{'CheckoutStatus'}{'Status'} || 'Unknown'
$order->{'CheckoutStatus'}{'Status'}
Truth table0%0/2
A
A= $order->{'CheckoutStatus'}{'Status'}
Aresult
11
00
104 0 return "$order_status: $payment_status";
105 }
106  
107  
108 =head2 ebay_order_number
109  
110 Ebay order id.
111  
112 =head2 remote_shop_order_id
113  
114 Same as C<ebay_order_number>
115  
116 =cut
117  
118 sub ebay_order_number {
119 0 ▶ return shift->order->{OrderID};
Subroutine ebay_order_number: not called CC=1 SCAR=6.9
Pod documented
120 }
121  
122 sub remote_shop_order_id {
123 0 ▶ return shift->ebay_order_number;
Subroutine remote_shop_order_id: not called CC=1 SCAR=6.9
Pod documented
124 }
125  
126 5 has shipping_address => (is => 'lazy');
127  
128 sub _build_shipping_address {
129 1 ▶ my $self = shift;
Subroutine _build_shipping_address: called CC=1 SCAR=0.0
130 1 my $address = $self->order->{ShippingAddress};
131 1 return Marketplace::Ebay::Order::Address->new(%$address);
132 }
133  
134 5 has items_ref => (is => 'lazy');
135  
136 sub _build_items_ref {
137 2 ▶ my ($self) = @_;
Subroutine _build_items_ref: called CC=2 SCAR=6.9
138 2 my $orderline = $self->orderline;
139 2 my @items;
140 2 foreach my $item (@$orderline) {
141 # print Dumper($item);
142 2 push @items, Marketplace::Ebay::Order::Item->new(struct => $item);
143 }
144 2 return \@items;
145 }
146  
147  
148 5 has number_of_items => (is => 'lazy', isa => Int);
149  
150 sub _build_number_of_items {
151 1 ▶ my $self = shift;
Subroutine _build_number_of_items: called CC=2 SCAR=6.9
152 1 my @items = $self->items;
153 1 my $total = 0;
154 1 foreach my $i (@items) {
155 1 $total += $i->quantity;
156 }
157 1 return $total;
158 }
159  
160 5 has first_item => (is => 'lazy', isa => Object);
161  
162 sub _build_first_item {
163 2 ▶ my $self = shift;
Subroutine _build_first_item: called CC=2 SCAR=7.0
164 2 my ($first, @rest) = $self->items;
165 2 ▶ die "Missing items in transaction!" unless $first;
Branch: 1 branch
BranchTrueFalseTotal
unless $first 0 2 2
166 2 return $first;
167 }
168  
169 =head2 orderline
170  
171 An arrayref with the TransactionArray.Transaction structure. This is
172 used internally by C<items>.
173  
174 =cut
175  
176 sub orderline {
177 2 ▶ return shift->order->{TransactionArray}->{Transaction};
Subroutine orderline: called CC=1 SCAR=0.0
Pod documented
178 }
179  
180 =head2 items
181  
182 Return a list of L<Marketplace::Ebay::Order::Item> objects.
183  
184 =cut
185  
186 sub items {
187 6 ▶ my $self = shift;
Subroutine items: called CC=1 SCAR=0.0
Pod documented
188 6 return @{ $self->items_ref };
189 }
190  
191 =head2 order_date
192  
193 Return a DateTime object with the creation time of the order.
194  
195 =cut
196  
197 sub order_date {
198 1 ▶ my $self = shift;
Subroutine order_date: called CC=2 SCAR=7.6
Pod documented
199 1 ▶ if (my $date = $self->order->{CreatedTime}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $date = $self->order->{"CreatedTime"}) 1 0 1
200 1 return DateTime::Format::ISO8601->parse_datetime($date);
201 }
202 0 return;
203 }
204  
205 =head2 email
206  
207 The email of the buyer. Given that this is provided per item, the
208 first one is used.
209  
210 =cut
211  
212 sub email {
213 1 ▶ return shift->first_item->email;
Subroutine email: called CC=1 SCAR=0.0
Pod documented
214 }
215  
216 =head2 first_name
217  
218 The first name of the buyer, looked up from the first item.
219  
220 =cut
221  
222 sub first_name {
223 2 ▶ my $self = shift;
Subroutine first_name: called CC=3 SCAR=11.0
Pod documented
224 2 ▶ if ($self->name_from_shipping_address) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->name_from_shipping_address) { } 1 1 2
225 1 return $self->first_last_from_shipping_address->{first_name};
226 }
227 else {
228 1 ▶ return $self->first_item->first_name || '';
Condition50%1/2
$self->first_item->first_name || ''
l!l
10
MC/DC0%0/1
$self->first_item->first_name || ''
$self->first_item->first_name
Truth table50%1/2
A
A= $self->first_item->first_name
Aresult
11
00
229 }
230 }
231  
232 =head2 last_name
233  
234 The last name of the buyer, looked up from the first item.
235  
236 =cut
237  
238  
239 sub last_name {
240 2 ▶ my $self = shift;
Subroutine last_name: called CC=3 SCAR=11.0
Pod documented
241 2 ▶ if ($self->name_from_shipping_address) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->name_from_shipping_address) { } 1 1 2
242 1 return $self->first_last_from_shipping_address->{last_name};
243 }
244 else {
245 1 ▶ return $self->first_item->last_name || '';
Condition50%1/2
$self->first_item->last_name || ''
l!l
10
MC/DC0%0/1
$self->first_item->last_name || ''
$self->first_item->last_name
Truth table50%1/2
A
A= $self->first_item->last_name
Aresult
11
00
246 }
247 }
248  
249 5 has first_last_from_shipping_address => (is => 'lazy', isa => HashRef);
250  
251 sub _build_first_last_from_shipping_address {
252 1 ▶ my $self = shift;
Subroutine _build_first_last_from_shipping_address: called CC=3 SCAR=11.4
253 1 my ($first_name, $last_name) = ('', '');
254 1 ▶ if (my $name = $self->shipping_address->name) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $name = $self->shipping_address->name) 1 0 1
255 1 ▶ if ($name =~ m/\s*(.+?)\s+([\w-]+)\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /\s*(.+?)\s+([\w-]+)\s*$/) { } 1 0 1
256 1 $first_name = $1;
257 1 $last_name = $2;
258 }
259 else {
260 0 ($first_name, $last_name) = split(/\s+/, $name, 2);
261 }
262 }
263 return {
264 1 first_name => $first_name,
265 last_name => $last_name,
266 };
267 }
268  
269 =head2 comments
270  
271 The BuyerCheckoutMessage's field of the order.
272  
273 =cut
274  
275 sub comments {
276 0 ▶ my $self = shift;
Subroutine comments: not called CC=1 SCAR=6.9
Pod documented
277 0 return $self->order->{BuyerCheckoutMessage};
278 }
279  
280 =head2 shipping_method
281  
282 The order's ShippingServiceSelected.ShippingService value
283  
284 =cut
285  
286 sub shipping_method {
287 1 ▶ my $self = shift;
Subroutine shipping_method: called CC=3 SCAR=12.0
Pod documented
288 1 ▶ if (my $shipping = $self->order->{ShippingServiceSelected}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $shipping = $self->order->{"ShippingServiceSelected"}) 1 0 1
289 1 ▶ if (my $service = $shipping->{ShippingService}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $service = $shipping->{"ShippingService"}) 1 0 1
290 1 return $service;
291 }
292 }
293 0 return '';
294 }
295  
296 =head2 shipping_additional_costs
297  
298 The order's ShippingServiceSelected.ShippingServiceAdditionalCost
299 value. I.e., the cost of the shipping for the other items ordered.
300  
301 =cut
302  
303 sub shipping_additional_costs {
304 1 ▶ my $self = shift;
Subroutine shipping_additional_costs: called CC=4 SCAR=16.4
Pod documented
305 1 my $cost = 0;
306 1 ▶ if (my $shipping = $self->order->{ShippingServiceSelected}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $shipping = $self->order->{"ShippingServiceSelected"}) 1 0 1
307 1 ▶ if (my $num = $shipping->{ShippingServiceAdditionalCost}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $num = $shipping->{"ShippingServiceAdditionalCost"}) 0 1 1
308 0 ▶ $cost = $num->{_} || 0;
Condition0%0/2
$num->{'_'} || 0
l!l
00
MC/DC0%0/1
$num->{'_'} || 0
$num->{'_'}
Truth table0%0/2
A
A= $num->{'_'}
Aresult
11
00
309 }
310 }
311 1 return sprintf('%.2f', $cost);
312 }
313  
314 =head2 shipping_first_unit
315  
316 The order's ShippingServiceSelected.ShippingServiceCost value. This is
317 the cost of the shipping for the first item.
318  
319 =cut
320  
321 sub shipping_first_unit {
322 1 ▶ my $self = shift;
Subroutine shipping_first_unit: called CC=4 SCAR=14.5
Pod documented
323 1 my $cost = 0;
324 1 ▶ if (my $shipping = $self->order->{ShippingServiceSelected}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $shipping = $self->order->{"ShippingServiceSelected"}) 1 0 1
325 1 ▶ if (my $num = $shipping->{ShippingServiceCost}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $num = $shipping->{"ShippingServiceCost"}) 1 0 1
326 1 ▶ $cost = $num->{_} || 0;
Condition50%1/2
$num->{'_'} || 0
l!l
10
MC/DC0%0/1
$num->{'_'} || 0
$num->{'_'}
Truth table50%1/2
A
A= $num->{'_'}
Aresult
11
00
327 }
328 }
329 1 return sprintf('%.2f', $cost);
330 }
331  
332 =head2 shipping_cost
333  
334 The total cost of the shipping. It is the C<shipping_first_unit> + the
335 additional costs multiplied by the number of additional items.
336  
337 =cut
338  
339 sub shipping_cost {
340 1 ▶ my $self = shift;
Subroutine shipping_cost: called CC=3 SCAR=12.3
Pod documented
341 1 my $item_shipping = $self->shipping_first_unit;
342 1 ▶ if (my $additional = $self->shipping_additional_costs) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $additional = $self->shipping_additional_costs) 1 0 1
343 1 ▶ if ($self->number_of_items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->number_of_items > 1) 0 1 1
344 0 my $others = $self->number_of_items - 1;
345 0 $item_shipping = $item_shipping + ($additional * $others);
346 }
347 }
348 1 return sprintf('%.2f', $item_shipping);
349 }
350  
351 =head2 total_cost
352  
353 The total of the order, as reported by Ebay.
354  
355 =cut
356  
357 sub total_cost {
358 1 ▶ my $self = shift;
Subroutine total_cost: called CC=3 SCAR=11.2
Pod documented
359 1 my $total = 0;
360 1 ▶ if (my $amount = $self->order->{Total}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $amount = $self->order->{"Total"}) 1 0 1
361 1 ▶ if (my $num = $amount->{_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $num = $amount->{"_"}) 1 0 1
362 1 $total = $num;
363 }
364 }
365 1 return sprintf('%.2f', $total);
366 }
367  
368 =head2 subtotal
369  
370 Sum of the subtotal of all items.
371  
372 =cut
373  
374 sub subtotal {
375 1 ▶ my $self = shift;
Subroutine subtotal: called CC=2 SCAR=6.9
Pod documented
376 1 my @items = $self->items;
377 1 my $total = 0;
378 1 foreach my $i (@items) {
379 1 $total += $i->subtotal;
380 }
381 1 return sprintf('%.2f', $total);
382 }
383  
384 =head2 currency
385  
386 The currency code of the order (looked up in the total).
387  
388 =cut
389  
390 sub currency {
391 1 ▶ my $self = shift;
Subroutine currency: called CC=2 SCAR=7.6
Pod documented
392 1 ▶ if (my $currency = $self->order->{Total}->{currencyID}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $currency = $self->order->{'Total'}{'currencyID'}) { } 1 0 1
393 1 return $currency;
394 }
395 else {
396 0 die "Can't find currency total! " . Dumper($self->order);
397 }
398 }
399  
400 =head2 payment_method
401  
402 The CheckoutStatus.PaymentMethod value of the order.
403  
404 =cut
405  
406 sub payment_method {
407 1 ▶ my $self = shift;
Subroutine payment_method: called CC=2 SCAR=7.6
Pod documented
408 1 ▶ if (my $checkout = $self->order->{CheckoutStatus}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $checkout = $self->order->{"CheckoutStatus"}) 1 0 1
409 1 return $checkout->{PaymentMethod};
410 }
411 0 return;
412 }
413  
414 =head2 order_is_shipped
415  
416 Return true if all the items are marked as shipped.
417  
418 =cut
419  
420 sub order_is_shipped {
421 1 ▶ my $self = shift;
Subroutine order_is_shipped: called CC=3 SCAR=11.2
Pod documented
422 1 my $shipped;
423 1 foreach my $item ($self->items) {
424 1 ▶ if ($item->is_shipped) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->is_shipped) { } 0 1 1
425 0 $shipped = 1;
426 }
427 else {
428 1 $shipped = 0;
429 1 last;
430 }
431 }
432 1 return $shipped;
433 }
434  
435 =head2 ebay_site
436  
437 Return the site where the order was placed. We have to loop over all
438 the items and check if they match. If they don't, we throw an
439 exception.
440  
441 =cut
442  
443 sub ebay_site {
444 0 ▶ my $self = shift;
Subroutine ebay_site: not called CC=5 SCAR=34.0
Pod documented
445 0 my $site;
446 0 foreach my $item ($self->items) {
447 0 my $item_site = $item->ebay_site;
448 0 ▶ die $item->sku . " has not a Site attached!" unless $item_site;
Branch: 1 branch
BranchTrueFalseTotal
unless $item_site 0 0 0
449 # if defined, check, otherwise assign;
450 0 ▶ if (defined $site) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $site) { } 0 0 0
451 0 ▶ if ($site ne $item_site) {
Branch: 1 branch
BranchTrueFalseTotal
if ($site ne $item_site) 0 0 0
452 0 die "Mismatch $site != $item_site on " . Dumper($self);
453 }
454 }
455 else {
456 0 $site = $item_site;
457 }
458 }
459 0 return $site;
460 }
461  
462 =head2 username
463  
464 The ebay's username of the buyer.
465  
466 =cut
467  
468 sub username {
469 1 ▶ my $self = shift;
Subroutine username: called CC=2 SCAR=7.2
Pod documented
470 1 ▶ return $self->order->{BuyerUserID} || '';
Condition50%1/2
$self->order->{'BuyerUserID'} || ''
l!l
10
MC/DC0%0/1
$self->order->{'BuyerUserID'} || ''
$self->order->{'BuyerUserID'}
Truth table50%1/2
A
A= $self->order->{'BuyerUserID'}
Aresult
11
00
471 }
472  
473  
474 5 1;
« Ebay.pm ↑ Summary Ebay/Order/Address.pm »