/
QueryUPS.pm
316 lines (240 loc) · 7.45 KB
/
QueryUPS.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# Vend::Ship::QueryUPS - Interchange shipping code
#
# $Id: QueryUPS.pm,v 1.5 2005-11-08 18:14:47 jon Exp $
#
# Copyright (C) 2002-2005 Interchange Development Group
# Copyright (C) 1996-2002 Red Hat, Inc.
#
# 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;
my %exception = ( UK => 'GB');
if(! $::Variable->{UPS_COUNTRY_REMAP} ) {
# do nothing
}
elsif ($::Variable->{UPS_COUNTRY_REMAP} =~ /=/) {
my $new = Vend::Util::get_option_hash($::Variable->{UPS_COUNTRY_REMAP});
Vend::Util::get_option_hash(\%exception, $new);
}
else {
Vend::Util::hash_string($::Variable->{UPS_COUNTRY_REMAP}, \%exception);
}
$country = $exception{$country} if $exception{$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;