Amazon-MWS
view release on metacpan or search on metacpan
lib/Amazon/MWS/XML/Order.pm view on Meta::CPAN
package Amazon::MWS::XML::Order;
use Amazon::MWS::XML::Address;
use Amazon::MWS::XML::OrderlineItem;
use strict;
use warnings;
use DateTime;
use DateTime::Format::ISO8601;
use Data::Dumper;
use Moo;
use MooX::Types::MooseLike::Base qw(:all);
use namespace::clean;
=head1 NAME
Amazon::MWS::XML::Order
=head1 DESCRIPTION
Class to handle the xml structures returned by ListOrders and
ListOrderItems.
The constructor is meant to be called by L<Amazon::MWS::Uploader> when
C<get_orders> is called. A list of objects of this class will be
returned.
=head1 SYNOPSIS
my $order = Amazon::MWS::XML::Order->new(order => $struct, orderline => \@struct);
my @items = $order->items;
print $order->order_number, $order->amazon_order_number;
=head1 ACCESSORS
They should be passed to the constructor and are complex structures
parsed from the output of L<Amazon::MWS::Client>.
=head2 order
It should be the output of C<ListOrders> or C<GetOrder> without the
root, e.g. C<$response->{Orders}->{Order}->[0]>
Field description:
http://docs.developer.amazonservices.com/en_US/orders/2013-09-01/Orders_GetOrder.html
=head2 orderline
It should be the output of C<ListOrderItems> without the root, like
C<$response->{OrderItems}->{OrderItem}>.
=head2 retrieve_orderline_sub
If you want to save API calls, instead of initialize the orderline,
you may want to pass a subroutine (which will accept no arguments, so
it should be a closure) to the constructor instead, which will be
called lazily if the object needs to access the orderline.
=head2 order_number
Our order ID.
=head2 shipping_address
Shipping address as C<Amazon::MWS::Client::Address> object.
=cut
lib/Amazon/MWS/XML/Order.pm view on Meta::CPAN
my $self = shift;
my ($first, $last) = $self->_get_first_last_name;
return $first || '';
}
has last_name => (is => 'lazy');
sub _build_last_name {
my $self = shift;
my ($first, $last) = $self->_get_first_last_name;
return $last || '';
}
sub _get_first_last_name {
my $self = shift;
my $address = $self->shipping_address;
die "Missing name in shipping address" unless $address->name;
# this is totally euristic
my ($first_name, $last_name) = ('', '');
if (my $name = $address->name) {
if ($name =~ m/\s*(.+?)\s+([\w-]+)\s*$/) {
$first_name = $1;
$last_name = $2;
}
elsif ($name =~ m/\s*(.+?)\s*$/) {
# nothing to split, so this is just the last name
$last_name = $1;
}
}
return ($first_name, $last_name);
}
has items_ref => (is => 'lazy');
sub _build_items_ref {
my ($self) = @_;
my $orderline = $self->orderline;
my @items;
foreach my $item (@$orderline) {
# print Dumper($item);
push @items, Amazon::MWS::XML::OrderlineItem->new(%$item);
}
return \@items;
}
=head2 items
Return a list of L<Amazon::MWS::XML::OrderlineItem> objects with the
ordered items.
=cut
sub items {
my $self = shift;
return @{ $self->items_ref };
}
=head2 order_date
Return a L<DateTime> object with th purchase date.
=cut
sub order_date {
my ($self) = @_;
return $self->_get_dt($self->order->{PurchaseDate});
}
sub _get_dt {
my ($self, $date) = @_;
return DateTime::Format::ISO8601->parse_datetime($date);
}
=head2 shipping_cost
The total shipping cost, built summing up the shipping cost of each
item.
=cut
sub shipping_cost {
my $self = shift;
my @items = $self->items;
my $shipping = 0;
foreach my $i (@items) {
$shipping += $i->shipping;
}
return sprintf('%.2f', $shipping);
}
=head2 subtotal
The subtotal of the order, built summing up the subtotal of each
orderline's item.
=cut
sub subtotal {
my $self = shift;
my @items = $self->items;
my $total = 0;
foreach my $i (@items) {
$total += $i->subtotal;
}
return sprintf('%.2f', $total);
}
=head2 number_of_items
Total number of items ordered.
=cut
sub number_of_items {
my $self = shift;
my @items = $self->items;
my $total = 0;
foreach my $i (@items) {
$total += $i->quantity;
}
return $total;
}
=head2 total_cost;
Return OrderTotal.Amount. Throws an exception if it doesn't match
shipping_cost + subtotal.
=cut
( run in 0.823 second using v1.01-cache-2.11-cpan-39bf76dae61 )