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
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 =head1 stockrotation.pl
22 Script to handle stockrotation. Including rotas, their associated stages
30 use C4::Auth qw( get_template_and_user );
32 use C4::Output qw( output_html_with_http_headers );
35 use Koha::StockRotationRotas;
36 use Koha::StockRotationItems;
37 use Koha::StockRotationStages;
39 use Koha::Util::StockRotation qw( get_branches get_stages move_to_next_stage toggle_indemand remove_from_stage add_items_to_rota get_barcodes_status );
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',
61 # Grab all passed data
62 # 'our' since Plack changes the scoping
64 our %params = $input->Vars();
70 # No operation is supplied, we're just displaying the list of rotas
71 my $rotas = Koha::StockRotationRotas->search(
74 order_by => { -asc => 'title' }
79 existing_rotas => $rotas,
83 } elsif ($op eq 'create_edit_rota') {
85 # Edit an existing rota or define a new one
86 my $rota_id = $params{rota_id};
90 if (!defined $rota_id) {
92 # No ID supplied, we're creating a new rota
93 # Create a shell rota hashref
100 # ID supplied, we're editing an existing rota
101 $rota = Koha::StockRotationRotas->find($rota_id);
110 } elsif ($op eq 'toggle_rota') {
112 # Find and update the active status of the rota
113 my $rota = Koha::StockRotationRotas->find($params{rota_id});
115 my $new_active = ($rota->active == 1) ? 0 : 1;
117 $rota->active($new_active)->store;
119 # Return to rotas page
120 print $input->redirect('stockrotation.pl');
122 } elsif ($op eq 'process_rota') {
124 # Get a hashref of the submitted rota data
125 my $rota = get_rota_from_form();
127 if (!process_rota($rota)) {
129 # The submitted rota was invalid
131 error => 'invalid_form',
133 op => 'create_edit_rota'
138 # All was well, return to the rotas list
139 print $input->redirect('stockrotation.pl');
143 } elsif ($op eq 'manage_stages') {
145 my $rota = Koha::StockRotationRotas->find($params{rota_id});
149 branches => get_branches(),
150 existing_stages => get_stages($rota),
151 rota_id => $params{rota_id},
155 } elsif ($op eq 'create_edit_stage') {
157 # Edit an existing stage or define a new one
158 my $stage_id = $params{stage_id};
160 my $rota_id = $params{rota_id};
162 if (!defined $stage_id) {
164 # No ID supplied, we're creating a new stage
166 branches => get_branches(),
174 # ID supplied, we're editing an existing stage
175 my $stage = Koha::StockRotationStages->find($stage_id);
178 branches => get_branches(),
180 rota_id => $stage->rota->rota_id,
186 } elsif ($op eq 'confirm_remove_from_rota') {
188 # Get the stage we're deleting
191 rota_id => $params{rota_id},
192 stage_id => $params{stage_id},
193 item_id => $params{item_id}
196 } elsif ($op eq 'confirm_delete_stage') {
198 # Get the stage we're deleting
199 my $stage = Koha::StockRotationStages->find($params{stage_id});
206 } elsif ($op eq 'delete_stage') {
208 # Get the stage we're deleting
209 my $stage = Koha::StockRotationStages->find($params{stage_id});
211 # Get the ID of the rota with which this stage is associated
212 # (so we can return to the "Manage stages" page after deletion)
213 my $rota_id = $stage->rota->rota_id;
217 # Return to the stages list
218 print $input->redirect("?op=manage_stages&rota_id=$rota_id");
220 } elsif ($op eq 'process_stage') {
222 # Get a hashref of the submitted stage data
223 my $stage = get_stage_from_form();
225 # The rota we're managing
226 my $rota_id = $params{rota_id};
228 if (!process_stage($stage, $rota_id)) {
230 # The submitted stage was invalid
232 my $branches = get_branches();
235 error => 'invalid_form',
236 all_branches => $branches,
239 op => 'create_edit_stage'
244 # All was well, return to the stages list
245 print $input->redirect("?op=manage_stages&rota_id=$rota_id");
249 } elsif ($op eq 'manage_items') {
251 my $rota = Koha::StockRotationRotas->find($params{rota_id});
253 # Get all items on this rota, for each prefetch their
254 # stage and biblio objects
255 my $sritems = Koha::StockRotationItems->search(
256 { 'stage.rota_id' => $params{rota_id} },
260 'stockrotationitems' => {
261 'itemnumber' => 'biblionumber'
269 rota_id => $params{rota_id},
270 error => $params{error},
272 branches => get_branches(),
273 stages => get_stages($rota),
278 } elsif ($op eq 'move_to_next_stage') {
280 move_to_next_stage($params{item_id}, $params{stage_id});
282 # Return to the items list
283 print $input->redirect("?op=manage_items&rota_id=" . $params{rota_id});
285 } elsif ($op eq 'toggle_in_demand') {
287 # Toggle the item's in_demand
288 toggle_indemand($params{item_id}, $params{stage_id});
290 # Return to the items list
291 print $input->redirect("?op=manage_items&rota_id=".$params{rota_id});
293 } elsif ($op eq 'remove_item_from_stage') {
295 # Remove the item from the stage
296 remove_from_stage($params{item_id}, $params{stage_id});
298 # Return to the items list
299 print $input->redirect("?op=manage_items&rota_id=".$params{rota_id});
301 } elsif ($op eq 'add_items_to_rota') {
303 # The item's barcode,
304 # which we may or may not have been passed
305 my $barcode = $params{barcode};
307 # The rota we're adding the item to
308 my $rota_id = $params{rota_id};
310 # The uploaded file filehandle,
311 # which we may or may not have been passed
312 my $barcode_file = $input->upload("barcodefile");
314 # We need to create an array of one or more barcodes to
318 # If the barcode input box was populated, use it
319 push @barcodes, $barcode if $barcode;
321 # Only parse the uploaded file if necessary
324 # Call binmode on the filehandle as we want to set a
326 binmode($barcode_file, ":encoding(UTF-8)");
327 # Parse the file into an array of barcodes
328 while (my $barcode = <$barcode_file>) {
329 $barcode =~ s/\r/\n/g;
330 $barcode =~ s/\n+/\n/g;
331 my @data = split(/\n/, $barcode);
332 push @barcodes, @data;
337 # A hashref to hold the status of each barcode
338 my $barcode_status = {
345 # If we have something to work with, do it
346 get_barcodes_status($rota_id, \@barcodes, $barcode_status) if (@barcodes);
348 # Now we know the status of each barcode, add those that
350 if (scalar @{$barcode_status->{ok}} > 0) {
352 add_items_to_rota($rota_id, $barcode_status->{ok});
355 # If we were only passed one barcode and it was successfully
356 # added, redirect back to ourselves, we don't want to display
357 # a report, redirect also if we were passed no barcodes
359 scalar @barcodes == 0 ||
360 (scalar @barcodes == 1 && scalar @{$barcode_status->{ok}} == 1)
363 print $input->redirect("?op=manage_items&rota_id=$rota_id");
367 # Report on the outcome
369 barcode_status => $barcode_status,
376 } elsif ($op eq 'move_items_to_rota') {
378 # The barcodes of the items we're moving
379 my @move = $input->multi_param('move_item');
381 foreach my $item(@move) {
383 # The item we're moving
384 my $item = Koha::Items->find($item);
386 # Move it to the new rota
387 $item->add_to_rota($params{rota_id});
391 # Return to the items list
392 print $input->redirect("?op=manage_items&rota_id=".$params{rota_id});
396 output_html_with_http_headers $input, $cookie, $template->output;
398 sub get_rota_from_form {
402 title => $params{title},
403 cyclical => $params{cyclical},
404 description => $params{description}
408 sub get_stage_from_form {
411 stage_id => $params{stage_id},
412 branchcode => $params{branchcode},
413 duration => $params{duration}
419 my $sub_rota = shift;
422 my @required = ('title','cyclical');
424 # Count of the number of required fields we have
427 # Ensure we have everything we require
428 foreach my $req(@required) {
430 if (exists $sub_rota->{$req}) {
432 chomp(my $value = $sub_rota->{$req});
433 if (length $value > 0) {
441 # If we don't have everything we need
442 return 0 if $valid != scalar @required;
445 # Find the rota we're updating
446 my $rota = Koha::StockRotationRotas->find($sub_rota->{id});
453 $sub_rota->{cyclical}
455 $sub_rota->{description}
460 $rota = Koha::StockRotationRota->new({
461 title => $sub_rota->{title},
462 cyclical => $sub_rota->{cyclical},
464 description => $sub_rota->{description}
474 my ($sub_stage, $rota_id) = @_;
477 my @required = ('branchcode','duration');
479 # Count of the number of required fields we have
482 # Ensure we have everything we require
483 foreach my $req(@required) {
485 if (exists $sub_stage->{$req}) {
487 chomp(my $value = $sub_stage->{$req});
488 if (length $value > 0) {
496 # If we don't have everything we need
497 return 0 if $valid != scalar @required;
500 # Find the stage we're updating
501 my $stage = Koha::StockRotationStages->find($sub_stage->{stage_id});
505 # Updating an existing stage
506 $stage->branchcode_id(
507 $sub_stage->{branchcode}
509 $sub_stage->{duration}
514 # Creating a new stage
515 $stage = Koha::StockRotationStage->new({
516 branchcode_id => $sub_stage->{branchcode},
518 duration => $sub_stage->{duration}
528 Andrew Isherwood <andrew.isherwood@ptfs-europe.com>