Filter:   InfoImg
download barcodesGenerator.pl
Language: Perl
License: GPL
LOC: 248
Project Info
Koha Library Automation Package(koha)
Server: Savannah NonGNU
Type: cvs
...\k\koha\koha\koha\barcodes\
   barcodes.pl
   barcodesGenerator.pl
   label-home.pl
   label-item-search.pl
   label-manager.pl
   label-print-opus-pdf.pl
   label-print-pdf.pl
   label-print.pl
   pdfViewer.pl
   printerConfig.pl
   test.textblock.pl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#!/usr/bin/perl

# script to generate items barcodes
# written 07/04
# by Veleda Matias - matias_veleda@hotmail.com - Physics Library UNLP Argentina and
#    Castaeda Sebastian - seba3c@yahoo.com.ar - Physics Library UNLP Argentina and

# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA  02111-1307 USA

require Exporter;

use strict;

use CGI;
use C4::Context;
use C4::Output;


#FIXME : module deprecated ?
use PDF::API2;
use PDF::API2::Page;
use PDF::API2::Util;
use C4::Barcodes::PrinterConfig;
use Time::localtime;

# This function returns the path to deal with the correct files, considering
# templates set and language.
sub getPath {
    my $type         = shift @_;
    my $templatesSet = C4::Context->preference('template');
    my $lang         = C4::Context->preference('opaclanguages');
    if ( $type eq "intranet" ) {
        return "$ENV{'DOCUMENT_ROOT'}/intranet-tmpl/$templatesSet/$lang";
    }
    else {
        return "$ENV{'DOCUMENT_ROOT'}/opac-tmpl/$templatesSet/$lang";
    }
}

# Load a configuration file. Before use this function, check if that file exists.
sub loadConfFromFile {
    my $fileName = shift @_;
    my %keyValues;
    open FILE, "<$fileName";
    while (<FILE>) {
        chomp;
        if (/\s*([\w_]*)\s*=\s*([\[\]\<\>\w_\s:@,\.-]*)\s*/) {
            $keyValues{$1} = $2;
        }
    }
    close FILE;
    return %keyValues;
}

# Save settings to a configuration file. It delete previous configuration settings.
sub saveConfToFile {
    my $fileName  = shift @_;
    my %keyValues = %{ shift @_ };
    my $i;
    open FILE, ">$fileName";
    foreach $i ( keys(%keyValues) ) {
        print FILE $i . " = " . $keyValues{$i} . "\n";
    }
    close FILE;
}

# Load the config file.
my $filenameConf =
  &getPath("intranet") . "/includes/labelConfig/itemsLabelConfig.conf";
my %labelConfig = &loadConfFromFile($filenameConf);

# Creates a CGI object and take its parameters
my $cgi              = new CGI;
my $from             = $cgi->param('from');
my $to               = $cgi->param('to');
my $individualCodes  = $cgi->param('individualCodes');
my $rangeType        = $cgi->param('rangeType');
my $pageType         = $cgi->param('pages');
my $label            = $cgi->param('label');
my $numbersystem     = $cgi->param('numbersystem');
my $text_under_label = $cgi->param('text_under_label');

# Generate the checksum from an inventary code
sub checksum {

    sub calculateDigit {
        my $code       = shift @_;
        my $sum        = 0;
        my $odd_parity = 1;
        my $i;
        for ( $i = length($code) - 1 ; $i >= 0 ; $i-- ) {
            if ($odd_parity) {
                $sum = $sum + ( 3 * substr( $code, $i, 1 ) );
            }
            else {
                $sum = $sum + substr( $code, $i, 1 );
            }
            $odd_parity = !$odd_parity;
        }
        my $check_digit = 10 - ( $sum % 10 );
        if ( $check_digit == 10 ) {
            $check_digit = 0;
        }
        return $code . $check_digit;
    }

    my $currentCode = shift @_;
    $currentCode = &calculateDigit($currentCode);
    return $currentCode;
}

# Assigns a temporary name to the PDF file
sub assingFilename {
    my ( $from, $to ) = @_;
    my $ip      = $cgi->remote_addr();
    my $random  = int( rand(1000000) );
    my $timeObj = localtime();
    my ( $day, $month, $year, $hour, $min, $sec ) = (
        $timeObj->mday,
        $timeObj->mon + 1,
        $timeObj->year + 1900,
        $timeObj->hour, $timeObj->min, $timeObj->sec
    );
    my $tmpFileName =
        $random . '-' . $ip
      . '-(From '
      . $from . ' to '
      . $to . ')-['
      . $day . '.'
      . $month . '.'
      . $year . ']-['
      . $hour . ':'
      . $min . ':'
      . $sec . '].pdf';
    return $tmpFileName;
}

sub getCallnum {

    #grabs a callnumber for the specified barcode
    my ($barcode) = @_;
    my $query =
"select dewey from items,biblioitems where items.biblionumber=biblioitems.biblionumber and items.barcode=?";
    my $dbh = C4::Context->dbh;
    my $sth = $dbh->prepare($query);
    $sth->execute($barcode);
    my ($callnum) = $sth->fetchrow_array();
    warn "Call number is:" . $barcode;
    return $callnum;
}

# Takes inventary codes from database and if they are between
# the interval specify by parameters, it generates the correspond barcodes
sub barcodesGenerator {
    my ( $from, $to, $rangeType, $individualCodes, $text_under_label ) = @_;

    # Returns a database handler
    my $dbh = C4::Context->dbh;

    # Create the query to database
    # Assigns a temporary filename for the pdf file
    my $tmpFileName = &assingFilename( $from, $to );

    # warn "range type: ".$rangeType;
    if ( $rangeType eq 'continuous' ) {

        # Set the temp directory for pdfs files
        if ( !defined( $ENV{'TEMP'} ) ) {
            $ENV{'TEMP'} = '/tmp/';
        }
        $tmpFileName = $ENV{'TEMP'} . $tmpFileName;

        # Creates a PDF object
        my $pdf = PDF::API2->new( -file => $tmpFileName );

        # Set the positions where barcodes are going to be placed
        C4::Barcodes::PrinterConfig::setPositionsForX(
            $labelConfig{'marginLeft'}, $labelConfig{'labelWidth'},
            $labelConfig{'columns'},    $labelConfig{'pageType'}
        );
        C4::Barcodes::PrinterConfig::setPositionsForY(
            $labelConfig{'marginBottom'}, $labelConfig{'labelHeigth'},
            $labelConfig{'rows'},         $labelConfig{'pageType'}
        );

        # Creates a font object
        my $tr = $pdf->corefont('Helvetica-Bold');

        # Barcode position
        my ( $page, $gfx, $text );
        for ( my $code = $from ; $code <= $to ; $code++ ) {

            # Generetase checksum
            my $codeC = &checksum($code);

            # Generate the corresponde barcode to $code
            # warn "Code is :-->".$codeC."<--";
            my $barcode = $pdf->barcode(
                -font => $tr,         # The font object to use
                -type => 'ean128',    # Standard of codification
                -code => $codeC,      # Text to codify
                -extn => '012345',    # Barcode extension (if it is aplicable)
                -umzn => 10,          # Top limit of the finished bar
                -lmzn => 10,          # Bottom limit of the finished bar
                -zone => 15,          # Bars size
                -quzn => 0,           # Space destinated for legend
                -ofwt => 0.01,        # Bars width
                -fnsz => 8,           # Font size
                -text => ''
            );

            ( my $x, my $y, $pdf, $page, $gfx, $text, $tr, $label ) =
              C4::Barcodes::PrinterConfig::getLabelPosition( $label, $pdf,
                $page, $gfx, $text, $tr, $pageType );

            # Assigns a barcodes to $gfx
            $gfx->barcode( $barcode, $x, $y,
                ( 72 / $labelConfig{'systemDpi'} ) );

            # Assigns the additional information to the barcode (Legend)
            $text->translate( $x - 48, $y - 22 );

            #warn "code is ".$codeC;
            if ($text_under_label) {
                $text->text($text_under_label);
            }
            else {
                $text->text( getCallnum($code) );
            }
        }

        # Writes the objects added in $gfx to $page
        $pdf->finishobjects( $page, $gfx, $text );

        # Save changes to the PDF
        $pdf->saveas;

        # Close the conection with the PDF file
        $pdf->end;

        # Show the PDF file
        print $cgi->redirect(
            "/cgi-bin/koha/barcodes/pdfViewer.pl?tmpFileName=$tmpFileName");
    }
    else {
        my $rangeCondition;
        if ( $individualCodes ne "" ) {
            $rangeCondition = "AND (I.barcode IN " . $individualCodes . ")";
        }
        else {
            $rangeCondition =
              "AND (I.barcode >= " . $from . " AND I.barcode <=" . $to . " )";
        }

        my $query =
"SELECT CONCAT('$numbersystem',REPEAT('0',((12 - LENGTH('$numbersystem')) - LENGTH(I.barcode))), I.barcode) AS Codigo, B.title, B.author FROM biblio B, items I WHERE (I.biblionumber = B.biblioNumber ) "
          . $rangeCondition
          . " AND (I.barcode <> 'FALTA') ORDER BY Codigo";

        # Prepare the query
        my $sth = $dbh->prepare($query);

        # Executes the query
        $sth->execute;
        if ( $sth->rows ) {    # There are inventary codes
                               # Set the temp directory for pdfs files
            if ( !defined( $ENV{'TEMP'} ) ) {
                $ENV{'TEMP'} = '/tmp/';
            }

            # Assigns a temporary filename for the pdf file
            my $tmpFileName = &assingFilename( $from, $to );
            $tmpFileName = $ENV{'TEMP'} . $tmpFileName;

            # Creates a PDF object
            my $pdf = PDF::API2->new( -file => $tmpFileName );

            # Set the positions where barcodes are going to be placed
            C4::Barcodes::PrinterConfig::setPositionsForX(
                $labelConfig{'marginLeft'}, $labelConfig{'labelWidth'},
                $labelConfig{'columns'},    $labelConfig{'pageType'}
            );
            C4::Barcodes::PrinterConfig::setPositionsForY(
                $labelConfig{'marginBottom'}, $labelConfig{'labelHeigth'},
                $labelConfig{'rows'},         $labelConfig{'pageType'}
            );

            # Creates a font object
            my $tr = $pdf->corefont('Helvetica-Bold');

            # Barcode position
            my ( $page, $gfx, $text );
            while ( my ( $code, $dewey, $title, $author ) =
                $sth->fetchrow_array )
            {

                # Generetase checksum
                $code = &checksum($code);

                # Generate the corresponde barcode to $code
                my $barcode = $pdf->barcode(
                    -font => $tr,       # The font object to use
                    -type => 'ean13',   # Standard of codification
                    -code => $code,     # Text to codify
                    -extn => '012345',  # Barcode extension (if it is aplicable)
                    -umzn => 10,        # Top limit of the finished bar
                    -lmzn => 10,        # Bottom limit of the finished bar
                    -zone => 15,        # Bars size
                    -quzn => 0,         # Space destinated for legend
                    -ofwt => 0.01,      # Bars width
                    -fnsz => 8,         # Font size
                    -text => ''
                );

                ( my $x, my $y, $pdf, $page, $gfx, $text, $tr, $label ) =
                  C4::Barcodes::PrinterConfig::getLabelPosition( $label, $pdf,
                    $page, $gfx, $text, $tr, $pageType );

                # Assigns a barcodes to $gfx
                $gfx->barcode( $barcode, $x, $y,
                    ( 72 / $labelConfig{'systemDpi'} ) );

                # Assigns the additional information to the barcode (Legend)
                $text->translate( $x - 48, $y - 22 );
                if ($text_under_label) {
                    $text->text($text_under_label);
                }
                else {
                    $text->text( substr $title, 0, 30 );
                    $text->translate( $x - 48, $y - 29 );

                    #$text->text(substr $author, 0, 30);
                    $text->text( substr $author, 0, 30 );
                }
            }

            # Writes the objects added in $gfx to $page
            $pdf->finishobjects( $page, $gfx, $text );

            # Save changes to the PDF
            $pdf->saveas;

            # Close the conection with the PDF file
            $pdf->end;

            # Show the PDF file
            print $cgi->redirect(
                "/cgi-bin/koha/barcodes/pdfViewer.pl?tmpFileName=$tmpFileName");
        }
        else {

            # Rollback and shows the error legend
            print $cgi->redirect("/cgi-bin/koha/barcodes/barcodes.pl?error=1");
        }
        $sth->finish;
    }
}

barcodesGenerator( $from, $to, $rangeType, $individualCodes,
    $text_under_label );