Bug 24545: Fix license statements
[koha.git] / Koha / Util / StockRotation.pm
1 package Koha::Util::StockRotation;
2
3 # Module contains subroutines used with Stock Rotation
4 #
5 # Copyright 2016 PTFS-Europe Ltd
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21
22 use Modern::Perl;
23
24 use Koha::Items;
25 use Koha::StockRotationItems;
26 use Koha::Database;
27
28 our ( @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS );
29 BEGIN {
30     require Exporter;
31     @ISA = qw( Exporter );
32     @EXPORT = qw( );
33     @EXPORT_OK = qw(
34         get_branches
35         get_stages
36         toggle_indemand
37         remove_from_stage
38         get_barcodes_status
39         add_items_to_rota
40         move_to_next_stage
41     );
42     %EXPORT_TAGS = ( ALL => [ @EXPORT_OK, @EXPORT ] );
43 }
44
45 =head1 NAME
46
47 Koha::Util::StockRotation - utility class with routines for Stock Rotation
48
49 =head1 FUNCTIONS
50
51 =head2 get_branches
52
53     returns all branches ordered by branchname as an array, each element
54     contains a hashref containing branch details
55
56 =cut
57
58 sub get_branches {
59
60     return Koha::Libraries->search(
61         {},
62         { order_by => ['branchname'] }
63     )->unblessed;
64
65 }
66
67 =head2 get_stages
68
69     returns an arrayref of StockRotationStage objects representing
70     all stages for a passed rota
71
72 =cut
73
74 sub get_stages {
75
76     my $rota = shift;
77
78     my @out = ();
79
80     if ($rota->stockrotationstages->count > 0) {
81
82         push @out, $rota->first_stage->unblessed;
83
84         push @out, @{$rota->first_stage->siblings->unblessed};
85
86     }
87
88     return \@out;
89 }
90
91 =head2 toggle_indemand
92
93     given an item's ID & stage ID toggle that item's in_demand
94     status on that stage
95
96 =cut
97
98 sub toggle_indemand {
99
100     my ($item_id, $stage_id) = @_;
101
102     # Get the item object
103     my $item = Koha::StockRotationItems->find(
104         {
105             itemnumber_id => $item_id,
106             stage_id      => $stage_id
107         }
108     );
109
110     # Toggle the item's indemand flag
111     my $new_indemand = ($item->indemand == 1) ? 0 : 1;
112
113     $item->indemand($new_indemand)->store;
114
115 }
116
117 =head2 move_to_next_stage
118
119     given an item's ID and stage ID, move it
120     to the next stage on the rota
121
122 =cut
123
124 sub move_to_next_stage {
125
126     my ($item_id, $stage_id) = shift;
127
128     # Get the item object
129     my $item = Koha::StockRotationItems->find(
130         {
131             itemnumber_id => $item_id,
132             stage_id      => $stage_id
133         }
134     );
135
136     $item->advance;
137
138 }
139
140 =head2 remove_from_stage
141
142     given an item's ID & stage ID, remove that item from that stage
143
144 =cut
145
146 sub remove_from_stage {
147
148     my ($item_id, $stage_id) = @_;
149
150     # Get the item object and delete it
151     Koha::StockRotationItems->find(
152         {
153             itemnumber_id => $item_id,
154             stage_id      => $stage_id
155         }
156     )->delete;
157
158 }
159
160 =head2 get_barcodes_status
161
162     take an arrayref of barcodes and a status hashref and populate it
163
164 =cut
165
166 sub get_barcodes_status {
167
168     my ($rota_id, $barcodes, $status) = @_;
169
170     # Get the items associated with these barcodes
171     my $items = Koha::Items->search(
172         {
173             barcode => { '-in' => $barcodes }
174         },
175         {
176             prefetch => 'stockrotationitem'
177         }
178     );
179     # Get an array of barcodes that were found
180     # Assign each barcode's status
181     my @found = ();
182     while (my $item = $items->next) {
183
184         push @found, $item->barcode if $item->barcode;
185
186         # Check if it's on a rota
187         my $on_rota = $item->stockrotationitem;
188
189         # It is on a rota
190         if ($on_rota) {
191
192             # Check if it's on this rota
193             if ($on_rota->stage->rota->rota_id == $rota_id) {
194
195                 # It's on this rota
196                 push @{$status->{on_this}}, $item;
197
198             } else {
199
200                 # It's on another rota
201                 push @{$status->{on_other}}, $item;
202
203             }
204
205         } else {
206
207             # Item is not on a rota
208             push @{$status->{ok}}, $item;
209
210         }
211
212     }
213
214     # Create an array of barcodes supplied in the file that
215     # were not found in the catalogue
216     my %found_in_cat = map{ $_ => 1 } @found;
217     push @{$status->{not_found}}, grep(
218         !defined $found_in_cat{$_}, @{$barcodes}
219     );
220
221 }
222
223 =head2 add_items_to_rota
224
225     take an arrayref of Koha::Item objects and add them to the passed rota
226
227 =cut
228
229 sub add_items_to_rota {
230
231     my ($rota_id, $items) = @_;
232
233     foreach my $item(@{$items}) {
234
235         $item->add_to_rota($rota_id);
236
237     }
238
239 }
240
241 1;
242
243 =head1 AUTHOR
244
245 Andrew Isherwood <andrew.isherwood@ptfs-europe.com>
246
247 =cut