Bug 26265: (QA follow-up) Remove g option from regex, add few dirs
[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 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
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
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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;
31 use C4::Output;
32 use C4::Search;
33 use C4::Serials;
34
35 use Koha::Biblio;
36 use Koha::Item;
37 use Koha::StockRotationRotas;
38 use Koha::StockRotationStages;
39 use Koha::Util::StockRotation qw(:ALL);
40
41 my $input = new CGI;
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         authnotrequired => 0,
61         flagsrequired   => {
62             catalogue => 1,
63             stockrotation => 'manage_rota_items',
64         },
65     }
66 );
67
68 if (!defined $op) {
69
70     # List all items along with their associated rotas
71     my $biblio = Koha::Biblios->find($biblionumber);
72
73     my $items = $biblio->items;
74
75     # Get only rotas with stages
76     my $rotas = Koha::StockRotationRotas->search(
77         {
78             'stockrotationstages.stage_id' => { '!=', undef }
79         },
80         {
81             join     => 'stockrotationstages',
82             collapse => 1,
83             order_by => 'title'
84         }
85     );
86
87     # Construct a model to pass to the view
88     my @item_data = ();
89
90     while (my $item = $items->next) {
91
92         my $item_hashref = {
93             bib_item   => $item
94         };
95
96         my $stockrotationitem = $item->stockrotationitem;
97
98         # If this item is on a rota
99         if ($stockrotationitem != 0) {
100
101             # This item's rota
102             my $rota = $stockrotationitem->stage->rota;
103
104             # This rota's stages
105             my $stages = get_stages($rota);
106
107             $item_hashref->{rota} = $rota;
108
109             $item_hashref->{stockrotationitem} = $stockrotationitem;
110
111             $item_hashref->{stages} = $stages;
112
113         }
114
115         push @item_data, $item_hashref;
116
117     }
118
119     $template->param(
120         no_op_set         => 1,
121         rotas             => $rotas,
122         items             => \@item_data,
123         branches          => get_branches(),
124         biblio            => $biblio,
125         biblionumber      => $biblio->biblionumber,
126         stockrotationview => 1,
127         subscriptionsnumber => CountSubscriptionFromBiblionumber($biblionumber),
128         C4::Search::enabled_staff_search_views
129     );
130
131 } elsif ($op eq "toggle_in_demand") {
132
133     # Toggle in demand
134     toggle_indemand($params{item_id}, $params{stage_id});
135
136     # Return to items list
137     print $input->redirect("?biblionumber=$biblionumber");
138
139 } elsif ($op eq "remove_item_from_stage") {
140
141     # Remove from the stage
142     remove_from_stage($params{item_id}, $params{stage_id});
143
144     # Return to items list
145     print $input->redirect("?biblionumber=$biblionumber");
146
147 } elsif ($op eq "move_to_next_stage") {
148
149     move_to_next_stage($params{item_id}, $params{stage_id});
150
151     # Return to items list
152     print $input->redirect("?biblionumber=" . $params{biblionumber});
153
154 } elsif ($op eq "add_item_to_rota") {
155
156     my $item = Koha::Items->find($params{item_id});
157
158     $item->add_to_rota($params{rota_id});
159
160     print $input->redirect("?biblionumber=" . $params{biblionumber});
161
162 } elsif ($op eq "confirm_remove_from_rota") {
163
164     $template->param(
165         op                => $params{op},
166         stage_id          => $params{stage_id},
167         item_id           => $params{item_id},
168         biblionumber      => $params{biblionumber},
169         stockrotationview => 1,
170         subscriptionsnumber => CountSubscriptionFromBiblionumber($biblionumber),
171         C4::Search::enabled_staff_search_views
172     );
173
174 }
175
176 output_html_with_http_headers $input, $cookie, $template->output;
177
178 =head1 AUTHOR
179
180 Andrew Isherwood <andrew.isherwood@ptfs-europe.com>
181
182 =cut