[ic] db access in commonadjust-called usertag - endless problems

Stefan Hornburg racke at linuxia.de
Wed Oct 6 02:55:25 EDT 2004


On Fri, 01 Oct 2004 17:03:48 +0300
Kaarel Jogi <kaarel at hiiumaa.ee> wrote:

> Hi folks!
> 
> Situation: I need to calculate the item price from number of different
> fields in products, depending on user profile.  So if the profile is
> profile_a, the price would be
> (products.price_a * products.amount).
> 
> Additionally i need to check second table for existence of possible
> "this user special" price, which should be used instead the ones in
> products db then.
> 
> I guess this is not possible directly with commonadjust functions. So i
> defined a usertag to be passed sku and which would look up right place
> from databases.  And i just cant get it working right, after several
> days on mailinglists and irc :(

I'm sure that I posted more than once how this can be done, here
we go again:

CommonAdjust [calc_price]

UserTag calc_price Order code quantity
UserTag calc_price AddAttr
UserTag calc_price Routine <<EOF
sub {
	my ($code, $quantity, $opt) = @_;
	my ($set, $price, @list);

	if ($Vend::Interpolate::item) {
		$code = $Vend::Interpolate::item->{code};
		$quantity = $Vend::Interpolate::item->{quantity};
	}

	# allow invocation with just the item code
	unless (defined $quantity) {
		$quantity = 1;
	}
	
	$Tag->perl({tables => 'Artikel Kundenpreise Gruppenpreise'});

	unless ($opt->{regular}) {
		# check for special offers
		my ($from, $to, $special_price) = $Db{Artikel}->get_slice($code, [qw(AktionVon AktionBis AktionNetto)]);
		my $date = $Tag->time({format => '%Y-%m-%d'});
	
		if ($special_price && $from le $date && $to ge $date) {
			return $special_price;
		}
	}
	
	$set = $Db{Kundenpreise}->query(qq{select Preis from Kundenpreise where ArtikelID = $code and AdressenID = $Session->{username}});
	if (@$set) {
		# price specific for this customer
		return $set->[0]->[0];
	}
	$set = $Db{Gruppenpreise}->query(qq{select vkNetto,Ab from Gruppenpreise where ArtikelID = $code and PreisgruppenID = $Scratch->{Preisgruppe} order by Ab desc});

	if ($opt->{list}) {
		return join(',', map {"$_->[1]=$_->[0]"} (reverse @$set));
	} else {
		for (@$set) {
			if ($quantity >= $_->[1]) {
				return $_->[0];
			}
		}
	}
}
EOF

> 
> Perhaps someone has a confirmed, working example of commonadjust-called
> usertag that accesses products?
> 

Couple of them.


Ciao
	Racke


-- 
LinuXia Systems => http://www.linuxia.de/
Expert Interchange Consulting and System Administration
ICDEVGROUP => http://www.icdevgroup.org/
Interchange Development Team



More information about the interchange-users mailing list