[ic] Calculating saletax for individual items

Mike Heins interchange-users@icdevgroup.org
Fri May 2 22:30:01 2003


Quoting Jamie Neil (jamie@versado.net):
 >
> > I have setup 4.9.6 to use UK VAT. My country tax setup and item's
> > tax_category are been correctly used when the cart calculates the total
> > salestax for all items in the cart.
> >
> > But I would like to know what the salestax value is for each
> > *individual* item rather than just for the whole cart (as displayed by
> > [salestax]). Basically, I want to modify the flypage and search results
> > to display the full price including tax. So my question is: Is there a
> > built-in way to access the applicable tax for an individual item? Or do
> > I have to write a UserTag [price-with-tax] that does the lookup using
> > tax_category and parse the tax field from the country table?
> 
> Andy,
> 
> I wanted to do the same thing, but found no built in mechanism for
> displaying prices with tax. So I wrote a usertag.
> 
> This is probably not the best or most efficient way of doing this (as
> several better coders will no doubt point out soon enough). The function of
> the tag is to add tax at the correct rate to any supplied figure. This means
> it can be wrapped around the price, subtotal and other tags as required. It
> is only designed to work with tax type "multi" where tax categories are
> stored in the country table, but could be adapted to support the other
> systems.
> 
> Syntax: [addtax amount tax_category nontaxable noformat] [addtax
> amount="amount" cat="tax_category" notax="nontaxable" noformat="noformat"]
> 
> amount - unformatted price
> cat - tax category (from country table)
> notax - whether the amount should be taxed or not (0|1)
> noformat - returns unformated figure (0|1)
> 
> Example usage:
> 
> [addtax [item-price noformat=1][item-field tax_category][item-field
> nontaxable]]
> 
> addtax.tag
> ----------
> 
> UserTag addtax              Order        amount cat notax
> UserTag addtax              addAttr
> UserTag addtax              Routine   <<EOR
> sub {
>         my ($amount, $cat, $notax, $ref) = @_;
> ::logDebug("amount=$amount, cat=$cat, notax=$notax, ref=$ref");
>         return currency( $amount, $ref->{noformat} ) if $notax;
>         my $cfield = $::Variable->{MV_COUNTRY_FIELD} || 'country';
>         my $country = $ref->{country} || $::Values->{$cfield};
>         return 0 if ! $country;
>         my $ctable   = $ref->{country_table}
>                                 || $::Variable->{MV_COUNTRY_TABLE}
>                                 || 'country';
>         my $c_taxfield   = $ref->{country_tax_field}
>                                 || $::Variable->{MV_COUNTRY_TAX_FIELD}
>                                 || 'tax';
> #::logDebug("ctable=$ctable c_taxfield=$c_taxfield country=$country");
>         my $type ||= $Tag->data($ctable, $c_taxfield, $country);
>         return currency( $amount, $ref->{noformat} ) if ! $type;
> #::logDebug("tax type=$type");
>         $type =~ s/^\s+//;
>         $type =~ s/\s+$//;
>         my $tax = Vend::Util::get_option_hash($type);
> #::logDebug("tax hash=" . uneval($tax));
>         my $taxrate = defined $tax->{$cat} ? $tax->{$cat} : $tax->{default};
> #::logDebug("cat=$cat rate=$taxrate");
>         $taxrate =~ s/\s*%\s*$// and $taxrate /= 100;
>         $amount += $amount * $taxrate;
> #::logDebug("amount=$amount");
>         return currency( $amount, $ref->{noformat} );
> }
> EOR
> 

Looks perfect to me. I like your coding style! 8-)

-- 
Mike Heins
Perusion -- Expert Interchange Consulting    http://www.perusion.com/
phone +1.513.523.7621      <mike@perusion.com>

Fast, reliable, cheap.  Pick two and we'll talk.  -- unknown