add barcodes to invoices, HTML part, RT10698
authorlevinse <levinse>
Wed, 16 Feb 2011 07:39:02 +0000 (07:39 +0000)
committerlevinse <levinse>
Wed, 16 Feb 2011 07:39:02 +0000 (07:39 +0000)
FS/FS/cust_bill.pm
conf/invoice_html
httemplate/view/cust_bill-barcode.cgi [new file with mode: 0755]
httemplate/view/cust_bill.cgi

index 29ec1d3..e44747b 100644 (file)
@@ -960,6 +960,19 @@ sub generate_email {
       'Filename'   => 'logo.png',
       'Content-ID' => "<$content_id>",
     ;
+   
+    my $barcode;
+    if($conf->exists('invoice-barcode')){
+       my $barcode_content_id = join('.', rand()*(2**32), $$, time). "\@$from";
+       $barcode = build MIME::Entity
+         'Type'       => 'image/png',
+         'Encoding'   => 'base64',
+         'Data'       => $self->invoice_barcode(0),
+         'Filename'   => 'barcode.png',
+         'Content-ID' => "<$barcode_content_id>",
+       ;
+       $opt{'barcode_cid'} = $barcode_content_id;
+    }
 
     $alternative->attach(
       'Type'        => 'text/html',
@@ -1033,7 +1046,12 @@ sub generate_email {
       #   image/png
 
       $return{'content-type'} = 'multipart/related';
-      $return{'mimeparts'} = [ $alternative, $image, @otherparts ];
+      if($conf->exists('invoice-barcode')){
+         $return{'mimeparts'} = [ $alternative, $image, $barcode, @otherparts ];
+      }
+      else {
+         $return{'mimeparts'} = [ $alternative, $image, @otherparts ];
+      }
       $return{'type'} = 'multipart/alternative'; #Content-Type of first part...
       #$return{'disposition'} = 'inline';
 
@@ -2124,19 +2142,7 @@ sub print_latex {
   $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 $png_file = $self->invoice_barcode($dir);
       my $eps_file = $png_file;
       $eps_file =~ s/\.png$/.eps/g;
       $png_file =~ /(barcode.*png)/;
@@ -2149,7 +2155,7 @@ sub print_latex {
       # 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
+      system('sam2p', '-j:quiet', $png_file, 'EPS:', $eps_file ) == 0
        or die "sam2p failed: $!\n";
       unlink($png_file);
       chdir($curr_dir);
@@ -2172,6 +2178,35 @@ sub print_latex {
 
 }
 
+=item invoice_barcode DIR_OR_FALSE
+
+Generates an invoice barcode PNG. If DIR_OR_FALSE is a true value,
+it is taken as the temp directory where the PNG file will be generated and the
+PNG file name is returned. Otherwise, the PNG image itself is returned.
+
+=cut
+
+sub invoice_barcode {
+    my ($self, $dir) = (shift,shift);
+    
+    my $gdbar = new GD::Barcode('Code39',$self->invnum);
+       die "can't create barcode: " . $GD::Barcode::errStr unless $gdbar;
+    my $gd = $gdbar->plot(Height => 30);
+
+    if($dir) {
+       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;
+       return $png_file;
+    }
+    return $gd->png;
+}
+
 =item print_generic OPTION => VALUE ...
 
 Internal method - returns a filled-in template for this invoice as a scalar.
@@ -2533,6 +2568,10 @@ sub print_generic {
     if $params{'logo_file'};
   $invoice_data{'barcode_file'} = $params{'barcode_file'}
     if $params{'barcode_file'};
+  $invoice_data{'barcode_img'} = $params{'barcode_img'}
+    if $params{'barcode_img'};
+  $invoice_data{'barcode_cid'} = $params{'barcode_cid'}
+    if $params{'barcode_cid'};
 
   my( $pr_total, @pr_cust_bill ) = $self->previous; #previous balance
 #  my( $cr_total, @cr_cust_credit ) = $self->cust_credit; #credits
@@ -3246,7 +3285,7 @@ sub print_html {
   }
 
   $params{'format'} = 'html';
-
+  
   $self->print_generic( %params );
 }
 
index 2073d7f..fc553d5 100644 (file)
                        : ''
       %>
       <td align="right">
+    <%=
+       if($barcode_cid) {
+           $OUT .= qq! <img src="cid:$barcode_cid"><br> !;
+       }
+       elsif($barcode_img) {
+           $OUT .= qq! <img src="cust_bill-barcode.cgi?invnum=$invnum;template=$template"><br> !;
+       }
+    %>
         Terms: <%= $terms %><BR>
         <%= $po_line %>
       </td>
diff --git a/httemplate/view/cust_bill-barcode.cgi b/httemplate/view/cust_bill-barcode.cgi
new file mode 100755 (executable)
index 0000000..dd8f8b8
--- /dev/null
@@ -0,0 +1,18 @@
+<% $png %>
+<%init>
+
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('View invoices');
+
+my $conf = new FS::Conf;
+
+die 'invalid query' unless $cgi->param('invnum');
+
+my $cust_bill = qsearchs('cust_bill', { 'invnum' => $cgi->param('invnum') } )
+or die 'unknown invnum';
+
+my $png = $cust_bill->invoice_barcode(0);
+
+http_header('Content-Type' => 'image/png' );
+
+</%init>
index 0928d04..3d3fb70 100755 (executable)
@@ -125,6 +125,8 @@ my %opt = (
   'notice_name'   => $notice_name,
 );
 
+$opt{'barcode_img'} = 1 if $conf->exists('invoice-barcode');
+
 my @payby =  grep /\w/, $conf->config('payby');
 #@payby = (qw( CARD DCRD CHEK DCHK LECB BILL CASH WEST COMP ))
 @payby = (qw( CARD DCRD CHEK DCHK LECB BILL CASH COMP ))