[ic] How to calculate multiple Tax of any items?

Joachim Leidinger interchange-users@interchange.redhat.com
Thu Dec 13 17:22:01 2001


Joachim Leidinger wrote:
> > > What i want is
> > >
> > > sku    title    price
> > > 1      beer     10,00 EUR (with 16% tax = 1,60 EUR)
> > > 2      water    10,00 EUR (with  7% tax = 0,70 EUR)
> > > subtotal = 20,00 EUR
> > > shipping =  5,00 EUR (with 16% tax = 0,80 EUR)
> > > total    = 25,00 EUR
> > > total incl. salestax =  3,10 EUR
> > >
> > > Any ideas?
> > > Any helps, suggestions and tips are very welcome!
> > >
> >
> > Search the mail archive for "tax_category" and my name. I posted
> > something which is perfect for this.

Dear Mike,

I apologize for my bad mental capability of perl to understand your
usertag with tax_category. But I was able to write my own usertag.
Please look at the codes below. It is not big. Did you see any possible
problems with my usertag? If no, I will share the usertag compute_vat
with you. You can use it and fit it for other german IC users like me,
if you wish!

Thanks!

Joachim

I've in my catalog.cfg

--------- snip ---------
###
#
# for the UserTag compute_vat
#
###
# the name of the tax field in your products file
Variable    COMPUTE_VAT_VATFIELD       tax
# is you price net or gross?
# or Net or net or Netto or netto, otherwise gross
Variable    COMPUTE_VAT_PRICEMODE      gross
# the name of your price field in your products file
Variable    COMPUTE_VAT_PRICEFIELD     price
# is your shipping net or gross?
# or Net or net or Netto or netto, otherwise gross    
Variable    COMPUTE_VAT_SHIPMODE       gross
# the default amount of your tax or vat for a fallback call, 
# if there are no any tax or vat in your tax field
Variable    COMPUTE_VAT_DEFAULT        16
--------- snip ---------

and an UserTag

--------- compute_vat.tag ----------
UserTag compute_vat Order debug
UserTag compute_vat PosNumber 1
UserTag compute_vat Routine <<EOF
sub {
	my ($debug) = @_;
        my $compute_shipping = 0;
        my $vatfield = $::Variable->{COMPUTE_VAT_VATFIELD};
        my $pricemode = $::Variable->{COMPUTE_VAT_PRICEMODE};
        my $pricefield = $::Variable->{COMPUTE_VAT_PRICEFIELD};
	my $shipmode = $::Variable->{COMPUTE_VAT_SHIPMODE};
	my $vat_default = $::Variable->{COMPUTE_VAT_DEFAULT};
# only for debug with [compute_vat 1] ! Without debug mode use
[compute_vat]
        my $out = "vatfield = $vatfield <BR> pricemode = $pricemode <BR>
pricefield = $pricefield <BR> shipmode = $shipmode <BR> vatdefault =
$vat_default <BR>";
        my $vat = 0;
	my $shippingvat = 0;
	my $vatdivide = 0;
	my $vatrate = 0;
        my $outprice = 0;
        my $subtotal = 0;
        my $compute_summvat = 0;
        my $compute_total_cost = 0;

	$::Values->{mv_handling} = '';
# get the value of the shipping
	$compute_shipping += shipping($::Values->{mv_shipmode});
	foreach my $item (@$Vend::Items) {
		for(@{$Vend::Cfg->{ProductFiles}}) {
			unless ($Vend::Database{$_}) {
			  die "$_ not a database, cannot use as products file\n";
			}
                        # only for debug
                        $out .= "database = $_ <BR>";
			$vatrate = tag_data($_, $vatfield, $item->{code});
			$outprice = tag_data($_, $pricefield, $item->{code});
                        last if($outprice);
		}
                if (!$vatrate){$vatrate = ($vat_default || 16);}
                if ($pricemode =~ /N/ || $pricemode =~ /n/){
			$vatdivide = $vatrate / 100;
		} else { 
			$vatdivide = 1 / (100 + $vatrate) * $vatrate;
                }
		$vat += ($outprice * $item->{quantity}) * $vatdivide;
                $subtotal += $outprice * $item->{quantity};
        }
        # you can use [scratch compute_vat_vat] for special case in your
page
        # it is a sum of tax/vat of each items
	$Scratch->{'compute_vat_vat'} = $vat;
        if ($shipmode =~ /N/ || $shipmode =~ /n/){
                # you can use [scratch compute_vat_shippin] for special
case in your page
                # it is a tax/vat of the shipping
		$Scratch->{'compute__vat_shipping'} = $compute_shipping * $vat_default
/ 100;
	} else {
		$Scratch->{'compute_vat_shipping'} = $compute_shipping / (100 +
$vat_default) * $vat_default;
	}
        # for some special case, you can use [scratch
compute_vat_summvat] in your page
        # it is a sum of "tax/vat of shippin" and "tax/vat of items"
        $Scratch->{'compute_vat_summvat'} =
$Scratch->{'compute_vat_shipping'} + $vat;

if ($pricemode =~ /N/ || $pricemode =~ /n/){
# the price is net
	$::Values->{mv_handling} = 1; 
	$Vend::Session->{assigned}{shipping} = $compute_shipping;
	$Vend::Session->{assigned}{salestax} =
$Scratch->{'compute_vat_summvat'};
	$compute_total_cost = $subtotal + $compute_shipping +
$Scratch->{'compute_vat_summvat'};
} else {
# the price is gross
	$::Values->{mv_handling} = 1; 
	$Vend::Session->{assigned}{shipping} = $compute_shipping;
	$Vend::Session->{assigned}{salestax} =
$Scratch->{'compute_vat_summvat'};
	$compute_total_cost = $subtotal + $compute_shipping;
}
if($debug){
return "test( $debug  ):<BR> $out <BR> subtotal = $subtotal <BR>
shipping = $compute_shipping <BR> compute_vat_vat =
$Scratch->{'compute_vat_vat'} <BR>  compute_vat_shipping =
$Scratch->{'compute_vat_shipping'} <BR> compute_vat_summvat =
$Scratch->{'compute_vat_summvat'}<BR><BR>$compute_total_cost";
} else { return Vend::Util::currency($compute_total_cost);}

}
EOF
--------- compute_vat.tag ----------

In my checkout, I've replaced the [total-cost] tag with [compute_vat]
and I'm able to get the right tax/vat (sum of different tax of each
items plus tax/vat of the shipping), shipping and salestax. I'm able to
use it with prices, who are net or gross.




-- 
Hans-Joachim Leidinger | Dipl.-Phys.Ing. Entwicklung eCommerce
[Hans-Joachim.leidinger@bpanet.de]
Black Point Arts Internet Solutions GmbH
http://www.bpanet.de