[interchange-cvs] interchange - heins modified lib/Vend/Order.pm

interchange-core@icdevgroup.org interchange-core@icdevgroup.org
Thu Apr 10 22:36:01 2003


User:      heins
Date:      2003-04-11 02:35:39 GMT
Modified:  lib/Vend Order.pm
Log:
* Add multizip routine. Works for the US and Canada as before with
  zip=postcode, but ignores any countries not set in MV_ZIP_REQUIRED
  and doesn't require a zip code.

  In other words:

  	[set checkprofile]
	   country=required
	   zip=multizip
	[/set]

  With

  	Variable MV_ZIP_REQUIRED        AL DZ

  the only countries that will require a zip code besides the US and
  Canada are Algeria and Albania.

  When a zip code is required, the zip just needs to be 4 characters
  or more. (Is this reasonable?)

  To set a custom state routine, do:

  $Vend::Order::zip_routine{AL} = sub { $_[0] =~ /^\s*\d\d\d\d^\s*$/; }

  Called with $sub->($state_value_to_check);

  To override the default error message, do

  $Vend::Order::zip_error{AL} = "'%s' not an Albanian zip code!";

* Change multistate to match logic of multizip. Works for the US and
  Canada as before with state=state_province, but ignores any countries
  not set in MV_STATE_REQUIRED and doesn't require a state.

  In other words:

  	[set checkprofile]
	   country=required
	   state=multistate
	[/set]

  With

  	Variable MV_ZIP_REQUIRED        AL DZ

  the only countries that will require a state besides the US and
  Canada are Algeria and Albania.

* I am open to authoritative defaults for other countries.

* It might make sense to add a "datastate" and "datazip" to the
  code/OrderCheck directory, ones that look for state info in
  the database.

Revision  Changes    Path
2.52      +53 -11    interchange/lib/Vend/Order.pm


rev 2.52, prev_rev 2.51
Index: Order.pm
===================================================================
RCS file: /var/cvs/interchange/lib/Vend/Order.pm,v
retrieving revision 2.51
retrieving revision 2.52
diff -u -r2.51 -r2.52
--- Order.pm	11 Apr 2003 00:00:52 -0000	2.51
+++ Order.pm	11 Apr 2003 02:35:38 -0000	2.52
@@ -1,6 +1,6 @@
 # Vend::Order - Interchange order routing routines
 #
-# $Id: Order.pm,v 2.51 2003/04/11 00:00:52 mheins Exp $
+# $Id: Order.pm,v 2.52 2003/04/11 02:35:38 mheins Exp $
 #
 # Copyright (C) 1996-2001 Red Hat, Inc. <interchange@redhat.com>
 #
@@ -28,7 +28,7 @@
 package Vend::Order;
 require Exporter;
 
-$VERSION = substr(q$Revision: 2.51 $, 10);
+$VERSION = substr(q$Revision: 2.52 $, 10);
 
 @ISA = qw(Exporter);
 
@@ -1167,7 +1167,7 @@
 	return $status;
 }
 
-use vars qw/ %state_template %state_error /;
+use vars qw/ %state_template %state_error %zip_routine %zip_error /;
 $state_error{US} = "'%s' not a two-letter state code";
 $state_error{CA} = "'%s' not a two-letter province code";
 $state_template{US} = <<EOF;
@@ -1180,6 +1180,17 @@
 | AB BC MB NB NF NS NT ON PE QC SK YT YK |
 EOF
 
+$zip_error{US} = "'%s' not a US zip code";
+$zip_routine{US} = sub { $_[0] =~ /^\s*\d\d\d\d\d(?:-?\d\d\d\d)?$/ };
+
+$zip_error{CA} = "'%s' not a Canadian postal code";
+$zip_routine{CA} = sub {
+	my $val = shift;
+	return undef unless defined $val;
+	$val =~ s/[_\W]+//g;
+	$val =~ /^[ABCEGHJKLMNPRSTVXYabceghjklmnprstvxy]\d[A-Za-z]\d[A-Za-z]\d$/;
+};
+
 sub _state_province {
 	my($ref,$var,$val) = @_;
 	my $error;
@@ -1235,23 +1246,54 @@
 	}
 }
 
-sub _multistate {
-	my($ref,$var,$val) = @_;
-
-	my $error;
-
+sub _get_cval {
+	my ($ref, $var) = @_;
 	my $cfield = $::Variable->{MV_COUNTRY_FIELD} || 'country';
 	my $cval = $ref->{$cfield} || $::Values->{$cfield};
 
 	if($var =~ /^b_/ and $ref->{"b_$cfield"} || $::Values->{"b_$cfield"}) {
 		$cval = $ref->{"b_$cfield"} || $::Values->{"b_$cfield"};
 	}
+	return $cval;
+}
 
-	if (length($val) < 2) {
-		$error = 1;
+sub _multizip {
+	my($ref,$var,$val) = @_;
+
+	$val =~ s/^\s+//;
+	my $error;
+	my $cval = _get_cval($ref, $var);
+
+	if (my $sub = $zip_routine{$cval}) {
+		$sub->($val) or $error = 1;
 	}
-	elsif(my $sval = $state_template{$cval}) {
+	elsif($::Variable->{MV_ZIP_REQUIRED}) {
+	    " $::Variable->{MV_ZIP_REQUIRED} " =~ /\s$cval\s/
+			and
+		length($val) < 4 and $error = 1;
+	}
+
+	if($error) {
+		my $tpl = $zip_error{$cval} || "'%s' not a valid post code for country '%s'";
+		my $msg = errmsg( $tpl, $val, $cval );
+		return (undef, $var, $msg );
+	}
+	return (1, $var, '');
+}
+
+sub _multistate {
+	my($ref,$var,$val) = @_;
+
+	my $error;
+	my $cval = _get_cval($ref, $var);
+
+	if(my $sval = $state_template{$cval}) {
 		$error = 1 unless $sval =~ /\s$val\s/;
+	}
+	elsif($::Variable->{MV_STATE_REQUIRED}) {
+	    " $::Variable->{MV_STATE_REQUIRED} " =~ /\s$cval\s/
+			and
+		length($val) < 2 and $error = 1;
 	}
 
 	if($error) {