X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fcust_main.pm;h=9d40e7306683c36c428e8bcc38b4b4195da2537c;hb=f04616ad2c32641c1b85820cb97bcb22edbbc9f5;hp=ca5a4e83f29ffb976afa5075d7487f816c4e3a3c;hpb=5c35f5323f1cdcf7eabe6632d0352ea417d3047e;p=freeside.git diff --git a/FS/FS/cust_main.pm b/FS/FS/cust_main.pm index ca5a4e83f..9d40e7306 100644 --- a/FS/FS/cust_main.pm +++ b/FS/FS/cust_main.pm @@ -1845,6 +1845,8 @@ sub check { my( $m, $y ); if ( $self->paydate =~ /^(\d{1,2})[\/\-](\d{2}(\d{2})?)$/ ) { ( $m, $y ) = ( $1, length($2) == 4 ? $2 : "20$2" ); + } elsif ( $self->paydate =~ /^19(\d{2})[\/\-](\d{1,2})[\/\-]\d+$/ ) { + ( $m, $y ) = ( $2, "19$1" ); } elsif ( $self->paydate =~ /^(20)?(\d{2})[\/\-](\d{1,2})[\/\-]\d+$/ ) { ( $m, $y ) = ( $3, "20$2" ); } else { @@ -2460,7 +2462,7 @@ An array ref of specific packages (objects) to attempt billing, instead trying a =item not_pkgpart -A hashref of pkgparts to exclude from this billing run. +A hashref of pkgparts to exclude from this billing run (can also be specified as a comma-separated scalar). =item invoice_time @@ -2487,6 +2489,10 @@ sub bill { my $invoice_time = $options{'invoice_time'} || $time; $options{'not_pkgpart'} ||= {}; + $options{'not_pkgpart'} = { map { $_ => 1 } + split(/\s*,\s*/, $options{'not_pkgpart'}) + } + unless ref($options{'not_pkgpart'}); local $SIG{HUP} = 'IGNORE'; local $SIG{INT} = 'IGNORE'; @@ -2715,6 +2721,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, @@ -2722,6 +2745,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, }; @@ -2759,11 +2783,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 ) + : 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 ) { @@ -3003,7 +3040,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; @@ -3024,6 +3061,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 = (); @@ -3031,8 +3069,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' ) { @@ -3114,7 +3152,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? @@ -3123,18 +3163,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 }; } @@ -6166,19 +6216,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'; @@ -6194,7 +6248,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"; @@ -6303,19 +6357,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'; @@ -6379,7 +6438,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; @@ -6949,6 +7008,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 @@ -6956,6 +7033,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 { @@ -7087,6 +7169,9 @@ New-style, with a hashref of options: #vendor taxation 'taxproduct' => 2, #part_pkg_taxproduct 'override' => {}, #XXX describe + + #will be filled in with the new object + 'cust_pkg_ref' => \$cust_pkg, } ); @@ -7102,6 +7187,7 @@ sub charge { my ( $pkg, $comment, $additional ); my ( $setuptax, $taxclass ); #internal taxes my ( $taxproduct, $override ); #vendor (CCH) taxes + my $cust_pkg_ref = ''; if ( ref( $_[0] ) ) { $amount = $_[0]->{amount}; $quantity = exists($_[0]->{quantity}) ? $_[0]->{quantity} : 1; @@ -7115,6 +7201,7 @@ sub charge { $additional = $_[0]->{additional} || []; $taxproduct = $_[0]->{taxproductnum}; $override = { '' => $_[0]->{tax_override} }; + $cust_pkg_ref = exists($_[0]->{cust_pkg_ref}) ? $_[0]->{cust_pkg_ref} : ''; } else { $amount = shift; $quantity = 1; @@ -7186,6 +7273,8 @@ sub charge { if ( $error ) { $dbh->rollback if $oldAutoCommit; return $error; + } elsif ( $cust_pkg_ref ) { + ${$cust_pkg_ref} = $cust_pkg; } $dbh->commit or die $dbh->errstr if $oldAutoCommit; @@ -7228,6 +7317,7 @@ Returns all the invoices (see L) for this customer. sub cust_bill { my $self = shift; + map { $_ } #return $self->num_cust_bill unless wantarray; sort { $a->_date <=> $b->_date } qsearch('cust_bill', { 'custnum' => $self->custnum, } ) } @@ -7259,6 +7349,7 @@ Returns all the statements (see L) for this customer. sub cust_statement { my $self = shift; + map { $_ } #return $self->num_cust_statement unless wantarray; sort { $a->_date <=> $b->_date } qsearch('cust_statement', { 'custnum' => $self->custnum, } ) } @@ -7271,6 +7362,7 @@ Returns all the credits (see L) for this customer. sub cust_credit { my $self = shift; + map { $_ } #return $self->num_cust_credit unless wantarray; sort { $a->_date <=> $b->_date } qsearch( 'cust_credit', { 'custnum' => $self->custnum } ) } @@ -7284,6 +7376,7 @@ package when using experimental package balances. sub cust_credit_pkgnum { my( $self, $pkgnum ) = @_; + map { $_ } #return $self->num_cust_credit_pkgnum($pkgnum) unless wantarray; sort { $a->_date <=> $b->_date } qsearch( 'cust_credit', { 'custnum' => $self->custnum, 'pkgnum' => $pkgnum, @@ -7299,10 +7392,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 @@ -7312,6 +7421,7 @@ package when using experimental package balances. sub cust_pay_pkgnum { my( $self, $pkgnum ) = @_; + map { $_ } #return $self->num_cust_pay_pkgnum($pkgnum) unless wantarray; sort { $a->_date <=> $b->_date } qsearch( 'cust_pay', { 'custnum' => $self->custnum, 'pkgnum' => $pkgnum, @@ -7327,6 +7437,7 @@ Returns all voided payments (see L) for this customer. sub cust_pay_void { my $self = shift; + map { $_ } #return $self->num_cust_pay_void unless wantarray; sort { $a->_date <=> $b->_date } qsearch( 'cust_pay_void', { 'custnum' => $self->custnum } ) } @@ -7339,6 +7450,7 @@ Returns all batched payments (see L) for this customer. sub cust_pay_batch { my $self = shift; + map { $_ } #return $self->num_cust_pay_batch unless wantarray; sort { $a->paybatchnum <=> $b->paybatchnum } qsearch( 'cust_pay_batch', { 'custnum' => $self->custnum } ) } @@ -7386,6 +7498,7 @@ Returns all the refunds (see L) for this customer. sub cust_refund { my $self = shift; + map { $_ } #return $self->num_cust_refund unless wantarray; sort { $a->_date <=> $b->_date } qsearch( 'cust_refund', { 'custnum' => $self->custnum } ) } @@ -8201,6 +8314,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'}); @@ -8262,6 +8378,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;