From 6577fcc50b6a711f1988bb4c83924e90f4bc97d6 Mon Sep 17 00:00:00 2001 From: jeff Date: Tue, 24 Jun 2008 16:39:56 +0000 Subject: [PATCH] postal invoice fees --- FS/FS/Conf.pm | 19 +++++++++++++++++++ FS/FS/Conf_compat17.pm | 19 +++++++++++++++++++ FS/FS/cust_main.pm | 45 +++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 79 insertions(+), 4 deletions(-) diff --git a/FS/FS/Conf.pm b/FS/FS/Conf.pm index e54283d2b..4f460d93b 100644 --- a/FS/FS/Conf.pm +++ b/FS/FS/Conf.pm @@ -1882,6 +1882,25 @@ worry that config_items is freeside-specific and icky. }, { + 'key' => 'postal_invoice-fee_pkgpart', + 'section' => 'billing', + 'description' => 'This allows selection of a package to insert on invoices for customers with postal invoices selected.', + 'type' => 'select-sub', + 'options_sub' => sub { require FS::Record; + require FS::part_pkg; + map { $_->pkgpart => $_->pkg } + FS::Record::qsearch('part_pkg', { disabled=>'' } ); + }, + 'option_sub' => sub { require FS::Record; + require FS::part_pkg; + my $part_pkg = FS::Record::qsearchs( + 'part_pkg', { 'pkgpart'=>shift } + ); + $part_pkg ? $part_pkg->pkg : ''; + }, + }, + + { 'key' => 'batch-enable', 'section' => 'billing', 'description' => 'Enable credit card and/or ACH batching - leave disabled for real-time installations.', diff --git a/FS/FS/Conf_compat17.pm b/FS/FS/Conf_compat17.pm index 693c02f72..817712060 100644 --- a/FS/FS/Conf_compat17.pm +++ b/FS/FS/Conf_compat17.pm @@ -1954,6 +1954,25 @@ httemplate/docs/config.html }, { + 'key' => 'postal_invoice-fee_pkgpart', + 'section' => 'billing', + 'description' => 'This allows selection of a package to insert on invoices for customers with postal invoices selected.', + 'type' => 'select-sub', + 'options_sub' => sub { require FS::Record; + require FS::part_pkg; + map { $_->pkgpart => $_->pkg } + FS::Record::qsearch('part_pkg', { disabled=>'' } ); + }, + 'option_sub' => sub { require FS::Record; + require FS::part_pkg; + my $part_pkg = FS::Record::qsearchs( + 'part_pkg', { 'pkgpart'=>shift } + ); + $part_pkg ? $part_pkg->pkg : ''; + }, + }, + + { 'key' => 'batch-enable', 'section' => 'billing', 'description' => 'Enable credit card and/or ACH batching - leave disabled for real-time installations.', diff --git a/FS/FS/cust_main.pm b/FS/FS/cust_main.pm index dfe254c89..19323e8a4 100644 --- a/FS/FS/cust_main.pm +++ b/FS/FS/cust_main.pm @@ -2068,15 +2068,14 @@ sub bill { # & generate invoice database. ### - my( $total_setup, $total_recur ) = ( 0, 0 ); + my( $total_setup, $total_recur, $postal_charge ) = ( 0, 0, 0 ); my %tax; my %taxlisthash; my %taxname; my @precommit_hooks = (); - foreach my $cust_pkg ( - qsearch('cust_pkg', { 'custnum' => $self->custnum } ) - ) { + my @cust_pkgs = qsearch('cust_pkg', { 'custnum' => $self->custnum } ); + foreach my $cust_pkg (@cust_pkgs) { #NO!! next if $cust_pkg->cancel; next if $cust_pkg->getfield('cancel'); @@ -2242,6 +2241,17 @@ sub bill { if ( $setup != 0 || $recur != 0 ) { + unless ($postal_charge) { + $postal_charge = 1; # try only once + my $postal_pkg = $self->charge_postal_fee(); + if ( $postal_pkg && !ref( $postal_pkg ) ) { + $dbh->rollback if $oldAutoCommit; + return "can't charge postal invoice fee for customer ". + $self->custnum. ": $postal_pkg"; + } + push @cust_pkgs, $postal_pkg if $postal_pkg; + } + warn " charges (setup=$setup, recur=$recur); adding line items\n" if $DEBUG > 1; my $cust_bill_pkg = new FS::cust_bill_pkg { @@ -4764,6 +4774,33 @@ sub charge { } +#=item charge_postal_fee +# +#Applies a one time charge this customer. If there is an error, +#returns the error, returns the cust_pkg charge object or false +#if there was no charge. +# +#=cut +# +# This should be a customer event. For that to work requires that bill +# also be a customer event. + +sub charge_postal_fee { + my $self = shift; + + my $pkgpart = $conf->config('postal_invoice-fee_pkgpart'); + return '' unless ($pkgpart && grep { $_ eq 'POST' } $self->invoicing_list); + + my $cust_pkg = new FS::cust_pkg ( { + 'custnum' => $self->custnum, + 'pkgpart' => $pkgpart, + 'quantity' => 1, + } ); + + my $error = $cust_pkg->insert; + $error ? $error : $cust_pkg; +} + =item cust_bill Returns all the invoices (see L) for this customer. -- 2.11.0