/
Old48.pm
440 lines (375 loc) · 10.5 KB
/
Old48.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# Vend::Options::Old48 - Interchange 4.8 compatible product options
#
# Copyright (C) 2002-2007 Interchange Development Group, https://www.interchangecommerce.org/
# Copyright (C) 2002-2003 Mike Heins <mikeh@perusion.net>
# 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., 51 Franklin St, Fifth Floor, Boston,
# MA 02110-1301 USA.
#
package Vend::Options::Old48;
$VERSION = '1.14';
=head1 NAME
Vend::Options::Old48 - Interchange Compatibility Options Support
=head1 SYNOPSIS
[item-options]
or
[price code=SKU]
=head1 PREREQUISITES
Vend::Options
=head1 DESCRIPTION
The Vend::Options::Old48 module implements simple and matrix product
options for Interchange. It is compatible with Interchange 4.8.x
matrix options. Newer versions use Simple and Matrix options instead.
If the Interchange Variable MV_OPTION_TABLE is not set, it defaults
to "options", which combines options for Simple, Matrix, and
Modular into that one table. This goes along with foundation and
construct demos up until Interchange 4.9.8.
The "options" table remains the default for matrix options.
=head1 AUTHORS
Mike Heins <mikeh@perusion.net>
=head1 CREDITS
Jon Jensen <jon@swelter.net>
=cut
use Vend::Util;
use Vend::Data;
use Vend::Interpolate;
use Vend::Options;
sub option_cost {
my ($item, $opt) = @_;
}
sub display_options_matrix {
my ($item, $opt, $loc) = @_;
$loc ||= $Vend::Cfg->{Options_repository}{Old48} || \%Default;
#::logDebug("Matrix options by module, old");
my $sku = $item->{mv_sku} || $item->{code};
my $db;
my $tab;
if(not $db = $opt->{options_db}) {
$tab = $opt->{table} || $::Variable->{MV_OPTION_TABLE} || 'options';
$db = database_exists_ref($tab)
or do {
logOnce(
"Matrix options: unable to find table %s for item %s",
$tab,
$sku,
);
return undef;
};
}
my $record;
if(not $record = $opt->{options_record}) {
$db->record_exists($sku)
or do {
logOnce(
"Matrix options: unable to find record in table %s for item %s",
$tab,
$sku,
);
return;
};
$record = $db->row_hash($sku) || {};
}
my $tname = $db->name();
if(not $opt->{display_type} ||= $record->{display_type}) {
$opt->{display_type} = $record->{o_matrix} == 2 ? 'separate' : 'single';
}
$opt->{display_type} = lc $opt->{display_type};
my $map;
if(not $map = $opt->{options_map}) {
$map = $opt->{options_map} = {};
if(my $remap = $opt->{remap} || $::Variable->{MV_OPTION_TABLE_MAP}) {
remap_option_record($record, $map, $remap);
}
}
my @rf;
my @out;
my $out;
my $inv_func;
if($opt->{inventory}) {
my ($tab, $col) = split /:+/, $opt->{inventory};
MAKEFUNC: {
my $idb = dbref($tab)
or do {
logError("Bad table %s for inventory function.", $tab);
last MAKEFUNC;
};
$idb->test_column($col)
or do {
logError(
"Bad column %s in table %s for inventory function.",
$col,
$tab,
);
last MAKEFUNC;
};
$inv_func = sub {
my $key = shift;
return $idb->field($key, $col);
};
}
}
my $rsort = find_sort($opt, $db, $loc);
if($opt->{display_type} eq 'separate') {
for(qw/code o_enable o_group o_value o_label o_widget price/) {
push @rf, ($map->{$_} || $_);
}
my @def;
if($item and $item->{code}) {
@def = split /-/, $item->{code};
}
my $fsel = $map->{sku} || 'sku';
my $rsel = $db->quote($sku, $fsel);
my $q = "SELECT " .
join (",", @rf) .
" FROM $tname where $fsel = $rsel $rsort";
#::logDebug("tag_options matrix query: $q");
my $ary = $db->query($q);
#::logDebug("tag_options matrix ary: " . ::uneval($ary));
my $ref;
my $i = 0;
my $phony = { %{$item || { }} };
foreach $ref (@$ary) {
next unless $ref->[3];
# skip based on inventory if enabled
if($inv_func) {
my $oh = $inv_func->($ref->[0]);
next if $oh <= 0;
}
$i++;
# skip unless o_value
$phony->{mv_sku} = $def[$i];
if ($opt->{label}) {
$ref->[4] = "<b>$ref->[4]</b>" if $opt->{bold};
push @out, $ref->[4];
}
push @out, Vend::Interpolate::tag_accessories(
$sku,
'',
{
passed => $ref->[3],
type => $opt->{type} || $ref->[5] || 'select',
attribute => 'mv_sku',
price_data => $ref->[6],
price => $opt->{price},
extra => $opt->{extra},
js => $opt->{js},
item => $phony,
},
$phony || undef,
);
}
$phony->{mv_sku} = $sku;
my $begin = Vend::Interpolate::tag_accessories(
$sku,
'',
{
type => 'hidden',
attribute => 'mv_sku',
item => $phony,
default => $sku,
},
$phony,
);
if($opt->{td}) {
for(@out) {
$out .= "<td>$begin$_</td>";
$begin = '';
}
}
else {
$opt->{joiner} = "<br$Vend::Xtrailer>" if ! $opt->{joiner};
$out .= $begin;
$out .= join $opt->{joiner}, @out;
}
}
else {
for(qw/code o_enable o_group description price weight volume differential o_widget/) {
push @rf, ($map->{$_} || $_);
}
my $ccol = $map->{code} || 'code';
my $lcol = $map->{sku} || 'sku';
my $lval = $db->quote($sku, $lcol);
my $q = "SELECT " . join(",", @rf);
$q .= " FROM $tname where $lcol = $lval AND $ccol <> $lval $rsort";
#::logDebug("tag_options matrix query: $q");
my $ary = $db->query($q);
#::logDebug("tag_options matrix ary: " . ::uneval($ary));
my $ref;
my $price = {};
foreach $ref (@$ary) {
# skip unless description
next unless $ref->[3];
# skip based on inventory if enabled
if($inv_func) {
my $oh = $inv_func->($ref->[0]);
next if $oh <= 0;
}
$ref->[3] =~ s/,/,/g;
$ref->[3] =~ s/=/=/g;
$price->{$ref->[0]} = $ref->[4];
push @out, "$ref->[0]=$ref->[3]";
}
$out .= "<td>" if $opt->{td};
$out .= Vend::Interpolate::tag_accessories(
$sku,
'',
{
attribute => 'code',
default => undef,
extra => $opt->{extra},
item => $item,
js => $opt->{js},
name => 'mv_sku',
passed => join(",", @out),
price => $opt->{price},
price_data => $price,
type => $opt->{type} || $ref->[8] || 'select',
},
$item || undef,
);
$out .= "</td>" if $opt->{td};
#::logDebug("matrix option returning $out");
}
return $out;
}
sub price_options {
my ($item, $table, $final, $loc) = @_;
#::logDebug("option_cost table=$table");
$loc ||= $Vend::Cfg->{Options_repository}{Old48} || {};
my $sku = $item->{mv_sku} || $item->{code};
my $db = database_exists_ref($table)
or return undef;
#::logDebug("option_cost db=$db");
my $map = $loc->{map} || {};
my $fsel = $map->{sku} || 'sku';
my $rsel = $db->quote($sku, $fsel);
my @rf;
for(qw/o_group price/) {
push @rf, ($map->{$_} || $_);
}
my $q = "SELECT " . join (",", @rf) . " FROM $table WHERE $fsel = $rsel";
#::logDebug("option_cost query=$q");
my $ary = $db->query($q);
return if ! $ary->[0];
my $ref;
my $price = 0;
my $f;
foreach $ref (@$ary) {
#::logDebug("checking option " . uneval_it($ref));
next unless defined $item->{$ref->[0]};
$ref->[1] =~ s/^\s+//;
$ref->[1] =~ s/\s+$//;
$ref->[1] =~ s/==/=:/g;
my %info = split /\s*[=,]\s*/, $ref->[1];
if(defined $info{ $item->{$ref->[0]} } ) {
my $atom = $info{ $item->{$ref->[0]} };
if($atom =~ s/^://) {
$f = $atom;
next;
}
elsif ($atom =~ s/\%$//) {
$f = $final if ! defined $f;
$f += ($atom * $final / 100);
}
else {
$price += $atom;
}
}
}
#::logDebug("option_cost returning price=$price f=$f");
return ($price, $f);
}
sub display_options_simple {
my ($item, $opt) = @_;
#::logDebug("Simple options, item=" . ::uneval($item) . "\nopt=" . ::uneval($opt));
my $map = $opt->{options_map} ||= {};
#::logDebug("Simple options by module, old");
my $sku = $item->{code};
my $db;
my $tab;
if(not $db = $opt->{options_db}) {
$tab = $opt->{table} ||= $::Variable->{MV_OPTION_TABLE_SIMPLE}
||= $::Variable->{MV_OPTION_TABLE}
||= 'options';
$db = database_exists_ref($tab)
or do {
logOnce(
"Simple options: unable to find table %s for item %s",
$tab,
$sku,
);
return undef;
};
}
my $tname = $db->name();
my @rf;
my @out;
my $out;
my $ishash = defined $item->{mv_ip} ? 1 : 0;
for(qw/code o_enable o_group o_value o_label o_widget price o_height o_width/) {
push @rf, ($map->{$_} || $_);
}
my $fsel = $map->{sku} || 'sku';
my $rsel = $db->quote($sku, $fsel);
my $q = "SELECT " . join (",", @rf) . " FROM $tname where $fsel = $rsel";
if(my $rsort = find_sort($opt, $db, $loc)) {
$q .= " $rsort";
}
#::logDebug("tag_options simple query: $q");
my $ary = $db->query($q)
or return;
my $ref;
foreach $ref (@$ary) {
# skip unless o_value
next unless $ref->[3];
if ($opt->{label}) {
$ref->[4] = "<b>$ref->[4]</b>" if $opt->{bold};
push @out, $ref->[4];
}
my $precursor = $opt->{report}
? "$ref->[2]$opt->{separator}"
: qq{<input type="hidden" name="mv_item_option" value="$ref->[2]">};
push @out, $precursor . Vend::Interpolate::tag_accessories(
$sku,
'',
{
attribute => $ref->[2],
default => undef,
extra => $opt->{extra},
item => $item,
name => $ishash ? undef : "mv_order_$ref->[2]",
js => $opt->{js},
passed => $ref->[3],
price => $opt->{price},
price_data => $ref->[6],
height => $opt->{height} || $ref->[7],
width => $opt->{width} || $ref->[8],
type => $opt->{type} || $ref->[5] || 'select',
},
$item || undef,
);
}
if($opt->{td}) {
for(@out) {
$out .= "<td>$_</td>";
}
}
else {
$opt->{joiner} = "<br$Vend::Xtrailer>" if ! $opt->{joiner};
$out .= join $opt->{joiner}, @out;
}
return $out;
}
*display_options = \&display_options_simple;
1;