Hide Show 6 lines of Pod
[
qw(InflateColumn::DateTime TimeStamp)
];
Hide Show 63 lines of Pod
primary_column
zones_id
=> {
data_type
=>
"integer"
,
is_auto_increment
=> 1,
sequence
=>
"zones_id_seq"
};
Hide Show 8 lines of Pod
unique_column
zone
=> {
data_type
=>
"varchar"
,
size
=> 255 };
Hide Show 7 lines of Pod
column
created
=> {
data_type
=>
"datetime"
,
set_on_create
=> 1 };
Hide Show 7 lines of Pod
column
last_modified
=> {
data_type
=>
"datetime"
,
set_on_create
=> 1,
set_on_update
=> 1,
};
Hide Show 10 lines of Pod
has_many
zone_countries
=>
"Interchange6::Schema::Result::ZoneCountry"
,
"zones_id"
,
{
cascade_copy
=> 0,
cascade_delete
=> 0 };
Hide Show 8 lines of Pod
many_to_many
countries
=>
"zone_countries"
,
"country"
,
{
order_by
=>
'country.name'
};
Hide Show 8 lines of Pod
has_many
zone_states
=>
"Interchange6::Schema::Result::ZoneState"
,
"zones_id"
,
{
cascade_copy
=> 0,
cascade_delete
=> 0 };
Hide Show 8 lines of Pod
many_to_many
states
=>
"zone_states"
,
"state"
, {
order_by
=>
'state.name'
};
Hide Show 7 lines of Pod
has_many
shipment_destinations
=>
"Interchange6::Schema::Result::ShipmentDestination"
,
"zones_id"
;
Hide Show 7 lines of Pod
many_to_many
shipment_methods
=>
"shipment_destinations"
,
"shipment_method"
;
Hide Show 16 lines of Pod
sub
new {
my
(
$class
,
$attrs
) =
@_
;
my
(
$countries
,
$states
,
$new
);
if
(
$attrs
->{countries} ) {
if
(
ref
(
$attrs
->{countries} ) eq
'ARRAY'
) {
push
@$countries
, @{
$attrs
->{countries} };
}
else
{
push
@$countries
,
$attrs
->{countries};
}
delete
$attrs
->{countries};
if
(
$attrs
->{states} ) {
if
(
ref
(
$attrs
->{states} ) eq
'ARRAY'
) {
push
@$states
, @{
$attrs
->{states} };
}
else
{
push
@$states
,
$attrs
->{states};
}
delete
$attrs
->{states};
}
}
elsif
(
$attrs
->{states} ) {
die
"Cannot create Zone with states but without countries"
;
}
$new
=
$class
->
next
::method(
$attrs
);
$new
->add_countries(
$countries
)
if
$countries
;
$new
->add_states(
$states
)
if
$states
;
return
$new
;
}
Hide Show 18 lines of Pod
sub
_get_country_obj {
my
(
$self
,
$country
) =
@_
;
if
( !
defined
$country
) {
$self
->throw_exception(
"Country must be defined"
);
}
elsif
( blessed(
$country
) ) {
my
$class
=
ref
(
$country
);
$self
->throw_exception(
"Country cannot be a $class"
)
unless
$country
->isa(
'Interchange6::Schema::Result::Country'
);
}
elsif
(
$country
=~ m/^[a-z]{2}$/i ) {
my
$result
=
$self
->result_source->schema->resultset(
"Country"
)
->find( {
country_iso_code
=>
uc
(
$country
) } );
$self
->throw_exception(
"No country found for code: $country"
)
unless
defined
$result
;
$country
=
$result
;
}
else
{
$self
->throw_exception(
"Bad country: $country"
);
}
return
$country
;
}
sub
add_countries {
my
(
$self
,
$arg
) = (
shift
,
shift
);
my
$schema
=
$self
->result_source->schema;
if
(
$self
->state_count > 0 ) {
$self
->throw_exception(
"Cannot add countries to zone containing states"
);
}
elsif
(
ref
(
$arg
) ne
"ARRAY"
) {
$arg
= [
$arg
];
}
my
$guard
=
$schema
->txn_scope_guard;
foreach
my
$country
(
@$arg
) {
$country
=
$self
->_get_country_obj(
$country
);
if
(
$self
->has_country(
$country
) ) {
$self
->throw_exception(
"Zone already includes country: "
.
$country
->name );
}
$self
->add_to_countries(
$country
);
}
$guard
->commit;
return
$self
;
}
Hide Show 6 lines of Pod
sub
has_country {
my
(
$self
,
$country
) = (
shift
,
shift
);
my
$rset
;
if
( blessed(
$country
) ) {
if
(
$country
->isa(
'Interchange6::Schema::Result::Country'
) ) {
$rset
=
$self
->countries->search(
{
"country.country_iso_code"
=>
$country
->country_iso_code, } );
return
1
if
$rset
->count == 1;
}
else
{
return
0;
}
}
else
{
if
(
$country
=~ /^[a-z]{2}$/i ) {
$rset
=
$self
->countries->search(
{
"country.country_iso_code"
=>
uc
(
$country
) } );
return
1
if
$rset
->count == 1;
}
else
{
$rset
=
$self
->countries->search( {
"country.name"
=>
$country
} );
return
1
if
$rset
->count == 1;
}
}
return
0;
}
Hide Show 6 lines of Pod
sub
country_count {
my
$self
=
shift
;
return
$self
->countries->count;
}
Hide Show 8 lines of Pod
sub
remove_countries {
my
(
$self
,
$arg
) = (
shift
,
shift
);
my
$schema
=
$self
->result_source->schema;
if
(
$self
->state_count > 0 ) {
$self
->throw_exception(
"States must be removed before countries"
);
}
elsif
(
ref
(
$arg
) ne
"ARRAY"
) {
$arg
= [
$arg
];
}
my
$guard
=
$schema
->txn_scope_guard;
foreach
my
$country
(
@$arg
) {
$country
=
$self
->_get_country_obj(
$country
);
unless
(
$self
->has_country(
$country
) ) {
$self
->throw_exception(
"Country does not exist in zone: "
.
$country
->name );
}
$self
->remove_from_countries(
$country
);
}
$guard
->commit;
return
$self
;
}
Hide Show 18 lines of Pod
sub
_get_state_obj {
my
(
$self
,
$state
) =
@_
;
if
( !
defined
$state
) {
$self
->throw_exception(
"State must be defined"
);
}
elsif
( blessed(
$state
) ) {
my
$class
=
ref
(
$state
);
$self
->throw_exception(
"State cannot be a $class"
)
unless
$state
->isa(
'Interchange6::Schema::Result::State'
);
}
elsif
(
$state
=~ m/^[a-z]{2}$/i ) {
if
(
$self
->country_count == 1 ) {
my
$result
=
$self
->result_source->schema->resultset(
"State"
)->single(
{
country_iso_code
=> {
-in
=>
$self
->countries->get_column(
'country_iso_code'
)
->as_query
},
state_iso_code
=>
uc
(
$state
),
}
);
$self
->throw_exception(
"No state found for code: $state"
)
unless
defined
$result
;
$state
=
$result
;
}
else
{
if
(
$self
->country_count == 0 ) {
$self
->throw_exception(
"Cannot resolve state_iso_code for zone with no country"
);
}
else
{
$self
->throw_exception(
"Cannot resolve state_iso_code for zone with > 1 country"
);
}
}
}
else
{
$self
->throw_exception(
"Bad state: $state"
);
}
return
$state
;
}
sub
add_states {
my
(
$self
,
$arg
) = (
shift
,
shift
);
my
$schema
=
$self
->result_source->schema;
if
(
$self
->country_count > 1 ) {
$self
->throw_exception(
"Cannot add state to zone with multiple countries"
);
}
elsif
(
ref
(
$arg
) ne
"ARRAY"
) {
$arg
= [
$arg
];
}
my
$guard
=
$schema
->txn_scope_guard;
foreach
my
$state
(
@$arg
) {
$state
=
$self
->_get_state_obj(
$state
);
if
(
$self
->country_count == 0 ) {
$self
->add_countries(
$state
->country );
}
else
{
my
$country
=
$self
->countries->single;
unless
(
$country
->country_iso_code eq
$state
->country->country_iso_code )
{
$self
->throw_exception(
"State "
.
$state
->name
.
" is not in country "
.
$country
->name );
}
}
if
(
$self
->has_state(
$state
) ) {
$self
->throw_exception(
"Zone already includes state: "
.
$state
->name );
}
$self
->add_to_states(
$state
);
}
$guard
->commit;
return
$self
;
}
Hide Show 6 lines of Pod
sub
has_state {
my
(
$self
,
$state
) = (
shift
,
shift
);
my
$rset
;
if
( blessed(
$state
) ) {
if
(
$state
->isa(
'Interchange6::Schema::Result::State'
) ) {
$rset
=
$self
->states->search(
{
"state.country_iso_code"
=>
$state
->country_iso_code,
"state.state_iso_code"
=>
$state
->state_iso_code
}
);
return
1
if
$rset
->count == 1;
}
else
{
return
0;
}
}
else
{
if
(
$state
=~ /^[a-z]{2}$/i ) {
$rset
=
$self
->states->search( {
state_iso_code
=>
uc
(
$state
) } );
return
1
if
$rset
->count == 1;
}
else
{
$rset
=
$self
->states->search( {
name
=>
$state
} );
return
1
if
$rset
->count == 1;
}
}
return
0;
}
Hide Show 6 lines of Pod
sub
state_count {
my
$self
=
shift
;
return
$self
->states->search( {} )->count;
}
Hide Show 8 lines of Pod
sub
remove_states {
my
(
$self
,
$arg
) = (
shift
,
shift
);
my
$schema
=
$self
->result_source->schema;
if
(
ref
(
$arg
) ne
"ARRAY"
) {
$arg
= [
$arg
];
}
my
$guard
=
$schema
->txn_scope_guard;
foreach
my
$state
(
@$arg
) {
$state
=
$self
->_get_state_obj(
$state
);
$self
->remove_from_states(
$state
);
}
$guard
->commit;
return
$self
;
}
1;