From b50b2e5f94774268c271484f9c07bfe316f95527 Mon Sep 17 00:00:00 2001 From: jeff Date: Fri, 22 Feb 2002 23:18:34 +0000 Subject: add some reporting features --- FS/bin/freeside-credit-report | 184 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 184 insertions(+) create mode 100755 FS/bin/freeside-credit-report (limited to 'FS/bin/freeside-credit-report') diff --git a/FS/bin/freeside-credit-report b/FS/bin/freeside-credit-report new file mode 100755 index 000000000..4307a21b0 --- /dev/null +++ b/FS/bin/freeside-credit-report @@ -0,0 +1,184 @@ +#!/usr/bin/perl -Tw + +use strict; +use Date::Parse; +use Time::Local; +use Getopt::Std; +use FS::Conf; +use FS::UID qw(adminsuidsetup); +use FS::Record qw(qsearch); +use FS::cust_credit; + +# Set the mail program +my $mail_program = "/usr/sbin/sendmail -t -n"; + +&untaint_argv; #what it sounds like (eww) +use vars qw($opt_v $opt_p $opt_e $opt_d $opt_s); +getopts("vped:s:"); #switches + +#we're at now now (and later). +my($_enddate)= $main::opt_d ? str2time($main::opt_d) : $^T; +my($_startdate)= $main::opt_s ? str2time($main::opt_s) : $^T; + +# Get the current month +my ($ssec,$smin,$shour,$smday,$smon,$syear) = + (localtime($_startdate) )[0,1,2,3,4,5]; +$syear+=1900; +$smon++; + +# Get the current month +my ($esec,$emin,$ehour,$emday,$emon,$eyear) = + (localtime($_enddate) )[0,1,2,3,4,5]; +$eyear+=1900; +$emon++; + +# Login to the database +my $user = shift or die &usage; +adminsuidsetup $user; + +# Get the needed configuration files +my $conf = new FS::Conf; +my $lpr = $conf->config('lpr'); +my $email = $conf->config('email'); + +my(@cust_credits)=qsearch('cust_credit',{}); +if (scalar(@cust_credits) == 0) +{ + exit 1; +} + +# Open print and email pipes +# $lpr and opt_p for printing +# $email and opt_e for email + +if ($lpr && $main::opt_p) +{ + open(LPR, "|$lpr"); + print LPR qq~ I N H O U S E C R E D I T S for period beginning: $smon/$smday/$syear and ending $emon/$emday/$eyear\n\n~; +} + +if ($email && $main::opt_e) +{ + open (MAIL, "|$mail_program"); + print MAIL <getfield('_date'); + my $amount = $cust_credit->getfield('amount'); + my $credited = $cust_credit->getfield('credited'); + + + if ($_date >= $_startdate && $_date <= $_enddate) { + $total += $amount; + + my ($sec,$min,$hour,$mday,$mon,$year) = + (localtime($_date) )[0,1,2,3,4,5]; + $mon++; + + } + +} + +if ($main::opt_v) { + printf(qq{\n\n%25s%14.2f\n}, "Credits Offered", $total); + printf(qq{\n%39s\n%39.2f\n}, "=========", $total); +} + +# Now I need to close LPR and EMAIL if they were open +if($lpr && $main::opt_p) +{ + printf(LPR qq{\n\n%25s%14.2f\n}, "Credits Offered", $total); + printf(LPR qq{\n%39s\n%39.2f\n}, "=========", $total); + close LPR || die "Could not close printer: $lpr\n"; +} +if($email && $main::opt_e) +{ + printf(MAIL qq{\n\n%25s%14.2f\n}, "Credits Offered", $total); + printf(MAIL qq{\n%39s\n%39.2f\n}, "=========", $total); + close MAIL || die "Could not close printer: $email\n"; +} + + +# subroutines +sub untaint_argv { + foreach $_ ( $[ .. $#ARGV ) { #untaint @ARGV + $ARGV[$_] =~ /^([\w\-\/ :]*)$/ || die "Illegal argument \"$ARGV[$_]\""; + $ARGV[$_]=$1; + } +} + +sub usage { + die "Usage:\n\n freeside-credit-report [-v] [-p] [-e] user\n"; +} + +=head1 NAME + +freeside-credit-report - Prints or emails in house credits offered in a given period. + +=head1 SYNOPSIS + + freeside-credit-report [-v] [-p] [-e] user + +=head1 DESCRIPTION + +Prints or emails in house credits offered in a given period. + +-v: Verbose - Prints records to STDOUT. + +-p: Print to printer lpr as found in the conf directory. + +-e: Email output to user found in the Conf email file. + +user: From the mapsecrets file - see config.html from the base documentation + +=head1 VERSION + +$Id: freeside-credit-report,v 1.1 2002-02-22 23:18:32 jeff Exp $ + +=head1 BUGS + +Yes..... Use at your own risk. No guarantees or warrantees of any +kind apply to this program. Parts of this program are hacked from +other GNU licensed software created mainly by Ivan Kohler. + +This is released under the GNU Public License. See www.gnu.org +for more information regarding this license. + +=head1 SEE ALSO + +L, config.html from the base documentation + +=head1 HISTORY + +griff@aver-computer.com July 99 + +$Log: freeside-credit-report,v $ +Revision 1.1 2002-02-22 23:18:32 jeff +add some reporting features + +Revision 1.1 2002/02/19 14:24:53 jeff +might be functional now + +Revision 1.1 2000/09/20 19:25:19 jeff +local modifications + +Revision 1.1 2000/05/13 21:57:56 ivan +add print_batch script from Joel Griffiths + + +=cut + + -- cgit v1.2.1 From 6c2f4c44fc083bde9dd055bd4db51e65fa377379 Mon Sep 17 00:00:00 2001 From: jeff Date: Tue, 5 Mar 2002 23:13:24 +0000 Subject: consistency is nice --- FS/bin/freeside-credit-report | 135 ++++++++++++++++++++++++++---------------- 1 file changed, 85 insertions(+), 50 deletions(-) (limited to 'FS/bin/freeside-credit-report') diff --git a/FS/bin/freeside-credit-report b/FS/bin/freeside-credit-report index 4307a21b0..92931bcee 100755 --- a/FS/bin/freeside-credit-report +++ b/FS/bin/freeside-credit-report @@ -1,9 +1,11 @@ #!/usr/bin/perl -Tw + use strict; use Date::Parse; use Time::Local; use Getopt::Std; +use Text::Template; use FS::Conf; use FS::UID qw(adminsuidsetup); use FS::Record qw(qsearch); @@ -13,24 +15,24 @@ use FS::cust_credit; my $mail_program = "/usr/sbin/sendmail -t -n"; &untaint_argv; #what it sounds like (eww) -use vars qw($opt_v $opt_p $opt_e $opt_d $opt_s); -getopts("vped:s:"); #switches +use vars qw($opt_v $opt_p $opt_m $opt_e $opt_t $opt_s $opt_f $report_lines $report_template @buf); +getopts("vpmef:s:"); #switches #we're at now now (and later). -my($_enddate)= $main::opt_d ? str2time($main::opt_d) : $^T; -my($_startdate)= $main::opt_s ? str2time($main::opt_s) : $^T; +my($_finishdate)= $opt_f ? str2time($main::opt_f) : $^T; +my($_startdate)= $opt_s ? str2time($main::opt_s) : $^T; # Get the current month my ($ssec,$smin,$shour,$smday,$smon,$syear) = (localtime($_startdate) )[0,1,2,3,4,5]; -$syear+=1900; $smon++; +$syear += 1900; # Get the current month -my ($esec,$emin,$ehour,$emday,$emon,$eyear) = - (localtime($_enddate) )[0,1,2,3,4,5]; -$eyear+=1900; -$emon++; +my ($fsec,$fmin,$fhour,$fmday,$fmon,$fyear) = + (localtime($_finishdate) )[0,1,2,3,4,5]; +$fmon++; +$fyear += 1900; # Login to the database my $user = shift or die &usage; @@ -40,6 +42,19 @@ adminsuidsetup $user; my $conf = new FS::Conf; my $lpr = $conf->config('lpr'); my $email = $conf->config('email'); +my @report_template = $conf->config('report_template') + or die "cannot load config file report_template"; +$report_lines = 0; +foreach ( grep /report_lines\(\d+\)/, @report_template ) { #kludgy :/ + /report_lines\((\d+)\)/; + $report_lines += $1; +} +die "no report_lines() functions in template?" unless $report_lines; +$report_template = new Text::Template ( + TYPE => 'ARRAY', + SOURCE => [ map "$_\n", @report_template ], +) or die "can't create new Text::Template object: $Text::Template::ERROR"; + my(@cust_credits)=qsearch('cust_credit',{}); if (scalar(@cust_credits) == 0) @@ -49,15 +64,14 @@ if (scalar(@cust_credits) == 0) # Open print and email pipes # $lpr and opt_p for printing -# $email and opt_e for email +# $email and opt_m for email if ($lpr && $main::opt_p) { open(LPR, "|$lpr"); - print LPR qq~ I N H O U S E C R E D I T S for period beginning: $smon/$smday/$syear and ending $emon/$emday/$eyear\n\n~; } -if ($email && $main::opt_e) +if ($email && $main::opt_m) { open (MAIL, "|$mail_program"); print MAIL <getfield('_date'); my $amount = $cust_credit->getfield('amount'); - my $credited = $cust_credit->getfield('credited'); - - if ($_date >= $_startdate && $_date <= $_enddate) { + if ($_date >= $_startdate && $_date <= $_finishdate) { $total += $amount; - - my ($sec,$min,$hour,$mday,$mon,$year) = - (localtime($_date) )[0,1,2,3,4,5]; - $mon++; - } +} +push @buf, sprintf(qq{\n%25s%14.2f\n}, "Credits Offered", $total); +push @buf, sprintf(qq{\n%39s\n%39.2f\n}, "=========", $total); + +sub FS::credit_report::_template::report_lines { + my $lines = shift; + map { + scalar(@buf) ? shift @buf : '' ; + } + ( 1 .. $lines ); } -if ($main::opt_v) { - printf(qq{\n\n%25s%14.2f\n}, "Credits Offered", $total); - printf(qq{\n%39s\n%39.2f\n}, "=========", $total); +$FS::credit_report::_template::title = qq~IN HOUSE CREDITS for $smon/$smday/$syear through $fmon/$fmday/$fyear~; +$FS::credit_report::_template::title = $opt_t if $opt_t; +$FS::credit_report::_template::page = 1; +$FS::credit_report::_template::date = $^T; +$FS::credit_report::_template::date = $^T; +$FS::credit_report::_template::fdate = $_finishdate; +$FS::credit_report::_template::fdate = $_finishdate; +$FS::credit_report::_template::sdate = $_startdate; +$FS::credit_report::_template::sdate = $_startdate; +$FS::credit_report::_template::total_pages = + int( scalar(@buf) / $report_lines); +$FS::credit_report::_template::total_pages++ if scalar(@buf) % $report_lines; + +my @report; +while (@buf) { + push @report, split("\n", + $report_template->fill_in( PACKAGE => 'FS::credit_report::_template' ) + ); + $FS::credit_report::_template::page++; } -# Now I need to close LPR and EMAIL if they were open -if($lpr && $main::opt_p) +if ($opt_v) { + print map "$_\n", @report; +} +if($lpr && $opt_p) { - printf(LPR qq{\n\n%25s%14.2f\n}, "Credits Offered", $total); - printf(LPR qq{\n%39s\n%39.2f\n}, "=========", $total); - close LPR || die "Could not close printer: $lpr\n"; + print LPR map "$_\n", @report; + print LPR "\f" if $opt_e; + close LPR || die "Could not close printer: $lpr\n"; } -if($email && $main::opt_e) +if($email && $opt_m) { - printf(MAIL qq{\n\n%25s%14.2f\n}, "Credits Offered", $total); - printf(MAIL qq{\n%39s\n%39.2f\n}, "=========", $total); - close MAIL || die "Could not close printer: $email\n"; + print MAIL map "$_\n", @report; + close MAIL || die "Could not close printer: $email\n"; } @@ -126,27 +162,35 @@ sub usage { =head1 NAME -freeside-credit-report - Prints or emails in house credits offered in a given period. +freeside-credit-report - Prints or emails total credit memos in a given period. =head1 SYNOPSIS - freeside-credit-report [-v] [-p] [-e] user + freeside-credit-report [-v] [-p] [-m] [-e] [-t "title"] [-s date] [-f date] user =head1 DESCRIPTION -Prints or emails in house credits offered in a given period. +Prints or emails total credit memos in a given period. -v: Verbose - Prints records to STDOUT. -p: Print to printer lpr as found in the conf directory. --e: Email output to user found in the Conf email file. +-m: Email output to user found in the Conf email file. + +-e: Print a final form feed to the printer. + +-t: supply a title for the top of each page. + +-s: starting date for inclusion + +-f: final date for inclusion user: From the mapsecrets file - see config.html from the base documentation =head1 VERSION -$Id: freeside-credit-report,v 1.1 2002-02-22 23:18:32 jeff Exp $ +$Id: freeside-credit-report,v 1.2 2002-03-05 23:13:23 jeff Exp $ =head1 BUGS @@ -166,17 +210,8 @@ L, config.html from the base documentation griff@aver-computer.com July 99 $Log: freeside-credit-report,v $ -Revision 1.1 2002-02-22 23:18:32 jeff -add some reporting features - -Revision 1.1 2002/02/19 14:24:53 jeff -might be functional now - -Revision 1.1 2000/09/20 19:25:19 jeff -local modifications - -Revision 1.1 2000/05/13 21:57:56 ivan -add print_batch script from Joel Griffiths +Revision 1.2 2002-03-05 23:13:23 jeff +consistency is nice =cut -- cgit v1.2.1 From 8fd504d36e02fd1ac3d0d5c9d6dc723fdb419aa1 Mon Sep 17 00:00:00 2001 From: ivan Date: Wed, 6 Mar 2002 00:17:32 +0000 Subject: remove CVS Log tag --- FS/bin/freeside-credit-report | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) (limited to 'FS/bin/freeside-credit-report') diff --git a/FS/bin/freeside-credit-report b/FS/bin/freeside-credit-report index 92931bcee..c73988321 100755 --- a/FS/bin/freeside-credit-report +++ b/FS/bin/freeside-credit-report @@ -190,7 +190,7 @@ user: From the mapsecrets file - see config.html from the base documentation =head1 VERSION -$Id: freeside-credit-report,v 1.2 2002-03-05 23:13:23 jeff Exp $ +$Id: freeside-credit-report,v 1.3 2002-03-06 00:17:32 ivan Exp $ =head1 BUGS @@ -205,15 +205,11 @@ for more information regarding this license. L, config.html from the base documentation -=head1 HISTORY +=head1 AUTHOR -griff@aver-computer.com July 99 - -$Log: freeside-credit-report,v $ -Revision 1.2 2002-03-05 23:13:23 jeff -consistency is nice +Jeff Finucane +based on print-batch by Joel Griffiths =cut - -- cgit v1.2.1 From 3e2e5fecb9ef3cf39a6ac098aacb76763edd3938 Mon Sep 17 00:00:00 2001 From: jeff Date: Thu, 7 Mar 2002 19:50:24 +0000 Subject: less shelling, more perly - abolish some pipes to sendmail --- FS/bin/freeside-credit-report | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) (limited to 'FS/bin/freeside-credit-report') diff --git a/FS/bin/freeside-credit-report b/FS/bin/freeside-credit-report index c73988321..7699daf4d 100755 --- a/FS/bin/freeside-credit-report +++ b/FS/bin/freeside-credit-report @@ -6,16 +6,17 @@ use Date::Parse; use Time::Local; use Getopt::Std; use Text::Template; +use Net::SMTP; +use Mail::Header; +use Mail::Internet; use FS::Conf; use FS::UID qw(adminsuidsetup); use FS::Record qw(qsearch); use FS::cust_credit; -# Set the mail program -my $mail_program = "/usr/sbin/sendmail -t -n"; &untaint_argv; #what it sounds like (eww) -use vars qw($opt_v $opt_p $opt_m $opt_e $opt_t $opt_s $opt_f $report_lines $report_template @buf); +use vars qw($opt_v $opt_p $opt_m $opt_e $opt_t $opt_s $opt_f $report_lines $report_template @buf $header); getopts("vpmef:s:"); #switches #we're at now now (and later). @@ -42,6 +43,9 @@ adminsuidsetup $user; my $conf = new FS::Conf; my $lpr = $conf->config('lpr'); my $email = $conf->config('email'); +my $smtpmachine = $conf->config('smtpmachine'); +my $mail_sender = $conf->exists('invoice_from') ? $conf->config('invoice_from') : + 'postmaster'; my @report_template = $conf->config('report_template') or die "cannot load config file report_template"; $report_lines = 0; @@ -73,14 +77,14 @@ if ($lpr && $main::opt_p) if ($email && $main::opt_m) { - open (MAIL, "|$mail_program"); - print MAIL < $header, + 'Body' => [ (@report) ], + ); + $!=0; + $message->smtpsend( Host => "$smtpmachine" ) + or die "can't send report to $email via $smtpmachine: $!"; } @@ -190,7 +199,7 @@ user: From the mapsecrets file - see config.html from the base documentation =head1 VERSION -$Id: freeside-credit-report,v 1.3 2002-03-06 00:17:32 ivan Exp $ +$Id: freeside-credit-report,v 1.4 2002-03-07 19:50:24 jeff Exp $ =head1 BUGS -- cgit v1.2.1 From 91292eadb6254740a9b72e5dc95f575593f6a35d Mon Sep 17 00:00:00 2001 From: ivan Date: Mon, 9 Sep 2002 22:57:34 +0000 Subject: allow . in untaint_argv, for usernames --- FS/bin/freeside-credit-report | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'FS/bin/freeside-credit-report') diff --git a/FS/bin/freeside-credit-report b/FS/bin/freeside-credit-report index 7699daf4d..410dabe8f 100755 --- a/FS/bin/freeside-credit-report +++ b/FS/bin/freeside-credit-report @@ -160,7 +160,7 @@ if($email && $opt_m) # subroutines sub untaint_argv { foreach $_ ( $[ .. $#ARGV ) { #untaint @ARGV - $ARGV[$_] =~ /^([\w\-\/ :]*)$/ || die "Illegal argument \"$ARGV[$_]\""; + $ARGV[$_] =~ /^([\w\-\/ :\.]*)$/ || die "Illegal argument \"$ARGV[$_]\""; $ARGV[$_]=$1; } } @@ -199,7 +199,7 @@ user: From the mapsecrets file - see config.html from the base documentation =head1 VERSION -$Id: freeside-credit-report,v 1.4 2002-03-07 19:50:24 jeff Exp $ +$Id: freeside-credit-report,v 1.5 2002-09-09 22:57:34 ivan Exp $ =head1 BUGS -- cgit v1.2.1