X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fcust_main.pm;h=638036a15beb2b77f8ac4991715e5455ca9593ae;hb=357b4e26965895666685590d59f72de331fecb08;hp=cdfe938482362181c6a9258c31fa8f3dfce2698c;hpb=d9a702d507ac900c84a21b9336db3112da313ed8;p=freeside.git diff --git a/FS/FS/cust_main.pm b/FS/FS/cust_main.pm index cdfe93848..638036a15 100644 --- a/FS/FS/cust_main.pm +++ b/FS/FS/cust_main.pm @@ -2717,6 +2717,23 @@ sub bill { $tax = sprintf('%.2f', $tax ); $total_setup = sprintf('%.2f', $total_setup+$tax ); + my $pkg_category = qsearchs( 'pkg_category', { 'categoryname' => $taxname, + 'disabled' => '', + }, + ); + + my @display = (); + if ( $pkg_category and + $conf->config('invoice_latexsummary') || + $conf->config('invoice_htmlsummary') + ) + { + + my %hash = ( 'section' => $pkg_category->categoryname ); + push @display, new FS::cust_bill_pkg_display { type => 'S', %hash }; + + } + push @cust_bill_pkg, new FS::cust_bill_pkg { 'pkgnum' => 0, 'setup' => $tax, @@ -2724,6 +2741,7 @@ sub bill { 'sdate' => '', 'edate' => '', 'itemdesc' => $taxname, + 'display' => \@display, 'cust_bill_pkg_tax_location' => \@cust_bill_pkg_tax_location, 'cust_bill_pkg_tax_rate_location' => \@cust_bill_pkg_tax_rate_location, }; @@ -2761,11 +2779,24 @@ sub bill { my $charged = sprintf('%.2f', $total_setup + $total_recur ); + my @cust_bill = $self->cust_bill; + my $balance = $self->balance; + my $previous_balance = scalar(@cust_bill) + ? $cust_bill[$#cust_bill]->billing_balance + : 0; + + $previous_balance += $cust_bill[$#cust_bill]->charged + if scalar(@cust_bill); + #my $balance_adjustments = + # sprintf('%.2f', $balance - $prior_prior_balance - $prior_charged); + #create the new invoice my $cust_bill = new FS::cust_bill ( { - 'custnum' => $self->custnum, - '_date' => ( $invoice_time ), - 'charged' => $charged, + 'custnum' => $self->custnum, + '_date' => ( $invoice_time ), + 'charged' => $charged, + 'billing_balance' => $balance, + 'previous_balance' => $previous_balance, } ); $error = $cust_bill->insert; if ( $error ) { @@ -3005,7 +3036,7 @@ sub _make_lines { ### my $error = - $self->_handle_taxes($part_pkg, $taxlisthash, $cust_bill_pkg, $cust_pkg, $options{invoice_time}, $real_pkgpart); + $self->_handle_taxes($part_pkg, $taxlisthash, $cust_bill_pkg, $cust_pkg, $options{invoice_time}, $real_pkgpart, \%options); return $error if $error; push @$cust_bill_pkgs, $cust_bill_pkg; @@ -3026,6 +3057,7 @@ sub _handle_taxes { my $cust_pkg = shift; my $invoice_time = shift; my $real_pkgpart = shift; + my $options = shift; my %cust_bill_pkg = (); my %taxes = (); @@ -3033,8 +3065,8 @@ sub _handle_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 && !$options->{cancel}); + push @classes, 'recur' if ($cust_bill_pkg->recur && !$options->{cancel}); if ( $self->tax !~ /Y/i && $self->payby ne 'COMP' ) { @@ -3116,7 +3148,9 @@ sub _handle_taxes { } my @display = (); - if ( $conf->exists('separate_usage') || $cust_bill_pkg->hidden ) { + my $separate = $conf->exists('separate_usage'); + my $usage_mandate = $cust_pkg->part_pkg->option('usage_mandate', 'Hush!'); + if ( $separate || $cust_bill_pkg->hidden || $usage_mandate ) { my $temp_pkg = new FS::cust_pkg { pkgpart => $real_pkgpart }; my %hash = $cust_bill_pkg->hidden # maybe for all bill linked? @@ -3125,18 +3159,28 @@ sub _handle_taxes { my $section = $cust_pkg->part_pkg->option('usage_section', 'Hush!'); my $summary = $cust_pkg->part_pkg->option('summarize_usage', 'Hush!'); - push @display, new FS::cust_bill_pkg_display { type => 'S', %hash }; - push @display, new FS::cust_bill_pkg_display { type => 'R', %hash }; + if ( $separate ) { + push @display, new FS::cust_bill_pkg_display { type => 'S', %hash }; + push @display, new FS::cust_bill_pkg_display { type => 'R', %hash }; + } else { + push @display, new FS::cust_bill_pkg_display + { type => '', + %hash, + ( ( $usage_mandate ) ? ( 'summary' => 'Y' ) : () ), + }; + } - if ($section && $summary) { + if ($separate && $section && $summary) { push @display, new FS::cust_bill_pkg_display { type => 'U', summary => 'Y', %hash, }; + } + if ($usage_mandate || $section && $summary) { $hash{post_total} = 'Y'; } - $hash{section} = $section if $conf->exists('separate_usage'); + $hash{section} = $section if ($separate || $usage_mandate); push @display, new FS::cust_bill_pkg_display { type => 'U', %hash }; } @@ -6168,19 +6212,23 @@ sub batch_card { ''; } -=item apply_payments_and_credits +=item apply_payments_and_credits [ OPTION => VALUE ... ] Applies unapplied payments and credits. In most cases, this new method should be used in place of sequential apply_payments and apply_credits methods. +A hash of optional arguments may be passed. Currently "manual" is supported. +If true, a payment receipt is sent instead of a statement when +'payment_receipt_email' configuration option is set. + If there is an error, returns the error, otherwise returns false. =cut sub apply_payments_and_credits { - my $self = shift; + my( $self, %options ) = @_; local $SIG{HUP} = 'IGNORE'; local $SIG{INT} = 'IGNORE'; @@ -6196,7 +6244,7 @@ sub apply_payments_and_credits { $self->select_for_update; #mutex foreach my $cust_bill ( $self->open_cust_bill ) { - my $error = $cust_bill->apply_payments_and_credits; + my $error = $cust_bill->apply_payments_and_credits(%options); if ( $error ) { $dbh->rollback if $oldAutoCommit; return "Error applying: $error"; @@ -6305,19 +6353,24 @@ sub apply_credits { return $total_unapplied_credits; } -=item apply_payments +=item apply_payments [ OPTION => VALUE ... ] Applies (see L) unapplied payments (see L) to outstanding invoice balances in chronological order. #and returns the value of any remaining unapplied payments. +A hash of optional arguments may be passed. Currently "manual" is supported. +If true, a payment receipt is sent instead of a statement when +'payment_receipt_email' configuration option is set. + + Dies if there is an error. =cut sub apply_payments { - my $self = shift; + my( $self, %options ) = @_; local $SIG{HUP} = 'IGNORE'; local $SIG{INT} = 'IGNORE'; @@ -6381,7 +6434,7 @@ sub apply_payments { } ); $cust_bill_pay->pkgnum( $payment->pkgnum ) if $conf->exists('pkg-balances') && $payment->pkgnum; - my $error = $cust_bill_pay->insert; + my $error = $cust_bill_pay->insert(%options); if ( $error ) { $dbh->rollback or die $dbh->errstr if $oldAutoCommit; die $error; @@ -6951,6 +7004,24 @@ sub invoicing_list_emailonly_scalar { join(', ', $self->invoicing_list_emailonly); } +=item referral_custnum_cust_main + +Returns the customer who referred this customer (or the empty string, if +this customer was not referred). + +Note the difference with referral_cust_main method: This method, +referral_custnum_cust_main returns the single customer (if any) who referred +this customer, while referral_cust_main returns an array of customers referred +BY this customer. + +=cut + +sub referral_custnum_cust_main { + my $self = shift; + return '' unless $self->referral_custnum; + qsearchs('cust_main', { 'custnum' => $self->referral_custnum } ); +} + =item referral_cust_main [ DEPTH [ EXCLUDE_HASHREF ] ] Returns an array of customers referred by this customer (referral_custnum set @@ -6958,6 +7029,11 @@ to this custnum). If DEPTH is given, recurses up to the given depth, returning customers referred by customers referred by this customer and so on, inclusive. The default behavior is DEPTH 1 (no recursion). +Note the difference with referral_custnum_cust_main method: This method, +referral_cust_main, returns an array of customers referred BY this customer, +while referral_custnum_cust_main returns the single customer (if any) who +referred this customer. + =cut sub referral_cust_main { @@ -7301,10 +7377,26 @@ Returns all the payments (see L) for this customer. sub cust_pay { my $self = shift; + return $self->num_cust_pay unless wantarray; sort { $a->_date <=> $b->_date } qsearch( 'cust_pay', { 'custnum' => $self->custnum } ) } +=item num_cust_pay + +Returns the number of payments (see L) for this customer. Also +called automatically when the cust_pay method is used in a scalar context. + +=cut + +sub num_cust_pay { + my $self = shift; + my $sql = "SELECT COUNT(*) FROM cust_pay WHERE custnum = ?"; + my $sth = dbh->prepare($sql) or die dbh->errstr; + $sth->execute($self->custnum) or die $sth->errstr; + $sth->fetchrow_arrayref->[0]; +} + =item cust_pay_pkgnum Returns all the payments (see L) for this customer's specific @@ -8203,6 +8295,9 @@ sub email_search_sql { my $job = delete $params->{'job'}; + $params->{'payby'} = [ split(/\0/, $params->{'payby'}) ] + unless ref($params->{'payby'}); + my $sql_query = $class->search_sql($params); my $count_query = delete($sql_query->{'count_query'}); @@ -8264,6 +8359,9 @@ sub process_email_search_sql { $param->{'job'} = $job; + $param->{'payby'} = [ split(/\0/, $param->{'payby'}) ] + unless ref($param->{'payby'}); + my $error = FS::cust_main->email_search_sql( $param ); die $error if $error;