Bug 28606: Remove $DEBUG and $ENV{DEBUG}
[koha-ffzg.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) = @_;
101
102     # Get the item object
103     my $sr_item = Koha::StockRotationItems->find(
104         {
105             itemnumber_id => $item_id,
106         }
107     );
108
109     $sr_item->toggle_indemand;
110 }
111
112 =head2 move_to_next_stage
113
114     given an item's ID and stage ID, move it
115     to the next stage on the rota
116
117 =cut
118
119 sub move_to_next_stage {
120
121     my ($item_id, $stage_id) = shift;
122
123     # Get the item object
124     my $item = Koha::StockRotationItems->find(
125         {
126             itemnumber_id => $item_id,
127             stage_id      => $stage_id
128         }
129     );
130
131     $item->advance;
132
133 }
134
135 =head2 remove_from_stage
136
137     given an item's ID & stage ID, remove that item from that stage
138
139 =cut
140
141 sub remove_from_stage {
142
143     my ($item_id, $stage_id) = @_;
144
145     # Get the item object and delete it
146     Koha::StockRotationItems->find(
147         {
148             itemnumber_id => $item_id,
149             stage_id      => $stage_id
150         }
151     )->delete;
152
153 }
154
155 =head2 get_barcodes_status
156
157     take an arrayref of barcodes and a status hashref and populate it
158
159 =cut
160
161 sub get_barcodes_status {
162
163     my ($rota_id, $barcodes, $status) = @_;
164
165     # Get the items associated with these barcodes
166     my $items = Koha::Items->search(
167         {
168             barcode => { '-in' => $barcodes }
169         },
170         {
171             prefetch => 'stockrotationitem'
172         }
173     );
174     # Get an array of barcodes that were found
175     # Assign each barcode's status
176     my @found = ();
177     while (my $item = $items->next) {
178
179         push @found, $item->barcode if $item->barcode;
180
181         # Check if it's on a rota
182         my $on_rota = $item->stockrotationitem;
183
184         # It is on a rota
185         if ($on_rota) {
186
187             # Check if it's on this rota
188             if ($on_rota->stage->rota->rota_id == $rota_id) {
189
190                 # It's on this rota
191                 push @{$status->{on_this}}, $item;
192
193             } else {
194
195                 # It's on another rota
196                 push @{$status->{on_other}}, $item;
197
198             }
199
200         } else {
201
202             # Item is not on a rota
203             push @{$status->{ok}}, $item;
204
205         }
206
207     }
208
209     # Create an array of barcodes supplied in the file that
210     # were not found in the catalogue
211     my %found_in_cat = map{ $_ => 1 } @found;
212     push @{$status->{not_found}}, grep(
213         !defined $found_in_cat{$_}, @{$barcodes}
214     );
215
216 }
217
218 =head2 add_items_to_rota
219
220     take an arrayref of Koha::Item objects and add them to the passed rota
221
222 =cut
223
224 sub add_items_to_rota {
225
226     my ($rota_id, $items) = @_;
227
228     foreach my $item(@{$items}) {
229
230         $item->add_to_rota($rota_id);
231
232     }
233
234 }
235
236 1;
237
238 =head1 AUTHOR
239
240 Andrew Isherwood <andrew.isherwood@ptfs-europe.com>
241
242 =cut