(bug #2908) adding send shelf by e-mail feature
[koha.git] / C4 / Labels.pm
index 050ee5d..2330713 100644 (file)
@@ -18,6 +18,7 @@ package C4::Labels;
 # Suite 330, Boston, MA  02111-1307 USA
 
 use strict;
+# use warnings;   # FIXME
 use vars qw($VERSION @ISA @EXPORT);
 
 use PDF::Reuse;
@@ -41,8 +42,11 @@ BEGIN {
                &GetAllLabelTemplates &DeleteTemplate
                &GetSingleLabelTemplate &SaveTemplate
                &CreateTemplate &SetActiveTemplate
-               &SaveConf &DrawSpineText &GetTextWrapCols
-               &GetUnitsValue &DrawBarcode &DrawPatronCardText
+               &SaveConf &GetTextWrapCols
+               &GetUnitsValue
+        &DrawSpineText
+        &DrawBarcode
+        &DrawPatronCardText
                &get_printingtypes &GetPatronCardItems
                &get_layouts
                &get_barcode_types
@@ -56,9 +60,9 @@ BEGIN {
                &delete_layout &get_active_layout
                &get_highest_batch
                &deduplicate_batch
-                &GetAllPrinterProfiles &GetSinglePrinterProfile
-                &SaveProfile &CreateProfile &DeleteProfile
-                &GetAssociatedProfile &SetAssociatedProfile
+        &GetAllPrinterProfiles &GetSinglePrinterProfile
+        &SaveProfile &CreateProfile &DeleteProfile
+        &GetAssociatedProfile &SetAssociatedProfile
        );
 }
 
@@ -69,9 +73,7 @@ C4::Labels - Functions for printing spine labels and barcodes in Koha
 
 =head1 FUNCTIONS
 
-=over 2
-
-=item get_label_options;
+=head2 get_label_options;
 
        $options = get_label_options()
 
@@ -88,30 +90,25 @@ sub get_label_options {
 
 sub get_layouts {
     my $dbh = C4::Context->dbh;
-    my @data;
     my $query = " Select * from labels_conf";
     my $sth   = $dbh->prepare($query);
     $sth->execute();
     my @resultsloop;
     while ( my $data = $sth->fetchrow_hashref ) {
-
         $data->{'fieldlist'} = get_text_fields( $data->{'id'} );
         push( @resultsloop, $data );
     }
-    $sth->finish;
     return @resultsloop;
 }
 
 sub get_layout {
     my ($layout_id) = @_;
     my $dbh = C4::Context->dbh;
-
     # get the actual items to be printed.
     my $query = " Select * from labels_conf where id = ?";
     my $sth   = $dbh->prepare($query);
     $sth->execute($layout_id);
     my $data = $sth->fetchrow_hashref;
-    $sth->finish;
     return $data;
 }
 
@@ -125,12 +122,10 @@ sub get_active_layout {
 sub delete_layout {
     my ($layout_id) = @_;
     my $dbh = C4::Context->dbh;
-
     # get the actual items to be printed.
     my $query = "delete from  labels_conf where id = ?";
     my $sth   = $dbh->prepare($query);
     $sth->execute($layout_id);
-    $sth->finish;
 }
 
 sub get_printingtypes {
@@ -257,23 +252,28 @@ sub get_text_fields {
         }
         return $active_fields;
     }
-
 }
 
 =head2 sub add_batch
+
 =over 4
+
  add_batch($batch_type,\@batch_list);
  if $batch_list is supplied,
    create a new batch with those items.
  else, return the next available batch_id.
-=return
+
+=back
+
 =cut
 
 sub add_batch ($;$) {
        my $table = (@_ and 'patroncards' eq shift) ? 'patroncards' : 'labels';
     my $batch_list = (@_) ? shift : undef;
     my $dbh = C4::Context->dbh;
-    my $q ="SELECT MAX(DISTINCT batch_id) FROM $table";
+    # FIXME : batch_id  should be an auto_incr INT.  Temporarily casting as int ( see koha bug 2555 )
+    # until a label_batches table is added, and we can convert batch_id to int.
+    my $q ="SELECT MAX( CAST(batch_id AS SIGNED) ) FROM $table";
     my $sth = $dbh->prepare($q);
     $sth->execute();
     my ($batch_id) = $sth->fetchrow_array || 0;
@@ -315,12 +315,10 @@ sub get_batches (;$) {
 
 sub delete_batch {
     my ($batch_id, $batch_type) = @_;
-    warn "Deleteing batch of type $batch_type";
-    my $dbh        = C4::Context->dbh;
-    my $q          = "DELETE FROM $batch_type WHERE batch_id  = ?";
-    my $sth        = $dbh->prepare($q);
+    warn "Deleteing batch (id:$batch_id) of type $batch_type";
+    my $q   = "DELETE FROM $batch_type WHERE batch_id  = ?";
+    my $sth = C4::Context->dbh->prepare($q);
     $sth->execute($batch_id);
-    $sth->finish;
 }
 
 sub get_barcode_types {
@@ -338,7 +336,6 @@ sub get_barcode_types {
         if ( $line->{'code'} eq $barcode ) {
             $line->{'active'} = 1;
         }
-
     }
     return @array;
 }
@@ -346,7 +343,6 @@ sub get_barcode_types {
 sub GetUnitsValue {
     my ($units) = @_;
     my $unitvalue;
-
     $unitvalue = '1'          if ( $units eq 'POINT' );
     $unitvalue = '2.83464567' if ( $units eq 'MM' );
     $unitvalue = '28.3464567' if ( $units eq 'CM' );
@@ -377,7 +373,6 @@ sub GetActiveLabelTemplate {
     my $sth   = $dbh->prepare($query);
     $sth->execute();
     my $active_tmpl = $sth->fetchrow_hashref;
-    $sth->finish;
     return $active_tmpl;
 }
 
@@ -388,14 +383,11 @@ sub GetSingleLabelTemplate {
     my $sth       = $dbh->prepare($query);
     $sth->execute($tmpl_id);
     my $template = $sth->fetchrow_hashref;
-    $sth->finish;
     return $template;
 }
 
 sub SetActiveTemplate {
-
     my ($tmpl_id) = @_;
-  
     my $dbh   = C4::Context->dbh;
     my $query = " UPDATE labels_templates SET active = NULL";
     my $sth   = $dbh->prepare($query);
@@ -404,11 +396,9 @@ sub SetActiveTemplate {
     $query = "UPDATE labels_templates SET active = 1 WHERE tmpl_id = ?";
     $sth   = $dbh->prepare($query);
     $sth->execute($tmpl_id);
-    $sth->finish;
 }
 
 sub set_active_layout {
-
     my ($layout_id) = @_;
     my $dbh         = C4::Context->dbh;
     my $query       = " UPDATE labels_conf SET active = NULL";
@@ -418,7 +408,6 @@ sub set_active_layout {
     $query = "UPDATE labels_conf SET active = 1 WHERE id = ?";
     $sth   = $dbh->prepare($query);
     $sth->execute($layout_id);
-    $sth->finish;
 }
 
 sub DeleteTemplate {
@@ -427,7 +416,6 @@ sub DeleteTemplate {
     my $query     = " DELETE  FROM labels_templates where tmpl_id = ?";
     my $sth       = $dbh->prepare($query);
     $sth->execute($tmpl_id);
-    $sth->finish;
 }
 
 sub SaveTemplate {
@@ -454,7 +442,6 @@ sub SaveTemplate {
         $font,        $fontsize,     $units,      $tmpl_id
     );
     my $dberror = $sth->errstr;
-    $sth->finish;
     return $dberror;
 }
 
@@ -482,13 +469,11 @@ sub CreateTemplate {
         $font,        $fontsize,    $units
     );
     my $dberror = $sth->errstr;
-    $sth->finish;
     return $dberror;
 }
 
 sub GetAllLabelTemplates {
     my $dbh = C4::Context->dbh;
-
     # get the actual items to be printed.
     my @data;
     my $query = " Select * from labels_templates ";
@@ -498,8 +483,6 @@ sub GetAllLabelTemplates {
     while ( my $data = $sth->fetchrow_hashref ) {
         push( @resultsloop, $data );
     }
-    $sth->finish;
-
     #warn Dumper @resultsloop;
     return @resultsloop;
 }
@@ -509,8 +492,8 @@ sub add_layout {
 
     my (
         $barcodetype,  $title,                 $subtitle,      $isbn,       $issn,
-        $itemtype,     $bcn,            $dcn,        $classif,
-        $subclass,     $itemcallnumber, $author,     $tmpl_id,
+        $itemtype,     $bcn,            $text_justify,        $callnum_split,
+        $itemcallnumber, $author,     $tmpl_id,
         $printingtype, $guidebox,       $startlabel, $layoutname, $formatstring
     ) = @_;
 
@@ -520,29 +503,25 @@ sub add_layout {
     $sth2->execute();
     $query2 = "INSERT INTO labels_conf
             ( barcodetype, title, subtitle, isbn,issn, itemtype, barcode,
-              dewey, classification, subclass, itemcallnumber, author, printingtype,
+              text_justify, callnum_split, itemcallnumber, author, printingtype,
                 guidebox, startlabel, layoutname, formatstring, active )
-               values ( ?, ?,?, ?, ?, ?, ?, ?,  ?,?, ?, ?, ?, ?, ?,?,?, 1 )";
+               values ( ?, ?,?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?,?, 1 )";
     $sth2 = $dbh->prepare($query2);
     $sth2->execute(
         $barcodetype, $title, $subtitle, $isbn, $issn,
-
-        $itemtype, $bcn,            $dcn,    $classif,
-        $subclass, $itemcallnumber, $author, $printingtype,
+        $itemtype, $bcn,            $text_justify,    $callnum_split,
+        $itemcallnumber, $author, $printingtype,
         $guidebox, $startlabel,     $layoutname, $formatstring
     );
-    $sth2->finish;
-
     SetActiveTemplate($tmpl_id);
-    return;
 }
 
 sub save_layout {
 
     my (
         $barcodetype,  $title,          $subtitle,     $isbn,       $issn,
-        $itemtype,     $bcn,            $dcn,        $classif,
-        $subclass,     $itemcallnumber, $author,     $tmpl_id,
+        $itemtype,     $bcn,            $text_justify,        $callnum_split,
+        $itemcallnumber, $author,     $tmpl_id,
         $printingtype, $guidebox,       $startlabel, $layoutname, $formatstring,
         $layout_id
     ) = @_;
@@ -552,22 +531,19 @@ sub save_layout {
     my $dbh    = C4::Context->dbh;
     my $query2 = "update labels_conf set 
              barcodetype=?, title=?, subtitle=?, isbn=?,issn=?, 
-            itemtype=?, barcode=?,    dewey=?, classification=?,
-             subclass=?, itemcallnumber=?, author=?,  printingtype=?,  
+            itemtype=?, barcode=?,    text_justify=?, callnum_split=?,
+            itemcallnumber=?, author=?,  printingtype=?,  
                guidebox=?, startlabel=?, layoutname=?, formatstring=? where id = ?";
     my $sth2 = $dbh->prepare($query2);
     $sth2->execute(
         $barcodetype, $title,          $subtitle,      $isbn,       $issn,
-        $itemtype,    $bcn,            $dcn,        $classif,
-        $subclass,    $itemcallnumber, $author,     $printingtype,
+        $itemtype,    $bcn,            $text_justify,        $callnum_split,
+        $itemcallnumber, $author,     $printingtype,
         $guidebox,    $startlabel,     $layoutname, $formatstring,  $layout_id
     );
-    $sth2->finish;
-
-    return;
 }
 
-=item GetAllPrinterProfiles;
+=head2 GetAllPrinterProfiles;
 
     @profiles = GetAllPrinterProfiles()
 
@@ -576,22 +552,19 @@ Returns an array of references-to-hash, whos keys are .....
 =cut
 
 sub GetAllPrinterProfiles {
-
     my $dbh = C4::Context->dbh;
     my @data;
-    my $query = "SELECT * FROM printers_profile AS pp INNER JOIN labels_templates AS lt ON pp.tmpl_id = lt.tmpl_id";
+    my $query = "SELECT * FROM printers_profile AS pp INNER JOIN labels_templates AS lt ON pp.tmpl_id = lt.tmpl_id";
     my $sth = $dbh->prepare($query);
     $sth->execute();
     my @resultsloop;
     while ( my $data = $sth->fetchrow_hashref ) {
         push( @resultsloop, $data );
     }
-    $sth->finish;
-
     return @resultsloop;
 }
 
-=item GetSinglePrinterProfile;
+=head2 GetSinglePrinterProfile;
 
     $profile = GetSinglePrinterProfile()
 
@@ -601,16 +574,14 @@ Returns a hashref whos keys are...
 
 sub GetSinglePrinterProfile {
     my ($prof_id) = @_;
-    my $dbh       = C4::Context->dbh;
-    my $query     = " SELECT * FROM printers_profile WHERE prof_id = ?; ";
-    my $sth       = $dbh->prepare($query);
+    my $query     = "SELECT * FROM printers_profile WHERE prof_id = ?";
+    my $sth       = C4::Context->dbh->prepare($query);
     $sth->execute($prof_id);
     my $template = $sth->fetchrow_hashref;
-    $sth->finish;
     return $template;
 }
 
-=item SaveProfile;
+=head2 SaveProfile;
 
     SaveProfile('parameters')
 
@@ -631,10 +602,9 @@ sub SaveProfile {
     $sth->execute(
         $offset_horz,   $offset_vert,   $creep_horz,    $creep_vert,    $units,         $prof_id
     );
-    $sth->finish;
 }
 
-=item CreateProfile;
+=head2 CreateProfile;
 
     CreateProfile('parameters')
 
@@ -659,11 +629,10 @@ sub CreateProfile {
         $offset_vert,   $creep_horz,    $creep_vert,    $units
     );
     my $error =  $sth->errstr;
-    $sth->finish;
     return $error;
 }
 
-=item DeleteProfile;
+=head2 DeleteProfile;
 
     DeleteProfile(prof_id)
 
@@ -678,11 +647,10 @@ sub DeleteProfile {
     my $sth       = $dbh->prepare($query);
     $sth->execute($prof_id);
     my $error = $sth->errstr;
-    $sth->finish;
     return $error;
 }
 
-=item GetAssociatedProfile;
+=head2 GetAssociatedProfile;
 
     $assoc_prof = GetAssociatedProfile(tmpl_id)
 
@@ -698,14 +666,13 @@ sub GetAssociatedProfile {
     my $query = "SELECT * FROM labels_profile WHERE tmpl_id = ?";
     my $sth   = $dbh->prepare($query);
     $sth->execute($tmpl_id);
-    my $assoc_prof = $sth->fetchrow_hashref;
-    $sth->finish;
+    my $assoc_prof = $sth->fetchrow_hashref or return;
     # Then we retrieve that profile and return it to the caller...
     $assoc_prof = GetSinglePrinterProfile($assoc_prof->{'prof_id'});
     return $assoc_prof;
 }
 
-=item SetAssociatedProfile;
+=head2 SetAssociatedProfile;
 
     SetAssociatedProfile($prof_id, $tmpl_id)
 
@@ -715,18 +682,15 @@ than one profile may be associated with any given template at the same time.
 =cut
 
 sub SetAssociatedProfile {
-
     my ($prof_id, $tmpl_id) = @_;
-  
     my $dbh = C4::Context->dbh;
     my $query = "INSERT INTO labels_profile (prof_id, tmpl_id) VALUES (?,?) ON DUPLICATE KEY UPDATE prof_id = ?";
     my $sth = $dbh->prepare($query);
     $sth->execute($prof_id, $tmpl_id, $prof_id);
-    $sth->finish;
 }
 
 
-=item GetLabelItems;
+=head2 GetLabelItems;
 
         $options = GetLabelItems()
 
@@ -800,15 +764,18 @@ sub GetItemFields {
     return @fields;
 }
 
-=head GetBarcodeData
+=head2 GetBarcodeData
 
 =over 4
+
 Parse labels_conf.formatstring value
 (one value of the csv, which has already been split)
 and return string from koha tables or MARC record.
+
 =back
+
 =cut
-#'
+
 sub GetBarcodeData {
     my ( $f, $item, $record ) = @_;
     my $kohatables = &_descKohaTables();
@@ -821,7 +788,7 @@ sub GetBarcodeData {
             @{ $kohatables->{items} }
         )
     );
-    while ($f) {
+    while ($f) {  
         $f =~ s/^\s?//;
         if ( $f =~ /^'(.*)'.*/ ) {
             # single quotes indicate a static text string.
@@ -833,25 +800,37 @@ sub GetBarcodeData {
             $f = $';
         }
         elsif ( $f =~ /^([0-9a-z]{3})(\w)(\W?).*?/ ) {
-            my $marc_field = $1;
-            foreach my $subfield ($record->field($marc_field)) {
-                if ( $subfield->subfield('9') eq $item->{'itemnumber'} ) {
-                    $datastring .= $subfield->subfield($2 ) . $3;
-                    last;
+            my ($field,$subf,$ws) = ($1,$2,$3);
+            my $subf_data;
+            my ($itemtag, $itemsubfieldcode) = &GetMarcFromKohaField("items.itemnumber",'');
+            my @marcfield = $record->field($field);
+            if(@marcfield) {
+                if($field eq $itemtag) {  # item-level data, we need to get the right item.
+                    foreach my $itemfield (@marcfield) {
+                        if ( $itemfield->subfield($itemsubfieldcode) eq $item->{'itemnumber'} ) {
+                            $datastring .= $itemfield->subfield($subf ) . $ws;
+                            last;
+                        }
+                    }
+                } else {  # bib-level data, we'll take the first matching tag/subfield.
+                    $datastring .= $marcfield[0]->subfield($subf) . $ws ;
                 }
             }
             $f = $';
         }
         else {
+            warn "failed to parse label formatstring: $f";
             last;    # Failed to match
         }
     }
     return $datastring;
 }
 
-=head descKohaTables
+=head2 descKohaTables
+
 Return a hashref of an array of hashes,
 with name,type keys.
+
 =cut
 
 sub _descKohaTables {
@@ -862,13 +841,11 @@ sub _descKohaTables {
                while (my $info = $sth->fetchrow_hashref()){
                        push @{$kohatables->{$table}} , $info->{'COLUMN_NAME'} ;
                }
-               $sth->finish;
        }
        return $kohatables;
 }
 
 sub GetPatronCardItems {
-
     my ( $batch_id ) = @_;
     my @resultsloop;
     
@@ -887,9 +864,7 @@ sub GetPatronCardItems {
         push( @resultsloop, $patron_data );
         $cardno++;
     }
-    $sth->finish;
     return @resultsloop;
-
 }
 
 sub deduplicate_batch {
@@ -986,30 +961,39 @@ sub split_fcn {
     return @fcn_split;
 }
 
-sub DrawSpineText {
+my %itemtypemap;
+# Class variable to avoid querying itemtypes for every DrawSpineText call!!
+sub get_itemtype_descriptions () {
+    unless (scalar keys %itemtypemap) {
+        my $sth = C4::Context->dbh->prepare("SELECT itemtype,description FROM itemtypes");
+        $sth->execute();
+        while (my $data = $sth->fetchrow_hashref) {
+            $itemtypemap{$data->{itemtype}} = $data->{description};
+        }
+    }
+    return \%itemtypemap;
+}
 
+sub DrawSpineText {
     my ( $x_pos, $y_pos, $label_height, $label_width, $fontname, $fontsize, $left_text_margin,
         $text_wrap_cols, $item, $conf_data, $printingtype ) = @_;
     
-    # Replaced item's itemtype with the more user-friendly description...
-    my $dbh = C4::Context->dbh;
-    my $sth = $dbh->prepare("SELECT itemtype,description FROM itemtypes");
-    $sth->execute();
-    while ( my $data = $sth->fetchrow_hashref ) {
-        $$item->{'itemtype'} = $data->{'description'} if ($$item->{'itemtype'} eq $data->{'itemtype'});
-        $$item->{'itype'} = $data->{'description'} if ($$item->{'itype'} eq $data->{'itemtype'});
+    # Replace item's itemtype with the more user-friendly description...
+    my $descriptions = get_itemtype_descriptions();
+    foreach (qw(itemtype itype)) {
+        my $description = $descriptions->{$$item->{$_}} or next;
+        $$item->{$_} = $description;
     }
-
     my $str = '';
 
     my $top_text_margin = ( $fontsize + 3 );    #FIXME: This should be a template parameter and passed in...
-    my $line_spacer = ( $fontsize * 1 );    # number of pixels between text rows (This is actually leading: baseline to baseline minus font size. Recommended starting point is 20% of font size.).
+    my $line_spacer     = ( $fontsize * 1 );    # number of pixels between text rows (This is actually leading: baseline to baseline minus font size. Recommended starting point is 20% of font size.).
 
     my $layout_id = $$conf_data->{'id'};
 
     my $vPos = ( $y_pos + ( $label_height - $top_text_margin ) );
 
-    my @str_fields = get_text_fields($layout_id, 'codes' );
+    my @str_fields = get_text_fields($layout_id, 'codes' );  
     my $record = GetMarcBiblio($$item->{biblionumber});
     # FIXME - returns all items, so you can't get data from an embedded holdings field.
     # TODO - add a GetMarcBiblio1item(bibnum,itemnum) or a GetMarcItem(itemnum).
@@ -1018,17 +1002,17 @@ sub DrawSpineText {
 
     # Grab the cn_source and if that is NULL, the DefaultClassificationSource syspref
     my $cn_source = ($$item->{'cn_source'} ? $$item->{'cn_source'} : C4::Context->preference('DefaultClassificationSource'));
-
     for my $field (@str_fields) {
         $field->{'code'} or warn "get_text_fields($layout_id, 'codes') element missing 'code' field";
-        if ($$conf_data->{'formatstring'}) {
-            $field->{'data'} =  GetBarcodeData($field->{'code'},$$item,$record) ;
-        }
-        elsif ($field->{'code'} eq 'itemtype') {
+        if ($field->{'code'} eq 'itemtype') {
             $field->{'data'} = C4::Context->preference('item-level_itypes') ? $$item->{'itype'} : $$item->{'itemtype'};
         }
+        elsif ($$conf_data->{'formatstring'}) {
+            # if labels_conf.formatstring has a value, then it overrides the  hardcoded option.
+            $field->{'data'} = GetBarcodeData($field->{'code'},$$item,$record) ;
+        }
         else {
-            $field->{data} =   $$item->{$field->{'code'}}  ;
+            $field->{'data'} = $$item->{$field->{'code'}};
         }
         # This allows us to print the title in italic (oblique) type... (Times Roman has a different nomenclature.)
         # It seems there should be a better way to handle fonts in the label/patron card tool altogether -fbcit
@@ -1044,8 +1028,8 @@ sub DrawSpineText {
             $str =~ s/\n//g;
             $str =~ s/\r//g;
             my @strings;
-            my @callnumber_list = ('itemcallnumber', '050a', '050b', '082a', '952o'); # Fields which hold call number data
-            if ((grep {$field->{code} =~ m/$_/} @callnumber_list) and ($printingtype eq 'BIB')) { # If the field contains the call number, we do some sp
+            my @callnumber_list = ('itemcallnumber', '050a', '050b', '082a', '952o'); # Fields which hold call number data  ( 060? 090? 092? 099? )
+            if ((grep {$field->{code} =~ m/$_/} @callnumber_list) and ($printingtype eq 'BIB') and ($$conf_data->{'callnum_split'})) { # If the field contains the call number, we do some sp
                 if ($cn_source eq 'lcc') {
                     @strings = split_lccn($str);
                     @strings = split_fcn($str) if !@strings;    # If it was not a true lccn, try it as a fiction call number
@@ -1066,31 +1050,33 @@ sub DrawSpineText {
                 $Text::Wrap::columns = $text_wrap_cols;
                 my @line = split(/\n/ ,wrap('', '', $str));
                 # If this is a title field, limit to two lines; all others limit to one...
-                if ($field->{code} eq 'title' && scalar(@line) >= 2) {
-                    while (scalar(@line) > 2) {
-                        pop @line;
-                    }
-                } else {
-                    while (scalar(@line) > 1) {
-                        pop @line;
-                    }
+                my $limit = ($field->{code} eq 'title') ? 2 : 1;
+                while (scalar(@line) > $limit) {
+                    pop @line;
                 }
                 push(@strings, @line);
             }
             # loop for each string line
             foreach my $str (@strings) {
-                my $hPos = 0;
-                if ( $printingtype eq 'BIB' ) { #FIXME: This is a hack and needs to be implimented as a text justification option in the template...
+                my $hPos = $x_pos;
+                my $stringwidth = prStrWidth($str, $fontname, $fontsize);
+                if ( $$conf_data->{'text_justify'} eq 'R' ) { 
+                    $hPos += $label_width - ($left_text_margin + $stringwidth);
+                } elsif($$conf_data->{'text_justify'} eq 'C') {
                     # some code to try and center each line on the label based on font size and string point width...
-                    my $stringwidth = prStrWidth($str, $fontname, $fontsize);
                     my $whitespace = ( $label_width - ( $stringwidth + (2 * $left_text_margin) ) );
-                    $hPos = ( ( $whitespace  / 2 ) + $x_pos + $left_text_margin );
+                    $hPos += ($whitespace / 2) + $left_text_margin;
                     #warn "\$label_width=$label_width \$stringwidth=$stringwidth \$whitespace=$whitespace \$left_text_margin=$left_text_margin for $str\n";
                 } else {
-                    $hPos = ( $x_pos + $left_text_margin );
+                    $hPos += $left_text_margin;
                 }
+# utf8::encode($str);
+# Say $str has a diacritical like: The séance 
+# WITOUT encode, PrintText crashes with: Wide character in syswrite at /usr/local/share/perl/5.8.8/PDF/Reuse.pm line 968
+# WITH   encode, PrintText prints: The seÌ•ancee
+# Neither is appropriate.
                 PrintText( $hPos, $vPos, $font, $fontsize, $str );
-                $vPos = $vPos - $line_spacer;
+                $vPos -= $line_spacer;
             }
        }
     }  #foreach field
@@ -1103,7 +1089,6 @@ sub PrintText {
 }
 
 sub DrawPatronCardText {
-
     my ( $x_pos, $y_pos, $label_height, $label_width, $fontname, $fontsize, $left_text_margin,
         $text_wrap_cols, $text, $printingtype )
       = @_;
@@ -1139,7 +1124,6 @@ sub DrawPatronCardText {
 #}
 
 sub DrawBarcode {
-
     # x and y are from the top-left :)
     my ( $x_pos, $y_pos, $height, $width, $barcode, $barcodetype ) = @_;
     my $num_of_bars = length($barcode);
@@ -1164,13 +1148,8 @@ sub DrawBarcode {
                 hide_asterisk => 1,
             );
         };
-        if ($@) {
-            warn "$barcodetype, $barcode FAILED:$@";
-        }
     }
-
     elsif ( $barcodetype eq 'CODE39MOD' ) {
-
         # get modulo43 checksum
         my $c39 = CheckDigits('code_39');
         $barcode = $c39->complete($barcode);
@@ -1189,13 +1168,8 @@ sub DrawBarcode {
                 hide_asterisk => 1,
             );
         };
-
-        if ($@) {
-            warn "$barcodetype, $barcode FAILED:$@";
-        }
     }
     elsif ( $barcodetype eq 'CODE39MOD10' ) {
         # get modulo43 checksum
         my $c39_10 = CheckDigits('visa');
         $barcode = $c39_10->complete($barcode);
@@ -1212,16 +1186,10 @@ sub DrawBarcode {
                 ySize         => ( .02 * $height ),
                 xSize         => $xsize_ratio,
                 hide_asterisk => 1,
-                               text         => 0, 
+                               text          => 0, 
             );
         };
-
-        if ($@) {
-            warn "$barcodetype, $barcode FAILED:$@";
-        }
     }
-
     elsif ( $barcodetype eq 'COOP2OF5' ) {
         $bar_length = '9.43333333333333';
         $tot_bar_length =
@@ -1236,11 +1204,7 @@ sub DrawBarcode {
                 xSize => $xsize_ratio,
             );
         };
-        if ($@) {
-            warn "$barcodetype, $barcode FAILED:$@";
-        }
     }
-
     elsif ( $barcodetype eq 'INDUSTRIAL2OF5' ) {
         $bar_length = '13.1333333333333';
         $tot_bar_length =
@@ -1255,136 +1219,88 @@ sub DrawBarcode {
                 xSize => $xsize_ratio,
             );
         };
-        if ($@) {
-            warn "$barcodetype, $barcode FAILED:$@";
-        }
+    } # else {die "Unknown barcodetype '$barcodetype'";}
+
+    if ($@) {
+        warn "DrawBarcode (type: $barcodetype) FAILED for value '$barcode' :$@";
     }
 
     my $moo2 = $tot_bar_length * $xsize_ratio;
 
-    warn "$x_pos, $y_pos, $barcode, $barcodetype" if $debug;
-    warn "BAR_WDTH = $bar_width, TOT.BAR.LGHT=$tot_bar_length  R*TOT.BAR =$moo2" if $debug;
+    warn "x_pos,y_pos,barcode,barcodetype = $x_pos, $y_pos, $barcode, $barcodetype\n"
+        . "BAR_WDTH = $bar_width, TOT.BAR.LGHT=$tot_bar_length  R*TOT.BAR =$moo2" if $debug;
 }
 
-=item build_circ_barcode;
+=head2 build_circ_barcode;
 
-  build_circ_barcode( $x_pos, $y_pos, $barcode,
-               $barcodetype, \$item);
+  build_circ_barcode( $x_pos, $y_pos, $barcode, $barcodetype, \$item);
 
 $item is the result of a previous call to GetLabelItems();
 
 =cut
 
-#'
 sub build_circ_barcode {
     my ( $x_pos_circ, $y_pos, $value, $barcodetype, $item ) = @_;
 
     #warn Dumper \$item;
-
-    #warn "value = $value\n";
-
+    #warn "Barcode (type: $barcodetype) value = $value\n";
     #$DB::single = 1;
 
     if ( $barcodetype eq 'EAN13' ) {
-
         #testing EAN13 barcodes hack
         $value = $value . '000000000';
         $value =~ s/-//;
         $value = substr( $value, 0, 12 );
-
-        #warn $value;
+        #warn "revised value: $value";
         eval {
             PDF::Reuse::Barcode::EAN13(
                 x     => ( $x_pos_circ + 27 ),
                 y     => ( $y_pos + 15 ),
                 value => $value,
-
-                #            prolong => 2.96,
-                #            xSize   => 1.5,
-
+                # prolong => 2.96,
+                # xSize   => 1.5,
                 # ySize   => 1.2,
-
 # added for xpdf compat. doesnt use type3 fonts., but increases filesize from 20k to 200k
 # i think its embedding extra fonts in the pdf file.
 #  mode => 'graphic',
             );
         };
-        if ($@) {
-            $item->{'barcodeerror'} = 1;
-
-            #warn "EAN13BARCODE FAILED:$@";
-        }
-
-        #warn $barcodetype;
-
     }
     elsif ( $barcodetype eq 'Code39' ) {
-
         eval {
             PDF::Reuse::Barcode::Code39(
                 x     => ( $x_pos_circ + 9 ),
                 y     => ( $y_pos + 15 ),
                 value => $value,
-
-                #           prolong => 2.96,
+                # prolong => 2.96,
                 xSize => .85,
-
                 ySize => 1.3,
             );
         };
-        if ($@) {
-            $item->{'barcodeerror'} = 1;
-
-            #warn "CODE39BARCODE $value FAILED:$@";
-        }
-
-        #warn $barcodetype;
-
     }
-
     elsif ( $barcodetype eq 'Matrix2of5' ) {
-
-        #warn "MATRIX ELSE:";
-
-        #testing MATRIX25  barcodes hack
-        #    $value = $value.'000000000';
+        # testing MATRIX25  barcodes hack
+        # $value = $value.'000000000';
         $value =~ s/-//;
-
-        #    $value = substr( $value, 0, 12 );
-        #warn $value;
-
+        # $value = substr( $value, 0, 12 );
+        #warn "revised value: $value";
         eval {
             PDF::Reuse::Barcode::Matrix2of5(
                 x     => ( $x_pos_circ + 27 ),
                 y     => ( $y_pos + 15 ),
                 value => $value,
-
-                #        prolong => 2.96,
-                #       xSize   => 1.5,
-
+                # prolong => 2.96,
+                # xSize   => 1.5,
                 # ySize   => 1.2,
             );
         };
-        if ($@) {
-            $item->{'barcodeerror'} = 1;
-
-            #warn "BARCODE FAILED:$@";
-        }
-
-        #warn $barcodetype;
-
     }
-
     elsif ( $barcodetype eq 'EAN8' ) {
-
         #testing ean8 barcodes hack
         $value = $value . '000000000';
         $value =~ s/-//;
         $value = substr( $value, 0, 8 );
-
-        #warn $value;
-
-        #warn "EAN8 ELSEIF";
+        #warn "revised value: $value";
         eval {
             PDF::Reuse::Barcode::EAN8(
                 x       => ( $x_pos_circ + 42 ),
@@ -1392,21 +1308,10 @@ sub build_circ_barcode {
                 value   => $value,
                 prolong => 2.96,
                 xSize   => 1.5,
-
                 # ySize   => 1.2,
             );
         };
-
-        if ($@) {
-            $item->{'barcodeerror'} = 1;
-
-            #warn "BARCODE FAILED:$@";
-        }
-
-        #warn $barcodetype;
-
     }
-
     elsif ( $barcodetype eq 'UPC-E' ) {
         eval {
             PDF::Reuse::Barcode::UPCE(
@@ -1415,19 +1320,9 @@ sub build_circ_barcode {
                 value   => $value,
                 prolong => 2.96,
                 xSize   => 1.5,
-
                 # ySize   => 1.2,
             );
         };
-
-        if ($@) {
-            $item->{'barcodeerror'} = 1;
-
-            #warn "BARCODE FAILED:$@";
-        }
-
-        #warn $barcodetype;
-
     }
     elsif ( $barcodetype eq 'NW7' ) {
         eval {
@@ -1437,19 +1332,9 @@ sub build_circ_barcode {
                 value   => $value,
                 prolong => 2.96,
                 xSize   => 1.5,
-
                 # ySize   => 1.2,
             );
         };
-
-        if ($@) {
-            $item->{'barcodeerror'} = 1;
-
-            #warn "BARCODE FAILED:$@";
-        }
-
-        #warn $barcodetype;
-
     }
     elsif ( $barcodetype eq 'ITF' ) {
         eval {
@@ -1459,19 +1344,9 @@ sub build_circ_barcode {
                 value   => $value,
                 prolong => 2.96,
                 xSize   => 1.5,
-
                 # ySize   => 1.2,
             );
         };
-
-        if ($@) {
-            $item->{'barcodeerror'} = 1;
-
-            #warn "BARCODE FAILED:$@";
-        }
-
-        #warn $barcodetype;
-
     }
     elsif ( $barcodetype eq 'Industrial2of5' ) {
         eval {
@@ -1481,18 +1356,9 @@ sub build_circ_barcode {
                 value   => $value,
                 prolong => 2.96,
                 xSize   => 1.5,
-
                 # ySize   => 1.2,
             );
         };
-        if ($@) {
-            $item->{'barcodeerror'} = 1;
-
-            #warn "BARCODE FAILED:$@";
-        }
-
-        #warn $barcodetype;
-
     }
     elsif ( $barcodetype eq 'IATA2of5' ) {
         eval {
@@ -1502,20 +1368,10 @@ sub build_circ_barcode {
                 value   => $value,
                 prolong => 2.96,
                 xSize   => 1.5,
-
                 # ySize   => 1.2,
             );
         };
-        if ($@) {
-            $item->{'barcodeerror'} = 1;
-
-            #warn "BARCODE FAILED:$@";
-        }
-
-        #warn $barcodetype;
-
     }
-
     elsif ( $barcodetype eq 'COOP2of5' ) {
         eval {
             PDF::Reuse::Barcode::COOP2of5(
@@ -1524,21 +1380,11 @@ sub build_circ_barcode {
                 value   => $value,
                 prolong => 2.96,
                 xSize   => 1.5,
-
                 # ySize   => 1.2,
             );
         };
-        if ($@) {
-            $item->{'barcodeerror'} = 1;
-
-            #warn "BARCODE FAILED:$@";
-        }
-
-        #warn $barcodetype;
-
     }
     elsif ( $barcodetype eq 'UPC-A' ) {
-
         eval {
             PDF::Reuse::Barcode::UPCA(
                 x       => ( $x_pos_circ + 27 ),
@@ -1546,23 +1392,17 @@ sub build_circ_barcode {
                 value   => $value,
                 prolong => 2.96,
                 xSize   => 1.5,
-
                 # ySize   => 1.2,
             );
         };
-        if ($@) {
-            $item->{'barcodeerror'} = 1;
-
-            #warn "BARCODE FAILED:$@";
-        }
-
-        #warn $barcodetype;
-
     }
-
+    if ($@) {
+        $item->{'barcodeerror'} = 1;
+        #warn "BARCODE (type: $barcodetype) FAILED:$@";
+    }
 }
 
-=item draw_boundaries
+=head2 draw_boundaries
 
  sub draw_boundaries ($x_pos_spine, $x_pos_circ1, $x_pos_circ2,
                 $y_pos, $spine_width, $label_height, $circ_width)  
@@ -1571,9 +1411,7 @@ This sub draws boundary lines where the label outlines are, to aid in printer te
 
 =cut
 
-#'
 sub draw_boundaries {
-
     my (
         $x_pos_spine, $x_pos_circ1,  $x_pos_circ2, $y_pos,
         $spine_width, $label_height, $circ_width
@@ -1584,19 +1422,15 @@ sub draw_boundaries {
     my $i             = 1;
 
     for ( $i = 1 ; $i <= 8 ; $i++ ) {
-
         &drawbox( $x_pos_spine, $y_pos, ($spine_width), ($label_height) );
-
    #warn "OLD BOXES  x=$x_pos_spine, y=$y_pos, w=$spine_width, h=$label_height";
         &drawbox( $x_pos_circ1, $y_pos, ($circ_width), ($label_height) );
         &drawbox( $x_pos_circ2, $y_pos, ($circ_width), ($label_height) );
-
         $y_pos = ( $y_pos - $label_height );
-
     }
 }
 
-=item drawbox
+=head2 drawbox
 
        sub drawbox {   $lower_left_x, $lower_left_y, 
                        $upper_right_x, $upper_right_y )
@@ -1609,10 +1443,8 @@ and $lower_left_x, $lower_left_y are ABSOLUTE, this caught me out!
 
 =cut
 
-#'
 sub drawbox {
     my ( $llx, $lly, $urx, $ury ) = @_;
-
     #    warn "llx,y= $llx,$lly  ,   urx,y=$urx,$ury \n";
 
     my $str = "q\n";    # save the graphic state
@@ -1626,16 +1458,11 @@ sub drawbox {
     $str .= "Q\n";                         # save the graphic state
 
     prAdd($str);
-
 }
 
-END { }    # module clean-up code here (global destructor)
-
 1;
 __END__
 
-=back
-
 =head1 AUTHOR
 
 Mason James <mason@katipo.co.nz>