Revision 205 (by ahitrov, 2012/04/06 11:27:43) Delivery price differs according to order sum
<%once>

   use JSON::XS;
   my $json = JSON::XS->new;

</%once>
<%args>

	$name	=> undef
	$SETS	=> undef

</%args>
<%init>

    return undef	if (! ref($SETS));
    return undef	if (! $name);

    my $FIELDS = {};
    while (my ($element,$value) = each %{$SETS}) {
	if ( $element =~ /^${name}_level_(\d+)$/ ) {
		my $index = $1;
		$value =~ s/^\s+//s;
		$value =~ s/\s+$//s;
		if ( defined $value && $value =~ /^\d+$/ ) {
			$FIELDS->{$index}{level} = $value;
		}
	} elsif ( $element =~ /^${name}_cost_(\d+)$/ ) {
		my $index = $1;
		$value =~ s/^\s+//s;
		$value =~ s/\s+$//s;
		if ( defined $value && ($value =~ /^\d+$/ || $value =~ /^\d+[\.\,]\d+$/ || $value =~ /^\d+%$/) ) {
			$value =~ s/\,/\./;
			$FIELDS->{$index}{cost} = $value;
		}
	}
    }
    my @FIELDS = sort { int($a->{level}) <=> int($b->{level}) } grep { $_->{level} && defined $_->{cost} } values %$FIELDS;
    return $json->encode(\@FIELDS);

</%init>