Bug 1980: some cleanup, getting ready to add pagination to label-item-search.pl
[koha_fer] / labels / label-print-pdf.pl
index 2abacd6..f9bc090 100755 (executable)
@@ -6,11 +6,13 @@ use C4::Labels;
 use C4::Auth;
 use C4::Output;
 use C4::Context;
+use C4::Members;
+use C4::Branch;
 use HTML::Template::Pro;
 use PDF::Reuse;
 use PDF::Reuse::Barcode;
 use POSIX;
-#use C4::Labels;
+use Data::Dumper;
 #use Smart::Comments;
 
 my $DEBUG = 0;
@@ -27,13 +29,14 @@ my $spine_text = "";
 # get the printing settings
 my $template    = GetActiveLabelTemplate();
 my $conf_data   = get_label_options();
-my $profile = GetAssociatedProfile($template->{'tmpl_id'});
+my $profile     = GetAssociatedProfile($template->{'tmpl_id'});
 
 my $batch_id =   $cgi->param('batch_id');
-my @resultsloop = get_label_items($batch_id);
+my @resultsloop;
 
 #$DB::single = 1;
 
+my $batch_type   = $conf_data->{'type'};
 my $barcodetype  = $conf_data->{'barcodetype'};
 my $printingtype = $conf_data->{'printingtype'};
 my $guidebox     = $conf_data->{'guidebox'};
@@ -42,29 +45,23 @@ if ($cgi->param('startlabel')) {
         $start_label = $cgi->param('startlabel');       # A bit of a hack to allow setting the starting label from the address bar... -fbcit
     }
 warn "Starting on label #$start_label" if $DEBUG;
-my $fontsize     = $template->{'fontsize'};
 my $units        = $template->{'units'};
 
-### $printingtype;
-
-=c
-################### defaults for testing
-my $barcodetype  = 'CODE39';
-my $printingtype = 'BARBIB';
-my $guidebox     = 1;
-my $start_label  = 1;
-my $units        = 'POINTS'
-=cut
-
-#my $fontsize = 3;
+if ($printingtype eq 'PATCRD') {
+    @resultsloop = GetPatronCardItems($batch_id);
+} else {
+    @resultsloop = GetLabelItems($batch_id);
+}
 
 #warn "UNITS $units";
 #warn "fontsize = $fontsize";
 #warn Dumper $template;
 
 my $unitvalue = GetUnitsValue($units);
+my $prof_unitvalue = GetUnitsValue($profile->{'unit'});
 
 warn "Template units: $units which converts to $unitvalue PostScript Points" if $DEBUG;
+warn "Profile units: $profile->{'unit'} which converts to $prof_unitvalue PostScript Points" if $DEBUG;
 
 my $tmpl_code = $template->{'tmpl_code'};
 my $tmpl_desc = $template->{'tmpl_desc'};
@@ -86,8 +83,24 @@ warn "pghth=$page_height, pgwth=$page_width, lblhth=$label_height, lblwth=$label
 my $label_cols = $template->{'cols'};
 my $label_rows = $template->{'rows'};
 
-my $text_wrap_cols = GetTextWrapCols( $fontsize, $label_width );
+my $margin           = $top_margin;
+my $left_text_margin = 3;       # FIXME: This value should not be hardcoded
+my $str;
 
+prInitVars();
+$| = 1;
+prFile();
+
+# Some peritent notes from PDF::Reuse regarding prFont()...
+# If a font wasn't found, Helvetica will be set.
+# These names are always recognized: Times-Roman, Times-Bold, Times-Italic, Times-BoldItalic, Courier, Courier-Bold,
+#   Courier-Oblique, Courier-BoldOblique, Helvetica, Helvetica-Bold, Helvetica-Oblique, Helvetica-BoldOblique
+# They can be abbreviated: TR, TB, TI, TBI, C, CB, CO, CBO, H, HB, HO, HBO
+
+my $fontsize    = $template->{'fontsize'};
+my $fontname    = $template->{'font'};
+
+my $text_wrap_cols = GetTextWrapCols( $fontname, $fontsize, $label_width, $left_text_margin );
 
 #warn $label_cols, $label_rows;
 
@@ -97,20 +110,8 @@ my $lowerLeftY  = 0;
 my $upperRightX = $page_width;
 my $upperRightY = $page_height;
 
-prInitVars();
-$| = 1;
-prFile();
-
 prMbox( $lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY );
 
-# later feature, change the font-type and size?
-prFont('C');    # Just setting a font
-prFontSize($fontsize);
-
-my $margin           = $top_margin;
-my $left_text_margin = 3;
-my $str;
-
 #warn "STARTROW = $startrow\n";
 
 #my $page_break_count = $startrow;
@@ -127,27 +128,31 @@ drawbox( ($left_margin), ($top_margin), ($page_width-(2*$left_margin)), ($page_h
 # Suggested proceedure: Adjust margins until both top and left margins are correct. Then adjust the label
 # height and width to correct label creep across and down page. Units are PostScript Points (72 per inch).
 
-warn "Active profile: $profile->{'prof_id'}" if $DEBUG;
+warn "Active profile: " . ($profile->{'prof_id'}?$profile->{'prof_id'}:"None") if $DEBUG;
 
 if ( $DEBUG ) {
-warn "-------------------------INITIAL VALUES-----------------------------";
-warn "top margin = $top_margin points\n";
-warn "left margin = $left_margin points\n";
-warn "label height = $label_height points\n";
-warn "label width = $label_width points\n";
+    warn "-------------------------INITIAL VALUES-----------------------------";
+    warn "top margin = $top_margin points\n";
+    warn "left margin = $left_margin points\n";
+    warn "label height = $label_height points\n";
+    warn "label width = $label_width points\n";
 }
 
-$top_margin = $top_margin + $profile->{'offset_vert'};    #  controls vertical offset
-$label_height = $label_height + $profile->{'creep_vert'};    # controls vertical creep
-$left_margin = $left_margin + $profile->{'offset_horz'};    # controls horizontal offset
-$label_width = $label_width + $profile->{'creep_horz'};    # controls horizontal creep
+if ( $profile->{'prof_id'} ) {
+    $top_margin = $top_margin + ($profile->{'offset_vert'} * $prof_unitvalue);    #  controls vertical offset
+    $label_height = $label_height + ($profile->{'creep_vert'} * $prof_unitvalue);    # controls vertical creep
+    $left_margin = $left_margin + ($profile->{'offset_horz'} * $prof_unitvalue);    # controls horizontal offset
+    $label_width = $label_width + ($profile->{'creep_horz'} * $prof_unitvalue);    # controls horizontal creep
+}
 
-if ( $DEBUG ) {
-warn "-------------------------ADJUSTED VALUES-----------------------------";
-warn "top margin = $top_margin points\n";
-warn "left margin = $left_margin points\n";
-warn "label height = $label_height points\n";
-warn "label width = $label_width points\n";
+if ( $DEBUG && $profile->{'prof_id'} ) {
+    warn "-------------------------ADJUSTED VALUES-----------------------------";
+    warn "top margin = $top_margin points\n";
+    warn "left margin = $left_margin points\n";
+    warn "label height = $label_height points\n";
+    warn "label width = $label_width points\n";
+} elsif ( $DEBUG ) {
+    warn "No profile associated so no adjustment applied.";
 }
 
 my $item;
@@ -200,10 +205,9 @@ else {
 
 foreach $item (@resultsloop) {
     warn "Label parameters: xpos=$x_pos, ypos=$y_pos, lblwid=$label_width, lblhig=$label_height" if $DEBUG;
-    my $barcode = $item->{'barcode'};
     if ( $printingtype eq 'BAR' ) {
         drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
-        DrawBarcode( $x_pos, $y_pos, $label_height, $label_width, $barcode,
+        DrawBarcode( $x_pos, $y_pos, $label_height, $label_width, $item->{'barcode'},
             $barcodetype );
         CalcNextLabelPos();
     }
@@ -216,9 +220,9 @@ foreach $item (@resultsloop) {
         my $barcode_y      = $y_pos + ( $label_height / 2.5  );   ## scaling voodoo
 
         DrawBarcode( $x_pos, $barcode_y, $barcode_height, $label_width,
-            $barcode, $barcodetype );
-        DrawSpineText( $y_pos, $text_height, $fontsize, $x_pos,
-            $left_text_margin, $text_wrap_cols, \$item, \$conf_data );
+            $item->{'barcode'}, $barcodetype );
+        DrawSpineText( $x_pos, $y_pos, $label_height, $label_width, $fontname, $fontsize,
+            $left_text_margin, $text_wrap_cols, \$item, \$conf_data, $printingtype );
 
         CalcNextLabelPos();
 
@@ -226,22 +230,22 @@ foreach $item (@resultsloop) {
     elsif ( $printingtype eq 'BIBBAR' ) {
         drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
         my $barcode_height = $label_height / 2;
-        DrawBarcode( $x_pos, $y_pos, $barcode_height, $label_width, $barcode,
+        DrawBarcode( $x_pos, $y_pos, $barcode_height, $label_width, $item->{'barcode'},
             $barcodetype );
-        DrawSpineText( $y_pos, $label_height, $fontsize, $x_pos,
-            $left_text_margin, $text_wrap_cols, \$item, \$conf_data );
+        DrawSpineText( $x_pos, $y_pos, $label_height, $label_width, $fontname, $fontsize,
+            $left_text_margin, $text_wrap_cols, \$item, \$conf_data, $printingtype, '1' );
 
         CalcNextLabelPos();
     }
 
     elsif ( $printingtype eq 'ALT' ) {
         drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
-        DrawBarcode( $x_pos, $y_pos, $label_height, $label_width, $barcode,
+        DrawBarcode( $x_pos, $y_pos, $label_height, $label_width, $item->{'barcode'},
             $barcodetype );
         CalcNextLabelPos();
         drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
-        DrawSpineText( $y_pos, $label_height, $fontsize, $x_pos,
-            $left_text_margin, $text_wrap_cols, \$item, \$conf_data );
+        DrawSpineText( $x_pos, $y_pos, $label_height, $label_width, $fontname, $fontsize,
+            $left_text_margin, $text_wrap_cols, \$item, \$conf_data, $printingtype );
 
         CalcNextLabelPos();
     }
@@ -249,8 +253,29 @@ foreach $item (@resultsloop) {
 
     elsif ( $printingtype eq 'BIB' ) {
         drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
-        DrawSpineText( $y_pos, $label_height, $fontsize, $x_pos,
-            $left_text_margin, $text_wrap_cols, \$item, \$conf_data );
+        DrawSpineText( $x_pos, $y_pos, $label_height, $label_width, $fontname, $fontsize,
+            $left_text_margin, $text_wrap_cols, \$item, \$conf_data, $printingtype );
+        CalcNextLabelPos();
+    }
+
+    elsif ( $printingtype eq 'PATCRD' ) {
+        my $patron_data = $item;
+
+        #FIXME: This needs to be paramatized and passed in from the user...
+        #Each element of this hash is a separate line on the patron card. Keys are the text to print and the associated data is the point size.
+        my $text = {        
+            $patron_data->{'description'}  => $fontsize,
+            $patron_data->{'branchname'}   => ($fontsize + 3),
+        };
+
+        warn "Generating patron card for cardnumber $patron_data->{'cardnumber'}";
+
+        drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
+        my $barcode_height = $label_height / 2.75; #FIXME: Scaling barcode height; this needs to be a user parameter.
+        DrawBarcode( $x_pos, $y_pos, $barcode_height, $label_width, $patron_data->{'cardnumber'},
+            $barcodetype );
+        DrawPatronCardText( $x_pos, $y_pos, $label_height, $label_width, $fontname, $fontsize,
+            $left_text_margin, $text_wrap_cols, $text, $printingtype );
         CalcNextLabelPos();
     }