has
struct
=> (
is
=>
'ro'
,
isa
=> HashRef,
required
=> 1);
has
order_number
=> (
is
=>
'rw'
);
has
shipping_address
=> (
is
=>
'lazy'
);
has
billing_address
=> (
is
=>
'lazy'
);
sub
_build_shipping_address {
my
$self
=
shift
;
if
(
my
$data
=
$self
->struct->{FulfillmentData}) {
if
(
my
$address
=
$data
->{Address}) {
return
Amazon::MWS::XML::Address->new(
%$address
);
}
}
return
undef
;
}
sub
_build_billing_address {
my
$self
=
shift
;
if
(
my
$data
=
$self
->struct->{BillingData}) {
if
(
my
$address
=
$data
->{Address}) {
return
Amazon::MWS::XML::Address->new(
%$address
);
}
}
return
undef
;
}
has
_items_ref
=> (
is
=>
'lazy'
);
sub
_build__items_ref {
my
$self
=
shift
;
my
@items
;
if
(
my
$list
=
$self
->struct->{Item}) {
foreach
my
$item
(
@$list
) {
my
$obj
= Amazon::MWS::XML::Response::OrderReport::Item->new(
%$item
);
push
@items
,
$obj
;
}
}
return
\
@items
;
}
sub
amazon_order_number {
return
shift
->struct->{AmazonOrderID};
}
sub
email {
my
$self
=
shift
;
if
(
my
$billing
=
$self
->struct->{BillingData}) {
if
(
exists
$billing
->{BuyerEmailAddress}) {
return
$billing
->{BuyerEmailAddress};
}
};
return
;
}
sub
order_date {
my
$self
=
shift
;
my
$struct
=
$self
->struct;
my
$date
=
$struct
->{OrderPostedDate} ||
$struct
->{OrderDate};
return
DateTime::Format::ISO8601->parse_datetime(
$date
);
}
sub
items {
return
@{
shift
->_items_ref };
}
sub
as_ack_order_hashref {
my
$self
=
shift
;
my
@items
;
foreach
my
$item
(
$self
->items) {
push
@items
,
$item
->as_ack_orderline_item_hashref;
}
return
{
AmazonOrderID
=>
$self
->amazon_order_number,
MerchantOrderID
=>
$self
->order_number,
Item
=> \
@items
,
};
}
sub
can_be_imported {
return
1;
}
sub
order_status {
return
'Report'
;
}
has
currency
=> (
is
=>
'lazy'
,
isa
=> Str);
sub
_build_currency {
my
$self
=
shift
;
my
$currency
;
foreach
my
$item
(
$self
->items) {
my
$item_currency
=
$item
->currency;
die
"Missign currency on item?"
. Dumper(
$item
)
unless
$item_currency
;
if
(
$currency
) {
if
(
$currency
ne
$item_currency
) {
die
"Currency mismatch in the same order, should happen"
. Dumper(
$self
);
}
}
else
{
$currency
=
$item_currency
;
}
}
return
$currency
;
}
has
shipping_cost
=> (
is
=>
'lazy'
,
isa
=> Str);
sub
_build_shipping_cost {
return
shift
->_calc_sum_item_method(
'shipping'
);
}
has
subtotal
=> (
is
=>
'lazy'
,
isa
=> Str);
sub
_build_subtotal {
return
shift
->_calc_sum_item_method(
'subtotal'
);
}
has
total_cost
=> (
is
=>
'lazy'
,
isa
=> Str);
sub
_build_total_cost {
return
shift
->_calc_sum_item_method(
'total_price'
);
}
has
total_amazon_fee
=> (
is
=>
'lazy'
,
isa
=> Str);
sub
_build_total_amazon_fee {
return
shift
->_calc_sum_item_method(
'amazon_fee'
);
}
sub
_calc_sum_item_method {
my
(
$self
,
$method
) =
@_
;
die
unless
$method
;
my
$cost
= 0;
foreach
my
$item
(
$self
->items) {
$cost
+=
$item
->
$method
;
}
return
sprintf
(
'%.2f'
,
$cost
);
}
has
number_of_items
=> (
is
=>
'lazy'
,
isa
=> Int);
sub
_build_number_of_items {
my
$self
=
shift
;
my
$count
= 0;
foreach
my
$item
(
$self
->items) {
$count
+=
$item
->quantity;
}
return
$count
;
}
1;