Schema/Result/Zone.pm

filter: Statementstmt 94.0% 143 / 152 Branchbran 89.5% 77 / 86 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 15 / 15 Podpod 100% 9 / 9 totaltotal 93.1% 244 / 262 CC 48 SCAR 18.5
CC
48
Cov
92%
_get_state_obj
9.5
new
8.9
add_states
7.0
CRAP 6.4
« Schema/Result/Tax.pm ↑ Summary Schema/Result/Message.pm »
1 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Interchange6::Schema::Result::Zone;
4  
5 =head1 NAME
6  
7 Interchange6::Schema::Result::Zone
8  
9 =cut
10  
11 2 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 use Interchange6::Schema::Candy -components =>
15 2 ▶ [qw(InflateColumn::DateTime TimeStamp)];
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 =head1 DESCRIPTION
18  
19 In the context of zones the term 'state' refers to state, province or other principal subdivision of a country as defined in L<ISO 3116-2|http://en.wikipedia.org/wiki/ISO_3166-2>. Countries to be added to a zone must already exist in L<Interchange6::Schema::Result::Country> and states in L<Interchange6::Schema::Result::State>.
20  
21 Zones can contain any of the following:
22  
23 =over 4
24  
25 =item * No countries and no states
26  
27 An empty zone must be created before countries/states are added but otherwise is probably not useful.
28  
29 =item * Multiple countries
30  
31 For example to create a trading group like the European Union.
32  
33 =item * A single country
34  
35  
36 =item * A single country with a single state
37  
38 For example Quebec in Canada which has GST + QST
39  
40 =item * A single country with multiple states
41  
42 For example a group containing all Canadian provinces that charge only GST.
43  
44 =back
45  
46 The following combinations are NOT allowed:
47  
48 =over 4
49  
50 =item * Multiple countries with one or more states
51  
52 =item * One or more states with no country
53  
54 =back
55  
56 Countries and states should be added to and removed from the zone using these methods which are described further below:
57  
58 =over 4
59  
60 =item * add_countries
61  
62 =item * remove_countries
63  
64 =item * add_states
65  
66 =item * remove_states
67  
68 =back
69  
70 B<NOTE:> avoid using other methods from L<DBIx::Class::Relationship::Base> since you may inadvertently end up with an invalid zone.
71  
72 =head1 ACCESSORS
73  
74 =head2 zones_id
75  
76 Primary Key.
77  
78 =cut
79  
80 2 primary_column zones_id => {
81 data_type => "integer",
82 is_auto_increment => 1,
83 sequence => "zones_id_seq"
84 };
85  
86 =head2 zone
87  
88 For example for storing the UPS/USPS zone code or a simple name for the zone.
89  
90 Unique constraint.
91  
92 =cut
93  
94 2 unique_column zone => { data_type => "varchar", size => 255 };
95  
96 =head2 created
97  
98 Date and time when this record was created returned as L<DateTime> object.
99 Value is auto-set on insert.
100  
101 =cut
102  
103 2 column created => { data_type => "datetime", set_on_create => 1 };
104  
105 =head2 last_modified
106  
107 Date and time when this record was last modified returned as L<DateTime> object.
108 Value is auto-set on insert and update.
109  
110 =cut
111  
112 2 column last_modified => {
113 data_type => "datetime",
114 set_on_create => 1,
115 set_on_update => 1,
116 };
117  
118 =head1 RELATIONS
119  
120 =head2 zone_countries
121  
122 Type: has_many
123  
124 Related object: L<Interchange6::Schema::Result::ZoneCountry>
125  
126 =cut
127  
128 2 has_many
129 zone_countries => "Interchange6::Schema::Result::ZoneCountry",
130 "zones_id",
131 { cascade_copy => 0, cascade_delete => 0 };
132  
133 =head2 countries
134  
135 Type: many_to_many
136  
137 Accessor to related country results ordered by name.
138  
139 =cut
140  
141 2 many_to_many
142 countries => "zone_countries",
143 "country",
144 { order_by => 'country.name' };
145  
146 =head2 zone_states
147  
148 Type: has_many
149  
150 Related object: L<Interchange6::Schema::Result::ZoneState>
151  
152 =cut
153  
154 2 has_many
155 zone_states => "Interchange6::Schema::Result::ZoneState",
156 "zones_id",
157 { cascade_copy => 0, cascade_delete => 0 };
158  
159 =head2 states
160  
161 Type: many_to_many
162  
163 Accessor to related state results ordered by name.
164  
165 =cut
166  
167 2 many_to_many
168 states => "zone_states",
169 "state", { order_by => 'state.name' };
170  
171 =head2 shipment_destinations
172  
173 C<has_many> relationship with
174 L<Interchange6::Schema::Result::ShipmentDestination>
175  
176 =cut
177  
178 2 has_many
179 shipment_destinations => "Interchange6::Schema::Result::ShipmentDestination",
180 "zones_id";
181  
182 =head2 shipment_methods
183  
184 C<many_to_many> relationship to shipment_method. Currently it ignores
185 the C<active> field in shipment_destinations.
186  
187 =cut
188  
189 2 many_to_many shipment_methods => "shipment_destinations", "shipment_method";
190  
191 =head1 METHODS
192  
193 =head2 new
194  
195 Overloaded method. We allow a form of multi-create here so you can do something like:
196  
197 $schema->resultset('Zone')->create({
198 zone => 'some states of the USA',
199 countries => [ 'US' ],
200 states => [ 'CA', 'PA' ],
201 });
202  
203 If there is only a single country or state the value can be a scalar instead of a hashref.
204  
205 =cut
206  
207 sub new {
208 957 ▶ my ( $class, $attrs ) = @_;
Subroutine new: called CC=8 SCAR=21.9
Pod documented
209  
210 957 my ( $countries, $states, $new );
211  
212 957 ▶ if ( $attrs->{countries} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($attrs->{'countries'}) { } 1 956 957
elsif ($attrs->{'states'}) { } 0 956 956
213 1 ▶ if ( ref( $attrs->{countries} ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attrs->{'countries'} eq 'ARRAY') { } 0 1 1
214 0 push @$countries, @{ $attrs->{countries} };
215 }
216 else {
217 1 push @$countries, $attrs->{countries};
218 }
219 1 delete $attrs->{countries};
220  
221 1 ▶ if ( $attrs->{states} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attrs->{"states"}) 1 0 1
222 1 ▶ if ( ref( $attrs->{states} ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attrs->{'states'} eq 'ARRAY') { } 1 0 1
223 1 push @$states, @{ $attrs->{states} };
224 }
225 else {
226 0 push @$states, $attrs->{states};
227 }
228 1 delete $attrs->{states};
229 }
230 }
231 elsif ( $attrs->{states} ) {
232 0 die "Cannot create Zone with states but without countries";
233 }
234  
235 957 $new = $class->next::method($attrs);
236 957 ▶ $new->add_countries($countries) if $countries;
Branch: 1 branch
BranchTrueFalseTotal
if $countries 1 956 957
237 957 ▶ $new->add_states($states) if $states;
Branch: 1 branch
BranchTrueFalseTotal
if $states 1 956 957
238  
239 957 return $new;
240 }
241  
242 =head2 add_countries
243  
244 Argument is one of:
245  
246 =over 4
247  
248 =item an L<Interchange6::Schema::Result::Country> object
249  
250 =item a country ISO code
251  
252 =item an arrayref of the above (can include a mixture of both)
253  
254 =back
255  
256 Returns the zone object on success.
257  
258 =cut
259  
260 # add/remove_countries can be passed all sorts of junk but we need Country obj
261  
262 sub _get_country_obj {
263  
264 29 ▶ my ( $self, $country ) = @_;
Subroutine _get_country_obj: called CC=6 SCAR=17.9
265  
266 29 ▶ if ( !defined $country ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $country) { } 2 27 29
elsif (builtin::blessed($country)) { } 16 11 27
elsif ($country =~ /^[a-z]{2}$/i) { } 7 4 11
267 2 $self->throw_exception("Country must be defined");
268 }
269 elsif ( blessed($country) ) {
270  
271 16 my $class = ref($country);
272  
273 16 ▶ $self->throw_exception("Country cannot be a $class")
Branch: 1 branch
BranchTrueFalseTotal
unless $country->isa("Interchange6::Schema::Result::Country") 2 14 16
274 unless $country->isa('Interchange6::Schema::Result::Country');
275  
276 }
277 elsif ( $country =~ m/^[a-z]{2}$/i ) {
278  
279 7 my $result = $self->result_source->schema->resultset("Country")
280 ->find( { country_iso_code => uc($country) } );
281  
282 7 ▶ $self->throw_exception("No country found for code: $country")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result 2 5 7
283 unless defined $result;
284  
285 5 $country = $result;
286 }
287 else {
288 4 $self->throw_exception("Bad country: $country");
289 }
290  
291 19 return $country;
292 }
293  
294 sub add_countries {
295 19 ▶ my ( $self, $arg ) = ( shift, shift );
Subroutine add_countries: called CC=5 SCAR=16.1
Pod documented
296  
297 19 my $schema = $self->result_source->schema;
298  
299 19 ▶ if ( $self->state_count > 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->state_count > 0) { } 1 18 19
elsif (ref $arg ne 'ARRAY') { } 13 5 18
300 1 $self->throw_exception(
301 "Cannot add countries to zone containing states");
302 }
303 elsif ( ref($arg) ne "ARRAY" ) {
304  
305 # we need an arrayref
306 13 $arg = [$arg];
307 }
308  
309 # use a transaction when adding countries so that all succeed or all fail
310  
311 18 my $guard = $schema->txn_scope_guard;
312  
313 18 foreach my $country (@$arg) {
314  
315 18 $country = $self->_get_country_obj($country);
316  
317 11 ▶ if ( $self->has_country($country) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_country($country)) 1 10 11
318 1 $self->throw_exception(
319 "Zone already includes country: " . $country->name );
320 }
321  
322 10 $self->add_to_countries($country);
323 }
324  
325 10 $guard->commit;
326  
327 10 return $self;
328 }
329  
330 =head2 has_country
331  
332 Argument can be Interchange6::Schema::Result::Country, country name or iso code. Returns 1 if zone includes that country else 0;
333  
334 =cut
335  
336 sub has_country {
337 29 ▶ my ( $self, $country ) = ( shift, shift );
Subroutine has_country: called CC=7 SCAR=19.5
Pod documented
338 29 my $rset;
339  
340 # first try Country object
341  
342 29 ▶ if ( blessed($country) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($country)) { } 21 8 29
343 21 ▶ if ( $country->isa('Interchange6::Schema::Result::Country') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($country->isa('Interchange6::Schema::Result::Country')) { } 20 1 21
344  
345 20 $rset = $self->countries->search(
346 { "country.country_iso_code" => $country->country_iso_code, } );
347 20 ▶ return 1 if $rset->count == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $rset->count == 1 9 11 20
348  
349 }
350 else {
351 1 return 0;
352 }
353 }
354 else {
355  
356 # maybe an ISO code?
357  
358 8 ▶ if ( $country =~ /^[a-z]{2}$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($country =~ /^[a-z]{2}$/i) { } 6 2 8
359  
360 6 $rset = $self->countries->search(
361 { "country.country_iso_code" => uc($country) } );
362  
363 6 ▶ return 1 if $rset->count == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $rset->count == 1 4 2 6
364 }
365 else {
366  
367 # finally try country name
368  
369 2 $rset = $self->countries->search( { "country.name" => $country } );
370  
371 2 ▶ return 1 if $rset->count == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $rset->count == 1 1 1 2
372 }
373 }
374  
375 # failed to find the country
376 14 return 0;
377 }
378  
379 =head2 country_count
380  
381 Takes no args. Returns the number of countries in the zone.
382  
383 =cut
384  
385 sub country_count {
386 44 ▶ my $self = shift;
Subroutine country_count: called CC=1 SCAR=0.0
Pod documented
387 44 return $self->countries->count;
388 }
389  
390 =head2 remove_countries
391  
392 Argument is either a L<Interchange6::Schema::Result::Country> object or an arrayref of the same.
393  
394 Throws an exception on failure.
395  
396 =cut
397  
398 sub remove_countries {
399 11 ▶ my ( $self, $arg ) = ( shift, shift );
Subroutine remove_countries: called CC=5 SCAR=16.1
Pod documented
400  
401 11 my $schema = $self->result_source->schema;
402  
403 11 ▶ if ( $self->state_count > 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->state_count > 0) { } 1 10 11
elsif (ref $arg ne 'ARRAY') { } 5 5 10
404  
405 1 $self->throw_exception("States must be removed before countries");
406  
407 }
408 elsif ( ref($arg) ne "ARRAY" ) {
409  
410 # convert to arrayref
411 5 $arg = [$arg];
412 }
413  
414 # use a transaction when removing countries so that all succeed or all fail
415  
416 10 my $guard = $schema->txn_scope_guard;
417  
418 10 foreach my $country (@$arg) {
419  
420 11 $country = $self->_get_country_obj($country);
421  
422 8 ▶ unless ( $self->has_country($country) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->has_country($country)) 1 7 8
423 1 $self->throw_exception(
424 "Country does not exist in zone: " . $country->name );
425 }
426  
427 7 $self->remove_from_countries($country);
428 }
429  
430 6 $guard->commit;
431  
432 6 return $self;
433 }
434  
435 =head2 add_states
436  
437 Argument is one of:
438  
439 =over 4
440  
441 =item an L<Interchange6::Schema::Result::State> object
442  
443 =item a state ISO code
444  
445 =item an arrayref of the above (can include a mixture of both)
446  
447 =back
448  
449 Returns the zone object on success.
450  
451 =cut
452  
453 # add/remove_states can be passed all sorts of junk but we need State obj
454  
455 sub _get_state_obj {
456  
457 25 ▶ my ( $self, $state ) = @_;
Subroutine _get_state_obj: called CC=8 SCAR=22.5
458  
459 25 ▶ if ( !defined $state ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $state) { } 0 25 25
elsif (builtin::blessed($state)) { } 21 4 25
elsif ($state =~ /^[a-z]{2}$/i) { } 2 2 4
460 0 $self->throw_exception("State must be defined");
461 }
462 elsif ( blessed($state) ) {
463  
464 21 my $class = ref($state);
465  
466 21 ▶ $self->throw_exception("State cannot be a $class")
Branch: 1 branch
BranchTrueFalseTotal
unless $state->isa("Interchange6::Schema::Result::State") 3 18 21
467 unless $state->isa('Interchange6::Schema::Result::State');
468  
469 }
470 elsif ( $state =~ m/^[a-z]{2}$/i ) {
471  
472 2 ▶ if ( $self->country_count == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->country_count == 1) { } 2 0 2
elsif ($self->country_count == 0) { } 0 0 0
473  
474 2 my $country = $self->countries->first;
475  
476 2 my $result =
477 $self->result_source->schema->resultset("State")->find(
478 {
479 country_iso_code => $country->country_iso_code,
480 state_iso_code => uc($state)
481 }
482 );
483  
484 2 ▶ $self->throw_exception("No state found for code: $state")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result 0 2 2
485 unless defined $result;
486  
487 2 $state = $result;
488  
489 }
490 elsif ( $self->country_count == 0 ) {
491  
492 0 $self->throw_exception(
493 "Cannot resolve state_iso_code for zone with no country");
494 }
495 else {
496  
497 0 $self->throw_exception(
498 "Cannot resolve state_iso_code for zone with > 1 country");
499 }
500 }
501 else {
502 2 $self->throw_exception("Bad state: $state");
503 }
504  
505 20 return $state;
506 }
507  
508 sub add_states {
509 12 ▶ my ( $self, $arg ) = ( shift, shift );
Subroutine add_states: called CC=7 SCAR=19.5
Pod documented
510  
511 12 my $schema = $self->result_source->schema;
512  
513 12 ▶ if ( $self->country_count > 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->country_count > 1) { } 1 11 12
elsif (ref $arg ne 'ARRAY') { } 6 5 11
514  
515 1 $self->throw_exception(
516 "Cannot add state to zone with multiple countries");
517 }
518 elsif ( ref($arg) ne "ARRAY" ) {
519  
520 # we need an arayref
521 6 $arg = [$arg];
522 }
523  
524 # use a transaction when adding states so that all succeed or all fail
525  
526 11 my $guard = $schema->txn_scope_guard;
527  
528 11 foreach my $state (@$arg) {
529  
530 21 $state = $self->_get_state_obj($state);
531  
532 18 ▶ if ( $self->country_count == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->country_count == 0) { } 1 17 18
533  
534 # add the country first
535  
536 1 $self->add_countries( $state->country );
537 }
538 else {
539  
540 # make sure state is in the existing country
541  
542 17 my $country =
543 $self->countries->search( {}, { rows => 1 } )->single;
544  
545 17 ▶ unless ( $country->country_iso_code eq
Branch: 1 branch
BranchTrueFalseTotal
unless ($country->country_iso_code eq $state->country->country_iso_code) 2 15 17
546 $state->country->country_iso_code )
547 {
548 2 $self->throw_exception( "State "
549 . $state->name
550 . " is not in country "
551 . $country->name );
552 0 next;
553 }
554 }
555  
556 16 ▶ if ( $self->has_state($state) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_state($state)) 1 15 16
557 1 $self->throw_exception(
558 "Zone already includes state: " . $state->name );
559 0 next;
560 }
561  
562 # try to add the state
563  
564 15 $self->add_to_states($state);
565 }
566  
567 5 $guard->commit;
568  
569 5 return $self;
570 }
571  
572 =head2 has_state
573  
574 Argument can be Interchange6::Schema::Result::State, state name or iso code. Returns 1 if zone includes that state else 0;
575  
576 =cut
577  
578 sub has_state {
579 25 ▶ my ( $self, $state ) = ( shift, shift );
Subroutine has_state: called CC=7 SCAR=19.5
Pod documented
580 25 my $rset;
581  
582 # first try State object
583  
584 25 ▶ if ( blessed($state) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($state)) { } 18 7 25
585 18 ▶ if ( $state->isa('Interchange6::Schema::Result::State') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->isa('Interchange6::Schema::Result::State')) { } 17 1 18
586  
587 17 $rset = $self->states->search(
588 {
589 "state.country_iso_code" => $state->country_iso_code,
590 "state.state_iso_code" => $state->state_iso_code
591 }
592 );
593 17 ▶ return 1 if $rset->count == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $rset->count == 1 2 15 17
594  
595 }
596 else {
597 1 return 0;
598 }
599 }
600 else {
601  
602 # maybe an ISO code?
603  
604 7 ▶ if ( $state =~ /^[a-z]{2}$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state =~ /^[a-z]{2}$/i) { } 5 2 7
605  
606 5 $rset = $self->states->search( { state_iso_code => uc($state) } );
607  
608 5 ▶ return 1 if $rset->count == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $rset->count == 1 3 2 5
609 }
610 else {
611  
612 # finally try state name
613  
614 2 $rset = $self->states->search( { name => $state } );
615  
616 2 ▶ return 1 if $rset->count == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $rset->count == 1 1 1 2
617  
618 }
619 }
620  
621 # failed to find the state
622 18 return 0;
623 }
624  
625 =head2 state_count
626  
627 Takes no args. Returns the number of states in the zone.
628  
629 =cut
630  
631 sub state_count {
632 40 ▶ my $self = shift;
Subroutine state_count: called CC=1 SCAR=0.0
Pod documented
633 40 return $self->states->search( {} )->count;
634 }
635  
636 =head2 remove_states
637  
638 Argument is either a L<Interchange6::Schema::Result::State> object or an arrayref of the same.
639  
640 Returns the Zone object or undef on failure. Errors are available via errors method inherited from L<Interchange6::Schema::Role::Errors>.
641  
642 =cut
643  
644 sub remove_states {
645 4 ▶ my ( $self, $arg ) = ( shift, shift );
Subroutine remove_states: called CC=3 SCAR=11.0
Pod documented
646  
647 4 my $schema = $self->result_source->schema;
648  
649 4 ▶ if ( ref($arg) ne "ARRAY" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg ne "ARRAY") 3 1 4
650  
651 # we need an arrayref
652 3 $arg = [$arg];
653 }
654  
655 # use a transaction when removing states so that all succeed or all fail
656  
657 4 my $guard = $schema->txn_scope_guard;
658  
659 4 foreach my $state (@$arg) {
660  
661 4 $state = $self->_get_state_obj($state);
662  
663 2 $self->remove_from_states($state);
664 }
665  
666 2 $guard->commit;
667  
668 2 return $self;
669 }
670  
671 2 1;
« Schema/Result/Tax.pm ↑ Summary Schema/Result/Message.pm »