From b3c2e70e63eb8f7ab2543a4d400f72bd5044a9f9 Mon Sep 17 00:00:00 2001 From: levinse Date: Wed, 16 Feb 2011 06:21:29 +0000 Subject: [PATCH] add barcodes to invoices, PDF part, RT10698 --- FS/FS/Conf.pm | 7 +++++++ FS/FS/cust_bill.pm | 50 +++++++++++++++++++++++++++++++++++++++++++----- conf/invoice_latexcoupon | 2 ++ 3 files changed, 54 insertions(+), 5 deletions(-) diff --git a/FS/FS/Conf.pm b/FS/FS/Conf.pm index 88d1087d4..a46106f61 100644 --- a/FS/FS/Conf.pm +++ b/FS/FS/Conf.pm @@ -667,6 +667,13 @@ my %payment_gateway_options = ( 'description' => 'Generate a line item on an invoice even when a package is discounted 100%', 'type' => 'checkbox', }, + + { + 'key' => 'invoice-barcode', + 'section' => 'billing', + 'description' => 'Display a barcode on HTML and PDF invoices', + 'type' => 'checkbox', + }, { 'key' => 'encryption', diff --git a/FS/FS/cust_bill.pm b/FS/FS/cust_bill.pm index 6e86bd645..af388d3a6 100644 --- a/FS/FS/cust_bill.pm +++ b/FS/FS/cust_bill.pm @@ -13,6 +13,7 @@ use String::ShellQuote; use HTML::Entities; use Locale::Country; use Storable qw( freeze thaw ); +use GD::Barcode; use FS::UID qw( datasrc ); use FS::Misc qw( send_email send_fax generate_ps generate_pdf do_print ); use FS::Record qw( qsearch qsearchs dbh ); @@ -37,6 +38,7 @@ use FS::part_bill_event; use FS::payby; use FS::bill_batch; use FS::cust_bill_batch; +use Cwd; @ISA = qw( FS::cust_main_Mixin FS::Record ); @@ -2152,6 +2154,40 @@ sub print_latex { close $lh; $params{'logo_file'} = $lh->filename; + if($conf->exists('invoice-barcode')){ + my $gdbar = new GD::Barcode('Code39',$self->invnum); + die "can't create barcode: " . $GD::Barcode::errStr unless $gdbar; + my $gd = $gdbar->plot(Height => 20); + my $bh = new File::Temp( TEMPLATE => 'barcode.'. $self->invnum. '.XXXXXXXX', + DIR => $dir, + SUFFIX => '.png', + UNLINK => 0, + ) or die "can't open temp file: $!\n"; + print $bh $gd->png or die "cannot write barcode to file: $!\n"; + + my $png_file = $bh->filename; + close $bh; + + my $eps_file = $png_file; + $eps_file =~ s/\.png$/.eps/g; + $png_file =~ /(barcode.*png)/; + $png_file = $1; + $eps_file =~ /(barcode.*eps)/; + $eps_file = $1; + + my $curr_dir = cwd(); + chdir($dir); + # after painfuly long experimentation, it was determined that sam2p won't + # accept : and other chars in the path, no matter how hard I tried to + # escape them, hence the chdir (and chdir back, just to be safe) + system('sam2p', $png_file, 'EPS:', $eps_file ) == 0 + or die "sam2p failed: $!\n"; + unlink($png_file); + chdir($curr_dir); + + $params{'barcode_file'} = $eps_file; + } + my @filled_in = $self->print_generic( %params ); my $fh = new File::Temp( TEMPLATE => 'invoice.'. $self->invnum. '.XXXXXXXX', @@ -2163,7 +2199,7 @@ sub print_latex { close $fh; $fh->filename =~ /^(.*).tex$/ or die "unparsable filename: ". $fh->filename; - return ($1, $params{'logo_file'}); + return ($1, $params{'logo_file'}, $params{'barcode_file'}); } @@ -2526,6 +2562,8 @@ sub print_generic { $invoice_data{'logo_file'} = $params{'logo_file'} if $params{'logo_file'}; + $invoice_data{'barcode_file'} = $params{'barcode_file'} + if $params{'barcode_file'}; my( $pr_total, @pr_cust_bill ) = $self->previous; #previous balance # my( $cr_total, @cr_cust_credit ) = $self->cust_credit; #credits @@ -3172,9 +3210,10 @@ I, if specified, overrides "Invoice" as the name of the sent docume sub print_ps { my $self = shift; - my ($file, $lfile) = $self->print_latex(@_); + my ($file, $logofile, $barcodefile) = $self->print_latex(@_); my $ps = generate_ps($file); - unlink($lfile); + unlink($logofile); + unlink($barcodefile); $ps; } @@ -3200,9 +3239,10 @@ I, if specified, overrides "Invoice" as the name of the sent docume sub print_pdf { my $self = shift; - my ($file, $lfile) = $self->print_latex(@_); + my ($file, $logofile, $barcodefile) = $self->print_latex(@_); my $pdf = generate_pdf($file); - unlink($lfile); + unlink($logofile); + unlink($barcodefile); $pdf; } diff --git a/conf/invoice_latexcoupon b/conf/invoice_latexcoupon index a4ccddda7..c0d36e57b 100644 --- a/conf/invoice_latexcoupon +++ b/conf/invoice_latexcoupon @@ -18,6 +18,8 @@ Invoice date & \textbf{[@-- $date --@]} & \multirow{4}*{[@-- $verticalreturnaddr \addressline{[@-- $address2 --@]} \addressline{[@-- $city --@], [@-- $state --@]~~[@-- $zip --@]} \addressline{[@-- $country --@]} +\\ +[@-- $barcode_file ? '\includegraphics{'.$barcode_file.'}' : '' --@] \end{minipage}}}\\ Customer\#& \textbf{[@-- $custnum --@]} & \\ Total Due & \textbf{[@-- $balance --@]} & \\ -- 2.11.0