X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fcust_main_county.pm;h=a61d67e11508fe68b1665ff5300594254b2631e2;hb=63973c641c4be00765fa27e55c57cc5b9aa4da19;hp=87c1ca730addb5c46a33f349621512e5c164b72a;hpb=a72a10f754f7465121d6137bb3dcee0a21ea6443;p=freeside.git diff --git a/FS/FS/cust_main_county.pm b/FS/FS/cust_main_county.pm index 87c1ca730..a61d67e11 100644 --- a/FS/FS/cust_main_county.pm +++ b/FS/FS/cust_main_county.pm @@ -137,33 +137,6 @@ sub check { } -sub taxname { - my $self = shift; - if ( $self->dbdef_table->column('taxname') ) { - return $self->setfield('taxname', $_[0]) if @_; - return $self->getfield('taxname'); - } - return ''; -} - -sub setuptax { - my $self = shift; - if ( $self->dbdef_table->column('setuptax') ) { - return $self->setfield('setuptax', $_[0]) if @_; - return $self->getfield('setuptax'); - } - return ''; -} - -sub recurtax { - my $self = shift; - if ( $self->dbdef_table->column('recurtax') ) { - return $self->setfield('recurtax', $_[0]) if @_; - return $self->getfield('recurtax'); - } - return ''; -} - =item label OPTIONS Returns a label looking like "Anytown, Alameda County, CA, US". @@ -174,13 +147,10 @@ If the taxname field is set, it will look like If the taxclass is set, then it will be "Anytown, Alameda County, CA, US (International)". -Currently it will not contain the district, even if the city+county+state -is not unique. - -OPTIONS may contain "no_taxclass" (hides taxclass) and/or "no_city" -(hides city). It may also contain "out", in which case, if this -region (district+city+county+state+country) contains no non-zero -taxes, the label will read "Out of taxable region(s)". +OPTIONS may contain "with_taxclass", "with_city", and "with_district" to show +those fields. It may also contain "out", in which case, if this region +(district+city+county+state+country) contains no non-zero taxes, the label +will read "Out of taxable region(s)". =cut @@ -202,12 +172,15 @@ sub label { my $label = $self->country; $label = $self->state.", $label" if $self->state; $label = $self->county." County, $label" if $self->county; - if (!$opt{no_city}) { + if ($opt{with_city}) { $label = $self->city.", $label" if $self->city; + if ($opt{with_district} and $self->district) { + $label = $self->district . ", $label"; + } } # ugly labels when taxclass and taxname are both non-null... # but this is how the tax report does it - if (!$opt{no_taxclass}) { + if ($opt{with_taxclass}) { $label = "$label (".$self->taxclass.')' if $self->taxclass; } $label = $self->taxname." ($label)" if $self->taxname; @@ -258,10 +231,15 @@ sub _list_sql { =item taxline TAXABLES_ARRAYREF, [ OPTION => VALUE ... ] -Returns an hashref of a name and an amount of tax calculated for the -line items (L objects) in TAXABLES_ARRAYREF. The line -items must come from the same invoice. Returns a scalar error message -on error. +Takes an arrayref of L objects representing taxable +line items, and returns a new L object representing +the tax on them under this tax rate. + +This will have a pseudo-field, "cust_bill_pkg_tax_location", containing +an arrayref of L objects. Each of these +will in turn have a "taxable_cust_bill_pkg" pseudo-field linking it to one +of the taxable items. All of these links must be resolved as the objects +are inserted. In addition to calculating the tax for the line items, this will calculate any appropriate tax exemptions and attach them to the line items. @@ -275,8 +253,7 @@ tax exemption limit if there is one. =cut -# XXX this should just return a cust_bill_pkg object for the tax, -# but that requires changing stuff in tax_rate.pm also. +# XXX change tax_rate.pm to work like this sub taxline { my( $self, $taxables, %opt ) = @_; @@ -294,7 +271,8 @@ sub taxline { my $dbh = dbh; my $name = $self->taxname || 'Tax'; - my $amount = 0; + my $taxable_cents = 0; + my $tax_cents = 0; my $cust_bill = $taxables->[0]->cust_bill; my $custnum = $cust_bill ? $cust_bill->custnum : $opt{'custnum'}; @@ -325,6 +303,15 @@ sub taxline { push @existing_exemptions, @{ $_->cust_tax_exempt_pkg } for @$taxables; + my $tax_item = FS::cust_bill_pkg->new({ + 'pkgnum' => 0, + 'recur' => 0, + 'sdate' => '', + 'edate' => '', + 'itemdesc' => $name, + }); + my @tax_location; + foreach my $cust_bill_pkg (@$taxables) { my $cust_pkg = $cust_bill_pkg->cust_pkg; @@ -472,37 +459,51 @@ sub taxline { $_->taxnum($self->taxnum) foreach @new_exemptions; - #if ( $cust_bill_pkg->billpkgnum ) { - - #no, need to do this to e.g. calculate tax credit amounts - #die "tried to calculate tax exemptions on a previously billed line item\n"; - - # this is unnecessary -# foreach my $cust_tax_exempt_pkg (@new_exemptions) { -# my $error = $cust_tax_exempt_pkg->insert; -# if ( $error ) { -# $dbh->rollback if $oldAutoCommit; -# return "can't insert cust_tax_exempt_pkg: $error"; -# } -# } - #} - # attach them to the line item push @{ $cust_bill_pkg->cust_tax_exempt_pkg }, @new_exemptions; push @existing_exemptions, @new_exemptions; - # If we were smart, we'd also generate a cust_bill_pkg_tax_location - # record at this point, but that would require redesigning more stuff. $taxable_charged = sprintf( "%.2f", $taxable_charged); - - $amount += $taxable_charged * $self->tax / 100; + next if $taxable_charged == 0; + + my $this_tax_cents = int($taxable_charged * $self->tax); + my $location = FS::cust_bill_pkg_tax_location->new({ + 'taxnum' => $self->taxnum, + 'taxtype' => ref($self), + 'cents' => $this_tax_cents, + 'pkgnum' => $cust_bill_pkg->pkgnum, + 'locationnum' => $cust_bill_pkg->cust_pkg->tax_locationnum, + 'taxable_cust_bill_pkg' => $cust_bill_pkg, + 'tax_cust_bill_pkg' => $tax_item, + }); + push @tax_location, $location; + + $taxable_cents += $taxable_charged; + $tax_cents += $this_tax_cents; } #foreach $cust_bill_pkg - - return { - 'name' => $name, - 'amount' => $amount, - }; - + + # now round and distribute + my $extra_cents = sprintf('%.2f', $taxable_cents * $self->tax / 100) * 100 + - $tax_cents; + # make sure we have an integer + $extra_cents = sprintf('%.0f', $extra_cents); + if ( $extra_cents < 0 ) { + die "nonsense extra_cents value $extra_cents"; + } + $tax_cents += $extra_cents; + my $i = 0; + foreach (@tax_location) { # can never require more than a single pass, yes? + my $cents = $_->get('cents'); + if ( $extra_cents > 0 ) { + $cents++; + $extra_cents--; + } + $_->set('amount', sprintf('%.2f', $cents/100)); + } + $tax_item->set('setup' => sprintf('%.2f', $tax_cents / 100)); + $tax_item->set('cust_bill_pkg_tax_location', \@tax_location); + + return $tax_item; } =back