SOLVED! Re: [ic] calculating prices with variable quantity-pricings

Steffen Dettmer interchange-users@interchange.redhat.com
Mon Dec 3 16:25:00 2001


[If you found this while searching the archives, see at the end
for some hints! Since I found no example, I created a simple one
and even tested it :) ]

* Mike Heins wrote on Mon, Dec 03, 2001 at 14:43 -0500:
> Quoting Steffen Dettmer (steffen@dett.de):
> > =item & CODE
> >  [...] 
> > CommonAdjust & $Tag->my_price_tag( $item->{code}, $item->{'quantity'} )

Ohh, so there is the parameter $item->{'quantity'} passed.

> It needs to be a quoted string

Yes, urghhh... Thank you very much! Sometimes it's really
depressing how simple solutions there are, but what I'm unable to
find...

Now I have still a minor problem. The decimal digits are not
used. My Tag returns i.e. "6.49". I need "6,49". This is a
locale-issue? When I run perl from commandline, $a="6.49" + 0.0 is
6.49, but from IC I need $a="6,49" + 0.0 to get the working
result. For now, I hacked: 
$price =~ s/\./,/;
Is there a better way to do it? I read about a numeric parameter
for databases in catalog.cfg, does this return a converted result
from the DB? If I use [data] or works this even in [query]? 

Thank you very much to save me another 3 hours :) !

oki,

Steffen



Some hints for the archives:

- Yes, DO quote the & CODE parameter in CommonAdjust
- Yes, DO comment out CommonAdjust in etc/after.cfg 
- Yes, DO set "PriceField      0"
- Yes, CommonAdjust returns the price for one item
- Yes, you can use $Tag and friends
- For testing, you may use:

=====8<-----------------------------------------------------
PriceField      0
CommonAdjust "& $Tag->std_paralog( $item->{code}, $item->{quantity} )"

UserTag std_paralog Order one two three
UserTag std_paralog Routine <<EOR
sub
{
        my @para = @_;
        $Tag->log( { file => '/tmp/iclog'}, "Paralog: " . join("|", @_) );
}
EOR
---------------------------------------------------->8======

- An simple CommonAdjust Perl CODE example:

=====8<-----------------------------------------------------
PriceField      0
CommonAdjust "& $Tag->std_simple_price( $item->{code}, $item->{quantity} )"

UserTag std_simple_price Order sku quantity unwanted_rest
UserTag std_simple_price Routine <<EOR
sub
{
        my ($sku, $quantity, @rest) = @_;
        my $constant_global_price_adjust = 0.95; #5% off today!
        if ($quantity >= 1000) {
                #if yuo buy more than 1000, you get them for the half price!
                $constant_global_price_adjust = 0.5;
        }
        my $price = $Tag->data( {
                table => 'products',
                field => 'price',
                key => $sku,
        });
	#I need to uncomment the following line:
        #$price =~ s/\./,/;
	#just for debug:
        $Tag->std_paralog("sku: $sku, quantity: $quantity, off: "
                . $constant_global_price_adjust
                . " products::price: $price");
        if ($price ne $price + 0.0) {
                $Tag->std_paralog("locale error: $price " , $price+0.0);
                return 0;
        }
        $price = $price * $constant_global_price_adjust;
	#just for debug:
        $Tag->std_paralog("will return price: ", $price+0);
        return $price;
}
EOR
---------------------------------------------------->8======

- Take care about locale float handling. The interchange database
  tables default to varchar() fields, and even if you use float
  a database may return "123.56" as float. To make sure perl
  interpretes it like desired, try + 0.0, like:
  ...
  $Tag->std_paralog("price == '$price' == " , $price + 0.0);
  ...
  If in the logfile the left and right sides of "==" are not
  equal, you may have to same problem like me ;)



If somebody needs an example of product-specific quantity prices,
drop me a note, I'll post what I did.



oki,

Steffen

-- 
Dieses Schreiben wurde maschinell erstellt,
es trägt daher weder Unterschrift noch Siegel.