X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fcust_main%2FBilling.pm;h=0376fc857653853632b4c6db0e30e781cd55e58f;hb=2322f0123062b8cf2f52aed0a63bb4634cdbaef1;hp=bdb4520cff16cd801354254bda230bbd96136e26;hpb=3f9be57a4e888ffc2affe517025553c6c2f90265;p=freeside.git diff --git a/FS/FS/cust_main/Billing.pm b/FS/FS/cust_main/Billing.pm index bdb4520cf..0376fc857 100644 --- a/FS/FS/cust_main/Billing.pm +++ b/FS/FS/cust_main/Billing.pm @@ -570,7 +570,10 @@ sub bill { my $object = $event_fee->cust_event->cust_X; my $part_fee = $event_fee->part_fee; my $cust_bill; - if ( $object->isa('FS::cust_main') or $object->isa('FS::cust_pkg') ) { + if ( $object->isa('FS::cust_main') + or $object->isa('FS::cust_pkg') + or $object->isa('FS::cust_pay_batch') ) + { # Not the real cust_bill object that will be inserted--in particular # there are no taxes yet. If you want to charge a fee on the total # invoice amount including taxes, you have to put the fee on the next @@ -627,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; @@ -726,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 @@ -1114,10 +1120,16 @@ sub _make_lines { my @recur_discounts = (); my $sdate; if ( ! $cust_pkg->start_date - and ( ! $cust_pkg->susp || $cust_pkg->option('suspend_bill',1) - || ( $part_pkg->option('suspend_bill', 1) ) - && ! $cust_pkg->option('no_suspend_bill',1) - ) + and + ( ! $cust_pkg->susp + || ( $cust_pkg->susp != $cust_pkg->order_date + && ( $cust_pkg->option('suspend_bill',1) + || ( $part_pkg->option('suspend_bill', 1) + && ! $cust_pkg->option('no_suspend_bill',1) + ) + ) + ) + ) and ( $part_pkg->freq ne '0' && ( $cust_pkg->bill || 0 ) <= $cmp_time ) || ( $part_pkg->plan eq 'voip_cdr' @@ -1325,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( @@ -1445,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 @@ -1479,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 : '' ) @@ -1756,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 = '( ' @@ -2069,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'}, ); @@ -2108,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'} @@ -2392,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