MiniVend Akopia Services

[Date Prev][Date Next][Thread Prev][Thread Next][Minivend by date ][Minivend by thread ]

custom pricing routines using [perl] and [discount]



Hi All,

Thank you Christian and Betty for your help. I'm going to list my problem 
and non-working solution - perhaps someone's already invented this wheel. 
(I am running v3.14 on NT/IIS, and plan on serving it with v3.14 on 
Linux/Apache.)

Problem:

I would like to define quantity discounts to products based upon fractions 
and multiples of case quantities. For example, product MMS-0120 comes in 
cases of 12, so I'd like to offer breaks at 3, 6, 12, 24, 36, 48, 96, and 
540 (a full pallet). Product MMS-0180 comes in cases of 8, so I'd like to 
offer breaks at 4, 8, 16, 32, 64, and 240. As can be seen, I'd soon have a 
pricing.asc database with 100+ columns, with most pricing data just a copy 
of the cell to the left.

Partial solution:

Create a new database, prices.asc with two tab-delimited columns: code and 
prices. The code column contains the product code. The prices column 
contains all of the quantity pricing information, with quantity/price pairs 
comma-delimited, and the quantity and price pair colon-delimited. For 
example:

code              prices
MMS-0120     1:9.99,3:8.99,6:7.99,12:5.99
MMS-0180     1:14.99,4:13.99,8:9.99,16:8.99,32:8.49,64:7.99,240:5.99

The perl code I've managed to hack together creates a scalar which contains 
the perl code which should work (I think) within the [discount][/discount] 
tagset:
my $returnprice;
if ($q > 0) {
	$returnprice = "9.99";
}
if ($q > 2) {
	$returnprice = "8.99";
}
if ($q > 5) {
	$returnprice = "7.99";
}
if ($q > 11) {
	$returnprice = "5.99";
}
return $returnprice;

(Yes, I am aware that I should reverse the @pricepairs split and do 
elsif's, which I will do when I get this to work - that's how I had 
originally coded it, but I'm trying to reduce the problem to it's simplest 
form. As everyone can probably tell, I'm new to Perl, but I'm usually able 
to figure anything out through reading the books - Programming Perl and 
Perl Cookbook are sitting in front of me - and trial and error. In fact, as 
I spent 30 minutes writing and editing this email I must have tried at 
least 20 additional variations that came to mind.)

Here is the code on my flypage.html:
[set name=PricingData interpolate=1][data base="prices" field="prices" 
key="[item-code]"][/set]
[scratch PricingData]
[perl interpolate=1]
	my $numitems;
	my $numprice;
	my $pricepair;
	my $pricingdata = '[scratch PricingData]';
	my $debugstring = $pricingdata;
	my @pricepairs = split /,/ , $pricingdata;
	my $displaystring = "";
	my $pricestring = "my \$returnprice;\n";
	foreach $pricepair (@pricepairs) {
		($numitems, $numprice) = split /:/, $pricepair;
		$displaystring = $numitems . "\@" . "\$" . $numprice . " each; " . 
$displaystring;
		--$numitems;
		$pricestring .= "if (\$q > $numitems) {\n \$returnprice = 
\"$numprice\";\n } \n";
	}
	$pricestring .= "return \$returnprice; \n";
	$Safe{'scratch'}->{'DisplayString'} = $displaystring;
	$Safe{'scratch'}->{'DiscountFormula'} = $pricestring;
	return $pricestring . "::::" . $debugstring;
[/perl]
[discount [item-code]][scratch DiscountFormula][/discount]
DF:[scratch DiscountFormula]

The final return (within [perl][/perl] shows $pricestring and $debugstring 
exactly as expected - the formula (although the \n's seem to be stripped 
out) and the pricing data from prices.asc.
The DF: shows blank
I also tried setting the discount directly within the [perl][/perl] tags, 
both of the following:
	$Safe{'session'}->{'discount'}->{{$item}->[item-code]} = $pricestring;
and
	$Safe{'session'}->{'discount'}->{{item-code]} = $pricestring;
- I picked these out of Interpolate.pm

This is what I get in the MiniVend error.log when I load the flypage:
Discount for MMS-0120 has bad formula. Not applied.
Can't locate object method "scratch" via package "PricingData" at (eval 
292) line 5.

Discount for MMS-0120 has bad formula. Not applied.
Can't locate object method "scratch" via package "PricingData" at (eval 
294) line 5.

Discount for MMS-0120 has bad formula. Not applied.
Can't locate object method "scratch" via package "PricingData" at (eval 
296) line 5.

Discount for MMS-0120 has bad formula. Not applied.
Can't locate object method "scratch" via package "PricingData" at (eval 
298) line 5.

Any and all help, comments, and suggestions greatly appreciated!

Thank you,
Tim


Search for: Match: Format: Sort by: