3 # Copyright 2016 PTFS-Europe Ltd
5 # This file is part of Koha.
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 3 of the License, or (at your option) any later
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.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 =head1 stockrotation.pl
22 Script to handle stockrotation. Including rotas, their associated stages
35 use Koha
::StockRotationRotas
;
36 use Koha
::StockRotationItems
;
37 use Koha
::StockRotationStages
;
39 use Koha
::Util
::StockRotation
qw(:ALL);
43 unless (C4
::Context
->preference('StockRotation')) {
44 # redirect to Intranet home if self-check is not enabled
45 print $input->redirect("/cgi-bin/koha/mainpage.pl");
49 my ( $template, $loggedinuser, $cookie ) = get_template_and_user
(
51 template_name
=> 'tools/stockrotation.tt',
62 # Grab all passed data
63 # 'our' since Plack changes the scoping
65 our %params = $input->Vars();
71 # No operation is supplied, we're just displaying the list of rotas
72 my $rotas = Koha
::StockRotationRotas
->search(
75 order_by
=> { -asc
=> 'title' }
80 existing_rotas
=> $rotas,
84 } elsif ($op eq 'create_edit_rota') {
86 # Edit an existing rota or define a new one
87 my $rota_id = $params{rota_id
};
91 if (!defined $rota_id) {
93 # No ID supplied, we're creating a new rota
94 # Create a shell rota hashref
101 # ID supplied, we're editing an existing rota
102 $rota = Koha
::StockRotationRotas
->find($rota_id);
111 } elsif ($op eq 'toggle_rota') {
113 # Find and update the active status of the rota
114 my $rota = Koha
::StockRotationRotas
->find($params{rota_id
});
116 my $new_active = ($rota->active == 1) ?
0 : 1;
118 $rota->active($new_active)->store;
120 # Return to rotas page
121 print $input->redirect('stockrotation.pl');
123 } elsif ($op eq 'process_rota') {
125 # Get a hashref of the submitted rota data
126 my $rota = get_rota_from_form
();
128 if (!process_rota
($rota)) {
130 # The submitted rota was invalid
132 error
=> 'invalid_form',
134 op
=> 'create_edit_rota'
139 # All was well, return to the rotas list
140 print $input->redirect('stockrotation.pl');
144 } elsif ($op eq 'manage_stages') {
146 my $rota = Koha
::StockRotationRotas
->find($params{rota_id
});
150 branches
=> get_branches
(),
151 existing_stages
=> get_stages
($rota),
152 rota_id
=> $params{rota_id
},
156 } elsif ($op eq 'create_edit_stage') {
158 # Edit an existing stage or define a new one
159 my $stage_id = $params{stage_id
};
161 my $rota_id = $params{rota_id
};
163 if (!defined $stage_id) {
165 # No ID supplied, we're creating a new stage
167 branches
=> get_branches
(),
175 # ID supplied, we're editing an existing stage
176 my $stage = Koha
::StockRotationStages
->find($stage_id);
179 branches
=> get_branches
(),
181 rota_id
=> $stage->rota->rota_id,
187 } elsif ($op eq 'confirm_remove_from_rota') {
189 # Get the stage we're deleting
192 rota_id
=> $params{rota_id
},
193 stage_id
=> $params{stage_id
},
194 item_id
=> $params{item_id
}
197 } elsif ($op eq 'confirm_delete_stage') {
199 # Get the stage we're deleting
200 my $stage = Koha
::StockRotationStages
->find($params{stage_id
});
207 } elsif ($op eq 'delete_stage') {
209 # Get the stage we're deleting
210 my $stage = Koha
::StockRotationStages
->find($params{stage_id
});
212 # Get the ID of the rota with which this stage is associated
213 # (so we can return to the "Manage stages" page after deletion)
214 my $rota_id = $stage->rota->rota_id;
218 # Return to the stages list
219 print $input->redirect("?op=manage_stages&rota_id=$rota_id");
221 } elsif ($op eq 'process_stage') {
223 # Get a hashref of the submitted stage data
224 my $stage = get_stage_from_form
();
226 # The rota we're managing
227 my $rota_id = $params{rota_id
};
229 if (!process_stage
($stage, $rota_id)) {
231 # The submitted stage was invalid
233 my $branches = get_branches
();
236 error
=> 'invalid_form',
237 all_branches
=> $branches,
240 op
=> 'create_edit_stage'
245 # All was well, return to the stages list
246 print $input->redirect("?op=manage_stages&rota_id=$rota_id");
250 } elsif ($op eq 'manage_items') {
252 my $rota = Koha
::StockRotationRotas
->find($params{rota_id
});
254 # Get all items on this rota, for each prefetch their
255 # stage and biblio objects
256 my $items = Koha
::StockRotationItems
->search(
257 { 'stage.rota_id' => $params{rota_id
} },
261 'stockrotationitems' => {
262 'itemnumber' => 'biblionumber'
270 rota_id
=> $params{rota_id
},
271 error
=> $params{error
},
273 branches
=> get_branches
(),
274 stages
=> get_stages
($rota),
279 } elsif ($op eq 'move_to_next_stage') {
281 move_to_next_stage
($params{item_id
}, $params{stage_id
});
283 # Return to the items list
284 print $input->redirect("?op=manage_items&rota_id=" . $params{rota_id
});
286 } elsif ($op eq 'toggle_in_demand') {
288 # Toggle the item's in_demand
289 toggle_indemand
($params{item_id
}, $params{stage_id
});
291 # Return to the items list
292 print $input->redirect("?op=manage_items&rota_id=".$params{rota_id
});
294 } elsif ($op eq 'remove_item_from_stage') {
296 # Remove the item from the stage
297 remove_from_stage
($params{item_id
}, $params{stage_id
});
299 # Return to the items list
300 print $input->redirect("?op=manage_items&rota_id=".$params{rota_id
});
302 } elsif ($op eq 'add_items_to_rota') {
304 # The item's barcode,
305 # which we may or may not have been passed
306 my $barcode = $params{barcode
};
308 # The rota we're adding the item to
309 my $rota_id = $params{rota_id
};
311 # The uploaded file filehandle,
312 # which we may or may not have been passed
313 my $barcode_file = $input->upload("barcodefile");
315 # We need to create an array of one or more barcodes to
319 # If the barcode input box was populated, use it
320 push @barcodes, $barcode if $barcode;
322 # Only parse the uploaded file if necessary
325 # Call binmode on the filehandle as we want to set a
327 binmode($barcode_file, ":encoding(UTF-8)");
328 # Parse the file into an array of barcodes
329 while (my $barcode = <$barcode_file>) {
330 $barcode =~ s/\r/\n/g;
331 $barcode =~ s/\n+/\n/g;
332 my @data = split(/\n/, $barcode);
333 push @barcodes, @data;
338 # A hashref to hold the status of each barcode
339 my $barcode_status = {
346 # If we have something to work with, do it
347 get_barcodes_status
($rota_id, \
@barcodes, $barcode_status) if (@barcodes);
349 # Now we know the status of each barcode, add those that
351 if (scalar @
{$barcode_status->{ok
}} > 0) {
353 add_items_to_rota
($rota_id, $barcode_status->{ok
});
356 # If we were only passed one barcode and it was successfully
357 # added, redirect back to ourselves, we don't want to display
358 # a report, redirect also if we were passed no barcodes
360 scalar @barcodes == 0 ||
361 (scalar @barcodes == 1 && scalar @
{$barcode_status->{ok
}} == 1)
364 print $input->redirect("?op=manage_items&rota_id=$rota_id");
368 # Report on the outcome
370 barcode_status
=> $barcode_status,
377 } elsif ($op eq 'move_items_to_rota') {
379 # The barcodes of the items we're moving
380 my @move = $input->param('move_item');
382 foreach my $item(@move) {
384 # The item we're moving
385 my $item = Koha
::Items
->find($item);
387 # Move it to the new rota
388 $item->add_to_rota($params{rota_id
});
392 # Return to the items list
393 print $input->redirect("?op=manage_items&rota_id=".$params{rota_id
});
397 output_html_with_http_headers
$input, $cookie, $template->output;
399 sub get_rota_from_form
{
403 title
=> $params{title
},
404 cyclical
=> $params{cyclical
},
405 description
=> $params{description
}
409 sub get_stage_from_form
{
412 stage_id
=> $params{stage_id
},
413 branchcode
=> $params{branchcode
},
414 duration
=> $params{duration
}
420 my $sub_rota = shift;
423 my @required = ('title','cyclical');
425 # Count of the number of required fields we have
428 # Ensure we have everything we require
429 foreach my $req(@required) {
431 if (exists $sub_rota->{$req}) {
433 chomp(my $value = $sub_rota->{$req});
434 if (length $value > 0) {
442 # If we don't have everything we need
443 return 0 if $valid != scalar @required;
446 # Find the rota we're updating
447 my $rota = Koha
::StockRotationRotas
->find($sub_rota->{id
});
454 $sub_rota->{cyclical
}
456 $sub_rota->{description
}
461 $rota = Koha
::StockRotationRota
->new({
462 title
=> $sub_rota->{title
},
463 cyclical
=> $sub_rota->{cyclical
},
465 description
=> $sub_rota->{description
}
475 my ($sub_stage, $rota_id) = @_;
478 my @required = ('branchcode','duration');
480 # Count of the number of required fields we have
483 # Ensure we have everything we require
484 foreach my $req(@required) {
486 if (exists $sub_stage->{$req}) {
488 chomp(my $value = $sub_stage->{$req});
489 if (length $value > 0) {
497 # If we don't have everything we need
498 return 0 if $valid != scalar @required;
501 # Find the stage we're updating
502 my $stage = Koha
::StockRotationStages
->find($sub_stage->{stage_id
});
506 # Updating an existing stage
507 $stage->branchcode_id(
508 $sub_stage->{branchcode
}
510 $sub_stage->{duration
}
515 # Creating a new stage
516 $stage = Koha
::StockRotationStage
->new({
517 branchcode_id
=> $sub_stage->{branchcode
},
519 duration
=> $sub_stage->{duration
}
529 Andrew Isherwood <andrew.isherwood@ptfs-europe.com>