Bug 32482: (follow-up) Add markup comments
[koha.git] / catalogue / stockrotation.pl
1 #!/usr/bin/perl
2
3 # Copyright 2016 PTFS-Europe Ltd
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 =head1 stockrotation.pl
21
22  Script to manage item assignments to stock rotation rotas. Including their
23  assiciated stages
24
25 =cut
26
27 use Modern::Perl;
28 use CGI;
29
30 use C4::Auth qw( get_template_and_user );
31 use C4::Output qw( output_html_with_http_headers );
32 use C4::Search qw( enabled_staff_search_views );
33 use C4::Serials qw( CountSubscriptionFromBiblionumber );
34
35 use Koha::Biblio;
36 use Koha::Item;
37 use Koha::StockRotationRotas;
38 use Koha::StockRotationStages;
39 use Koha::Util::StockRotation qw( get_stages get_branches toggle_indemand remove_from_stage move_to_next_stage );
40
41 my $input = CGI->new;
42
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");
46     exit;
47 }
48
49 my %params = $input->Vars();
50
51 my $op = $params{op};
52
53 my $biblionumber = $input->param('biblionumber');
54
55 my ($template, $loggedinuser, $cookie) = get_template_and_user(
56     {
57         template_name   => 'catalogue/stockrotation.tt',
58         query           => $input,
59         type            => 'intranet',
60         flagsrequired   => {
61             catalogue => 1,
62             stockrotation => 'manage_rota_items',
63         },
64     }
65 );
66
67 if (!defined $op) {
68
69     # List all items along with their associated rotas
70     my $biblio = Koha::Biblios->find($biblionumber);
71
72     my $items = $biblio->items;
73
74     # Get only rotas with stages
75     my $rotas = Koha::StockRotationRotas->search(
76         {
77             'stockrotationstages.stage_id' => { '!=', undef }
78         },
79         {
80             join     => 'stockrotationstages',
81             collapse => 1,
82             order_by => 'title'
83         }
84     );
85
86     # Construct a model to pass to the view
87     my @item_data = ();
88
89     while (my $item = $items->next) {
90
91         my $item_hashref = {
92             bib_item   => $item
93         };
94
95         my $stockrotationitem = $item->stockrotationitem;
96
97         # If this item is on a rota
98         if ($stockrotationitem != 0) {
99
100             # This item's rota
101             my $rota = $stockrotationitem->stage->rota;
102
103             # This rota's stages
104             my $stages = get_stages($rota);
105
106             $item_hashref->{rota} = $rota;
107
108             $item_hashref->{stockrotationitem} = $stockrotationitem;
109
110             $item_hashref->{stages} = $stages;
111
112         }
113
114         push @item_data, $item_hashref;
115
116     }
117
118     $template->param(
119         no_op_set         => 1,
120         rotas             => $rotas,
121         items             => \@item_data,
122         branches          => get_branches(),
123         biblio            => $biblio,
124         biblionumber      => $biblio->biblionumber,
125         stockrotationview => 1,
126         subscriptionsnumber => CountSubscriptionFromBiblionumber($biblionumber),
127         C4::Search::enabled_staff_search_views
128     );
129
130 } elsif ($op eq "toggle_in_demand") {
131
132     # Toggle in demand
133     toggle_indemand($params{item_id}, $params{stage_id});
134
135     # Return to items list
136     print $input->redirect("?biblionumber=$biblionumber");
137
138 } elsif ($op eq "remove_item_from_stage") {
139
140     # Remove from the stage
141     remove_from_stage($params{item_id}, $params{stage_id});
142
143     # Return to items list
144     print $input->redirect("?biblionumber=$biblionumber");
145
146 } elsif ($op eq "move_to_next_stage") {
147
148     move_to_next_stage($params{item_id}, $params{stage_id});
149
150     # Return to items list
151     print $input->redirect("?biblionumber=" . $params{biblionumber});
152
153 } elsif ($op eq "add_item_to_rota") {
154
155     my $item = Koha::Items->find($params{item_id});
156
157     $item->add_to_rota($params{rota_id});
158
159     print $input->redirect("?biblionumber=" . $params{biblionumber});
160
161 } elsif ($op eq "confirm_remove_from_rota") {
162
163     $template->param(
164         op                => $params{op},
165         stage_id          => $params{stage_id},
166         item_id           => $params{item_id},
167         biblionumber      => $params{biblionumber},
168         stockrotationview => 1,
169         subscriptionsnumber => CountSubscriptionFromBiblionumber($biblionumber),
170         C4::Search::enabled_staff_search_views
171     );
172
173 }
174
175 output_html_with_http_headers $input, $cookie, $template->output;
176
177 =head1 AUTHOR
178
179 Andrew Isherwood <andrew.isherwood@ptfs-europe.com>
180
181 =cut