X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fcust_main%2FBilling.pm;h=0376fc857653853632b4c6db0e30e781cd55e58f;hb=2322f0123062b8cf2f52aed0a63bb4634cdbaef1;hp=fff0cb0763eb023cb2cadde126040fc9bd1fc116;hpb=729956733bf21293c801358c3e711c81d7df5b4f;p=freeside.git diff --git a/FS/FS/cust_main/Billing.pm b/FS/FS/cust_main/Billing.pm index fff0cb076..0376fc857 100644 --- a/FS/FS/cust_main/Billing.pm +++ b/FS/FS/cust_main/Billing.pm @@ -630,6 +630,7 @@ sub bill { $taxlisthash{$pass}, $fee_item, location => $fee_location + # probably not right to pass cancel => 1 for fees ); return $error if $error; @@ -729,14 +730,16 @@ sub bill { my $charged = sprintf('%.2f', ${ $total_setup{$pass} } + ${ $total_recur{$pass} } ); - my @cust_bill = $self->cust_bill; my $balance = $self->balance; - my $previous_bill = $cust_bill[-1] if @cust_bill; - my $previous_balance = 0; - if ( $previous_bill ) { - $previous_balance = $previous_bill->billing_balance - + $previous_bill->charged; - } + + my $previous_bill = qsearchs({ 'table' => 'cust_bill', + 'hashref' => { custnum=>$self->custnum }, + 'extra_sql' => 'ORDER BY _date DESC LIMIT 1', + }); + my $previous_balance = + $previous_bill + ? ( $previous_bill->billing_balance + $previous_bill->charged ) + : 0; warn "creating the new invoice\n" if $DEBUG; #create the new invoice @@ -1334,7 +1337,8 @@ sub _make_lines { # handle taxes ### - my $error = $self->_handle_taxes( $taxlisthash, $cust_bill_pkg ); + my $error = $self->_handle_taxes( $taxlisthash, $cust_bill_pkg, + cancel => $options{cancel} ); return $error if $error; $cust_bill_pkg->set_display( @@ -1454,6 +1458,8 @@ OPTIONS may include: - part_item: a part_pkg or part_fee object to be used as the package/fee definition. - location: a cust_location to be used as the billing location. +- cancel: true if this package is being billed on cancellation. This + allows tax to be calculated on usage charges only. If not supplied, part_item will be inferred from the pkgnum or feepart of the cust_bill_pkg, and location from the pkgnum (or, for fees, the invnum and @@ -1488,10 +1494,9 @@ sub _handle_taxes { my %taxes = (); my @classes; - #push @classes, $cust_bill_pkg->usage_classes if $cust_bill_pkg->type eq 'U'; push @classes, $cust_bill_pkg->usage_classes if $cust_bill_pkg->usage; - push @classes, 'setup' if $cust_bill_pkg->setup; - push @classes, 'recur' if $cust_bill_pkg->recur; + push @classes, 'setup' if $cust_bill_pkg->setup and !$options{cancel}; + push @classes, 'recur' if $cust_bill_pkg->recur and !$options{cancel}; my $exempt = $conf->exists('cust_class-tax_exempt') ? ( $self->cust_class ? $self->cust_class->tax : '' ) @@ -1765,7 +1770,8 @@ sub retry_realtime { #a little false laziness w/due_cust_event (not too bad, really) - my $join = FS::part_event_condition->join_conditions_sql; + # I guess this is always as of now? + my $join = FS::part_event_condition->join_conditions_sql('', 'time' => time); my $order = FS::part_event_condition->order_conditions_sql; my $mine = '( ' @@ -2078,7 +2084,8 @@ sub due_cust_event { #some false laziness w/Cron::bill bill_where - my $join = FS::part_event_condition->join_conditions_sql( $eventtable); + my $join = FS::part_event_condition->join_conditions_sql( $eventtable, + 'time' => $opt{'time'}); my $where = FS::part_event_condition->where_conditions_sql($eventtable, 'time'=>$opt{'time'}, ); @@ -2117,7 +2124,8 @@ sub due_cust_event { my $pkey = $object->primary_key; $cross_where = "$eventtable.$pkey = ". $object->$pkey(); - my $join = FS::part_event_condition->join_conditions_sql( $eventtable ); + my $join = FS::part_event_condition->join_conditions_sql( $eventtable, + 'time' => $opt{'time'}); my $extra_sql = FS::part_event_condition->where_conditions_sql( $eventtable, 'time'=>$opt{'time'} @@ -2401,13 +2409,9 @@ sub apply_payments { #return 0 unless - my @payments = sort { $b->_date <=> $a->_date } - grep { $_->unapplied > 0 } - $self->cust_pay; + my @payments = $self->unapplied_cust_pay; - my @invoices = sort { $a->_date <=> $b->_date} - grep { $_->owed > 0 } - $self->cust_bill; + my @invoices = $self->open_cust_bill; if ( $conf->exists('pkg-balances') ) { # limit @payments to those w/ a pkgnum grepped from $self