[20/40] Work on label printing code.
[koha.git] / C4 / Labels / Batch.pm
1 package C4::Labels::Batch;
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 Sys::Syslog qw(syslog);
24
25 use C4::Context;
26 use C4::Debug;
27 use Data::Dumper;
28
29 BEGIN {
30     use version; our $VERSION = qv('1.0.0_1');
31 }
32
33 sub _check_params {
34     my $given_params = {};
35     my $exit_code = 0;
36     my @valid_template_params = (
37         'label_id',
38         'batch_id',
39         'item_number',
40         'branch_code',
41     );
42     if (scalar(@_) >1) {
43         $given_params = {@_};
44         foreach my $key (keys %{$given_params}) {
45             if (!(grep m/$key/, @valid_template_params)) {
46                 syslog("LOG_ERR", "C4::Labels::Batch : Unrecognized parameter type of \"%s\".", $key);
47                 $exit_code = 1;
48             }
49         }
50     }
51     else {
52         if (!(grep m/$_/, @valid_template_params)) {
53             syslog("LOG_ERR", "C4::Labels::Batch : 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, template_id => template_id, profile_id => profile_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, template_id => template_id, profile_id => profile_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) = shift;
82     my $type = ref($invocant) || $invocant;
83     my $self = {
84         batch_id        => 0,
85         items           => [],
86         branch_code     => 'NB',
87         batch_stat      => 0,   # False if any data has changed and the db has not been updated
88         @_,
89     };
90     my $sth = C4::Context->dbh->prepare("SELECT MAX(batch_id) FROM labels_batches;");
91     $sth->execute();
92     my $batch_id = $sth->fetchrow_array;
93     $self->{'batch_id'} = ++$batch_id;
94     bless ($self, $type);
95     return $self;
96 }
97
98 =head2 $batch->add_item(item_number => $item_number, branch_code => $branch_code)
99
100     Invoking the I<add_item> method will add the supplied item to the batch object.
101
102     example:
103         $batch->add_item(item_number => $item_number, branch_code => $branch_code);
104
105 =cut
106
107 sub add_item {
108     my $self = shift;
109     my $item_number = shift;
110     my $query = "INSERT INTO labels_batches (batch_id, item_number, branch_code) VALUES (?,?,?);";
111     my $sth = C4::Context->dbh->prepare($query);
112 #    $sth->{'TraceLevel'} = 3;
113     $sth->execute($self->{'batch_id'}, $item_number, $self->{'branch_code'});
114     if ($sth->err) {
115         syslog("LOG_ERR", "C4::Labels::Batch->add_item : Database returned the following error on attempted INSERT: %s", $sth->errstr);
116         return -1;
117     }
118     $query = "SELECT max(label_id) FROM labels_batches WHERE batch_id=? AND item_number=? AND branch_code=?;";
119     my $sth1 = C4::Context->dbh->prepare($query);
120     $sth1->execute($self->{'batch_id'}, $item_number, $self->{'branch_code'});
121     my $label_id = $sth1->fetchrow_array;
122     push (@{$self->{'items'}}, {item_number => $item_number, label_id => $label_id});
123     $self->{'batch_stat'} = 0;
124     return 0;
125 }
126
127 =head2 $batch->get_attr()
128
129     Invoking the I<get_attr> method will return the requested attribute.
130
131     example:
132         my @items = $batch->get_attr($attr);
133
134 =cut
135
136 sub get_attr {
137     my $self = shift;
138     return $self->{$_[0]};
139 }
140
141 =head2 $batch->remove_item()
142
143     Invoking the I<remove_item> method will remove the supplied item from the batch object.
144
145     example:
146         $batch->remove_item();
147
148 =cut
149
150 sub remove_item {
151     my $self = shift;
152     my $label_id = shift;
153     my $query = "DELETE FROM labels_batches WHERE label_id=? AND batch_id=?;";
154     my $sth = C4::Context->dbh->prepare($query);
155 #    $sth->{'TraceLevel'} = 3;
156     $sth->execute($label_id, $self->{'batch_id'});
157     if ($sth->err) {
158         syslog("LOG_ERR", "C4::Labels::Batch->remove_item : Database returned the following error on attempted DELETE: %s", $sth->errstr);
159         return -1;
160     }
161     @{$self->{'items'}} = grep{$_->{'label_id'} != $label_id} @{$self->{'items'}};
162     $self->{'batch_stat'} = 1;
163     return 0;
164 }
165
166 =head2 $batch->save()
167
168     Invoking the I<save> method attempts to insert the batch into the database. The method returns
169     the new record batch_id upon success and -1 upon failure (This avoids conflicting with a record
170     batch_id of 1). Errors are logged to the syslog.
171
172     example:
173         my $exitstat = $batch->save(); # to save the record behind the $batch object
174
175 =cut
176
177 sub save {
178     my $self = shift;
179     foreach my $item_number (@{$self->{'items'}}) {
180         my $query = "INSERT INTO labels_batches (batch_id, item_number, branch_code) VALUES (?,?,?);";
181         my $sth1 = C4::Context->dbh->prepare($query);
182         $sth1->execute($self->{'batch_id'}, $item_number->{'item_number'}, $self->{'branch_code'});
183         if ($sth1->err) {
184             syslog("LOG_ERR", "C4::Labels::Batch->save : Database returned the following error on attempted INSERT: %s", $sth1->errstr);
185             return -1;
186         }
187         $self->{'batch_stat'} = 1;
188         return $self->{'batch_id'};
189     }
190 }
191
192 =head2 C4::Labels::Batch->retrieve(batch_id)
193
194     Invoking the I<retrieve> method constructs a new batch object containing the current values for batch_id. The method returns
195     a new object upon success and 1 upon failure. Errors are logged to the syslog.
196
197     examples:
198
199         my $batch = C4::Labels::Batch->retrieve(batch_id => 1); # Retrieves batch record 1 and returns an object containing the record
200
201 =cut
202
203 sub retrieve {
204     my $invocant = shift;
205     my %opts = @_;
206     my $type = ref($invocant) || $invocant;
207     my $record_flag = 0;
208     my $query = "SELECT * FROM labels_batches WHERE batch_id = ? ORDER BY label_id";  
209     my $sth = C4::Context->dbh->prepare($query);
210 #    $sth->{'TraceLevel'} = 3;
211     $sth->execute($opts{'batch_id'});
212     my $self = {
213         batch_id        => $opts{'batch_id'},
214         items           => [],
215     };
216     while (my $record = $sth->fetchrow_hashref) {
217         $self->{'branch_code'} = $record->{'branch_code'};
218         push (@{$self->{'items'}}, {item_number => $record->{'item_number'}, label_id => $record->{'label_id'}});
219         $record_flag = 1;       # true if one or more rows were retrieved
220     }
221     return -2 if $record_flag == 0;     # a hackish sort of way of indicating no such record exists
222     if ($sth->err) {
223         syslog("LOG_ERR", "C4::Labels::Batch->retrieve : Database returned the following error on attempted SELECT: %s", $sth->errstr);
224         return -1;
225     }
226     $self->{'batch_stat'} = 1;
227     bless ($self, $type);
228     return $self;
229 }
230
231 =head2 C4::Labels::Batch->delete(batch_id => batch_id) | $batch->delete()
232
233     Invoking the delete method attempts to delete the batch from the database. The method returns 0 upon success
234     and 1 upon failure. Errors are logged to the syslog.
235
236     examples:
237         my $exitstat = $batch->delete(); # to delete the record behind the $batch object
238         my $exitstat = C4::Labels::Batch->delete(batch_id => 1); # to delete batch record 1
239
240 =cut
241
242 sub delete {
243     my $self = {};
244     my %opts = ();
245     my $call_type = '';
246     my @query_params = ();
247     if (ref($_[0])) {
248         $self = shift;  # check to see if this is a method call
249         $call_type = 'C4::Labels::Batch->delete';
250         @query_params = ($self->{'batch_id'}, $self->{'branch_code'});
251     }
252     else {
253         %opts = @_;
254         $call_type = 'C4::Labels::Batch::delete';
255         @query_params = ($opts{'batch_id'}, $opts{'branch_code'});
256     }
257     if ($query_params[0] eq '') {   # If there is no template id then we cannot delete it
258         syslog("LOG_ERR", "%s : Cannot delete batch as the batch id is invalid or non-existant.", $call_type);
259         return -1;
260     }
261     my $query = "DELETE FROM labels_batches WHERE batch_id = ? AND branch_code =?";
262     my $sth = C4::Context->dbh->prepare($query);
263 #    $sth->{'TraceLevel'} = 3;
264     $sth->execute(@query_params);
265     if ($sth->err) {
266         syslog("LOG_ERR", "%s : Database returned the following error on attempted INSERT: %s", $call_type, $sth->errstr);
267         return -1;
268     }
269     return 0;
270 }
271
272
273 1;
274 __END__
275
276 =head1 AUTHOR
277
278 Chris Nighswonger <cnighswonger AT foundations DOT edu>
279
280 =cut
281