[interchange-cvs] interchange - heins modified 15 files

interchange-core@icdevgroup.org interchange-core@icdevgroup.org
Wed May 14 18:30:01 2003


User:      heins
Date:      2003-05-14 22:29:29 GMT
Modified:  .        MANIFEST
Modified:  code/UI_Tag update_order_status.tag
Modified:  code/UserTag ups_query.tag
Modified:  dist/foundation/config additional_fields additional_help
Modified:  dist/foundation/dbconf/mysql inventory.mysql mysql.cfg
Modified:           orderline.mysql transactions.mysql userdb.mysql
Modified:  dist/foundation/products variable.txt
Modified:  lib/Vend Config.pm Ship.pm
Added:     lib/Vend/Ship Postal.pm QueryUPS.pm
Log:
* Allow relocation of all shipping files with:

	Shipping default directory products/ship

* Update MySQL for 3.23/4.x and transactions (Foundation)

* Add Vend::Ship::QueryUPS and Vend::Ship::Postal modules with
  docs.

* Fix bug in UPS query caching.

* Allow update_order_status to actually send email.

Revision  Changes    Path
2.105     +7 -1      interchange/MANIFEST


rev 2.105, prev_rev 2.104
Index: MANIFEST
===================================================================
RCS file: /var/cvs/interchange/MANIFEST,v
retrieving revision 2.104
retrieving revision 2.105
diff -u -r2.104 -r2.105
--- MANIFEST	6 May 2003 11:01:30 -0000	2.104
+++ MANIFEST	14 May 2003 22:29:28 -0000	2.105
@@ -205,6 +205,7 @@
 code/UserTag/ups_query.tag
 code/UserTag/usertrack.tag
 code/UserTag/var.tag
+code/UserTag/weight.tag
 code/Widget/gpg_keys.widget
 code/Widget/imagedir.widget
 code/Widget/imagehelper.widget
@@ -258,7 +259,6 @@
 dist/catalog_before.cfg
 dist/error.log
 dist/etc/.empty
-dist/etc/jobs/i18n/update_locales
 dist/foundation/backup/.empty
 dist/foundation/catalog.cfg
 dist/foundation/config/additional_fields
@@ -1197,6 +1197,9 @@
 eg/te
 eg/usertag/benchmark
 eg/usertag/sleep
+eg/usps/html2tab
+eg/usps/join123local
+eg/usps/makezone
 extensions/ECML/Vend/ECML.pm
 extensions/ECML/ecml.coretag
 extensions/Interchange.pm
@@ -1278,6 +1281,9 @@
 lib/Vend/Session.pm
 lib/Vend/SessionDB.pm
 lib/Vend/SessionFile.pm
+lib/Vend/Ship.pm
+lib/Vend/Ship/Postal.pm
+lib/Vend/Ship/QueryUPS.pm
 lib/Vend/Swish.pm
 lib/Vend/Table/Common.pm
 lib/Vend/Table/DBI.pm



1.7       +16 -1     interchange/code/UI_Tag/update_order_status.tag


rev 1.7, prev_rev 1.6
Index: update_order_status.tag
===================================================================
RCS file: /var/cvs/interchange/code/UI_Tag/update_order_status.tag,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- update_order_status.tag	14 Jan 2003 00:20:17 -0000	1.6
+++ update_order_status.tag	14 May 2003 22:29:29 -0000	1.7
@@ -1,6 +1,6 @@
 UserTag update-order-status Order order_number
 UserTag update-order-status addAttr
-UserTag update-order-status Version $Id: update_order_status.tag,v 1.6 2003/01/14 00:20:17 mheins Exp $
+UserTag update-order-status Version $Id: update_order_status.tag,v 1.7 2003/05/14 22:29:29 mheins Exp $
 UserTag update-order-status Routine <<EOR
 sub {
 	my ($on, $opt) = @_;
@@ -307,6 +307,21 @@
 		$::Scratch->{ship_notice_username} = $user;
 		$::Scratch->{ship_notice_email} = $trec->{email}
 			or delete $::Scratch->{ship_notice_username};
+		if($opt->{send_email}) {
+			my $filename = $opt->{ship_notice_template} || 'etc/ship_notice';
+			my $contents = $Tag->file($filename);
+			if($contents) {
+				$contents = interpolate_html($contents);
+				$contents =~ s/^\s+//;
+				$contents =~ s/\s*$/\n/;
+				$Tag->email_raw({}, $contents);
+			}
+			else {
+				$Tag->warnings(
+						errmsg("No ship_notice_template '%s' found", $filename),
+					);
+			}
+		}
 	}
 	return;
 }



1.5       +1 -2      interchange/code/UserTag/ups_query.tag


rev 1.5, prev_rev 1.4
Index: ups_query.tag
===================================================================
RCS file: /var/cvs/interchange/code/UserTag/ups_query.tag,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- ups_query.tag	14 May 2003 12:41:08 -0000	1.4
+++ ups_query.tag	14 May 2003 22:29:29 -0000	1.5
@@ -77,9 +77,8 @@
 					undef $shipping;
 					$updated = $now;
 				}
-				if($shipping <= 0) {
+				elsif($shipping <= 0) {
 					$error = $shipping;
-					$updated = $now;
 					$shipping = 0;
 				}
 			}



2.5       +4 -0      interchange/dist/foundation/config/additional_fields


rev 2.5, prev_rev 2.4
Index: additional_fields
===================================================================
RCS file: /var/cvs/interchange/dist/foundation/config/additional_fields,v
retrieving revision 2.4
retrieving revision 2.5
diff -u -r2.4 -r2.5
--- additional_fields	25 Jan 2003 21:24:16 -0000	2.4
+++ additional_fields	14 May 2003 22:29:29 -0000	2.5
@@ -38,6 +38,10 @@
 Set to 1 to create '__MVC_SQLDB__' with mysqladmin
 1	0
 
+{MYSQL}MYSQLTRANSACT
+Set to 1 to use transactions for order log updates
+	1
+
 {MYSQLCREATE}MYSQLCREATEOPT
 Extra options for creation
 	-p	-u root -p



2.2       +10 -0     interchange/dist/foundation/config/additional_help


rev 2.2, prev_rev 2.1
Index: additional_help
===================================================================
RCS file: /var/cvs/interchange/dist/foundation/config/additional_help,v
retrieving revision 2.1
retrieving revision 2.2
diff -u -r2.1 -r2.2
--- additional_help	16 Aug 2002 21:44:51 -0000	2.1
+++ additional_help	14 May 2003 22:29:29 -0000	2.2
@@ -40,6 +40,16 @@
 Use the next option to add parameters. If your MySQL is password-protected
 (and you know the password) you may want to add "-u root -p".
 
+MYSQLTRANSACT
+.
+We can use the InnoDB table type in MySQL 3.23 and higher to
+allow transactions for orders. This prevents insertion of
+and updates to the order entry logs when there is an error.
+.
+If you have MySQL 4.0 you should certainly use this -- if you
+are using MySQL 3.23 or higher you might want to use it if the
+InnoDB table type is configured in.
+
 MYSQLCREATEOPT
 .
 To create the database you may need to supply a password and



2.2       +6 -0      interchange/dist/foundation/dbconf/mysql/inventory.mysql


rev 2.2, prev_rev 2.1
Index: inventory.mysql
===================================================================
RCS file: /var/cvs/interchange/dist/foundation/dbconf/mysql/inventory.mysql,v
retrieving revision 2.1
retrieving revision 2.2
diff -u -r2.1 -r2.2
--- inventory.mysql	30 Dec 2002 00:48:36 -0000	2.1
+++ inventory.mysql	14 May 2003 22:29:29 -0000	2.2
@@ -1,4 +1,10 @@
 Database  inventory  inventory.txt __SQLDSN__
+
+ifdef MYSQL_TRANSACTIONS
+Database  inventory  HAS_TRANSACTIONS  1
+Database  inventory  TABLE_TYPE   InnoDB
+endif
+
 Database  inventory  COLUMN_DEF   "sku=char(64) NOT NULL PRIMARY KEY"
 Database  inventory  COLUMN_DEF   "quantity=INT"
 Database  inventory  COLUMN_DEF   "stock_msg=char(64)"



2.4       +6 -1      interchange/dist/foundation/dbconf/mysql/mysql.cfg


rev 2.4, prev_rev 2.3
Index: mysql.cfg
===================================================================
RCS file: /var/cvs/interchange/dist/foundation/dbconf/mysql/mysql.cfg,v
retrieving revision 2.3
retrieving revision 2.4
diff -u -r2.3 -r2.4
--- mysql.cfg	21 Apr 2003 14:15:44 -0000	2.3
+++ mysql.cfg	14 May 2003 22:29:29 -0000	2.4
@@ -6,7 +6,12 @@
 DatabaseDefault USER __SQLUSER__
 DatabaseDefault PASS __SQLPASS__
 DatabaseDefault DEFAULT_TYPE varchar(255)
- 
+
+ifdef MYSQL_TRANSACTIONS
+## This falls through to order routes
+Variable TRANSACTION_TABLES userdb transactions orderline inventory
+endif
+
 # Include each table definition from its separate file.
 include dbconf/mysql/*.mysql
 



2.4       +7 -3      interchange/dist/foundation/dbconf/mysql/orderline.mysql


rev 2.4, prev_rev 2.3
Index: orderline.mysql
===================================================================
RCS file: /var/cvs/interchange/dist/foundation/dbconf/mysql/orderline.mysql,v
retrieving revision 2.3
retrieving revision 2.4
diff -u -r2.3 -r2.4
--- orderline.mysql	30 Dec 2002 00:48:36 -0000	2.3
+++ orderline.mysql	14 May 2003 22:29:29 -0000	2.4
@@ -1,5 +1,11 @@
 Database  orderline  orderline.txt __SQLDSN__
 Database  orderline  DEFAULT_TYPE varchar(128)
+
+ifdef MYSQL_TRANSACTIONS
+Database  orderline  HAS_TRANSACTIONS  1
+Database  orderline  TABLE_TYPE   InnoDB
+endif
+
 Database  orderline  COLUMN_DEF   "code=char(14) NOT NULL PRIMARY KEY"
 Database  orderline  COLUMN_DEF   "store_id=varchar(9) DEFAULT '' NOT NULL"
 Database  orderline  COLUMN_DEF   "order_number=varchar(14) NOT NULL"
@@ -12,12 +18,10 @@
 Database  orderline  COLUMN_DEF   "subtotal=varchar(12) NOT NULL"
 Database  orderline  COLUMN_DEF   "shipping=varchar(12)"
 Database  orderline  COLUMN_DEF   "taxable=varchar(3)"
-Database  orderline  COLUMN_DEF   "mv_mi=varchar(64)"
-Database  orderline  COLUMN_DEF   "mv_si=varchar(3)"
-Database  orderline  COLUMN_DEF   "mv_mp=varchar(3)"
 Database  orderline  COLUMN_DEF   "options=varchar(255)"
 Database  orderline  COLUMN_DEF   "order_date=varchar(32) NOT NULL"
 Database  orderline  COLUMN_DEF   "update_date=timestamp"
 Database  orderline  COLUMN_DEF   "status=varchar(32)"
+Database  orderline  COLUMN_DEF   "comment=text"
 Database  orderline  COLUMN_DEF   "parent=varchar(9)"
 Database  orderline  INDEX         store_id order_number



2.6       +6 -0      interchange/dist/foundation/dbconf/mysql/transactions.mysql


rev 2.6, prev_rev 2.5
Index: transactions.mysql
===================================================================
RCS file: /var/cvs/interchange/dist/foundation/dbconf/mysql/transactions.mysql,v
retrieving revision 2.5
retrieving revision 2.6
diff -u -r2.5 -r2.6
--- transactions.mysql	30 Dec 2002 00:48:36 -0000	2.5
+++ transactions.mysql	14 May 2003 22:29:29 -0000	2.6
@@ -1,5 +1,11 @@
 Database  transactions  transactions.txt __SQLDSN__
 Database  transactions  DEFAULT_TYPE varchar(128)
+
+ifdef MYSQL_TRANSACTIONS
+Database  transactions  HAS_TRANSACTIONS  1
+Database  transactions  TABLE_TYPE   InnoDB
+endif
+
 Database  transactions  COLUMN_DEF   "code=char(14) NOT NULL PRIMARY KEY"
 Database  transactions  COLUMN_DEF   "store_id=varchar(9) DEFAULT '' NOT NULL"
 Database  transactions  INDEX         store_id



2.4       +6 -0      interchange/dist/foundation/dbconf/mysql/userdb.mysql


rev 2.4, prev_rev 2.3
Index: userdb.mysql
===================================================================
RCS file: /var/cvs/interchange/dist/foundation/dbconf/mysql/userdb.mysql,v
retrieving revision 2.3
retrieving revision 2.4
diff -u -r2.3 -r2.4
--- userdb.mysql	30 Dec 2002 00:48:36 -0000	2.3
+++ userdb.mysql	14 May 2003 22:29:29 -0000	2.4
@@ -1,4 +1,10 @@
 Database  userdb  userdb.txt   __SQLDSN__
+
+ifdef MYSQL_TRANSACTIONS
+Database  userdb  HAS_TRANSACTIONS  1
+Database  userdb  TABLE_TYPE   InnoDB
+endif
+
 Database  userdb  COLUMN_DEF   "code=char(20) NOT NULL PRIMARY KEY"
 Database  userdb  COLUMN_DEF   "password=CHAR(20) NOT NULL"
 Database  userdb  COLUMN_DEF   "acl=text"



2.28      +1 -0      interchange/dist/foundation/products/variable.txt


rev 2.28, prev_rev 2.27
Index: variable.txt
===================================================================
RCS file: /var/cvs/interchange/dist/foundation/products/variable.txt,v
retrieving revision 2.27
retrieving revision 2.28
diff -u -r2.27 -r2.28
--- variable.txt	6 May 2003 23:06:34 -0000	2.27
+++ variable.txt	14 May 2003 22:29:29 -0000	2.28
@@ -42,6 +42,7 @@
 MV_PAYMENT_SECRET		Payment
 MV_PAYMENT_VENDOR		Payment
 MYSQL	__MVC_MYSQL__	Database
+MYSQL_TRANSACTIONS	__MVC_MYSQLTRANSACT__	Database
 ORACLE	__MVC_ORACLE__	Database
 ORDERS_TO	__MVC_MAILORDERTO__	Order
 ORDER_ROUTES	log main copy_user	Order



2.116     +5 -2      interchange/lib/Vend/Config.pm


rev 2.116, prev_rev 2.115
Index: Config.pm
===================================================================
RCS file: /var/cvs/interchange/lib/Vend/Config.pm,v
retrieving revision 2.115
retrieving revision 2.116
diff -u -r2.115 -r2.116
--- Config.pm	13 May 2003 20:11:58 -0000	2.115
+++ Config.pm	14 May 2003 22:29:29 -0000	2.116
@@ -1,6 +1,6 @@
 # Vend::Config - Configure Interchange
 #
-# $Id: Config.pm,v 2.115 2003/05/13 20:11:58 mheins Exp $
+# $Id: Config.pm,v 2.116 2003/05/14 22:29:29 mheins Exp $
 #
 # Copyright (C) 1996-2002 Red Hat, Inc. <interchange@redhat.com>
 # Copyright (C) 2003 ICDEVGROUP <interchange@icdevgroup.org>
@@ -48,7 +48,7 @@
 use Vend::File;
 use Vend::Data;
 
-$VERSION = substr(q$Revision: 2.115 $, 10);
+$VERSION = substr(q$Revision: 2.116 $, 10);
 
 my %CDname;
 my %CPname;
@@ -2437,7 +2437,10 @@
 			my %seen;
 			my @types = grep !$seen{$_}++, keys %$o, @base;
 
+			my %module_ignore = qw/resolution 1 default 1/;
+
 			for(@types) {
+				next if $module_ignore{$_};
 				my $loc = $o->{$_} ||= {};
 				eval "require Vend::Ship::$_;";
 				if($@) {



2.2       +43 -9     interchange/lib/Vend/Ship.pm


rev 2.2, prev_rev 2.1
Index: Ship.pm
===================================================================
RCS file: /var/cvs/interchange/lib/Vend/Ship.pm,v
retrieving revision 2.1
retrieving revision 2.2
diff -u -r2.1 -r2.2
--- Ship.pm	13 May 2003 19:05:18 -0000	2.1
+++ Ship.pm	14 May 2003 22:29:29 -0000	2.2
@@ -1,6 +1,6 @@
 # Vend::Ship - Interchange shipping code
 # 
-# $Id: Ship.pm,v 2.1 2003/05/13 19:05:18 mheins Exp $
+# $Id: Ship.pm,v 2.2 2003/05/14 22:29:29 mheins Exp $
 #
 # Copyright (C) 1996-2002 Red Hat, Inc. <interchange@redhat.com>
 #
@@ -98,19 +98,51 @@
 	$opt = {} unless $opt;
     my($code, $desc, $min, $criterion, $max, $cost, $mode);
 
+	my $loc;
+	$loc = $Vend::Cfg->{Shipping_repository}{default}
+			if $Vend::Cfg->{Shipping_repository};
+
+	$loc ||= {};
+
+	my $base_dir = $loc->{directory} || $loc->{dir} || $Vend::Cfg->{ProductDir};
+
+	my @files;
 	if ($file) {
-		#nada
+		push @files, $file;
 	}
 	elsif($opt->{add} or $Vend::Cfg->{Variable}{MV_SHIPPING}) {
 		$file = "$Vend::Cfg->{ScratchDir}/shipping.asc";
 		Vend::Util::writefile(">$file", $opt->{add} || $Vend::Cfg->{Variable}{MV_SHIPPING});
+		push @files, $file;
 	}
 	else {
+		my %found;
+		if($Vend::Cfg->{Shipping}) {
+			my $repos = $Vend::Cfg->{Shipping_repository};
+			for(keys %$repos) {
+				next unless $file = $repos->{$_}{config_file};
+				$file = Vend::Util::catfile($base_dir, $file)
+					unless $file =~ m{/};
+#::logDebug("found shipping file=$file");
+				$found{$file} = 1;
+				push @files, $file;
+			}
+		}
 		$file = $Vend::Cfg->{Special}{'shipping.asc'}
-				|| Vend::Util::catfile($Vend::Cfg->{ProductDir},'shipping.asc');
+				|| Vend::Util::catfile($base_dir, 'shipping.asc');
+
+		if(-f $file and !$found{$file}) {
+			push @files, $file;
+		}
+	}
+
+#::logDebug("shipping files=" . ::uneval(\@files));
+	my @flines;
+	for(@files) {
+		push @flines, split /\n/, readfile($_);
+#::logDebug("shipping lines=" . scalar(@flines));
 	}
 
-	my @flines = split /\n/, readfile($file);
 	if ($Vend::Cfg->{CustomShipping} =~ /^select\s+/i) {
 		($Vend::Cfg->{SQL_shipping} = 1, return)
 			if $Global::Foreground;
@@ -315,11 +347,12 @@
 				};
 	}
 	UPSZONE: {
+
 		for (keys %zones) {
 			my $ref = $zones{$_};
 			if (! $ref->{zone_data}) {
 				$ref->{zone_file} = Vend::Util::catfile(
-											$Vend::Cfg->{ProductDir},
+											$base_dir,
 											"$ref->{zone_name}.csv",
 										) if ! $ref->{zone_file};
 				$ref->{zone_data} =  readfile($ref->{zone_file});
@@ -346,7 +379,6 @@
 				$zone[0] =~ s/^\w+/low,high/;
 				@zone = grep /,/, @zone;
 				$zone[0] =~	s/\s*,\s*/\t/g;
-my $i = 1;
 				for(@zone[1 .. $#zone]) {
 					s/^\s*(\w+)\s*,/make_three($1, $len) . ',' . make_three($1, $len) . ','/e;
 					s/^\s*(\w+)\s*-\s*(\w+),/make_three($1, $len) . ',' . make_three($2, $len) . ','/e;
@@ -725,8 +757,10 @@
 			$extra = $1;
 			my $loc = $Vend::Cfg->{Shipping_repository}{$what}
 				or return do_error("Unknown custom shipping type '%s'", $what);
-			my $routine = $loc->{cost_routine};
-			$routine ||= "Vend::Ship::${what}::calculate";
+			for(keys %$loc) {
+				$o->{$_} = $loc->{$_} unless defined $o->{$_};
+			}
+			my $routine = $o->{cost_routine} || "Vend::Ship::${what}::calculate";
 			my $sub = \&{"$routine"};
 			if(! defined $sub) {
 				::logOnce(
@@ -739,7 +773,7 @@
 				return undef;
 			}
 #::logDebug("ready to calculate custom Ship type=$what total=$total options=$o");
-			$cost = $sub->($mode, $total, $row, $o, $loc, $extra);
+			$cost = $sub->($mode, $total, $row, $o, $opt, $extra);
 			$final += $cost;
 			last SHIPIT unless $o->{continue};
 		}



1.1                  interchange/lib/Vend/Ship/Postal.pm


rev 1.1, prev_rev 1.0
Index: Postal.pm
===================================================================
# Vend::Ship - Interchange shipping code
# 
# $Id: Postal.pm,v 1.1 2003/05/14 22:29:29 mheins Exp $
#
# Copyright (C) 1996-2002 Red Hat, Inc. <interchange@redhat.com>
#
# This program was originally based on Vend 0.2 and 0.3
# Copyright 1995 by Andrew M. Wilcox <amw@wilcoxsolutions.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Free
# Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA  02111-1307  USA.

package Vend::Ship::Postal;

use Vend::Util;
use Vend::Interpolate;
use Vend::Data;
use Vend::Ship;

sub calculate {
	my ($mode, $weight, $row, $opt, $tagopt, $extra) = @_;

	$opt ||= { auto => 1 };

#::logDebug("Postal custom: mode=$mode weight=$weight row=$row opt=" . uneval($opt));

	$type = $opt->{table};
	$o->{geo} ||= 'country';

	if(! $type) {
		$extra = interpolate_html($extra) if $extra =~ /__|\[/;;
		($type) = split /\s+/, $extra;
	}

	unless($type) {
		do_error("No table/type specified for %s shipping", 'Postal');
		return 0;
	}

	$country = $::Values->{$o->{geo}};
#::logDebug("ready to calculate postal type=$type country=$country weight=$weight");

	if($opt->{source_grams}) {
		$weight *= 0.00220462;
	}
	elsif($opt->{source_kg}) {
		$weight *= 2.20462;
	}
	elsif($opt->{source_oz}) {
		$weight /= 16;
	}

	if($opt->{auto}) {
		if($type eq 'surf_lp') {
			$opt->{oz} = 1;
		}
		elsif ($type eq 'air_lp') {
			$opt->{oz} = 1;
		}

		if($type =~ /_([pl]p)$/) {
			$opt->{max_field} = "max_$1";
		}
		elsif ($type =~ /^(ems|gxg)$/) {
			$opt->{max_field} = "max_$1";
		}
	}

	if($opt->{oz}) {
		$weight *= 16;
	}

	$weight = POSIX::ceil($weight);

	$opt->{min_weight} ||= 1;

	$weight = $opt->{min_weight} if $opt->{min_weight} > $weight;

	if(my $modulo = $opt->{aggregate}) {
		if($weight > $modulo) {
			my $cost = 0;
			my $w = $weight;
			while($w > $modulo) {
				$w -= $modulo;
				$cost += tag_postal($type, $modulo, $country, $opt);
			}
			$cost += tag_postal($type, $w, $country, $opt);
			return $cost;
		}
	}

	$opt->{table} ||= $type;
	$opt->{zone_table} ||= 'usps';

	unless (defined $Vend::Database{$opt->{zone_table}}) {
		logError("Postal lookup called, no database table named '%s'", $opt->{zone_table});
		return undef;
	}

	unless (defined $Vend::Database{$opt->{table}}) {
		logError("Postal lookup called, no database table named '%s'", $opt->{table});
		return undef;
	}

	$country =~ s/\W+//;
	$country = uc $country;

	unless(length($country) == 2) {
		return do_error(
						'Country code %s improper format for postal shipping.',
						$country,
						);
	}


	my $crecord = tag_data($opt->{zone_table}, undef, $country, {hash => 1})
					or return do_error(
							'Country code %s has no zone for postal shipping.',
							$country,
						);

	$opt->{type_field} ||= $type;

	my $zone = $crecord->{$opt->{type_field}};

	unless($zone =~ /^\w+$/) {
		return do_error(
						'Country code %s has no zone for type %s.',
						$country,
						$type,
					);
	}

	$zone = "zone$zone" unless $zone =~ /^zone/ or $opt->{verbatim_zone};
	
	my $maxits = $opt->{max_modulo} || 4;
	my $its = 1;
	my $cost;

	do {
		$cost = tag_data($opt->{table}, $zone, $weight);
	} until $cost or $its++ > $maxits;
		
	return do_error(
					"Zero cost returned for mode %s, geo code %s.",
					$type,
					$country,
				)
		unless $cost;

	return $cost;
}

=head1 NAME

Vend::Ship::Postal -- Calculate US Postal service international rates

=head1 SYNOPSIS

 (in catalog.cfg)

    Database   usps             ship/usps.txt              TAB
    Database   air_pp           ship/air_pp.txt            TAB
    Database   surf_pp          ship/surf_pp.txt           TAB

 (in shipping.asc)

    air_pp: US Postal Air Parcel
        crit            weight
        min             0
        max             0
        cost            e No shipping needed!
        at_least        4
        adder           1
        aggregate       70
        table           air_pp

        min             0
        max             1000
        cost            s Postal

        min             70
        max             9999999
        cost            e Too heavy for Air Parcel

    surf_pp:    US Postal Surface Parcel
        crit            weight
        min             0
        max             0
        cost            e No shipping needed!
        at_least        4
        adder           1
        aggregate       70
        table           surf_pp

        min             0
        max             1000
        cost            s Postal

        min             70
        max             9999999
        cost            e Too heavy for Postal Parcel

=head1 DESCRIPTION

Looks up a service zone by country in the C<usps> table, then looks in
the appropriate rate table for a price by that zone.

Can aggregate shipments greater than 70 pounds by assuming you will ship
multiple 70-pound packages (plus one package with the remainder).

=cut

1;



1.1                  interchange/lib/Vend/Ship/QueryUPS.pm


rev 1.1, prev_rev 1.0
Index: QueryUPS.pm
===================================================================
# Vend::Ship::QueryUPS - Interchange shipping code
# 
# $Id: QueryUPS.pm,v 1.1 2003/05/14 22:29:29 mheins Exp $
#
# Copyright (C) 1996-2002 Red Hat, Inc. <interchange@redhat.com>
#
# This program was originally based on Vend 0.2 and 0.3
# Copyright 1995 by Andrew M. Wilcox <amw@wilcoxsolutions.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Free
# Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA  02111-1307  USA.

package Vend::Ship::QueryUPS;

use Vend::Util;
use Vend::Interpolate;
use Vend::Data;
use Vend::Ship;
use Business::UPS;

sub calculate {
	my ($mode, $weight, $row, $opt, $tagopt, $extra) = @_;

	$opt->{service}         ||= $opt->{table};
	if(! $opt->{service} and $extra =~ /^\w+$/)  {
		$opt->{service} = $extra;
	}
	$opt->{service} ||= $opt->{table} || $mode;

	$opt->{origin}			||= $::Variable->{UPS_ORIGIN};
	$opt->{country_field}	||= $::Variable->{UPS_COUNTRY_FIELD} || 'country';
	$opt->{geo}				||= $::Variable->{UPS_POSTCODE_FIELD} || 'zip';

	my $origin  = $opt->{origin};
	my $country = $opt->{country} || $::Values->{$opt->{country_field}};

	$country ||= $opt->{default_country} || 'US';

	my $zip     = $opt->{zip}	  || $::Values->{$opt->{geo}};
	$zip ||= $opt->{default_geo};

	my $modulo = $opt->{aggregate};

	if($modulo and $modulo <= 1) {
		$modulo = $::Variable->{UPS_QUERY_MODULO} || 150;
	}
	elsif(! $modulo) {
		$modulo = 9999999;
	}

	$country = uc $country;

	# In the U.S., UPS only wants the 5-digit base ZIP code, not ZIP+4
	$country eq 'US' and $zip =~ /^(\d{5})/ and $zip = $1;

#::logDebug("calling QueryUPS with: " . join("|", $opt->{service}, $origin, $zip, $weight, $country,$modulo));

	my $cache;
	my $cache_code;
	my $db;
	my $now;
	my $updated;
	my %cline;
	my $shipping;
	my $zone;
	my $error;

	my $ctable = $opt->{cache_table} || 'ups_cache';


	if($Vend::Database{$ctable}) {
		$Vend::WriteDatabase{$ctable} = 1;
		CACHE: {
			$db = dbref($ctable)
				or last CACHE;
			my $tname = $db->name();
			$cache = 1;
			%cline = (
				weight => $weight,
				origin => $origin,
				country => $country,
				zip	=> $zip,
				shipmode => $opt->{service},
			);

			my @items;
			# reverse sort makes zip first
			for(reverse sort keys %cline) {
				push @items, "$_ = " . $db->quote($cline{$_}, $_);
			}

			my $string = join " AND ", @items;
			my $q = qq{SELECT code,cost,updated from $tname WHERE $string};
			my $ary = $db->query($q);
#::logDebug("query cache: " . ::uneval($ary));
			if($ary and $ary->[0] and $cache_code = $ary->[0][0]) {
				$shipping = $ary->[0][1];
				$updated = $ary->[0][2];
				$now = time();
				if($now - $updated > 86000) {
					undef $shipping;
					$updated = $now;
				}
				elsif($shipping <= 0) {
					$error = $shipping;
					$updated = $now;
					$shipping = 0;
				}
			}
#::logDebug("shipping is: " . (defined $shipping ? $shipping : 'undef'));
		}
	}

	my $w = $weight;
	my $maxcost;
	my $tmpcost;

	unless(defined $shipping) {
		$shipping = 0;
		while($w > $modulo) {
			$w -= $modulo;
			if($maxcost) {
				$shipping += $maxcost;
				next;
			}

			($maxcost, $zone, $error)
				= getUPS( $opt->{service}, $origin, $zip, $modulo, $country);
			if($error) {
				do_error(	"Ship mode %s: Error calling UPS service %s",
							$mode,
							$opt->{service}, );
				return 0;
			}
			$shipping += $maxcost;
		}

		undef $error;
#::logDebug("calling getUPS( $opt->{service}, $origin, $zip, $w, $country)");
		($tmpcost, $zone, $error)
			= getUPS( $opt->{service}, $origin, $zip, $w, $country);

		$shipping += $tmpcost;
		if($cache) {
			$cline{updated} = $now || time();
			$cline{cost} = $shipping || $error;
			$db->set_slice($cache_code, \%cline);
		}
	}

	if($error) {
		do_error(	"Ship mode %s: Error calling UPS service %s",
					$mode,
					$opt->{service}, );
		$shipping = 0;
	}
	return $shipping;
}

=head1 NAME

Vend::Ship::QueryUPS -- calculate UPS costs via www

=head1 SYNOPSIS

  (catalog.cfg)

  Shipping  QueryUPS  default_geo  45056

  (shipping.asc)
  ground: UPS Ground Commercial
     origin  45056
     service GNDCOM

	 min	0
	 max	0
	 cost	e Nothing to ship!

	 min	0
	 max	150
	 cost	s QueryUPS

	 min	150
	 max	99999999
	 cost	e Too heavy for UPS.

=head1 DESCRIPTION

Calculates UPS costs via the WWW using Business::UPS. 

To activate, configure any parameter in catalog.cfg. A good choice
is the default origin zip.

Options:

=over 4

=item weight

Weight in pounds. Required -- normally passed via CRIT parameter.

=item service

Any valid Business::UPS mode (required). Example: 1DA,2DA,GNDCOM. Defaults
to the mode name.

=item geo

Location of field containing zip code. Default is 'zip'.

=item country_field

Location of field containing country code. Default is 'country'.

=item default_geo

The ZIP code to use if none supplied -- for defaulting shipping to some
value in absence of ZIP. No default -- will return 0 and error if
no zip.

=item default_country

The country code to use if none supplied -- for defaulting shipping to some
value in absence of country. Default US.

=item aggregate

If 1, aggregates by a call to weight=150 (or $Variable->{UPS_QUERY_MODULO}).
Multiplies that times number necessary, then runs a call for the
remainder. In other words:

	[ups-query weight=400 mode=GNDCOM aggregate=1]

is equivalent to:

	[calc]
		[ups-query weight=150 mode=GNDCOM] + 
		[ups-query weight=150 mode=GNDCOM] + 
		[ups-query weight=100 mode=GNDCOM];
	[/calc]

If set to a number above 1, will be the modulo to do repeated calls by. So:

	[ups-query weight=400 mode=GNDCOM aggregate=100]

is equivalent to:

	[calc]
		[ups-query weight=100 mode=GNDCOM] + 
		[ups-query weight=100 mode=GNDCOM] + 
		[ups-query weight=100 mode=GNDCOM] + 
		[ups-query weight=100 mode=GNDCOM];
	[/calc]

To aggregate by 1, use .999999.

=item cache_table

Set to the name of a table (default ups_cache) which can cache the
calls so repeated calls for the same values will not require repeated
calls to UPS.

Table needs to be set up with:

	Database   ups_cache        ship/ups_cache.txt         __SQLDSN__
	Database   ups_cache        AUTO_SEQUENCE  ups_cache_seq
	Database   ups_cache        DEFAULT_TYPE varchar(12)
	Database   ups_cache        INDEX  weight origin zip shipmode country

And have the fields:

	 code weight origin zip country shipmode cost updated

Typical cached data will be like:

	code	weight	origin	zip	country	shipmode	cost	updated
	14	11	45056	99501	US	2DA	35.14	1052704130
	15	11	45056	99501	US	1DA	57.78	1052704130
	16	11	45056	99501	US	2DA	35.14	1052704132
	17	11	45056	99501	US	1DA	57.78	1052704133

Cache expires in one day.

=back

=cut

1;