Improving patron search in serials routing
[koha.git] / C4 / Patroncards / Patroncards.pm
1 package C4::Labels::Patroncard;
2
3 # Copyright 2009 Foundations Bible College.
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;
21 use warnings;
22
23 use C4::Context;
24 use C4::Debug;
25 use C4::Biblio;
26 use C4::Labels::Layout 1.000000;        # use version 1.0.0 or better
27 use C4::Labels::Template 1.000000;
28 use Data::Dumper;
29
30 BEGIN {
31     use version; our $VERSION = qv('1.0.0_1');
32 }
33
34 sub _check_params {
35     my $given_params = {};
36     my $exit_code = 0;
37     my @valid_template_params = (
38         'layout_id',
39         'tmpl_id',
40         'prof_id',
41     );
42     if (scalar(@_) >1) {
43         $given_params = {@_};
44         foreach my $key (keys %{$given_params}) {
45             if (!(grep m/$key/, @valid_template_params)) {
46                 warn sprintf('Unrecognized parameter type of "%s".', $key);
47                 $exit_code = 1;
48             }
49         }
50     }
51     else {
52         if (!(grep m/$_/, @valid_template_params)) {
53             warn sprintf('Unrecognized parameter type of "%s".', $_);
54             $exit_code = 1;
55         }
56     }
57     return $exit_code;
58 }
59
60 =head1 NAME
61
62 C4::Labels::Batch - A class for creating and manipulating batch objects in Koha
63
64 =cut
65
66 =head1 METHODS
67
68 =head2 C4::Labels::Batch->new(layout_id => layout_id, tmpl_id => template_id, prof_id => prof_id)
69
70     Invoking the I<new> method constructs a new batch object with no items.
71
72     example:
73         my $batch = C4::Labels::Batch->new(layout_id => layout_id, tmpl_id => template_id, prof_id => prof_id);
74             # Creates and returns a new batch object
75
76     B<NOTE:> This batch is I<not> written to the database untill $batch->save() is invoked. You have been warned!
77
78 =cut
79
80 sub new {
81     my ($invocant, %params) = @_;
82     my $type = ref($invocant) || $invocant;
83     my $self = {
84         batch_id        => 0,
85         layout_id       => $params{layout_id},
86         tmpl_id         => $params{tmpl_id},
87         prof_id      => $params{prof_id},
88         items           => [],
89         batch_stat      => 0,   # False if any data has changed and the db has not been updated
90     };
91     bless ($self, $type);
92     return $self;
93 }
94
95 =head2 $batch->add_item($item_number)
96
97     Invoking the I<add_item> method will add the supplied item to the batch object.
98
99     example:
100         $batch->add_item($item_number);
101
102 =cut
103
104 sub add_item {
105     my $self = shift;
106     my $item_num = shift;
107     push (@{$self->{items}}, $item_num);
108     $self->{batch_stat} = 0;
109 }
110
111 =head2 $batch->get_attr()
112
113     Invoking the I<get_attr> method will return the requested attribute.
114
115     example:
116         my @items = $batch->get_attr($attr);
117
118 =cut
119
120 sub get_attr {
121     my $self = shift;
122     return $self->{$_[0]};
123 }
124
125 =head2 $batch->delete_item()
126
127     Invoking the I<delete_item> method will delete the supplied item from the batch object.
128
129     example:
130         $batch->delete_item();
131
132 =cut
133
134 sub delete_item {
135     my $self = shift;
136     my $item_num = shift;
137     my $index = 0;
138     ++$index until $$self->{items}[$index] == $item_num or $item_num > $#$self->{items};
139     delete ($$self->{items}[$index]);
140     $self->{batch_stat} = 0;
141 }
142
143 =head2 $batch->save()
144
145     Invoking the I<save> method attempts to insert the batch into the database if the batch is new and
146     update the existing batch record if the batch exists. The method returns the new record batch_id upon
147     success and -1 upon failure (This avoids conflicting with a record batch_id of 1). Errors are
148     logged to the Apache log.
149
150     example:
151         my $exitstat = $batch->save(); # to save the record behind the $batch object
152
153 =cut
154
155 sub save {
156     my $self = shift;
157     if ($self->{batch_id} > 0) {
158         foreach my $item_number (@$self->{items}) {
159             my $query = "UPDATE labels_batches SET item_number=?, layout_id=?, tmpl_id=?, prof_id=? WHERE batch_id=?;";
160             warn "DEBUG: Updating: $query\n" if $debug;
161             my $sth->C4::Context->dbh->prepare($query);
162             $sth->execute($item_number, $self->{layout_id}, $self->{tmpl_id}, $self->{prof_id}, $self->{batch_id});
163             if ($sth->err) {
164                 warn sprintf('Database returned the following error: %s', $sth->errstr);
165                 return -1;
166             }
167         }
168     }
169     else {
170         foreach my $item_number (@$self->{items}) {
171             my $query = "INSERT INTO labels_batches (item_number, layout_id, tmpl_id, prof_id) VALUES (?,?,?,?);";
172             warn "DEBUG: Inserting: $query\n" if $debug;
173             my $sth->C4::Context->dbh->prepare($query);
174             $sth->execute($item_number, $self->{layout_id}, $self->{tmpl_id}, $self->{prof_id});
175             if ($sth->err) {
176                 warn sprintf('Database returned the following error: %s', $sth->errstr);
177                 return -1;
178             }
179             my $sth1 = C4::Context->dbh->prepare("SELECT MAX(batch_id) FROM labels_batches;");
180             $sth1->execute();
181             my $batch_id = $sth1->fetchrow_array;
182             $self->{batch_id} = $batch_id;
183             return $batch_id;
184         }
185     }
186     $self->{batch_stat} = 1;
187 }
188
189 =head2 C4::Labels::Template->retrieve(template_id)
190
191     Invoking the I<retrieve> method constructs a new template object containing the current values for template_id. The method returns
192     a new object upon success and 1 upon failure. Errors are logged to the Apache log. Two further options may be accessed. See the example
193     below for further description.
194
195     examples:
196
197         my $template = C4::Labels::Template->retrieve(template_id => 1); # Retrieves template record 1 and returns an object containing the record
198
199         my $template = C4::Labels::Template->retrieve(template_id => 1, convert => 1); # Retrieves template record 1, converts the units to points,
200             and returns an object containing the record
201
202         my $template = C4::Labels::Template->retrieve(template_id => 1, prof_id => prof_id); # Retrieves template record 1, converts the units
203             to points, applies the given profile id, and returns an object containing the record
204
205 =cut
206
207 sub retrieve {
208     my $invocant = shift;
209     my %opts = @_;
210     my $type = ref($invocant) || $invocant;
211     my $query = "SELECT * FROM labels_batches WHERE batch_id = ? ORDER BY label_id";  
212     my $sth = C4::Context->dbh->prepare($query);
213     $sth->execute($opts{batch_id});
214     if ($sth->err) {
215         warn sprintf('Database returned the following error: %s', $sth->errstr);
216         return 1;
217     }
218     my $self = {
219         items   => [],
220     };
221     while (my $record = $sth->fetchrow_hashref) {
222         $self->{batch_id} = $record->{batch_id};        # FIXME: seems a bit wasteful to re-initialize these every trip: is there a better way?
223         $self->{layout_id} = $record->{layout_id};
224         $self->{tmpl_id} = $record->{tmpl_id};
225         $self->{prof_id} = $record->{prof_id};
226         push (@{$self->{items}}, $record->{item_number});
227     }
228     $self->{batch_stat} = 1;
229     bless ($self, $type);
230     return $self;
231 }
232
233 =head2 C4::Labels::Batch->delete(batch_id => batch_id) |  $batch->delete()
234
235     Invoking the delete method attempts to delete the batch from the database. The method returns 0 upon success
236     and 1 upon failure. Errors are logged to the Apache log.
237
238     examples:
239         my $exitstat = $batch->delete(); # to delete the record behind the $batch object
240         my $exitstat = C4::Labels::Batch->delete(batch_id => 1); # to delete batch record 1
241
242 =cut
243
244 sub delete {
245     my $self = shift;
246     my %opts = @_;
247     if ((ref $self) && !$self->{'batch_id'}) {   # If there is no batch batch_id then we cannot delete it from the db
248         warn 'Cannot delete batch: Batch has not been saved.';
249         return 1;
250     }
251     elsif (!$opts{batch_id}) {
252         warn 'Cannot delete batch: Missing batch_id.';
253         return 1;
254     }
255     my $query = "DELETE FROM labels_batches WHERE batch_id = ?";
256     my $sth = C4::Context->dbh->prepare($query);
257     $sth->execute($self->{'batch_id'});
258     return 0;
259 }
260
261
262 1;
263 __END__
264
265 =head1 AUTHOR
266
267 Chris Nighswonger <cnighswonger AT foundations DOT edu>
268
269 =cut
270
271
272 #
273 #    elsif ( $label_type eq 'PATCRD' ) {
274 #        my $patron_data = $item;
275 #
276 #        #FIXME: This needs to be paramatized and passed in from the user...
277 #        #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.
278 #        my $text = {        
279 #            $patron_data->{'description'}  => $template->get_attr('font_size'),
280 #            $patron_data->{'branchname'}   => ($template->get_attr('font_size') + 3),
281 #        };
282 #
283 #        $debug and warn "Generating patron card for cardnumber $patron_data->{'cardnumber'}";
284 #
285 #        my $barcode_height = $label_height / 2.75; #FIXME: Scaling barcode height; this needs to be a user parameter.
286 #        $label->barcode( $llx, $lly, $barcode_height, $label_width, $patron_data->{'cardnumber'},
287 #            $barcodetype );
288 #        DrawPatronCardText( $llx, $lly, $label_height, $label_width, $template->get_attr('font'), $template->get_attr('font_size'),
289 #            $left_text_margin, $text_wrap_cols, $text, $label_type );
290 #        _calc_next_label_pos();
291 #    }
292